1 /* $NetBSD: filemapper.c,v 1.4 2014/12/10 04:37:55 christos Exp $ */
4 static char *rcsid
= "Id: filemapper.c,v 1.1 2003/06/04 00:25:53 marka Exp ";
8 * Copyright (c) 2001,2002 Japan Network Information Center.
11 * By using this file, you agree to the terms and conditions set forth bellow.
13 * LICENSE TERMS AND CONDITIONS
15 * The following License Terms and Conditions apply, unless a different
16 * license is obtained from Japan Network Information Center ("JPNIC"),
17 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
18 * Chiyoda-ku, Tokyo 101-0047, Japan.
20 * 1. Use, Modification and Redistribution (including distribution of any
21 * modified or derived work) in source and/or binary forms is permitted
22 * under this License Terms and Conditions.
24 * 2. Redistribution of source code must retain the copyright notices as they
25 * appear in each source code file, this License Terms and Conditions.
27 * 3. Redistribution in binary form must reproduce the Copyright Notice,
28 * this License Terms and Conditions, in the documentation and/or other
29 * materials provided with the distribution. For the purposes of binary
30 * distribution the "Copyright Notice" refers to the following language:
31 * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
33 * 4. The name of JPNIC may not be used to endorse or promote products
34 * derived from this Software without specific prior written approval of
37 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
40 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
57 #include <idn/result.h>
58 #include <idn/assert.h>
60 #include <idn/logmacro.h>
61 #include <idn/debug.h>
63 #include <idn/ucsmap.h>
64 #include <idn/filemapper.h>
66 #define SUPPORT_VERSIONING
68 #define UCSBUF_LOCAL_SIZE 20
70 typedef struct ucsbuf
{
74 unsigned long local
[UCSBUF_LOCAL_SIZE
];
77 struct idn__filemapper
{
81 static void ucsbuf_init(ucsbuf_t
*b
);
82 static idn_result_t
ucsbuf_grow(ucsbuf_t
*b
);
83 static idn_result_t
ucsbuf_append(ucsbuf_t
*b
, unsigned long v
);
84 static void ucsbuf_free(ucsbuf_t
*b
);
85 static idn_result_t
read_file(const char *file
, FILE *fp
,
87 static idn_result_t
get_map(char *p
, ucsbuf_t
*b
);
88 static char *get_ucs(char *p
, unsigned long *vp
);
92 idn__filemapper_create(const char *file
, idn__filemapper_t
*ctxp
) {
94 idn__filemapper_t ctx
;
97 assert(file
!= NULL
&& ctxp
!= NULL
);
99 TRACE(("idn__filemapper_create(file=\"%-.100s\")\n", file
));
101 if ((fp
= fopen(file
, "r")) == NULL
) {
102 WARNING(("idn__filemapper_create: cannot open %-.100s\n",
106 if ((ctx
= malloc(sizeof(struct idn__filemapper
))) == NULL
)
107 return (idn_nomemory
);
109 if ((r
= idn_ucsmap_create(&ctx
->map
)) != idn_success
) {
114 r
= read_file(file
, fp
, ctx
->map
);
117 if (r
== idn_success
) {
118 idn_ucsmap_fix(ctx
->map
);
121 idn_ucsmap_destroy(ctx
->map
);
128 idn__filemapper_destroy(idn__filemapper_t ctx
) {
132 TRACE(("idn__filemapper_destroy()\n"));
134 idn_ucsmap_destroy(ctx
->map
);
139 idn__filemapper_map(idn__filemapper_t ctx
, const unsigned long *from
,
140 unsigned long *to
, size_t tolen
)
142 idn_result_t r
= idn_success
;
145 assert(ctx
!= NULL
&& from
!= NULL
&& to
!= NULL
);
147 TRACE(("idn__filemapper_map(from=\"%s\")\n",
148 idn__debug_ucs4xstring(from
, 50)));
150 /* Initialize temporary buffer. */
153 while (*from
!= '\0') {
155 r
= idn_ucsmap_map(ctx
->map
, *from
, ub
.ucs
, ub
.size
, &ub
.len
);
157 case idn_buffer_overflow
:
158 /* Temporary buffer too small. Enlarge and retry. */
159 if ((r
= ucsbuf_grow(&ub
)) != idn_success
)
163 /* There is no mapping. */
167 if (tolen
< ub
.len
) {
168 r
= idn_buffer_overflow
;
171 memcpy(to
, ub
.ucs
, sizeof(*to
) * ub
.len
);
184 if (r
== idn_success
) {
185 /* Terminate with NUL. */
187 return (idn_buffer_overflow
);
195 ucsbuf_init(ucsbuf_t
*b
) {
197 b
->size
= UCSBUF_LOCAL_SIZE
;
202 ucsbuf_grow(ucsbuf_t
*b
) {
203 unsigned long *newbuf
;
206 if (b
->ucs
== b
->local
) {
207 b
->ucs
= malloc(sizeof(unsigned long) * b
->size
);
209 return (idn_nomemory
);
210 memcpy(b
->ucs
, b
->local
, sizeof(b
->local
));
212 newbuf
= realloc(b
->ucs
, sizeof(unsigned long) * b
->size
);
214 return (idn_nomemory
);
217 return (idn_success
);
221 ucsbuf_append(ucsbuf_t
*b
, unsigned long v
) {
224 if (b
->len
+ 1 > b
->size
) {
226 if (r
!= idn_success
)
229 b
->ucs
[b
->len
++] = v
;
230 return (idn_success
);
234 ucsbuf_free(ucsbuf_t
*b
) {
235 if (b
->ucs
!= b
->local
&& b
->ucs
!= NULL
)
240 read_file(const char *file
, FILE *fp
, idn_ucsmap_t map
) {
243 idn_result_t r
= idn_success
;
248 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
252 while (isspace((unsigned char)*p
))
254 if (*p
== '\0' || *p
== '#')
256 #ifdef SUPPORT_VERSIONING
257 /* Skip version tag. */
258 if (lineno
== 1 && strncmp("version=", line
, 8) == 0)
266 r
= idn_ucsmap_add(map
, ub
.ucs
[0],
267 &ub
.ucs
[1], ub
.len
- 1);
269 case idn_buffer_overflow
:
270 if ((r
= ucsbuf_grow(&ub
)) != idn_success
)
273 case idn_invalid_syntax
:
274 WARNING(("syntax error in file \"%-.100s\" line %d: "
275 "%-.100s", file
, lineno
, line
));
287 get_map(char *p
, ucsbuf_t
*b
) {
289 idn_result_t r
= idn_success
;
292 if ((p
= get_ucs(p
, &v
)) == NULL
)
293 return (idn_invalid_syntax
);
294 if ((r
= ucsbuf_append(b
, v
)) != idn_success
)
298 return (idn_invalid_syntax
);
300 while (isspace((unsigned char)*p
))
304 if (*p
== ';' || *p
== '#' || *p
== '\0')
311 get_ucs(char *p
, unsigned long *vp
) {
314 /* Skip leading space */
315 while (isspace((unsigned char)*p
))
318 /* Skip optional 'U+' */
319 if (strncmp(p
, "U+", 2) == 0)
322 *vp
= strtoul(p
, &end
, 16);
324 INFO(("idn__filemapper_create: UCS code point expected\n"));
329 /* Skip trailing space */
330 while (isspace((unsigned char)*p
))
336 idn__filemapper_createproc(const char *parameter
, void **ctxp
) {
337 return idn__filemapper_create(parameter
, (idn__filemapper_t
*)ctxp
);
341 idn__filemapper_destroyproc(void *ctxp
) {
342 idn__filemapper_destroy((idn__filemapper_t
)ctxp
);
346 idn__filemapper_mapproc(void *ctx
, const unsigned long *from
,
347 unsigned long *to
, size_t tolen
) {
348 return idn__filemapper_map((idn__filemapper_t
)ctx
, from
, to
, tolen
);