-
Notifications
You must be signed in to change notification settings - Fork 48
/
utf8.c
177 lines (157 loc) · 3.13 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include "utf8.h"
void utf8_to_gbk(const char* u8,char* gbk,DWORD* gbksize)
{
if(u8 == NULL)
{
return;
}
//u8->unicode
size_t size = MultiByteToWideChar(CP_UTF8,0,u8,-1,NULL,0);
wchar_t* wansi = (wchar_t *)malloc((size)*sizeof(wchar_t));
memset(wansi,0,(size)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8,0,u8,-1,wansi,size);
//unicode->ascii
size = WideCharToMultiByte(CP_ACP,0,wansi,-1,NULL,0,NULL,NULL);
char* ansi = (char *)malloc(size);
memset(ansi,0,size);
WideCharToMultiByte(CP_ACP,0,wansi,-1,ansi,size,NULL,NULL);
memcpy(gbk,ansi,size);
*gbksize = size;
//clear
free(wansi);
free(ansi);
}
void gbk_to_utf8(const char* gbk,char* u8,DWORD* u8size)
{
if(gbk == NULL)
{
return;
}
//gbk->unicode
size_t size = MultiByteToWideChar(CP_ACP,0,gbk,-1,NULL,0);
wchar_t* wansi = (wchar_t *)malloc((size)*sizeof(wchar_t));
memset(wansi,0,(size)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP,0,gbk,-1,wansi,size);
//unicode->ascii
size = WideCharToMultiByte(CP_UTF8,0,wansi,-1,NULL,0,NULL,NULL);
char* ansi = (char *)malloc(size);
memset(ansi,0,size);
WideCharToMultiByte(CP_UTF8,0,wansi,-1,ansi,size,NULL,NULL);
memcpy(u8,ansi,size);
*u8size = size;
//clear
free(wansi);
free(ansi);
}
/**
* 检查bit位是否为真
* @param value [待检测值]
* @param bit [1~8位 低~高]
* @return [1 真 0 假]
*/
static inline int CheckBit(unsigned char value, int bit)
{
unsigned char bitvalue[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
if((bit >= 1)&&(bit <= 8))
{
if(value & bitvalue[bit-1])
return(1);
else
return(0);
}
else
{
printf("FILE: %s LINE: %d -- 传入的函数参数错误! bit=[%d]\n",
__FILE__, __LINE__, bit);
return(-1);
}
}
//2 utf8 dom
//1 utf8
//0 not utf8
int IsUtf8(char* buf,int size)
{
int i;
int u8 = 0;
int u8len = 0;
int firstbyte = 0;
const char BOM[3] = {0xef,0xbb,0xbf};
if(size == 0)
{
return 0;
}
if(size >= 3 && strncmp(buf,BOM,3) == 0)
{
return 2;
}
for(i=0;i<size;i++)
{
//should be binrary file.
if(buf[i] == 0)
{
return 0;
}
if(firstbyte == 0)
{
//pure ascii
if((buf[i]&0xff) <= 127)
{
u8 = 0;
u8len = 0;
}
//out of u8 range
else if((buf[i]&0xff) > 0xef)
{
u8 = 0;
u8len = 0;
}
else
{
firstbyte = 1;
//u8 3b 1110xxxx 10xxxxxx 10xxxxxx
if(CheckBit(buf[i],7) == 1 && CheckBit(buf[i],6) == 1 && CheckBit(buf[i],5) == 0)
{
u8 = 1;
u8len++;
}
else
{
i++;
firstbyte = 0;
u8 = 0;
u8len = 0;
}
}
}
else
{
if((buf[i]&0xff) <= 127 || (buf[i]&0xff) > 0xbf)
{
firstbyte = 0;
if(u8 == 1 && u8len % 3 != 0)
{
u8 = 0;
}
else if(u8 == 1 && u8len % 3 == 0)
{
//so we mark it utf8
break;
}
}
else
{
u8len++;
}
}
}
//check tail loop
if(u8 == 1 && u8len % 3 != 0)
{
u8 = 0;
}
return u8;
}