File: conv.c

package info (click to toggle)
zapping 0.10~cvs6-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,856 kB
  • ctags: 11,841
  • sloc: ansic: 111,154; asm: 11,770; sh: 9,812; xml: 2,742; makefile: 1,283; perl: 488
file content (625 lines) | stat: -rw-r--r-- 13,824 bytes parent folder | download | duplicates (6)
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
 *  libzvbi - Unicode conversion helper functions
 *
 *  Copyright (C) 2003, 2004 Michael H. Schimek
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $Id: conv.c,v 1.7 2005/09/01 01:40:52 mschimek Exp $ */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <langinfo.h>
#include <assert.h>
#ifdef ZAPPING8
#  include "common/intl-priv.h"
#else
#  include "intl-priv.h"
#endif
#include "misc.h"		/* N_ELEMENTS() */
#include "conv.h"

const char vbi3_intl_domainname [] = PACKAGE;

/**
 * @param cd Conversion object returned by vbi3_iconv_ucs2_open().
 *
 * Frees all resources associated with the coversion object.
 */
void
vbi3_iconv_ucs2_close		(iconv_t		cd)
{
	if ((iconv_t) -1 != cd)
		iconv_close (cd);
}

static iconv_t
xiconv_open			(const char *		dst_format,
				 const char *		src_format,
				 char **		dst,
				 unsigned int		dst_size)
{
	iconv_t cd;
	size_t n;

	if (NULL == dst_format)
		dst_format = "UTF-8";

	if (NULL == src_format)
		src_format = "UCS-2";

	cd = iconv_open (dst_format, src_format);

	if ((iconv_t) -1 == cd)
		return cd;

	/* Write out the byte sequence to get into the
	   initial state if this is necessary. */
	n = iconv (cd, NULL, NULL, dst, &dst_size);

	if ((size_t) -1 == n) {
		iconv_close (cd);
		cd = (iconv_t) -1;
	}

	return cd;
}

/**
 * @param dst_format Character set name for iconv() conversion,
 *   for example "ISO-8859-1". When @c NULL, the default is UTF-8.
 * @param dst Pointer to output buffer pointer, will be incremented
 *   by the number of bytes written.
 * @param dst_size Space available in the output buffer, in bytes.
 *
 * Helper function to convert UCS-2 coded text (as in vbi3_page) to
 * another format. A start byte sequence will be stored in @a dst if
 * necessary.
 *
 * @returns
 * (iconv_t) -1 when the conversion is not possible.
 */
iconv_t
vbi3_iconv_ucs2_open		(const char *		dst_format,
				 char **		dst,
				 unsigned long		dst_size)
{
	assert (NULL != dst);

	return xiconv_open (dst_format, NULL, dst, dst_size);
}

/* Like iconv, but converts unrepresentable characters to space
   0x0020. Source is assumed to be UTF-8 (csize 1) or UCS-2
   (csize 2) with native endianess. */
static size_t
xiconv				(iconv_t		cd,
				 const char **		s,
				 size_t *		sleft,
				 char **		d,
				 size_t *		dleft,
				 unsigned int		csize)
{
	size_t r;

	for (;;) {
		const uint16_t ucs2_space[1] = { 0x0020 };
		const uint8_t utf8_space[1] = { 0x20 };
		const char *s1;
		size_t sleft1;

		/* iconv() source pointer may be defined as char **, should
		   be const char ** or const void **. Ignore compiler
		   warnings. */
		r = iconv (cd, s, sleft, d, dleft);

		if ((size_t) -1 != r)
			break;

		if (EILSEQ != errno)
			break;

		/* Replace unrepresentable character by space. */

		if (1 == csize)
			s1 = (const char *) utf8_space;
		else
			s1 = (const char *) ucs2_space;

		sleft1 = csize;

		r = iconv (cd, &s1, &sleft1, d, dleft);

		if ((size_t) -1 == r)
			break;

		if (1 == csize) {
			do {
				++*s;
				--*sleft;
			} while (**s & 0x80);
		} else {
			*s += 2;
			*sleft -= 2;
		}
	}

	return r;
}

