Back to index

php5  5.3.10
utf8_decode.c
Go to the documentation of this file.
00001 /* utf8_decode.c */
00002 
00003 /* 2005-12-25 */
00004 
00005 /*
00006 Copyright (c) 2005 JSON.org
00007 
00008 Permission is hereby granted, free of charge, to any person obtaining a copy
00009 of this software and associated documentation files (the "Software"), to deal
00010 in the Software without restriction, including without limitation the rights
00011 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012 copies of the Software, and to permit persons to whom the Software is
00013 furnished to do so, subject to the following conditions:
00014 
00015 The above copyright notice and this permission notice shall be included in all
00016 copies or substantial portions of the Software.
00017 
00018 The Software shall be used for Good, not Evil.
00019 
00020 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00021 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00022 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00023 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00024 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00025 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00026 SOFTWARE.
00027 */
00028 
00029 #include "utf8_decode.h"
00030 
00031 /*
00032     Very Strict UTF-8 Decoder
00033 
00034     UTF-8 is a multibyte character encoding of Unicode. A character can be
00035     represented by 1-4 bytes. The bit pattern of the first byte indicates the
00036     number of continuation bytes.
00037 
00038     Most UTF-8 decoders tend to be lenient, attempting to recover as much
00039     information as possible, even from badly encoded input. This UTF-8
00040     decoder is not lenient. It will reject input which does not include
00041     proper continuation bytes. It will reject aliases (or suboptimal
00042     codings). It will reject surrogates. (Surrogate encoding should only be
00043     used with UTF-16.)
00044 
00045     Code     Contination Minimum Maximum
00046     0xxxxxxx           0       0     127
00047     10xxxxxx       error
00048     110xxxxx           1     128    2047
00049     1110xxxx           2    2048   65535 excluding 55296 - 57343
00050     11110xxx           3   65536 1114111
00051     11111xxx       error
00052 */
00053 
00054 
00055 /*
00056     Get the next byte. It returns UTF8_END if there are no more bytes.
00057 */
00058 static int 
00059 get(json_utf8_decode *utf8)
00060 {
00061     int c;
00062     if (utf8->the_index >= utf8->the_length) {
00063         return UTF8_END;
00064     }
00065     c = utf8->the_input[utf8->the_index] & 0xFF;
00066     utf8->the_index += 1;
00067     return c;
00068 }
00069 
00070 
00071 /*
00072     Get the 6-bit payload of the next continuation byte.
00073     Return UTF8_ERROR if it is not a contination byte.
00074 */
00075 static int 
00076 cont(json_utf8_decode *utf8)
00077 {
00078     int c = get(utf8);
00079     return ((c & 0xC0) == 0x80) ? (c & 0x3F) : UTF8_ERROR;
00080 }
00081 
00082 
00083 /*
00084     Initialize the UTF-8 decoder. The decoder is not reentrant,
00085 */
00086 void 
00087 utf8_decode_init(json_utf8_decode *utf8, char p[], int length)
00088 {
00089     utf8->the_index = 0;
00090     utf8->the_input = p;
00091     utf8->the_length = length;
00092     utf8->the_char = 0;
00093     utf8->the_byte = 0;
00094 }
00095 
00096 
00097 /*
00098     Get the current byte offset. This is generally used in error reporting.
00099 */
00100 int 
00101 utf8_decode_at_byte(json_utf8_decode *utf8)
00102 {
00103     return utf8->the_byte;
00104 }
00105 
00106 
00107 /*
00108     Get the current character offset. This is generally used in error reporting.
00109     The character offset matches the byte offset if the text is strictly ASCII.
00110 */
00111 int 
00112 utf8_decode_at_character(json_utf8_decode *utf8)
00113 {
00114     return utf8->the_char > 0 ? utf8->the_char - 1 : 0;
00115 }
00116 
00117 
00118 /*
00119     Extract the next character.
00120     Returns: the character (between 0 and 1114111)
00121          or  UTF8_END   (the end)
00122          or  UTF8_ERROR (error)
00123 */
00124 int 
00125 utf8_decode_next(json_utf8_decode *utf8)
00126 {
00127     int c;  /* the first byte of the character */
00128     int r;  /* the result */
00129 
00130     if (utf8->the_index >= utf8->the_length) {
00131         return utf8->the_index == utf8->the_length ? UTF8_END : UTF8_ERROR;
00132     }
00133     utf8->the_byte = utf8->the_index;
00134     utf8->the_char += 1;
00135     c = get(utf8);
00136 /*
00137     Zero continuation (0 to 127)
00138 */
00139     if ((c & 0x80) == 0) {
00140         return c;
00141     }
00142 /*
00143     One contination (128 to 2047)
00144 */
00145     if ((c & 0xE0) == 0xC0) {
00146         int c1 = cont(utf8);
00147         if (c1 < 0) {
00148             return UTF8_ERROR;
00149         }
00150         r = ((c & 0x1F) << 6) | c1;
00151         return r >= 128 ? r : UTF8_ERROR;
00152     }
00153 /*
00154     Two continuation (2048 to 55295 and 57344 to 65535) 
00155 */
00156     if ((c & 0xF0) == 0xE0) {
00157         int c1 = cont(utf8);
00158         int c2 = cont(utf8);
00159         if (c1 < 0 || c2 < 0) {
00160             return UTF8_ERROR;
00161         }
00162         r = ((c & 0x0F) << 12) | (c1 << 6) | c2;
00163         return r >= 2048 && (r < 55296 || r > 57343) ? r : UTF8_ERROR;
00164     }
00165 /*
00166     Three continuation (65536 to 1114111)
00167 */
00168     if ((c & 0xF8) == 0xF0) {
00169         int c1 = cont(utf8);
00170         int c2 = cont(utf8);
00171         int c3 = cont(utf8);
00172         if (c1 < 0 || c2 < 0 || c3 < 0) {
00173             return UTF8_ERROR;
00174         }
00175         r = ((c & 0x0F) << 18) | (c1 << 12) | (c2 << 6) | c3;
00176         return r >= 65536 && r <= 1114111 ? r : UTF8_ERROR;
00177     }
00178     return UTF8_ERROR;
00179 }