1 /*******************************************************************
3 The early netatalk 2.x was based on UCS-2.
4 UCS-2 don't support chars above U+10000.
5 Recent netatalk is based on UTF-16.
6 UTF-16 can support chars above U+10000, using Surrogate Pair.
7 However, Surrogate Pair is complex, dirty, filthy and disagreeable.
8 There might still be latent bugs...
9 ********************************************************************/
13 #endif /* HAVE_CONFIG_H */
18 #include <sys/param.h>
20 #include <atalk/logger.h>
22 #include <arpa/inet.h>
24 #include <atalk/unicode.h>
25 #include <atalk/byteorder.h>
27 #include "precompose.h"
29 /*******************************************************************
30 Convert a string to lower case.
31 return True if any char is converted
32 ********************************************************************/
33 /* surrogate pair support */
35 int strlower_w(ucs2_t *s)
40 if ((0xD800 <= *s) && (*s < 0xDC00)) {
41 if ((0xDC00 <= s[1]) && (s[1] < 0xE000)) {
42 uint32_t s_sp = (uint32_t)*s << 16 | (uint32_t)s[1];
43 uint32_t v_sp = tolower_sp(s_sp);
52 ucs2_t v = tolower_w(*s);
63 /*******************************************************************
64 Convert a string to upper case.
65 return True if any char is converted
66 ********************************************************************/
67 /* surrogate pair support */
69 int strupper_w(ucs2_t *s)
74 if ((0xD800 <= *s) && (*s < 0xDC00)) {
75 if ((0xDC00 <= s[1]) && (s[1] < 0xE000)) {
76 uint32_t s_sp = (uint32_t)*s << 16 | (uint32_t)s[1];
77 uint32_t v_sp = toupper_sp(s_sp);
86 ucs2_t v = toupper_w(*s);
97 /*******************************************************************
99 determine if a character is lowercase
100 ********************************************************************/
101 /* These functions are not used. */
103 int islower_w(ucs2_t c)
105 return ( c == tolower_w(c));
108 int islower_sp(uint32_t c_sp)
110 return ( c_sp == tolower_sp(c_sp));
113 /*******************************************************************
115 determine if a character is uppercase
116 ********************************************************************/
117 /* These functions are not used. */
119 int isupper_w(ucs2_t c)
121 return ( c == toupper_w(c));
124 int isupper_sp(uint32_t c_sp)
126 return ( c_sp == toupper_sp(c_sp));
129 /*******************************************************************
131 Count the number of characters in a UTF-16 string.
132 ********************************************************************/
133 /* NOTE: one surrogate pair is two characters. */
135 size_t strlen_w(const ucs2_t *src)
139 for(len = 0; *src++; len++) ;
144 /*******************************************************************
146 Count up to max number of characters in a UTF-16 string.
147 ********************************************************************/
148 /* NOTE: one surrogate pair is two characters. */
150 size_t strnlen_w(const ucs2_t *src, size_t max)
154 for(len = 0; *src++ && (len < max); len++) ;
159 /*******************************************************************
161 ********************************************************************/
162 /* NOTE: hi and lo of surrogate pair are separately processed. */
164 ucs2_t *strchr_w(const ucs2_t *s, ucs2_t c)
167 if (c == *s) return (ucs2_t *)s;
170 if (c == *s) return (ucs2_t *)s;
175 /*******************************************************************
176 wide & sp strcasechr()
177 ********************************************************************/
178 /* NOTE: separately process BMP and surrogate pair */
180 ucs2_t *strcasechr_w(const ucs2_t *s, ucs2_t c)
183 if (tolower_w(c) == tolower_w(*s)) return (ucs2_t *)s;
186 if (c == *s) return (ucs2_t *)s;
191 ucs2_t *strcasechr_sp(const ucs2_t *s, uint32_t c_sp)
193 if (*s == 0) return NULL;
195 if (tolower_sp(c_sp) == tolower_sp((uint32_t)*s << 16 | (uint32_t)s[1])) return (ucs2_t *)s;
202 /*******************************************************************
204 ********************************************************************/
205 /* no problem of surrogate pair */
207 int strcmp_w(const ucs2_t *a, const ucs2_t *b)
209 while (*b && *a == *b) { a++; b++; }
211 /* warning: if *a != *b and both are not 0 we retrun a random
212 greater or lesser than 0 number not realted to which
216 /*******************************************************************
218 ********************************************************************/
219 /* no problem of surrogate pair */
221 int strncmp_w(const ucs2_t *a, const ucs2_t *b, size_t len)
224 while ((n < len) && *b && *a == *b) { a++; b++; n++;}
225 return (len - n)?(*a - *b):0;
228 /*******************************************************************
230 ********************************************************************/
231 /* no problem of surrogate pair */
233 ucs2_t *strstr_w(const ucs2_t *s, const ucs2_t *ins)
238 if (!s || !*s || !ins || !*ins) return NULL;
240 inslen = strlen_w(ins);
242 while ((r = strchr_w(r, *ins))) {
243 if (strncmp_w(r, ins, inslen) == 0) return r;
249 /*******************************************************************
251 ********************************************************************/
252 /* surrogate pair support */
254 ucs2_t *strcasestr_w(const ucs2_t *s, const ucs2_t *ins)
259 if (!s || !*s || !ins || !*ins) return NULL;
261 inslen = strlen_w(ins);
264 if ((0xD800 <= *ins) && (*ins < 0xDC00)) {
265 if ((0xDC00 <= ins[1]) && (ins[1] < 0xE000)) {
266 uint32_t ins_sp = (uint32_t)*ins << 16 | (uint32_t)ins[1];
267 while ((r = strcasechr_sp(r, ins_sp))) {
268 if (strncasecmp_w(r, ins, inslen) == 0) return r;
272 return NULL; /* illegal sequence */
275 while ((r = strcasechr_w(r, *ins))) {
276 if (strncasecmp_w(r, ins, inslen) == 0) return r;
283 /*******************************************************************
285 case insensitive string comparison
286 ********************************************************************/
287 /* surrogate pair support */
289 int strcasecmp_w(const ucs2_t *a, const ucs2_t *b)
294 if ((0xD800 <= *a) && (*a < 0xDC00)) {
295 if (ret = tolower_sp((uint32_t)*a << 16 | (uint32_t)a[1]) - tolower_sp((uint32_t)*b << 16 | (uint32_t)b[1])) return ret;
298 if (!(*a && *b)) return (tolower_w(*a) - tolower_w(*b)); /* avoid buffer over run */
300 if (ret = tolower_w(*a) - tolower_w(*b)) return ret;
305 return (tolower_w(*a) - tolower_w(*b));
308 /*******************************************************************
310 case insensitive string comparison, length limited
311 ********************************************************************/
312 /* NOTE: compare up to 'len+1' if 'len' isolate surrogate pair */
314 int strncasecmp_w(const ucs2_t *a, const ucs2_t *b, size_t len)
319 while ((n < len) && *a && *b) {
320 if ((0xD800 <= *a) && (*a < 0xDC00)) {
321 if (ret = tolower_sp((uint32_t)*a << 16 | (uint32_t)a[1]) - tolower_sp((uint32_t)*b << 16 | (uint32_t)b[1])) return ret;
325 if (!((n < len) && *a && *b)) return (tolower_w(*a) - tolower_w(*b));
327 if (ret = tolower_w(*a) - tolower_w(*b)) return ret;
333 return (len - n)?(tolower_w(*a) - tolower_w(*b)):0;
336 /*******************************************************************
339 ********************************************************************/
340 /* NOTE: not check isolation of surrogate pair */
341 /* if len == 0 then duplicate the whole string */
343 ucs2_t *strndup_w(const ucs2_t *src, size_t len)
347 if (!len) len = strlen_w(src);
348 dest = (ucs2_t *)malloc((len + 1) * sizeof(ucs2_t));
350 LOG (log_error, logtype_default, "strdup_w: out of memory!");
354 memcpy(dest, src, len * sizeof(ucs2_t));
360 /*******************************************************************
363 ********************************************************************/
364 /* no problem of surrogate pair */
366 ucs2_t *strdup_w(const ucs2_t *src)
368 return strndup_w(src, 0);
371 /*******************************************************************
372 copy a string with max len
373 ********************************************************************/
374 /* This function is not used. */
375 /* NOTE: not check isolation of surrogate pair */
377 ucs2_t *strncpy_w(ucs2_t *dest, const ucs2_t *src, const size_t max)
381 if (!dest || !src) return NULL;
383 for (len = 0; (src[len] != 0) && (len < max); len++)
384 dest[len] = src[len];
392 /*******************************************************************
393 append a string of len bytes and add a terminator
394 ********************************************************************/
395 /* These functions are not used. */
397 /* NOTE: not check isolation of surrogate pair */
398 ucs2_t *strncat_w(ucs2_t *dest, const ucs2_t *src, const size_t max)
403 if (!dest || !src) return NULL;
405 start = strlen_w(dest);
406 len = strnlen_w(src, max);
408 memcpy(&dest[start], src, len*sizeof(ucs2_t));
414 /* no problem of surrogate pair */
415 ucs2_t *strcat_w(ucs2_t *dest, const ucs2_t *src)
420 if (!dest || !src) return NULL;
422 start = strlen_w(dest);
425 memcpy(&dest[start], src, len*sizeof(ucs2_t));
432 /*******************************************************************
433 binary search for pre|decomposition
434 ********************************************************************/
436 static ucs2_t do_precomposition(unsigned int base, unsigned int comb)
439 int max = PRECOMP_COUNT - 1;
441 uint32_t sought = (base << 16) | comb, that;
445 mid = (min + max) / 2;
446 that = (precompositions[mid].base << 16) | (precompositions[mid].comb);
449 } else if (that > sought) {
452 return precompositions[mid].replacement;
459 /* ------------------------ */
460 static uint32_t do_precomposition_sp(unsigned int base_sp, unsigned int comb_sp)
463 int max = PRECOMP_SP_COUNT - 1;
465 uint64_t sought_sp = ((uint64_t)base_sp << 32) | (uint64_t)comb_sp, that_sp;
469 mid = (min + max) / 2;
470 that_sp = ((uint64_t)precompositions_sp[mid].base_sp << 32) | ((uint64_t)precompositions_sp[mid].comb_sp);
471 if (that_sp < sought_sp) {
473 } else if (that_sp > sought_sp) {
476 return precompositions_sp[mid].replacement_sp;
483 /* -------------------------- */
484 static uint32_t do_decomposition(ucs2_t base)
487 int max = DECOMP_COUNT - 1;
489 uint32_t sought = base;
490 uint32_t result, that;
494 mid = (min + max) / 2;
495 that = decompositions[mid].replacement;
498 } else if (that > sought) {
501 result = (decompositions[mid].base << 16) | (decompositions[mid].comb);
509 /* -------------------------- */
510 static uint64_t do_decomposition_sp(unsigned int base_sp)
513 int max = DECOMP_SP_COUNT - 1;
515 uint32_t sought_sp = base_sp;
521 mid = (min + max) / 2;
522 that_sp = decompositions_sp[mid].replacement_sp;
523 if (that_sp < sought_sp) {
525 } else if (that_sp > sought_sp) {
528 result_sp = ((uint64_t)decompositions_sp[mid].base_sp << 32) | ((uint64_t)decompositions_sp[mid].comb_sp);
536 /*******************************************************************
539 we can't use static, this stuff needs to be reentrant
540 static char comp[MAXPATHLEN +1];
542 We don't implement Singleton and Canonical Ordering.
543 We ignore CompositionExclusions.txt.
544 because they cause the problem of the roundtrip
545 such as Dancing Icon.
547 exclude U2000-U2FFF, UFE30-UFE4F and U2F800-U2FA1F ranges
548 in precompose.h from composition according to AFP 3.x spec
549 ********************************************************************/
551 size_t precompose_w (ucs2_t *name, size_t inplen, ucs2_t *comp, size_t *outlen)
555 uint32_t base_sp, comb_sp;
557 ucs2_t lindex, vindex;
560 size_t o_len = *outlen;
562 if (!inplen || (inplen & 1) || inplen > o_len)
570 while (*outlen > 2) {
577 return o_len - *outlen;
583 /* Non-Combination Character */
586 /* Unicode Standard Annex #15 A10.3 Hangul Composition */
588 else if ((VBASE <= comb) && (comb <= VBASE + VCOUNT)) {
589 if ((LBASE <= base) && (base < LBASE + LCOUNT)) {
591 lindex = base - LBASE;
592 vindex = comb - VBASE;
593 base = SBASE + (lindex * VCOUNT + vindex) * TCOUNT;
598 else if ((TBASE < comb) && (comb < TBASE + TCOUNT)) {
599 if ((SBASE <= base) && (base < SBASE + SCOUNT) && (((base - SBASE) % TCOUNT) == 0)) {
601 base += comb - TBASE;
605 /* Binary Search for Surrogate Pair */
606 else if ((0xD800 <= base) && (base < 0xDC00)) {
607 if ((0xDC00 <= comb) && (comb < 0xE000) && (i + 6 <= inplen)) {
608 base_sp = ((uint32_t)base << 16) | (uint32_t)comb;
610 comb_sp = ((uint32_t)in[1] << 16) | (uint32_t)in[2];
611 if (result_sp = do_precomposition_sp(base_sp, comb_sp)) {
616 } while ((i + 6 <= inplen) && result_sp) ;
618 *out = base_sp >> 16;
627 *out = base_sp & 0xFFFF;
635 return o_len - *outlen;
644 /* Binary Search for BMP */
645 else if (result = do_precomposition(base, comb)) {
661 /* --------------- */
662 size_t decompose_w (ucs2_t *name, size_t inplen, ucs2_t *comp, size_t *outlen)
666 ucs2_t base, comb[COMBBUFLEN];
668 ucs2_t sindex, tjamo;
672 size_t o_len = *outlen;
674 if (!inplen || (inplen & 1))
684 /* check ASCII first. this is frequent. */
685 if (base <= 0x007f) ;
687 /* Unicode Standard Annex #15 A10.2 Hangul Decomposition */
688 else if ((SBASE <= base) && (base < SBASE + SCOUNT)) {
689 sindex = base - SBASE;
690 base = LBASE + sindex / NCOUNT;
691 comb[COMBBUFLEN-2] = VBASE + (sindex % NCOUNT) / TCOUNT;
694 if ((tjamo = TBASE + sindex % TCOUNT) == TBASE) {
695 comb[COMBBUFLEN-1] = comb[COMBBUFLEN-2];
701 comb[COMBBUFLEN-1] = tjamo;
706 /* Binary Search for Surrogate Pair */
707 else if ((0xD800 <= base) && (base < 0xDC00)) {
708 if (i + 2 < inplen) {
709 base_sp = ((uint32_t)base << 16) | (uint32_t)in[1];
711 if ( !(result_sp = do_decomposition_sp(base_sp))) break;
713 base_sp = result_sp >> 32;
714 comb[COMBBUFLEN-comblen] = (result_sp >> 16) & 0xFFFF; /* hi */
715 comb[COMBBUFLEN-comblen+1] = result_sp & 0xFFFF; /* lo */
716 } while (comblen < MAXCOMBSPLEN);
718 if (*outlen < (comblen + 1) << 1) {
723 *out = base_sp >> 16; /* hi */
727 base = base_sp & 0xFFFF; /* lo */
734 /* Binary Search for BMP */
737 if ( !(result = do_decomposition(base))) break;
740 comb[COMBBUFLEN-comblen] = result & 0xFFFF;
741 } while ((0x007f < base) && (comblen < MAXCOMBLEN));
744 if (*outlen < (comblen + 1) << 1) {
753 while ( comblen > 0 ) {
754 *out = comb[COMBBUFLEN-comblen];
765 return o_len-*outlen;
768 /*******************************************************************
769 length of UTF-8 character and string
770 ********************************************************************/
772 size_t utf8_charlen ( char* utf8 )
776 p = (unsigned char*) utf8;
780 else if ( *p > 0xC1 && *p < 0xe0 && *(p+1) > 0x7f && *(p+1) < 0xC0)
782 else if ( *p == 0xe0 && *(p+1) > 0x9f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0)
784 else if ( *p > 0xe0 && *p < 0xf0 && *(p+1) > 0x7f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0)
786 else if ( *p == 0xf0 && *(p+1) > 0x8f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
788 else if ( *p > 0xf0 && *p < 0xf4 && *(p+1) > 0x7f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
790 else if ( *p == 0xf4 && *(p+1) > 0x7f && *(p+1) < 0x90 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
793 return ((size_t) -1);
797 size_t utf8_strlen_validate ( char * utf8 )
802 p = (unsigned char*) utf8;
805 /* see http://www.unicode.org/unicode/reports/tr27/ for an explanation */
812 else if ( *p > 0xC1 && *p < 0xe0 && *(p+1) > 0x7f && *(p+1) < 0xC0)
815 else if ( *p == 0xe0 && *(p+1) > 0x9f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0)
818 else if ( *p > 0xe0 && *p < 0xf0 && *(p+1) > 0x7f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0)
821 else if ( *p == 0xf0 && *(p+1) > 0x8f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
824 else if ( *p > 0xf0 && *p < 0xf4 && *(p+1) > 0x7f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
827 else if ( *p == 0xf4 && *(p+1) > 0x7f && *(p+1) < 0x90 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
831 return ((size_t) -1);