/**
 * @param cd Conversion object returned by vbi3_iconv_ucs2_open().
 * @param dst Pointer to output buffer pointer, will be incremented
 *   by the number of bytes written.
 * @param dst_size Space available in the output buffer, in bytes.
 * @param src Source string in UCS-2 format.
 * @param src_size Number of characters (not bytes) in the
 *   source string.
 *
 * Converts UCS-2 coded text (as in vbi3_page) to another format
 * and stores it in the output buffer. Characters not representable
 * in the target format are converted to spaces 0x0020.
 *
 * @returns
 * FALSE on failure, including @a dst_size too small. Output buffer
 * contents are undefined on failure.
 */
vbi3_bool
vbi3_iconv_ucs2			(iconv_t		cd,
				 char **		dst,
				 unsigned long		dst_size,
				 const uint16_t *	src,
				 unsigned long		src_size)
{
	const char *s;
	size_t sleft;
	size_t dleft;
	size_t r;

	assert (NULL != dst);

	if (NULL == src) {
		static const uint16_t dummy[1] = { 0 };
		src = dummy;
	}

	s = (const char *) src;

	sleft = src_size * 2;
	dleft = dst_size;

	r = xiconv (cd, &s, &sleft, dst, &dleft, 2);

	if ((size_t) -1 == r)
		return FALSE;

	if (sleft > 0)
		return FALSE;

	return TRUE;
}

/**
 * @param cd Conversion object returned by vbi3_iconv_ucs2_open().
 * @param dst Pointer to output buffer pointer, will be incremented
 *   by the number of bytes written.
 * @param dst_size Space available in the output buffer, in bytes.
 * @param unicode Single 16 bit Unicode character.
 *
 * Converts UCS-2 character (as in vbi3_page) to another format
 * and stores it in the output buffer. Characters not representable
 * in the target format are converted to spaces 0x0020.
 *
 * @returns
 * FALSE on failure, including @a dst_size too small. Output buffer
 * contents are undefined on failure.
 */
vbi3_bool
vbi3_iconv_unicode		(iconv_t		cd,
				 char **		dst,
				 unsigned long		dst_size,
				 unsigned int		unicode)
{
	uint16_t t[1];

	assert (NULL != dst);

	t[0] = unicode;

	return vbi3_iconv_ucs2 (cd, dst, dst_size, t, 1);
}

/**
 * @param src String to be duplicated.
 *
 * Creates a duplicate of a NUL-terminated string encoded in the
 * character set used by gettext or the current locale.
 *
 * @returns
 * NUL terminated string. You must free() the string when no longer
 * needed. The function returns NULL when @a src is NULL or memory
 * is exhausted.
 */
char *
_vbi3_strdup_locale		(const char *		src)
{
  /* FIXME this is supposed to take multi-byte characters into account. */
	return strdup (src);
}

static char *
strdup_iconv			(const char *		dst_format,
				 const char *		src_format,
				 const char *		src,
				 unsigned long		src_size,
				 unsigned int		csize)
{
	char *buf;
	char *buf2;
	unsigned long buf_size;
	iconv_t cd;
	const char *s;
	char *d;
	size_t sleft;
	size_t dleft;

	if (!src)
		return NULL;

	buf_size = src_size * 8;

	if (!(buf = vbi3_malloc (buf_size)))
		return NULL;

	s = (const char *) src;
	d = buf;

	cd = xiconv_open (dst_format, src_format, &d, buf_size);

	if ((iconv_t) -1 == cd)
		goto failure;

	sleft = src_size;
	dleft = buf_size - (d - buf);

	while (sleft > 0) {
		size_t r;

		r = xiconv (cd, &s, &sleft, &d, &dleft, csize);

		if ((size_t) -1 != r)
			break;

		if (E2BIG != errno)
			goto failure;

		if (!(buf2 = vbi3_realloc (buf, buf_size * 2)))
			goto failure;

		d = buf2 + (d - buf);
		dleft += buf_size * 2 - buf_size;

		buf = buf2;
		buf_size *= 2;
	}

	if (!(buf2 = vbi3_realloc (buf, buf_size - dleft + 4)))
		goto failure;

	memset (buf2 + (d - buf), 0, 4);

	iconv_close (cd);

	return buf2;

 failure:
	if ((iconv_t) -1 != cd)
		iconv_close (cd);

	vbi3_free (buf);

	return NULL;
}

