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