security/sandbox/chromium/base/third_party/icu/icu_utf.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/sandbox/chromium/base/third_party/icu/icu_utf.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,228 @@
     1.4 +/*
     1.5 +******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 1999-2006, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +******************************************************************************
    1.11 +*   file name:  utf_impl.c
    1.12 +*   encoding:   US-ASCII
    1.13 +*   tab size:   8 (not used)
    1.14 +*   indentation:4
    1.15 +*
    1.16 +*   created on: 1999sep13
    1.17 +*   created by: Markus W. Scherer
    1.18 +*
    1.19 +*   This file provides implementation functions for macros in the utfXX.h
    1.20 +*   that would otherwise be too long as macros.
    1.21 +*/
    1.22 +
    1.23 +#include "base/third_party/icu/icu_utf.h"
    1.24 +
    1.25 +namespace base_icu {
    1.26 +
    1.27 +/**
    1.28 + * UTF8_ERROR_VALUE_1 and UTF8_ERROR_VALUE_2 are special error values for UTF-8,
    1.29 + * which need 1 or 2 bytes in UTF-8:
    1.30 + * \code
    1.31 + * U+0015 = NAK = Negative Acknowledge, C0 control character
    1.32 + * U+009f = highest C1 control character
    1.33 + * \endcode
    1.34 + *
    1.35 + * These are used by UTF8_..._SAFE macros so that they can return an error value
    1.36 + * that needs the same number of code units (bytes) as were seen by
    1.37 + * a macro. They should be tested with UTF_IS_ERROR() or UTF_IS_VALID().
    1.38 + *
    1.39 + * @deprecated ICU 2.4. Obsolete, see utf_old.h.
    1.40 + */
    1.41 +#define CBUTF8_ERROR_VALUE_1 0x15
    1.42 +
    1.43 +/**
    1.44 + * See documentation on UTF8_ERROR_VALUE_1 for details.
    1.45 + *
    1.46 + * @deprecated ICU 2.4. Obsolete, see utf_old.h.
    1.47 + */
    1.48 +#define CBUTF8_ERROR_VALUE_2 0x9f
    1.49 +
    1.50 +
    1.51 +/**
    1.52 + * Error value for all UTFs. This code point value will be set by macros with e>
    1.53 + * checking if an error is detected.
    1.54 + *
    1.55 + * @deprecated ICU 2.4. Obsolete, see utf_old.h.
    1.56 + */
    1.57 +#define CBUTF_ERROR_VALUE 0xffff
    1.58 +
    1.59 +/*
    1.60 + * This table could be replaced on many machines by
    1.61 + * a few lines of assembler code using an
    1.62 + * "index of first 0-bit from msb" instruction and
    1.63 + * one or two more integer instructions.
    1.64 + *
    1.65 + * For example, on an i386, do something like
    1.66 + * - MOV AL, leadByte
    1.67 + * - NOT AL         (8-bit, leave b15..b8==0..0, reverse only b7..b0)
    1.68 + * - MOV AH, 0
    1.69 + * - BSR BX, AX     (16-bit)
    1.70 + * - MOV AX, 6      (result)
    1.71 + * - JZ finish      (ZF==1 if leadByte==0xff)
    1.72 + * - SUB AX, BX (result)
    1.73 + * -finish:
    1.74 + * (BSR: Bit Scan Reverse, scans for a 1-bit, starting from the MSB)
    1.75 + *
    1.76 + * In Unicode, all UTF-8 byte sequences with more than 4 bytes are illegal;
    1.77 + * lead bytes above 0xf4 are illegal.
    1.78 + * We keep them in this table for skipping long ISO 10646-UTF-8 sequences.
    1.79 + */
    1.80 +const uint8
    1.81 +utf8_countTrailBytes[256]={
    1.82 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.83 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.84 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.85 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.86 +
    1.87 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.88 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.89 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.90 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.91 +
    1.92 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.93 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.94 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.95 +    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    1.96 +
    1.97 +    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1.98 +    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1.99 +
   1.100 +    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
   1.101 +    3, 3, 3, 3, 3,
   1.102 +    3, 3, 3,    /* illegal in Unicode */
   1.103 +    4, 4, 4, 4, /* illegal in Unicode */
   1.104 +    5, 5,       /* illegal in Unicode */
   1.105 +    0, 0        /* illegal bytes 0xfe and 0xff */
   1.106 +};
   1.107 +
   1.108 +static const UChar32
   1.109 +utf8_minLegal[4]={ 0, 0x80, 0x800, 0x10000 };
   1.110 +
   1.111 +static const UChar32
   1.112 +utf8_errorValue[6]={
   1.113 +    CBUTF8_ERROR_VALUE_1, CBUTF8_ERROR_VALUE_2, CBUTF_ERROR_VALUE, 0x10ffff,
   1.114 +    0x3ffffff, 0x7fffffff
   1.115 +};
   1.116 +
   1.117 +/*
   1.118 + * Handle the non-inline part of the U8_NEXT() macro and its obsolete sibling
   1.119 + * UTF8_NEXT_CHAR_SAFE().
   1.120 + *
   1.121 + * The "strict" parameter controls the error behavior:
   1.122 + * <0  "Safe" behavior of U8_NEXT(): All illegal byte sequences yield a negative
   1.123 + *     code point result.
   1.124 + *  0  Obsolete "safe" behavior of UTF8_NEXT_CHAR_SAFE(..., FALSE):
   1.125 + *     All illegal byte sequences yield a positive code point such that this
   1.126 + *     result code point would be encoded with the same number of bytes as
   1.127 + *     the illegal sequence.
   1.128 + * >0  Obsolete "strict" behavior of UTF8_NEXT_CHAR_SAFE(..., TRUE):
   1.129 + *     Same as the obsolete "safe" behavior, but non-characters are also treated
   1.130 + *     like illegal sequences.
   1.131 + *
   1.132 + * The special negative (<0) value -2 is used for lenient treatment of surrogate
   1.133 + * code points as legal. Some implementations use this for roundtripping of
   1.134 + * Unicode 16-bit strings that are not well-formed UTF-16, that is, they
   1.135 + * contain unpaired surrogates.
   1.136 + *
   1.137 + * Note that a UBool is the same as an int8_t.
   1.138 + */
   1.139 +UChar32
   1.140 +utf8_nextCharSafeBody(const uint8 *s, int32 *pi, int32 length, UChar32 c, UBool strict) {
   1.141 +    int32 i=*pi;
   1.142 +    uint8 count=CBU8_COUNT_TRAIL_BYTES(c);
   1.143 +    if((i)+count<=(length)) {
   1.144 +        uint8 trail, illegal=0;
   1.145 +
   1.146 +        CBU8_MASK_LEAD_BYTE((c), count);
   1.147 +        /* count==0 for illegally leading trail bytes and the illegal bytes 0xfe and 0xff */
   1.148 +        switch(count) {
   1.149 +        /* each branch falls through to the next one */
   1.150 +        case 5:
   1.151 +        case 4:
   1.152 +            /* count>=4 is always illegal: no more than 3 trail bytes in Unicode's UTF-8 */
   1.153 +            illegal=1;
   1.154 +            break;
   1.155 +        case 3:
   1.156 +            trail=s[(i)++];
   1.157 +            (c)=((c)<<6)|(trail&0x3f);
   1.158 +            if(c<0x110) {
   1.159 +                illegal|=(trail&0xc0)^0x80;
   1.160 +            } else {
   1.161 +                /* code point>0x10ffff, outside Unicode */
   1.162 +                illegal=1;
   1.163 +                break;
   1.164 +            }
   1.165 +        case 2:
   1.166 +            trail=s[(i)++];
   1.167 +            (c)=((c)<<6)|(trail&0x3f);
   1.168 +            illegal|=(trail&0xc0)^0x80;
   1.169 +        case 1:
   1.170 +            trail=s[(i)++];
   1.171 +            (c)=((c)<<6)|(trail&0x3f);
   1.172 +            illegal|=(trail&0xc0)^0x80;
   1.173 +            break;
   1.174 +        case 0:
   1.175 +            if(strict>=0) {
   1.176 +                return CBUTF8_ERROR_VALUE_1;
   1.177 +            } else {
   1.178 +                return CBU_SENTINEL;
   1.179 +            }
   1.180 +        /* no default branch to optimize switch()  - all values are covered */
   1.181 +        }
   1.182 +
   1.183 +        /*
   1.184 +         * All the error handling should return a value
   1.185 +         * that needs count bytes so that UTF8_GET_CHAR_SAFE() works right.
   1.186 +         *
   1.187 +         * Starting with Unicode 3.0.1, non-shortest forms are illegal.
   1.188 +         * Starting with Unicode 3.2, surrogate code points must not be
   1.189 +         * encoded in UTF-8, and there are no irregular sequences any more.
   1.190 +         *
   1.191 +         * U8_ macros (new in ICU 2.4) return negative values for error conditions.
   1.192 +         */
   1.193 +
   1.194 +        /* correct sequence - all trail bytes have (b7..b6)==(10)? */
   1.195 +        /* illegal is also set if count>=4 */
   1.196 +        if(illegal || (c)<utf8_minLegal[count] || (CBU_IS_SURROGATE(c) && strict!=-2)) {
   1.197 +            /* error handling */
   1.198 +            uint8 errorCount=count;
   1.199 +            /* don't go beyond this sequence */
   1.200 +            i=*pi;
   1.201 +            while(count>0 && CBU8_IS_TRAIL(s[i])) {
   1.202 +                ++(i);
   1.203 +                --count;
   1.204 +            }
   1.205 +            if(strict>=0) {
   1.206 +                c=utf8_errorValue[errorCount-count];
   1.207 +            } else {
   1.208 +                c=CBU_SENTINEL;
   1.209 +            }
   1.210 +        } else if((strict)>0 && CBU_IS_UNICODE_NONCHAR(c)) {
   1.211 +            /* strict: forbid non-characters like U+fffe */
   1.212 +            c=utf8_errorValue[count];
   1.213 +        }
   1.214 +    } else /* too few bytes left */ {
   1.215 +        /* error handling */
   1.216 +        int32 i0=i;
   1.217 +        /* don't just set (i)=(length) in case there is an illegal sequence */
   1.218 +        while((i)<(length) && CBU8_IS_TRAIL(s[i])) {
   1.219 +            ++(i);
   1.220 +        }
   1.221 +        if(strict>=0) {
   1.222 +            c=utf8_errorValue[i-i0];
   1.223 +        } else {
   1.224 +            c=CBU_SENTINEL;
   1.225 +        }
   1.226 +    }
   1.227 +    *pi=i;
   1.228 +    return c;
   1.229 +}
   1.230 +
   1.231 +}  // namespace base_icu

mercurial