summaryrefslogtreecommitdiff
path: root/gio/gcharsetconverter.c
blob: 9edab8a6c8790b1414550cc86c92d4d2b4542393 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright (C) 2009 Red Hat, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

#include "config.h"

#include "gcharsetconverter.h"

#include <errno.h>

#include "ginitable.h"
#include "gioerror.h"
#include "glibintl.h"


enum {
  PROP_0,
  PROP_FROM_CHARSET,
  PROP_TO_CHARSET,
  PROP_USE_FALLBACK
};

/**
 * SECTION:gcharsetconverter
 * @short_description: Convert between charsets
 * @include: gio/gio.h
 *
 * #GCharsetConverter is an implementation of #GConverter based on
 * GIConv.
 */

static void g_charset_converter_iface_init          (GConverterIface *iface);
static void g_charset_converter_initable_iface_init (GInitableIface  *iface);

/**
 * GCharsetConverter:
 *
 * Conversions between character sets.
 */
struct _GCharsetConverter
{
  GObject parent_instance;

  char *from;
  char *to;
  GIConv iconv;
  gboolean use_fallback;
  guint n_fallback_errors;
};

G_DEFINE_TYPE_WITH_CODE (GCharsetConverter, g_charset_converter, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
						g_charset_converter_iface_init);
			 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
						g_charset_converter_initable_iface_init))

static void
g_charset_converter_finalize (GObject *object)
{
  GCharsetConverter *conv;

  conv = G_CHARSET_CONVERTER (object);

  g_free (conv->from);
  g_free (conv->to);
  if (conv->iconv)
    g_iconv_close (conv->iconv);

  G_OBJECT_CLASS (g_charset_converter_parent_class)->finalize (object);
}

static void
g_charset_converter_set_property (GObject      *object,
				  guint         prop_id,
				  const GValue *value,
				  GParamSpec   *pspec)
{
  GCharsetConverter *conv;

  conv = G_CHARSET_CONVERTER (object);

  switch (prop_id)
    {
    case PROP_TO_CHARSET:
      g_free (conv->to);
      conv->to = g_value_dup_string (value);
      break;

    case PROP_FROM_CHARSET:
      g_free (conv->from);
      conv->from = g_value_dup_string (value);
      break;

    case PROP_USE_FALLBACK:
      conv->use_fallback = g_value_get_boolean (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }

}

static void
g_charset_converter_get_property (GObject    *object,
				  guint       prop_id,
				  GValue     *value,
				  GParamSpec *pspec)
{
  GCharsetConverter *conv;

  conv = G_CHARSET_CONVERTER (object);

  switch (prop_id)
    {
    case PROP_TO_CHARSET:
      g_value_set_string (value, conv->to);
      break;

    case PROP_FROM_CHARSET:
      g_value_set_string (value, conv->from);
      break;

    case PROP_USE_FALLBACK:
      g_value_set_boolean (value, conv->use_fallback);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
g_charset_converter_class_init (GCharsetConverterClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = g_charset_converter_finalize;
  gobject_class->get_property = g_charset_converter_get_property;
  gobject_class->set_property = g_charset_converter_set_property;

  g_object_class_install_property (gobject_class,
				   PROP_TO_CHARSET,
				   g_param_spec_string ("to-charset", NULL, NULL,
							NULL,
							G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
							G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class,
				   PROP_FROM_CHARSET,
				   g_param_spec_string ("from-charset", NULL, NULL,
							NULL,
							G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
							G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (gobject_class,
				   PROP_USE_FALLBACK,
				   g_param_spec_boolean ("use-fallback", NULL, NULL,
							 FALSE,
							 G_PARAM_READWRITE |
							 G_PARAM_CONSTRUCT |
							 G_PARAM_STATIC_STRINGS));
}

static void
g_charset_converter_init (GCharsetConverter *local)
{
}


/**
 * g_charset_converter_new:
 * @to_charset: destination charset
 * @from_charset: source charset
 * @error: #GError for error reporting, or %NULL to ignore.
 *
 * Creates a new #GCharsetConverter.
 *
 * Returns: a new #GCharsetConverter or %NULL on error.
 *
 * Since: 2.24
 **/
GCharsetConverter *
g_charset_converter_new (const gchar *to_charset,
			 const gchar *from_charset,
			 GError      **error)
{
  GCharsetConverter *conv;

  conv = g_initable_new (G_TYPE_CHARSET_CONVERTER,
			 NULL, error,
			 "to-charset", to_charset,
			 "from-charset", from_charset,
			 NULL);

  return conv;
}

static void
g_charset_converter_reset (GConverter *converter)
{
  GCharsetConverter *conv = G_CHARSET_CONVERTER (converter);

  if (conv->iconv == NULL)
    {
      g_warning ("Invalid object, not initialized");
      return;
    }

  g_iconv (conv->iconv, NULL, NULL, NULL, NULL);
  conv->n_fallback_errors = 0;
}

static GConverterResult
g_charset_converter_convert (GConverter       *converter,
			     const void       *inbuf,
			     gsize             inbuf_size,
			     void             *outbuf,
			     gsize             outbuf_size,
			     GConverterFlags   flags,
			     gsize            *bytes_read,
			     gsize            *bytes_written,
			     GError          **error)
{
  GCharsetConverter  *conv;
  gsize res;
  GConverterResult ret;
  gchar *inbufp, *outbufp;
  gsize in_left, out_left;
  int errsv;
  gboolean reset;

  conv = G_CHARSET_CONVERTER (converter);

  if (conv->iconv == NULL)
    {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_INITIALIZED,
			   _("Invalid object, not initialized"));
      return G_CONVERTER_ERROR;
    }

  inbufp = (char *)inbuf;
  outbufp = (char *)outbuf;
  in_left = inbuf_size;
  out_left = outbuf_size;
  reset = FALSE;

  /* if there is not input try to flush the data */
  if (inbuf_size == 0)
    {
      if (flags & G_CONVERTER_INPUT_AT_END ||
          flags & G_CONVERTER_FLUSH)
        {
          reset = TRUE;
        }
      else
        {
          g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
                               _("Incomplete multibyte sequence in input"));
          return G_CONVERTER_ERROR;
        }
    }

  if (reset)
    /* call g_iconv with NULL inbuf to cleanup shift state */
    res = g_iconv (conv->iconv,
                   NULL, &in_left,
                   &outbufp, &out_left);
  else
    res = g_iconv (conv->iconv,
                   &inbufp, &in_left,
                   &outbufp, &out_left);

  *bytes_read = inbufp - (char *)inbuf;
  *bytes_written = outbufp - (char *)outbuf;

  /* Don't report error if we converted anything */
  if (res == (gsize) -1 && *bytes_read == 0)
    {
      errsv = errno;

      switch (errsv)
	{
	case EINVAL:
	  /* Incomplete input text */
	  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
			       _("Incomplete multibyte sequence in input"));
	  break;

	case E2BIG:
	  /* Not enough destination space */
	  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
			       _("Not enough space in destination"));
	  break;

	case EILSEQ:
	  /* Invalid code sequence */
	  if (conv->use_fallback)
	    {
	      if (outbuf_size < 3)
		g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
				     _("Not enough space in destination"));
	      else
		{
		  const char hex[] = "0123456789ABCDEF";
		  guint8 v = *(guint8 *)inbuf;
		  guint8 *out = (guint8 *)outbuf;
		  out[0] = '\\';
		  out[1] = hex[(v & 0xf0) >> 4];
		  out[2] = hex[(v & 0x0f) >> 0];
		  *bytes_read = 1;
		  *bytes_written = 3;
		  in_left--;
		  conv->n_fallback_errors++;
		  goto ok;
		}
	    }
	  else
	    g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
				 _("Invalid byte sequence in conversion input"));
	  break;

	default:
	  g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
		       _("Error during conversion: %s"),
		       g_strerror (errsv));
	  break;
	}
      ret = G_CONVERTER_ERROR;
    }
  else
    {
    ok:
      ret = G_CONVERTER_CONVERTED;

      if (reset &&
	  (flags & G_CONVERTER_INPUT_AT_END))
        ret = G_CONVERTER_FINISHED;
      else if (reset &&
	       (flags & G_CONVERTER_FLUSH))
        ret = G_CONVERTER_FLUSHED;
    }

  return ret;
}

