/src/dcmtk/oficonv/libsrc/citrus_mapper_646.c
Line | Count | Source |
1 | | /*- |
2 | | * Copyright (c)2003 Citrus Project, |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * |
14 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
15 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
18 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 | | * SUCH DAMAGE. |
25 | | */ |
26 | | |
27 | | #include "dcmtk/config/osconfig.h" |
28 | | #include "citrus_mapper_646.h" |
29 | | |
30 | | #ifdef HAVE_SYS_QUEUE_H |
31 | | #include <sys/queue.h> |
32 | | #else |
33 | | #include "dcmtk/oficonv/queue.h" |
34 | | #endif |
35 | | |
36 | | #include <errno.h> |
37 | | #include <limits.h> |
38 | | #include <stdio.h> |
39 | | #include <stdlib.h> |
40 | | #include <string.h> |
41 | | |
42 | | #include "citrus_bcs.h" |
43 | | #include "citrus_types.h" |
44 | | #include "citrus_module.h" |
45 | | #include "citrus_region.h" |
46 | | #include "citrus_memstream.h" |
47 | | #include "citrus_mmap.h" |
48 | | #include "citrus_hash.h" |
49 | | #include "citrus_mapper.h" |
50 | | |
51 | | /* ---------------------------------------------------------------------- */ |
52 | | |
53 | | _CITRUS_MAPPER_DECLS(mapper_646); |
54 | | _CITRUS_MAPPER_DEF_OPS(mapper_646); |
55 | | |
56 | | /* ---------------------------------------------------------------------- */ |
57 | | |
58 | | #define ILSEQ 0xFFFFFFFE |
59 | 0 | #define INVALID 0xFFFFFFFF |
60 | | #define SPECIALS(x) \ |
61 | 0 | x(0x23) \ |
62 | 0 | x(0x24) \ |
63 | 0 | x(0x40) \ |
64 | 0 | x(0x5B) \ |
65 | 0 | x(0x5C) \ |
66 | 0 | x(0x5D) \ |
67 | 0 | x(0x5E) \ |
68 | 0 | x(0x60) \ |
69 | 0 | x(0x7B) \ |
70 | 0 | x(0x7C) \ |
71 | 0 | x(0x7D) \ |
72 | 0 | x(0x7E) |
73 | | |
74 | | #define INDEX(x) INDEX_##x, |
75 | | |
76 | | enum { |
77 | | SPECIALS(INDEX) |
78 | | NUM_OF_SPECIALS |
79 | | }; |
80 | | struct _citrus_mapper_646 { |
81 | | _citrus_index_t m6_map[NUM_OF_SPECIALS]; |
82 | | int m6_forward; |
83 | | }; |
84 | | |
85 | | int |
86 | | _citrus_mapper_646_mapper_getops(struct _citrus_mapper_ops *ops) |
87 | 0 | { |
88 | |
|
89 | 0 | memcpy(ops, &_citrus_mapper_646_mapper_ops, |
90 | 0 | sizeof(_citrus_mapper_646_mapper_ops)); |
91 | |
|
92 | 0 | return (0); |
93 | 0 | } |
94 | | |
95 | 0 | #define T_COMM '#' |
96 | | static int |
97 | | parse_file(struct _citrus_mapper_646 *m6, const char *path) |
98 | 0 | { |
99 | 0 | struct _citrus_memory_stream ms; |
100 | 0 | struct _citrus_region r; |
101 | 0 | const char *p; |
102 | 0 | char *pp; |
103 | 0 | size_t len; |
104 | 0 | char buf[OFICONV_PATH_MAX]; |
105 | 0 | int i, ret; |
106 | |
|
107 | 0 | ret = _citrus_map_file(&r, path); |
108 | 0 | if (ret) |
109 | 0 | return (ret); |
110 | 0 | _citrus_memory_stream_bind(&ms, &r); |
111 | 0 | for (i = 0; i < NUM_OF_SPECIALS; i++) { |
112 | 0 | retry: |
113 | 0 | p = _citrus_memory_stream_getln(&ms, &len); |
114 | 0 | if (p == NULL) { |
115 | 0 | ret = EINVAL; |
116 | 0 | break; |
117 | 0 | } |
118 | 0 | p = _citrus_bcs_skip_ws_len(p, &len); |
119 | 0 | if (*p == T_COMM || len==0) |
120 | 0 | goto retry; |
121 | 0 | if (!_citrus_bcs_isdigit(*p)) { |
122 | 0 | ret = EINVAL; |
123 | 0 | break; |
124 | 0 | } |
125 | 0 | snprintf(buf, sizeof(buf), "%.*s", (int)len, p); |
126 | 0 | pp = CITRUS_DECONST(void *, p); |
127 | 0 | m6->m6_map[i] = strtoul(buf, (char **)&pp, 0); |
128 | 0 | p = _citrus_bcs_skip_ws(buf); |
129 | 0 | if (*p != T_COMM && !*p) { |
130 | 0 | ret = EINVAL; |
131 | 0 | break; |
132 | 0 | } |
133 | 0 | } |
134 | 0 | _citrus_unmap_file(&r); |
135 | |
|
136 | 0 | return (ret); |
137 | 0 | }; |
138 | | |
139 | | static int |
140 | | parse_var(struct _citrus_mapper_646 *m6, struct _citrus_memory_stream *ms, |
141 | | const char *dir) |
142 | 0 | { |
143 | 0 | struct _citrus_region r; |
144 | 0 | char path[OFICONV_PATH_MAX]; |
145 | |
|
146 | 0 | memset(&r, 0, sizeof(struct _citrus_region )); |
147 | 0 | m6->m6_forward = 1; |
148 | 0 | _citrus_memory_stream_skip_ws(ms); |
149 | | /* whether backward */ |
150 | 0 | if (_citrus_memory_stream_peek(ms) == '!') { |
151 | 0 | _citrus_memory_stream_getc(ms); |
152 | 0 | m6->m6_forward = 0; |
153 | 0 | } |
154 | | /* get description file path */ |
155 | 0 | _citrus_memory_stream_getregion(ms, &r, _citrus_memory_stream_remainder(ms)); |
156 | 0 | snprintf(path, sizeof(path), "%s/%.*s", |
157 | 0 | dir, (int)_citrus_region_size(&r), (char *)_citrus_region_head(&r)); |
158 | | /* remove trailing white spaces */ |
159 | 0 | path[_citrus_bcs_skip_nonws(path)-path] = '\0'; |
160 | 0 | return (parse_file(m6, path)); |
161 | 0 | } |
162 | | |
163 | | static int |
164 | | /*ARGSUSED*/ |
165 | | _citrus_mapper_646_mapper_init(struct _citrus_mapper_area * ma , |
166 | | struct _citrus_csmapper * cm, const char * dir, |
167 | | const void * var, size_t lenvar, |
168 | | struct _citrus_mapper_traits * mt, size_t lenmt) |
169 | 0 | { |
170 | 0 | struct _citrus_mapper_646 *m6; |
171 | 0 | struct _citrus_memory_stream ms; |
172 | 0 | struct _citrus_region r; |
173 | 0 | int ret; |
174 | 0 | (void) ma; |
175 | |
|
176 | 0 | if (lenmt < sizeof(*mt)) |
177 | 0 | return (EINVAL); |
178 | | |
179 | 0 | m6 = malloc(sizeof(*m6)); |
180 | 0 | if (m6 == NULL) |
181 | 0 | return (errno); |
182 | | |
183 | 0 | _citrus_region_init(&r, CITRUS_DECONST(void *, var), lenvar); |
184 | 0 | _citrus_memory_stream_bind(&ms, &r); |
185 | 0 | ret = parse_var(m6, &ms, dir); |
186 | 0 | if (ret) { |
187 | 0 | free(m6); |
188 | 0 | return (ret); |
189 | 0 | } |
190 | | |
191 | 0 | cm->cm_closure = m6; |
192 | 0 | mt->mt_src_max = mt->mt_dst_max = 1; /* 1:1 converter */ |
193 | 0 | mt->mt_state_size = 0; /* stateless */ |
194 | |
|
195 | 0 | return (0); |
196 | 0 | } |
197 | | |
198 | | static void |
199 | | /*ARGSUSED*/ |
200 | | _citrus_mapper_646_mapper_uninit(struct _citrus_csmapper *cm) |
201 | 0 | { |
202 | |
|
203 | 0 | if (cm && cm->cm_closure) |
204 | 0 | free(cm->cm_closure); |
205 | 0 | } |
206 | | |
207 | | static int |
208 | | /*ARGSUSED*/ |
209 | | _citrus_mapper_646_mapper_convert(struct _citrus_csmapper * cm, |
210 | | _citrus_index_t * dst, _citrus_index_t src, void * ps ) |
211 | 0 | { |
212 | 0 | struct _citrus_mapper_646 *m6; |
213 | 0 | (void) ps; |
214 | |
|
215 | 0 | m6 = cm->cm_closure; |
216 | 0 | if (m6->m6_forward) { |
217 | | /* forward */ |
218 | 0 | if (src >= 0x80) |
219 | 0 | return (_CITRUS_MAPPER_CONVERT_ILSEQ); |
220 | 0 | #define FORWARD(x) \ |
221 | 0 | if (src == (x)) { \ |
222 | 0 | if (m6->m6_map[INDEX_##x]==INVALID) \ |
223 | 0 | return (_CITRUS_MAPPER_CONVERT_NONIDENTICAL); \ |
224 | 0 | *dst = m6->m6_map[INDEX_##x]; \ |
225 | 0 | return (0); \ |
226 | 0 | } else |
227 | 0 | SPECIALS(FORWARD); |
228 | 0 | *dst = src; |
229 | 0 | } else { |
230 | | /* backward */ |
231 | 0 | #define BACKWARD(x) \ |
232 | 0 | if (m6->m6_map[INDEX_##x] != INVALID && src == m6->m6_map[INDEX_##x]) { \ |
233 | 0 | *dst = (x); \ |
234 | 0 | return (0); \ |
235 | 0 | } else if (src == (x)) \ |
236 | 0 | return (_CITRUS_MAPPER_CONVERT_ILSEQ); \ |
237 | 0 | else |
238 | 0 | SPECIALS(BACKWARD); |
239 | 0 | if (src >= 0x80) |
240 | 0 | return (_CITRUS_MAPPER_CONVERT_NONIDENTICAL); |
241 | 0 | *dst = src; |
242 | 0 | } |
243 | | |
244 | 0 | return (_CITRUS_MAPPER_CONVERT_SUCCESS); |
245 | 0 | } |
246 | | |
247 | | static void |
248 | | /*ARGSUSED*/ |
249 | | _citrus_mapper_646_mapper_init_state(void) |
250 | 0 | { |
251 | |
|
252 | 0 | } |