1 /* $NetBSD: filechecker.c,v 1.4 2014/12/10 04:37:55 christos Exp $ */
4 static char *rcsid
= "Id: filechecker.c,v 1.1 2003/06/04 00:25:52 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/ucsset.h>
62 #include <idn/filechecker.h>
63 #include <idn/debug.h>
65 #define SUPPORT_VERSIONING
67 struct idn__filechecker
{
71 static idn_result_t
read_file(const char *file
, FILE *fp
,
73 static int get_range(char *s
, unsigned long *ucs1
,
75 static char *get_ucs(char *p
, unsigned long *vp
);
79 idn__filechecker_create(const char *file
, idn__filechecker_t
*ctxp
) {
81 idn__filechecker_t ctx
;
84 assert(file
!= NULL
&& ctxp
!= NULL
);
86 TRACE(("idn__filechecker_create(file=\"%-.100s\")\n", file
));
88 if ((fp
= fopen(file
, "r")) == NULL
) {
89 WARNING(("idn__filechecker_create: cannot open %-.100s\n",
94 if ((ctx
= malloc(sizeof(struct idn__filechecker
))) == NULL
)
95 return (idn_nomemory
);
97 if ((r
= idn_ucsset_create(&ctx
->set
)) != idn_success
) {
102 r
= read_file(file
, fp
, ctx
->set
);
105 if (r
== idn_success
) {
106 idn_ucsset_fix(ctx
->set
);
109 idn_ucsset_destroy(ctx
->set
);
116 idn__filechecker_destroy(idn__filechecker_t ctx
) {
119 TRACE(("idn__filechecker_destroy()\n"));
121 idn_ucsset_destroy(ctx
->set
);
126 idn__filechecker_lookup(idn__filechecker_t ctx
, const unsigned long *str
,
127 const unsigned long **found
) {
128 idn_result_t r
= idn_success
;
130 assert(ctx
!= NULL
&& str
!= NULL
);
132 TRACE(("idn__filechecker_lookup(str=\"%s\")\n",
133 idn__debug_ucs4xstring(str
, 50)));
135 while (*str
!= '\0') {
138 r
= idn_ucsset_lookup(ctx
->set
, *str
, &exists
);
140 if (r
!= idn_success
) {
145 return (idn_success
);
150 return (idn_success
);
154 read_file(const char *file
, FILE *fp
, idn_ucsset_t set
) {
159 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
161 unsigned long ucs1
, ucs2
;
164 while (isspace((unsigned char)*p
))
166 if (*p
== '\0' || *p
== '#')
169 #ifdef SUPPORT_VERSIONING
170 /* Skip version tag. */
171 if (lineno
== 1 && strncmp("version=", line
, 8) == 0)
174 if (!get_range(p
, &ucs1
, &ucs2
)) {
175 WARNING(("syntax error in file \"%-.100s\" line %d: "
176 "%-.100s", file
, lineno
, line
));
177 return (idn_invalid_syntax
);
179 if ((r
= idn_ucsset_addrange(set
, ucs1
, ucs2
)) != idn_success
)
182 return (idn_success
);
186 get_range(char *s
, unsigned long *ucs1
, unsigned long *ucs2
) {
187 if ((s
= get_ucs(s
, ucs1
)) == NULL
)
203 if ((s
= get_ucs(s
+ 1, ucs2
)) == NULL
)
207 INFO(("idn__filechecker_create: invalid range spec "
208 "U+%X-U+%X\n", *ucs1
, *ucs2
));
225 get_ucs(char *p
, unsigned long *vp
) {
228 /* Skip leading space */
229 while (isspace((unsigned char)*p
))
232 /* Skip optional 'U+' */
233 if (strncmp(p
, "U+", 2) == 0)
236 *vp
= strtoul(p
, &end
, 16);
238 INFO(("idn__filechecker_create: UCS code point expected\n"));
243 /* Skip trailing space */
244 while (isspace((unsigned char)*p
))
250 idn__filechecker_createproc(const char *parameter
, void **ctxp
) {
251 return idn__filechecker_create(parameter
, (idn__filechecker_t
*)ctxp
);
255 idn__filechecker_destroyproc(void *ctxp
) {
256 idn__filechecker_destroy((idn__filechecker_t
)ctxp
);
260 idn__filechecker_lookupproc(void *ctx
, const unsigned long *str
,
261 const unsigned long **found
) {
262 return idn__filechecker_lookup((idn__filechecker_t
)ctx
, str
, found
);