2000-02-03 19:47:30 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
2000-02-03 19:47:30 +00:00
|
|
|
*
|
2005-08-02 18:12:39 +00:00
|
|
|
* Copyright (C) 1999-2005, International Business Machines
|
2000-02-03 19:47:30 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
2000-02-03 19:47:30 +00:00
|
|
|
* file name: utf_impl.c
|
|
|
|
* encoding: US-ASCII
|
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:4
|
|
|
|
*
|
|
|
|
* created on: 1999sep13
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*
|
|
|
|
* This file provides implementation functions for macros in the utfXX.h
|
|
|
|
* that would otherwise be too long as macros.
|
|
|
|
*/
|
|
|
|
|
2000-07-26 02:01:00 +00:00
|
|
|
/* set import/export definitions */
|
2000-07-26 20:37:53 +00:00
|
|
|
#ifndef U_UTF8_IMPL
|
|
|
|
# define U_UTF8_IMPL
|
2000-07-26 02:01:00 +00:00
|
|
|
#endif
|
|
|
|
|
2001-07-19 22:10:16 +00:00
|
|
|
#include "unicode/utypes.h"
|
2000-02-03 19:47:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This table could be replaced on many machines by
|
|
|
|
* a few lines of assembler code using an
|
|
|
|
* "index of first 0-bit from msb" instruction and
|
|
|
|
* one or two more integer instructions.
|
|
|
|
*
|
|
|
|
* For example, on an i386, do something like
|
|
|
|
* - MOV AL, leadByte
|
|
|
|
* - NOT AL (8-bit, leave b15..b8==0..0, reverse only b7..b0)
|
|
|
|
* - MOV AH, 0
|
|
|
|
* - BSR BX, AX (16-bit)
|
|
|
|
* - MOV AX, 6 (result)
|
|
|
|
* - JZ finish (ZF==1 if leadByte==0xff)
|
|
|
|
* - SUB AX, BX (result)
|
|
|
|
* -finish:
|
|
|
|
* (BSR: Bit Scan Reverse, scans for a 1-bit, starting from the MSB)
|
2001-10-12 18:53:26 +00:00
|
|
|
*
|
|
|
|
* In Unicode, all UTF-8 byte sequences with more than 4 bytes are illegal;
|
|
|
|
* lead bytes above 0xf4 are illegal.
|
|
|
|
* We keep them in this table for skipping long ISO 10646-UTF-8 sequences.
|
2000-02-03 19:47:30 +00:00
|
|
|
*/
|
2001-11-21 02:29:12 +00:00
|
|
|
U_EXPORT const uint8_t
|
2000-02-03 19:47:30 +00:00
|
|
|
utf8_countTrailBytes[256]={
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
|
|
|
|
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
2001-10-12 18:53:26 +00:00
|
|
|
3, 3, 3, 3, 3,
|
|
|
|
3, 3, 3, /* illegal in Unicode */
|
|
|
|
4, 4, 4, 4, /* illegal in Unicode */
|
|
|
|
5, 5, /* illegal in Unicode */
|
|
|
|
0, 0 /* illegal bytes 0xfe and 0xff */
|
2000-02-03 19:47:30 +00:00
|
|
|
};
|
|
|
|
|
2001-08-24 00:00:49 +00:00
|
|
|
static const UChar32
|
2001-10-12 18:53:26 +00:00
|
|
|
utf8_minLegal[4]={ 0, 0x80, 0x800, 0x10000 };
|
2000-02-03 19:47:30 +00:00
|
|
|
|
2001-08-24 00:00:49 +00:00
|
|
|
static const UChar32
|
2000-04-03 23:53:59 +00:00
|
|
|
utf8_errorValue[6]={
|
2000-02-03 19:47:30 +00:00
|
|
|
UTF8_ERROR_VALUE_1, UTF8_ERROR_VALUE_2, UTF_ERROR_VALUE, 0x10ffff,
|
2000-04-03 23:53:59 +00:00
|
|
|
0x3ffffff, 0x7fffffff
|
2000-02-03 19:47:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
U_CAPI UChar32 U_EXPORT2
|
2002-09-30 04:00:17 +00:00
|
|
|
utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t i=*pi;
|
2000-02-03 19:47:30 +00:00
|
|
|
uint8_t count=UTF8_COUNT_TRAIL_BYTES(c);
|
|
|
|
if((i)+count<=(length)) {
|
|
|
|
uint8_t trail, illegal=0;
|
|
|
|
|
|
|
|
UTF8_MASK_LEAD_BYTE((c), count);
|
2000-04-03 23:53:59 +00:00
|
|
|
/* count==0 for illegally leading trail bytes and the illegal bytes 0xfe and 0xff */
|
2000-02-03 19:47:30 +00:00
|
|
|
switch(count) {
|
|
|
|
/* each branch falls through to the next one */
|
|
|
|
case 5:
|
|
|
|
case 4:
|
2001-10-12 18:53:26 +00:00
|
|
|
/* count>=4 is always illegal: no more than 3 trail bytes in Unicode's UTF-8 */
|
|
|
|
illegal=1;
|
|
|
|
break;
|
2000-02-03 19:47:30 +00:00
|
|
|
case 3:
|
|
|
|
trail=s[(i)++];
|
|
|
|
(c)=((c)<<6)|(trail&0x3f);
|
|
|
|
if(c<0x110) {
|
|
|
|
illegal|=(trail&0xc0)^0x80;
|
|
|
|
} else {
|
|
|
|
/* code point>0x10ffff, outside Unicode */
|
|
|
|
illegal=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
trail=s[(i)++];
|
|
|
|
(c)=((c)<<6)|(trail&0x3f);
|
|
|
|
illegal|=(trail&0xc0)^0x80;
|
|
|
|
case 1:
|
|
|
|
trail=s[(i)++];
|
|
|
|
(c)=((c)<<6)|(trail&0x3f);
|
|
|
|
illegal|=(trail&0xc0)^0x80;
|
|
|
|
break;
|
|
|
|
case 0:
|
2002-09-30 04:00:17 +00:00
|
|
|
if(strict>=0) {
|
|
|
|
return UTF8_ERROR_VALUE_1;
|
|
|
|
} else {
|
|
|
|
return U_SENTINEL;
|
2002-08-03 00:54:39 +00:00
|
|
|
}
|
2000-02-03 19:47:30 +00:00
|
|
|
/* no default branch to optimize switch() - all values are covered */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All the error handling should return a value
|
|
|
|
* that needs count bytes so that UTF8_GET_CHAR_SAFE() works right.
|
2001-10-12 18:53:26 +00:00
|
|
|
*
|
|
|
|
* Starting with Unicode 3.0.1, non-shortest forms are illegal.
|
2002-07-02 00:51:16 +00:00
|
|
|
* Starting with Unicode 3.2, surrogate code points must not be
|
|
|
|
* encoded in UTF-8, and there are no irregular sequences any more.
|
2002-09-30 04:00:17 +00:00
|
|
|
*
|
|
|
|
* U8_ macros (new in ICU 2.4) return negative values for error conditions.
|
2000-02-03 19:47:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* correct sequence - all trail bytes have (b7..b6)==(10)? */
|
2001-10-12 18:53:26 +00:00
|
|
|
/* illegal is also set if count>=4 */
|
2002-07-02 00:51:16 +00:00
|
|
|
if(illegal || (c)<utf8_minLegal[count] || UTF_IS_SURROGATE(c)) {
|
2000-02-03 19:47:30 +00:00
|
|
|
/* error handling */
|
|
|
|
uint8_t errorCount=count;
|
|
|
|
/* don't go beyond this sequence */
|
2001-10-12 18:53:26 +00:00
|
|
|
i=*pi;
|
2000-02-03 19:47:30 +00:00
|
|
|
while(count>0 && UTF8_IS_TRAIL(s[i])) {
|
|
|
|
++(i);
|
|
|
|
--count;
|
|
|
|
}
|
2002-09-30 04:00:17 +00:00
|
|
|
if(strict>=0) {
|
|
|
|
c=utf8_errorValue[errorCount-count];
|
|
|
|
} else {
|
|
|
|
c=U_SENTINEL;
|
2002-08-03 00:54:39 +00:00
|
|
|
}
|
2002-09-30 04:00:17 +00:00
|
|
|
} else if((strict)>0 && UTF_IS_UNICODE_NONCHAR(c)) {
|
2002-07-02 00:51:16 +00:00
|
|
|
/* strict: forbid non-characters like U+fffe */
|
2000-02-03 19:47:30 +00:00
|
|
|
c=utf8_errorValue[count];
|
|
|
|
}
|
|
|
|
} else /* too few bytes left */ {
|
|
|
|
/* error handling */
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t i0=i;
|
2000-02-03 19:47:30 +00:00
|
|
|
/* don't just set (i)=(length) in case there is an illegal sequence */
|
|
|
|
while((i)<(length) && UTF8_IS_TRAIL(s[i])) {
|
|
|
|
++(i);
|
|
|
|
}
|
2002-09-30 04:00:17 +00:00
|
|
|
if(strict>=0) {
|
|
|
|
c=utf8_errorValue[i-i0];
|
|
|
|
} else {
|
|
|
|
c=U_SENTINEL;
|
2002-08-03 00:54:39 +00:00
|
|
|
}
|
2000-02-03 19:47:30 +00:00
|
|
|
}
|
|
|
|
*pi=i;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2002-09-30 04:00:17 +00:00
|
|
|
utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool *pIsError) {
|
|
|
|
if((uint32_t)(c)<=0x7ff) {
|
2000-02-03 19:47:30 +00:00
|
|
|
if((i)+1<(length)) {
|
2000-08-10 01:59:18 +00:00
|
|
|
(s)[(i)++]=(uint8_t)(((c)>>6)|0xc0);
|
2000-09-25 18:53:50 +00:00
|
|
|
(s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80);
|
2000-02-03 19:47:30 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
} else if((uint32_t)(c)<=0xffff) {
|
2002-07-02 00:51:16 +00:00
|
|
|
/* Starting with Unicode 3.2, surrogate code points must not be encoded in UTF-8. */
|
2002-09-30 04:00:17 +00:00
|
|
|
if((i)+2<(length) && !U_IS_SURROGATE(c)) {
|
2000-08-10 01:59:18 +00:00
|
|
|
(s)[(i)++]=(uint8_t)(((c)>>12)|0xe0);
|
2000-09-25 18:53:50 +00:00
|
|
|
(s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80);
|
|
|
|
(s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80);
|
2000-02-03 19:47:30 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
} else if((uint32_t)(c)<=0x10ffff) {
|
|
|
|
if((i)+3<(length)) {
|
2000-08-10 01:59:18 +00:00
|
|
|
(s)[(i)++]=(uint8_t)(((c)>>18)|0xf0);
|
2000-09-25 18:53:50 +00:00
|
|
|
(s)[(i)++]=(uint8_t)((((c)>>12)&0x3f)|0x80);
|
|
|
|
(s)[(i)++]=(uint8_t)((((c)>>6)&0x3f)|0x80);
|
|
|
|
(s)[(i)++]=(uint8_t)(((c)&0x3f)|0x80);
|
2000-02-03 19:47:30 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* c>0x10ffff or not enough space, write an error value */
|
2002-09-30 04:00:17 +00:00
|
|
|
if(pIsError!=NULL) {
|
|
|
|
*pIsError=TRUE;
|
|
|
|
} else {
|
|
|
|
length-=i;
|
|
|
|
if(length>0) {
|
|
|
|
int32_t offset;
|
|
|
|
if(length>3) {
|
|
|
|
length=3;
|
|
|
|
}
|
|
|
|
s+=i;
|
|
|
|
offset=0;
|
|
|
|
c=utf8_errorValue[length-1];
|
|
|
|
UTF8_APPEND_CHAR_UNSAFE(s, offset, c);
|
|
|
|
i=i+offset;
|
2000-02-03 19:47:30 +00:00
|
|
|
}
|
2002-09-30 04:00:17 +00:00
|
|
|
}
|
2000-02-03 19:47:30 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UChar32 U_EXPORT2
|
2002-03-12 01:32:42 +00:00
|
|
|
utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict) {
|
|
|
|
int32_t i=*pi;
|
2000-04-12 19:36:07 +00:00
|
|
|
uint8_t b, count=1, shift=6;
|
2000-02-03 19:47:30 +00:00
|
|
|
|
2000-04-12 19:36:07 +00:00
|
|
|
/* extract value bits from the last trail byte */
|
|
|
|
c&=0x3f;
|
2000-02-03 19:47:30 +00:00
|
|
|
|
2000-04-12 19:36:07 +00:00
|
|
|
for(;;) {
|
|
|
|
if(i<=start) {
|
|
|
|
/* no lead byte at all */
|
2002-09-30 04:00:17 +00:00
|
|
|
if(strict>=0) {
|
|
|
|
return UTF8_ERROR_VALUE_1;
|
|
|
|
} else {
|
|
|
|
return U_SENTINEL;
|
|
|
|
}
|
2005-08-02 18:12:39 +00:00
|
|
|
/*break;*/
|
2000-04-12 19:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* read another previous byte */
|
|
|
|
b=s[--i];
|
|
|
|
if((uint8_t)(b-0x80)<0x7e) { /* 0x80<=b<0xfe */
|
|
|
|
if(b&0x40) {
|
|
|
|
/* lead byte, this will always end the loop */
|
|
|
|
uint8_t shouldCount=UTF8_COUNT_TRAIL_BYTES(b);
|
|
|
|
|
|
|
|
if(count==shouldCount) {
|
|
|
|
/* set the new position */
|
|
|
|
*pi=i;
|
|
|
|
UTF8_MASK_LEAD_BYTE(b, count);
|
|
|
|
c|=(UChar32)b<<shift;
|
2003-01-10 23:40:58 +00:00
|
|
|
if(count>=4 || c>0x10ffff || c<utf8_minLegal[count] || UTF_IS_SURROGATE(c) || (strict>0 && UTF_IS_UNICODE_NONCHAR(c))) {
|
2002-07-02 00:51:16 +00:00
|
|
|
/* illegal sequence or (strict and non-character) */
|
2001-10-12 18:53:26 +00:00
|
|
|
if(count>=4) {
|
|
|
|
count=3;
|
|
|
|
}
|
2002-09-30 04:00:17 +00:00
|
|
|
if(strict>=0) {
|
|
|
|
c=utf8_errorValue[count];
|
|
|
|
} else {
|
|
|
|
c=U_SENTINEL;
|
|
|
|
}
|
2000-02-03 19:47:30 +00:00
|
|
|
} else {
|
2000-04-12 19:36:07 +00:00
|
|
|
/* exit with correct c */
|
2000-02-03 19:47:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2000-04-12 19:36:07 +00:00
|
|
|
/* the lead byte does not match the number of trail bytes */
|
|
|
|
/* only set the position to the lead byte if it would
|
|
|
|
include the trail byte that we started with */
|
|
|
|
if(count<shouldCount) {
|
|
|
|
*pi=i;
|
2002-09-30 04:00:17 +00:00
|
|
|
if(strict>=0) {
|
|
|
|
c=utf8_errorValue[count];
|
|
|
|
} else {
|
|
|
|
c=U_SENTINEL;
|
|
|
|
}
|
2000-04-12 19:36:07 +00:00
|
|
|
} else {
|
2002-09-30 04:00:17 +00:00
|
|
|
if(strict>=0) {
|
|
|
|
c=UTF8_ERROR_VALUE_1;
|
|
|
|
} else {
|
|
|
|
c=U_SENTINEL;
|
|
|
|
}
|
2000-04-12 19:36:07 +00:00
|
|
|
}
|
2000-02-03 19:47:30 +00:00
|
|
|
}
|
2000-04-12 19:36:07 +00:00
|
|
|
break;
|
|
|
|
} else if(count<5) {
|
|
|
|
/* trail byte */
|
|
|
|
c|=(UChar32)(b&0x3f)<<shift;
|
|
|
|
++count;
|
|
|
|
shift+=6;
|
2000-02-03 19:47:30 +00:00
|
|
|
} else {
|
2000-04-12 19:36:07 +00:00
|
|
|
/* more than 5 trail bytes is illegal */
|
2002-09-30 04:00:17 +00:00
|
|
|
if(strict>=0) {
|
|
|
|
c=UTF8_ERROR_VALUE_1;
|
|
|
|
} else {
|
|
|
|
c=U_SENTINEL;
|
|
|
|
}
|
2000-02-03 19:47:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2000-04-12 19:36:07 +00:00
|
|
|
} else {
|
|
|
|
/* single-byte character precedes trailing bytes */
|
2002-09-30 04:00:17 +00:00
|
|
|
if(strict>=0) {
|
|
|
|
c=UTF8_ERROR_VALUE_1;
|
|
|
|
} else {
|
|
|
|
c=U_SENTINEL;
|
|
|
|
}
|
2000-04-12 19:36:07 +00:00
|
|
|
break;
|
2000-02-03 19:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
2000-04-12 19:36:07 +00:00
|
|
|
return c;
|
2000-02-03 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
utf8_back1SafeBody(const uint8_t *s, int32_t start, int32_t i) {
|
2000-02-03 19:47:30 +00:00
|
|
|
/* i had been decremented once before the function call */
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t I=i, Z;
|
2000-02-03 19:47:30 +00:00
|
|
|
uint8_t b;
|
|
|
|
|
2000-04-03 23:53:59 +00:00
|
|
|
/* read at most the 6 bytes s[Z] to s[i], inclusively */
|
2000-04-12 19:36:07 +00:00
|
|
|
if(I-5>start) {
|
2000-04-03 23:53:59 +00:00
|
|
|
Z=I-5;
|
2000-02-03 19:47:30 +00:00
|
|
|
} else {
|
2000-04-12 19:36:07 +00:00
|
|
|
Z=start;
|
2000-02-03 19:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return I if the sequence starting there is long enough to include i */
|
2001-10-12 18:53:26 +00:00
|
|
|
do {
|
2000-02-03 19:47:30 +00:00
|
|
|
b=s[I];
|
2000-04-03 23:53:59 +00:00
|
|
|
if((uint8_t)(b-0x80)>=0x7e) { /* not 0x80<=b<0xfe */
|
2000-02-03 19:47:30 +00:00
|
|
|
break;
|
|
|
|
} else if(b>=0xc0) {
|
|
|
|
if(UTF8_COUNT_TRAIL_BYTES(b)>=(i-I)) {
|
|
|
|
return I;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-10-12 18:53:26 +00:00
|
|
|
} while(Z<=--I);
|
2000-02-03 19:47:30 +00:00
|
|
|
|
|
|
|
/* return i itself to be consistent with the FWD_1 macro */
|
|
|
|
return i;
|
|
|
|
}
|