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 utf8_to_utf16(unsigned short w[], char p[], int length) /* {{{ */
33 19 : {
34 : int c;
35 19 : int the_index = 0;
36 : json_utf8_decode utf8;
37 :
38 19 : utf8_decode_init(&utf8, p, length);
39 : for (;;) {
40 4775 : c = utf8_decode_next(&utf8);
41 4775 : if (c < 0) {
42 19 : return (c == UTF8_END) ? the_index : UTF8_ERROR;
43 : }
44 4756 : if (c < 0x10000) {
45 4756 : w[the_index] = (unsigned short)c;
46 4756 : the_index += 1;
47 : } else {
48 0 : c -= 0x10000;
49 0 : w[the_index] = (unsigned short)(0xD800 | (c >> 10));
50 0 : the_index += 1;
51 0 : w[the_index] = (unsigned short)(0xDC00 | (c & 0x3FF));
52 0 : the_index += 1;
53 : }
54 4756 : }
55 : }
56 : /* }}} */
57 :
58 : /*
59 : * Local variables:
60 : * tab-width: 4
61 : * c-basic-offset: 4
62 : * End:
63 : * vim600: noet sw=4 ts=4
64 : * vim<600: noet sw=4 ts=4
65 : */
|