Sync Citrus iconv support with NetBSD.
[dragonfly.git] / lib / libc / citrus / modules / citrus_mapper_serial.c
1 /* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_serial.c,v 1.2 2003/07/12 15:39:20 tshiozak Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_serial.c,v 1.3 2008/04/10 10:21:01 hasso Exp $ */
3
4 /*-
5  * Copyright (c)2003 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "citrus_namespace.h"
40 #include "citrus_types.h"
41 #include "citrus_bcs.h"
42 #include "citrus_module.h"
43 #include "citrus_region.h"
44 #include "citrus_memstream.h"
45 #include "citrus_mmap.h"
46 #include "citrus_hash.h"
47 #include "citrus_mapper.h"
48 #include "citrus_mapper_serial.h"
49
50 /* ---------------------------------------------------------------------- */
51
52 _CITRUS_MAPPER_DECLS(mapper_serial);
53 _CITRUS_MAPPER_DEF_OPS(mapper_serial);
54
55 #define _citrus_mapper_parallel_mapper_init             \
56         _citrus_mapper_serial_mapper_init
57 #define _citrus_mapper_parallel_mapper_uninit           \
58         _citrus_mapper_serial_mapper_uninit
59 #define _citrus_mapper_parallel_mapper_init_state       \
60         _citrus_mapper_serial_mapper_init_state
61 static int      _citrus_mapper_parallel_mapper_convert(
62         struct _citrus_mapper * __restrict, _index_t * __restrict, _index_t,
63         void * __restrict);
64
65 CITRUS_MODULE(mapper_parallel, mapper, _citrus_mapper_parallel_mapper_getops)
66 _CITRUS_MAPPER_DEF_OPS(mapper_parallel);
67 #undef _citrus_mapper_parallel_mapper_init
68 #undef _citrus_mapper_parallel_mapper_uninit
69 #undef _citrus_mapper_parallel_mapper_init_state
70
71
72 /* ---------------------------------------------------------------------- */
73
74 struct maplink {
75         STAILQ_ENTRY(maplink)   ml_entry;
76         struct _mapper          *ml_mapper;
77 };
78 STAILQ_HEAD(maplist, maplink);
79
80 struct _citrus_mapper_serial {
81         struct maplist  sr_mappers;
82 };
83
84 int
85 _citrus_mapper_serial_mapper_getops(struct _citrus_mapper_ops *ops,
86                                     size_t lenops, uint32_t expected_version)
87 {
88         if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
89                 return EINVAL;
90
91         memcpy(ops, &_citrus_mapper_serial_mapper_ops,
92                sizeof(_citrus_mapper_serial_mapper_ops));
93
94         return 0;
95 }
96
97 int
98 _citrus_mapper_parallel_mapper_getops(struct _citrus_mapper_ops *ops,
99                                       size_t lenops, uint32_t expected_version)
100 {
101         if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
102                 return EINVAL;
103
104         memcpy(ops, &_citrus_mapper_parallel_mapper_ops,
105                sizeof(_citrus_mapper_parallel_mapper_ops));
106
107         return 0;
108 }
109
110 static void
111 uninit(struct _citrus_mapper_serial *sr)
112 {
113         struct maplink *ml;
114
115         while ((ml = STAILQ_FIRST(&sr->sr_mappers)) != NULL) {
116                 STAILQ_REMOVE_HEAD(&sr->sr_mappers, ml_entry);
117                 _mapper_close(ml->ml_mapper);
118                 free(ml);
119         }
120 }
121
122 static int
123 parse_var(struct _citrus_mapper_area *__restrict ma,
124           struct _citrus_mapper_serial *sr, struct _memstream *ms)
125 {
126         int ret;
127         struct _region r;
128         char mapname[PATH_MAX];
129         struct maplink *ml;
130
131         STAILQ_INIT(&sr->sr_mappers);
132         while (1) {
133                 /* remove beginning white spaces */
134                 _memstream_skip_ws(ms);
135                 if (_memstream_iseof(ms))
136                         break;
137                 /* cut down a mapper name */
138                 _memstream_chr(ms, &r, ',');
139                 snprintf(mapname, sizeof(mapname), "%.*s",
140                          (int)_region_size(&r), (char *)_region_head(&r));
141                 /* remove trailing white spaces */
142                 mapname[_bcs_skip_nonws(mapname)-mapname] = '\0';
143                 /* create a new mapper record */
144                 ml = malloc(sizeof(*ml));
145                 if (ml == NULL)
146                         return errno;
147                 ret = _mapper_open(ma, &ml->ml_mapper, mapname);
148                 if (ret) {
149                         free(ml);
150                         return ret;
151                 }
152                 /* support only 1:1 and stateless converter */
153                 if (_mapper_get_src_max(ml->ml_mapper) != 1 ||
154                     _mapper_get_dst_max(ml->ml_mapper) != 1 ||
155                     _mapper_get_state_size(ml->ml_mapper) != 0) {
156                         free(ml);
157                         return EINVAL;
158                 }
159                 STAILQ_INSERT_TAIL(&sr->sr_mappers, ml, ml_entry);
160         }
161         return 0;
162 }
163
164 static int
165 /*ARGSUSED*/
166 _citrus_mapper_serial_mapper_init(struct _citrus_mapper_area *__restrict ma,
167                                   struct _citrus_mapper * __restrict cm,
168                                   const char * __restrict dir,
169                                   const void * __restrict var, size_t lenvar,
170                                   struct _citrus_mapper_traits * __restrict mt,
171                                   size_t lenmt)
172 {
173         struct _citrus_mapper_serial *sr;
174         struct _memstream ms;
175         struct _region r;
176
177         _DIAGASSERT(cm && dir && mt);
178
179         if (lenmt<sizeof(*mt))
180                 return EINVAL;
181
182         sr = malloc(sizeof(*sr));
183         if (sr == NULL)
184                 return errno;
185
186         _region_init(&r, (void *)var, lenvar);
187         _memstream_bind(&ms, &r);
188         if (parse_var(ma, sr, &ms)) {
189                 uninit(sr);
190                 free(sr);
191                 return EINVAL;
192         }
193         cm->cm_closure = sr;
194         mt->mt_src_max = mt->mt_dst_max = 1;    /* 1:1 converter */
195         mt->mt_state_size = 0;                  /* stateless */
196
197         return 0;
198 }
199
200 static void
201 /*ARGSUSED*/
202 _citrus_mapper_serial_mapper_uninit(struct _citrus_mapper *cm)
203 {
204         if (cm && cm->cm_closure) {
205                 uninit(cm->cm_closure);
206                 free(cm->cm_closure);
207         }
208 }
209
210 static int
211 /*ARGSUSED*/
212 _citrus_mapper_serial_mapper_convert(struct _citrus_mapper * __restrict cm,
213                                      _index_t * __restrict dst, _index_t src,
214                                      void * __restrict ps)
215 {
216         int ret;
217         struct _citrus_mapper_serial *sr;
218         struct maplink *ml;
219
220         _DIAGASSERT(cm && cm->cm_closure);
221
222         sr = cm->cm_closure;
223         STAILQ_FOREACH(ml, &sr->sr_mappers, ml_entry) {
224                 ret = _mapper_convert(ml->ml_mapper, &src, src, NULL);
225                 if (ret != _MAPPER_CONVERT_SUCCESS)
226                         return ret;
227         }
228         *dst = src;
229         return _MAPPER_CONVERT_SUCCESS;
230 }
231
232 static int
233 /*ARGSUSED*/
234 _citrus_mapper_parallel_mapper_convert(struct _citrus_mapper * __restrict cm,
235                                        _index_t * __restrict dst, _index_t src,
236                                        void * __restrict ps)
237 {
238         int ret;
239         struct _citrus_mapper_serial *sr;
240         struct maplink *ml;
241         _index_t tmp;
242
243         _DIAGASSERT(cm && cm->cm_closure);
244
245         sr = cm->cm_closure;
246         STAILQ_FOREACH(ml, &sr->sr_mappers, ml_entry) {
247                 ret = _mapper_convert(ml->ml_mapper, &tmp, src, NULL);
248                 if (ret == _MAPPER_CONVERT_SUCCESS) {
249                         *dst = tmp;
250                         return _MAPPER_CONVERT_SUCCESS;
251                 } else if (ret == _MAPPER_CONVERT_ILSEQ)
252                         return _MAPPER_CONVERT_ILSEQ;
253         }
254         return _MAPPER_CONVERT_NONIDENTICAL;
255 }
256
257 static void
258 /*ARGSUSED*/
259 _citrus_mapper_serial_mapper_init_state(struct _citrus_mapper * __restrict cm,
260                                         void * __restrict ps)
261 {
262 }