2 /* $NetBSD: citrus_mapper_std.c,v 1.11 2018/06/11 18:03:38 kamil Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2003, 2006 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #include <sys/endian.h>
34 #include <sys/queue.h>
44 #include "citrus_namespace.h"
45 #include "citrus_types.h"
46 #include "citrus_bcs.h"
47 #include "citrus_region.h"
48 #include "citrus_mmap.h"
49 #include "citrus_module.h"
50 #include "citrus_hash.h"
51 #include "citrus_mapper.h"
52 #include "citrus_db.h"
53 #include "citrus_db_hash.h"
55 #include "citrus_mapper_std.h"
56 #include "citrus_mapper_std_file.h"
58 /* ---------------------------------------------------------------------- */
60 _CITRUS_MAPPER_DECLS(mapper_std);
61 _CITRUS_MAPPER_DEF_OPS(mapper_std);
64 /* ---------------------------------------------------------------------- */
67 _citrus_mapper_std_mapper_getops(struct _citrus_mapper_ops *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, void * __restrict ps __unused)
83 struct _citrus_mapper_std_linear_zone *lz;
84 struct _citrus_mapper_std_rowcol *rc;
89 /* ps may be unused */
92 for (i = rc->rc_src_rowcol_len * rc->rc_src_rowcol_bits,
93 lz = &rc->rc_src_rowcol[0]; i > 0; ++lz) {
94 i -= rc->rc_src_rowcol_bits;
95 n = (src >> i) & rc->rc_src_rowcol_mask;
96 if (n < lz->begin || n > lz->end) {
97 switch (rc->rc_oob_mode) {
98 case _CITRUS_MAPPER_STD_OOB_NONIDENTICAL:
99 *dst = rc->rc_dst_invalid;
100 return (_MAPPER_CONVERT_NONIDENTICAL);
101 case _CITRUS_MAPPER_STD_OOB_ILSEQ:
102 return (_MAPPER_CONVERT_ILSEQ);
104 return (_MAPPER_CONVERT_FATAL);
107 idx = idx * lz->width + n - lz->begin;
109 switch (rc->rc_dst_unit_bits) {
111 conv = _region_peek8(&rc->rc_table, idx);
114 conv = be16toh(_region_peek16(&rc->rc_table, idx*2));
117 conv = be32toh(_region_peek32(&rc->rc_table, idx*4));
120 return (_MAPPER_CONVERT_FATAL);
123 if (conv == rc->rc_dst_invalid) {
124 *dst = rc->rc_dst_invalid;
125 return (_MAPPER_CONVERT_NONIDENTICAL);
127 if (conv == rc->rc_dst_ilseq)
128 return (_MAPPER_CONVERT_ILSEQ);
132 return (_MAPPER_CONVERT_SUCCESS);
136 set_linear_zone(struct _citrus_mapper_std_linear_zone *lz,
137 uint32_t begin, uint32_t end)
145 lz->width= end - begin + 1;
151 rowcol_parse_variable_compat(struct _citrus_mapper_std_rowcol *rc,
154 const struct _citrus_mapper_std_rowcol_info_compat_x *rcx;
155 struct _citrus_mapper_std_linear_zone *lz;
159 rcx = _region_head(r);
161 rc->rc_dst_invalid = be32toh(rcx->rcx_dst_invalid);
162 rc->rc_dst_unit_bits = be32toh(rcx->rcx_dst_unit_bits);
163 m = be32toh(rcx->rcx_src_col_bits);
166 rc->rc_src_rowcol_bits = m;
167 rc->rc_src_rowcol_mask = n;
169 rc->rc_src_rowcol = malloc(2 *
170 sizeof(*rc->rc_src_rowcol));
171 if (rc->rc_src_rowcol == NULL)
173 lz = rc->rc_src_rowcol;
174 rc->rc_src_rowcol_len = 1;
175 m = be32toh(rcx->rcx_src_row_begin);
176 n = be32toh(rcx->rcx_src_row_end);
178 ret = set_linear_zone(lz, m, n);
180 free(rc->rc_src_rowcol);
181 rc->rc_src_rowcol = NULL;
184 ++rc->rc_src_rowcol_len, ++lz;
186 m = be32toh(rcx->rcx_src_col_begin);
187 n = be32toh(rcx->rcx_src_col_end);
189 return (set_linear_zone(lz, m, n));
193 rowcol_parse_variable(struct _citrus_mapper_std_rowcol *rc,
196 const struct _citrus_mapper_std_rowcol_info_x *rcx;
197 struct _citrus_mapper_std_linear_zone *lz;
202 rcx = _region_head(r);
204 rc->rc_dst_invalid = be32toh(rcx->rcx_dst_invalid);
205 rc->rc_dst_unit_bits = be32toh(rcx->rcx_dst_unit_bits);
207 m = be32toh(rcx->rcx_src_rowcol_bits);
210 rc->rc_src_rowcol_bits = m;
211 rc->rc_src_rowcol_mask = n;
213 rc->rc_src_rowcol_len = be32toh(rcx->rcx_src_rowcol_len);
214 if (rc->rc_src_rowcol_len > _CITRUS_MAPPER_STD_ROWCOL_MAX)
216 rc->rc_src_rowcol = malloc(rc->rc_src_rowcol_len *
217 sizeof(*rc->rc_src_rowcol));
218 if (rc->rc_src_rowcol == NULL)
220 for (i = 0, lz = rc->rc_src_rowcol;
221 i < rc->rc_src_rowcol_len; ++i, ++lz) {
222 m = be32toh(rcx->rcx_src_rowcol[i].begin),
223 n = be32toh(rcx->rcx_src_rowcol[i].end);
224 ret = set_linear_zone(lz, m, n);
226 free(rc->rc_src_rowcol);
227 rc->rc_src_rowcol = NULL;
235 rowcol_uninit(struct _citrus_mapper_std *ms)
237 struct _citrus_mapper_std_rowcol *rc;
240 free(rc->rc_src_rowcol);
244 rowcol_init(struct _citrus_mapper_std *ms)
246 struct _citrus_mapper_std_linear_zone *lz;
247 struct _citrus_mapper_std_rowcol *rc;
248 const struct _citrus_mapper_std_rowcol_ext_ilseq_info_x *eix;
254 ms->ms_convert = &rowcol_convert;
255 ms->ms_uninit = &rowcol_uninit;
258 /* get table region */
259 ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TABLE,
260 &rc->rc_table, NULL);
267 /* get table information */
268 ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_INFO, &r, NULL);
274 switch (_region_size(&r)) {
275 case _CITRUS_MAPPER_STD_ROWCOL_INFO_COMPAT_SIZE:
276 ret = rowcol_parse_variable_compat(rc, &r);
278 case _CITRUS_MAPPER_STD_ROWCOL_INFO_SIZE:
279 ret = rowcol_parse_variable(rc, &r);
287 switch (rc->rc_src_rowcol_bits) {
288 case 8: case 16: case 32:
289 if (rc->rc_src_rowcol_len <= 32 / rc->rc_src_rowcol_bits)
296 /* ilseq extension */
297 rc->rc_oob_mode = _CITRUS_MAPPER_STD_OOB_NONIDENTICAL;
298 rc->rc_dst_ilseq = rc->rc_dst_invalid;
299 ret = _db_lookup_by_s(ms->ms_db,
300 _CITRUS_MAPPER_STD_SYM_ROWCOL_EXT_ILSEQ, &r, NULL);
301 if (ret && ret != ENOENT)
303 if (_region_size(&r) < sizeof(*eix))
306 eix = _region_head(&r);
307 rc->rc_oob_mode = be32toh(eix->eix_oob_mode);
308 rc->rc_dst_ilseq = be32toh(eix->eix_dst_ilseq);
311 /* calcurate expected table size */
312 i = rc->rc_src_rowcol_len;
313 lz = &rc->rc_src_rowcol[--i];
314 table_size = lz->width;
316 lz = &rc->rc_src_rowcol[--i];
317 table_size *= lz->width;
319 table_size *= rc->rc_dst_unit_bits/8;
321 if (table_size > UINT32_MAX ||
322 _region_size(&rc->rc_table) < table_size)
328 typedef int (*initfunc_t)(struct _citrus_mapper_std *);
329 static const struct {
333 { &rowcol_init, _CITRUS_MAPPER_STD_TYPE_ROWCOL },
335 #define NUM_OF_TYPES ((int)(sizeof(types)/sizeof(types[0])))
339 _citrus_mapper_std_mapper_init(struct _citrus_mapper_area *__restrict ma __unused,
340 struct _citrus_mapper * __restrict cm, const char * __restrict curdir,
341 const void * __restrict var, size_t lenvar,
342 struct _citrus_mapper_traits * __restrict mt, size_t lenmt)
344 struct _citrus_mapper_std *ms;
350 if (lenmt < sizeof(*mt)) {
354 mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */
355 mt->mt_state_size = 0; /* stateless */
357 /* alloc mapper std structure */
358 ms = malloc(sizeof(*ms));
364 /* open mapper file */
365 snprintf(path, sizeof(path), "%s/%.*s", curdir, (int)lenvar,
367 ret = _map_file(&ms->ms_file, path);
371 ret = _db_open(&ms->ms_db, &ms->ms_file, _CITRUS_MAPPER_STD_MAGIC,
372 &_db_hash_std, NULL);
376 /* get mapper type */
377 ret = _db_lookupstr_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TYPE,
384 for (id = 0; id < NUM_OF_TYPES; id++)
385 if (_bcs_strcasecmp(type, types[id].t_name) == 0)
388 if (id == NUM_OF_TYPES)
391 /* init the per-type structure */
392 ret = (*types[id].t_init)(ms);
401 _db_close(ms->ms_db);
403 _unmap_file(&ms->ms_file);
412 _citrus_mapper_std_mapper_uninit(struct _citrus_mapper *cm)
414 struct _citrus_mapper_std *ms;
418 (*ms->ms_uninit)(ms);
419 _db_close(ms->ms_db);
420 _unmap_file(&ms->ms_file);
426 _citrus_mapper_std_mapper_init_state(void)
433 _citrus_mapper_std_mapper_convert(struct _citrus_mapper * __restrict cm,
434 _index_t * __restrict dst, _index_t src, void * __restrict ps)
436 struct _citrus_mapper_std *ms;
439 return ((*ms->ms_convert)(ms, dst, src, ps));