• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ******************************************************************************
3 *
4 *   Copyright (C) 1999-2012, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 ******************************************************************************
8 *   file name:  utf_impl.c
9 *   encoding:   US-ASCII
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 1999sep13
14 *   created by: Markus W. Scherer
15 *
16 *   This file provides implementation functions for macros in the utfXX.h
17 *   that would otherwise be too long as macros.
18 */
19 
20 /* set import/export definitions */
21 #ifndef U_UTF8_IMPL
22 #   define U_UTF8_IMPL
23 #endif
24 
25 #include "unicode/utypes.h"
26 #include "unicode/utf.h"
27 #include "unicode/utf8.h"
28 #include "unicode/utf_old.h"
29 #include "uassert.h"
30 
31 /*
32  * This table could be replaced on many machines by
33  * a few lines of assembler code using an
34  * "index of first 0-bit from msb" instruction and
35  * one or two more integer instructions.
36  *
37  * For example, on an i386, do something like
38  * - MOV AL, leadByte
39  * - NOT AL         (8-bit, leave b15..b8==0..0, reverse only b7..b0)
40  * - MOV AH, 0
41  * - BSR BX, AX     (16-bit)
42  * - MOV AX, 6      (result)
43  * - JZ finish      (ZF==1 if leadByte==0xff)
44  * - SUB AX, BX (result)
45  * -finish:
46  * (BSR: Bit Scan Reverse, scans for a 1-bit, starting from the MSB)
47  *
48  * In Unicode, all UTF-8 byte sequences with more than 4 bytes are illegal;
49  * lead bytes above 0xf4 are illegal.
50  * We keep them in this table for skipping long ISO 10646-UTF-8 sequences.
51  */
52 U_EXPORT const uint8_t
53 utf8_countTrailBytes[256]={
54     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 
59     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 
64     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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 
69     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
70     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
71 
72     2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
73     3, 3, 3, 3, 3,
74     3, 3, 3,    /* illegal in Unicode */
75     4, 4, 4, 4, /* illegal in Unicode */
76     5, 5,       /* illegal in Unicode */
77     0, 0        /* illegal bytes 0xfe and 0xff */
78 };
79 
80 static const UChar32
81 utf8_minLegal[4]={ 0, 0x80, 0x800, 0x10000 };
82 
83 static const UChar32
84 utf8_errorValue[6]={
85     UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_2, UTF_ERROR_VALUE, 0x10ffff,
86     0x3ffffff, 0x7fffffff
87 };
88 
89 static UChar32
errorValue(int32_t count,int8_t strict)90 errorValue(int32_t count, int8_t strict) {
91     if(strict>=0) {
92         return utf8_errorValue[count];
93     } else if(strict==-3) {
94         return 0xfffd;
95     } else {
96         return U_SENTINEL;
97     }
98 }
99 
100 /*
101  * Handle the non-inline part of the U8_NEXT() and U8_NEXT_FFFD() macros
102  * and their obsolete sibling UTF8_NEXT_CHAR_SAFE().
103  *
104  * U8_NEXT() supports NUL-terminated strings indicated via length<0.
105  *
106  * The "strict" parameter controls the error behavior:
107  * <0  "Safe" behavior of U8_NEXT():
108  *     -1: All illegal byte sequences yield U_SENTINEL=-1.
109  *     -2: Same as -1, except for lenient treatment of surrogate code points as legal.
110  *         Some implementations use this for roundtripping of
111  *         Unicode 16-bit strings that are not well-formed UTF-16, that is, they
112  *         contain unpaired surrogates.
113  *     -3: All illegal byte sequences yield U+FFFD.
114  *  0  Obsolete "safe" behavior of UTF8_NEXT_CHAR_SAFE(..., FALSE):
115  *     All illegal byte sequences yield a positive code point such that this
116  *     result code point would be encoded with the same number of bytes as
117  *     the illegal sequence.
118  * >0  Obsolete "strict" behavior of UTF8_NEXT_CHAR_SAFE(..., TRUE):
119  *     Same as the obsolete "safe" behavior, but non-characters are also treated
120  *     like illegal sequences.
121  *
122  * Note that a UBool is the same as an int8_t.
123  */
124 U_CAPI UChar32 U_EXPORT2
utf8_nextCharSafeBody(const uint8_t * s,int32_t * pi,int32_t length,UChar32 c,UBool strict)125 utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict) {
126     int32_t i=*pi;
127     uint8_t count=U8_COUNT_TRAIL_BYTES(c);
128     U_ASSERT(count <= 5); /* U8_COUNT_TRAIL_BYTES returns value 0...5 */
129     if(i+count<=length || length<0) {
130         uint8_t trail;
131 
132         U8_MASK_LEAD_BYTE(c, count);
133         /* support NUL-terminated strings: do not read beyond the first non-trail byte */
134         switch(count) {
135         /* each branch falls through to the next one */
136         case 0:
137             /* count==0 for illegally leading trail bytes and the illegal bytes 0xfe and 0xff */
138         case 5:
139         case 4:
140             /* count>=4 is always illegal: no more than 3 trail bytes in Unicode's UTF-8 */
141             break;
142         case 3:
143             trail=s[i++]-0x80;
144             c=(c<<6)|trail;
145             /* c>=0x110 would result in code point>0x10ffff, outside Unicode */
146             if(c>=0x110 || trail>0x3f) { break; }
147         case 2:
148             trail=s[i++]-0x80;
149             c=(c<<6)|trail;
150             /*
151              * test for a surrogate d800..dfff unless we are lenient:
152              * before the last (c<<6), a surrogate is c=360..37f
153              */
154             if(((c&0xffe0)==0x360 && strict!=-2) || trail>0x3f) { break; }
155         case 1:
156             trail=s[i++]-0x80;
157             c=(c<<6)|trail;
158             if(trail>0x3f) { break; }
159             /* correct sequence - all trail bytes have (b7..b6)==(10) */
160             if(c>=utf8_minLegal[count] &&
161                     /* strict: forbid non-characters like U+fffe */
162                     (strict<=0 || !U_IS_UNICODE_NONCHAR(c))) {
163                 *pi=i;
164                 return c;
165             }
166         /* no default branch to optimize switch()  - all values are covered */
167         }
168     } else {
169         /* too few bytes left */
170         count=length-i;
171     }
172 
173     /* error handling */
174     i=*pi;
175     while(count>0 && U8_IS_TRAIL(s[i])) {
176         ++i;
177         --count;
178     }
179     c=errorValue(i-*pi, strict);
180     *pi=i;
181     return c;
182 }
183 
184 U_CAPI int32_t U_EXPORT2
utf8_appendCharSafeBody(uint8_t * s,int32_t i,int32_t length,UChar32 c,UBool * pIsError)185 utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError) {
186     if((uint32_t)(c)<=0x7ff) {
187         if((i)+1<(length)) {
188             (s)[(i)++]=(uint8_t)(((c)>>6)|0xc0);
189             (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80);
190             return i;
191         }
192     } else if((uint32_t)(c)<=0xffff) {
193         /* Starting with Unicode 3.2, surrogate code points must not be encoded in UTF-8. */
194         if((i)+2<(length) && !U_IS_SURROGATE(c)) {
195             (s)[(i)++]=(uint8_t)(((c)>>12)|0xe0);
196             (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80);
197             (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80);
198             return i;
199         }
200     } else if((uint32_t)(c)<=0x10ffff) {
201         if((i)+3<(length)) {
202             (s)[(i)++]=(uint8_t)(((c)>>18)|0xf0);
203             (s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80);
204             (s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80);
205             (s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80);
206             return i;
207         }
208     }
209     /* c>0x10ffff or not enough space, write an error value */
210     if(pIsError!=NULL) {
211         *pIsError=TRUE;
212     } else {
213         length-=i;
214         if(length>0) {
215             int32_t offset;
216             if(length>3) {
217                 length=3;
218             }
219             s+=i;
220             offset=0;
221             c=utf8_errorValue[length-1];
222             UTF8_APPEND_CHAR_UNSAFE(s, offset, c);
223             i=i+offset;
224         }
225     }
226     return i;
227 }
228 
229 U_CAPI UChar32 U_EXPORT2
utf8_prevCharSafeBody(const uint8_t * s,int32_t start,int32_t * pi,UChar32 c,UBool strict)230 utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict) {
231     int32_t i=*pi;
232     uint8_t b, count=1, shift=6;
233 
234     if(!U8_IS_TRAIL(c)) { return errorValue(0, strict); }
235 
236     /* extract value bits from the last trail byte */
237     c&=0x3f;
238 
239     for(;;) {
240         if(i<=start) {
241             /* no lead byte at all */
242             return errorValue(0, strict);
243         }
244 
245         /* read another previous byte */
246         b=s[--i];
247         if((uint8_t)(b-0x80)<0x7e) { /* 0x80<=b<0xfe */
248             if(b&0x40) {
249                 /* lead byte, this will always end the loop */
250                 uint8_t shouldCount=U8_COUNT_TRAIL_BYTES(b);
251 
252                 if(count==shouldCount) {
253                     /* set the new position */
254                     *pi=i;
255                     U8_MASK_LEAD_BYTE(b, count);
256                     c|=(UChar32)b<<shift;
257                     if(count>=4 || c>0x10ffff || c<utf8_minLegal[count] || (U_IS_SURROGATE(c) && strict!=-2) || (strict>0 && U_IS_UNICODE_NONCHAR(c))) {
258                         /* illegal sequence or (strict and non-character) */
259                         if(count>=4) {
260                             count=3;
261                         }
262                         c=errorValue(count, strict);
263                     } else {
264                         /* exit with correct c */
265                     }
266                 } else {
267                     /* the lead byte does not match the number of trail bytes */
268                     /* only set the position to the lead byte if it would
269                        include the trail byte that we started with */
270                     if(count<shouldCount) {
271                         *pi=i;
272                         c=errorValue(count, strict);
273                     } else {
274                         c=errorValue(0, strict);
275                     }
276                 }
277                 break;
278             } else if(count<5) {
279                 /* trail byte */
280                 c|=(UChar32)(b&0x3f)<<shift;
281                 ++count;
282                 shift+=6;
283             } else {
284                 /* more than 5 trail bytes is illegal */
285                 c=errorValue(0, strict);
286                 break;
287             }
288         } else {
289             /* single-byte character precedes trailing bytes */
290             c=errorValue(0, strict);
291             break;
292         }
293     }
294     return c;
295 }
296 
297 U_CAPI int32_t U_EXPORT2
utf8_back1SafeBody(const uint8_t * s,int32_t start,int32_t i)298 utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i) {
299     /* i had been decremented once before the function call */
300     int32_t I=i, Z;
301     uint8_t b;
302 
303     /* read at most the 6 bytes s[Z] to s[i], inclusively */
304     if(I-5>start) {
305         Z=I-5;
306     } else {
307         Z=start;
308     }
309 
310     /* return I if the sequence starting there is long enough to include i */
311     do {
312         b=s[I];
313         if((uint8_t)(b-0x80)>=0x7e) { /* not 0x80<=b<0xfe */
314             break;
315         } else if(b>=0xc0) {
316             if(U8_COUNT_TRAIL_BYTES(b)>=(i-I)) {
317                 return I;
318             } else {
319                 break;
320             }
321         }
322     } while(Z<=--I);
323 
324     /* return i itself to be consistent with the FWD_1 macro */
325     return i;
326 }
327