/**
 * @param dst_format Character set name for iconv() conversion,
 *   for example "ISO-8859-1". When @c NULL, the default is UTF-8.
 * @param src Input buffer.
 * @param src_size Number of characters (not bytes) in the
 *   input buffer.
 *
 * Converts UCS-2 coded text (as in vbi3_page) to another format,
 * and stores it in a newly allocated buffer. Characters not representable
 * in the @a dst_format are converted to spaces 0x0020.
 *
 * @returns
 * NUL terminated string. You must free() the string when no longer
 * needed. The function returns an empty string when @a src_size is 0,
 * a @c NULL pointer on failure.
 */
char *
vbi3_strdup_iconv_ucs2		(const char *		dst_format,
				 const uint16_t *	src,
				 unsigned long		src_size)
{
	return strdup_iconv (dst_format, NULL,
			     (const char *) src, src_size * 2, 2);
}

/**
 * @internal
 * @param src Input buffer.
 * @param src_size Number of characters (not bytes) in the
 *   input buffer.
 *
 * Converts UCS-2 coded text (as in vbi3_page) to the character set
 * used by gettext or the current locale and stores it in a newly
 * allocated buffer. Characters not representable
 * in the target format are converted to spaces 0x0020.
 *
 * @returns
 * NUL terminated string. You must free() the string when no longer
 * needed. The function returns an empty string when @a src_size is 0,
 * a @c NULL pointer on failure.
 */
char *
_vbi3_strdup_locale_ucs2		(const uint16_t *	src,
				 unsigned long		src_size)
{
	const char *dst_format;

	if (!src)
		return NULL;

	dst_format = bind_textdomain_codeset (vbi3_intl_domainname, NULL);

	if (NULL == dst_format)
		dst_format = nl_langinfo (CODESET);

	if (NULL == dst_format)
		return NULL;

	return strdup_iconv (dst_format, NULL,
			     (const char *) src, src_size * 2, 2);
}

/**
 * @internal
 * @param src NUL-terminated string in UTF-8 format.
 *
 * Converts UTF-8 coded string to the character set used by gettext
 * or the current locale and stores it in a newly allocated buffer.
 * Characters not representable in the output format are converted
 * to spaces 0x0020.
 *
 * @returns
 * NUL terminated string. You must free() the string when no longer
 * needed. The function returns an empty string when @a src is @c NULL,
 * a @c NULL pointer on failure.
 */
char *
_vbi3_strdup_locale_utf8		(const char *		src)
{
	const char *dst_format;
	unsigned long src_size;

	if (!src)
		return NULL;

	dst_format = bind_textdomain_codeset (vbi3_intl_domainname, NULL);

	if (NULL == dst_format)
		dst_format = nl_langinfo (CODESET);

	if (NULL == dst_format)
		return NULL;

	if (0 == strcmp (dst_format, "UTF-8"))
		return strdup (src);

	src_size = strlen (src);

	return strdup_iconv (dst_format, "UTF-8", src, src_size, 1);
}

/**
 */
char *
_vbi3_strdup_locale_teletext	(const uint8_t *	src,
				 unsigned long		src_size,
				 const vbi3_character_set *cs)
{
	uint16_t buffer[64];
	unsigned long begin;
	unsigned long end;
	unsigned long i;

	if (!src)
		return NULL;

	assert (src_size < N_ELEMENTS (buffer));

	for (begin = 0; begin < src_size; ++begin)
		if ((src[begin] & 0x7F) > 0x20)
			break;

	if (begin >= src_size)
		return NULL;

	for (end = src_size; end > 0; --end)
		if ((src[end - 1] & 0x7F) > 0x20)
			break;

	for (i = begin; i < end; ++i) {
		buffer[i] = vbi3_teletext_unicode
		  (cs->g0, cs->subset, (unsigned int)(src[i] & 0x7F));
	}

	return _vbi3_strdup_locale_ucs2 (buffer + begin, end - begin);
}

