1 : /* utf8_to_utf16.c */
2 :
3 : /* 2005-12-25 */
4 :
5 : /*
6 : Copyright (c) 2005 JSON.org
7 :
8 : Permission is hereby granted, free of charge, to any person obtaining a copy
9 : of this software and associated documentation files (the "Software"), to deal
10 : in the Software without restriction, including without limitation the rights
11 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 : copies of the Software, and to permit persons to whom the Software is
13 : furnished to do so, subject to the following conditions:
14 :
15 : The above copyright notice and this permission notice shall be included in all
16 : copies or substantial portions of the Software.
17 :
18 : The Software shall be used for Good, not Evil.
19 :
20 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 : SOFTWARE.
27 : */
28 :
29 : #include "utf8_to_utf16.h"
30 : #include "utf8_decode.h"
31 :
32 : int
33 : utf8_to_utf16(unsigned short w[], char p[], int length)
34 464 : {
35 : int c;
36 464 : int the_index = 0;
37 : json_utf8_decode utf8;
38 :
39 464 : utf8_decode_init(&utf8, p, length);
40 : for (;;) {
41 18730 : c = utf8_decode_next(&utf8);
42 18730 : if (c < 0) {
43 464 : return (c == UTF8_END) ? the_index : UTF8_ERROR;
44 : }
45 18266 : if (c < 0x10000) {
46 18249 : w[the_index] = (unsigned short)c;
47 18249 : the_index += 1;
48 : } else {
49 17 : c -= 0x10000;
50 17 : w[the_index] = (unsigned short)(0xD800 | (c >> 10));
51 17 : the_index += 1;
52 17 : w[the_index] = (unsigned short)(0xDC00 | (c & 0x3FF));
53 17 : the_index += 1;
54 : }
55 18266 : }
56 : }
|