1 /* $NetBSD: citrus_mapper_std.c,v 1.11 2018/06/11 18:03:38 kamil Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c)2003, 2006 Citrus Project,
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #include <sys/endian.h>
33 #include <sys/queue.h>
34
35 #include <assert.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "citrus_namespace.h"
44 #include "citrus_types.h"
45 #include "citrus_bcs.h"
46 #include "citrus_region.h"
47 #include "citrus_mmap.h"
48 #include "citrus_module.h"
49 #include "citrus_hash.h"
50 #include "citrus_mapper.h"
51 #include "citrus_db.h"
52 #include "citrus_db_hash.h"
53
54 #include "citrus_mapper_std.h"
55 #include "citrus_mapper_std_file.h"
56
57 /* ---------------------------------------------------------------------- */
58
59 _CITRUS_MAPPER_DECLS(mapper_std);
60 _CITRUS_MAPPER_DEF_OPS(mapper_std);
61
62
63 /* ---------------------------------------------------------------------- */
64
65 int
_citrus_mapper_std_mapper_getops(struct _citrus_mapper_ops * ops)66 _citrus_mapper_std_mapper_getops(struct _citrus_mapper_ops *ops)
67 {
68
69 memcpy(ops, &_citrus_mapper_std_mapper_ops,
70 sizeof(_citrus_mapper_std_mapper_ops));
71
72 return (0);
73 }
74
75 /* ---------------------------------------------------------------------- */
76
77 static int
78 /*ARGSUSED*/
rowcol_convert(struct _citrus_mapper_std * __restrict ms,_index_t * __restrict dst,_index_t src,void * __restrict ps __unused)79 rowcol_convert(struct _citrus_mapper_std * __restrict ms,
80 _index_t * __restrict dst, _index_t src, void * __restrict ps __unused)
81 {
82 struct _citrus_mapper_std_linear_zone *lz;
83 struct _citrus_mapper_std_rowcol *rc;
84 _index_t idx = 0, n;
85 size_t i;
86 uint32_t conv;
87
88 /* ps may be unused */
89 rc = &ms->ms_rowcol;
90
91 for (i = rc->rc_src_rowcol_len * rc->rc_src_rowcol_bits,
92 lz = &rc->rc_src_rowcol[0]; i > 0; ++lz) {
93 i -= rc->rc_src_rowcol_bits;
94 n = (src >> i) & rc->rc_src_rowcol_mask;
95 if (n < lz->begin || n > lz->end) {
96 switch (rc->rc_oob_mode) {
97 case _CITRUS_MAPPER_STD_OOB_NONIDENTICAL:
98 *dst = rc->rc_dst_invalid;
99 return (_MAPPER_CONVERT_NONIDENTICAL);
100 case _CITRUS_MAPPER_STD_OOB_ILSEQ:
101 return (_MAPPER_CONVERT_ILSEQ);
102 default:
103 return (_MAPPER_CONVERT_FATAL);
104 }
105 }
106 idx = idx * lz->width + n - lz->begin;
107 }
108 switch (rc->rc_dst_unit_bits) {
109 case 8:
110 conv = _region_peek8(&rc->rc_table, idx);
111 break;
112 case 16:
113 conv = be16toh(_region_peek16(&rc->rc_table, idx*2));
114 break;
115 case 32:
116 conv = be32toh(_region_peek32(&rc->rc_table, idx*4));
117 break;
118 default:
119 return (_MAPPER_CONVERT_FATAL);
120 }
121
122 if (conv == rc->rc_dst_invalid) {
123 *dst = rc->rc_dst_invalid;
124 return (_MAPPER_CONVERT_NONIDENTICAL);
125 }
126 if (conv == rc->rc_dst_ilseq)
127 return (_MAPPER_CONVERT_ILSEQ);
128
129 *dst = conv;
130
131 return (_MAPPER_CONVERT_SUCCESS);
132 }
133
134 static __inline int
set_linear_zone(struct _citrus_mapper_std_linear_zone * lz,uint32_t begin,uint32_t end)135 set_linear_zone(struct _citrus_mapper_std_linear_zone *lz,
136 uint32_t begin, uint32_t end)
137 {
138
139 if (begin > end)
140 return (EFTYPE);
141
142 lz->begin = begin;
143 lz->end = end;
144 lz->width= end - begin + 1;
145
146 return (0);
147 }
148
149 static __inline int
rowcol_parse_variable_compat(struct _citrus_mapper_std_rowcol * rc,struct _region * r)150 rowcol_parse_variable_compat(struct _citrus_mapper_std_rowcol *rc,
151 struct _region *r)
152 {
153 const struct _citrus_mapper_std_rowcol_info_compat_x *rcx;
154 struct _citrus_mapper_std_linear_zone *lz;
155 uint32_t m, n;
156 int ret;
157
158 rcx = _region_head(r);
159
160 rc->rc_dst_invalid = be32toh(rcx->rcx_dst_invalid);
161 rc->rc_dst_unit_bits = be32toh(rcx->rcx_dst_unit_bits);
162 m = be32toh(rcx->rcx_src_col_bits);
163 n = 1U << (m - 1);
164 n |= n - 1;
165 rc->rc_src_rowcol_bits = m;
166 rc->rc_src_rowcol_mask = n;
167
168 rc->rc_src_rowcol = malloc(2 *
169 sizeof(*rc->rc_src_rowcol));
170 if (rc->rc_src_rowcol == NULL)
171 return (ENOMEM);
172 lz = rc->rc_src_rowcol;
173 rc->rc_src_rowcol_len = 1;
174 m = be32toh(rcx->rcx_src_row_begin);
175 n = be32toh(rcx->rcx_src_row_end);
176 if (m + n > 0) {
177 ret = set_linear_zone(lz, m, n);
178 if (ret != 0) {
179 free(rc->rc_src_rowcol);
180 rc->rc_src_rowcol = NULL;
181 return (ret);
182 }
183 ++rc->rc_src_rowcol_len, ++lz;
184 }
185 m = be32toh(rcx->rcx_src_col_begin);
186 n = be32toh(rcx->rcx_src_col_end);
187
188 return (set_linear_zone(lz, m, n));
189 }
190
191 static __inline int
rowcol_parse_variable(struct _citrus_mapper_std_rowcol * rc,struct _region * r)192 rowcol_parse_variable(struct _citrus_mapper_std_rowcol *rc,
193 struct _region *r)
194 {
195 const struct _citrus_mapper_std_rowcol_info_x *rcx;
196 struct _citrus_mapper_std_linear_zone *lz;
197 size_t i;
198 uint32_t m, n;
199 int ret;
200
201 rcx = _region_head(r);
202
203 rc->rc_dst_invalid = be32toh(rcx->rcx_dst_invalid);
204 rc->rc_dst_unit_bits = be32toh(rcx->rcx_dst_unit_bits);
205
206 m = be32toh(rcx->rcx_src_rowcol_bits);
207 n = 1 << (m - 1);
208 n |= n - 1;
209 rc->rc_src_rowcol_bits = m;
210 rc->rc_src_rowcol_mask = n;
211
212 rc->rc_src_rowcol_len = be32toh(rcx->rcx_src_rowcol_len);
213 if (rc->rc_src_rowcol_len > _CITRUS_MAPPER_STD_ROWCOL_MAX)
214 return (EFTYPE);
215 rc->rc_src_rowcol = malloc(rc->rc_src_rowcol_len *
216 sizeof(*rc->rc_src_rowcol));
217 if (rc->rc_src_rowcol == NULL)
218 return (ENOMEM);
219 for (i = 0, lz = rc->rc_src_rowcol;
220 i < rc->rc_src_rowcol_len; ++i, ++lz) {
221 m = be32toh(rcx->rcx_src_rowcol[i].begin),
222 n = be32toh(rcx->rcx_src_rowcol[i].end);
223 ret = set_linear_zone(lz, m, n);
224 if (ret != 0) {
225 free(rc->rc_src_rowcol);
226 rc->rc_src_rowcol = NULL;
227 return (ret);
228 }
229 }
230 return (0);
231 }
232
233 static void
rowcol_uninit(struct _citrus_mapper_std * ms)234 rowcol_uninit(struct _citrus_mapper_std *ms)
235 {
236 struct _citrus_mapper_std_rowcol *rc;
237
238 rc = &ms->ms_rowcol;
239 free(rc->rc_src_rowcol);
240 }
241
242 static int
rowcol_init(struct _citrus_mapper_std * ms)243 rowcol_init(struct _citrus_mapper_std *ms)
244 {
245 struct _citrus_mapper_std_linear_zone *lz;
246 struct _citrus_mapper_std_rowcol *rc;
247 const struct _citrus_mapper_std_rowcol_ext_ilseq_info_x *eix;
248 struct _region r;
249 uint64_t table_size;
250 size_t i;
251 int ret;
252
253 ms->ms_convert = &rowcol_convert;
254 ms->ms_uninit = &rowcol_uninit;
255 rc = &ms->ms_rowcol;
256
257 /* get table region */
258 ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TABLE,
259 &rc->rc_table, NULL);
260 if (ret) {
261 if (ret == ENOENT)
262 ret = EFTYPE;
263 return (ret);
264 }
265
266 /* get table information */
267 ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_INFO, &r, NULL);
268 if (ret) {
269 if (ret == ENOENT)
270 ret = EFTYPE;
271 return (ret);
272 }
273 switch (_region_size(&r)) {
274 case _CITRUS_MAPPER_STD_ROWCOL_INFO_COMPAT_SIZE:
275 ret = rowcol_parse_variable_compat(rc, &r);
276 break;
277 case _CITRUS_MAPPER_STD_ROWCOL_INFO_SIZE:
278 ret = rowcol_parse_variable(rc, &r);
279 break;
280 default:
281 return (EFTYPE);
282 }
283 if (ret != 0)
284 return (ret);
285 /* sanity check */
286 switch (rc->rc_src_rowcol_bits) {
287 case 8: case 16: case 32:
288 if (rc->rc_src_rowcol_len <= 32 / rc->rc_src_rowcol_bits)
289 break;
290 /*FALLTHROUGH*/
291 default:
292 return (EFTYPE);
293 }
294
295 /* ilseq extension */
296 rc->rc_oob_mode = _CITRUS_MAPPER_STD_OOB_NONIDENTICAL;
297 rc->rc_dst_ilseq = rc->rc_dst_invalid;
298 ret = _db_lookup_by_s(ms->ms_db,
299 _CITRUS_MAPPER_STD_SYM_ROWCOL_EXT_ILSEQ, &r, NULL);
300 if (ret && ret != ENOENT)
301 return (ret);
302 if (_region_size(&r) < sizeof(*eix))
303 return (EFTYPE);
304 if (ret == 0) {
305 eix = _region_head(&r);
306 rc->rc_oob_mode = be32toh(eix->eix_oob_mode);
307 rc->rc_dst_ilseq = be32toh(eix->eix_dst_ilseq);
308 }
309
310 /* calcurate expected table size */
311 i = rc->rc_src_rowcol_len;
312 lz = &rc->rc_src_rowcol[--i];
313 table_size = lz->width;
314 while (i > 0) {
315 lz = &rc->rc_src_rowcol[--i];
316 table_size *= lz->width;
317 }
318 table_size *= rc->rc_dst_unit_bits/8;
319
320 if (table_size > UINT32_MAX ||
321 _region_size(&rc->rc_table) < table_size)
322 return (EFTYPE);
323
324 return (0);
325 }
326
327 typedef int (*initfunc_t)(struct _citrus_mapper_std *);
328 static const struct {
329 initfunc_t t_init;
330 const char *t_name;
331 } types[] = {
332 { &rowcol_init, _CITRUS_MAPPER_STD_TYPE_ROWCOL },
333 };
334 #define NUM_OF_TYPES ((int)(sizeof(types)/sizeof(types[0])))
335
336 static int
337 /*ARGSUSED*/
_citrus_mapper_std_mapper_init(struct _citrus_mapper_area * __restrict ma __unused,struct _citrus_mapper * __restrict cm,const char * __restrict curdir,const void * __restrict var,size_t lenvar,struct _citrus_mapper_traits * __restrict mt,size_t lenmt)338 _citrus_mapper_std_mapper_init(struct _citrus_mapper_area *__restrict ma __unused,
339 struct _citrus_mapper * __restrict cm, const char * __restrict curdir,
340 const void * __restrict var, size_t lenvar,
341 struct _citrus_mapper_traits * __restrict mt, size_t lenmt)
342 {
343 struct _citrus_mapper_std *ms;
344 char path[PATH_MAX];
345 const char *type;
346 int id, ret;
347
348 /* set traits */
349 if (lenmt < sizeof(*mt)) {
350 ret = EINVAL;
351 goto err0;
352 }
353 mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */
354 mt->mt_state_size = 0; /* stateless */
355
356 /* alloc mapper std structure */
357 ms = malloc(sizeof(*ms));
358 if (ms == NULL) {
359 ret = errno;
360 goto err0;
361 }
362
363 /* open mapper file */
364 snprintf(path, sizeof(path), "%s/%.*s", curdir, (int)lenvar,
365 (const char *)var);
366 ret = _map_file(&ms->ms_file, path);
367 if (ret)
368 goto err1;
369
370 ret = _db_open(&ms->ms_db, &ms->ms_file, _CITRUS_MAPPER_STD_MAGIC,
371 &_db_hash_std, NULL);
372 if (ret)
373 goto err2;
374
375 /* get mapper type */
376 ret = _db_lookupstr_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TYPE,
377 &type, NULL);
378 if (ret) {
379 if (ret == ENOENT)
380 ret = EFTYPE;
381 goto err3;
382 }
383 for (id = 0; id < NUM_OF_TYPES; id++)
384 if (_bcs_strcasecmp(type, types[id].t_name) == 0)
385 break;
386
387 if (id == NUM_OF_TYPES)
388 goto err3;
389
390 /* init the per-type structure */
391 ret = (*types[id].t_init)(ms);
392 if (ret)
393 goto err3;
394
395 cm->cm_closure = ms;
396
397 return (0);
398
399 err3:
400 _db_close(ms->ms_db);
401 err2:
402 _unmap_file(&ms->ms_file);
403 err1:
404 free(ms);
405 err0:
406 return (ret);
407 }
408
409 static void
410 /*ARGSUSED*/
_citrus_mapper_std_mapper_uninit(struct _citrus_mapper * cm)411 _citrus_mapper_std_mapper_uninit(struct _citrus_mapper *cm)
412 {
413 struct _citrus_mapper_std *ms;
414
415 ms = cm->cm_closure;
416 if (ms->ms_uninit)
417 (*ms->ms_uninit)(ms);
418 _db_close(ms->ms_db);
419 _unmap_file(&ms->ms_file);
420 free(ms);
421 }
422
423 static void
424 /*ARGSUSED*/
_citrus_mapper_std_mapper_init_state(void)425 _citrus_mapper_std_mapper_init_state(void)
426 {
427
428 }
429
430 static int
431 /*ARGSUSED*/
_citrus_mapper_std_mapper_convert(struct _citrus_mapper * __restrict cm,_index_t * __restrict dst,_index_t src,void * __restrict ps)432 _citrus_mapper_std_mapper_convert(struct _citrus_mapper * __restrict cm,
433 _index_t * __restrict dst, _index_t src, void * __restrict ps)
434 {
435 struct _citrus_mapper_std *ms;
436
437 ms = cm->cm_closure;
438 return ((*ms->ms_convert)(ms, dst, src, ps));
439 }
440