summaryrefslogtreecommitdiff
path: root/src/iconv.c
blob: a96a5a61e23571379c28791b3f7196ad3705a9a5 (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* iconv implementation - see iconv.h for docs */

#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unicode/charsets.h>
#include <unicode/encoding.h>

#include <iconv-internal/iconv.h>

#include "internal.h"

static struct encoding_context *context_list;

static int character_callback(void *handle, UCS4 c);
static void parse_parameters(struct encoding_context *e, const char *params,
		bool destination);
static void parse_parameter(struct encoding_context *e, const char *param,
		int length, bool destination);

int iconv_initialise(const char *aliases_file)
{
	if (aliases_file == NULL)
		return false;

	if (create_alias_data(aliases_file) == false)
		return false;

	encoding_initialise();

	return true;
}

void iconv_finalise(void)
{
	struct encoding_context *a, *b;

	/* clients may quit / die without cleaning up. */
	for (a = context_list; a; a = b) {
		b = a->next;
		if (a->in)
			encoding_delete(a->in);
		if (a->out)
			encoding_delete(a->out);
		iconv_eightbit_delete(a);
		free(a);
	}

	free_alias_data();

	/* finalise the unicode library */
	encoding_tidyup();
}

iconv_t iconv_open(const char *tocode, const char *fromcode)
{
	int to = 0, from = 0;
	struct encoding_context *e;
	struct canon *c;
	bool to_force_le = false, from_force_le = false;
	bool to_no_bom = false, from_no_bom = false;
	char totemp[128], fromtemp[128];
	const char *slash;
	unsigned int len;

	/* can't do anything without these */
	if (!tocode || !fromcode) {
		errno = EINVAL;
		return (iconv_t)(-1);
	}

	e = calloc(1, sizeof(*e));
	if (!e) {
		LOG(("malloc failed"));
		errno = ENOMEM;
		return (iconv_t)(-1);
	}

	/* strip any parameters off the end of the tocode string */
	slash = strchr(tocode, '/');
	len = slash ? (unsigned) (slash - tocode) : strlen(tocode);
	snprintf(totemp, sizeof totemp, "%.*s", len, tocode);

	/* parse parameters */
	if (slash && *(slash + 1) == '/' && *(slash + 2) != '\0')
		parse_parameters(e, slash + 2, true);

	/* strip any parameters off the end of the fromcode string */
	slash = strchr(fromcode, '/');
	len = slash ? (unsigned) (slash - fromcode) : strlen(fromcode);
	snprintf(fromtemp, sizeof fromtemp, "%.*s", len, fromcode);

	/* parse parameters */
	if (slash && *(slash + 1) == '/' && *(slash + 2) != '\0')
		parse_parameters(e, slash + 2, false);

	/* try our own 8bit charset code first */
	to = iconv_eightbit_number_from_name(totemp);
	from = iconv_eightbit_number_from_name(fromtemp);

	/* if that failed, try the UnicodeLib functionality */
	if (!to)
		to = iconv_encoding_number_from_name(totemp);

	if (!from)
		from = iconv_encoding_number_from_name(fromtemp);

	/* if that failed, perhaps it was an endian-specific variant of
	 * something UnicodeLib can handle? */
	if (!to) {
		c = alias_canonicalise(totemp);
		if (c) {
			switch(c->mib_enum) {
			case 1013: /* UTF-16BE */
				to = csUnicode11;
				to_no_bom = true;
				break;
			case 1014: /* UTF-16LE */
				to = csUnicode11;
				to_force_le = true;
				to_no_bom = true;
				break;
			case 1018: /* UTF-32BE */
				to = csUCS4;
				to_no_bom = true;
				break;
			case 1019: /* UTF-32LE */
				to = csUCS4;
				to_force_le = true;
				to_no_bom = true;
				break;
			}
		} else if (strcasecmp(totemp, "UCS-4-INTERNAL") == 0) {
			uint32_t test = 0x55aa55aa;
			to = csUCS4;
			to_force_le = ((const uint8_t *) &test)[0] == 0xaa;
			to_no_bom = true;
		}
	}

	if (!from) {
		c = alias_canonicalise(fromtemp);
		if (c) {
			switch(c->mib_enum) {
			case 1013: /* UTF-16BE */
				from = csUnicode11;
				from_no_bom = true;
				break;
			case 1014: /* UTF-16LE */
				from = csUnicode11;
				from_force_le = true;
				from_no_bom = true;
				break;
			case 1018: /* UTF-32BE */
				from = csUCS4;
				from_no_bom = true;
				break;
			case 1019: /* UTF-32LE */
				from = csUCS4;
				from_force_le = true;
				from_no_bom = true;
				break;
			}
		} else if (strcasecmp(fromtemp, "UCS-4-INTERNAL") == 0) {
			uint32_t test = 0x55aa55aa;
			from = csUCS4;
			from_force_le = ((const uint8_t *) &test)[0] == 0xaa;
			from_no_bom = true;
		}
	}

	LOG(("to: %d(%s) from: %d(%s)", to, totemp, from, fromtemp));

	/* ensure both encodings are recognised */
	if (to == 0 || from == 0) {
		free(e);
		errno = EINVAL;
		return (iconv_t)(-1);
	}

	/* bit 30 set indicates that this is an 8bit encoding */
	if (from & (1<<30))
		e->intab = iconv_eightbit_new(from & ~(1<<30));
	else {
		e->in = encoding_new(from, encoding_READ);
		if (e->in) {
			/* Set encoding flags */
			unsigned int flags = 0;
			if (from_force_le)
				flags |= encoding_FLAG_LITTLE_ENDIAN;

			if (from == csUnicode || from_no_bom)
				flags |= encoding_FLAG_NO_HEADER;

			encoding_set_flags(e->in, flags, flags);

			e->inflags = flags;
		}
	}

	/* neither created => memory error or somesuch. assume ENOMEM */
	/* no table is ever generated for ASCII */
	if (!e->in && !e->intab && (from & ~(1<<30)) != csASCII) {
		free(e);
		errno = ENOMEM;
		return (iconv_t)(-1);
	}

	if (to & (1<<30))
		e->outtab = iconv_eightbit_new(to & ~(1<<30));
	else {
		e->out = encoding_new(to, encoding_WRITE_STRICT);
		if (e->out) {
			/* Set encoding flags */
			unsigned int flags = 0;
			if (to_force_le)
				flags |= encoding_FLAG_LITTLE_ENDIAN;

			if (to == csUnicode || to_no_bom)
				flags |= encoding_FLAG_NO_HEADER;

			encoding_set_flags(e->out, flags, flags);

			e->outflags = flags;
		}
	}

	/* neither created => ENOMEM */
	if (!e->out && !e->outtab && (to & ~(1<<30)) != csASCII) {
		if (e->in)
			encoding_delete(e->in);
		iconv_eightbit_delete(e);
		free(e);
		errno = ENOMEM;
		return (iconv_t)(-1);
	}

	/* add to list */
	e->prev = 0;
	e->next = context_list;
	if (context_list)
		context_list->prev = e;
	context_list = e;

	return (iconv_t)e;
}

size_t iconv(iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf,
		size_t *outbytesleft)
{
	struct encoding_context *e;
	unsigned int read;

	/* search for cd in list */
	for (e = context_list; e; e = e->next)
		if (e == (struct encoding_context *)cd)
			break;

	/* not found => invalid */
	if (!e) {
		errno = EINVAL;
		return (size_t)(-1);
	}

	if (inbuf == NULL || *inbuf == NULL) {
		/* Clear skip */
		e->skip = 0;

		/* Reset read codec */
		if (e->in) {
			encoding_reset(e->in);
			encoding_set_flags(e->in, e->inflags, e->inflags);
		}

		/* Reset write codec, flushing shift sequences, if asked */
		if (e->out) {
			if (outbuf != NULL) {
				char *prev_outbuf = *outbuf;
				size_t prev_outbytesleft = *outbytesleft;
				int ret;

				ret = encoding_write(e->out, NULL_UCS4, 
						outbuf, (int*) outbytesleft);

				LOG(("ret: %d", ret));

				/* Why the need for this nonsense? UnicodeLib 
				 * appears to decrease the count of free space
				 * in the buffer even if it doesn't write into 
				 * it. This is a bug, as the documentation says 
				 * that the buffer pointer AND free space count
				 * are left unmodified if nothing is written.
				 * Therefore, we have this hack until 
				 * UnicodeLib gets fixed.
				 */
				if (ret == -1) {
					*outbytesleft = prev_outbytesleft -
						(*outbuf - prev_outbuf);

					errno = EINVAL;
					return (size_t)-1;
				}
			}
			encoding_reset(e->out);
			encoding_set_flags(e->out, e->outflags, e->outflags);
		}
		return 0;
	}

	/* Is there any point doing anything? */
	if (!outbuf || !(*outbuf) || !outbytesleft) {
		errno = EINVAL;
		return (size_t)(-1);
	}

	e->outbuf = outbuf;
	e->outbytesleft = outbytesleft;

	LOG(("reading"));

	/* If, on the previous attempt to convert data, we reached the end
	 * of the input buffer mid-sequence, then we retain the number of
	 * bytes into the sequence we have read so far. We need to skip over
	 * these bytes in the input now because UnicodeLib expects the next
	 * byte to be the next in the sequence rather than the iconv()
	 * semantics of replaying the entire incomplete sequence from the 
	 * start.
	 */
	if (e->skip != 0) {
		*inbuf += e->skip;
		*inbytesleft -= e->skip;

		e->skip = 0;
	}

	/* Perform the conversion.
	 *
	 * To ensure that we detect the correct error conditions
	 * and point to the _start_ of erroneous input on error, we
	 * have to convert each character independently. Then we
	 * inspect for errors and only continue if there were none.
	 */
	while (*inbytesleft > 0) {
		/* Clear current write state */
		e->write_state = WRITE_NONE;

		if (e->in)
			read = encoding_read(e->in, character_callback, *inbuf,
					*inbytesleft, e);
		else
			read = iconv_eightbit_read(e, character_callback, 
					*inbuf, *inbytesleft, e);

		/* Stop on error */
		if (e->write_state != WRITE_SUCCESS)
			break;

		/* Advance input */
		*inbuf += read;
		*inbytesleft -= read;
	}

	LOG(("done"));

	LOG(("read: %d, ibl: %zd, obl: %zd", 
			read, *inbytesleft, *outbytesleft));

	/* Determine correct return value/error code */
	switch (e->write_state) {
	case WRITE_SUCCESS: /* 2 */
		/** \todo We really should calculate the correct number of 
		 * irreversible conversions that have been performed. For now, 
		 * assume everything's reversible. */
		return 0;
	case WRITE_NONE:    /* 3 */
		/* Mark where we need to start in the next call */
		e->skip = read;
		errno = EINVAL;
		break;
	case WRITE_NOMEM:   /* 4 */
		errno = E2BIG;
		break;
	case WRITE_FAILED:  /* 1 */
		errno = EILSEQ;
		break;
	}

	LOG(("errno: %d", errno));

	return (size_t)(-1);
}

int iconv_close(iconv_t cd)
{
	struct encoding_context *e;

	/* search for cd in list */
	for (e = context_list; e; e = e->next)
		if (e == (struct encoding_context *)cd)
			break;

	/* not found => invalid */
	if (!e)
		return 0;

	if (e->in)
		encoding_delete(e->in);
	if (e->out)
		encoding_delete(e->out);
	iconv_eightbit_delete(e);

	/* remove from list */
	if (e->next)
		e->next->prev = e->prev;
	if (e->prev)
		e->prev->next = e->next;
	else
		context_list = e->next;

	free(e);

	/* reduce our memory usage somewhat */
	encoding_table_remove_unused(8 /* recommended value */);

	return 0;
}

/* this is called for each converted character */
int character_callback(void *handle, UCS4 c)
{
	struct encoding_context *e;
	int ret;

	e = (struct encoding_context*)handle;

	if (c == 0xFFFE)
		c = 0xFFFD;

	/* Stop on invalid characters if we're not transliterating */
	/** \todo is this sane? -- we can't distinguish between illegal input 
	 * or valid input which just happens to correspond with U+fffd. */
	if ((c == 0xFFFD && !e->transliterate) || c == 0xFFFF) {
		e->write_state = WRITE_FAILED;
		return 1;
	}

	LOG(("outbuf: %p, free: %zd", *e->outbuf, *e->outbytesleft));
	LOG(("writing: %d", c));

	if (e->out) {
		char *prev_outbuf = *e->outbuf;
		size_t prev_outbytesleft = *e->outbytesleft;

		ret = encoding_write(e->out, c, e->outbuf,
				(int*)e->outbytesleft);

		LOG(("ret: %d", ret));

		/* Why the need for this nonsense? UnicodeLib appears to
		 * decrease the count of free space in the buffer even
		 * if it doesn't write into it. This is a bug, as the
		 * documentation says that the buffer pointer AND free
		 * space count are left unmodified if nothing is written.
		 * Therefore, we have this hack until UnicodeLib gets fixed.
		 *
		 * We need to restore the space remaining in the output buffer
		 * on memory exhaustion, too. Otherwise, it will be negative,
		 * which will break the client.
		 */
		if (ret <= 0) {
			*e->outbytesleft = prev_outbytesleft -
					(*e->outbuf - prev_outbuf);
		}
	} else {
		ret = iconv_eightbit_write(e, c, e->outbuf,
				(int*)e->outbytesleft);
	}

	e->write_state = ret == -1 ? WRITE_FAILED 
				   : ret == 0 ? WRITE_NOMEM : WRITE_SUCCESS;

	if (ret == -1) {
		/* Transliterate, if we've been asked to.
		 * Assumes that output is 8bit/8bit multibyte with ASCII G0.
		 * This should be fine as the only <>8bit encodings are
		 * UCS{2,4}, UTF-{16,32}, neither of which return -1.
		 * Also, afaiaa, all supported multibyte encodings are ASCII
		 * compatible. */
		/** \todo Actually perform some kind of transliteration */
		if (e->transliterate) {
			if ((int)*e->outbytesleft > 0) {
				if (e->out) {
				/* Flush through any pending shift sequences */
				/** \todo this is a bit dodgy, as we only
				 * really need to ensure that the ASCII set
				 * is mapped into G0 in ISO2022 encodings.
				 * This will reset G1->G3, too, which may
				 * break things. If so, we may have to
				 * perform some dirty hackery which relies
				 * upon knowledge of UnicodeLib's internals
				 */
					encoding_write(e->out, NULL_UCS4, 
						e->outbuf,
						(int*)e->outbytesleft);
				}

				if ((int)*e->outbytesleft > 0) {
					*(*e->outbuf)++ = '?';
					--*e->outbytesleft;

					e->write_state = WRITE_SUCCESS;
				} else {
					e->write_state = WRITE_NOMEM;
				}
			} else {
				e->write_state = WRITE_NOMEM;
			}
		} else {
			e->write_state = WRITE_FAILED;
		}
	}

	/* Always stop after processing each character */
	return 1;
}

void parse_parameters(struct encoding_context *e, const char *params,
		bool destination)
{
	char *slash = NULL, *prev = NULL;
	int len;

	len = strlen(params);

	while (slash - params < len &&
			(slash = strchr(params, '/')) != NULL) {
		parse_parameter(e, prev == NULL ? params : prev,
				slash - (prev == NULL ? params : prev),
				destination);

		prev = slash + 2;
		slash += 2;
	}

	if (slash == NULL)
		parse_parameter(e, prev == NULL ? params : prev,
				(params + len) -
					(prev == NULL ? params : prev),
				destination);
}

void parse_parameter(struct encoding_context *e, const char *param,
		int length, bool destination)
{
	if (length == 8 && strncasecmp(param, "TRANSLIT", 8) == 0) {
		if (destination)
			e->transliterate = 1;
	}
}