Back to index

php5  5.3.10
utf8_to_utf16.c
Go to the documentation of this file.
00001 /* utf8_to_utf16.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_to_utf16.h"
00030 #include "utf8_decode.h"
00031 
00032 int 
00033 utf8_to_utf16(unsigned short w[], char p[], int length) 
00034 {
00035     int c;
00036     int the_index = 0;
00037     json_utf8_decode utf8;
00038     
00039     utf8_decode_init(&utf8, p, length);
00040     for (;;) {
00041         c = utf8_decode_next(&utf8);
00042         if (c < 0) {
00043             return (c == UTF8_END) ? the_index : UTF8_ERROR;
00044         }
00045         if (c < 0x10000) {
00046             w[the_index] = (unsigned short)c;
00047             the_index += 1;
00048         } else {
00049             c -= 0x10000;
00050             w[the_index] = (unsigned short)(0xD800 | (c >> 10));
00051             the_index += 1;
00052             w[the_index] = (unsigned short)(0xDC00 | (c & 0x3FF));
00053             the_index += 1;
00054         }
00055     }
00056 }