citrus_mapper_std.c revision 1.5 1 1.5 dogcow /* $NetBSD: citrus_mapper_std.c,v 1.5 2006/02/09 22:03:15 dogcow Exp $ */
2 1.1 tshiozak
3 1.1 tshiozak /*-
4 1.1 tshiozak * Copyright (c)2003 Citrus Project,
5 1.1 tshiozak * All rights reserved.
6 1.1 tshiozak *
7 1.1 tshiozak * Redistribution and use in source and binary forms, with or without
8 1.1 tshiozak * modification, are permitted provided that the following conditions
9 1.1 tshiozak * are met:
10 1.1 tshiozak * 1. Redistributions of source code must retain the above copyright
11 1.1 tshiozak * notice, this list of conditions and the following disclaimer.
12 1.1 tshiozak * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tshiozak * notice, this list of conditions and the following disclaimer in the
14 1.1 tshiozak * documentation and/or other materials provided with the distribution.
15 1.1 tshiozak *
16 1.1 tshiozak * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 tshiozak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 tshiozak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 tshiozak * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 tshiozak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 tshiozak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 tshiozak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 tshiozak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 tshiozak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 tshiozak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 tshiozak * SUCH DAMAGE.
27 1.1 tshiozak */
28 1.1 tshiozak
29 1.1 tshiozak #include <sys/cdefs.h>
30 1.1 tshiozak #if defined(LIBC_SCCS) && !defined(lint)
31 1.5 dogcow __RCSID("$NetBSD: citrus_mapper_std.c,v 1.5 2006/02/09 22:03:15 dogcow Exp $");
32 1.1 tshiozak #endif /* LIBC_SCCS and not lint */
33 1.1 tshiozak
34 1.1 tshiozak #include <assert.h>
35 1.1 tshiozak #include <errno.h>
36 1.1 tshiozak #include <limits.h>
37 1.1 tshiozak #include <stdio.h>
38 1.1 tshiozak #include <stdlib.h>
39 1.1 tshiozak #include <stdint.h>
40 1.1 tshiozak #include <string.h>
41 1.5 dogcow #include <machine/endian.h>
42 1.2 tshiozak #include <sys/queue.h>
43 1.1 tshiozak
44 1.1 tshiozak #include "citrus_namespace.h"
45 1.1 tshiozak #include "citrus_types.h"
46 1.1 tshiozak #include "citrus_bcs.h"
47 1.1 tshiozak #include "citrus_region.h"
48 1.1 tshiozak #include "citrus_mmap.h"
49 1.1 tshiozak #include "citrus_module.h"
50 1.1 tshiozak #include "citrus_hash.h"
51 1.1 tshiozak #include "citrus_mapper.h"
52 1.1 tshiozak #include "citrus_db.h"
53 1.1 tshiozak #include "citrus_db_hash.h"
54 1.1 tshiozak
55 1.1 tshiozak #include "citrus_mapper_std.h"
56 1.1 tshiozak #include "citrus_mapper_std_file.h"
57 1.1 tshiozak
58 1.1 tshiozak /* ---------------------------------------------------------------------- */
59 1.1 tshiozak
60 1.1 tshiozak _CITRUS_MAPPER_DECLS(mapper_std);
61 1.1 tshiozak _CITRUS_MAPPER_DEF_OPS(mapper_std);
62 1.1 tshiozak
63 1.1 tshiozak
64 1.1 tshiozak /* ---------------------------------------------------------------------- */
65 1.1 tshiozak
66 1.1 tshiozak int
67 1.1 tshiozak _citrus_mapper_std_mapper_getops(struct _citrus_mapper_ops *ops, size_t lenops,
68 1.1 tshiozak u_int32_t expected_version)
69 1.1 tshiozak {
70 1.1 tshiozak if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
71 1.1 tshiozak return (EINVAL);
72 1.1 tshiozak
73 1.1 tshiozak memcpy(ops, &_citrus_mapper_std_mapper_ops,
74 1.1 tshiozak sizeof(_citrus_mapper_std_mapper_ops));
75 1.1 tshiozak
76 1.1 tshiozak return (0);
77 1.1 tshiozak }
78 1.1 tshiozak
79 1.1 tshiozak /* ---------------------------------------------------------------------- */
80 1.1 tshiozak
81 1.1 tshiozak static int
82 1.1 tshiozak /*ARGSUSED*/
83 1.1 tshiozak rowcol_convert(struct _citrus_mapper_std * __restrict ms,
84 1.1 tshiozak _index_t * __restrict dst, _index_t src,
85 1.1 tshiozak void * __restrict ps)
86 1.1 tshiozak {
87 1.1 tshiozak struct _citrus_mapper_std_rowcol *rc = &ms->ms_rowcol;
88 1.1 tshiozak _index_t row, col, idx;
89 1.1 tshiozak u_int32_t conv;
90 1.1 tshiozak
91 1.1 tshiozak if (rc->rc_src_col_bits == 32) {
92 1.1 tshiozak row = 0;
93 1.1 tshiozak col = src;
94 1.1 tshiozak } else {
95 1.1 tshiozak row = src >> rc->rc_src_col_bits;
96 1.1 tshiozak col = src & ((1U<<rc->rc_src_col_bits)-1);
97 1.1 tshiozak }
98 1.1 tshiozak if (row < rc->rc_src_row_begin || row > rc->rc_src_row_end ||
99 1.1 tshiozak col < rc->rc_src_col_begin || col > rc->rc_src_col_end) {
100 1.3 tshiozak switch (rc->rc_oob_mode) {
101 1.3 tshiozak case _CITRUS_MAPPER_STD_OOB_NONIDENTICAL:
102 1.3 tshiozak *dst = rc->rc_dst_invalid;
103 1.3 tshiozak return _MAPPER_CONVERT_NONIDENTICAL;
104 1.3 tshiozak case _CITRUS_MAPPER_STD_OOB_ILSEQ:
105 1.3 tshiozak return _MAPPER_CONVERT_ILSEQ;
106 1.3 tshiozak default:
107 1.3 tshiozak return _MAPPER_CONVERT_FATAL;
108 1.3 tshiozak }
109 1.1 tshiozak }
110 1.1 tshiozak
111 1.1 tshiozak idx =
112 1.1 tshiozak (row - rc->rc_src_row_begin)*rc->rc_src_col_width +
113 1.1 tshiozak (col - rc->rc_src_col_begin);
114 1.1 tshiozak
115 1.1 tshiozak switch (rc->rc_dst_unit_bits) {
116 1.1 tshiozak case 8:
117 1.1 tshiozak conv = _region_peek8(&rc->rc_table, idx);
118 1.1 tshiozak break;
119 1.1 tshiozak case 16:
120 1.1 tshiozak conv = be16toh(_region_peek16(&rc->rc_table, idx*2));
121 1.1 tshiozak break;
122 1.1 tshiozak case 32:
123 1.1 tshiozak conv = be32toh(_region_peek32(&rc->rc_table, idx*4));
124 1.1 tshiozak break;
125 1.1 tshiozak }
126 1.1 tshiozak
127 1.1 tshiozak if (conv == rc->rc_dst_invalid) {
128 1.1 tshiozak *dst = rc->rc_dst_invalid;
129 1.3 tshiozak return _MAPPER_CONVERT_NONIDENTICAL;
130 1.1 tshiozak }
131 1.3 tshiozak if (conv == rc->rc_dst_ilseq)
132 1.3 tshiozak return _MAPPER_CONVERT_ILSEQ;
133 1.1 tshiozak
134 1.1 tshiozak *dst = conv;
135 1.1 tshiozak
136 1.1 tshiozak return _MAPPER_CONVERT_SUCCESS;
137 1.1 tshiozak }
138 1.1 tshiozak
139 1.1 tshiozak
140 1.1 tshiozak static int
141 1.1 tshiozak rowcol_init(struct _citrus_mapper_std *ms)
142 1.1 tshiozak {
143 1.1 tshiozak int ret;
144 1.1 tshiozak struct _citrus_mapper_std_rowcol *rc = &ms->ms_rowcol;
145 1.1 tshiozak const struct _citrus_mapper_std_rowcol_info_x *rcx;
146 1.3 tshiozak const struct _citrus_mapper_std_rowcol_ext_ilseq_info_x *eix;
147 1.1 tshiozak struct _region r;
148 1.1 tshiozak u_int64_t table_size;
149 1.1 tshiozak
150 1.1 tshiozak ms->ms_convert = &rowcol_convert;
151 1.1 tshiozak ms->ms_uninit = NULL;
152 1.1 tshiozak
153 1.1 tshiozak /* get table region */
154 1.1 tshiozak ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TABLE,
155 1.1 tshiozak &rc->rc_table, NULL);
156 1.1 tshiozak if (ret) {
157 1.1 tshiozak if (ret==ENOENT)
158 1.1 tshiozak ret = EFTYPE;
159 1.1 tshiozak return ret;
160 1.1 tshiozak }
161 1.1 tshiozak
162 1.1 tshiozak /* get table information */
163 1.1 tshiozak ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_INFO, &r, NULL);
164 1.1 tshiozak if (ret) {
165 1.1 tshiozak if (ret==ENOENT)
166 1.3 tshiozak ret = EFTYPE;
167 1.1 tshiozak return ret;
168 1.1 tshiozak }
169 1.1 tshiozak if (_region_size(&r) < sizeof(*rcx))
170 1.1 tshiozak return EFTYPE;
171 1.1 tshiozak rcx = _region_head(&r);
172 1.1 tshiozak
173 1.1 tshiozak /* convert */
174 1.1 tshiozak #define CONV_ROWCOL(rc, rcx, elem) \
175 1.1 tshiozak do { \
176 1.1 tshiozak (rc)->rc_##elem = be32toh((rcx)->rcx_##elem); \
177 1.1 tshiozak } while (/*CONSTCOND*/0)
178 1.1 tshiozak CONV_ROWCOL(rc, rcx, src_col_bits);
179 1.1 tshiozak CONV_ROWCOL(rc, rcx, dst_invalid);
180 1.1 tshiozak CONV_ROWCOL(rc, rcx, src_row_begin);
181 1.1 tshiozak CONV_ROWCOL(rc, rcx, src_row_end);
182 1.1 tshiozak CONV_ROWCOL(rc, rcx, src_col_begin);
183 1.1 tshiozak CONV_ROWCOL(rc, rcx, src_col_end);
184 1.1 tshiozak CONV_ROWCOL(rc, rcx, dst_unit_bits);
185 1.1 tshiozak
186 1.3 tshiozak /* ilseq extension */
187 1.3 tshiozak rc->rc_oob_mode = _CITRUS_MAPPER_STD_OOB_NONIDENTICAL;
188 1.3 tshiozak rc->rc_dst_ilseq = rc->rc_dst_invalid;
189 1.3 tshiozak ret = _db_lookup_by_s(ms->ms_db,
190 1.3 tshiozak _CITRUS_MAPPER_STD_SYM_ROWCOL_EXT_ILSEQ,
191 1.3 tshiozak &r, NULL);
192 1.3 tshiozak if (ret && ret != ENOENT)
193 1.3 tshiozak return ret;
194 1.3 tshiozak if (_region_size(&r) < sizeof(*eix))
195 1.3 tshiozak return EFTYPE;
196 1.3 tshiozak if (ret == 0) {
197 1.3 tshiozak eix = _region_head(&r);
198 1.3 tshiozak rc->rc_oob_mode = be32toh(eix->eix_oob_mode);
199 1.3 tshiozak rc->rc_dst_ilseq = be32toh(eix->eix_dst_ilseq);
200 1.3 tshiozak }
201 1.1 tshiozak rc->rc_src_col_width = rc->rc_src_col_end - rc->rc_src_col_begin +1;
202 1.1 tshiozak
203 1.1 tshiozak /* validation checks */
204 1.1 tshiozak if (rc->rc_src_col_end < rc->rc_src_col_begin ||
205 1.1 tshiozak rc->rc_src_row_end < rc->rc_src_row_begin ||
206 1.1 tshiozak !(rc->rc_dst_unit_bits==8 || rc->rc_dst_unit_bits==16 ||
207 1.1 tshiozak rc->rc_dst_unit_bits==32) ||
208 1.1 tshiozak !(rc->rc_src_col_bits >= 0 && rc->rc_src_col_bits <= 32))
209 1.1 tshiozak return EFTYPE;
210 1.1 tshiozak
211 1.1 tshiozak /* calcurate expected table size */
212 1.1 tshiozak table_size = rc->rc_src_row_end - rc->rc_src_row_begin + 1;
213 1.1 tshiozak table_size *= rc->rc_src_col_width;
214 1.1 tshiozak table_size *= rc->rc_dst_unit_bits/8;
215 1.1 tshiozak
216 1.1 tshiozak if (table_size > UINT32_MAX ||
217 1.1 tshiozak _region_size(&rc->rc_table) < table_size)
218 1.1 tshiozak return EFTYPE;
219 1.1 tshiozak
220 1.1 tshiozak return 0;
221 1.1 tshiozak }
222 1.1 tshiozak
223 1.1 tshiozak typedef int (*initfunc_t)(struct _citrus_mapper_std *);
224 1.4 yamt static const struct {
225 1.1 tshiozak const char *t_name;
226 1.1 tshiozak initfunc_t t_init;
227 1.1 tshiozak } types[] = {
228 1.1 tshiozak { _CITRUS_MAPPER_STD_TYPE_ROWCOL, &rowcol_init },
229 1.1 tshiozak };
230 1.1 tshiozak #define NUM_OF_TYPES ((int)(sizeof(types)/sizeof(types[0])))
231 1.1 tshiozak
232 1.1 tshiozak static int
233 1.1 tshiozak /*ARGSUSED*/
234 1.1 tshiozak _citrus_mapper_std_mapper_init(struct _citrus_mapper_area *__restrict ma,
235 1.1 tshiozak struct _citrus_mapper * __restrict cm,
236 1.1 tshiozak const char * __restrict curdir,
237 1.1 tshiozak const void * __restrict var, size_t lenvar,
238 1.1 tshiozak struct _citrus_mapper_traits * __restrict mt,
239 1.1 tshiozak size_t lenmt)
240 1.1 tshiozak {
241 1.1 tshiozak char path[PATH_MAX];
242 1.1 tshiozak const char *type;
243 1.1 tshiozak int ret, id;
244 1.1 tshiozak struct _citrus_mapper_std *ms;
245 1.1 tshiozak
246 1.1 tshiozak /* set traits */
247 1.1 tshiozak if (lenmt<sizeof(*mt)) {
248 1.1 tshiozak ret = EINVAL;
249 1.1 tshiozak goto err0;
250 1.1 tshiozak }
251 1.1 tshiozak mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */
252 1.1 tshiozak mt->mt_state_size = 0; /* stateless */
253 1.1 tshiozak
254 1.1 tshiozak /* alloc mapper std structure */
255 1.1 tshiozak ms = malloc(sizeof(*ms));
256 1.1 tshiozak if (ms==NULL) {
257 1.1 tshiozak ret = errno;
258 1.1 tshiozak goto err0;
259 1.1 tshiozak }
260 1.1 tshiozak
261 1.1 tshiozak /* open mapper file */
262 1.1 tshiozak snprintf(path, sizeof(path),
263 1.1 tshiozak "%s/%.*s", curdir, (int)lenvar, (const char *)var);
264 1.1 tshiozak ret = _map_file(&ms->ms_file, path);
265 1.1 tshiozak if (ret)
266 1.1 tshiozak goto err1;
267 1.1 tshiozak
268 1.1 tshiozak ret = _db_open(&ms->ms_db, &ms->ms_file, _CITRUS_MAPPER_STD_MAGIC,
269 1.1 tshiozak &_db_hash_std, NULL);
270 1.1 tshiozak if (ret)
271 1.1 tshiozak goto err2;
272 1.1 tshiozak
273 1.1 tshiozak /* get mapper type */
274 1.1 tshiozak ret = _db_lookupstr_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TYPE,
275 1.1 tshiozak &type, NULL);
276 1.1 tshiozak if (ret) {
277 1.1 tshiozak if (ret==ENOENT)
278 1.1 tshiozak ret = EFTYPE;
279 1.1 tshiozak goto err3;
280 1.1 tshiozak }
281 1.1 tshiozak for (id=0; id<NUM_OF_TYPES; id++)
282 1.1 tshiozak if (_bcs_strcasecmp(type, types[id].t_name) == 0)
283 1.1 tshiozak break;
284 1.1 tshiozak
285 1.1 tshiozak if (id == NUM_OF_TYPES)
286 1.1 tshiozak goto err3;
287 1.1 tshiozak
288 1.1 tshiozak /* init the per-type structure */
289 1.1 tshiozak ret = (*types[id].t_init)(ms);
290 1.1 tshiozak if (ret)
291 1.1 tshiozak goto err3;
292 1.1 tshiozak
293 1.1 tshiozak cm->cm_closure = ms;
294 1.1 tshiozak
295 1.1 tshiozak return 0;
296 1.1 tshiozak
297 1.1 tshiozak err3:
298 1.1 tshiozak _db_close(ms->ms_db);
299 1.1 tshiozak err2:
300 1.1 tshiozak _unmap_file(&ms->ms_file);
301 1.1 tshiozak err1:
302 1.1 tshiozak free(ms);
303 1.1 tshiozak err0:
304 1.1 tshiozak return ret;
305 1.1 tshiozak }
306 1.1 tshiozak
307 1.1 tshiozak static void
308 1.1 tshiozak /*ARGSUSED*/
309 1.1 tshiozak _citrus_mapper_std_mapper_uninit(struct _citrus_mapper *cm)
310 1.1 tshiozak {
311 1.1 tshiozak struct _citrus_mapper_std *ms;
312 1.1 tshiozak
313 1.1 tshiozak _DIAGASSERT(cm!=NULL & cm->cm_closure!=NULL);
314 1.1 tshiozak
315 1.1 tshiozak ms = cm->cm_closure;
316 1.1 tshiozak if (ms->ms_uninit)
317 1.1 tshiozak (*ms->ms_uninit)(ms);
318 1.1 tshiozak _db_close(ms->ms_db);
319 1.1 tshiozak _unmap_file(&ms->ms_file);
320 1.1 tshiozak free(ms);
321 1.1 tshiozak }
322 1.1 tshiozak
323 1.1 tshiozak static void
324 1.1 tshiozak /*ARGSUSED*/
325 1.1 tshiozak _citrus_mapper_std_mapper_init_state(struct _citrus_mapper * __restrict cm,
326 1.1 tshiozak void * __restrict ps)
327 1.1 tshiozak {
328 1.1 tshiozak }
329 1.1 tshiozak
330 1.1 tshiozak static int
331 1.1 tshiozak /*ARGSUSED*/
332 1.1 tshiozak _citrus_mapper_std_mapper_convert(struct _citrus_mapper * __restrict cm,
333 1.1 tshiozak _index_t * __restrict dst, _index_t src,
334 1.1 tshiozak void * __restrict ps)
335 1.1 tshiozak {
336 1.1 tshiozak struct _citrus_mapper_std *ms;
337 1.1 tshiozak
338 1.1 tshiozak _DIAGASSERT(cm!=NULL && cm->cm_closure!=NULL);
339 1.1 tshiozak
340 1.1 tshiozak ms = cm->cm_closure;
341 1.1 tshiozak
342 1.1 tshiozak _DIAGASSERT(ms->ms_convert != NULL);
343 1.1 tshiozak
344 1.1 tshiozak return (*ms->ms_convert)(ms, dst, src, ps);
345 1.1 tshiozak }
346