aboutsummaryrefslogtreecommitdiff
path: root/common/conv.c
blob: aaa7af37e548c0541b8b17ca9e566398ec442f2a (plain) (blame)
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
/*-
 * Copyright (c) 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 * Copyright (c) 2011, 2012
 *	Zhihao Yuan.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#ifndef lint
static const char sccsid[] = "$Id: conv.c,v 2.40 2014/02/27 16:25:29 zy Exp $";
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>

#include <bitstring.h>
#include <errno.h>
#include <limits.h>
#include <langinfo.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

#include "common.h"

/*
 * codeset --
 *	Get the locale encoding.
 *
 * PUBLIC: char * codeset(void);
 */
char *
codeset(void)
{
	static char *cs;

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

	return cs;
}

#ifdef USE_WIDECHAR
static int 
raw2int(SCR *sp, const char * str, ssize_t len, CONVWIN *cw, size_t *tolen,
    CHAR_T **dst)
{
	int i;
	CHAR_T **tostr = &cw->bp1.wc;
	size_t  *blen = &cw->blen1;

	BINC_RETW(NULL, *tostr, *blen, len);

	*tolen = len;
	for (i = 0; i < len; ++i)
		(*tostr)[i] = (u_char) str[i];

	*dst = cw->bp1.wc;

	return 0;
}

#define CONV_BUFFER_SIZE    512
/* fill the buffer with codeset encoding of string pointed to by str
 * left has the number of bytes left in str and is adjusted
 * len contains the number of bytes put in the buffer
 */
#ifdef USE_ICONV
#define CONVERT(str, left, src, len)					\
	do {								\
		size_t outleft;						\
		char *bp = buffer;					\
		outleft = CONV_BUFFER_SIZE;				\
		errno = 0;						\
		if (iconv(id, (iconv_src_t)&str, &left, &bp, &outleft)	\
		    == -1 && errno != E2BIG)				\
			goto err;					\
		if ((len = CONV_BUFFER_SIZE - outleft) == 0) {		\
			error = -left;					\
			goto err;					\
		}							\
		src = buffer;						\
	} while (0)

#define IC_RESET()							\
	do {								\
		if (id != (iconv_t)-1)					\
			iconv(id, NULL, NULL, NULL, NULL);		\
	} while(0)
#else
#define CONVERT(str, left, src, len)
#define IC_RESET()
#endif

static int 
default_char2int(SCR *sp, const char * str, ssize_t len, CONVWIN *cw, 
    size_t *tolen, CHAR_T **dst, iconv_t id)
{
	size_t i = 0, j;
	CHAR_T **tostr = &cw->bp1.wc;
	size_t *blen = &cw->blen1;
	mbstate_t mbs;
	size_t n;
	ssize_t nlen = len;
	char *src = (char *)str;
#ifdef USE_ICONV
	char buffer[CONV_BUFFER_SIZE];
#endif
	size_t left = len;
	int error = 1;

	BZERO(&mbs, 1);
	BINC_RETW(NULL, *tostr, *blen, nlen);

#ifdef USE_ICONV
	if (id != (iconv_t)-1)
		CONVERT(str, left, src, len);
#endif

	for (i = 0, j = 0; j < len; ) {
		n = mbrtowc((*tostr)+i, src+j, len-j, &mbs);
		/* NULL character converted */
		if (n == -2)
			error = -(len-j);
		if (n == -1 || n == -2)
			goto err;
		if (n == 0)
			n = 1;
		j += n;
		if (++i >= *blen) {
			nlen += 256;
			BINC_RETW(NULL, *tostr, *blen, nlen);
		}
		if (id != (iconv_t)-1 && j == len && left) {
			CONVERT(str, left, src, len);
			j = 0;
		}
	}

	error = 0;
err:
	*tolen = i;
	*dst = cw->bp1.wc;
	IC_RESET();

	return error;
}

static int 
fe_char2int(SCR *sp, const char * str, ssize_t len, CONVWIN *cw, size_t *tolen,
    CHAR_T **dst)
{
	return default_char2int(sp, str, len, cw, tolen, dst,
	    sp->conv.id[IC_FE_CHAR2INT]);
}

static int 
ie_char2int(SCR *sp, const char * str, ssize_t len, CONVWIN *cw, size_t *tolen,
    CHAR_T **dst)
{
	return default_char2int(sp, str, len, cw, tolen, dst,
	    sp->conv.id[IC_IE_CHAR2INT]);
}

static int 
cs_char2int(SCR *sp, const char * str, ssize_t len, CONVWIN *cw, size_t *tolen,
    CHAR_T **dst)
{
	return default_char2int(sp, str, len, cw, tolen, dst, (iconv_t)-1);
}

