citrus_mapper_std.c revision 1.1 1 1.1 tshiozak /* $NetBSD: citrus_mapper_std.c,v 1.1 2003/06/25 09:51:47 tshiozak 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.1 tshiozak __RCSID("$NetBSD: citrus_mapper_std.c,v 1.1 2003/06/25 09:51:47 tshiozak 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.1 tshiozak #include <sys/endian.h>
42 1.1 tshiozak
43 1.1 tshiozak #include "citrus_namespace.h"
44 1.1 tshiozak #include "citrus_types.h"
45 1.1 tshiozak #include "citrus_bcs.h"
46 1.1 tshiozak #include "citrus_region.h"
47 1.1 tshiozak #include "citrus_mmap.h"
48 1.1 tshiozak #include "citrus_module.h"
49 1.1 tshiozak #include "citrus_hash.h"
50 1.1 tshiozak #include "citrus_mapper.h"
51 1.1 tshiozak #include "citrus_db.h"
52 1.1 tshiozak #include "citrus_db_hash.h"
53 1.1 tshiozak
54 1.1 tshiozak #include "citrus_mapper_std.h"
55 1.1 tshiozak #include "citrus_mapper_std_file.h"
56 1.1 tshiozak
57 1.1 tshiozak /* ---------------------------------------------------------------------- */
58 1.1 tshiozak
59 1.1 tshiozak _CITRUS_MAPPER_DECLS(mapper_std);
60 1.1 tshiozak _CITRUS_MAPPER_DEF_OPS(mapper_std);
61 1.1 tshiozak
62 1.1 tshiozak
63 1.1 tshiozak /* ---------------------------------------------------------------------- */
64 1.1 tshiozak
65 1.1 tshiozak int
66 1.1 tshiozak _citrus_mapper_std_mapper_getops(struct _citrus_mapper_ops *ops, size_t lenops,
67 1.1 tshiozak u_int32_t expected_version)
68 1.1 tshiozak {
69 1.1 tshiozak if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
70 1.1 tshiozak return (EINVAL);
71 1.1 tshiozak
72 1.1 tshiozak memcpy(ops, &_citrus_mapper_std_mapper_ops,
73 1.1 tshiozak sizeof(_citrus_mapper_std_mapper_ops));
74 1.1 tshiozak
75 1.1 tshiozak return (0);
76 1.1 tshiozak }
77 1.1 tshiozak
78 1.1 tshiozak /* ---------------------------------------------------------------------- */
79 1.1 tshiozak
80 1.1 tshiozak static int
81 1.1 tshiozak /*ARGSUSED*/
82 1.1 tshiozak rowcol_convert(struct _citrus_mapper_std * __restrict ms,
83 1.1 tshiozak _index_t * __restrict dst, _index_t src,
84 1.1 tshiozak void * __restrict ps)
85 1.1 tshiozak {
86 1.1 tshiozak struct _citrus_mapper_std_rowcol *rc = &ms->ms_rowcol;
87 1.1 tshiozak _index_t row, col, idx;
88 1.1 tshiozak u_int32_t conv;
89 1.1 tshiozak
90 1.1 tshiozak if (rc->rc_src_col_bits == 32) {
91 1.1 tshiozak row = 0;
92 1.1 tshiozak col = src;
93 1.1 tshiozak } else {
94 1.1 tshiozak row = src >> rc->rc_src_col_bits;
95 1.1 tshiozak col = src & ((1U<<rc->rc_src_col_bits)-1);
96 1.1 tshiozak }
97 1.1 tshiozak if (row < rc->rc_src_row_begin || row > rc->rc_src_row_end ||
98 1.1 tshiozak col < rc->rc_src_col_begin || col > rc->rc_src_col_end) {
99 1.1 tshiozak *dst = rc->rc_dst_invalid;
100 1.1 tshiozak return _MAPPER_CONVERT_INVAL;
101 1.1 tshiozak }
102 1.1 tshiozak
103 1.1 tshiozak idx =
104 1.1 tshiozak (row - rc->rc_src_row_begin)*rc->rc_src_col_width +
105 1.1 tshiozak (col - rc->rc_src_col_begin);
106 1.1 tshiozak
107 1.1 tshiozak switch (rc->rc_dst_unit_bits) {
108 1.1 tshiozak case 8:
109 1.1 tshiozak conv = _region_peek8(&rc->rc_table, idx);
110 1.1 tshiozak break;
111 1.1 tshiozak case 16:
112 1.1 tshiozak conv = be16toh(_region_peek16(&rc->rc_table, idx*2));
113 1.1 tshiozak break;
114 1.1 tshiozak case 32:
115 1.1 tshiozak conv = be32toh(_region_peek32(&rc->rc_table, idx*4));
116 1.1 tshiozak break;
117 1.1 tshiozak }
118 1.1 tshiozak
119 1.1 tshiozak if (conv == rc->rc_dst_invalid) {
120 1.1 tshiozak *dst = rc->rc_dst_invalid;
121 1.1 tshiozak return _MAPPER_CONVERT_INVAL;
122 1.1 tshiozak }
123 1.1 tshiozak
124 1.1 tshiozak *dst = conv;
125 1.1 tshiozak
126 1.1 tshiozak return _MAPPER_CONVERT_SUCCESS;
127 1.1 tshiozak }
128 1.1 tshiozak
129 1.1 tshiozak
130 1.1 tshiozak static int
131 1.1 tshiozak rowcol_init(struct _citrus_mapper_std *ms)
132 1.1 tshiozak {
133 1.1 tshiozak int ret;
134 1.1 tshiozak struct _citrus_mapper_std_rowcol *rc = &ms->ms_rowcol;
135 1.1 tshiozak const struct _citrus_mapper_std_rowcol_info_x *rcx;
136 1.1 tshiozak struct _region r;
137 1.1 tshiozak u_int64_t table_size;
138 1.1 tshiozak
139 1.1 tshiozak ms->ms_convert = &rowcol_convert;
140 1.1 tshiozak ms->ms_uninit = NULL;
141 1.1 tshiozak
142 1.1 tshiozak /* get table region */
143 1.1 tshiozak ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TABLE,
144 1.1 tshiozak &rc->rc_table, NULL);
145 1.1 tshiozak if (ret) {
146 1.1 tshiozak if (ret==ENOENT)
147 1.1 tshiozak ret = EFTYPE;
148 1.1 tshiozak return ret;
149 1.1 tshiozak }
150 1.1 tshiozak
151 1.1 tshiozak /* get table information */
152 1.1 tshiozak ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_INFO, &r, NULL);
153 1.1 tshiozak if (ret) {
154 1.1 tshiozak if (ret==ENOENT)
155 1.1 tshiozak ret =EFTYPE;
156 1.1 tshiozak return ret;
157 1.1 tshiozak }
158 1.1 tshiozak if (_region_size(&r) < sizeof(*rcx))
159 1.1 tshiozak return EFTYPE;
160 1.1 tshiozak rcx = _region_head(&r);
161 1.1 tshiozak
162 1.1 tshiozak /* convert */
163 1.1 tshiozak #define CONV_ROWCOL(rc, rcx, elem) \
164 1.1 tshiozak do { \
165 1.1 tshiozak (rc)->rc_##elem = be32toh((rcx)->rcx_##elem); \
166 1.1 tshiozak } while (/*CONSTCOND*/0)
167 1.1 tshiozak CONV_ROWCOL(rc, rcx, src_col_bits);
168 1.1 tshiozak CONV_ROWCOL(rc, rcx, dst_invalid);
169 1.1 tshiozak CONV_ROWCOL(rc, rcx, src_row_begin);
170 1.1 tshiozak CONV_ROWCOL(rc, rcx, src_row_end);
171 1.1 tshiozak CONV_ROWCOL(rc, rcx, src_col_begin);
172 1.1 tshiozak CONV_ROWCOL(rc, rcx, src_col_end);
173 1.1 tshiozak CONV_ROWCOL(rc, rcx, dst_unit_bits);
174 1.1 tshiozak
175 1.1 tshiozak rc->rc_src_col_width = rc->rc_src_col_end - rc->rc_src_col_begin +1;
176 1.1 tshiozak
177 1.1 tshiozak /* validation checks */
178 1.1 tshiozak if (rc->rc_src_col_end < rc->rc_src_col_begin ||
179 1.1 tshiozak rc->rc_src_row_end < rc->rc_src_row_begin ||
180 1.1 tshiozak !(rc->rc_dst_unit_bits==8 || rc->rc_dst_unit_bits==16 ||
181 1.1 tshiozak rc->rc_dst_unit_bits==32) ||
182 1.1 tshiozak !(rc->rc_src_col_bits >= 0 && rc->rc_src_col_bits <= 32))
183 1.1 tshiozak return EFTYPE;
184 1.1 tshiozak
185 1.1 tshiozak /* calcurate expected table size */
186 1.1 tshiozak table_size = rc->rc_src_row_end - rc->rc_src_row_begin + 1;
187 1.1 tshiozak table_size *= rc->rc_src_col_width;
188 1.1 tshiozak table_size *= rc->rc_dst_unit_bits/8;
189 1.1 tshiozak
190 1.1 tshiozak if (table_size > UINT32_MAX ||
191 1.1 tshiozak _region_size(&rc->rc_table) < table_size)
192 1.1 tshiozak return EFTYPE;
193 1.1 tshiozak
194 1.1 tshiozak return 0;
195 1.1 tshiozak }
196 1.1 tshiozak
197 1.1 tshiozak typedef int (*initfunc_t)(struct _citrus_mapper_std *);
198 1.1 tshiozak static struct {
199 1.1 tshiozak const char *t_name;
200 1.1 tshiozak initfunc_t t_init;
201 1.1 tshiozak } types[] = {
202 1.1 tshiozak { _CITRUS_MAPPER_STD_TYPE_ROWCOL, &rowcol_init },
203 1.1 tshiozak };
204 1.1 tshiozak #define NUM_OF_TYPES ((int)(sizeof(types)/sizeof(types[0])))
205 1.1 tshiozak
206 1.1 tshiozak static int
207 1.1 tshiozak /*ARGSUSED*/
208 1.1 tshiozak _citrus_mapper_std_mapper_init(struct _citrus_mapper_area *__restrict ma,
209 1.1 tshiozak struct _citrus_mapper * __restrict cm,
210 1.1 tshiozak const char * __restrict curdir,
211 1.1 tshiozak const void * __restrict var, size_t lenvar,
212 1.1 tshiozak struct _citrus_mapper_traits * __restrict mt,
213 1.1 tshiozak size_t lenmt)
214 1.1 tshiozak {
215 1.1 tshiozak char path[PATH_MAX];
216 1.1 tshiozak const char *type;
217 1.1 tshiozak int ret, id;
218 1.1 tshiozak struct _citrus_mapper_std *ms;
219 1.1 tshiozak
220 1.1 tshiozak /* set traits */
221 1.1 tshiozak if (lenmt<sizeof(*mt)) {
222 1.1 tshiozak ret = EINVAL;
223 1.1 tshiozak goto err0;
224 1.1 tshiozak }
225 1.1 tshiozak mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */
226 1.1 tshiozak mt->mt_state_size = 0; /* stateless */
227 1.1 tshiozak
228 1.1 tshiozak /* alloc mapper std structure */
229 1.1 tshiozak ms = malloc(sizeof(*ms));
230 1.1 tshiozak if (ms==NULL) {
231 1.1 tshiozak ret = errno;
232 1.1 tshiozak goto err0;
233 1.1 tshiozak }
234 1.1 tshiozak
235 1.1 tshiozak /* open mapper file */
236 1.1 tshiozak snprintf(path, sizeof(path),
237 1.1 tshiozak "%s/%.*s", curdir, (int)lenvar, (const char *)var);
238 1.1 tshiozak ret = _map_file(&ms->ms_file, path);
239 1.1 tshiozak if (ret)
240 1.1 tshiozak goto err1;
241 1.1 tshiozak
242 1.1 tshiozak ret = _db_open(&ms->ms_db, &ms->ms_file, _CITRUS_MAPPER_STD_MAGIC,
243 1.1 tshiozak &_db_hash_std, NULL);
244 1.1 tshiozak if (ret)
245 1.1 tshiozak goto err2;
246 1.1 tshiozak
247 1.1 tshiozak /* get mapper type */
248 1.1 tshiozak ret = _db_lookupstr_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TYPE,
249 1.1 tshiozak &type, NULL);
250 1.1 tshiozak if (ret) {
251 1.1 tshiozak if (ret==ENOENT)
252 1.1 tshiozak ret = EFTYPE;
253 1.1 tshiozak goto err3;
254 1.1 tshiozak }
255 1.1 tshiozak for (id=0; id<NUM_OF_TYPES; id++)
256 1.1 tshiozak if (_bcs_strcasecmp(type, types[id].t_name) == 0)
257 1.1 tshiozak break;
258 1.1 tshiozak
259 1.1 tshiozak if (id == NUM_OF_TYPES)
260 1.1 tshiozak goto err3;
261 1.1 tshiozak
262 1.1 tshiozak /* init the per-type structure */
263 1.1 tshiozak ret = (*types[id].t_init)(ms);
264 1.1 tshiozak if (ret)
265 1.1 tshiozak goto err3;
266 1.1 tshiozak
267 1.1 tshiozak cm->cm_closure = ms;
268 1.1 tshiozak
269 1.1 tshiozak return 0;
270 1.1 tshiozak
271 1.1 tshiozak err3:
272 1.1 tshiozak _db_close(ms->ms_db);
273 1.1 tshiozak err2:
274 1.1 tshiozak _unmap_file(&ms->ms_file);
275 1.1 tshiozak err1:
276 1.1 tshiozak free(ms);
277 1.1 tshiozak err0:
278 1.1 tshiozak return ret;
279 1.1 tshiozak }
280 1.1 tshiozak
281 1.1 tshiozak static void
282 1.1 tshiozak /*ARGSUSED*/
283 1.1 tshiozak _citrus_mapper_std_mapper_uninit(struct _citrus_mapper *cm)
284 1.1 tshiozak {
285 1.1 tshiozak struct _citrus_mapper_std *ms;
286 1.1 tshiozak
287 1.1 tshiozak _DIAGASSERT(cm!=NULL & cm->cm_closure!=NULL);
288 1.1 tshiozak
289 1.1 tshiozak ms = cm->cm_closure;
290 1.1 tshiozak if (ms->ms_uninit)
291 1.1 tshiozak (*ms->ms_uninit)(ms);
292 1.1 tshiozak _db_close(ms->ms_db);
293 1.1 tshiozak _unmap_file(&ms->ms_file);
294 1.1 tshiozak free(ms);
295 1.1 tshiozak }
296 1.1 tshiozak
297 1.1 tshiozak static void
298 1.1 tshiozak /*ARGSUSED*/
299 1.1 tshiozak _citrus_mapper_std_mapper_init_state(struct _citrus_mapper * __restrict cm,
300 1.1 tshiozak void * __restrict ps)
301 1.1 tshiozak {
302 1.1 tshiozak }
303 1.1 tshiozak
304 1.1 tshiozak static int
305 1.1 tshiozak /*ARGSUSED*/
306 1.1 tshiozak _citrus_mapper_std_mapper_convert(struct _citrus_mapper * __restrict cm,
307 1.1 tshiozak _index_t * __restrict dst, _index_t src,
308 1.1 tshiozak void * __restrict ps)
309 1.1 tshiozak {
310 1.1 tshiozak struct _citrus_mapper_std *ms;
311 1.1 tshiozak
312 1.1 tshiozak _DIAGASSERT(cm!=NULL && cm->cm_closure!=NULL);
313 1.1 tshiozak
314 1.1 tshiozak ms = cm->cm_closure;
315 1.1 tshiozak
316 1.1 tshiozak _DIAGASSERT(ms->ms_convert != NULL);
317 1.1 tshiozak
318 1.1 tshiozak return (*ms->ms_convert)(ms, dst, src, ps);
319 1.1 tshiozak }
320