FreeBSD kernel libkern code
iconv_xlat.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2000-2001 Boris Popov
3  * 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 
27 #include <sys/cdefs.h>
28 __FBSDID("$BSDSUniX$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/iconv.h>
35 
36 #include "iconv_converter_if.h"
37 
38 /*
39  * "XLAT" converter
40  */
41 
42 #ifdef MODULE_DEPEND
43 MODULE_DEPEND(iconv_xlat, libiconv, 2, 2, 2);
44 #endif
45 
46 /*
47  * XLAT converter instance
48  */
49 struct iconv_xlat {
51  u_char * d_table;
52  struct iconv_cspair * d_csp;
53 };
54 
55 static int
56 iconv_xlat_open(struct iconv_converter_class *dcp,
57  struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp)
58 {
59  struct iconv_xlat *dp;
60 
61  dp = (struct iconv_xlat *)kobj_create((struct kobj_class*)dcp, M_ICONV, M_WAITOK);
62  dp->d_table = csp->cp_data;
63  dp->d_csp = csp;
64  csp->cp_refcount++;
65  *dpp = (void*)dp;
66  return 0;
67 }
68 
69 static int
70 iconv_xlat_close(void *data)
71 {
72  struct iconv_xlat *dp = data;
73 
74  dp->d_csp->cp_refcount--;
75  kobj_delete((struct kobj*)data, M_ICONV);
76  return 0;
77 }
78 
79 static int
80 iconv_xlat_conv(void *d2p, const char **inbuf,
81  size_t *inbytesleft, char **outbuf, size_t *outbytesleft,
82  int convchar, int casetype)
83 {
84  struct iconv_xlat *dp = (struct iconv_xlat*)d2p;
85  const char *src;
86  char *dst;
87  int n, r;
88 
89  if (inbuf == NULL || *inbuf == NULL || outbuf == NULL || *outbuf == NULL)
90  return 0;
91  if (casetype != 0)
92  return -1;
93  if (convchar == 1)
94  r = n = 1;
95  else
96  r = n = min(*inbytesleft, *outbytesleft);
97  src = *inbuf;
98  dst = *outbuf;
99  while(r--)
100  *dst++ = dp->d_table[(u_char)*src++];
101  *inbuf += n;
102  *outbuf += n;
103  *inbytesleft -= n;
104  *outbytesleft -= n;
105  return 0;
106 }
107 
108 static const char *
109 iconv_xlat_name(struct iconv_converter_class *dcp)
110 {
111  return "xlat";
112 }
113 
114 static kobj_method_t iconv_xlat_methods[] = {
115  KOBJMETHOD(iconv_converter_open, iconv_xlat_open),
116  KOBJMETHOD(iconv_converter_close, iconv_xlat_close),
117  KOBJMETHOD(iconv_converter_conv, iconv_xlat_conv),
118 #if 0
119  KOBJMETHOD(iconv_converter_init, iconv_xlat_init),
120  KOBJMETHOD(iconv_converter_done, iconv_xlat_done),
121 #endif
122  KOBJMETHOD(iconv_converter_name, iconv_xlat_name),
123  {0, 0}
124 };
125 
126 KICONV_CONVERTER(xlat, sizeof(struct iconv_xlat));
static int iconv_xlat_open(struct iconv_converter_class *dcp, struct iconv_cspair *csp, struct iconv_cspair *cspf, void **dpp)
Definition: iconv_xlat.c:56
const char ** inbuf
static int iconv_xlat_conv(void *d2p, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int convchar, int casetype)
Definition: iconv_xlat.c:80
struct iconv_cspair * d_csp
Definition: iconv_xlat.c:52
int convchar
__FBSDID("$BSDSUniX$")
int casetype
KICONV_CONVERTER(xlat, sizeof(struct iconv_xlat))
u_char * d_table
Definition: iconv_xlat.c:51
char ** outbuf
static const char * iconv_xlat_name(struct iconv_converter_class *dcp)
Definition: iconv_xlat.c:109
static int iconv_xlat_close(void *data)
Definition: iconv_xlat.c:70
size_t * outbytesleft
static kobj_method_t iconv_xlat_methods[]
Definition: iconv_xlat.c:114
size_t * inbytesleft
#define min(a, b)
Definition: qsort.c:44