Mercurial > hg
annotate mcabber/src/utf8.c @ 363:913915140ad2
Fix segfault when mcabber receives a KILL signal before entering curses mode
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 24 Jul 2005 15:01:24 +0100 |
parents | 53dfe6aa6a23 |
children | 33b8e801ffa6 |
rev | line source |
---|---|
24 | 1 #include <stdlib.h> |
2 #include <string.h> | |
3 | |
4 #include "utf8.h" | |
5 | |
6 | |
7 /* Desc: convert UTF8 -> ASCII | |
8 * | |
9 * In : UTF8 string | |
10 * Out : ASCII string | |
11 * | |
12 * Note: it is up to the caller to free the returned string | |
13 */ | |
14 char *utf8_decode(const char *src) | |
15 { | |
327 | 16 unsigned char *ret, *aux; |
326
1bc374915787
Fix crash when utf8_decode parameter is NULL
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
17 |
1bc374915787
Fix crash when utf8_decode parameter is NULL
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
18 if (!src) return NULL; |
1bc374915787
Fix crash when utf8_decode parameter is NULL
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
19 |
327 | 20 aux = ret = calloc(1, strlen(src) + 1); |
24 | 21 |
22 while (*src) { | |
23 unsigned char lead = *src++; | |
24 if ((lead & 0xe0) == 0xc0) { | |
25 unsigned char ch2 = *src++; | |
327 | 26 *aux = ((lead & 0x1f) << 6) | (ch2 & 0x3f); |
24 | 27 } else { |
327 | 28 *aux = lead; |
24 | 29 } |
327 | 30 aux++; |
24 | 31 } |
32 | |
326
1bc374915787
Fix crash when utf8_decode parameter is NULL
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
33 return (char*)ret; |
24 | 34 } |
35 | |
36 | |
37 /* Desc: convert ASCII -> UTF8 | |
38 * | |
39 * In : ASCII string | |
40 * Out : UTF8 string | |
41 * | |
42 * Note: it is up to the caller to free the returned string | |
43 */ | |
44 char *utf8_encode(const char *src) | |
45 { | |
327 | 46 unsigned char *ret, *aux; |
326
1bc374915787
Fix crash when utf8_decode parameter is NULL
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
47 |
1bc374915787
Fix crash when utf8_decode parameter is NULL
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
48 if (!src) return NULL; |
1bc374915787
Fix crash when utf8_decode parameter is NULL
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
49 |
327 | 50 aux = ret = calloc(1, (strlen(src) * 2) + 1); |
24 | 51 |
52 while (*src) { | |
53 unsigned char ch = *src++; | |
34 | 54 if (ch < 0x80U) { |
327 | 55 *aux++ = ch; |
48 | 56 } else { /* if (ch < 0x800U) { */ |
327 | 57 *aux++ = 0xc0 | (ch >> 6); |
58 *aux++ = 0x80 | (ch & 0x3f); | |
24 | 59 } |
60 } | |
61 | |
326
1bc374915787
Fix crash when utf8_decode parameter is NULL
Mikael Berthe <mikael@lilotux.net>
parents:
49
diff
changeset
|
62 return (char*)ret; |
24 | 63 } |