static int 
int2raw(SCR *sp, const CHAR_T * str, ssize_t len, CONVWIN *cw, size_t *tolen,
    char **dst)
{
	int i;
	char **tostr = &cw->bp1.c;
	size_t  *blen = &cw->blen1;

	BINC_RETC(NULL, *tostr, *blen, len);

	*tolen = len;
	for (i = 0; i < len; ++i)
		(*tostr)[i] = str[i];

	*dst = cw->bp1.c;

	return 0;
}

static int 
default_int2char(SCR *sp, const CHAR_T * str, ssize_t len, CONVWIN *cw, 
    size_t *tolen, char **pdst, iconv_t id)
{
	size_t i, j, offset = 0;
	char **tostr = &cw->bp1.c;
	size_t *blen = &cw->blen1;
	mbstate_t mbs;
	size_t n;
	ssize_t  nlen = len + MB_CUR_MAX;
	char *dst;
	size_t buflen;
#ifdef USE_ICONV
	char buffer[CONV_BUFFER_SIZE];
#endif
	int error = 1;

/* convert first len bytes of buffer and append it to cw->bp
 * len is adjusted => 0
 * offset contains the offset in cw->bp and is adjusted
 * cw->bp is grown as required
 */
#ifdef USE_ICONV
#define CONVERT2(_buffer, lenp, cw, offset)				\
	do {								\
		char *bp = _buffer;					\
		int ret;						\
		do {							\
			size_t outleft = cw->blen1 - offset;		\
			char *obp = cw->bp1.c + offset;			\
			if (cw->blen1 < offset + MB_CUR_MAX) {		\
				nlen += 256;				\
				BINC_RETC(NULL, cw->bp1.c, cw->blen1,	\
				    nlen);				\
			}						\
			errno = 0;					\
			ret = iconv(id, (iconv_src_t)&bp, lenp, &obp,	\
			    &outleft);					\
			if (ret == -1 && errno != E2BIG)		\
				goto err;				\
			offset = cw->blen1 - outleft;			\
		} while (ret != 0); 					\
	} while (0)
#else
#define CONVERT2(_buffer, lenp, cw, offset)
#endif


	BZERO(&mbs, 1);
	BINC_RETC(NULL, *tostr, *blen, nlen);
	dst = *tostr; buflen = *blen;

#ifdef USE_ICONV
	if (id != (iconv_t)-1) {
		dst = buffer; buflen = CONV_BUFFER_SIZE;
	}
#endif

	for (i = 0, j = 0; i < len; ++i) {
		n = wcrtomb(dst+j, str[i], &mbs);
		if (n == -1)
			goto err;
		j += n;
		if (buflen < j + MB_CUR_MAX) {
			if (id != (iconv_t)-1) {
				CONVERT2(buffer, &j, cw, offset);
			} else {
				nlen += 256;
				BINC_RETC(NULL, *tostr, *blen, nlen);
				dst = *tostr; buflen = *blen;
			}
		}
	}

	n = wcrtomb(dst+j, L'\0', &mbs);
	j += n - 1;				/* don't count NUL at the end */
	*tolen = j;

	if (id != (iconv_t)-1) {
		CONVERT2(buffer, &j, cw, offset);
		/* back to the initial state */
		CONVERT2(NULL, NULL, cw, offset);
		*tolen = offset;
	}

	error = 0;
err:
	if (error)
		*tolen = j;
	*pdst = cw->bp1.c;
	IC_RESET();

	return error;
}

static int 
fe_int2char(SCR *sp, const CHAR_T * str, ssize_t len, CONVWIN *cw, 
    size_t *tolen, char **dst)
{
	return default_int2char(sp, str, len, cw, tolen, dst,
		sp->conv.id[IC_FE_INT2CHAR]);
}

static int 
cs_int2char(SCR *sp, const CHAR_T * str, ssize_t len, CONVWIN *cw, 
    size_t *tolen, char **dst)
{
	return default_int2char(sp, str, len, cw, tolen, dst, (iconv_t)-1);
}

#endif

/*
 * conv_init --
 *	Initialize the iconv environment.
 *
 * PUBLIC: void conv_init(SCR *, SCR *);
 */
