iscontrol(8): Fix synopsis, sync usage() & improve markup
[dragonfly.git] / sys / libiconv / iconv_xlat16.c
blobaec7fd96bd5f12bd2ac8531e8d311144029fe9c5
1 /*-
2 * Copyright (c) 2003, Ryuichiro Imura
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/sys/libkern/iconv_xlat16.c,v 1.3.20.1 2009/04/15 03:14:26 kensmith Exp $
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/iconv.h>
35 #include "iconv_converter_if.h"
38 * "XLAT16" converter
41 #ifdef MODULE_DEPEND
42 MODULE_DEPEND(iconv_xlat16, libiconv, 2, 2, 2);
43 #endif
46 * XLAT16 converter instance
48 struct iconv_xlat16 {
49 KOBJ_FIELDS;
50 uint32_t * d_table[0x200];
51 struct iconv_cspair * d_csp;
54 static int
55 iconv_xlat16_open(struct iconv_converter_class *dcp,
56 struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp)
58 struct iconv_xlat16 *dp;
59 uint32_t *headp, **idxp;
60 int i;
62 dp = (struct iconv_xlat16 *)kobj_create((struct kobj_class*)dcp, M_ICONV, M_WAITOK);
63 headp = (uint32_t *)((caddr_t)csp->cp_data + sizeof(dp->d_table));
64 idxp = (uint32_t **)csp->cp_data;
65 for (i = 0 ; i < 0x200 ; i++) {
66 if (*idxp) {
67 dp->d_table[i] = headp;
68 headp += 0x80;
69 } else {
70 dp->d_table[i] = NULL;
72 idxp++;
74 dp->d_csp = csp;
75 csp->cp_refcount++;
76 *dpp = (void*)dp;
77 return (0);
80 static int
81 iconv_xlat16_close(void *data)
83 struct iconv_xlat16 *dp = data;
85 dp->d_csp->cp_refcount--;
86 kobj_delete((struct kobj*)data, M_ICONV);
87 return (0);
90 static int
91 iconv_xlat16_conv(void *d2p, const char **inbuf,
92 size_t *inbytesleft, char **outbuf, size_t *outbytesleft,
93 int convchar, int casetype)
95 struct iconv_xlat16 *dp = (struct iconv_xlat16*)d2p;
96 const char *src;
97 char *dst;
98 int nullin, ret = 0;
99 size_t in, on, ir, or, inlen;
100 uint32_t code;
101 u_char u, l;
102 uint16_t c1, c2;
104 if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL)
105 return (0);
106 ir = in = *inbytesleft;
107 or = on = *outbytesleft;
108 src = *inbuf;
109 dst = *outbuf;
111 while(ir > 0 && or > 0) {
113 inlen = 0;
114 code = '\0';
116 c1 = ir > 1 ? *(src+1) & 0xff : 0;
117 c2 = *src & 0xff;
119 c1 = c2 & 0x80 ? c1 | 0x100 : c1;
120 c2 = c2 & 0x80 ? c2 & 0x7f : c2;
122 if (ir > 1 && dp->d_table[c1]) {
124 * inbuf char is a double byte char
126 code = dp->d_table[c1][c2];
127 if (code)
128 inlen = 2;
131 if (inlen == 0) {
132 c1 &= 0xff00;
133 if (!dp->d_table[c1]) {
134 ret = -1;
135 break;
138 * inbuf char is a single byte char
140 inlen = 1;
141 code = dp->d_table[c1][c2];
142 if (!code) {
143 ret = -1;
144 break;
148 nullin = (code & XLAT16_ACCEPT_NULL_IN) ? 1 : 0;
149 if (inlen == 1 && nullin) {
151 * XLAT16_ACCEPT_NULL_IN requires inbuf has 2byte
153 ret = -1;
154 break;
158 * now start translation
160 if ((casetype == KICONV_FROM_LOWER && code & XLAT16_HAS_FROM_LOWER_CASE) ||
161 (casetype == KICONV_FROM_UPPER && code & XLAT16_HAS_FROM_UPPER_CASE)) {
162 c2 = (u_char)(code >> 16);
163 c1 = c2 & 0x80 ? 0x100 : 0;
164 c2 = c2 & 0x80 ? c2 & 0x7f : c2;
165 code = dp->d_table[c1][c2];
168 u = (u_char)(code >> 8);
169 l = (u_char)code;
171 #ifdef XLAT16_ACCEPT_3BYTE_CHR
172 if (code & XLAT16_IS_3BYTE_CHR) {
173 if (or < 3) {
174 ret = -1;
175 break;
177 *dst++ = u;
178 *dst++ = l;
179 *dst++ = (u_char)(code >> 16);
180 or -= 3;
181 } else
182 #endif
183 if (u || code & XLAT16_ACCEPT_NULL_OUT) {
184 if (or < 2) {
185 ret = -1;
186 break;
188 *dst++ = u;
189 *dst++ = l;
190 or -= 2;
191 } else {
192 if ((casetype == KICONV_LOWER && code & XLAT16_HAS_LOWER_CASE) ||
193 (casetype == KICONV_UPPER && code & XLAT16_HAS_UPPER_CASE))
194 *dst++ = (u_char)(code >> 16);
195 else
196 *dst++ = l;
197 or--;
200 if (inlen == 2) {
202 * there is a case that inbuf char is a single
203 * byte char while inlen == 2
205 if ((u_char)*(src+1) == 0 && !nullin ) {
206 src++;
207 ir--;
208 } else {
209 src += 2;
210 ir -= 2;
212 } else {
213 src++;
214 ir--;
217 if (convchar == 1)
218 break;
221 *inbuf += in - ir;
222 *outbuf += on - or;
223 *inbytesleft -= in - ir;
224 *outbytesleft -= on - or;
225 return (ret);
228 static const char *
229 iconv_xlat16_name(struct iconv_converter_class *dcp)
231 return ("xlat16");
234 static kobj_method_t iconv_xlat16_methods[] = {
235 KOBJMETHOD(iconv_converter_open, iconv_xlat16_open),
236 KOBJMETHOD(iconv_converter_close, iconv_xlat16_close),
237 KOBJMETHOD(iconv_converter_conv, iconv_xlat16_conv),
238 #if 0
239 KOBJMETHOD(iconv_converter_init, iconv_xlat16_init),
240 KOBJMETHOD(iconv_converter_done, iconv_xlat16_done),
241 #endif
242 KOBJMETHOD(iconv_converter_name, iconv_xlat16_name),
243 {0, 0}
246 KICONV_CONVERTER(xlat16, sizeof(struct iconv_xlat16));