fixed segv for files with wrong font types
[swftools.git] / lib / utf8.c
blobacc10c4627eb501aa03f570fbc023db6703a4b87
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <memory.h>
4 #include "utf8.h"
6 static char utf8buf[16];
8 void writeUTF8(unsigned int charnum, char*dest)
10 dest[0] = 0;
11 if(charnum < 0x80) {
12 dest[0] = charnum;
13 dest[1] = 0;
14 } else if(charnum <0x800) {
15 /* 0000 0080-0000 07FF 110xxxxx 10xxxxxx */
16 dest[0] = 0xc0 | (charnum >> 6);
17 dest[1] = 0x80 | (charnum & 0x3f);
18 dest[2] = 0;
19 } else if(charnum < 0x10000) {
20 /* 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx */
21 dest[0] = 0xe0 | (charnum >> 12);
22 dest[1] = 0x80 |((charnum >> 6)&0x3f);
23 dest[2] = 0x80 |((charnum )&0x3f);
24 dest[3] = 0;
25 } else if(charnum < 0x200000) {
26 /* 0001 0000-001F FFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
27 dest[0] = 0xf0 | (charnum >> 18);
28 dest[1] = 0x80 |((charnum >> 12)&0x3f);
29 dest[2] = 0x80 |((charnum >> 6 )&0x3f);
30 dest[3] = 0x80 |((charnum )&0x3f);
31 dest[4] = 0;
32 } else if(charnum < 0x4000000) {
33 /* 0020 0000-03FF FFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
34 dest[0] = 0xf8 | (charnum >> 24);
35 dest[1] = 0x80 |((charnum >> 18)&0x3f);
36 dest[2] = 0x80 |((charnum >> 12)&0x3f);
37 dest[3] = 0x80 |((charnum >> 6 )&0x3f);
38 dest[4] = 0x80 |((charnum )&0x3f);
39 dest[5] = 0;
40 } else if(charnum < 0x80000000) {
41 /* 0400 0000-7FFF FFFF 1111110x 10xxxxxx ... 10xxxxxx */
42 dest[0] = 0xfc | (charnum >> 30);
43 dest[1] = 0x80 |((charnum >> 24)&0x3f);
44 dest[2] = 0x80 |((charnum >> 18)&0x3f);
45 dest[3] = 0x80 |((charnum >> 12)&0x3f);
46 dest[4] = 0x80 |((charnum >> 6 )&0x3f);
47 dest[5] = 0x80 |((charnum )&0x3f);
48 dest[6] = 0;
49 } else {
50 fprintf(stderr, "Illegal character: 0x%08x\n", charnum);
51 dest[0] = 0;
55 char* getUTF8(unsigned int charnum)
57 memset(utf8buf, 0, sizeof(utf8buf));
58 writeUTF8(charnum, utf8buf);
59 return utf8buf;