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