/**
 * g_charset_converter_set_use_fallback:
 * @converter: a #GCharsetConverter
 * @use_fallback: %TRUE to use fallbacks
 *
 * Sets the #GCharsetConverter:use-fallback property.
 *
 * Since: 2.24
 */
void
g_charset_converter_set_use_fallback (GCharsetConverter *converter,
				      gboolean           use_fallback)
{
  use_fallback = !!use_fallback;

  if (converter->use_fallback != use_fallback)
    {
      converter->use_fallback = use_fallback;
      g_object_notify (G_OBJECT (converter), "use-fallback");
    }
}

/**
 * g_charset_converter_get_use_fallback:
 * @converter: a #GCharsetConverter
 *
 * Gets the #GCharsetConverter:use-fallback property.
 *
 * Returns: %TRUE if fallbacks are used by @converter
 *
 * Since: 2.24
 */
gboolean
g_charset_converter_get_use_fallback (GCharsetConverter *converter)
{
  return converter->use_fallback;
}

/**
 * g_charset_converter_get_num_fallbacks:
 * @converter: a #GCharsetConverter
 *
 * Gets the number of fallbacks that @converter has applied so far.
 *
 * Returns: the number of fallbacks that @converter has applied
 *
 * Since: 2.24
 */
guint
g_charset_converter_get_num_fallbacks (GCharsetConverter *converter)
{
  return converter->n_fallback_errors;
}

static void
g_charset_converter_iface_init (GConverterIface *iface)
{
  iface->convert = g_charset_converter_convert;
  iface->reset = g_charset_converter_reset;
}

static gboolean
g_charset_converter_initable_init (GInitable     *initable,
				   GCancellable  *cancellable,
				   GError       **error)
{
  GCharsetConverter  *conv;
  int errsv;

  g_return_val_if_fail (G_IS_CHARSET_CONVERTER (initable), FALSE);

  conv = G_CHARSET_CONVERTER (initable);

  if (cancellable != NULL)
    {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
			   _("Cancellable initialization not supported"));
      return FALSE;
    }

  conv->iconv = g_iconv_open (conv->to, conv->from);
  errsv = errno;

  if (conv->iconv == (GIConv)-1)
    {
      if (errsv == EINVAL)
	g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
		     _("Conversion from character set “%s” to “%s” is not supported"),
		     conv->from, conv->to);
      else
	g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
		     _("Could not open converter from “%s” to “%s”"),
		     conv->from, conv->to);
      return FALSE;
    }

  return TRUE;
}

static void
g_charset_converter_initable_iface_init (GInitableIface *iface)
{
  iface->init = g_charset_converter_initable_init;
}