utf8proc
view utf8proc.c @ 0:a0368662434c
Version 0.1
author | jbe |
---|---|
date | Fri Jun 02 12:00:00 2006 +0200 (2006-06-02) |
parents | |
children | 61a89ecc2fb9 |
line source
1 /*
2 * Copyright (c) 2006, FlexiGuided GmbH, Berlin, Germany
3 * Author: Jan Behrens <jan.behrens@flexiguided.de>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the FlexiGuided GmbH nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 *
32 * This library contains derived data from a modified version of the
33 * Unicode data files.
34 *
35 * The original data files are available at
36 * http://www.unicode.org/Public/UNIDATA/
37 *
38 * Please notice the copyright statement in the file "utf8proc_data.c".
39 *
40 */
43 /*
44 * File name: utf8proc.c
45 * Version: 0.1
46 * Last changed: 2006-05-31
47 *
48 * Description:
49 * Implementation of libutf8proc.
50 */
53 #include "utf8proc.h"
54 #include "utf8proc_data.c"
57 const int8_t utf8proc_utf8class[256] = {
58 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
59 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
60 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
61 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
62 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
63 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
64 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
65 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
71 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
72 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
73 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0 };
75 #define UTF8PROC_HANGUL_SBASE 0xAC00
76 #define UTF8PROC_HANGUL_LBASE 0x1100
77 #define UTF8PROC_HANGUL_VBASE 0x1161
78 #define UTF8PROC_HANGUL_TBASE 0x11A7
79 #define UTF8PROC_HANGUL_LCOUNT 19
80 #define UTF8PROC_HANGUL_VCOUNT 21
81 #define UTF8PROC_HANGUL_TCOUNT 28
82 #define UTF8PROC_HANGUL_NCOUNT 588
83 #define UTF8PROC_HANGUL_SCOUNT 11172
86 const char *utf8proc_errmsg(ssize_t errcode) {
87 switch (errcode) {
88 case UTF8PROC_ERROR_NOMEM:
89 return "Memory for processing UTF-8 data could not be allocated.";
90 case UTF8PROC_ERROR_OVERFLOW:
91 return "UTF-8 string is too long to be processed.";
92 case UTF8PROC_ERROR_INVALIDUTF8:
93 return "Invalid UTF-8 string";
94 case UTF8PROC_ERROR_NOTASSIGNED:
95 return "Unassigned Unicode code point found in UTF-8 string.";
96 default:
97 return "An unknown error occured while processing UTF-8 data.";
98 }
99 }
101 ssize_t utf8proc_iterate(uint8_t *str, ssize_t strlen, int32_t *dst) {
102 int length;
103 int i;
104 int32_t uc = -1;
105 *dst = -1;
106 if (!strlen) return 0;
107 length = utf8proc_utf8class[str[0]];
108 if (!length) return UTF8PROC_ERROR_INVALIDUTF8;
109 if (strlen >= 0 && length > strlen) return UTF8PROC_ERROR_INVALIDUTF8;
110 for (i=1; i<length; i++) {
111 if ((str[i] & 0xC0) != 0x80) return UTF8PROC_ERROR_INVALIDUTF8;
112 }
113 switch (length) {
114 case 1:
115 uc = str[0];
116 break;
117 case 2:
118 uc = ((str[0] & 0x1F) << 6) + (str[1] & 0x3F);
119 if (uc < 0x80) uc = -1;
120 break;
121 case 3:
122 uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
123 + (str[2] & 0x3F);
124 if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
125 (uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
126 break;
127 case 4:
128 uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
129 + ((str[2] & 0x3F) << 6) + (str[3] & 0x3F);
130 if (uc < 0x10000 || uc >= 0x110000) uc = -1;
131 break;
132 }
133 if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE)) return UTF8PROC_ERROR_INVALIDUTF8;
134 *dst = uc;
135 return length;
136 }
138 ssize_t utf8proc_encode_char(int32_t uc, uint8_t *dst) {
139 if (uc < 0x00) {
140 return 0;
141 } else if (uc < 0x80) {
142 dst[0] = uc;
143 return 1;
144 } else if (uc < 0x800) {
145 dst[0] = 0xC0 + (uc >> 6);
146 dst[1] = 0x80 + (uc & 0x3F);
147 return 2;
148 } else if (uc < 0x10000) {
149 dst[0] = 0xE0 + (uc >> 12);
150 dst[1] = 0x80 + ((uc >> 6) & 0x3F);
151 dst[2] = 0x80 + (uc & 0x3F);
152 return 3;
153 } else if (uc < 0x110000) {
154 dst[0] = 0xF0 + (uc >> 18);
155 dst[1] = 0x80 + ((uc >> 12) & 0x3F);
156 dst[2] = 0x80 + ((uc >> 6) & 0x3F);
157 dst[3] = 0x80 + (uc & 0x3F);
158 return 4;
159 } else return 0;
160 }
162 const utf8proc_property_t *utf8proc_get_property(int32_t uc) {
163 // ASSERT: uc >= 0 && uc < 0x110000
164 return utf8proc_properties + (
165 utf8proc_stage2table[
166 utf8proc_stage1table[uc >> 8] + (uc & 0xFF)
167 ]
168 );
169 }
171 ssize_t utf8proc_decompose_char(int32_t uc, int32_t *dst, ssize_t bufsize,
172 int options) {
173 // ASSERT: uc >= 0 && uc < 0x110000
174 const utf8proc_property_t *property;
175 int32_t hangul_sindex;
176 property = utf8proc_get_property(uc);
177 hangul_sindex = uc - UTF8PROC_HANGUL_SBASE;
178 if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT) {
179 int32_t hangul_tindex;
180 if (bufsize >= 1) {
181 dst[0] = UTF8PROC_HANGUL_LBASE +
182 hangul_sindex / UTF8PROC_HANGUL_NCOUNT;
183 if (bufsize >= 2) dst[1] = UTF8PROC_HANGUL_VBASE +
184 (hangul_sindex % UTF8PROC_HANGUL_NCOUNT) / UTF8PROC_HANGUL_TCOUNT;
185 }
186 hangul_tindex = hangul_sindex % UTF8PROC_HANGUL_TCOUNT;
187 if (!hangul_tindex) return 2;
188 if (bufsize >= 3) dst[2] = UTF8PROC_HANGUL_TBASE + hangul_tindex;
189 return 3;
190 } else if ((options & UTF8PROC_REJECTNA) && !property->category) {
191 return UTF8PROC_ERROR_NOTASSIGNED;
192 } else if ((options & UTF8PROC_IGNORE) && property->ignorable) {
193 return 0;
194 } else if ((options & UTF8PROC_CASEFOLD) && property->casefold_mapping) {
195 const int32_t *casefold_entry;
196 ssize_t written = 0;
197 for (casefold_entry = property->casefold_mapping;
198 *casefold_entry >= 0; casefold_entry++) {
199 written += utf8proc_decompose_char(*casefold_entry, dst+written,
200 (bufsize > written) ? (bufsize - written) : 0, options);
201 if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
202 }
203 return written;
204 } else if (property->decomp_mapping &&
205 (!property->decomp_type || (options & UTF8PROC_COMPAT))) {
206 const int32_t *decomp_entry;
207 ssize_t written = 0;
208 for (decomp_entry = property->decomp_mapping;
209 *decomp_entry >= 0; decomp_entry++) {
210 written += utf8proc_decompose_char(*decomp_entry, dst+written,
211 (bufsize > written) ? (bufsize - written) : 0, options);
212 if (written < 0) return UTF8PROC_ERROR_OVERFLOW;
213 }
214 return written;
215 } else {
216 if (bufsize >= 1) *dst = uc;
217 return 1;
218 }
219 }
221 ssize_t utf8proc_decompose(uint8_t *str, ssize_t strlen,
222 int32_t *buffer, ssize_t bufsize, int options) {
223 // strlen will be ignored, if UTF8PROC_NULLTERM is set in options
224 ssize_t wpos = 0;
225 {
226 int32_t uc;
227 ssize_t rpos = 0;
228 ssize_t decomp_result;
229 while (1) {
230 if (options & UTF8PROC_NULLTERM) {
231 rpos += utf8proc_iterate(str + rpos, -1, &uc);
232 // checking of return value is not neccessary,
233 // as 'uc' is < 0 in case of error
234 if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
235 if (rpos < 0) return UTF8PROC_ERROR_OVERFLOW;
236 if (uc == 0) break;
237 } else {
238 if (rpos >= strlen) break;
239 rpos += utf8proc_iterate(str + rpos, strlen - rpos, &uc);
240 if (uc < 0) return UTF8PROC_ERROR_INVALIDUTF8;
241 }
242 decomp_result = utf8proc_decompose_char(
243 uc, buffer + wpos, (bufsize > wpos) ? (bufsize - wpos) : 0, options
244 );
245 if (decomp_result < 0) return decomp_result;
246 wpos += decomp_result;
247 // prohibiting integer overflows due to too long strings:
248 if (wpos < 0 || wpos > SSIZE_MAX/sizeof(int32_t)/2)
249 return UTF8PROC_ERROR_OVERFLOW;
250 }
251 }
252 if (bufsize >= wpos) {
253 ssize_t pos = 0;
254 while (pos < wpos-1) {
255 int32_t uc1, uc2;
256 const utf8proc_property_t *property1, *property2;
257 uc1 = buffer[pos];
258 uc2 = buffer[pos+1];
259 property1 = utf8proc_get_property(uc1);
260 property2 = utf8proc_get_property(uc2);
261 if (property1->combining_class > property2->combining_class &&
262 property2->combining_class > 0) {
263 buffer[pos] = uc2;
264 buffer[pos+1] = uc1;
265 if (pos > 0) pos--; else pos++;
266 } else {
267 pos++;
268 }
269 }
270 }
271 return wpos;
272 }
274 ssize_t utf8proc_reencode(int32_t *buffer, ssize_t length, int options) {
275 // UTF8PROC_NULLTERM option will be ignored, 'length' is never ignored
276 // ASSERT: 'buffer' has one spare byte of free space at the end!
277 if (options & (UTF8PROC_NLF2LS | UTF8PROC_NLF2PS | UTF8PROC_STRIPCC)) {
278 ssize_t rpos;
279 ssize_t wpos = 0;
280 int32_t uc;
281 for (rpos = 0; rpos < length; rpos++) {
282 uc = buffer[rpos];
283 if (uc == 0x000D && rpos < length-1 && buffer[rpos+1] == 0x000A) rpos++;
284 if (uc == 0x000A || uc == 0x000D || uc == 0x0085 ||
285 ((options & UTF8PROC_STRIPCC) && (uc == 0x000B || uc == 0x000C))) {
286 if (options & UTF8PROC_NLF2LS) {
287 if (options & UTF8PROC_NLF2PS) {
288 buffer[wpos++] = 0x000A;
289 } else {
290 buffer[wpos++] = 0x2028;
291 }
292 } else {
293 if (options & UTF8PROC_NLF2PS) {
294 buffer[wpos++] = 0x2029;
295 } else {
296 buffer[wpos++] = 0x0020;
297 }
298 }
299 } else if ((options & UTF8PROC_STRIPCC) &&
300 (uc < 0x0020 || (uc >= 0x007F && uc < 0x00A0))) {
301 if (uc == 0x0009) buffer[wpos++] = 0x0020;
302 } else {
303 buffer[wpos++] = uc;
304 }
305 }
306 length = wpos;
307 }
308 if (options & UTF8PROC_COMPOSE) {
309 int32_t *starter = NULL;
310 int32_t current_char;
311 const utf8proc_property_t *starter_property = NULL, *current_property;
312 int16_t max_combining_class = -1;
313 ssize_t rpos;
314 ssize_t wpos = 0;
315 int32_t composition;
316 for (rpos = 0; rpos < length; rpos++) {
317 current_char = buffer[rpos];
318 current_property = utf8proc_get_property(current_char);
319 if (starter && current_property->combining_class > max_combining_class) {
320 // combination perhaps possible
321 int32_t hangul_lindex;
322 int32_t hangul_sindex;
323 hangul_lindex = *starter - UTF8PROC_HANGUL_LBASE;
324 if (hangul_lindex >= 0 && hangul_lindex < UTF8PROC_HANGUL_LCOUNT) {
325 int32_t hangul_vindex;
326 hangul_vindex = current_char - UTF8PROC_HANGUL_VBASE;
327 if (hangul_vindex >= 0 && hangul_vindex < UTF8PROC_HANGUL_VCOUNT) {
328 *starter = UTF8PROC_HANGUL_SBASE +
329 (hangul_lindex * UTF8PROC_HANGUL_VCOUNT + hangul_vindex) *
330 UTF8PROC_HANGUL_TCOUNT;
331 starter_property = NULL;
332 continue;
333 }
334 }
335 hangul_sindex = *starter - UTF8PROC_HANGUL_SBASE;
336 if (hangul_sindex >= 0 && hangul_sindex < UTF8PROC_HANGUL_SCOUNT &&
337 (hangul_sindex % UTF8PROC_HANGUL_TCOUNT) == 0) {
338 int32_t hangul_tindex;
339 hangul_tindex = current_char - UTF8PROC_HANGUL_TBASE;
340 if (hangul_tindex >= 0 && hangul_tindex < UTF8PROC_HANGUL_TCOUNT) {
341 *starter += hangul_tindex;
342 starter_property = NULL;
343 continue;
344 }
345 }
346 if (!starter_property) {
347 starter_property = utf8proc_get_property(*starter);
348 }
349 if (starter_property->comb1st_index >= 0 &&
350 current_property->comb2nd_index >= 0) {
351 composition = utf8proc_combinations[
352 starter_property->comb1st_index +
353 current_property->comb2nd_index
354 ];
355 if (composition >= 0 && (!(options & UTF8PROC_STABLE) ||
356 !(utf8proc_get_property(composition)->comp_exclusion))) {
357 *starter = composition;
358 starter_property = NULL;
359 continue;
360 }
361 }
362 }
363 buffer[wpos] = current_char;
364 if (current_property->combining_class) {
365 if (current_property->combining_class > max_combining_class) {
366 max_combining_class = current_property->combining_class;
367 }
368 } else {
369 starter = buffer + wpos;
370 starter_property = NULL;
371 max_combining_class = -1;
372 }
373 wpos++;
374 }
375 length = wpos;
376 }
377 {
378 ssize_t rpos, wpos = 0;
379 int32_t uc;
380 for (rpos = 0; rpos < length; rpos++) {
381 uc = buffer[rpos];
382 wpos += utf8proc_encode_char(uc, ((uint8_t *)buffer) + wpos);
383 }
384 ((uint8_t *)buffer)[wpos] = 0;
385 return wpos;
386 }
387 }
389 ssize_t utf8proc_map(uint8_t *str, ssize_t strlen, uint8_t **dstptr,
390 int options) {
391 int32_t *buffer;
392 ssize_t result;
393 *dstptr = NULL;
394 result = utf8proc_decompose(str, strlen, NULL, 0, options);
395 if (result < 0) return result;
396 buffer = malloc(result * sizeof(int32_t) + 1);
397 if (!buffer) return UTF8PROC_ERROR_NOMEM;
398 result = utf8proc_decompose(str, strlen, buffer, result, options);
399 if (result < 0) {
400 free(buffer);
401 return result;
402 }
403 result = utf8proc_reencode(buffer, result, options);
404 if (result < 0) {
405 free(buffer);
406 return result;
407 }
408 {
409 int32_t *newptr;
410 newptr = realloc(buffer, result+1);
411 if (newptr) buffer = newptr;
412 }
413 *dstptr = (uint8_t *)buffer;
414 return result;
415 }
417 uint8_t *utf8proc_NFD(uint8_t *str) {
418 uint8_t *retval;
419 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE);
420 return retval;
421 }
423 uint8_t *utf8proc_NFC(uint8_t *str) {
424 uint8_t *retval;
425 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
426 UTF8PROC_COMPOSE);
427 return retval;
428 }
430 uint8_t *utf8proc_NFKD(uint8_t *str) {
431 uint8_t *retval;
432 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
433 UTF8PROC_COMPAT);
434 return retval;
435 }
437 uint8_t *utf8proc_NFKC(uint8_t *str) {
438 uint8_t *retval;
439 utf8proc_map(str, 0, &retval, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
440 UTF8PROC_COMPOSE | UTF8PROC_COMPAT);
441 return retval;
442 }