libpurple/protocols/qq/char_conv.c

branch
cpw.masca.webkit
changeset 32503
ab886d3a38ae
parent 32502
e64e49502c79
parent 31944
77d17906f1c3
child 32504
8243b910ed4c
equal deleted inserted replaced
32502:e64e49502c79 32503:ab886d3a38ae
1 /**
2 * @file char_conv.c
3 *
4 * purple
5 *
6 * Purple is the legal property of its developers, whose names are too numerous
7 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * source distribution.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 */
24
25 #include "internal.h"
26 #include "debug.h"
27
28 #include "char_conv.h"
29 #include "packet_parse.h"
30 #include "utils.h"
31
32 #define UTF8 "UTF-8"
33 #define QQ_CHARSET_ZH_CN "GB18030"
34 #define QQ_CHARSET_ENG "ISO-8859-1"
35
36 #define QQ_NULL_MSG "(NULL)" /* return this if conversion fails */
37
38 /* convert a string from from_charset to to_charset, using g_convert */
39 /* Warning: do not return NULL */
40 static gchar *do_convert(const gchar *str, gssize len, const gchar *to_charset, const gchar *from_charset)
41 {
42 GError *error = NULL;
43 gchar *ret;
44 gsize byte_read, byte_write;
45
46 g_return_val_if_fail(str != NULL && to_charset != NULL && from_charset != NULL, g_strdup(QQ_NULL_MSG));
47
48 ret = g_convert(str, len, to_charset, from_charset, &byte_read, &byte_write, &error);
49
50 if (error == NULL) {
51 return ret; /* convert is OK */
52 }
53
54 /* convert error */
55 purple_debug_error("QQ_CONVERT", "%s\n", error->message);
56 qq_show_packet("Dump failed text", (guint8 *) str, (len == -1) ? strlen(str) : len);
57
58 g_error_free(error);
59 return g_strdup(QQ_NULL_MSG);
60 }
61
62 /*
63 * take the input as a pascal string and return a converted c-string in UTF-8
64 * returns the number of bytes read, return -1 if fatal error
65 * the converted UTF-8 will be saved in ret
66 * Return: *ret != NULL
67 */
68 gint qq_get_vstr(gchar **ret, const gchar *from_charset, guint8 *data)
69 {
70 guint8 len;
71
72 g_return_val_if_fail(data != NULL && from_charset != NULL, -1);
73
74 len = data[0];
75 if (len == 0) {
76 *ret = g_strdup("");
77 return 1;
78 }
79 *ret = do_convert((gchar *) (data + 1), (gssize) len, UTF8, from_charset);
80
81 return len + 1;
82 }
83
84 gint qq_put_vstr(guint8 *buf, const gchar *str_utf8, const gchar *to_charset)
85 {
86 gchar *str;
87 guint8 len;
88
89 if (str_utf8 == NULL || (len = strlen(str_utf8)) == 0) {
90 buf[0] = 0;
91 return 1;
92 }
93 str = do_convert(str_utf8, -1, to_charset, UTF8);
94 len = strlen(str_utf8);
95 buf[0] = len;
96 if (len > 0) {
97 memcpy(buf + 1, str, len);
98 }
99 return 1 + len;
100 }
101
102 /* Warning: do not return NULL */
103 gchar *utf8_to_qq(const gchar *str, const gchar *to_charset)
104 {
105 return do_convert(str, -1, to_charset, UTF8);
106 }
107
108 /* Warning: do not return NULL */
109 gchar *qq_to_utf8(const gchar *str, const gchar *from_charset)
110 {
111 return do_convert(str, -1, UTF8, from_charset);
112 }
113

mercurial