Sync Citrus iconv support with NetBSD.
[dragonfly.git] / lib / libc / citrus / modules / citrus_mapper_zone.c
1 /* $NetBSD: src/lib/libc/citrus/modules/citrus_mapper_zone.c,v 1.4 2003/07/12 15:39:21 tshiozak Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_mapper_zone.c,v 1.2 2008/04/10 10:21:02 hasso Exp $ */
3
4 /*-
5  * Copyright (c)2003 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "citrus_namespace.h"
39 #include "citrus_types.h"
40 #include "citrus_bcs.h"
41 #include "citrus_module.h"
42 #include "citrus_region.h"
43 #include "citrus_memstream.h"
44 #include "citrus_mmap.h"
45 #include "citrus_hash.h"
46 #include "citrus_mapper.h"
47 #include "citrus_mapper_zone.h"
48
49 /* ---------------------------------------------------------------------- */
50
51 _CITRUS_MAPPER_DECLS(mapper_zone);
52 _CITRUS_MAPPER_DEF_OPS(mapper_zone);
53
54
55 /* ---------------------------------------------------------------------- */
56
57 struct _zone {
58         u_int32_t z_begin;
59         u_int32_t z_end;
60 };
61
62 struct _citrus_mapper_zone {
63         struct _zone    mz_row;
64         struct _zone    mz_col;
65         int             mz_col_bits;
66         int32_t         mz_row_offset;
67         int32_t         mz_col_offset;
68 };
69
70 struct _parse_state {
71         enum { S_BEGIN, S_OFFSET }      ps_state;
72         union {
73                 u_int32_t       u_imm;
74                 int32_t         s_imm;
75                 struct _zone    zone;
76         } u;
77 #define ps_u_imm        u.u_imm
78 #define ps_s_imm        u.s_imm
79 #define ps_zone         u.zone
80         int ps_top;
81 };
82
83 int
84 _citrus_mapper_zone_mapper_getops(struct _citrus_mapper_ops *ops,
85                                   size_t lenops, uint32_t expected_version)
86 {
87         if (expected_version<_CITRUS_MAPPER_ABI_VERSION || lenops<sizeof(*ops))
88                 return EINVAL;
89
90         memcpy(ops, &_citrus_mapper_zone_mapper_ops,
91                sizeof(_citrus_mapper_zone_mapper_ops));
92
93         return 0;
94 }
95
96 #define BUFSIZE 20
97 #define T_ERR   0x100
98 #define T_IMM   0x101
99
100 static int
101 get_imm(struct _memstream *ms, struct _parse_state *ps)
102 {
103         int sign = 0;
104         int c, i;
105         char buf[BUFSIZE+1], *p;
106
107         for (i=0; i<BUFSIZE; i++) {
108 retry:
109                 c = _memstream_peek(ms);
110                 if (i==0) {
111                         if (sign == 0 && (c == '+' || c == '-')) {
112                                 sign = c;
113                                 _memstream_getc(ms);
114                                 goto retry;
115                         } else if (!_bcs_isdigit(c))
116                                 break;
117                 } else if (!_bcs_isxdigit(c))
118                         if (!(i==1 && c == 'x'))
119                                 break;
120                 buf[i] = _memstream_getc(ms);
121         }
122         buf[i] = '\0';
123         ps->ps_u_imm = strtoul(buf, &p, 0);
124         if ((p-buf) != i)
125                 return T_ERR;
126         if (sign == '-')
127                 ps->ps_u_imm = (unsigned long)-(long)ps->ps_u_imm;
128         return T_IMM;
129 }
130
131 static int
132 get_tok(struct _memstream *ms, struct _parse_state *ps)
133 {
134         int c;
135
136 loop:
137         c = _memstream_peek(ms);
138         if (c==0x00)
139                 return EOF;
140         if (_bcs_isspace(c)) {
141                 _memstream_getc(ms);
142                 goto loop;
143         }
144
145         switch (ps->ps_state) {
146         case S_BEGIN:
147                 switch (c) {
148                 case ':':
149                 case '-':
150                 case '/':
151                         _memstream_getc(ms);
152                         return c;
153                 case '0':
154                 case '1':
155                 case '2':
156                 case '3':
157                 case '4':
158                 case '5':
159                 case '6':
160                 case '7':
161                 case '8':
162                 case '9':
163                         return get_imm(ms, ps);
164                 }
165                 break;
166         case S_OFFSET:
167                 switch (c) {
168                 case '/':
169                         _memstream_getc(ms);
170                         return c;
171                 case '+': 
172                 case '-': 
173                 case '0':
174                 case '1':
175                 case '2':
176                 case '3':
177                 case '4':
178                 case '5':
179                 case '6':
180                 case '7':
181                 case '8':
182                 case '9':
183                         return get_imm(ms, ps);
184                 }
185                 break;
186         }
187         return T_ERR;
188 }
189
190 static int
191 parse_zone(struct _memstream *ms, struct _parse_state *ps, struct _zone *z)
192 {
193         if (get_tok(ms, ps) != T_IMM)
194                 return -1;
195         z->z_begin = ps->ps_u_imm;
196         if (get_tok(ms, ps) != '-')
197                 return -1;
198         if (get_tok(ms, ps) != T_IMM)
199                 return -1;
200         z->z_end = ps->ps_u_imm;
201
202         if (z->z_begin > z->z_end)
203                 return -1;
204
205         return 0;
206 }
207
208 static int
209 check_rowcol(struct _zone *z, int32_t ofs, uint32_t maxval)
210 {
211         u_int32_t remain;
212
213         if (maxval != 0 && z->z_end >= maxval)
214                 return -1;
215
216         if (ofs > 0) {
217                 if (maxval == 0) {
218                         /* this should 0x100000000 - z->z_end */
219                         if (z->z_end == 0) {
220                                 remain = 0xFFFFFFFF;
221                         } else {
222                                 remain = 0xFFFFFFFF - z->z_end + 1;
223                         }
224                 } else
225                         remain = maxval - z->z_end;
226                 if ((u_int32_t)ofs > remain)
227                         return -1;
228         } else if (ofs < 0) {
229                 if (z->z_begin < (u_int32_t)-ofs)
230                         return -1;
231         }
232
233         return 0;
234 }
235
236 static int
237 parse_var(struct _citrus_mapper_zone *mz, struct _memstream *ms)
238 {
239         struct _parse_state ps;
240         int ret, isrc;
241         uint32_t rowmax, colmax;
242
243         ps.ps_state = S_BEGIN;
244
245         if (parse_zone(ms, &ps, &mz->mz_col))
246                 return -1;
247
248         ret = get_tok(ms, &ps);
249         if (ret == '/') {
250                 /* rowzone / colzone / bits */
251                 isrc = 1;
252                 mz->mz_row = mz->mz_col;
253
254                 if (parse_zone(ms, &ps, &mz->mz_col))
255                         return -1;
256                 if (get_tok(ms, &ps) != '/')
257                         return -1;
258                 if (get_tok(ms, &ps) != T_IMM)
259                         return -1;
260                 mz->mz_col_bits = ps.ps_u_imm;
261                 if (mz->mz_col_bits<0 || mz->mz_col_bits>32)
262                         return -1;
263                 ret = get_tok(ms, &ps);
264         } else {
265                 /* colzone */
266                 isrc = 0;
267                 mz->mz_col_bits = 32;
268                 mz->mz_row.z_begin = mz->mz_row.z_end = 0;
269         }
270         if (ret == ':') {
271                 /* offset */
272                 ps.ps_state = S_OFFSET;
273                 if (get_tok(ms, &ps) != T_IMM)
274                         return -1;
275                 mz->mz_col_offset = ps.ps_s_imm;
276                 if (isrc) {
277                         /* row/col */
278                         mz->mz_row_offset = mz->mz_col_offset;
279                         if (get_tok(ms, &ps) != '/')
280                                 return -1;
281                         if (get_tok(ms, &ps) != T_IMM)
282                                 return -1;
283                         mz->mz_col_offset = ps.ps_s_imm;
284                 } else
285                         mz->mz_row_offset = 0;
286                 ret = get_tok(ms, &ps);
287         }
288         if (ret != EOF)
289                 return -1;
290
291         /* sanity check */
292         if (mz->mz_col_bits==32)
293                 colmax = 0;
294         else
295                 colmax = 1 << mz->mz_col_bits;
296         if (mz->mz_col_bits==0)
297                 rowmax = 0;
298         else
299                 rowmax = 1 << (32-mz->mz_col_bits);
300         if (check_rowcol(&mz->mz_col, mz->mz_col_offset, colmax))
301                 return -1;
302         if (check_rowcol(&mz->mz_row, mz->mz_row_offset, rowmax))
303                 return -1;
304
305         return 0;
306 }
307
308 static int
309 /*ARGSUSED*/
310 _citrus_mapper_zone_mapper_init(struct _citrus_mapper_area *__restrict ma,
311                                 struct _citrus_mapper * __restrict cm,
312                                 const char * __restrict dir,
313                                 const void * __restrict var, size_t lenvar,
314                                 struct _citrus_mapper_traits * __restrict mt,
315                                 size_t lenmt)
316 {
317         struct _citrus_mapper_zone *mz;
318         struct _memstream ms;
319         struct _region r;
320
321         _DIAGASSERT(cm && dir && mt);
322
323         if (lenmt<sizeof(*mt))
324                 return EINVAL;
325
326         mz = malloc(sizeof(*mz));
327         if (mz == NULL)
328                 return errno;
329
330         mz->mz_col.z_begin = mz->mz_col.z_end = 0;
331         mz->mz_row.z_begin = mz->mz_row.z_end = 0;
332         mz->mz_col_bits = 0;
333         mz->mz_row_offset = 0;
334         mz->mz_col_offset = 0;
335
336         _region_init(&r, (void *)var, lenvar);
337         _memstream_bind(&ms, &r);
338         if (parse_var(mz, &ms)) {
339                 free(mz);
340                 return EINVAL;
341         }
342         cm->cm_closure = mz;
343         mt->mt_src_max = mt->mt_dst_max = 1;    /* 1:1 converter */
344         mt->mt_state_size = 0;                  /* stateless */
345
346         return 0;
347 }
348
349 static void
350 /*ARGSUSED*/
351 _citrus_mapper_zone_mapper_uninit(struct _citrus_mapper *cm)
352 {
353 }
354
355 static int
356 /*ARGSUSED*/
357 _citrus_mapper_zone_mapper_convert(struct _citrus_mapper * __restrict cm,
358                                    _citrus_index_t * __restrict dst,
359                                    _citrus_index_t src, void * __restrict ps)
360 {
361         u_int32_t row, col;
362         struct _citrus_mapper_zone *mz = cm->cm_closure;
363
364         if (mz->mz_col_bits == 32) {
365                 col = src;
366                 row = 0;
367                 if (col < mz->mz_col.z_begin || col > mz->mz_col.z_end)
368                         return _CITRUS_MAPPER_CONVERT_NONIDENTICAL;
369                 if (mz->mz_col_offset>0)
370                         col += (u_int32_t)mz->mz_col_offset;
371                 else
372                         col -= (u_int32_t)-mz->mz_col_offset;
373                 *dst = col;
374         } else {
375                 col = src & (((u_int32_t)1<<mz->mz_col_bits)-1);
376                 row = src >> mz->mz_col_bits;
377                 if (row < mz->mz_row.z_begin || row > mz->mz_row.z_end ||
378                     col < mz->mz_col.z_begin || col > mz->mz_col.z_end)
379                         return _CITRUS_MAPPER_CONVERT_NONIDENTICAL;
380                 if (mz->mz_col_offset>0)
381                         col += (u_int32_t)mz->mz_col_offset;
382                 else
383                         col -= (u_int32_t)-mz->mz_col_offset;
384                 if (mz->mz_row_offset>0)
385                         row += (u_int32_t)mz->mz_row_offset;
386                 else
387                         row -= (u_int32_t)-mz->mz_row_offset;
388                 *dst = col | (row << mz->mz_col_bits);
389         }
390         return _CITRUS_MAPPER_CONVERT_SUCCESS;
391 }
392
393 static void
394 /*ARGSUSED*/
395 _citrus_mapper_zone_mapper_init_state(struct _citrus_mapper * __restrict cm,
396                                       void * __restrict ps)
397 {
398 }