/**
 * @internal
 * @param src NUL-terminated string in UTF-8 format.
 *
 * Converts UTF-8 coded string to UCS-2 string and stores it in a
 * newly allocated buffer.
 *
 * @returns
 * NUL terminated string. You must free() the string when no longer
 * needed. The function returns an empty string when @a src is @c NULL,
 * a @c NULL pointer on failure.
 */
uint16_t *
_vbi3_strdup_ucs2_utf8		(const char *		src)
{
	unsigned long src_size;

	if (!src)
		return NULL;

	src_size = strlen (src);

	return (uint16_t *) strdup_iconv ("UCS-2", "UTF-8", src, src_size, 1);
}

/**
 * @param fp Output file.
 * @param cd Conversion object returned by vbi3_iconv_ucs2_open().
 * @param src Input buffer.
 * @param src_size Number of characters (not bytes) in the
 *   input buffer.
 *
 * Converts UCS-2 coded text (as in vbi3_page) to another format,
 * and writes it into the given file. Characters not representable
 * in the output format are converted to spaces 0x0020.
 *
 * @returns
 * FALSE on failure.
 */
vbi3_bool
vbi3_stdio_cd_ucs2		(FILE *			fp,
				 iconv_t		cd,
				 const uint16_t *	src,
				 unsigned long		src_size)
{
	char buffer[4096];
	const char *s;
	size_t sleft;

	s = (const char *) src;
	sleft = src_size * 2;

	while (sleft > 0) {
		char *d;
		size_t dleft;
		size_t r;
		size_t n;

		d = buffer;
		dleft = sizeof (buffer);

		r = xiconv (cd, &s, &sleft, &d, &dleft, 2);

		if ((size_t) -1 == r)
			if (E2BIG != errno)
				return FALSE;

		n = d - buffer;

		r = fwrite (buffer, 1, n, fp);

		if (n != r)
			return FALSE;
	}

	return TRUE;
}

/**
 * @param fp Output file.
 * @param dst_format Character set name for iconv() conversion,
 *   for example "ISO-8859-1". When @c NULL, the default is UTF-8.
 * @param src Input buffer.
 * @param src_size Number of characters (not bytes) in the
 *   input buffer.
 *
 * Converts UCS-2 coded text (as in vbi3_page) to another format,
 * and writes it into the given file. Characters not representable
 * in the output format are converted to spaces 0x0020.
 *
 * @returns
 * FALSE on failure.
 */
vbi3_bool
vbi3_stdio_iconv_ucs2		(FILE *			fp,
				 const char *		dst_format,
				 const uint16_t *	src,
				 unsigned long		src_size)
{
	char buffer[4096];
	iconv_t cd;
	const char *s;
	char *d;
	size_t sleft;
	size_t dleft;

	s = (const char *) src;
	d = buffer;

	cd = xiconv_open (dst_format, NULL, &d, sizeof (buffer));

	if ((iconv_t) -1 == cd)
		return FALSE;

	sleft = src_size * 2;
	dleft = sizeof (buffer) - (buffer - d);

	while (sleft > 0) {
		size_t r;
		size_t n;

		r = xiconv (cd, &s, &sleft, &d, &dleft, 2);

		if ((size_t) -1 == r)
			if (E2BIG != errno)
				goto failure;

		n = d - buffer;

		r = fwrite (buffer, 1, n, fp);

		if (n != r)
			goto failure;

		d = buffer;
		dleft = sizeof (buffer);
	}

	iconv_close (cd);

	return TRUE;

 failure:
	iconv_close (cd);

	return FALSE;
}