4 * Copyright 2008 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
26 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
28 * Copyright 1998-2008 The OpenLDAP Foundation.
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted only as authorized by the OpenLDAP
35 * A copy of this license is available in the file LICENSE in the
36 * top-level directory of the distribution or, alternatively, at
37 * <http://www.OpenLDAP.org/license.html>.
39 /* Basic UTF-8 routines
41 * These routines are "dumb". Though they understand UTF-8,
42 * they don't grok Unicode. That is, they can push bits,
43 * but don't have a clue what the bits represent. That's
44 * good enough for use with the KRB5 Client SDK.
46 * These routines are not optimized.
49 #include "k5-platform.h"
54 * return the number of bytes required to hold the
55 * NULL-terminated UTF-8 string NOT INCLUDING the
58 size_t krb5int_utf8_bytes(const char *p
)
62 for (bytes
= 0; p
[bytes
]; bytes
++)
68 size_t krb5int_utf8_chars(const char *p
)
70 /* could be optimized and could check for invalid sequences */
73 for ( ; *p
; KRB5_UTF8_INCR(p
))
79 size_t krb5int_utf8c_chars(const char *p
, size_t length
)
81 /* could be optimized and could check for invalid sequences */
83 const char *end
= p
+ length
;
85 for ( ; p
< end
; KRB5_UTF8_INCR(p
))
91 /* return offset to next character */
92 int krb5int_utf8_offset(const char *p
)
94 return KRB5_UTF8_NEXT(p
) - p
;
98 * Returns length indicated by first byte.
100 const char krb5int_utf8_lentab
[] = {
101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
106 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
107 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
108 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 };
110 int krb5int_utf8_charlen(const char *p
)
115 return krb5int_utf8_lentab
[*(const unsigned char *)p
^ 0x80];
119 * Make sure the UTF-8 char used the shortest possible encoding
120 * returns charlen if valid, 0 if not.
122 * Here are the valid UTF-8 encodings, taken from RFC 2279 page 4.
123 * The table is slightly modified from that of the RFC.
125 * UCS-4 range (hex) UTF-8 sequence (binary)
126 * 0000 0000-0000 007F 0.......
127 * 0000 0080-0000 07FF 110++++. 10......
128 * 0000 0800-0000 FFFF 1110++++ 10+..... 10......
129 * 0001 0000-001F FFFF 11110+++ 10++.... 10...... 10......
130 * 0020 0000-03FF FFFF 111110++ 10+++... 10...... 10...... 10......
131 * 0400 0000-7FFF FFFF 1111110+ 10++++.. 10...... 10...... 10...... 10......
133 * The '.' bits are "don't cares". When validating a UTF-8 sequence,
134 * at least one of the '+' bits must be set, otherwise the character
135 * should have been encoded in fewer octets. Note that in the two-octet
136 * case, only the first octet needs to be validated, and this is done
137 * in the krb5int_utf8_lentab[] above.
140 /* mask of required bits in second octet */
143 c krb5int_utf8_mintab
[] = {
144 (c
)0x20, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80,
145 (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80,
146 (c
)0x30, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x80,
147 (c
)0x38, (c
)0x80, (c
)0x80, (c
)0x80, (c
)0x3c, (c
)0x80, (c
)0x00, (c
)0x00 };
150 int krb5int_utf8_charlen2(const char *p
)
152 int i
= KRB5_UTF8_CHARLEN(p
);
155 if (!(krb5int_utf8_mintab
[*p
& 0x1f] & p
[1]))
163 * Convert a UTF8 character to a UCS4 character. Return 0 on success,
166 int krb5int_utf8_to_ucs4(const char *p
, krb5_ucs4
*out
)
168 const unsigned char *c
= (const unsigned char *) p
;
171 static unsigned char mask
[] = {
172 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
175 len
= KRB5_UTF8_CHARLEN2(p
, len
);
180 ch
= c
[0] & mask
[len
];
182 for (i
= 1; i
< len
; i
++) {
183 if ((c
[i
] & 0xc0) != 0x80)
194 int krb5int_utf8_to_ucs2(const char *p
, krb5_ucs2
*out
)
199 if (krb5int_utf8_to_ucs4(p
, &ch
) == -1 || ch
> 0xFFFF)
201 *out
= (krb5_ucs2
) ch
;
205 /* conv UCS-2 to UTF-8, not used */
206 size_t krb5int_ucs4_to_utf8(krb5_ucs4 c
, char *buf
)
209 unsigned char *p
= (unsigned char *) buf
;
211 /* not a valid Unicode character */
215 /* Just return length, don't convert */
217 if (c
< 0x80) return 1;
218 else if (c
< 0x800) return 2;
219 else if (c
< 0x10000) return 3;
220 else if (c
< 0x200000) return 4;
221 else if (c
< 0x4000000) return 5;
227 } else if (c
< 0x800) {
228 p
[len
++] = 0xc0 | ( c
>> 6 );
229 p
[len
++] = 0x80 | ( c
& 0x3f );
230 } else if (c
< 0x10000) {
231 p
[len
++] = 0xe0 | ( c
>> 12 );
232 p
[len
++] = 0x80 | ( (c
>> 6) & 0x3f );
233 p
[len
++] = 0x80 | ( c
& 0x3f );
234 } else if (c
< 0x200000) {
235 p
[len
++] = 0xf0 | ( c
>> 18 );
236 p
[len
++] = 0x80 | ( (c
>> 12) & 0x3f );
237 p
[len
++] = 0x80 | ( (c
>> 6) & 0x3f );
238 p
[len
++] = 0x80 | ( c
& 0x3f );
239 } else if (c
< 0x4000000) {
240 p
[len
++] = 0xf8 | ( c
>> 24 );
241 p
[len
++] = 0x80 | ( (c
>> 18) & 0x3f );
242 p
[len
++] = 0x80 | ( (c
>> 12) & 0x3f );
243 p
[len
++] = 0x80 | ( (c
>> 6) & 0x3f );
244 p
[len
++] = 0x80 | ( c
& 0x3f );
245 } else /* if( c < 0x80000000 ) */ {
246 p
[len
++] = 0xfc | ( c
>> 30 );
247 p
[len
++] = 0x80 | ( (c
>> 24) & 0x3f );
248 p
[len
++] = 0x80 | ( (c
>> 18) & 0x3f );
249 p
[len
++] = 0x80 | ( (c
>> 12) & 0x3f );
250 p
[len
++] = 0x80 | ( (c
>> 6) & 0x3f );
251 p
[len
++] = 0x80 | ( c
& 0x3f );
257 size_t krb5int_ucs2_to_utf8(krb5_ucs2 c
, char *buf
)
259 return krb5int_ucs4_to_utf8((krb5_ucs4
)c
, buf
);
262 #define KRB5_UCS_UTF8LEN(c) \
263 c < 0 ? 0 : (c < 0x80 ? 1 : (c < 0x800 ? 2 : (c < 0x10000 ? 3 : \
264 (c < 0x200000 ? 4 : (c < 0x4000000 ? 5 : 6)))))
267 * Advance to the next UTF-8 character
269 * Ignores length of multibyte character, instead rely on
270 * continuation markers to find start of next character.
271 * This allows for "resyncing" of when invalid characters
272 * are provided provided the start of the next character
273 * is appears within the 6 bytes examined.
275 char *krb5int_utf8_next(const char *p
)
278 const unsigned char *u
= (const unsigned char *) p
;
280 if (KRB5_UTF8_ISASCII(u
)) {
281 return (char *) &p
[1];
284 for (i
= 1; i
< 6; i
++) {
285 if ((u
[i
] & 0xc0) != 0x80) {
286 return (char *) &p
[i
];
290 return (char *) &p
[i
];
294 * Advance to the previous UTF-8 character
296 * Ignores length of multibyte character, instead rely on
297 * continuation markers to find start of next character.
298 * This allows for "resyncing" of when invalid characters
299 * are provided provided the start of the next character
300 * is appears within the 6 bytes examined.
302 char *krb5int_utf8_prev(const char *p
)
305 const unsigned char *u
= (const unsigned char *) p
;
307 for (i
= -1; i
>-6 ; i
--) {
308 if ((u
[i
] & 0xc0 ) != 0x80) {
309 return (char *) &p
[i
];
313 return (char *) &p
[i
];
317 * Copy one UTF-8 character from src to dst returning
318 * number of bytes copied.
320 * Ignores length of multibyte character, instead rely on
321 * continuation markers to find start of next character.
322 * This allows for "resyncing" of when invalid characters
323 * are provided provided the start of the next character
324 * is appears within the 6 bytes examined.
326 int krb5int_utf8_copy(char* dst
, const char *src
)
329 const unsigned char *u
= (const unsigned char *) src
;
333 if (KRB5_UTF8_ISASCII(u
)) {
337 for (i
=1; i
<6; i
++) {
338 if ((u
[i
] & 0xc0) != 0x80) {
347 #ifndef UTF8_ALPHA_CTYPE
349 * UTF-8 ctype routines
350 * Only deals with characters < 0x80 (ie: US-ASCII)
353 int krb5int_utf8_isascii(const char * p
)
355 unsigned c
= * (const unsigned char *) p
;
357 return KRB5_ASCII(c
);
360 int krb5int_utf8_isdigit(const char * p
)
362 unsigned c
= * (const unsigned char *) p
;
367 return KRB5_DIGIT( c
);
370 int krb5int_utf8_isxdigit(const char * p
)
372 unsigned c
= * (const unsigned char *) p
;
380 int krb5int_utf8_isspace(const char * p
)
382 unsigned c
= * (const unsigned char *) p
;
401 * These are not needed by the C SDK and are
402 * not "good enough" for general use.
404 int krb5int_utf8_isalpha(const char * p
)
406 unsigned c
= * (const unsigned char *) p
;
411 return KRB5_ALPHA(c
);
414 int krb5int_utf8_isalnum(const char * p
)
416 unsigned c
= * (const unsigned char *) p
;
421 return KRB5_ALNUM(c
);
425 int krb5int_utf8_islower(const char * p
)
427 unsigned c
= * (const unsigned char *) p
;
432 return KRB5_LOWER(c
);
435 int krb5int_utf8_isupper(const char * p
)
437 unsigned c
= * (const unsigned char *) p
;
442 return KRB5_UPPER(c
);
449 * UTF-8 string routines
453 char *krb5int_utf8_strchr(const char *str
, const char *chr
)
457 if (krb5int_utf8_to_ucs4(chr
, &ch
) == -1)
459 for ( ; *str
!= '\0'; KRB5_UTF8_INCR(str
)) {
460 if (krb5int_utf8_to_ucs4(str
, &chs
) == 0 && chs
== ch
)
467 /* like strcspn() but returns number of bytes, not characters */
468 size_t krb5int_utf8_strcspn(const char *str
, const char *set
)
470 const char *cstr
, *cset
;
471 krb5_ucs4 chstr
, chset
;
473 for (cstr
= str
; *cstr
!= '\0'; KRB5_UTF8_INCR(cstr
)) {
474 for (cset
= set
; *cset
!= '\0'; KRB5_UTF8_INCR(cset
)) {
475 if (krb5int_utf8_to_ucs4(cstr
, &chstr
) == 0
476 && krb5int_utf8_to_ucs4(cset
, &chset
) == 0 && chstr
== chset
)
484 /* like strspn() but returns number of bytes, not characters */
485 size_t krb5int_utf8_strspn(const char *str
, const char *set
)
487 const char *cstr
, *cset
;
488 krb5_ucs4 chstr
, chset
;
490 for (cstr
= str
; *cstr
!= '\0'; KRB5_UTF8_INCR(cstr
)) {
491 for (cset
= set
; ; KRB5_UTF8_INCR(cset
)) {
494 if (krb5int_utf8_to_ucs4(cstr
, &chstr
) == 0
495 && krb5int_utf8_to_ucs4(cset
, &chset
) == 0 && chstr
== chset
)
503 /* like strpbrk(), replaces strchr() as well */
504 char *krb5int_utf8_strpbrk(const char *str
, const char *set
)
507 krb5_ucs4 chstr
, chset
;
509 for ( ; *str
!= '\0'; KRB5_UTF8_INCR(str
)) {
510 for (cset
= set
; *cset
!= '\0'; KRB5_UTF8_INCR(cset
)) {
511 if (krb5int_utf8_to_ucs4(str
, &chstr
) == 0
512 && krb5int_utf8_to_ucs4(cset
, &chset
) == 0 && chstr
== chset
)
520 /* like strtok_r(), not strtok() */
521 char *krb5int_utf8_strtok(char *str
, const char *sep
, char **last
)
529 begin
= str
? str
: *last
;
531 begin
+= krb5int_utf8_strspn(begin
, sep
);
533 if (*begin
== '\0') {
538 end
= &begin
[krb5int_utf8_strcspn(begin
, sep
)];
541 char *next
= KRB5_UTF8_NEXT(end
);