forked from cuckoosandbox/cuckoomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutf8.c
39 lines (35 loc) · 780 Bytes
/
utf8.c
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
#include <stdio.h>
#include <windows.h>
#include "utf8.h"
int utf8_encode(unsigned short c, unsigned char *out)
{
if(c < 0x80) {
*out = c & 0x7f;
return 1;
}
else if(c < 0x800) {
*out = 0xc0 + ((c >> 8) << 2) + (c >> 6);
out[1] = 0x80 + (c & 0x3f);
return 2;
}
else {
*out = 0xe0 + (c >> 12);
out[1] = 0x80 + (((c >> 8) & 0x1f) << 2) + ((c >> 6) & 0x3);
out[2] = 0x80 + (c & 0x3f);
return 3;
}
}
int utf8_length(unsigned short x)
{
unsigned char buf[3];
return utf8_encode(x, buf);
}
int utf8_strlen_unicode(const wchar_t *s, int len)
{
if(len < 0) len = lstrlenW(s);
int ret = 0;
while (len-- != 0) {
ret += utf8_length(*s++);
}
return ret;
}