1 /*
2  * Copyright (c) 2003-2004, Artem B. Bityuckiy
3  * Copyright (c) 1999,2000, Konstantin Chuguev. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 #include "cesbi.h"
27 
28 #if defined (ICONV_TO_UCS_CES_TABLE) \
29  || defined (ICONV_FROM_UCS_CES_TABLE)
30 
31 #include <_ansi.h>
32 #include <newlib.h>
33 #include <sys/types.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/iconvnls.h>
39 #include "../lib/endian.h"
40 #include "../lib/local.h"
41 #include "../lib/ucsconv.h"
42 #include "../ccs/ccs.h"
43 
44 /*
45  * Table-based CES converter is implemented here.  Table-based CES converter
46  * deals with encodings with "null" CES, like KOI8-R. In this case it is
47  * possible to implement one generic algorithm which works with different
48  * CCS tables.
49  *
50  * Table-based CES converter deals with CCS tables placed into iconv/ccs
51  * subdirectory. First, converter tries to find needed CCS table among
52  * linked-in tables. If not found, it tries to load it from external file
53  * (only if corespondent capability was enabled in Newlib configuration).
54  *
55  * 16 bit encodings are assumed to be Big Endian.
56  */
57 
58 static ucs2_t
59 find_code_size (ucs2_t code, const __uint16_t *tblp);
60 
61 static __inline ucs2_t
62 find_code_speed (ucs2_t code, const __uint16_t *tblp);
63 
64 static __inline ucs2_t
65 find_code_speed_8bit (ucs2_t code, const unsigned char *tblp);
66 
67 #ifdef _ICONV_ENABLE_EXTERNAL_CCS
68 static const iconv_ccs_desc_t *
69 load_file (const char *name, int direction);
70 #endif
71 
72 /*
73  * Interface data and functions implementation.
74  */
75 static size_t
table_close(void * data)76 table_close (
77                     void *data)
78 {
79   const iconv_ccs_desc_t *ccsp = (iconv_ccs_desc_t *)data;
80 
81   if (ccsp->type == TABLE_EXTERNAL)
82     free ((void *)ccsp->tbl);
83 
84   free((void *)ccsp);
85   return 0;
86 }
87 
88 #if defined (ICONV_FROM_UCS_CES_TABLE)
89 static void *
table_init_from_ucs(const char * encoding)90 table_init_from_ucs (
91                             const char *encoding)
92 {
93   int i;
94   const iconv_ccs_t *biccsp = NULL;
95   iconv_ccs_desc_t *ccsp;
96 
97   for (i = 0; _iconv_ccs[i] != NULL; i++)
98     if (strcmp (_iconv_ccs[i]->name, encoding) == 0)
99       {
100         biccsp = _iconv_ccs[i];
101         break;
102       }
103 
104   if (biccsp != NULL)
105     {
106       if (biccsp->from_ucs == NULL
107           || (ccsp = (iconv_ccs_desc_t *)
108                      malloc (sizeof (iconv_ccs_desc_t))) == NULL)
109         return NULL;
110 
111       ccsp->type = TABLE_BUILTIN;
112       ccsp->bits = biccsp->bits;
113       ccsp->optimization = biccsp->from_ucs_type;
114       ccsp->tbl = biccsp->from_ucs;
115 
116       return (void *)ccsp;
117     }
118 
119 #ifdef _ICONV_ENABLE_EXTERNAL_CCS
120   return (void *)load_file (encoding, 1);
121 #else
122   return NULL;
123 #endif
124 }
125 
126 static size_t
table_convert_from_ucs(void * data,ucs4_t in,unsigned char ** outbuf,size_t * outbytesleft)127 table_convert_from_ucs (void *data,
128                                ucs4_t in,
129                                unsigned char **outbuf,
130                                size_t *outbytesleft)
131 {
132   const iconv_ccs_desc_t *ccsp = (iconv_ccs_desc_t *)data;
133   ucs2_t code;
134 
135   if (in > 0xFFFF || in == INVALC)
136     return (size_t)ICONV_CES_INVALID_CHARACTER;
137 
138   if (ccsp->bits == TABLE_8BIT)
139     {
140       code = find_code_speed_8bit ((ucs2_t)in,
141                                   (const unsigned char *)ccsp->tbl);
142       if (code == INVALC)
143         return (size_t)ICONV_CES_INVALID_CHARACTER;
144       **outbuf = (unsigned char)code;
145       *outbuf += 1;
146       *outbytesleft -= 1;
147       return 1;
148     }
149   else if (ccsp->optimization == TABLE_SPEED_OPTIMIZED)
150     code = find_code_speed ((ucs2_t)in, ccsp->tbl);
151   else
152     code = find_code_size ((ucs2_t)in, ccsp->tbl);
153 
154   if (code == INVALC)
155     return (size_t)ICONV_CES_INVALID_CHARACTER;
156 
157   if (*outbytesleft < 2)
158     return (size_t)ICONV_CES_NOSPACE;
159 
160   /* We can't store whole word since **outbuf may be not 2-byte aligned */
161   **outbuf = (unsigned char)((ucs2_t)code >> 8);
162   *(*outbuf + 1) = (unsigned char)code;
163   *outbuf += 2;
164   *outbytesleft -= 2;
165   return 2;
166 }
167 #endif /* ICONV_FROM_UCS_CES_TABLE */
168 
169 #if defined (ICONV_TO_UCS_CES_TABLE)
170 static void *
table_init_to_ucs(const char * encoding)171 table_init_to_ucs (
172                           const char *encoding)
173 {
174   int i;
175   const iconv_ccs_t *biccsp = NULL;
176   iconv_ccs_desc_t *ccsp;
177 
178   for (i = 0; _iconv_ccs[i] != NULL; i++)
179     if (strcmp (_iconv_ccs[i]->name, encoding) == 0)
180       {
181         biccsp = _iconv_ccs[i];
182         break;
183       }
184 
185   if (biccsp != NULL)
186     {
187       if (biccsp->to_ucs == NULL
188           || (ccsp = (iconv_ccs_desc_t *)
189                      malloc (sizeof (iconv_ccs_desc_t))) == NULL)
190         return NULL;
191 
192       ccsp->type = TABLE_BUILTIN;
193       ccsp->bits = biccsp->bits;
194       ccsp->optimization = biccsp->to_ucs_type;
195       ccsp->tbl = biccsp->to_ucs;
196 
197       return (void *)ccsp;
198     }
199 
200 #ifdef _ICONV_ENABLE_EXTERNAL_CCS
201   return (void *)load_file (encoding, 0);
202 #else
203   return NULL;
204 #endif
205 }
206 
207 static ucs4_t
table_convert_to_ucs(void * data,const unsigned char ** inbuf,size_t * inbytesleft)208 table_convert_to_ucs (void *data,
209                              const unsigned char **inbuf,
210                              size_t *inbytesleft)
211 {
212   const iconv_ccs_desc_t *ccsp = (iconv_ccs_desc_t *)data;
213   ucs2_t ucs;
214 
215   if (ccsp->bits == TABLE_8BIT)
216     {
217       if (*inbytesleft < 1)
218         return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
219 
220       ucs = (ucs2_t)ccsp->tbl[**inbuf];
221 
222       if (ucs == INVALC)
223         return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
224 
225       *inbytesleft -= 1;
226       *inbuf += 1;
227       return (ucs4_t)ucs;
228     }
229 
230   if (*inbytesleft < 2)
231     return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
232 
233   if (ccsp->optimization == TABLE_SIZE_OPTIMIZED)
234     ucs = find_code_size((ucs2_t)**inbuf << 8 | (ucs2_t)*(*inbuf + 1),
235                          ccsp->tbl);
236   else
237     ucs = find_code_speed((ucs2_t)**inbuf << 8 | (ucs2_t)*(*inbuf + 1),
238                           ccsp->tbl);
239 
240   if (ucs == INVALC)
241     return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
242 
243   *inbuf += 2;
244   *inbytesleft -= 2;
245   return (ucs4_t)ucs;
246 }
247 #endif /* ICONV_TO_UCS_CES_TABLE */
248 
249 static int
table_get_mb_cur_max(void * data)250 table_get_mb_cur_max (void *data)
251 {
252   return ((iconv_ccs_desc_t *)data)->bits/8;
253 }
254 
255 
256 #if defined (ICONV_TO_UCS_CES_TABLE)
257 const iconv_to_ucs_ces_handlers_t
258 _iconv_to_ucs_ces_handlers_table =
259 {
260   table_init_to_ucs,
261   table_close,
262   table_get_mb_cur_max,
263   NULL,
264   NULL,
265   NULL,
266   table_convert_to_ucs
267 };
268 #endif /* ICONV_FROM_UCS_CES_TABLE */
269 
270 #if defined (ICONV_FROM_UCS_CES_TABLE)
271 const iconv_from_ucs_ces_handlers_t
272 _iconv_from_ucs_ces_handlers_table =
273 {
274   table_init_from_ucs,
275   table_close,
276   table_get_mb_cur_max,
277   NULL,
278   NULL,
279   NULL,
280   table_convert_from_ucs
281 };
282 #endif /* ICONV_TO_UCS_CES_TABLE */
283 
284 /*
285  * Supplementary functions.
286  */
287 
288 /*
289  * find_code_speed - find code in 16 bit speed-optimized table.
290  *
291  * PARAMETERS:
292  *     ucs2_t code - code whose mapping to find.
293  *     const __uint16_t *tblp - table pointer.
294  *
295  * RETURN:
296  *     Code that corresponds to 'code'.
297  */
298 static __inline ucs2_t
find_code_speed(ucs2_t code,const __uint16_t * tblp)299 find_code_speed (ucs2_t code,
300                         const __uint16_t *tblp)
301 {
302   int idx = tblp[code >> 8];
303 
304   if (idx == INVBLK)
305     return (ucs2_t)INVALC;
306 
307   return (ucs2_t)tblp[(code & 0x00FF) + idx];
308 }
309 
310 /*
311  * find_code_speed_8bit - find code in 8 bit speed-optimized table.
312  *
313  * PARAMETERS:
314  *     ucs2_t code - code whose mapping to find.
315  *     const __uint16_t *tblp - table pointer.
316  *
317  * RETURN:
318  *     Code that corresponds to 'code'.
319  */
320 static __inline ucs2_t
find_code_speed_8bit(ucs2_t code,const unsigned char * tblp)321 find_code_speed_8bit (ucs2_t code,
322                              const unsigned char *tblp)
323 {
324   int idx;
325   unsigned char ccs;
326 
327   if (code == ((ucs2_t *)tblp)[0])
328     return (ucs2_t)0xFF;
329 
330   idx = ((ucs2_t *)tblp)[1 + (code >> 8)];
331 
332   if (idx == INVBLK)
333     return (ucs2_t)INVALC;
334 
335   ccs = tblp[(code & 0x00FF) + idx];
336 
337   return ccs == 0xFF ? (ucs2_t)INVALC : (ucs2_t)ccs;
338 }
339 
340 /* Left range boundary */
341 #define RANGE_LEFT(n)     (tblp[FIRST_RANGE_INDEX + (n)*3 + 0])
342 /* Right range boundary */
343 #define RANGE_RIGHT(n)    (tblp[FIRST_RANGE_INDEX + (n)*3 + 1])
344 /* Range offset */
345 #define RANGE_INDEX(n)    (tblp[FIRST_RANGE_INDEX + (n)*3 + 2])
346 /* Un-ranged offset */
347 #define UNRANGED_INDEX(n) (tblp[FIRST_UNRANGED_INDEX_INDEX] + (n)*2)
348 
349 /*
350  * find_code_size - find code in 16 bit size-optimized table.
351  *
352  * PARAMETERS:
353  *     ucs2_t code - code whose mapping to find.
354  *     const __uint16_t *tblp - table pointer.
355  *
356  * RETURN:
357  *     Code that corresponds to 'code'.
358  */
359 static ucs2_t
find_code_size(ucs2_t code,const __uint16_t * tblp)360 find_code_size (ucs2_t code,
361                        const __uint16_t *tblp)
362 {
363   int first, last, cur, center;
364 
365   if (tblp[RANGES_NUM_INDEX] > 0)
366     {
367       first = 0;
368       last = tblp[RANGES_NUM_INDEX] - 1;
369 
370       do
371         {
372           center = (last - first)/2;
373           cur = center + first;
374 
375           if (code > RANGE_RIGHT (cur))
376             first = cur;
377           else if (code < RANGE_LEFT (cur))
378             last = cur;
379           else
380             return (ucs2_t)tblp[RANGE_INDEX (cur) + code - RANGE_LEFT (cur)];
381         } while (center > 0);
382 
383         if (last - first == 1)
384           {
385             if (code >= RANGE_LEFT (first) && code <= RANGE_RIGHT (first))
386               return (ucs2_t)tblp[RANGE_INDEX (first)
387                                   + code - RANGE_LEFT (first)];
388             if (code >= RANGE_LEFT (last) && code <= RANGE_RIGHT (last))
389               return (ucs2_t)tblp[RANGE_INDEX (last)
390                                   + code - RANGE_LEFT (last)];
391           }
392     }
393 
394   if (tblp[UNRANGED_NUM_INDEX] > 0)
395     {
396       first = 0;
397       last = tblp[UNRANGED_NUM_INDEX] - 1;
398 
399       do
400         {
401           int c;
402 
403           center = (last - first)/2;
404           cur = center + first;
405           c = tblp[UNRANGED_INDEX (cur)];
406 
407           if (code > c)
408             first = cur;
409           else if (code < c)
410             last = cur;
411           else
412             return (ucs2_t)tblp[UNRANGED_INDEX (cur) + 1];
413         } while (center > 0);
414 
415         if (last - first == 1)
416           {
417             if (code == tblp[UNRANGED_INDEX (first)])
418               return (ucs2_t)tblp[UNRANGED_INDEX (first) + 1];
419             if (code == tblp[UNRANGED_INDEX (last)])
420               return (ucs2_t)tblp[UNRANGED_INDEX (last) + 1];
421           }
422     }
423 
424   return (ucs2_t)INVALC;
425 }
426 
427 #ifdef _ICONV_ENABLE_EXTERNAL_CCS
428 
429 #define _16BIT_ELT(offset) \
430     ICONV_BETOHS(*((__uint16_t *)(buf + (offset))))
431 #define _32BIT_ELT(offset) \
432     ICONV_BETOHL(*((__uint32_t *)(buf + (offset))))
433 
434 /*
435  * load_file - load conversion table from external file and initialize
436  *             iconv_ccs_desc_t object.
437  *
438  * PARAMETERS:
439  *    const char *name - encoding name.
440  *    int direction - conversion direction.
441  *
442  * DESCRIPTION:
443  *    Loads conversion table of appropriate endianess from external file
444  *    and initializes 'iconv_ccs_desc_t' table description structure.
445  *    If 'direction' is 0 - load "To UCS" table, else load "From UCS"
446  *    table.
447  *
448  * RETURN:
449  *    iconv_ccs_desc_t * pointer is success, NULL if failure.
450  */
451 static const iconv_ccs_desc_t *
load_file(const char * name,int direction)452 load_file (
453                   const char *name,
454                   int direction)
455 {
456   int fd;
457   const unsigned char *buf;
458   int tbllen, hdrlen;
459   off_t off;
460   const char *fname;
461   iconv_ccs_desc_t *ccsp = NULL;
462   int nmlen = strlen(name);
463   /* Since CCS table name length can vary - it is aligned (by adding extra
464    * bytes to it's end) to 4-byte boundary. */
465   int alignment = nmlen & 3 ? 4 - (nmlen & 3) : 0;
466 
467   nmlen = strlen(name);
468 
469   hdrlen = nmlen + EXTTABLE_HEADER_LEN + alignment;
470 
471   if ((fname = _iconv_nls_construct_filename (name, ICONV_SUBDIR,
472                                               ICONV_DATA_EXT)) == NULL)
473     return NULL;
474 
475   if ((fd = open (fname, O_RDONLY, S_IRUSR)) == -1)
476     goto error1;
477 
478   if ((buf = (const unsigned char *)malloc (hdrlen)) == NULL)
479     goto error2;
480 
481   if (read (fd, (void *)buf, hdrlen) != hdrlen)
482     goto error3;
483 
484   if (_16BIT_ELT (EXTTABLE_VERSION_OFF) != TABLE_VERSION_1
485       || _32BIT_ELT (EXTTABLE_CCSNAME_LEN_OFF) != nmlen
486       || strncmp (buf + EXTTABLE_CCSNAME_OFF, name, nmlen) != 0)
487     goto error3; /* Bad file */
488 
489   if ((ccsp = (iconv_ccs_desc_t *)
490            calloc (1, sizeof (iconv_ccs_desc_t))) == NULL)
491     goto error3;
492 
493   ccsp->bits = _16BIT_ELT (EXTTABLE_BITS_OFF);
494   ccsp->type = TABLE_EXTERNAL;
495 
496   /* Add 4-byte alignment to name length */
497   nmlen += alignment;
498 
499   if (ccsp->bits == TABLE_8BIT)
500     {
501       if (direction == 0) /* Load "To UCS" table */
502         {
503           off = (off_t)_32BIT_ELT (nmlen + EXTTABLE_TO_SPEED_OFF);
504           tbllen = _32BIT_ELT (nmlen + EXTTABLE_TO_SPEED_LEN_OFF);
505         }
506       else /* Load "From UCS" table */
507         {
508           off = (off_t)_32BIT_ELT (nmlen + EXTTABLE_FROM_SPEED_OFF);
509           tbllen = _32BIT_ELT (nmlen + EXTTABLE_FROM_SPEED_LEN_OFF);
510         }
511     }
512   else if (ccsp->bits == TABLE_16BIT)
513     {
514       if (direction == 0) /* Load "To UCS" table */
515         {
516 #ifdef TABLE_USE_SIZE_OPTIMIZATION
517           off = (off_t)_32BIT_ELT (nmlen + EXTTABLE_TO_SIZE_OFF);
518           tbllen = _32BIT_ELT (nmlen + EXTTABLE_TO_SIZE_LEN_OFF);
519 #else
520           off = (off_t)_32BIT_ELT (nmlen + EXTTABLE_TO_SPEED_OFF);
521           tbllen = _32BIT_ELT (nmlen + EXTTABLE_TO_SPEED_LEN_OFF);
522 #endif
523         }
524       else /* Load "From UCS" table */
525         {
526 #ifdef TABLE_USE_SIZE_OPTIMIZATION
527           off = (off_t)_32BIT_ELT (nmlen + EXTTABLE_FROM_SIZE_OFF);
528           tbllen = _32BIT_ELT (nmlen + EXTTABLE_FROM_SIZE_LEN_OFF);
529 #else
530           off = (off_t)_32BIT_ELT (nmlen + EXTTABLE_FROM_SPEED_OFF);
531           tbllen = _32BIT_ELT (nmlen + EXTTABLE_FROM_SPEED_LEN_OFF);
532 #endif
533         }
534 #ifdef TABLE_USE_SIZE_OPTIMIZATION
535       ccsp->optimization = TABLE_SIZE_OPTIMIZED;
536 #else
537       ccsp->optimization = TABLE_SPEED_OPTIMIZED;
538 #endif
539     }
540   else
541     goto error4; /* Bad file */
542 
543   if (off == EXTTABLE_NO_TABLE)
544     goto error4; /* No correspondent table in file */
545 
546   if ((ccsp->tbl = (ucs2_t *)malloc (tbllen)) == NULL)
547     goto error4;
548 
549   if (lseek (fd, off, SEEK_SET) == (off_t)-1
550       || read (fd, (void *)ccsp->tbl, tbllen) != tbllen)
551     goto error5;
552 
553   goto normal_exit;
554 
555 error5:
556   free ((void *)ccsp->tbl);
557   ccsp->tbl = NULL;
558 error4:
559   free ((void *)ccsp);
560   ccsp = NULL;
561 error3:
562 normal_exit:
563   free ((void *)buf);
564 error2:
565   if (close (fd) == -1)
566     {
567       if (ccsp != NULL)
568         {
569           if (ccsp->tbl != NULL)
570             free ((void *)ccsp->tbl);
571           free ((void *)ccsp);
572         }
573       ccsp = NULL;
574     }
575 error1:
576   free ((void *)fname);
577   return ccsp;
578 }
579 #endif
580 
581 #endif /* ICONV_TO_UCS_CES_TABLE || ICONV_FROM_UCS_CES_TABLE */
582 
583