1 /* $NetBSD: citrus_mapper_std.c,v 1.8 2006/09/11 13:06:33 tnozaki Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_std.c,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
5 * Copyright (c)2003, 2006 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/types.h>
31 #include <sys/endian.h>
32 #include <sys/queue.h>
41 #include "citrus_namespace.h"
42 #include "citrus_types.h"
43 #include "citrus_bcs.h"
44 #include "citrus_region.h"
45 #include "citrus_mmap.h"
46 #include "citrus_module.h"
47 #include "citrus_hash.h"
48 #include "citrus_mapper.h"
49 #include "citrus_db.h"
50 #include "citrus_db_hash.h"
52 #include "citrus_mapper_std.h"
53 #include "citrus_mapper_std_file.h"
55 /* ---------------------------------------------------------------------- */
57 _CITRUS_MAPPER_DECLS(mapper_std);
58 _CITRUS_MAPPER_DEF_OPS(mapper_std);
61 /* ---------------------------------------------------------------------- */
64 _citrus_mapper_std_mapper_getops(struct _citrus_mapper_ops *ops, size_t lenops,
65 u_int32_t expected_version)
67 if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
70 memcpy(ops, &_citrus_mapper_std_mapper_ops,
71 sizeof(_citrus_mapper_std_mapper_ops));
76 /* ---------------------------------------------------------------------- */
80 rowcol_convert(struct _citrus_mapper_std * __restrict ms,
81 _index_t * __restrict dst, _index_t src,
84 struct _citrus_mapper_std_rowcol *rc;
86 struct _citrus_mapper_std_linear_zone *lz;
90 _DIAGASSERT(ms != NULL);
91 _DIAGASSERT(dst != NULL);
92 /* ps may be unused */
95 for (i = rc->rc_src_rowcol_len * rc->rc_src_rowcol_bits,
96 lz = &rc->rc_src_rowcol[0]; i > 0; ++lz) {
97 i -= rc->rc_src_rowcol_bits;
98 n = (src >> i) & rc->rc_src_rowcol_mask;
99 if (n < lz->begin || n > lz->end) {
100 switch (rc->rc_oob_mode) {
101 case _CITRUS_MAPPER_STD_OOB_NONIDENTICAL:
102 *dst = rc->rc_dst_invalid;
103 return _MAPPER_CONVERT_NONIDENTICAL;
104 case _CITRUS_MAPPER_STD_OOB_ILSEQ:
105 return _MAPPER_CONVERT_ILSEQ;
107 return _MAPPER_CONVERT_FATAL;
110 idx = idx * lz->width + n - lz->begin;
112 switch (rc->rc_dst_unit_bits) {
114 conv = _region_peek8(&rc->rc_table, idx);
117 conv = be16toh(_region_peek16(&rc->rc_table, idx*2));
120 conv = be32toh(_region_peek32(&rc->rc_table, idx*4));
123 return _MAPPER_CONVERT_FATAL;
126 if (conv == rc->rc_dst_invalid) {
127 *dst = rc->rc_dst_invalid;
128 return _MAPPER_CONVERT_NONIDENTICAL;
130 if (conv == rc->rc_dst_ilseq)
131 return _MAPPER_CONVERT_ILSEQ;
135 return _MAPPER_CONVERT_SUCCESS;
139 set_linear_zone(struct _citrus_mapper_std_linear_zone *lz,
140 u_int32_t begin, u_int32_t end)
142 _DIAGASSERT(lz != NULL);
149 lz->width= end - begin + 1;
155 rowcol_parse_variable_compat(struct _citrus_mapper_std_rowcol *rc,
158 const struct _citrus_mapper_std_rowcol_info_compat_x *rcx;
159 struct _citrus_mapper_std_linear_zone *lz;
163 _DIAGASSERT(rc != NULL);
164 _DIAGASSERT(r != NULL && _region_size(r) == sizeof(*rcx));
165 rcx = _region_head(r);
167 rc->rc_dst_invalid = be32toh(rcx->rcx_dst_invalid);
168 rc->rc_dst_unit_bits = be32toh(rcx->rcx_dst_unit_bits);
169 m = be32toh(rcx->rcx_src_col_bits);
172 rc->rc_src_rowcol_bits = m;
173 rc->rc_src_rowcol_mask = n;
175 rc->rc_src_rowcol = malloc(2 *
176 sizeof(*rc->rc_src_rowcol));
177 if (rc->rc_src_rowcol == NULL)
179 lz = rc->rc_src_rowcol;
180 rc->rc_src_rowcol_len = 1;
181 m = be32toh(rcx->rcx_src_row_begin);
182 n = be32toh(rcx->rcx_src_row_end);
184 ret = set_linear_zone(lz, m, n);
187 ++rc->rc_src_rowcol_len, ++lz;
189 m = be32toh(rcx->rcx_src_col_begin);
190 n = be32toh(rcx->rcx_src_col_end);
192 return set_linear_zone(lz, m, n);
196 rowcol_parse_variable(struct _citrus_mapper_std_rowcol *rc,
199 const struct _citrus_mapper_std_rowcol_info_x *rcx;
200 struct _citrus_mapper_std_linear_zone *lz;
205 _DIAGASSERT(rc != NULL);
206 _DIAGASSERT(r != NULL && _region_size(r) == sizeof(*rcx));
207 rcx = _region_head(r);
209 rc->rc_dst_invalid = be32toh(rcx->rcx_dst_invalid);
210 rc->rc_dst_unit_bits = be32toh(rcx->rcx_dst_unit_bits);
212 m = be32toh(rcx->rcx_src_rowcol_bits);
215 rc->rc_src_rowcol_bits = m;
216 rc->rc_src_rowcol_mask = n;
218 rc->rc_src_rowcol_len = be32toh(rcx->rcx_src_rowcol_len);
219 if (rc->rc_src_rowcol_len > _CITRUS_MAPPER_STD_ROWCOL_MAX)
221 rc->rc_src_rowcol = malloc(rc->rc_src_rowcol_len *
222 sizeof(*rc->rc_src_rowcol));
223 if (rc->rc_src_rowcol == NULL)
225 for (i = 0, lz = rc->rc_src_rowcol;
226 i < rc->rc_src_rowcol_len; ++i, ++lz) {
227 m = be32toh(rcx->rcx_src_rowcol[i].begin),
228 n = be32toh(rcx->rcx_src_rowcol[i].end);
229 ret = set_linear_zone(lz, m, n);
231 free(rc->rc_src_rowcol);
232 rc->rc_src_rowcol = NULL;
240 rowcol_uninit(struct _citrus_mapper_std *ms)
242 struct _citrus_mapper_std_rowcol *rc;
243 _DIAGASSERT(ms != NULL);
246 free(rc->rc_src_rowcol);
250 rowcol_init(struct _citrus_mapper_std *ms)
253 struct _citrus_mapper_std_rowcol *rc;
254 const struct _citrus_mapper_std_rowcol_ext_ilseq_info_x *eix;
256 u_int64_t table_size;
258 struct _citrus_mapper_std_linear_zone *lz;
260 _DIAGASSERT(ms != NULL);
261 ms->ms_convert = &rowcol_convert;
262 ms->ms_uninit = &rowcol_uninit;
265 /* get table region */
266 ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TABLE,
267 &rc->rc_table, NULL);
274 /* get table information */
275 ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_INFO, &r, NULL);
281 switch (_region_size(&r)) {
282 case _CITRUS_MAPPER_STD_ROWCOL_INFO_COMPAT_SIZE:
283 ret = rowcol_parse_variable_compat(rc, &r);
285 case _CITRUS_MAPPER_STD_ROWCOL_INFO_SIZE:
286 ret = rowcol_parse_variable(rc, &r);
294 switch (rc->rc_src_rowcol_bits) {
295 case 8: case 16: case 32:
296 if (rc->rc_src_rowcol_len <= 32 / rc->rc_src_rowcol_bits)
303 /* ilseq extension */
304 rc->rc_oob_mode = _CITRUS_MAPPER_STD_OOB_NONIDENTICAL;
305 rc->rc_dst_ilseq = rc->rc_dst_invalid;
306 ret = _db_lookup_by_s(ms->ms_db,
307 _CITRUS_MAPPER_STD_SYM_ROWCOL_EXT_ILSEQ,
309 if (ret && ret != ENOENT)
311 if (_region_size(&r) < sizeof(*eix))
314 eix = _region_head(&r);
315 rc->rc_oob_mode = be32toh(eix->eix_oob_mode);
316 rc->rc_dst_ilseq = be32toh(eix->eix_dst_ilseq);
319 /* calcurate expected table size */
320 i = rc->rc_src_rowcol_len;
321 lz = &rc->rc_src_rowcol[--i];
322 table_size = lz->width;
324 lz = &rc->rc_src_rowcol[--i];
325 table_size *= lz->width;
327 table_size *= rc->rc_dst_unit_bits/8;
329 if (table_size > UINT32_MAX ||
330 _region_size(&rc->rc_table) < table_size)
336 typedef int (*initfunc_t)(struct _citrus_mapper_std *);
337 static const struct {
341 { _CITRUS_MAPPER_STD_TYPE_ROWCOL, &rowcol_init },
343 #define NUM_OF_TYPES ((int)(sizeof(types)/sizeof(types[0])))
347 _citrus_mapper_std_mapper_init(struct _citrus_mapper_area *__restrict ma,
348 struct _citrus_mapper * __restrict cm,
349 const char * __restrict curdir,
350 const void * __restrict var, size_t lenvar,
351 struct _citrus_mapper_traits * __restrict mt,
357 struct _citrus_mapper_std *ms;
360 if (lenmt<sizeof(*mt)) {
364 mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */
365 mt->mt_state_size = 0; /* stateless */
367 /* alloc mapper std structure */
368 ms = malloc(sizeof(*ms));
374 /* open mapper file */
375 snprintf(path, sizeof(path),
376 "%s/%.*s", curdir, (int)lenvar, (const char *)var);
377 ret = _map_file(&ms->ms_file, path);
381 ret = _db_open(&ms->ms_db, &ms->ms_file, _CITRUS_MAPPER_STD_MAGIC,
382 &_db_hash_std, NULL);
386 /* get mapper type */
387 ret = _db_lookupstr_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TYPE,
394 for (id=0; id<NUM_OF_TYPES; id++)
395 if (_bcs_strcasecmp(type, types[id].t_name) == 0)
398 if (id == NUM_OF_TYPES)
401 /* init the per-type structure */
402 ret = (*types[id].t_init)(ms);
411 _db_close(ms->ms_db);
413 _unmap_file(&ms->ms_file);
422 _citrus_mapper_std_mapper_uninit(struct _citrus_mapper *cm)
424 struct _citrus_mapper_std *ms;
426 _DIAGASSERT(cm!=NULL & cm->cm_closure!=NULL);
430 (*ms->ms_uninit)(ms);
431 _db_close(ms->ms_db);
432 _unmap_file(&ms->ms_file);
438 _citrus_mapper_std_mapper_init_state(struct _citrus_mapper * __restrict cm,
439 void * __restrict ps)
445 _citrus_mapper_std_mapper_convert(struct _citrus_mapper * __restrict cm,
446 _index_t * __restrict dst, _index_t src,
447 void * __restrict ps)
449 struct _citrus_mapper_std *ms;
451 _DIAGASSERT(cm!=NULL && cm->cm_closure!=NULL);
455 _DIAGASSERT(ms->ms_convert != NULL);
457 return (*ms->ms_convert)(ms, dst, src, ps);