1 diff -urN gaim-0.45.org/src/protocols/gg/Makefile.am gaim-0.45/src/protocols/gg/Makefile.am
2 --- gaim-0.45.org/src/protocols/gg/Makefile.am Sun Oct 14 19:46:14 2001
3 +++ gaim-0.45/src/protocols/gg/Makefile.am Wed Oct 17 10:05:28 2001
6 libgg_a_SOURCES = libgg.c \
15 libgg_la_SOURCES = libgg.c \
22 diff -urN gaim-0.45.org/src/protocols/gg/gg.c gaim-0.45/src/protocols/gg/gg.c
23 --- gaim-0.45.org/src/protocols/gg/gg.c Sun Oct 14 19:46:14 2001
24 +++ gaim-0.45/src/protocols/gg/gg.c Wed Oct 17 10:03:47 2001
39 +#include "iconv_string.h"
41 /* Library from EKG (Eksperymentalny Klient Gadu-Gadu) */
45 static gchar *charset_convert(const gchar *locstr, char *encsrc, char *encdst)
47 + gchar *result = NULL;
50 - size_t loclen, dstlen;
51 - gchar *fsave, *tsave;
53 - static iconv_t cd = (iconv_t)(-1);
55 - if (cd == (iconv_t)(-1)) {
56 - cd = iconv_open(encdst, encsrc);
57 - if (cd == (iconv_t)(-1)) {
58 - return g_strdup(locstr);
62 - loclen = strlen(locstr);
63 - /* we are ready for multibyte conversions */
64 - dstlen = MB_LEN_MAX * loclen;
65 - dststr = g_new0(gchar, dstlen + 1);
66 - fsave = (gchar *)locstr;
68 - count = iconv(cd, &fsave, &loclen, &tsave, &dstlen);
71 - return g_strdup(locstr);
75 - return g_strdup(locstr);
76 + if (iconv_string(encdst, encsrc, locstr, locstr+strlen(locstr)+1, &result, NULL) < 0)
78 + return g_strdup(locstr);
82 static gboolean invalid_uin(char *uin)
83 diff -urN gaim-0.45.org/src/protocols/gg/iconv_string.c gaim-0.45/src/protocols/gg/iconv_string.c
84 --- gaim-0.45.org/src/protocols/gg/iconv_string.c Thu Jan 1 01:00:00 1970
85 +++ gaim-0.45/src/protocols/gg/iconv_string.c Wed Oct 17 09:59:49 2001
87 +/* Copyright (C) 1999-2001 Bruno Haible.
88 + This file is not part of the GNU LIBICONV Library.
89 + This file is put into the public domain. */
96 +#include "iconv_string.h"
102 +#define tmpbufsize 4096
104 +int iconv_string (const char* tocode, const char* fromcode,
105 + const char* start, const char* end,
106 + char** resultp, size_t* lengthp)
108 + iconv_t cd = iconv_open(tocode,fromcode);
111 + if (cd == (iconv_t)(-1)) {
112 + if (errno != EINVAL)
114 + /* Unsupported fromcode or tocode. Check whether the caller requested
116 + if (!strcmp(fromcode,"autodetect_utf8")) {
118 + /* Try UTF-8 first. There are very few ISO-8859-1 inputs that would
119 + be valid UTF-8, but many UTF-8 inputs are valid ISO-8859-1. */
120 + ret = iconv_string(tocode,"UTF-8",start,end,resultp,lengthp);
121 + if (!(ret < 0 && errno == EILSEQ))
123 + ret = iconv_string(tocode,"ISO-8859-1",start,end,resultp,lengthp);
126 + if (!strcmp(fromcode,"autodetect_jp")) {
128 + /* Try 7-bit encoding first. If the input contains bytes >= 0x80,
130 + ret = iconv_string(tocode,"ISO-2022-JP-2",start,end,resultp,lengthp);
131 + if (!(ret < 0 && errno == EILSEQ))
133 + /* Try EUC-JP next. Short SHIFT_JIS inputs may come out wrong. This
134 + is unavoidable. People will condemn SHIFT_JIS.
135 + If we tried SHIFT_JIS first, then some short EUC-JP inputs would
136 + come out wrong, and people would condemn EUC-JP and Unix, which
137 + would not be good. */
138 + ret = iconv_string(tocode,"EUC-JP",start,end,resultp,lengthp);
139 + if (!(ret < 0 && errno == EILSEQ))
141 + /* Finally try SHIFT_JIS. */
142 + ret = iconv_string(tocode,"SHIFT_JIS",start,end,resultp,lengthp);
145 + if (!strcmp(fromcode,"autodetect_kr")) {
147 + /* Try 7-bit encoding first. If the input contains bytes >= 0x80,
149 + ret = iconv_string(tocode,"ISO-2022-KR",start,end,resultp,lengthp);
150 + if (!(ret < 0 && errno == EILSEQ))
152 + /* Finally try EUC-KR. */
153 + ret = iconv_string(tocode,"EUC-KR",start,end,resultp,lengthp);
159 + /* Determine the length we need. */
162 + char tmpbuf[tmpbufsize];
163 + const char* inptr = start;
164 + size_t insize = end-start;
165 + while (insize > 0) {
166 + char* outptr = tmpbuf;
167 + size_t outsize = tmpbufsize;
168 + size_t res = iconv(cd,&inptr,&insize,&outptr,&outsize);
169 + if (res == (size_t)(-1)) {
170 + if (errno == EINVAL)
173 + int saved_errno = errno;
175 + errno = saved_errno;
179 + count += outptr-tmpbuf;
182 + char* outptr = tmpbuf;
183 + size_t outsize = tmpbufsize;
184 + size_t res = iconv(cd,NULL,NULL,&outptr,&outsize);
185 + if (res == (size_t)(-1)) {
186 + int saved_errno = errno;
188 + errno = saved_errno;
191 + count += outptr-tmpbuf;
195 + if (lengthp != NULL)
197 + if (resultp == NULL) {
201 + result = (*resultp == NULL ? malloc(length) : realloc(*resultp,length));
207 + if (result == NULL) {
212 + iconv(cd,NULL,NULL,NULL,NULL); /* return to the initial state */
213 + /* Do the conversion for real. */
215 + const char* inptr = start;
216 + size_t insize = end-start;
217 + char* outptr = result;
218 + size_t outsize = length;
219 + while (insize > 0) {
220 + size_t res = iconv(cd,&inptr,&insize,&outptr,&outsize);
221 + if (res == (size_t)(-1)) {
222 + if (errno == EINVAL)
225 + int saved_errno = errno;
227 + errno = saved_errno;
233 + size_t res = iconv(cd,NULL,NULL,&outptr,&outsize);
234 + if (res == (size_t)(-1)) {
235 + int saved_errno = errno;
237 + errno = saved_errno;
241 + if (outsize != 0) abort();
248 diff -urN gaim-0.45.org/src/protocols/gg/iconv_string.h gaim-0.45/src/protocols/gg/iconv_string.h
249 --- gaim-0.45.org/src/protocols/gg/iconv_string.h Thu Jan 1 01:00:00 1970
250 +++ gaim-0.45/src/protocols/gg/iconv_string.h Mon Mar 19 21:22:17 2001
252 +/* Copyright (C) 1999-2001 Bruno Haible.
253 + This file is not part of the GNU LIBICONV Library.
254 + This file is put into the public domain. */
257 + * This C function converts an entire string from one encoding to another,
258 + * using iconv. Easier to use than iconv() itself, and supports autodetect
259 + * encodings on input.
261 + * int iconv_string (const char* tocode, const char* fromcode,
262 + * const char* start, const char* end,
263 + * char** resultp, size_t* lengthp)
265 + * Converts a memory region given in encoding FROMCODE to a new memory
266 + * region in encoding TOCODE. FROMCODE and TOCODE are as for iconv_open(3),
267 + * except that FROMCODE may be one of the values
268 + * "autodetect_utf8" supports ISO-8859-1 and UTF-8
269 + * "autodetect_jp" supports EUC-JP, ISO-2022-JP-2 and SHIFT_JIS
270 + * "autodetect_kr" supports EUC-KR and ISO-2022-KR
271 + * The input is in the memory region between start (inclusive) and end
272 + * (exclusive). If resultp is not NULL, the output string is stored in
273 + * *resultp; malloc/realloc is used to allocate the result.
275 + * This function does not treat zero characters specially.
277 + * Return value: 0 if successful, otherwise -1 and errno set. Particular
278 + * errno values: EILSEQ and ENOMEM.
281 + * const char* s = ...;
282 + * char* result = NULL;
283 + * if (iconv_string("UCS-4-INTERNAL", "autodetect_utf8",
284 + * s, s+strlen(s)+1, &result, NULL) < 0)
285 + * perror("iconv_string");
294 +extern int iconv_string (const char* tocode, const char* fromcode, const char* start, const char* end, char** resultp, size_t* lengthp);