Branch data Line data Source code
1 : : /* GIO - GLib Input, Output and Streaming Library
2 : : *
3 : : * Copyright (C) 2009 Red Hat, Inc.
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General
18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 : : *
20 : : * Author: Alexander Larsson <alexl@redhat.com>
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include "gcharsetconverter.h"
26 : :
27 : : #include <errno.h>
28 : :
29 : : #include "ginitable.h"
30 : : #include "gioerror.h"
31 : : #include "glibintl.h"
32 : :
33 : :
34 : : enum {
35 : : PROP_0,
36 : : PROP_FROM_CHARSET,
37 : : PROP_TO_CHARSET,
38 : : PROP_USE_FALLBACK
39 : : };
40 : :
41 : : /**
42 : : * GCharsetConverter:
43 : : *
44 : : * `GCharsetConverter` is an implementation of [iface@Gio.Converter] based on
45 : : * [struct@GLib.IConv].
46 : : */
47 : :
48 : : static void g_charset_converter_iface_init (GConverterIface *iface);
49 : : static void g_charset_converter_initable_iface_init (GInitableIface *iface);
50 : :
51 : : struct _GCharsetConverter
52 : : {
53 : : GObject parent_instance;
54 : :
55 : : char *from;
56 : : char *to;
57 : : GIConv iconv;
58 : : gboolean use_fallback;
59 : : guint n_fallback_errors;
60 : : };
61 : :
62 : 227 : G_DEFINE_TYPE_WITH_CODE (GCharsetConverter, g_charset_converter, G_TYPE_OBJECT,
63 : : G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
64 : : g_charset_converter_iface_init);
65 : : G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
66 : : g_charset_converter_initable_iface_init))
67 : :
68 : : static void
69 : 5 : g_charset_converter_finalize (GObject *object)
70 : : {
71 : : GCharsetConverter *conv;
72 : :
73 : 5 : conv = G_CHARSET_CONVERTER (object);
74 : :
75 : 5 : g_free (conv->from);
76 : 5 : g_free (conv->to);
77 : 5 : if (conv->iconv)
78 : 5 : g_iconv_close (conv->iconv);
79 : :
80 : 5 : G_OBJECT_CLASS (g_charset_converter_parent_class)->finalize (object);
81 : 5 : }
82 : :
83 : : static void
84 : 15 : g_charset_converter_set_property (GObject *object,
85 : : guint prop_id,
86 : : const GValue *value,
87 : : GParamSpec *pspec)
88 : : {
89 : : GCharsetConverter *conv;
90 : :
91 : 15 : conv = G_CHARSET_CONVERTER (object);
92 : :
93 : 15 : switch (prop_id)
94 : : {
95 : 5 : case PROP_TO_CHARSET:
96 : 5 : g_free (conv->to);
97 : 5 : conv->to = g_value_dup_string (value);
98 : 5 : break;
99 : :
100 : 5 : case PROP_FROM_CHARSET:
101 : 5 : g_free (conv->from);
102 : 5 : conv->from = g_value_dup_string (value);
103 : 5 : break;
104 : :
105 : 5 : case PROP_USE_FALLBACK:
106 : 5 : conv->use_fallback = g_value_get_boolean (value);
107 : 5 : break;
108 : :
109 : 0 : default:
110 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 : 0 : break;
112 : : }
113 : :
114 : 15 : }
115 : :
116 : : static void
117 : 5 : g_charset_converter_get_property (GObject *object,
118 : : guint prop_id,
119 : : GValue *value,
120 : : GParamSpec *pspec)
121 : : {
122 : : GCharsetConverter *conv;
123 : :
124 : 5 : conv = G_CHARSET_CONVERTER (object);
125 : :
126 : 5 : switch (prop_id)
127 : : {
128 : 1 : case PROP_TO_CHARSET:
129 : 1 : g_value_set_string (value, conv->to);
130 : 1 : break;
131 : :
132 : 1 : case PROP_FROM_CHARSET:
133 : 1 : g_value_set_string (value, conv->from);
134 : 1 : break;
135 : :
136 : 3 : case PROP_USE_FALLBACK:
137 : 3 : g_value_set_boolean (value, conv->use_fallback);
138 : 3 : break;
139 : :
140 : 0 : default:
141 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 : 0 : break;
143 : : }
144 : 5 : }
145 : :
146 : : static void
147 : 3 : g_charset_converter_class_init (GCharsetConverterClass *klass)
148 : : {
149 : 3 : GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
150 : :
151 : 3 : gobject_class->finalize = g_charset_converter_finalize;
152 : 3 : gobject_class->get_property = g_charset_converter_get_property;
153 : 3 : gobject_class->set_property = g_charset_converter_set_property;
154 : :
155 : : /**
156 : : * GCharsetConverter:to-charset:
157 : : *
158 : : * The character encoding to convert to.
159 : : *
160 : : * Since: 2.24
161 : : */
162 : 3 : g_object_class_install_property (gobject_class,
163 : : PROP_TO_CHARSET,
164 : : g_param_spec_string ("to-charset", NULL, NULL,
165 : : NULL,
166 : : G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
167 : : G_PARAM_STATIC_STRINGS));
168 : :
169 : : /**
170 : : * GCharsetConverter:from-charset:
171 : : *
172 : : * The character encoding to convert from.
173 : : *
174 : : * Since: 2.24
175 : : */
176 : 3 : g_object_class_install_property (gobject_class,
177 : : PROP_FROM_CHARSET,
178 : : g_param_spec_string ("from-charset", NULL, NULL,
179 : : NULL,
180 : : G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
181 : : G_PARAM_STATIC_STRINGS));
182 : :
183 : : /**
184 : : * GCharsetConverter:use-fallback:
185 : : *
186 : : * Use fallback (of form `\<hexval>`) for invalid bytes.
187 : : *
188 : : * Since: 2.24
189 : : */
190 : 3 : g_object_class_install_property (gobject_class,
191 : : PROP_USE_FALLBACK,
192 : : g_param_spec_boolean ("use-fallback", NULL, NULL,
193 : : FALSE,
194 : : G_PARAM_READWRITE |
195 : : G_PARAM_CONSTRUCT |
196 : : G_PARAM_STATIC_STRINGS));
197 : 3 : }
198 : :
199 : : static void
200 : 5 : g_charset_converter_init (GCharsetConverter *local)
201 : : {
202 : 5 : }
203 : :
204 : :
205 : : /**
206 : : * g_charset_converter_new:
207 : : * @to_charset: destination charset
208 : : * @from_charset: source charset
209 : : * @error: #GError for error reporting, or %NULL to ignore.
210 : : *
211 : : * Creates a new #GCharsetConverter.
212 : : *
213 : : * Returns: a new #GCharsetConverter or %NULL on error.
214 : : *
215 : : * Since: 2.24
216 : : **/
217 : : GCharsetConverter *
218 : 5 : g_charset_converter_new (const gchar *to_charset,
219 : : const gchar *from_charset,
220 : : GError **error)
221 : : {
222 : : GCharsetConverter *conv;
223 : :
224 : 5 : conv = g_initable_new (G_TYPE_CHARSET_CONVERTER,
225 : : NULL, error,
226 : : "to-charset", to_charset,
227 : : "from-charset", from_charset,
228 : : NULL);
229 : :
230 : 5 : return conv;
231 : : }
232 : :
233 : : static void
234 : 1 : g_charset_converter_reset (GConverter *converter)
235 : : {
236 : 1 : GCharsetConverter *conv = G_CHARSET_CONVERTER (converter);
237 : :
238 : 1 : if (conv->iconv == NULL)
239 : : {
240 : 0 : g_warning ("Invalid object, not initialized");
241 : 0 : return;
242 : : }
243 : :
244 : 1 : g_iconv (conv->iconv, NULL, NULL, NULL, NULL);
245 : 1 : conv->n_fallback_errors = 0;
246 : : }
247 : :
248 : : static GConverterResult
249 : 13 : g_charset_converter_convert (GConverter *converter,
250 : : const void *inbuf,
251 : : gsize inbuf_size,
252 : : void *outbuf,
253 : : gsize outbuf_size,
254 : : GConverterFlags flags,
255 : : gsize *bytes_read,
256 : : gsize *bytes_written,
257 : : GError **error)
258 : : {
259 : : GCharsetConverter *conv;
260 : : gsize res;
261 : : GConverterResult ret;
262 : : gchar *inbufp, *outbufp;
263 : : gsize in_left, out_left;
264 : : int errsv;
265 : : gboolean reset;
266 : :
267 : 13 : conv = G_CHARSET_CONVERTER (converter);
268 : :
269 : 13 : if (conv->iconv == NULL)
270 : : {
271 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
272 : : _("Invalid object, not initialized"));
273 : 0 : return G_CONVERTER_ERROR;
274 : : }
275 : :
276 : 13 : inbufp = (char *)inbuf;
277 : 13 : outbufp = (char *)outbuf;
278 : 13 : in_left = inbuf_size;
279 : 13 : out_left = outbuf_size;
280 : 13 : reset = FALSE;
281 : :
282 : : /* if there is not input try to flush the data */
283 : 13 : if (inbuf_size == 0)
284 : : {
285 : 3 : if (flags & G_CONVERTER_INPUT_AT_END ||
286 : 0 : flags & G_CONVERTER_FLUSH)
287 : : {
288 : 3 : reset = TRUE;
289 : : }
290 : : else
291 : : {
292 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
293 : : _("Incomplete multibyte sequence in input"));
294 : 0 : return G_CONVERTER_ERROR;
295 : : }
296 : : }
297 : :
298 : 13 : if (reset)
299 : : /* call g_iconv with NULL inbuf to cleanup shift state */
300 : 3 : res = g_iconv (conv->iconv,
301 : : NULL, &in_left,
302 : : &outbufp, &out_left);
303 : : else
304 : 10 : res = g_iconv (conv->iconv,
305 : : &inbufp, &in_left,
306 : : &outbufp, &out_left);
307 : :
308 : 13 : g_assert (inbufp >= (char *) inbuf);
309 : 13 : g_assert (outbufp >= (char *) outbuf);
310 : :
311 : 13 : *bytes_read = (size_t) (inbufp - (char *) inbuf);
312 : 13 : *bytes_written = (size_t) (outbufp - (char *) outbuf);
313 : :
314 : : /* Don't report error if we converted anything */
315 : 13 : if (res == (gsize) -1 && *bytes_read == 0)
316 : : {
317 : 5 : errsv = errno;
318 : :
319 : 5 : switch (errsv)
320 : : {
321 : 0 : case EINVAL:
322 : : /* Incomplete input text */
323 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
324 : : _("Incomplete multibyte sequence in input"));
325 : 0 : break;
326 : :
327 : 0 : case E2BIG:
328 : : /* Not enough destination space */
329 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
330 : : _("Not enough space in destination"));
331 : 0 : break;
332 : :
333 : 5 : case EILSEQ:
334 : : /* Invalid code sequence */
335 : 5 : if (conv->use_fallback)
336 : : {
337 : 4 : if (outbuf_size < 3)
338 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
339 : : _("Not enough space in destination"));
340 : : else
341 : : {
342 : 4 : const char hex[] = "0123456789ABCDEF";
343 : 4 : guint8 v = *(guint8 *)inbuf;
344 : 4 : guint8 *out = (guint8 *)outbuf;
345 : 4 : out[0] = '\\';
346 : 4 : out[1] = (guint8) hex[(v & 0xf0) >> 4];
347 : 4 : out[2] = (guint8) hex[(v & 0x0f) >> 0];
348 : 4 : *bytes_read = 1;
349 : 4 : *bytes_written = 3;
350 : 4 : in_left--;
351 : 4 : conv->n_fallback_errors++;
352 : 4 : goto ok;
353 : : }
354 : : }
355 : : else
356 : 1 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
357 : : _("Invalid byte sequence in conversion input"));
358 : 1 : break;
359 : :
360 : 0 : default:
361 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
362 : : _("Error during conversion: %s"),
363 : : g_strerror (errsv));
364 : 0 : break;
365 : : }
366 : 1 : ret = G_CONVERTER_ERROR;
367 : : }
368 : : else
369 : : {
370 : 8 : ok:
371 : 12 : ret = G_CONVERTER_CONVERTED;
372 : :
373 : 12 : if (reset &&
374 : 3 : (flags & G_CONVERTER_INPUT_AT_END))
375 : 3 : ret = G_CONVERTER_FINISHED;
376 : 9 : else if (reset &&
377 : 0 : (flags & G_CONVERTER_FLUSH))
378 : 0 : ret = G_CONVERTER_FLUSHED;
379 : : }
380 : :
381 : 13 : return ret;
382 : : }
383 : :
384 : : /**
385 : : * g_charset_converter_set_use_fallback:
386 : : * @converter: a #GCharsetConverter
387 : : * @use_fallback: %TRUE to use fallbacks
388 : : *
389 : : * Sets the #GCharsetConverter:use-fallback property.
390 : : *
391 : : * Since: 2.24
392 : : */
393 : : void
394 : 1 : g_charset_converter_set_use_fallback (GCharsetConverter *converter,
395 : : gboolean use_fallback)
396 : : {
397 : 1 : use_fallback = !!use_fallback;
398 : :
399 : 1 : if (converter->use_fallback != use_fallback)
400 : : {
401 : 1 : converter->use_fallback = use_fallback;
402 : 1 : g_object_notify (G_OBJECT (converter), "use-fallback");
403 : : }
404 : 1 : }
405 : :
406 : : /**
407 : : * g_charset_converter_get_use_fallback:
408 : : * @converter: a #GCharsetConverter
409 : : *
410 : : * Gets the #GCharsetConverter:use-fallback property.
411 : : *
412 : : * Returns: %TRUE if fallbacks are used by @converter
413 : : *
414 : : * Since: 2.24
415 : : */
416 : : gboolean
417 : 1 : g_charset_converter_get_use_fallback (GCharsetConverter *converter)
418 : : {
419 : 1 : return converter->use_fallback;
420 : : }
421 : :
422 : : /**
423 : : * g_charset_converter_get_num_fallbacks:
424 : : * @converter: a #GCharsetConverter
425 : : *
426 : : * Gets the number of fallbacks that @converter has applied so far.
427 : : *
428 : : * Returns: the number of fallbacks that @converter has applied
429 : : *
430 : : * Since: 2.24
431 : : */
432 : : guint
433 : 1 : g_charset_converter_get_num_fallbacks (GCharsetConverter *converter)
434 : : {
435 : 1 : return converter->n_fallback_errors;
436 : : }
437 : :
438 : : static void
439 : 3 : g_charset_converter_iface_init (GConverterIface *iface)
440 : : {
441 : 3 : iface->convert = g_charset_converter_convert;
442 : 3 : iface->reset = g_charset_converter_reset;
443 : 3 : }
444 : :
445 : : static gboolean
446 : 5 : g_charset_converter_initable_init (GInitable *initable,
447 : : GCancellable *cancellable,
448 : : GError **error)
449 : : {
450 : : GCharsetConverter *conv;
451 : : int errsv;
452 : :
453 : 5 : g_return_val_if_fail (G_IS_CHARSET_CONVERTER (initable), FALSE);
454 : :
455 : 5 : conv = G_CHARSET_CONVERTER (initable);
456 : :
457 : 5 : if (cancellable != NULL)
458 : : {
459 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
460 : : _("Cancellable initialization not supported"));
461 : 0 : return FALSE;
462 : : }
463 : :
464 : 5 : conv->iconv = g_iconv_open (conv->to, conv->from);
465 : 5 : errsv = errno;
466 : :
467 : 5 : if (conv->iconv == (GIConv)-1)
468 : : {
469 : 0 : if (errsv == EINVAL)
470 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
471 : : _("Conversion from character set “%s” to “%s” is not supported"),
472 : : conv->from, conv->to);
473 : : else
474 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
475 : : _("Could not open converter from “%s” to “%s”"),
476 : : conv->from, conv->to);
477 : 0 : return FALSE;
478 : : }
479 : :
480 : 5 : return TRUE;
481 : : }
482 : :
483 : : static void
484 : 3 : g_charset_converter_initable_iface_init (GInitableIface *iface)
485 : : {
486 : 3 : iface->init = g_charset_converter_initable_init;
487 : 3 : }
|