xref: /illumos-gate/usr/src/lib/libldap5/sources/ldap/common/unescape.c (revision c65ebfc7045424bd04a6c7719a27b0ad3399ad54)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 /*
4  * The contents of this file are subject to the Netscape Public
5  * License Version 1.1 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.mozilla.org/NPL/
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * The Original Code is Mozilla Communicator client code, released
15  * March 31, 1998.
16  *
17  * The Initial Developer of the Original Code is Netscape
18  * Communications Corporation. Portions created by Netscape are
19  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
20  * Rights Reserved.
21  *
22  * Contributor(s):
23  */
24 
25 /*
26  *  LIBLDAP unescape.c -- LDAP URL un-escape routines
27  *  We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
28  */
29 
30 #include "ldap-int.h"
31 
32 
33 static int unhex( char c );
34 
35 
36 void
37 nsldapi_hex_unescape( char *s )
38 {
39 /*
40  * Remove URL hex escapes from s... done in place.  The basic concept for
41  * this routine is borrowed from the WWW library HTUnEscape() routine.
42  */
43 	char	*p;
44 
45 	for ( p = s; *s != '\0'; ++s ) {
46 		if ( *s == '%' ) {
47 			if ( *++s == '\0' ) {
48 				break;
49 			}
50 			*p = unhex( *s ) << 4;
51 			if ( *++s == '\0' ) {
52 				break;
53 			}
54 			*p++ += unhex( *s );
55 
56 		} else {
57 			*p++ = *s;
58 		}
59 	}
60 
61 	*p = '\0';
62 }
63 
64 
65 static int
66 unhex( char c )
67 {
68 	return( c >= '0' && c <= '9' ? c - '0'
69 	    : c >= 'A' && c <= 'F' ? c - 'A' + 10
70 	    : c - 'a' + 10 );
71 }
72