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
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2011 Nexenta Systems, Inc. All rights reserved.
*/
/*
* CHARMAP file handling for iconv.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <alloca.h>
#include <sys/avl.h>
#include <stddef.h>
#include <unistd.h>
#include "charmap.h"
#include "parser.tab.h"
#include <assert.h>
enum cmap_pass cmap_pass;
static avl_tree_t cmap_sym;
static avl_tree_t cmap_mbs;
typedef struct charmap {
const char *cm_name;
struct charmap *cm_alias_of;
avl_node_t cm_avl_sym;
avl_node_t cm_avl_mbs;
int cm_warned;
int cm_frmbs_len;
int cm_tombs_len;
char cm_frmbs[MB_LEN_MAX + 1]; /* input */
char cm_tombs[MB_LEN_MAX + 1]; /* output */
} charmap_t;
static void add_charmap_impl_fr(char *sym, char *mbs, int mbs_len, int nodups);
static void add_charmap_impl_to(char *sym, char *mbs, int mbs_len, int nodups);
/*
* Array of POSIX specific portable characters.
*/
static const struct {
char *name;
int ch;
} portable_chars[] = {
{ "NUL", '\0' },
{ "alert", '\a' },
{ "backspace", '\b' },
{ "tab", '\t' },
{ "carriage-return", '\r' },
{ "newline", '\n' },
{ "vertical-tab", '\v' },
{ "form-feed", '\f' },
{ "space", ' ' },
{ "exclamation-mark", '!' },
{ "quotation-mark", '"' },
{ "number-sign", '#' },
{ "dollar-sign", '$' },
{ "percent-sign", '%' },
{ "ampersand", '&' },
{ "apostrophe", '\'' },
{ "left-parenthesis", '(' },
{ "right-parenthesis", '(' },
{ "asterisk", '*' },
{ "plus-sign", '+' },
{ "comma", ','},
{ "hyphen-minus", '-' },
{ "hyphen", '-' },
{ "full-stop", '.' },
{ "period", '.' },
{ "slash", '/' },
{ "solidus", '/' },
{ "zero", '0' },
{ "one", '1' },
{ "two", '2' },
{ "three", '3' },
{ "four", '4' },
{ "five", '5' },
{ "six", '6' },
{ "seven", '7' },
{ "eight", '8' },
{ "nine", '9' },
{ "colon", ':' },
{ "semicolon", ';' },
{ "less-than-sign", '<' },
{ "equals-sign", '=' },
{ "greater-than-sign", '>' },
{ "question-mark", '?' },
{ "commercial-at", '@' },
{ "left-square-bracket", '[' },
{ "backslash", '\\' },
{ "reverse-solidus", '\\' },
{ "right-square-bracket", ']' },
{ "circumflex", '^' },
{ "circumflex-accent", '^' },
{ "low-line", '_' },
{ "underscore", '_' },
{ "grave-accent", '`' },
{ "left-brace", '{' },
{ "left-curly-bracket", '{' },
{ "vertical-line", '|' },
{ "right-brace", '}' },
{ "right-curly-bracket", '}' },
{ "tilde", '~' },
{ "A", 'A' },
{ "B", 'B' },
{ "C", 'C' },
{ "D", 'D' },
{ "E", 'E' },
{ "F", 'F' },
{ "G", 'G' },
{ "H", 'H' },
{ "I", 'I' },
{ "J", 'J' },
{ "K", 'K' },
{ "L", 'L' },
{ "M", 'M' },
{ "N", 'N' },
{ "O", 'O' },
{ "P", 'P' },
{ "Q", 'Q' },
{ "R", 'R' },
{ "S", 'S' },
{ "T", 'T' },
{ "U", 'U' },
{ "V", 'V' },
{ "W", 'W' },
{ "X", 'X' },
{ "Y", 'Y' },
{ "Z", 'Z' },
{ "a", 'a' },
{ "b", 'b' },
{ "c", 'c' },
{ "d", 'd' },
{ "e", 'e' },
{ "f", 'f' },
{ "g", 'g' },
{ "h", 'h' },
{ "i", 'i' },
{ "j", 'j' },
{ "k", 'k' },
{ "l", 'l' },
{ "m", 'm' },
{ "n", 'n' },
{ "o", 'o' },
{ "p", 'p' },
{ "q", 'q' },
{ "r", 'r' },
{ "s", 's' },
{ "t", 't' },
{ "u", 'u' },
{ "v", 'v' },
{ "w", 'w' },
{ "x", 'x' },
{ "y", 'y' },
{ "z", 'z' },
{ NULL, 0 }
};
static int
cmap_compare_sym(const void *n1, const void *n2)
{
const charmap_t *c1 = n1;
const charmap_t *c2 = n2;
int rv;
rv = strcmp(c1->cm_name, c2->cm_name);
return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
}
/*
* In order for partial match searches to work,
* we need these sorted by mbs contents.
*/
static int
cmap_compare_mbs(const void *n1, const void *n2)
{
const charmap_t *c1 = n1;
const charmap_t *c2 = n2;
int len, rv;
len = c1->cm_frmbs_len;
if (len < c2->cm_frmbs_len)
len = c2->cm_frmbs_len;
rv = memcmp(c1->cm_frmbs, c2->cm_frmbs, len);
if (rv < 0)
return (-1);
if (rv > 0)
return (1);
/* they match through length */
if (c1->cm_frmbs_len < c2->cm_frmbs_len)
return (-1);
if (c2->cm_frmbs_len < c1->cm_frmbs_len)
return (1);
return (0);
}
void
charmap_init(char *to_map, char *from_map)
{
avl_create(&cmap_sym, cmap_compare_sym, sizeof (charmap_t),
offsetof(charmap_t, cm_avl_sym));
avl_create(&cmap_mbs, cmap_compare_mbs, sizeof (charmap_t),
offsetof(charmap_t, cm_avl_mbs));
cmap_pass = CMAP_PASS_FROM;
reset_scanner(from_map);
(void) yyparse();
add_charmap_posix();
cmap_pass = CMAP_PASS_TO;
reset_scanner(to_map);
(void) yyparse();
}
void
charmap_dump()
{
charmap_t *cm;
int i;
cm = avl_first(&cmap_mbs);
while (cm != NULL) {
(void) printf("name=\"%s\"\n", cm->cm_name);
(void) printf("\timbs=\"");
for (i = 0; i < cm->cm_frmbs_len; i++)
(void) printf("\\x%02x", cm->cm_frmbs[i] & 0xFF);
(void) printf("\"\n");
(void) printf("\tombs=\"");
for (i = 0; i < cm->cm_tombs_len; i++)
(void) printf("\\x%02x", cm->cm_tombs[i] & 0xFF);
(void) printf("\"\n");
cm = AVL_NEXT(&cmap_mbs, cm);
}
}
/*
* We parse two charmap files: First the "from" map, where we build
* cmap_mbs and cmap_sym which we'll later use to translate the input
* stream (mbs encodings) to symbols. Second, we parse the "to" map,
* where we fill in the tombs members of entries in cmap_sym, (which
* must alread exist) used later to write the output encoding.
*/
static void
add_charmap_impl(char *sym, char *mbs, int mbs_len, int nodups)
{
/*
* While parsing both the "from" and "to" cmaps,
* require both the symbol and encoding.
*/
if (sym == NULL || mbs == NULL) {
errf(_("invalid charmap entry"));
return;
}
switch (cmap_pass) {
case CMAP_PASS_FROM:
add_charmap_impl_fr(sym, mbs, mbs_len, nodups);
break;
case CMAP_PASS_TO:
add_charmap_impl_to(sym, mbs, mbs_len, nodups);
break;
default:
abort();
break;
}
}
static void
add_charmap_impl_fr(char *sym, char *mbs, int mbs_len, int nodups)
{
charmap_t *m, *n, *s;
avl_index_t where_sym, where_mbs;
if ((n = calloc(1, sizeof (*n))) == NULL) {
errf(_("out of memory"));
return;
}
n->cm_name = sym;
assert(0 < mbs_len && mbs_len <= MB_LEN_MAX);
(void) memcpy(n->cm_frmbs, mbs, mbs_len);
n->cm_frmbs_len = mbs_len;
m = avl_find(&cmap_mbs, n, &where_mbs);
s = avl_find(&cmap_sym, n, &where_sym);
/*
* If we found the symbol, this is a dup.
*/
if (s != NULL) {
if (nodups) {
warn(_("%s: duplicate character symbol"), sym);
}
free(n);
return;
}
/*
* If we found the mbs, the new one is an alias,
* which we'll add _only_ to the symbol AVL.
*/
if (m != NULL) {
/* The new one is an alias of the original. */
n->cm_alias_of = m;
avl_insert(&cmap_sym, n, where_sym);
return;
}
avl_insert(&cmap_sym, n, where_sym);
avl_insert(&cmap_mbs, n, where_mbs);
}
static void
add_charmap_impl_to(char *sym, char *mbs, int mbs_len, int nodups)
{
charmap_t srch = {0};
charmap_t *m;
assert(0 < mbs_len && mbs_len <= MB_LEN_MAX);
srch.cm_name = sym;
m = avl_find(&cmap_sym, &srch, NULL);
if (m == NULL) {
if (sflag == 0)
warn(_("%s: symbol not found"), sym);
return;
}
if (m->cm_alias_of != NULL) {
m = m->cm_alias_of;
/* don't warn for dups with aliases */
if (m->cm_tombs_len != 0)
return;
}
if (m->cm_tombs_len != 0) {
if (nodups) {
warn(_("%s: duplicate encoding for"), sym);
}
return;
}
(void) memcpy(m->cm_tombs, mbs, mbs_len);
m->cm_tombs_len = mbs_len;
}
void
add_charmap(char *sym, char *mbs)
{
/* mbs[0] is the length */
int mbs_len = *mbs++;
assert(0 < mbs_len && mbs_len <= MB_LEN_MAX);
add_charmap_impl(sym, mbs, mbs_len, 1);
}
/*
* This is called by the parser with start/end symbol strings (ssym, esym),
* which are allocated in the scanner (T_SYMBOL) and free'd here.
*/
void
add_charmap_range(char *ssym, char *esym, char *mbs)
{
int ls, le;
int si;
int sn, en;
int i;
int mbs_len;
char tmbs[MB_LEN_MAX+1];
char *mb_last;
static const char *digits = "0123456789";
/* mbs[0] is the length */
mbs_len = *mbs++;
assert(0 < mbs_len && mbs_len <= MB_LEN_MAX);
(void) memcpy(tmbs, mbs, mbs_len);
mb_last = tmbs + mbs_len - 1;
ls = strlen(ssym);
le = strlen(esym);
if (((si = strcspn(ssym, digits)) == 0) || (si == ls) ||
(strncmp(ssym, esym, si) != 0) ||
(strspn(ssym + si, digits) != (ls - si)) ||
(strspn(esym + si, digits) != (le - si)) ||
((sn = atoi(ssym + si)) > ((en = atoi(esym + si))))) {
errf(_("malformed charmap range"));
return;
}
ssym[si] = 0;
for (i = sn; i <= en; i++) {
char *nn;
(void) asprintf(&nn, "%s%0*u", ssym, ls - si, i);
if (nn == NULL) {
errf(_("out of memory"));
return;
}
add_charmap_impl(nn, tmbs, mbs_len, 1);
(*mb_last)++;
}
free(ssym);
free(esym);
}
void
add_charmap_char(char *name, int c)
{
char mbs[MB_LEN_MAX+1];
mbs[0] = c;
mbs[1] = '\0';
add_charmap_impl(name, mbs, 1, 0);
}
/*
* POSIX insists that certain entries be present, even when not in the
* orginal charmap file.
*/
void
add_charmap_posix(void)
{
int i;
for (i = 0; portable_chars[i].name; i++) {
add_charmap_char(portable_chars[i].name, portable_chars[i].ch);
}
}
/*
* This is called with a buffer of (typically) MB_LEN_MAX bytes,
* which is potentially a multi-byte symbol, but often contains
* extra bytes. Find and return the longest match in the charmap.
*/
static charmap_t *
find_mbs(const char *mbs, int len)
{
charmap_t srch = {0};
charmap_t *cm = NULL;
while (len > 0) {
(void) memcpy(srch.cm_frmbs, mbs, len);
srch.cm_frmbs_len = len;
cm = avl_find(&cmap_mbs, &srch, NULL);
if (cm != NULL)
break;
len--;
}
return (cm);
}
/*
* Return true if this sequence matches the initial part
* of any sequence known in this charmap.
*/
static boolean_t
find_mbs_partial(const char *mbs, int len)
{
charmap_t srch = {0};
charmap_t *cm;
avl_index_t where;
(void) memcpy(srch.cm_frmbs, mbs, len);
srch.cm_frmbs_len = len;
cm = avl_find(&cmap_mbs, &srch, &where);
if (cm != NULL) {
/* full match - not expected, but OK */
return (B_TRUE);
}
cm = avl_nearest(&cmap_mbs, where, AVL_AFTER);
if (cm != NULL && 0 == memcmp(cm->cm_frmbs, mbs, len))
return (B_TRUE);
return (B_FALSE);
}
/*
* Do like iconv(3), but with charmaps.
*/
size_t
cm_iconv(const char **iptr, size_t *ileft, char **optr, size_t *oleft)
{
charmap_t *cm;
int mbs_len;
/* Ignore state reset requests. */
if (iptr == NULL || *iptr == NULL)
return (0);
if (*oleft < MB_LEN_MAX) {
errno = E2BIG;
return ((size_t)-1);
}
while (*ileft > 0 && *oleft >= MB_LEN_MAX) {
mbs_len = MB_LEN_MAX;
if (mbs_len > *ileft)
mbs_len = *ileft;
cm = find_mbs(*iptr, mbs_len);
if (cm == NULL) {
if (mbs_len < MB_LEN_MAX &&
find_mbs_partial(*iptr, mbs_len)) {
/* incomplete sequence */
errno = EINVAL;
} else {
errno = EILSEQ;
}
return ((size_t)-1);
}
assert(cm->cm_frmbs_len > 0);
if (cm->cm_tombs_len == 0) {
if (sflag == 0 && cm->cm_warned == 0) {
cm->cm_warned = 1;
warn(_("To-map does not encode <%s>\n"),
cm->cm_name);
}
if (cflag == 0) {
errno = EILSEQ;
return ((size_t)-1);
}
/* just skip this input seq. */
*iptr += cm->cm_frmbs_len;
*ileft -= cm->cm_frmbs_len;
continue;
}
*iptr += cm->cm_frmbs_len;
*ileft -= cm->cm_frmbs_len;
(void) memcpy(*optr, cm->cm_tombs, cm->cm_tombs_len);
*optr += cm->cm_tombs_len;
*oleft -= cm->cm_tombs_len;
}
return (0);
}
|