76 int UTF8_getc(
const unsigned char *str,
int len,
unsigned long *val)
78 const unsigned char *
p;
81 if(len <= 0)
return 0;
85 if((*p & 0x80) == 0) {
88 }
else if((*p & 0xe0) == 0xc0) {
89 if(len < 2)
return -1;
90 if((p[1] & 0xc0) != 0x80)
return -3;
91 value = (*p++ & 0x1f) << 6;
93 if(value < 0x80)
return -4;
95 }
else if((*p & 0xf0) == 0xe0) {
96 if(len < 3)
return -1;
97 if( ((p[1] & 0xc0) != 0x80)
98 || ((p[2] & 0xc0) != 0x80) )
return -3;
99 value = (*p++ & 0xf) << 12;
100 value |= (*p++ & 0x3f) << 6;
101 value |= *p++ & 0x3f;
102 if(value < 0x800)
return -4;
104 }
else if((*p & 0xf8) == 0xf0) {
105 if(len < 4)
return -1;
106 if( ((p[1] & 0xc0) != 0x80)
107 || ((p[2] & 0xc0) != 0x80)
108 || ((p[3] & 0xc0) != 0x80) )
return -3;
109 value = ((
unsigned long)(*p++ & 0x7)) << 18;
110 value |= (*p++ & 0x3f) << 12;
111 value |= (*p++ & 0x3f) << 6;
112 value |= *p++ & 0x3f;
113 if(value < 0x10000)
return -4;
115 }
else if((*p & 0xfc) == 0xf8) {
116 if(len < 5)
return -1;
117 if( ((p[1] & 0xc0) != 0x80)
118 || ((p[2] & 0xc0) != 0x80)
119 || ((p[3] & 0xc0) != 0x80)
120 || ((p[4] & 0xc0) != 0x80) )
return -3;
121 value = ((
unsigned long)(*p++ & 0x3)) << 24;
122 value |= ((
unsigned long)(*p++ & 0x3f)) << 18;
123 value |= ((
unsigned long)(*p++ & 0x3f)) << 12;
124 value |= (*p++ & 0x3f) << 6;
125 value |= *p++ & 0x3f;
126 if(value < 0x200000)
return -4;
128 }
else if((*p & 0xfe) == 0xfc) {
129 if(len < 6)
return -1;
130 if( ((p[1] & 0xc0) != 0x80)
131 || ((p[2] & 0xc0) != 0x80)
132 || ((p[3] & 0xc0) != 0x80)
133 || ((p[4] & 0xc0) != 0x80)
134 || ((p[5] & 0xc0) != 0x80) )
return -3;
135 value = ((
unsigned long)(*p++ & 0x1)) << 30;
136 value |= ((
unsigned long)(*p++ & 0x3f)) << 24;
137 value |= ((
unsigned long)(*p++ & 0x3f)) << 18;
138 value |= ((
unsigned long)(*p++ & 0x3f)) << 12;
139 value |= (*p++ & 0x3f) << 6;
140 value |= *p++ & 0x3f;
141 if(value < 0x4000000)
return -4;
158 else if(len <= 0)
return -1;
160 if(str) *str = (
unsigned char)value;
164 if(len < 2)
return -1;
166 *str++ = (
unsigned char)(((value >> 6) & 0x1f) | 0xc0);
167 *str = (
unsigned char)((value & 0x3f) | 0x80);
171 if(value < 0x10000) {
172 if(len < 3)
return -1;
174 *str++ = (
unsigned char)(((value >> 12) & 0xf) | 0xe0);
175 *str++ = (
unsigned char)(((value >> 6) & 0x3f) | 0x80);
176 *str = (
unsigned char)((value & 0x3f) | 0x80);
180 if(value < 0x200000) {
181 if(len < 4)
return -1;
183 *str++ = (
unsigned char)(((value >> 18) & 0x7) | 0xf0);
184 *str++ = (
unsigned char)(((value >> 12) & 0x3f) | 0x80);
185 *str++ = (
unsigned char)(((value >> 6) & 0x3f) | 0x80);
186 *str = (
unsigned char)((value & 0x3f) | 0x80);
190 if(value < 0x4000000) {
191 if(len < 5)
return -1;
193 *str++ = (
unsigned char)(((value >> 24) & 0x3) | 0xf8);
194 *str++ = (
unsigned char)(((value >> 18) & 0x3f) | 0x80);
195 *str++ = (
unsigned char)(((value >> 12) & 0x3f) | 0x80);
196 *str++ = (
unsigned char)(((value >> 6) & 0x3f) | 0x80);
197 *str = (
unsigned char)((value & 0x3f) | 0x80);
201 if(len < 6)
return -1;
203 *str++ = (
unsigned char)(((value >> 30) & 0x1) | 0xfc);
204 *str++ = (
unsigned char)(((value >> 24) & 0x3f) | 0x80);
205 *str++ = (
unsigned char)(((value >> 18) & 0x3f) | 0x80);
206 *str++ = (
unsigned char)(((value >> 12) & 0x3f) | 0x80);
207 *str++ = (
unsigned char)(((value >> 6) & 0x3f) | 0x80);
208 *str = (
unsigned char)((value & 0x3f) | 0x80);