annotate mcabber/utf8.c @ 0:b3b2332715fb

Tailorization of /trunk Import of the upstream sources from Repository: file:///tmp/svn-mcabber Module: /trunk Revision: 15
author tailor@frmp8452
date Thu, 30 Jun 2005 21:39:31 +0000
parents
children
rev   line source
0
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
1 #include <stdlib.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
2 #include <string.h>
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
3
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
4 #include "utf8.h"
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
5
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
6
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
7 /* Desc: convert UTF8 -> ASCII
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
8 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
9 * In : UTF8 string
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
10 * Out : ASCII string
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
11 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
12 * Note: it is up to the caller to free the returned string
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
13 */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
14 char *utf8_decode(const char *src)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
15 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
16 char *ret = calloc(1, strlen(src) + 1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
17 char *aux = ret;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
18
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
19 while (*src) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
20 unsigned char lead = *src++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
21 if ((lead & 0xe0) == 0xc0) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
22 unsigned char ch2 = *src++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
23 *aux = ((lead & 0x1f) << 6) | (ch2 & 0x3f);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
24 } else {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
25 *aux = lead;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
26 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
27 aux++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
28 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
29
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
30 return ret;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
31 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
32
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
33
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
34 /* Desc: convert ASCII -> UTF8
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
35 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
36 * In : ASCII string
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
37 * Out : UTF8 string
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
38 *
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
39 * Note: it is up to the caller to free the returned string
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
40 */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
41 char *utf8_encode(const char *src)
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
42 {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
43 char *ret = calloc(1, (strlen(src) * 2) + 1);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
44 char *aux = ret;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
45
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
46 while (*src) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
47 unsigned char ch = *src++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
48 if (ch < 0x80) {
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
49 *aux = ch;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
50 } else { /* if (ch < 0x800) { */
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
51 *aux++ = 0xc0 | (ch >> 6 & 0x1f);
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
52 *aux = 0xc0 | (0x80 | (ch & 0x3f));
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
53 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
54 aux++;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
55 }
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
56
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
57 return ret;
b3b2332715fb Tailorization of /trunk
tailor@frmp8452
parents:
diff changeset
58 }