void
conv_init(SCR *orig, SCR *sp)
{
	int i;

	if (orig == NULL)
		setlocale(LC_ALL, "");
	if (orig != NULL)
		BCOPY(&orig->conv, &sp->conv, 1);
#ifdef USE_WIDECHAR
	else {
		char *ctype = setlocale(LC_CTYPE, NULL);

		/*
		 * XXX
		 * This hack fixes the libncursesw issue on FreeBSD.
		 */
		if (!strcmp(ctype, "ko_KR.CP949"))
			setlocale(LC_CTYPE, "ko_KR.eucKR");
		else if (!strcmp(ctype, "zh_CN.GB2312"))
			setlocale(LC_CTYPE, "zh_CN.eucCN");
		else if (!strcmp(ctype, "zh_CN.GBK"))
			setlocale(LC_CTYPE, "zh_CN.GB18030");

		/*
		 * Switch to 8bit mode if locale is C;
		 * LC_CTYPE should be reseted to C if unmatched.
		 */
		if (!strcmp(ctype, "C") || !strcmp(ctype, "POSIX")) {
			sp->conv.sys2int = sp->conv.file2int = raw2int;
			sp->conv.int2sys = sp->conv.int2file = int2raw;
			sp->conv.input2int = raw2int;
		} else {
			sp->conv.sys2int = cs_char2int;
			sp->conv.int2sys = cs_int2char;
			sp->conv.file2int = fe_char2int;
			sp->conv.int2file = fe_int2char;
			sp->conv.input2int = ie_char2int;
		}
#ifdef USE_ICONV
		o_set(sp, O_INPUTENCODING, OS_STRDUP, codeset(), 0);
#endif
	}
#endif

	/* iconv descriptors must be distinct to screens. */
	for (i = 0; i <= IC_IE_TO_UTF16; ++i)
		sp->conv.id[i] = (iconv_t)-1;
#ifdef USE_ICONV
	conv_enc(sp, O_INPUTENCODING, 0);
#endif
}

/*
 * conv_enc --
 *	Convert file/input encoding.
 *
 * PUBLIC: int conv_enc(SCR *, int, char *);
 */
int
conv_enc(SCR *sp, int option, char *enc)
{
#if defined(USE_WIDECHAR) && defined(USE_ICONV)
	iconv_t *c2w, *w2c;
	iconv_t id_c2w, id_w2c;

	switch (option) {
	case O_FILEENCODING:
		c2w = sp->conv.id + IC_FE_CHAR2INT;
		w2c = sp->conv.id + IC_FE_INT2CHAR;
		if (!enc)
			enc = O_STR(sp, O_FILEENCODING);

		if (strcasecmp(codeset(), enc)) {
			if ((id_c2w = iconv_open(codeset(), enc)) ==
			    (iconv_t)-1)
				goto err;
			if ((id_w2c = iconv_open(enc, codeset())) ==
			    (iconv_t)-1)
				goto err;
		} else {
			id_c2w = (iconv_t)-1;
			id_w2c = (iconv_t)-1;
		}

		break;

	case O_INPUTENCODING:
		c2w = sp->conv.id + IC_IE_CHAR2INT;
		w2c = sp->conv.id + IC_IE_TO_UTF16;
		if (!enc)
			enc = O_STR(sp, O_INPUTENCODING);

		if (strcasecmp(codeset(), enc)) {
			if ((id_c2w = iconv_open(codeset(), enc)) ==
			    (iconv_t)-1)
				goto err;
		} else
			id_c2w = (iconv_t)-1;

		/* UTF-16 can not be locale and can not be inputed. */
		if ((id_w2c = iconv_open("utf-16be", enc)) == (iconv_t)-1)
			goto err;

		break;

	default:
		abort();
	}

	if (*c2w != (iconv_t)-1)
		iconv_close(*c2w);
	if (*w2c != (iconv_t)-1)
		iconv_close(*w2c);

	*c2w = id_c2w;
	*w2c = id_w2c;

	F_CLR(sp, SC_CONV_ERROR);
	F_SET(sp, SC_SCR_REFORMAT);

	return 0;
err:
#endif
	switch (option) {
	case O_FILEENCODING:
		msgq(sp, M_ERR, "321|File encoding conversion not supported");
		break;
	case O_INPUTENCODING:
		msgq(sp, M_ERR, "322|Input encoding conversion not supported");
		break;
	}
	return 1;
}

/*
 * conv_end --
 *	Close the iconv descriptors, release the buffer.
 *
 * PUBLIC: void conv_end(SCR *);
 */
void
conv_end(SCR *sp)
{
#if defined(USE_WIDECHAR) && defined(USE_ICONV)
	int i;
	for (i = 0; i <= IC_IE_TO_UTF16; ++i)
		if (sp->conv.id[i] != (iconv_t)-1)
			iconv_close(sp->conv.id[i]);
	if (sp->cw.bp1.c != NULL)
		free(sp->cw.bp1.c);
#endif
}