citrus_mapper_std.c revision 1.2 1 /* $NetBSD: citrus_mapper_std.c,v 1.2 2003/06/27 17:53:31 tshiozak Exp $ */
2
3 /*-
4 * Copyright (c)2003 Citrus Project,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_mapper_std.c,v 1.2 2003/06/27 17:53:31 tshiozak Exp $");
32 #endif /* LIBC_SCCS and not lint */
33
34 #include <assert.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdint.h>
40 #include <string.h>
41 #include <sys/endian.h>
42 #include <sys/queue.h>
43
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"
54
55 #include "citrus_mapper_std.h"
56 #include "citrus_mapper_std_file.h"
57
58 /* ---------------------------------------------------------------------- */
59
60 _CITRUS_MAPPER_DECLS(mapper_std);
61 _CITRUS_MAPPER_DEF_OPS(mapper_std);
62
63
64 /* ---------------------------------------------------------------------- */
65
66 int
67 _citrus_mapper_std_mapper_getops(struct _citrus_mapper_ops *ops, size_t lenops,
68 u_int32_t expected_version)
69 {
70 if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
71 return (EINVAL);
72
73 memcpy(ops, &_citrus_mapper_std_mapper_ops,
74 sizeof(_citrus_mapper_std_mapper_ops));
75
76 return (0);
77 }
78
79 /* ---------------------------------------------------------------------- */
80
81 static int
82 /*ARGSUSED*/
83 rowcol_convert(struct _citrus_mapper_std * __restrict ms,
84 _index_t * __restrict dst, _index_t src,
85 void * __restrict ps)
86 {
87 struct _citrus_mapper_std_rowcol *rc = &ms->ms_rowcol;
88 _index_t row, col, idx;
89 u_int32_t conv;
90
91 if (rc->rc_src_col_bits == 32) {
92 row = 0;
93 col = src;
94 } else {
95 row = src >> rc->rc_src_col_bits;
96 col = src & ((1U<<rc->rc_src_col_bits)-1);
97 }
98 if (row < rc->rc_src_row_begin || row > rc->rc_src_row_end ||
99 col < rc->rc_src_col_begin || col > rc->rc_src_col_end) {
100 *dst = rc->rc_dst_invalid;
101 return _MAPPER_CONVERT_INVAL;
102 }
103
104 idx =
105 (row - rc->rc_src_row_begin)*rc->rc_src_col_width +
106 (col - rc->rc_src_col_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 }
119
120 if (conv == rc->rc_dst_invalid) {
121 *dst = rc->rc_dst_invalid;
122 return _MAPPER_CONVERT_INVAL;
123 }
124
125 *dst = conv;
126
127 return _MAPPER_CONVERT_SUCCESS;
128 }
129
130
131 static int
132 rowcol_init(struct _citrus_mapper_std *ms)
133 {
134 int ret;
135 struct _citrus_mapper_std_rowcol *rc = &ms->ms_rowcol;
136 const struct _citrus_mapper_std_rowcol_info_x *rcx;
137 struct _region r;
138 u_int64_t table_size;
139
140 ms->ms_convert = &rowcol_convert;
141 ms->ms_uninit = NULL;
142
143 /* get table region */
144 ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TABLE,
145 &rc->rc_table, NULL);
146 if (ret) {
147 if (ret==ENOENT)
148 ret = EFTYPE;
149 return ret;
150 }
151
152 /* get table information */
153 ret = _db_lookup_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_INFO, &r, NULL);
154 if (ret) {
155 if (ret==ENOENT)
156 ret =EFTYPE;
157 return ret;
158 }
159 if (_region_size(&r) < sizeof(*rcx))
160 return EFTYPE;
161 rcx = _region_head(&r);
162
163 /* convert */
164 #define CONV_ROWCOL(rc, rcx, elem) \
165 do { \
166 (rc)->rc_##elem = be32toh((rcx)->rcx_##elem); \
167 } while (/*CONSTCOND*/0)
168 CONV_ROWCOL(rc, rcx, src_col_bits);
169 CONV_ROWCOL(rc, rcx, dst_invalid);
170 CONV_ROWCOL(rc, rcx, src_row_begin);
171 CONV_ROWCOL(rc, rcx, src_row_end);
172 CONV_ROWCOL(rc, rcx, src_col_begin);
173 CONV_ROWCOL(rc, rcx, src_col_end);
174 CONV_ROWCOL(rc, rcx, dst_unit_bits);
175
176 rc->rc_src_col_width = rc->rc_src_col_end - rc->rc_src_col_begin +1;
177
178 /* validation checks */
179 if (rc->rc_src_col_end < rc->rc_src_col_begin ||
180 rc->rc_src_row_end < rc->rc_src_row_begin ||
181 !(rc->rc_dst_unit_bits==8 || rc->rc_dst_unit_bits==16 ||
182 rc->rc_dst_unit_bits==32) ||
183 !(rc->rc_src_col_bits >= 0 && rc->rc_src_col_bits <= 32))
184 return EFTYPE;
185
186 /* calcurate expected table size */
187 table_size = rc->rc_src_row_end - rc->rc_src_row_begin + 1;
188 table_size *= rc->rc_src_col_width;
189 table_size *= rc->rc_dst_unit_bits/8;
190
191 if (table_size > UINT32_MAX ||
192 _region_size(&rc->rc_table) < table_size)
193 return EFTYPE;
194
195 return 0;
196 }
197
198 typedef int (*initfunc_t)(struct _citrus_mapper_std *);
199 static struct {
200 const char *t_name;
201 initfunc_t t_init;
202 } types[] = {
203 { _CITRUS_MAPPER_STD_TYPE_ROWCOL, &rowcol_init },
204 };
205 #define NUM_OF_TYPES ((int)(sizeof(types)/sizeof(types[0])))
206
207 static int
208 /*ARGSUSED*/
209 _citrus_mapper_std_mapper_init(struct _citrus_mapper_area *__restrict ma,
210 struct _citrus_mapper * __restrict cm,
211 const char * __restrict curdir,
212 const void * __restrict var, size_t lenvar,
213 struct _citrus_mapper_traits * __restrict mt,
214 size_t lenmt)
215 {
216 char path[PATH_MAX];
217 const char *type;
218 int ret, id;
219 struct _citrus_mapper_std *ms;
220
221 /* set traits */
222 if (lenmt<sizeof(*mt)) {
223 ret = EINVAL;
224 goto err0;
225 }
226 mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */
227 mt->mt_state_size = 0; /* stateless */
228
229 /* alloc mapper std structure */
230 ms = malloc(sizeof(*ms));
231 if (ms==NULL) {
232 ret = errno;
233 goto err0;
234 }
235
236 /* open mapper file */
237 snprintf(path, sizeof(path),
238 "%s/%.*s", curdir, (int)lenvar, (const char *)var);
239 ret = _map_file(&ms->ms_file, path);
240 if (ret)
241 goto err1;
242
243 ret = _db_open(&ms->ms_db, &ms->ms_file, _CITRUS_MAPPER_STD_MAGIC,
244 &_db_hash_std, NULL);
245 if (ret)
246 goto err2;
247
248 /* get mapper type */
249 ret = _db_lookupstr_by_s(ms->ms_db, _CITRUS_MAPPER_STD_SYM_TYPE,
250 &type, NULL);
251 if (ret) {
252 if (ret==ENOENT)
253 ret = EFTYPE;
254 goto err3;
255 }
256 for (id=0; id<NUM_OF_TYPES; id++)
257 if (_bcs_strcasecmp(type, types[id].t_name) == 0)
258 break;
259
260 if (id == NUM_OF_TYPES)
261 goto err3;
262
263 /* init the per-type structure */
264 ret = (*types[id].t_init)(ms);
265 if (ret)
266 goto err3;
267
268 cm->cm_closure = ms;
269
270 return 0;
271
272 err3:
273 _db_close(ms->ms_db);
274 err2:
275 _unmap_file(&ms->ms_file);
276 err1:
277 free(ms);
278 err0:
279 return ret;
280 }
281
282 static void
283 /*ARGSUSED*/
284 _citrus_mapper_std_mapper_uninit(struct _citrus_mapper *cm)
285 {
286 struct _citrus_mapper_std *ms;
287
288 _DIAGASSERT(cm!=NULL & cm->cm_closure!=NULL);
289
290 ms = cm->cm_closure;
291 if (ms->ms_uninit)
292 (*ms->ms_uninit)(ms);
293 _db_close(ms->ms_db);
294 _unmap_file(&ms->ms_file);
295 free(ms);
296 }
297
298 static void
299 /*ARGSUSED*/
300 _citrus_mapper_std_mapper_init_state(struct _citrus_mapper * __restrict cm,
301 void * __restrict ps)
302 {
303 }
304
305 static int
306 /*ARGSUSED*/
307 _citrus_mapper_std_mapper_convert(struct _citrus_mapper * __restrict cm,
308 _index_t * __restrict dst, _index_t src,
309 void * __restrict ps)
310 {
311 struct _citrus_mapper_std *ms;
312
313 _DIAGASSERT(cm!=NULL && cm->cm_closure!=NULL);
314
315 ms = cm->cm_closure;
316
317 _DIAGASSERT(ms->ms_convert != NULL);
318
319 return (*ms->ms_convert)(ms, dst, src, ps);
320 }
321