]> git.pld-linux.org Git - packages/pidgin.git/blob - gaim-iconv.patch
- fix iconv in gg plugin
[packages/pidgin.git] / gaim-iconv.patch
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
4 @@ -13,6 +13,8 @@
5  
6  libgg_a_SOURCES = libgg.c \
7                   libgg.h \
8 +                 iconv_string.c \
9 +                 iconv_string.h \
10                   gg.c
11  
12  else
13 @@ -23,6 +25,8 @@
14  
15  libgg_la_SOURCES = libgg.c \
16                    libgg.h \
17 +                  iconv_string.c \
18 +                  iconv_string.h \
19                    gg.c
20  
21  endif
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
25 @@ -20,7 +20,9 @@
26   *
27   */
28  
29 +#ifdef HAVE_CONFIG_H
30  #include <config.h>
31 +#endif
32  
33  #include <netdb.h>
34  #include <unistd.h>
35 @@ -40,6 +42,7 @@
36  #endif
37  #ifdef HAVE_ICONV
38  #include <iconv.h>
39 +#include "iconv_string.h"
40  #endif
41  /* Library from EKG (Eksperymentalny Klient Gadu-Gadu) */
42  #include "libgg.h"
43 @@ -87,35 +90,12 @@
44  
45  static gchar *charset_convert(const gchar *locstr, char *encsrc, char *encdst)
46  {
47 +       gchar *result = NULL;
48  #ifdef HAVE_ICONV
49 -       gchar *dststr;
50 -       size_t loclen, dstlen;
51 -       gchar *fsave, *tsave;
52 -       size_t count;
53 -       static iconv_t cd = (iconv_t)(-1);
54 -
55 -       if (cd == (iconv_t)(-1)) {
56 -               cd = iconv_open(encdst, encsrc);
57 -               if (cd == (iconv_t)(-1)) {
58 -                       return g_strdup(locstr);
59 -               }
60 -       }
61 -
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;
67 -       tsave = dststr;
68 -       count = iconv(cd, &fsave, &loclen, &tsave, &dstlen);
69 -       if (count == -1) {
70 -               g_free(dststr);
71 -               return g_strdup(locstr);
72 -       }
73 -       return dststr;
74 -#else
75 -       return g_strdup(locstr);
76 +        if (iconv_string(encdst, encsrc, locstr, locstr+strlen(locstr)+1, &result, NULL) < 0)
77  #endif
78 +       return g_strdup(locstr);
79 +       return result;
80  }
81  
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
86 @@ -0,0 +1,161 @@
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.  */
90 +
91 +#ifdef HAVE_CONFIG_H
92 +#include <config.h>
93 +#endif
94 +
95 +#ifdef HAVE_ICONV
96 +#include "iconv_string.h"
97 +#include <iconv.h>
98 +#include <errno.h>
99 +#include <stdlib.h>
100 +#include <string.h>
101 +
102 +#define tmpbufsize 4096
103 +
104 +int iconv_string (const char* tocode, const char* fromcode,
105 +                  const char* start, const char* end,
106 +                  char** resultp, size_t* lengthp)
107 +{
108 +  iconv_t cd = iconv_open(tocode,fromcode);
109 +  size_t length;
110 +  char* result;
111 +  if (cd == (iconv_t)(-1)) {
112 +    if (errno != EINVAL)
113 +      return -1;
114 +    /* Unsupported fromcode or tocode. Check whether the caller requested
115 +       autodetection. */
116 +    if (!strcmp(fromcode,"autodetect_utf8")) {
117 +      int ret;
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))
122 +        return ret;
123 +      ret = iconv_string(tocode,"ISO-8859-1",start,end,resultp,lengthp);
124 +      return ret;
125 +    }
126 +    if (!strcmp(fromcode,"autodetect_jp")) {
127 +      int ret;
128 +      /* Try 7-bit encoding first. If the input contains bytes >= 0x80,
129 +         it will fail. */
130 +      ret = iconv_string(tocode,"ISO-2022-JP-2",start,end,resultp,lengthp);
131 +      if (!(ret < 0 && errno == EILSEQ))
132 +        return ret;
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))
140 +        return ret;
141 +      /* Finally try SHIFT_JIS. */
142 +      ret = iconv_string(tocode,"SHIFT_JIS",start,end,resultp,lengthp);
143 +      return ret;
144 +    }
145 +    if (!strcmp(fromcode,"autodetect_kr")) {
146 +      int ret;
147 +      /* Try 7-bit encoding first. If the input contains bytes >= 0x80,
148 +         it will fail. */
149 +      ret = iconv_string(tocode,"ISO-2022-KR",start,end,resultp,lengthp);
150 +      if (!(ret < 0 && errno == EILSEQ))
151 +        return ret;
152 +      /* Finally try EUC-KR. */
153 +      ret = iconv_string(tocode,"EUC-KR",start,end,resultp,lengthp);
154 +      return ret;
155 +    }
156 +    errno = EINVAL;
157 +    return -1;
158 +  }
159 +  /* Determine the length we need. */
160 +  {
161 +    size_t count = 0;
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)
171 +          break;
172 +        else {
173 +          int saved_errno = errno;
174 +          iconv_close(cd);
175 +          errno = saved_errno;
176 +          return -1;
177 +        }
178 +      }
179 +      count += outptr-tmpbuf;
180 +    }
181 +    {
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;
187 +        iconv_close(cd);
188 +        errno = saved_errno;
189 +        return -1;
190 +      }
191 +      count += outptr-tmpbuf;
192 +    }
193 +    length = count;
194 +  }
195 +  if (lengthp != NULL)
196 +    *lengthp = length;
197 +  if (resultp == NULL) {
198 +    iconv_close(cd);
199 +    return 0;
200 +  }
201 +  result = (*resultp == NULL ? malloc(length) : realloc(*resultp,length));
202 +  *resultp = result;
203 +  if (length == 0) {
204 +    iconv_close(cd);
205 +    return 0;
206 +  }
207 +  if (result == NULL) {
208 +    iconv_close(cd);
209 +    errno = ENOMEM;
210 +    return -1;
211 +  }
212 +  iconv(cd,NULL,NULL,NULL,NULL); /* return to the initial state */
213 +  /* Do the conversion for real. */
214 +  {
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)
223 +          break;
224 +        else {
225 +          int saved_errno = errno;
226 +          iconv_close(cd);
227 +          errno = saved_errno;
228 +          return -1;
229 +        }
230 +      }
231 +    }
232 +    {
233 +      size_t res = iconv(cd,NULL,NULL,&outptr,&outsize);
234 +      if (res == (size_t)(-1)) {
235 +        int saved_errno = errno;
236 +        iconv_close(cd);
237 +        errno = saved_errno;
238 +        return -1;
239 +      }
240 +    }
241 +    if (outsize != 0) abort();
242 +  }
243 +  iconv_close(cd);
244 +  return 0;
245 +}
246 +
247 +#endif
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
251 @@ -0,0 +1,47 @@
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.  */
255 +
256 +/*
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.
260 + *
261 + *   int iconv_string (const char* tocode, const char* fromcode,
262 + *                     const char* start, const char* end,
263 + *                     char** resultp, size_t* lengthp)
264 + *
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.
274 + *
275 + * This function does not treat zero characters specially.
276 + *
277 + * Return value: 0 if successful, otherwise -1 and errno set. Particular
278 + * errno values: EILSEQ and ENOMEM.
279 + *
280 + * Example:
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");
286 + *
287 + */
288 +#include <stddef.h>
289 +
290 +#ifdef __cplusplus
291 +extern "C" {
292 +#endif
293 +
294 +extern int iconv_string (const char* tocode, const char* fromcode, const char* start, const char* end, char** resultp, size_t* lengthp);
295 +
296 +#ifdef __cplusplus
297 +}
298 +#endif
This page took 0.07668 seconds and 3 git commands to generate.