3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
10 * LIBLDAP url.c -- LDAP URL (RFC 2255) related routines
12 * LDAP URLs look like this:
13 * ldap[is]://host:port[/[dn[?[attributes][?[scope][?[filter][?exts]]]]]]
16 * attributes is a comma separated list
17 * scope is one of these three strings: base one sub (default=base)
18 * filter is an string-represented filter as in RFC 2254
20 * e.g., ldap://host:port/dc=com?o,cn?base?o=openldap?extension
22 * We also tolerate URLs that look like: <ldapurl> and <URL:ldapurl>
29 #include <ac/stdlib.h>
31 #include <ac/socket.h>
32 #include <ac/string.h>
39 static const char* skip_url_prefix LDAP_P((
42 const char **scheme ));
44 int ldap_pvt_url_scheme2proto( const char *scheme )
48 if( scheme == NULL ) {
52 if( strcmp("ldap", scheme) == 0 ) {
53 return LDAP_PROTO_TCP;
56 if( strcmp("ldapi", scheme) == 0 ) {
57 return LDAP_PROTO_IPC;
60 if( strcmp("ldaps", scheme) == 0 ) {
61 return LDAP_PROTO_TCP;
67 LDAP_F(int) ldap_pvt_url_scheme2tls( const char *scheme )
71 if( scheme == NULL ) {
75 return strcmp("ldaps", scheme) == 0;
79 ldap_is_ldap_url( LDAP_CONST char *url )
88 if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
96 ldap_is_ldaps_url( LDAP_CONST char *url )
105 if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
109 return strcmp(scheme, "ldaps") == 0;
113 ldap_is_ldapi_url( LDAP_CONST char *url )
122 if( skip_url_prefix( url, &enclosed, &scheme ) == NULL ) {
126 return strcmp(scheme, "ldapi") == 0;
133 const char **scheme )
136 * return non-zero if this looks like a LDAP URL; zero if not
137 * if non-zero returned, *urlp will be moved past "ldap://" part of URL
147 /* skip leading '<' (if any) */
155 /* skip leading "URL:" (if any) */
156 if ( strncasecmp( p, LDAP_URL_URLCOLON, LDAP_URL_URLCOLON_LEN ) == 0 ) {
157 p += LDAP_URL_URLCOLON_LEN;
160 /* check for "ldap://" prefix */
161 if ( strncasecmp( p, LDAP_URL_PREFIX, LDAP_URL_PREFIX_LEN ) == 0 ) {
162 /* skip over "ldap://" prefix and return success */
163 p += LDAP_URL_PREFIX_LEN;
168 /* check for "ldaps://" prefix */
169 if ( strncasecmp( p, LDAPS_URL_PREFIX, LDAPS_URL_PREFIX_LEN ) == 0 ) {
170 /* skip over "ldaps://" prefix and return success */
171 p += LDAPS_URL_PREFIX_LEN;
176 /* check for "ldapi://" prefix */
177 if ( strncasecmp( p, LDAPI_URL_PREFIX, LDAPI_URL_PREFIX_LEN ) == 0 ) {
178 /* skip over "ldapi://" prefix and return success */
179 p += LDAPI_URL_PREFIX_LEN;
188 static int str2scope( const char *p )
190 if ( strcasecmp( p, "one" ) == 0 ) {
191 return LDAP_SCOPE_ONELEVEL;
193 } else if ( strcasecmp( p, "onetree" ) == 0 ) {
194 return LDAP_SCOPE_ONELEVEL;
196 } else if ( strcasecmp( p, "base" ) == 0 ) {
197 return LDAP_SCOPE_BASE;
199 } else if ( strcasecmp( p, "sub" ) == 0 ) {
200 return LDAP_SCOPE_SUBTREE;
202 } else if ( strcasecmp( p, "subtree" ) == 0 ) {
203 return LDAP_SCOPE_SUBTREE;
209 static int hex_escape( char *buf, const char *s, int list )
213 static const char hex[] = "0123456789ABCDEF";
215 if( s == NULL ) return 0;
217 for( pos=0,i=0; s[i]; i++ ) {
243 escape = s[i] < 0x20 || 0x1f >= s[i];
248 buf[pos++] = hex[ (s[i] >> 4) & 0x0f ];
249 buf[pos++] = hex[ s[i] & 0x0f ];
258 static int hex_escape_args( char *buf, char **s )
263 if( s == NULL ) return 0;
266 for( i=0; s[i] != NULL; i++ ) {
270 pos += hex_escape( &buf[pos], s[i], 1 );
276 char * ldap_url_desc2str( LDAPURLDesc *u )
283 if( u == NULL ) return NULL;
286 for( i=0; u->lud_exts[i]; i++ ) {
287 len += strlen( u->lud_exts[i] ) + 1;
292 if( u->lud_filter ) {
293 len += strlen( u->lud_filter );
296 if ( len ) len++; /* ? */
298 switch( u->lud_scope ) {
299 case LDAP_SCOPE_ONELEVEL:
300 case LDAP_SCOPE_SUBTREE:
301 case LDAP_SCOPE_BASE:
302 len += sizeof("base");
307 if ( len ) len++; /* ? */
311 for( i=0; u->lud_attrs[i]; i++ ) {
312 len += strlen( u->lud_attrs[i] ) + 1;
315 } else if ( len ) len++; /* ? */
318 len += strlen( u->lud_dn ) + 1;
327 len+=strlen( u->lud_host );
330 len += strlen( u->lud_scheme ) + sizeof("://");
332 /* allocate enough to hex escape everything -- overkill */
333 s = LDAP_MALLOC( 3*len );
335 if( s == NULL ) return NULL;
338 sprintf( s, "%s://%s:%d%n", u->lud_scheme,
339 u->lud_host, u->lud_port, &sofar );
341 sprintf( s, "%s://%s%n", u->lud_scheme,
342 u->lud_host, &sofar );
345 if( sep < 1 ) goto done;
348 sofar += hex_escape( &s[sofar], u->lud_dn, 0 );
350 if( sep < 2 ) goto done;
353 sofar += hex_escape_args( &s[sofar], u->lud_attrs );
355 if( sep < 3 ) goto done;
358 switch( u->lud_scope ) {
359 case LDAP_SCOPE_BASE:
360 strcpy( &s[sofar], "base" );
361 sofar += sizeof("base") - 1;
363 case LDAP_SCOPE_ONELEVEL:
364 strcpy( &s[sofar], "one" );
365 sofar += sizeof("one") - 1;
367 case LDAP_SCOPE_SUBTREE:
368 strcpy( &s[sofar], "sub" );
369 sofar += sizeof("sub") - 1;
373 if( sep < 4 ) goto done;
376 sofar += hex_escape( &s[sofar], u->lud_filter, 0 );
378 if( sep < 5 ) goto done;
381 sofar += hex_escape_args( &s[sofar], u->lud_exts );
389 ldap_url_parse_ext( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
392 * Pick apart the pieces of an LDAP URL.
398 const char *scheme = NULL;
402 if( url_in == NULL || ludpp == NULL ) {
403 return LDAP_URL_ERR_PARAM;
406 #ifndef LDAP_INT_IN_KERNEL
407 /* Global options may not be created yet
408 * We can't test if the global options are initialized
409 * because a call to LDAP_INT_GLOBAL_OPT() will try to allocate
410 * the options and cause infinite recursion
412 Debug( LDAP_DEBUG_TRACE, "ldap_url_parse_ext(%s)\n", url_in, 0, 0 );
415 *ludpp = NULL; /* pessimistic */
417 url_tmp = skip_url_prefix( url_in, &enclosed, &scheme );
419 if ( url_tmp == NULL ) {
420 return LDAP_URL_ERR_BADSCHEME;
425 /* make working copy of the remainder of the URL */
426 url = LDAP_STRDUP( url_tmp );
428 return LDAP_URL_ERR_MEM;
432 p = &url[strlen(url)-1];
436 return LDAP_URL_ERR_BADENCLOSURE;
442 /* allocate return struct */
443 ludp = (LDAPURLDesc *)LDAP_CALLOC( 1, sizeof( LDAPURLDesc ));
445 if ( ludp == NULL ) {
447 return LDAP_URL_ERR_MEM;
450 ludp->lud_next = NULL;
451 ludp->lud_host = NULL;
454 ludp->lud_attrs = NULL;
455 ludp->lud_filter = NULL;
456 ludp->lud_scope = LDAP_SCOPE_DEFAULT;
457 ludp->lud_filter = NULL;
458 ludp->lud_exts = NULL;
460 ludp->lud_scheme = LDAP_STRDUP( scheme );
462 if ( ludp->lud_scheme == NULL ) {
464 ldap_free_urldesc( ludp );
465 return LDAP_URL_ERR_MEM;
468 /* scan forward for '/' that marks end of hostport and begin. of dn */
469 p = strchr( url, '/' );
472 /* terminate hostport; point to start of dn */
476 /* IPv6 syntax with [ip address]:port */
478 r = strchr( url, ']' );
481 ldap_free_urldesc( ludp );
482 return LDAP_URL_ERR_BADURL;
485 q = strchr( r, ':' );
487 q = strchr( url, ':' );
492 ldap_pvt_hex_unescape( q );
496 ldap_free_urldesc( ludp );
497 return LDAP_URL_ERR_BADURL;
500 ludp->lud_port = atoi( q );
503 ldap_pvt_hex_unescape( url );
505 /* If [ip address]:port syntax, url is [ip and we skip the [ */
506 ludp->lud_host = LDAP_STRDUP( url + ( *url == '[' ) );
508 if( ludp->lud_host == NULL ) {
510 ldap_free_urldesc( ludp );
511 return LDAP_URL_ERR_MEM;
515 * Kludge. ldap://111.222.333.444:389??cn=abc,o=company
517 * On early Novell releases, search references/referrals were returned
518 * in this format, i.e., the dn was kind of in the scope position,
519 * but the required slash is missing. The whole thing is illegal syntax,
520 * but we need to account for it. Fortunately it can't be confused with
523 if( (p == NULL) && (q != NULL) && ((q = strchr( q, '?')) != NULL)) {
525 /* ? immediately followed by question */
530 ldap_pvt_hex_unescape( q );
531 ludp->lud_dn = LDAP_STRDUP( q );
533 ludp->lud_dn = LDAP_STRDUP( "" );
536 if( ludp->lud_dn == NULL ) {
538 ldap_free_urldesc( ludp );
539 return LDAP_URL_ERR_MEM;
547 return LDAP_URL_SUCCESS;
550 /* scan forward for '?' that may marks end of dn */
551 q = strchr( p, '?' );
554 /* terminate dn part */
560 ldap_pvt_hex_unescape( p );
561 ludp->lud_dn = LDAP_STRDUP( p );
563 ludp->lud_dn = LDAP_STRDUP( "" );
566 if( ludp->lud_dn == NULL ) {
568 ldap_free_urldesc( ludp );
569 return LDAP_URL_ERR_MEM;
576 return LDAP_URL_SUCCESS;
579 /* scan forward for '?' that may marks end of attributes */
581 q = strchr( p, '?' );
584 /* terminate attributes part */
589 /* parse attributes */
590 ldap_pvt_hex_unescape( p );
591 ludp->lud_attrs = ldap_str2charray( p, "," );
593 if( ludp->lud_attrs == NULL ) {
595 ldap_free_urldesc( ludp );
596 return LDAP_URL_ERR_BADATTRS;
604 return LDAP_URL_SUCCESS;
607 /* scan forward for '?' that may marks end of scope */
609 q = strchr( p, '?' );
612 /* terminate the scope part */
617 /* parse the scope */
618 ldap_pvt_hex_unescape( p );
619 ludp->lud_scope = str2scope( p );
621 if( ludp->lud_scope == -1 ) {
623 ldap_free_urldesc( ludp );
624 return LDAP_URL_ERR_BADSCOPE;
632 return LDAP_URL_SUCCESS;
635 /* scan forward for '?' that may marks end of filter */
637 q = strchr( p, '?' );
640 /* terminate the filter part */
645 /* parse the filter */
646 ldap_pvt_hex_unescape( p );
651 ldap_free_urldesc( ludp );
652 return LDAP_URL_ERR_BADFILTER;
655 LDAP_FREE( ludp->lud_filter );
656 ludp->lud_filter = LDAP_STRDUP( p );
658 if( ludp->lud_filter == NULL ) {
660 ldap_free_urldesc( ludp );
661 return LDAP_URL_ERR_MEM;
669 return LDAP_URL_SUCCESS;
672 /* scan forward for '?' that may marks end of extensions */
674 q = strchr( p, '?' );
679 ldap_free_urldesc( ludp );
680 return LDAP_URL_ERR_BADURL;
683 /* parse the extensions */
684 ludp->lud_exts = ldap_str2charray( p, "," );
686 if( ludp->lud_exts == NULL ) {
688 ldap_free_urldesc( ludp );
689 return LDAP_URL_ERR_BADEXTS;
692 for( i=0; ludp->lud_exts[i] != NULL; i++ ) {
693 ldap_pvt_hex_unescape( ludp->lud_exts[i] );
695 if( *ludp->lud_exts[i] == '!' ) {
696 /* count the number of critical extensions */
697 ludp->lud_crit_exts++;
702 /* must have 1 or more */
704 ldap_free_urldesc( ludp );
705 return LDAP_URL_ERR_BADEXTS;
711 return LDAP_URL_SUCCESS;
715 ldap_url_parse( LDAP_CONST char *url_in, LDAPURLDesc **ludpp )
717 int rc = ldap_url_parse_ext( url_in, ludpp );
719 if( rc != LDAP_URL_SUCCESS ) {
723 if ((*ludpp)->lud_scope == LDAP_SCOPE_DEFAULT) {
724 (*ludpp)->lud_scope = LDAP_SCOPE_BASE;
727 if ((*ludpp)->lud_host != NULL && *(*ludpp)->lud_host == '\0') {
728 LDAP_FREE( (*ludpp)->lud_host );
729 (*ludpp)->lud_host = NULL;
732 if ((*ludpp)->lud_port == 0) {
733 if( strcmp((*ludpp)->lud_scheme, "ldap") == 0 ) {
734 (*ludpp)->lud_port = LDAP_PORT;
735 } else if( strcmp((*ludpp)->lud_scheme, "ldaps") == 0 ) {
736 (*ludpp)->lud_port = LDAPS_PORT;
744 ldap_url_dup ( LDAPURLDesc *ludp )
748 if ( ludp == NULL ) {
752 dest = LDAP_MALLOC( sizeof(LDAPURLDesc) );
757 dest->lud_scheme = NULL;
758 dest->lud_host = NULL;
760 dest->lud_filter = NULL;
761 dest->lud_attrs = NULL;
762 dest->lud_exts = NULL;
763 dest->lud_next = NULL;
765 if ( ludp->lud_scheme != NULL ) {
766 dest->lud_scheme = LDAP_STRDUP( ludp->lud_scheme );
767 if (dest->lud_scheme == NULL) {
768 ldap_free_urldesc(dest);
773 if ( ludp->lud_host != NULL ) {
774 dest->lud_host = LDAP_STRDUP( ludp->lud_host );
775 if (dest->lud_host == NULL) {
776 ldap_free_urldesc(dest);
781 if ( ludp->lud_dn != NULL ) {
782 dest->lud_dn = LDAP_STRDUP( ludp->lud_dn );
783 if (dest->lud_dn == NULL) {
784 ldap_free_urldesc(dest);
789 if ( ludp->lud_filter != NULL ) {
790 dest->lud_filter = LDAP_STRDUP( ludp->lud_filter );
791 if (dest->lud_filter == NULL) {
792 ldap_free_urldesc(dest);
797 if ( ludp->lud_attrs != NULL ) {
798 dest->lud_attrs = ldap_charray_dup( ludp->lud_attrs );
799 if (dest->lud_attrs == NULL) {
800 ldap_free_urldesc(dest);
805 if ( ludp->lud_exts != NULL ) {
806 dest->lud_exts = ldap_charray_dup( ludp->lud_exts );
807 if (dest->lud_exts == NULL) {
808 ldap_free_urldesc(dest);
817 ldap_url_duplist (LDAPURLDesc *ludlist)
819 LDAPURLDesc *dest, *tail, *ludp, *newludp;
823 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
824 newludp = ldap_url_dup(ludp);
825 if (newludp == NULL) {
826 ldap_free_urllist(dest);
832 tail->lud_next = newludp;
839 ldap_url_parselist (LDAPURLDesc **ludlist, const char *url )
848 return LDAP_PARAM_ERROR;
850 urls = ldap_str2charray((char *)url, ", ");
852 return LDAP_NO_MEMORY;
854 /* count the URLs... */
855 for (i = 0; urls[i] != NULL; i++) ;
856 /* ...and put them in the "stack" backward */
858 rc = ldap_url_parse( urls[i], &ludp );
860 ldap_charray_free(urls);
861 ldap_free_urllist(*ludlist);
865 ludp->lud_next = *ludlist;
868 ldap_charray_free(urls);
874 LDAPURLDesc **ludlist,
885 return LDAP_PARAM_ERROR;
887 specs = ldap_str2charray((char *)hosts, ", ");
889 return LDAP_NO_MEMORY;
891 /* count the URLs... */
892 for (i = 0; specs[i] != NULL; i++) /* EMPTY */;
894 /* ...and put them in the "stack" backward */
896 ludp = LDAP_CALLOC( 1, sizeof(LDAPURLDesc) );
898 ldap_charray_free(specs);
899 ldap_free_urllist(*ludlist);
901 return LDAP_NO_MEMORY;
903 ludp->lud_port = port;
904 ludp->lud_host = specs[i];
906 p = strchr(ludp->lud_host, ':');
908 /* more than one :, IPv6 address */
909 if ( strchr(p+1, ':') != NULL ) {
910 /* allow [address] and [address]:port */
911 if ( *ludp->lud_host == '[' ) {
912 p = LDAP_STRDUP(ludp->lud_host+1);
913 /* copied, make sure we free source later */
914 specs[i] = ludp->lud_host;
916 p = strchr( ludp->lud_host, ']' );
918 return LDAP_PARAM_ERROR;
922 return LDAP_PARAM_ERROR;
931 ldap_pvt_hex_unescape(p);
932 ludp->lud_port = atoi(p);
935 ldap_pvt_hex_unescape(ludp->lud_host);
936 ludp->lud_scheme = LDAP_STRDUP("ldap");
937 ludp->lud_next = *ludlist;
941 /* this should be an array of NULLs now */
942 /* except entries starting with [ */
943 ldap_charray_free(specs);
948 ldap_url_list2hosts (LDAPURLDesc *ludlist)
952 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
957 /* figure out how big the string is */
958 size = 1; /* nul-term */
959 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
960 size += strlen(ludp->lud_host) + 1; /* host and space */
961 if (strchr(ludp->lud_host, ':')) /* will add [ ] below */
963 if (ludp->lud_port != 0)
964 size += sprintf(buf, ":%d", ludp->lud_port);
966 s = LDAP_MALLOC(size);
971 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
972 if (strchr(ludp->lud_host, ':')) {
973 p += sprintf(p, "[%s]", ludp->lud_host);
975 strcpy(p, ludp->lud_host);
976 p += strlen(ludp->lud_host);
978 if (ludp->lud_port != 0)
979 p += sprintf(p, ":%d", ludp->lud_port);
983 p--; /* nuke that extra space */
990 LDAPURLDesc *ludlist )
994 char *s, *p, buf[32]; /* big enough to hold a long decimal # (overkill) */
999 /* figure out how big the string is */
1000 size = 1; /* nul-term */
1001 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1002 size += strlen(ludp->lud_scheme) + strlen(ludp->lud_host);
1003 if (strchr(ludp->lud_host, ':')) /* will add [ ] below */
1005 size += sizeof(":/// ");
1007 if (ludp->lud_port != 0) {
1008 size += sprintf(buf, ":%d", ludp->lud_port);
1012 s = LDAP_MALLOC(size);
1018 for (ludp = ludlist; ludp != NULL; ludp = ludp->lud_next) {
1020 strchr(ludp->lud_host, ':') ? "%s://[%s]" : "%s://%s",
1021 ludp->lud_scheme, ludp->lud_host);
1022 if (ludp->lud_port != 0)
1023 p += sprintf(p, ":%d", ludp->lud_port);
1028 p--; /* nuke that extra space */
1034 ldap_free_urllist( LDAPURLDesc *ludlist )
1036 LDAPURLDesc *ludp, *next;
1038 for (ludp = ludlist; ludp != NULL; ludp = next) {
1039 next = ludp->lud_next;
1040 ldap_free_urldesc(ludp);
1045 ldap_free_urldesc( LDAPURLDesc *ludp )
1047 if ( ludp == NULL ) {
1051 if ( ludp->lud_scheme != NULL ) {
1052 LDAP_FREE( ludp->lud_scheme );
1055 if ( ludp->lud_host != NULL ) {
1056 LDAP_FREE( ludp->lud_host );
1059 if ( ludp->lud_dn != NULL ) {
1060 LDAP_FREE( ludp->lud_dn );
1063 if ( ludp->lud_filter != NULL ) {
1064 LDAP_FREE( ludp->lud_filter);
1067 if ( ludp->lud_attrs != NULL ) {
1068 LDAP_VFREE( ludp->lud_attrs );
1071 if ( ludp->lud_exts != NULL ) {
1072 LDAP_VFREE( ludp->lud_exts );
1081 ldap_url_search( LDAP *ld, LDAP_CONST char *url, int attrsonly )
1088 assert( ld != NULL );
1089 assert( LDAP_VALID( ld ) );
1091 if ( ldap_url_parse( url, &ludp ) != 0 ) {
1092 ld->ld_errno = LDAP_PARAM_ERROR;
1096 if( ludp->lud_crit_exts ) {
1097 /* we don't support any extension (yet) */
1098 ld->ld_errno = LDAP_NOT_SUPPORTED;
1102 ber = ldap_build_search_req( ld, ludp->lud_dn, ludp->lud_scope,
1103 ludp->lud_filter, ludp->lud_attrs, attrsonly, NULL, NULL,
1106 if ( ber == NULL ) {
1109 bind.ri_request = LDAP_REQ_SEARCH;
1110 bind.ri_msgid = ld->ld_msgid;
1111 bind.ri_url = (char *)url;
1112 err = ldap_send_server_request(
1113 ld, ber, ld->ld_msgid, NULL,
1114 NULL, NULL, &bind );
1117 ldap_free_urldesc( ludp );
1123 ldap_url_search_st( LDAP *ld, LDAP_CONST char *url, int attrsonly,
1124 struct timeval *timeout, LDAPMessage **res )
1128 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
1129 return( ld->ld_errno );
1132 if ( ldap_result( ld, msgid, 1, timeout, res ) == -1 ) {
1133 return( ld->ld_errno );
1136 if ( ld->ld_errno == LDAP_TIMEOUT ) {
1137 (void) ldap_abandon( ld, msgid );
1138 ld->ld_errno = LDAP_TIMEOUT;
1139 return( ld->ld_errno );
1142 return( ldap_result2error( ld, *res, 0 ));
1148 LDAP *ld, LDAP_CONST char *url, int attrsonly, LDAPMessage **res )
1152 if (( msgid = ldap_url_search( ld, url, attrsonly )) == -1 ) {
1153 return( ld->ld_errno );
1156 if ( ldap_result( ld, msgid, 1, (struct timeval *)NULL, res ) == -1 ) {
1157 return( ld->ld_errno );
1160 return( ldap_result2error( ld, *res, 0 ));
1165 ldap_pvt_hex_unescape( char *s )
1168 * Remove URL hex escapes from s... done in place. The basic concept for
1169 * this routine is borrowed from the WWW library HTUnEscape() routine.
1173 for ( p = s; *s != '\0'; ++s ) {
1175 if ( *++s != '\0' ) {
1176 *p = ldap_pvt_unhex( *s ) << 4;
1178 if ( *++s != '\0' ) {
1179 *p++ += ldap_pvt_unhex( *s );
1191 ldap_pvt_unhex( int c )
1193 return( c >= '0' && c <= '9' ? c - '0'
1194 : c >= 'A' && c <= 'F' ? c - 'A' + 10