1 /**************************************************************************
2 * Copyright (C), AirM2M Tech. Co., Ltd.
4 * Name: ucs2_to_gb2312.c
11 **************************************************************************/
17 #include "ucs2_to_gb2312_table.h"
18 #include "ucs2_to_gb2312_offset.h"
20 const uint8_t number_of_bit_1
[256] =
22 0x00, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03,
23 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
24 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
25 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
26 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
27 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
28 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
29 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
30 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
31 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
32 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
33 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
34 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
35 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
36 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
37 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
38 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04,
39 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
40 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
41 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
42 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
43 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
44 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
45 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
46 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05,
47 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
48 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
49 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
50 0x03, 0x04, 0x04, 0x05, 0x04, 0x05, 0x05, 0x06,
51 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
52 0x04, 0x05, 0x05, 0x06, 0x05, 0x06, 0x06, 0x07,
53 0x05, 0x06, 0x06, 0x07, 0x06, 0x07, 0x07, 0x08,
56 /* 0x4E00 <= ucs2 < 0xA000 */
57 static uint16_t get_ucs2_offset(uint16_t ucs2
)
59 uint16_t offset
, page
, tmp
;
60 uint8_t *mirror_ptr
, ch
;
62 page
= (ucs2
>>8) - 0x4E;
65 tmp
= ucs2
>>6; /* now 0 <= tmp < 4 */
66 offset
= ucs2_index_table_4E00_9FFF
[page
][tmp
];
67 mirror_ptr
= (uint8_t*)&ucs2_mirror_4E00_9FFF
[page
][tmp
<<3]; /* [0, 8, 16, 24] */
69 tmp
= ucs2
&0x3F; /* mod 64 */
73 offset
+= number_of_bit_1
[*mirror_ptr
];
80 { /* Ok , this ucs2 can be covert to GB2312. */
91 return (uint16_t)(-1);
94 uint16_t unicode_to_gb2312(uint16_t ucs2
, uint8_t marks
)
99 // can be convert to ASCII char
104 if((0x4E00 <= ucs2
) && (0xA000 > ucs2
))
106 uint16_t offset
= get_ucs2_offset(ucs2
);
107 if((uint16_t)(-1) != offset
)
109 gb
= ucs2_to_gb2312_table
[offset
];
114 uint16_t u16count
= sizeof(tab_UCS2_to_GBK
)/4;
115 for(uint16_t ui
=0; ui
< u16count
; ui
++)
117 if(ucs2
== tab_UCS2_to_GBK
[ui
][0])
119 gb
= tab_UCS2_to_GBK
[ui
][1];
128 /*+\NEW\liweiqiang\2013.11.26\ÍêÉÆgb2312<->ucs2(ucs2be)±àÂëת»»*/
129 static size_t iconv_ucs2_to_gb2312_endian(char **_inbuf
, size_t *inbytesleft
, char **_outbuf
, size_t *outbytesleft
, int endian
)
131 uint16_t gb2312
= 0xA1A1;
133 size_t gb_length
= 0;
134 uint16_t *ucs2buf
= (uint16_t*)*_inbuf
;
135 char *outbuf
= (char *)*_outbuf
;
136 size_t inlen
= *inbytesleft
/2;
137 size_t outlen
= *outbytesleft
;
142 if(gb_length
+2 > outlen
)
146 goto ucs2_to_gb2312_exit
;
152 ucs2
= (ucs2
<<8)|(ucs2
>>8);
154 gb2312
= unicode_to_gb2312(ucs2
, 0);
159 // can be convert to ASCII char
160 *outbuf
++ = (uint8_t)gb2312
;
165 *outbuf
++ = (uint8_t)(gb2312
>>8);
166 *outbuf
++ = (uint8_t)(gb2312
);
180 *inbytesleft
= inlen
;
181 *outbytesleft
-= gb_length
;
186 size_t iconv_ucs2_to_gb2312(char **_inbuf
, size_t *inbytesleft
, char **_outbuf
, size_t *outbytesleft
)
188 return iconv_ucs2_to_gb2312_endian(_inbuf
, inbytesleft
, _outbuf
, outbytesleft
, 0);
191 size_t iconv_ucs2be_to_gb2312(char **_inbuf
, size_t *inbytesleft
, char **_outbuf
, size_t *outbytesleft
)
193 return iconv_ucs2_to_gb2312_endian(_inbuf
, inbytesleft
, _outbuf
, outbytesleft
, 1);
195 /*-\NEW\liweiqiang\2013.11.26\ÍêÉÆgb2312<->ucs2(ucs2be)±àÂëת»»*/