1 /*
2  * Copyright (c) 2003-2004, Artem B. Bityuckiy
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 #include "cesbi.h"
26 
27 #if defined (ICONV_TO_UCS_CES_US_ASCII) \
28  || defined (ICONV_FROM_UCS_CES_US_ASCII)
29 
30 #include <sys/types.h>
31 #include "../lib/local.h"
32 #include "../lib/ucsconv.h"
33 
34 /*
35  * For optimization purposes us_ascii is implemented as separate CES converter.
36  * Another possible way is to add us_ascii CCS and use table-based CES converter.
37  */
38 
39 #if defined (ICONV_FROM_UCS_CES_US_ASCII)
40 static size_t
us_ascii_convert_from_ucs(void * data,ucs4_t in,unsigned char ** outbuf,size_t * outbytesleft)41 us_ascii_convert_from_ucs (void *data,
42                                   ucs4_t in,
43                                   unsigned char **outbuf,
44                                   size_t *outbytesleft)
45 {
46   (void) data;
47   if (in  > 0x7F)
48     return (size_t)ICONV_CES_INVALID_CHARACTER;
49 
50   *((char *)(*outbuf)) = (char)in;
51 
52   *outbuf += 1;
53   *outbytesleft -= 1;
54 
55   return 1;
56 }
57 #endif /* ICONV_FROM_UCS_CES_US_ASCII */
58 
59 #if defined (ICONV_TO_UCS_CES_US_ASCII)
60 static ucs4_t
us_ascii_convert_to_ucs(void * data,const unsigned char ** inbuf,size_t * inbytesleft)61 us_ascii_convert_to_ucs (void *data,
62                                 const unsigned char **inbuf,
63                                 size_t *inbytesleft)
64 {
65   (void) data;
66   ucs4_t res;
67 
68   if (*inbytesleft < 1)
69     return (ucs4_t)ICONV_CES_BAD_SEQUENCE;
70 
71   res = (ucs4_t)**inbuf;
72 
73   if (res  > 0x7F)
74     return (ucs4_t)ICONV_CES_INVALID_CHARACTER;
75 
76   *inbytesleft -= 1;
77   *inbuf += 1;
78 
79   return res;
80 }
81 #endif /* ICONV_TO_UCS_CES_US_ASCII */
82 
83 static int
us_ascii_get_mb_cur_max(void * data)84 us_ascii_get_mb_cur_max (void *data)
85 {
86   (void) data;
87   return 2;
88 }
89 
90 #if defined (ICONV_TO_UCS_CES_US_ASCII)
91 const iconv_to_ucs_ces_handlers_t
92 _iconv_to_ucs_ces_handlers_us_ascii =
93 {
94   NULL,
95   NULL,
96   us_ascii_get_mb_cur_max,
97   NULL,
98   NULL,
99   NULL,
100   us_ascii_convert_to_ucs
101 };
102 #endif
103 
104 #if defined (ICONV_FROM_UCS_CES_US_ASCII)
105 const iconv_from_ucs_ces_handlers_t
106 _iconv_from_ucs_ces_handlers_us_ascii =
107 {
108   NULL,
109   NULL,
110   us_ascii_get_mb_cur_max,
111   NULL,
112   NULL,
113   NULL,
114   us_ascii_convert_from_ucs
115 };
116 #endif
117 
118 #endif /* ICONV_TO_UCS_CES_US_ASCII || ICONV_FROM_UCS_CES_US_ASCII */
119 
120