-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf16.c
More file actions
62 lines (53 loc) · 1.01 KB
/
utf16.c
File metadata and controls
62 lines (53 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @file
* UTF16 => wchar_t conversion
*/
#include <wchar.h>
#include <stdint.h>
#include "myendian.h"
int
utf16LE_wchar(uint16_t *in, wchar_t *out, int len)
{
uint16_t c, c2;
int olen = 0;
while (--len >= 0) {
c = swaple16(*in++);
if (c < 0xD800 || c > 0xDFFF) {
*out++ = c;
++olen;
} else {
c2 = swaple16(*in++); --len;
*out++ = ((c & 0x3ff)<<10) | (c2 & 0x3ff);
++olen;
}
}
return olen;
}
int
utf16BE_wchar(uint16_t *in, wchar_t *out, int len)
{
uint16_t c, c2;
int olen = 0;
while (--len >= 0) {
c = swapbe16(*in++);
if (c < 0xD800 || c > 0xDFFF) {
*out++ = c;
++olen;
} else {
c2 = swapbe16(*in++); --len;
*out++ = ((c & 0x3ff)<<10) | (c2 & 0x3ff);
++olen;
}
}
return olen;
}
int
utf16BOM_wchar(uint16_t *in, wchar_t *out, int len)
{
uint16_t bom;
bom = *in++; --len;
if (bom == 0xFEFF)
return utf16BE_wchar(in, out, len);
else
return utf16LE_wchar(in, out, len);
}