3 #endif /* HAVE_CONFIG_H */
10 #include <atalk/logger.h>
12 #include <arpa/inet.h>
14 #include <atalk/unicode.h>
15 #include "precompose.h"
16 #include "byteorder.h"
18 /*******************************************************************
19 Convert a string to lower case.
20 return True if any char is converted
21 ********************************************************************/
22 int strlower_w(ucs2_t *s)
26 ucs2_t v = tolower_w(*s);
36 /*******************************************************************
37 Convert a string to upper case.
38 return True if any char is converted
39 ********************************************************************/
40 int strupper_w(ucs2_t *s)
44 ucs2_t v = toupper_w(*s);
55 /*******************************************************************
56 determine if a character is lowercase
57 ********************************************************************/
58 int islower_w(ucs2_t c)
60 return ( c == tolower_w(c));
63 /*******************************************************************
64 determine if a character is uppercase
65 ********************************************************************/
66 int isupper_w(ucs2_t c)
68 return ( c == toupper_w(c));
72 /*******************************************************************
73 Count the number of characters in a ucs2_t string.
74 ********************************************************************/
75 size_t strlen_w(const ucs2_t *src)
79 for(len = 0; *src++; len++) ;
84 /*******************************************************************
85 Count up to max number of characters in a ucs2_t string.
86 ********************************************************************/
87 size_t strnlen_w(const ucs2_t *src, size_t max)
91 for(len = 0; *src++ && (len < max); len++) ;
96 /*******************************************************************
98 ********************************************************************/
99 ucs2_t *strchr_w(const ucs2_t *s, ucs2_t c)
102 if (c == *s) return (ucs2_t *)s;
105 if (c == *s) return (ucs2_t *)s;
110 ucs2_t *strcasechr_w(const ucs2_t *s, ucs2_t c)
113 /* LOG(log_debug, logtype_default, "Comparing %X to %X (%X - %X)", c, *s, toupper_w(c), toupper_w(*s));*/
114 if (toupper_w(c) == toupper_w(*s)) return (ucs2_t *)s;
117 if (c == *s) return (ucs2_t *)s;
123 int strcmp_w(const ucs2_t *a, const ucs2_t *b)
125 while (*b && *a == *b) { a++; b++; }
127 /* warning: if *a != *b and both are not 0 we retrun a random
128 greater or lesser than 0 number not realted to which
132 int strncmp_w(const ucs2_t *a, const ucs2_t *b, size_t len)
135 while ((n < len) && *b && *a == *b) { a++; b++; n++;}
136 return (len - n)?(*a - *b):0;
139 /*******************************************************************
141 ********************************************************************/
142 ucs2_t *strstr_w(const ucs2_t *s, const ucs2_t *ins)
147 if (!s || !*s || !ins || !*ins) return NULL;
149 inslen = strlen_w(ins);
151 while ((r = strchr_w(r, *ins))) {
152 if (strncmp_w(r, ins, inslen) == 0) return r;
158 ucs2_t *strcasestr_w(const ucs2_t *s, const ucs2_t *ins)
163 if (!s || !*s || !ins || !*ins) return NULL;
165 inslen = strlen_w(ins);
167 while ((r = strcasechr_w(r, *ins))) {
168 if (strncasecmp_w(r, ins, inslen) == 0) return r;
177 /*******************************************************************
178 case insensitive string comparison
179 ********************************************************************/
180 int strcasecmp_w(const ucs2_t *a, const ucs2_t *b)
182 while (*b && toupper_w(*a) == toupper_w(*b)) { a++; b++; }
183 return (tolower_w(*a) - tolower_w(*b));
186 /*******************************************************************
187 case insensitive string comparison, lenght limited
188 ********************************************************************/
189 int strncasecmp_w(const ucs2_t *a, const ucs2_t *b, size_t len)
192 while ((n < len) && *b && (toupper_w(*a) == toupper_w(*b))) { a++; b++; n++; }
193 return (len - n)?(tolower_w(*a) - tolower_w(*b)):0;
196 /*******************************************************************
198 ********************************************************************/
199 /* if len == 0 then duplicate the whole string */
200 ucs2_t *strndup_w(const ucs2_t *src, size_t len)
204 if (!len) len = strlen_w(src);
205 dest = (ucs2_t *)malloc((len + 1) * sizeof(ucs2_t));
207 LOG (log_error, logtype_default, "strdup_w: out of memory!");
211 memcpy(dest, src, len * sizeof(ucs2_t));
217 ucs2_t *strdup_w(const ucs2_t *src)
219 return strndup_w(src, 0);
222 /*******************************************************************
223 copy a string with max len
224 ********************************************************************/
226 ucs2_t *strncpy_w(ucs2_t *dest, const ucs2_t *src, const size_t max)
230 if (!dest || !src) return NULL;
232 for (len = 0; (src[len] != 0) && (len < max); len++)
233 dest[len] = src[len];
241 /*******************************************************************
242 append a string of len bytes and add a terminator
243 ********************************************************************/
245 ucs2_t *strncat_w(ucs2_t *dest, const ucs2_t *src, const size_t max)
250 if (!dest || !src) return NULL;
252 start = strlen_w(dest);
253 len = strnlen_w(src, max);
255 memcpy(&dest[start], src, len*sizeof(ucs2_t));
262 ucs2_t *strcat_w(ucs2_t *dest, const ucs2_t *src)
267 if (!dest || !src) return NULL;
269 start = strlen_w(dest);
272 memcpy(&dest[start], src, len*sizeof(ucs2_t));
279 /*******************************************************************
280 binary search for pre|decomposition
281 ********************************************************************/
283 static ucs2_t do_precomposition(unsigned int base, unsigned int comb)
286 int max = PRECOMP_COUNT - 1;
288 u_int32_t sought = (base << 16) | comb, that;
292 mid = (min + max) / 2;
293 that = (precompositions[mid].base << 16) | (precompositions[mid].comb);
296 } else if (that > sought) {
299 return precompositions[mid].replacement;
306 /* ------------------------ */
307 static u_int32_t do_precomposition_sp(unsigned int base_sp, unsigned int comb_sp)
310 int max = PRECOMP_SP_COUNT - 1;
312 u_int64_t sought_sp = ((u_int64_t)base_sp << 32) | (u_int64_t)comb_sp, that_sp;
316 mid = (min + max) / 2;
317 that_sp = ((u_int64_t)precompositions_sp[mid].base_sp << 32) | ((u_int64_t)precompositions_sp[mid].comb_sp);
318 if (that_sp < sought_sp) {
320 } else if (that_sp > sought_sp) {
323 return precompositions_sp[mid].replacement_sp;
330 /* -------------------------- */
331 static u_int32_t do_decomposition(ucs2_t base)
334 int max = DECOMP_COUNT - 1;
336 u_int32_t sought = base;
337 u_int32_t result, that;
341 mid = (min + max) / 2;
342 that = decompositions[mid].replacement;
345 } else if (that > sought) {
348 result = (decompositions[mid].base << 16) | (decompositions[mid].comb);
356 /* -------------------------- */
357 static u_int64_t do_decomposition_sp(unsigned int base_sp)
360 int max = DECOMP_SP_COUNT - 1;
362 u_int32_t sought_sp = base_sp;
368 mid = (min + max) / 2;
369 that_sp = decompositions_sp[mid].replacement_sp;
370 if (that_sp < sought_sp) {
372 } else if (that_sp > sought_sp) {
375 result_sp = ((u_int64_t)decompositions_sp[mid].base_sp << 32) | ((u_int64_t)decompositions_sp[mid].comb_sp);
383 /*******************************************************************
386 we can't use static, this stuff needs to be reentrant
387 static char comp[MAXPATHLEN +1];
389 We don't implement Singleton and Canonical Ordering.
390 We ignore CompositionExclusions.txt.
391 because they cause the problem of the roundtrip
392 such as Dancing Icon.
394 exclude U2000-U2FFF, UFE30-UFE4F and U2F800-U2FA1F ranges
395 in precompose.h from composition according to AFP 3.x spec
396 ********************************************************************/
398 size_t precompose_w (ucs2_t *name, size_t inplen, ucs2_t *comp, size_t *outlen)
402 u_int32_t base_sp, comb_sp;
404 ucs2_t lindex, vindex;
407 size_t o_len = *outlen;
409 if (!inplen || (inplen & 1) || inplen > o_len)
417 while (*outlen > 2) {
426 return o_len - *outlen;
432 /* Non-Combination Character */
435 /* Unicode Standard Annex #15 A10.3 Hangul Composition */
437 else if ((VBASE <= comb) && (comb <= VBASE + VCOUNT)) {
438 if ((LBASE <= base) && (base < LBASE + LCOUNT)) {
440 lindex = base - LBASE;
441 vindex = comb - VBASE;
442 base = SBASE + (lindex * VCOUNT + vindex) * TCOUNT;
447 else if ((TBASE < comb) && (comb < TBASE + TCOUNT)) {
448 if ((SBASE <= base) && (base < SBASE + SCOUNT) && (((base - SBASE) % TCOUNT) == 0)) {
450 base += comb - TBASE;
454 /* Binary Search for Surrogate Pair */
455 else if ((0xD800 <= base) && (base < 0xDC00)) {
456 if ((0xDC00 <= comb) && (comb < 0xE000) && (i + 4 <= inplen)) {
457 base_sp = ((u_int32_t)base << 16) | (u_int32_t)comb;
459 comb_sp = ((u_int32_t)in[1] << 16) | (u_int32_t)in[2];
460 if (result_sp = do_precomposition_sp(base_sp, comb_sp)) {
465 } while ((i + 4 <= inplen) && result_sp) ;
467 *out = base_sp >> 16;
476 *out = base_sp & 0xFFFF;
488 /* Binary Search for BMP */
489 else if (result = do_precomposition(base, comb)) {
505 /* --------------- */
506 size_t decompose_w (ucs2_t *name, size_t inplen, ucs2_t *comp, size_t *outlen)
510 ucs2_t base, comb[COMBBUFLEN];
512 ucs2_t sindex, tjamo;
516 size_t o_len = *outlen;
518 if (!inplen || (inplen & 1))
528 /* check ASCII first. this is frequent. */
529 if (base <= 0x007f) ;
531 /* Unicode Standard Annex #15 A10.2 Hangul Decomposition */
532 else if ((SBASE <= base) && (base < SBASE + SCOUNT)) {
533 sindex = base - SBASE;
534 base = LBASE + sindex / NCOUNT;
535 comb[COMBBUFLEN-2] = VBASE + (sindex % NCOUNT) / TCOUNT;
538 if ((tjamo = TBASE + sindex % TCOUNT) == TBASE) {
539 comb[COMBBUFLEN-1] = comb[COMBBUFLEN-2];
545 comb[COMBBUFLEN-1] = tjamo;
550 /* Binary Search for Surrogate Pair */
551 else if ((0xD800 <= base) && (base < 0xDC00)) {
552 if (i + 2 < inplen) {
553 base_sp = ((u_int32_t)base << 16) | (u_int32_t)in[1];
555 if ( !(result_sp = do_decomposition_sp(base_sp))) break;
557 base_sp = result_sp >> 32;
558 comb[COMBBUFLEN-comblen] = (result_sp >> 16) & 0xFFFF; /* hi */
559 comb[COMBBUFLEN-comblen+1] = result_sp & 0xFFFF; /* lo */
560 } while (comblen < MAXCOMBSPLEN);
562 if (*outlen < (comblen + 1) << 1) {
567 *out = base_sp >> 16; /* hi */
571 base = base_sp & 0xFFFF; /* lo */
578 /* Binary Search for BMP */
581 if ( !(result = do_decomposition(base))) break;
584 comb[COMBBUFLEN-comblen] = result & 0xFFFF;
585 } while ((0x007f < base) && (comblen < MAXCOMBLEN));
588 if (*outlen < (comblen + 1) << 1) {
597 while ( comblen > 0 ) {
598 *out = comb[COMBBUFLEN-comblen];
609 return o_len-*outlen;
612 /*******************************************************************
613 length of UTF-8 character and string
614 ********************************************************************/
616 size_t utf8_charlen ( char* utf8 )
620 p = (unsigned char*) utf8;
624 else if ( *p > 0xC1 && *p < 0xe0 && *(p+1) > 0x7f && *(p+1) < 0xC0)
626 else if ( *p == 0xe0 && *(p+1) > 0x9f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0)
628 else if ( *p > 0xe0 && *p < 0xf0 && *(p+1) > 0x7f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0)
630 else if ( *p == 0xf0 && *(p+1) > 0x8f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
632 else if ( *p > 0xf0 && *p < 0xf4 && *(p+1) > 0x7f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
634 else if ( *p == 0xf4 && *(p+1) > 0x7f && *(p+1) < 0x90 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
637 return ((size_t) -1);
641 size_t utf8_strlen_validate ( char * utf8 )
646 p = (unsigned char*) utf8;
649 /* see http://www.unicode.org/unicode/reports/tr27/ for an explanation */
656 else if ( *p > 0xC1 && *p < 0xe0 && *(p+1) > 0x7f && *(p+1) < 0xC0)
659 else if ( *p == 0xe0 && *(p+1) > 0x9f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0)
662 else if ( *p > 0xe0 && *p < 0xf0 && *(p+1) > 0x7f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0)
665 else if ( *p == 0xf0 && *(p+1) > 0x8f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
668 else if ( *p > 0xf0 && *p < 0xf4 && *(p+1) > 0x7f && *(p+1) < 0xc0 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
671 else if ( *p == 0xf4 && *(p+1) > 0x7f && *(p+1) < 0x90 && *(p+2) > 0x7f && *(p+2) < 0xc0 && *(p+3) > 0x7f && *(p+3) < 0xc0 )
675 return ((size_t) -1);