utf-8.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /* utf-8.c -- Basic UTF-8 routines */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2022 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in the file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. /* Basic UTF-8 routines
  17. *
  18. * These routines are "dumb". Though they understand UTF-8,
  19. * they don't grok Unicode. That is, they can push bits,
  20. * but don't have a clue what the bits represent. That's
  21. * good enough for use with the LDAP Client SDK.
  22. *
  23. * These routines are not optimized.
  24. */
  25. #include "portable.h"
  26. #include <stdio.h>
  27. #include <ac/stdlib.h>
  28. #include <ac/socket.h>
  29. #include <ac/string.h>
  30. #include <ac/time.h>
  31. #include "ldap_utf8.h"
  32. #include "ldap-int.h"
  33. #include "ldap_defaults.h"
  34. /*
  35. * return the number of bytes required to hold the
  36. * NULL-terminated UTF-8 string NOT INCLUDING the
  37. * termination.
  38. */
  39. ber_len_t ldap_utf8_bytes( const char * p )
  40. {
  41. ber_len_t bytes;
  42. for( bytes=0; p[bytes]; bytes++ ) {
  43. /* EMPTY */ ;
  44. }
  45. return bytes;
  46. }
  47. ber_len_t ldap_utf8_chars( const char * p )
  48. {
  49. /* could be optimized and could check for invalid sequences */
  50. ber_len_t chars=0;
  51. for( ; *p ; LDAP_UTF8_INCR(p) ) {
  52. chars++;
  53. }
  54. return chars;
  55. }
  56. /* return offset to next character */
  57. int ldap_utf8_offset( const char * p )
  58. {
  59. return LDAP_UTF8_NEXT(p) - p;
  60. }
  61. /*
  62. * Returns length indicated by first byte.
  63. */
  64. const char ldap_utf8_lentab[] = {
  65. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  66. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  67. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  68. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  69. 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  70. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  71. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  72. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 };
  73. int ldap_utf8_charlen( const char * p )
  74. {
  75. if (!(*p & 0x80))
  76. return 1;
  77. return ldap_utf8_lentab[*(const unsigned char *)p ^ 0x80];
  78. }
  79. /*
  80. * Make sure the UTF-8 char used the shortest possible encoding
  81. * returns charlen if valid, 0 if not.
  82. *
  83. * Here are the valid UTF-8 encodings, taken from RFC 2279 page 4.
  84. * The table is slightly modified from that of the RFC.
  85. *
  86. * UCS-4 range (hex) UTF-8 sequence (binary)
  87. * 0000 0000-0000 007F 0.......
  88. * 0000 0080-0000 07FF 110++++. 10......
  89. * 0000 0800-0000 FFFF 1110++++ 10+..... 10......
  90. * 0001 0000-001F FFFF 11110+++ 10++.... 10...... 10......
  91. * 0020 0000-03FF FFFF 111110++ 10+++... 10...... 10...... 10......
  92. * 0400 0000-7FFF FFFF 1111110+ 10++++.. 10...... 10...... 10...... 10......
  93. *
  94. * The '.' bits are "don't cares". When validating a UTF-8 sequence,
  95. * at least one of the '+' bits must be set, otherwise the character
  96. * should have been encoded in fewer octets. Note that in the two-octet
  97. * case, only the first octet needs to be validated, and this is done
  98. * in the ldap_utf8_lentab[] above.
  99. */
  100. /* mask of required bits in second octet */
  101. #undef c
  102. #define c const char
  103. c ldap_utf8_mintab[] = {
  104. (c)0x20, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80,
  105. (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80,
  106. (c)0x30, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80, (c)0x80,
  107. (c)0x38, (c)0x80, (c)0x80, (c)0x80, (c)0x3c, (c)0x80, (c)0x00, (c)0x00 };
  108. #undef c
  109. int ldap_utf8_charlen2( const char * p )
  110. {
  111. int i = LDAP_UTF8_CHARLEN( p );
  112. if ( i > 2 ) {
  113. if ( !( ldap_utf8_mintab[*p & 0x1f] & p[1] ) )
  114. i = 0;
  115. }
  116. return i;
  117. }
  118. /* conv UTF-8 to UCS-4, useful for comparisons */
  119. ldap_ucs4_t ldap_x_utf8_to_ucs4( const char * p )
  120. {
  121. const unsigned char *c = (const unsigned char *) p;
  122. ldap_ucs4_t ch;
  123. int len, i;
  124. static unsigned char mask[] = {
  125. 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
  126. len = LDAP_UTF8_CHARLEN2(p, len);
  127. if( len == 0 ) return LDAP_UCS4_INVALID;
  128. ch = c[0] & mask[len];
  129. for(i=1; i < len; i++) {
  130. if ((c[i] & 0xc0) != 0x80) {
  131. return LDAP_UCS4_INVALID;
  132. }
  133. ch <<= 6;
  134. ch |= c[i] & 0x3f;
  135. }
  136. return ch;
  137. }
  138. /* conv UCS-4 to UTF-8, not used */
  139. int ldap_x_ucs4_to_utf8( ldap_ucs4_t c, char *buf )
  140. {
  141. int len=0;
  142. unsigned char* p = (unsigned char *) buf;
  143. /* not a valid Unicode character */
  144. if ( c < 0 ) return 0;
  145. /* Just return length, don't convert */
  146. if(buf == NULL) {
  147. if( c < 0x80 ) return 1;
  148. else if( c < 0x800 ) return 2;
  149. else if( c < 0x10000 ) return 3;
  150. else if( c < 0x200000 ) return 4;
  151. else if( c < 0x4000000 ) return 5;
  152. else return 6;
  153. }
  154. if( c < 0x80 ) {
  155. p[len++] = c;
  156. } else if( c < 0x800 ) {
  157. p[len++] = 0xc0 | ( c >> 6 );
  158. p[len++] = 0x80 | ( c & 0x3f );
  159. } else if( c < 0x10000 ) {
  160. p[len++] = 0xe0 | ( c >> 12 );
  161. p[len++] = 0x80 | ( (c >> 6) & 0x3f );
  162. p[len++] = 0x80 | ( c & 0x3f );
  163. } else if( c < 0x200000 ) {
  164. p[len++] = 0xf0 | ( c >> 18 );
  165. p[len++] = 0x80 | ( (c >> 12) & 0x3f );
  166. p[len++] = 0x80 | ( (c >> 6) & 0x3f );
  167. p[len++] = 0x80 | ( c & 0x3f );
  168. } else if( c < 0x4000000 ) {
  169. p[len++] = 0xf8 | ( c >> 24 );
  170. p[len++] = 0x80 | ( (c >> 18) & 0x3f );
  171. p[len++] = 0x80 | ( (c >> 12) & 0x3f );
  172. p[len++] = 0x80 | ( (c >> 6) & 0x3f );
  173. p[len++] = 0x80 | ( c & 0x3f );
  174. } else /* if( c < 0x80000000 ) */ {
  175. p[len++] = 0xfc | ( c >> 30 );
  176. p[len++] = 0x80 | ( (c >> 24) & 0x3f );
  177. p[len++] = 0x80 | ( (c >> 18) & 0x3f );
  178. p[len++] = 0x80 | ( (c >> 12) & 0x3f );
  179. p[len++] = 0x80 | ( (c >> 6) & 0x3f );
  180. p[len++] = 0x80 | ( c & 0x3f );
  181. }
  182. return len;
  183. }
  184. #define LDAP_UCS_UTF8LEN(c) \
  185. c < 0 ? 0 : (c < 0x80 ? 1 : (c < 0x800 ? 2 : (c < 0x10000 ? 3 : \
  186. (c < 0x200000 ? 4 : (c < 0x4000000 ? 5 : 6)))))
  187. /* Convert a string to UTF-8 format. The input string is expected to
  188. * have characters of 1, 2, or 4 octets (in network byte order)
  189. * corresponding to the ASN.1 T61STRING, BMPSTRING, and UNIVERSALSTRING
  190. * types respectively. (Here T61STRING just means that there is one
  191. * octet per character and characters may use the high bit of the octet.
  192. * The characters are assumed to use ISO mappings, no provision is made
  193. * for converting from T.61 coding rules to Unicode.)
  194. */
  195. int
  196. ldap_ucs_to_utf8s( struct berval *ucs, int csize, struct berval *utf8s )
  197. {
  198. unsigned char *in, *end;
  199. char *ptr;
  200. ldap_ucs4_t u;
  201. int i, l = 0;
  202. utf8s->bv_val = NULL;
  203. utf8s->bv_len = 0;
  204. in = (unsigned char *)ucs->bv_val;
  205. /* Make sure we stop at an even multiple of csize */
  206. end = in + ( ucs->bv_len & ~(csize-1) );
  207. for (; in < end; ) {
  208. u = *in++;
  209. if (csize > 1) {
  210. u <<= 8;
  211. u |= *in++;
  212. }
  213. if (csize > 2) {
  214. u <<= 8;
  215. u |= *in++;
  216. u <<= 8;
  217. u |= *in++;
  218. }
  219. i = LDAP_UCS_UTF8LEN(u);
  220. if (i == 0)
  221. return LDAP_INVALID_SYNTAX;
  222. l += i;
  223. }
  224. utf8s->bv_val = LDAP_MALLOC( l+1 );
  225. if (utf8s->bv_val == NULL)
  226. return LDAP_NO_MEMORY;
  227. utf8s->bv_len = l;
  228. ptr = utf8s->bv_val;
  229. for (in = (unsigned char *)ucs->bv_val; in < end; ) {
  230. u = *in++;
  231. if (csize > 1) {
  232. u <<= 8;
  233. u |= *in++;
  234. }
  235. if (csize > 2) {
  236. u <<= 8;
  237. u |= *in++;
  238. u <<= 8;
  239. u |= *in++;
  240. }
  241. ptr += ldap_x_ucs4_to_utf8(u, ptr);
  242. }
  243. *ptr = '\0';
  244. return LDAP_SUCCESS;
  245. }
  246. /*
  247. * Advance to the next UTF-8 character
  248. *
  249. * Ignores length of multibyte character, instead rely on
  250. * continuation markers to find start of next character.
  251. * This allows for "resyncing" of when invalid characters
  252. * are provided provided the start of the next character
  253. * is appears within the 6 bytes examined.
  254. */
  255. char* ldap_utf8_next( const char * p )
  256. {
  257. int i;
  258. const unsigned char *u = (const unsigned char *) p;
  259. if( LDAP_UTF8_ISASCII(u) ) {
  260. return (char *) &p[1];
  261. }
  262. for( i=1; i<6; i++ ) {
  263. if ( ( u[i] & 0xc0 ) != 0x80 ) {
  264. return (char *) &p[i];
  265. }
  266. }
  267. return (char *) &p[i];
  268. }
  269. /*
  270. * Advance to the previous UTF-8 character
  271. *
  272. * Ignores length of multibyte character, instead rely on
  273. * continuation markers to find start of next character.
  274. * This allows for "resyncing" of when invalid characters
  275. * are provided provided the start of the next character
  276. * is appears within the 6 bytes examined.
  277. */
  278. char* ldap_utf8_prev( const char * p )
  279. {
  280. int i;
  281. const unsigned char *u = (const unsigned char *) p;
  282. for( i=-1; i>-6 ; i-- ) {
  283. if ( ( u[i] & 0xc0 ) != 0x80 ) {
  284. return (char *) &p[i];
  285. }
  286. }
  287. return (char *) &p[i];
  288. }
  289. /*
  290. * Copy one UTF-8 character from src to dst returning
  291. * number of bytes copied.
  292. *
  293. * Ignores length of multibyte character, instead rely on
  294. * continuation markers to find start of next character.
  295. * This allows for "resyncing" of when invalid characters
  296. * are provided provided the start of the next character
  297. * is appears within the 6 bytes examined.
  298. */
  299. int ldap_utf8_copy( char* dst, const char *src )
  300. {
  301. int i;
  302. const unsigned char *u = (const unsigned char *) src;
  303. dst[0] = src[0];
  304. if( LDAP_UTF8_ISASCII(u) ) {
  305. return 1;
  306. }
  307. for( i=1; i<6; i++ ) {
  308. if ( ( u[i] & 0xc0 ) != 0x80 ) {
  309. return i;
  310. }
  311. dst[i] = src[i];
  312. }
  313. return i;
  314. }
  315. #ifndef UTF8_ALPHA_CTYPE
  316. /*
  317. * UTF-8 ctype routines
  318. * Only deals with characters < 0x80 (ie: US-ASCII)
  319. */
  320. int ldap_utf8_isascii( const char * p )
  321. {
  322. unsigned c = * (const unsigned char *) p;
  323. return LDAP_ASCII(c);
  324. }
  325. int ldap_utf8_isdigit( const char * p )
  326. {
  327. unsigned c = * (const unsigned char *) p;
  328. if(!LDAP_ASCII(c)) return 0;
  329. return LDAP_DIGIT( c );
  330. }
  331. int ldap_utf8_isxdigit( const char * p )
  332. {
  333. unsigned c = * (const unsigned char *) p;
  334. if(!LDAP_ASCII(c)) return 0;
  335. return LDAP_HEX(c);
  336. }
  337. int ldap_utf8_isspace( const char * p )
  338. {
  339. unsigned c = * (const unsigned char *) p;
  340. if(!LDAP_ASCII(c)) return 0;
  341. switch(c) {
  342. case ' ':
  343. case '\t':
  344. case '\n':
  345. case '\r':
  346. case '\v':
  347. case '\f':
  348. return 1;
  349. }
  350. return 0;
  351. }
  352. /*
  353. * These are not needed by the C SDK and are
  354. * not "good enough" for general use.
  355. */
  356. int ldap_utf8_isalpha( const char * p )
  357. {
  358. unsigned c = * (const unsigned char *) p;
  359. if(!LDAP_ASCII(c)) return 0;
  360. return LDAP_ALPHA(c);
  361. }
  362. int ldap_utf8_isalnum( const char * p )
  363. {
  364. unsigned c = * (const unsigned char *) p;
  365. if(!LDAP_ASCII(c)) return 0;
  366. return LDAP_ALNUM(c);
  367. }
  368. int ldap_utf8_islower( const char * p )
  369. {
  370. unsigned c = * (const unsigned char *) p;
  371. if(!LDAP_ASCII(c)) return 0;
  372. return LDAP_LOWER(c);
  373. }
  374. int ldap_utf8_isupper( const char * p )
  375. {
  376. unsigned c = * (const unsigned char *) p;
  377. if(!LDAP_ASCII(c)) return 0;
  378. return LDAP_UPPER(c);
  379. }
  380. #endif
  381. /*
  382. * UTF-8 string routines
  383. */
  384. /* like strchr() */
  385. char * (ldap_utf8_strchr)( const char *str, const char *chr )
  386. {
  387. for( ; *str != '\0'; LDAP_UTF8_INCR(str) ) {
  388. if( ldap_x_utf8_to_ucs4( str ) == ldap_x_utf8_to_ucs4( chr ) ) {
  389. return (char *) str;
  390. }
  391. }
  392. return NULL;
  393. }
  394. /* like strcspn() but returns number of bytes, not characters */
  395. ber_len_t (ldap_utf8_strcspn)( const char *str, const char *set )
  396. {
  397. const char *cstr;
  398. const char *cset;
  399. for( cstr = str; *cstr != '\0'; LDAP_UTF8_INCR(cstr) ) {
  400. for( cset = set; *cset != '\0'; LDAP_UTF8_INCR(cset) ) {
  401. if( ldap_x_utf8_to_ucs4( cstr ) == ldap_x_utf8_to_ucs4( cset ) ) {
  402. return cstr - str;
  403. }
  404. }
  405. }
  406. return cstr - str;
  407. }
  408. /* like strspn() but returns number of bytes, not characters */
  409. ber_len_t (ldap_utf8_strspn)( const char *str, const char *set )
  410. {
  411. const char *cstr;
  412. const char *cset;
  413. for( cstr = str; *cstr != '\0'; LDAP_UTF8_INCR(cstr) ) {
  414. for( cset = set; ; LDAP_UTF8_INCR(cset) ) {
  415. if( *cset == '\0' ) {
  416. return cstr - str;
  417. }
  418. if( ldap_x_utf8_to_ucs4( cstr ) == ldap_x_utf8_to_ucs4( cset ) ) {
  419. break;
  420. }
  421. }
  422. }
  423. return cstr - str;
  424. }
  425. /* like strpbrk(), replaces strchr() as well */
  426. char *(ldap_utf8_strpbrk)( const char *str, const char *set )
  427. {
  428. for( ; *str != '\0'; LDAP_UTF8_INCR(str) ) {
  429. const char *cset;
  430. for( cset = set; *cset != '\0'; LDAP_UTF8_INCR(cset) ) {
  431. if( ldap_x_utf8_to_ucs4( str ) == ldap_x_utf8_to_ucs4( cset ) ) {
  432. return (char *) str;
  433. }
  434. }
  435. }
  436. return NULL;
  437. }
  438. /* like strtok_r(), not strtok() */
  439. char *(ldap_utf8_strtok)(char *str, const char *sep, char **last)
  440. {
  441. char *begin;
  442. char *end;
  443. if( last == NULL ) return NULL;
  444. begin = str ? str : *last;
  445. begin += ldap_utf8_strspn( begin, sep );
  446. if( *begin == '\0' ) {
  447. *last = NULL;
  448. return NULL;
  449. }
  450. end = &begin[ ldap_utf8_strcspn( begin, sep ) ];
  451. if( *end != '\0' ) {
  452. char *next = LDAP_UTF8_NEXT( end );
  453. *end = '\0';
  454. end = next;
  455. }
  456. *last = end;
  457. return begin;
  458. }