1 #ifndef lint
2 static char *rcsid = "$Id: delimitermap.c,v 1.15 2002/11/29 09:08:04 ishisone Exp $";
3 #endif
4
5 /*
6 * Copyright (c) 2001,2002 Japan Network Information Center.
7 * All rights reserved.
8 *
9 * By using this file, you agree to the terms and conditions set forth bellow.
10 *
11 * LICENSE TERMS AND CONDITIONS
12 *
13 * The following License Terms and Conditions apply, unless a different
14 * license is obtained from Japan Network Information Center ("JPNIC"),
15 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
16 * Chiyoda-ku, Tokyo 101-0047, Japan.
17 *
18 * 1. Use, Modification and Redistribution (including distribution of any
19 * modified or derived work) in source and/or binary forms is permitted
20 * under this License Terms and Conditions.
21 *
22 * 2. Redistribution of source code must retain the copyright notices as they
23 * appear in each source code file, this License Terms and Conditions.
24 *
25 * 3. Redistribution in binary form must reproduce the Copyright Notice,
26 * this License Terms and Conditions, in the documentation and/or other
27 * materials provided with the distribution. For the purposes of binary
28 * distribution the "Copyright Notice" refers to the following language:
29 * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
30 *
31 * 4. The name of JPNIC may not be used to endorse or promote products
32 * derived from this Software without specific prior written approval of
33 * JPNIC.
34 *
35 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
36 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
38 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
43 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
44 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
45 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
46 */
47
48 #include <config.h>
49
50 #include <stddef.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include <idn/result.h>
55 #include <idn/assert.h>
56 #include <idn/logmacro.h>
57 #include <idn/delimitermap.h>
58 #include <idn/util.h>
59 #include <idn/debug.h>
60 #include <idn/ucs4.h>
61
62 /*
63 * Mapper object type.
64 */
65 struct idn_delimitermap {
66 int ndelimiters;
67 int delimiter_size;
68 unsigned long *delimiters;
69 int reference_count;
70 };
71
72 #define DELIMITERMAP_INITIAL_DELIMITER_SIZE 4
73 #define UNICODE_MAX 0x10ffff
74 #define IS_SURROGATE_HIGH(v) (0xd800 <= (v) && (v) <= 0xdbff)
75 #define IS_SURROGATE_LOW(v) (0xdc00 <= (v) && (v) <= 0xdfff)
76
77 idn_result_t
idn_delimitermap_create(idn_delimitermap_t * ctxp)78 idn_delimitermap_create(idn_delimitermap_t *ctxp) {
79 idn_delimitermap_t ctx = NULL;
80 idn_result_t r;
81
82 assert(ctxp != NULL);
83 TRACE(("idn_delimitermap_create()\n"));
84
85 ctx = (idn_delimitermap_t) malloc(sizeof(struct idn_delimitermap));
86 if (ctx == NULL) {
87 WARNING(("idn_mapper_create: malloc failed\n"));
88 r = idn_nomemory;
89 goto ret;
90 }
91
92 ctx->delimiters = (unsigned long *) malloc(sizeof(unsigned long)
93 * DELIMITERMAP_INITIAL_DELIMITER_SIZE);
94 if (ctx->delimiters == NULL) {
95 r = idn_nomemory;
96 goto ret;
97 }
98 ctx->ndelimiters = 0;
99 ctx->delimiter_size = DELIMITERMAP_INITIAL_DELIMITER_SIZE;
100 ctx->reference_count = 1;
101 *ctxp = ctx;
102 r = idn_success;
103
104 ret:
105 if (r != idn_success)
106 free(ctx);
107 TRACE(("idn_delimitermap_create(): %s\n", idn_result_tostring(r)));
108 return (r);
109 }
110
111 void
idn_delimitermap_destroy(idn_delimitermap_t ctx)112 idn_delimitermap_destroy(idn_delimitermap_t ctx) {
113 assert(ctx != NULL);
114
115 TRACE(("idn_delimitermap_destroy()\n"));
116
117 ctx->reference_count--;
118 if (ctx->reference_count <= 0) {
119 TRACE(("idn_mapper_destroy(): the object is destroyed\n"));
120 free(ctx->delimiters);
121 free(ctx);
122 } else {
123 TRACE(("idn_delimitermap_destroy(): "
124 "update reference count (%d->%d)\n",
125 ctx->reference_count + 1, ctx->reference_count));
126 }
127 }
128
129 void
idn_delimitermap_incrref(idn_delimitermap_t ctx)130 idn_delimitermap_incrref(idn_delimitermap_t ctx) {
131 assert(ctx != NULL);
132
133 TRACE(("idn_delimitermap_incrref()\n"));
134 TRACE(("idn_delimitermap_incrref: update reference count (%d->%d)\n",
135 ctx->reference_count, ctx->reference_count + 1));
136
137 ctx->reference_count++;
138 }
139
140 idn_result_t
idn_delimitermap_add(idn_delimitermap_t ctx,unsigned long delimiter)141 idn_delimitermap_add(idn_delimitermap_t ctx, unsigned long delimiter) {
142 idn_result_t r;
143
144 assert(ctx != NULL && ctx->ndelimiters <= ctx->delimiter_size);
145 TRACE(("idn_delimitermap_add(delimiter=\\x%04lx)\n", delimiter));
146
147 if (delimiter == 0 || delimiter > UNICODE_MAX ||
148 IS_SURROGATE_HIGH(delimiter) || IS_SURROGATE_LOW(delimiter)) {
149 r = idn_invalid_codepoint;
150 goto ret;
151 }
152
153 if (ctx->ndelimiters == ctx->delimiter_size) {
154 unsigned long *new_delimiters;
155
156 new_delimiters = (unsigned long *) realloc(ctx->delimiters,
157 sizeof(unsigned long) * ctx->delimiter_size * 2);
158 if (new_delimiters == NULL) {
159 r = idn_nomemory;
160 goto ret;
161 }
162 ctx->delimiters = new_delimiters;
163 ctx->delimiter_size *= 2;
164 }
165
166 ctx->delimiters[ctx->ndelimiters] = delimiter;
167 ctx->ndelimiters++;
168 r = idn_success;
169
170 ret:
171 TRACE(("idn_delimitermap_add(): %s\n", idn_result_tostring(r)));
172 return (r);
173 }
174
175 idn_result_t
idn_delimitermap_addall(idn_delimitermap_t ctx,unsigned long * delimiters,int ndelimiters)176 idn_delimitermap_addall(idn_delimitermap_t ctx, unsigned long *delimiters,
177 int ndelimiters) {
178 idn_result_t r;
179 int i;
180
181 assert(ctx != NULL && delimiters != NULL);
182
183 TRACE(("idn_delimitermap_addall(ndelimiters=%d)\n", ndelimiters));
184
185 for (i = 0; i < ndelimiters; i++) {
186 r = idn_delimitermap_add(ctx, *delimiters);
187 if (r != idn_success)
188 goto ret;
189 delimiters++;
190 }
191
192 r = idn_success;
193 ret:
194 TRACE(("idn_delimitermap_addall(): %s\n", idn_result_tostring(r)));
195 return (r);
196 }
197
198 idn_result_t
idn_delimitermap_map(idn_delimitermap_t ctx,const unsigned long * from,unsigned long * to,size_t tolen)199 idn_delimitermap_map(idn_delimitermap_t ctx, const unsigned long *from,
200 unsigned long *to, size_t tolen) {
201
202 /* default delimiters (label separators) from IDNA specification */
203 static const unsigned long default_delimiters[] =
204 { 0x002e, /* full stop */
205 0x3002, /* ideographic full stop */
206 0xff0e, /* fullwidth full stop */
207 0xff61, /* halfwidth ideographic full stop */
208 0x0000 };
209
210 unsigned long *to_org = to;
211 idn_result_t r;
212 int i, j;
213 int found;
214
215 assert(ctx != NULL && from != NULL && to != NULL);
216
217 TRACE(("idn_delimitermap_map(from=\"%s\", tolen=%d)\n",
218 idn__debug_ucs4xstring(from, 50), (int)tolen));
219
220 /*
221 * Map.
222 */
223 while (*from != '\0') {
224 found = 0;
225 if (tolen < 1) {
226 r = idn_buffer_overflow;
227 goto ret;
228 }
229 for (j = 0; default_delimiters[j] != 0x0000; j++) {
230 if (default_delimiters[j] == *from) {
231 found = 1;
232 break;
233 }
234 }
235 if (!found) {
236 for (i = 0; i < ctx->ndelimiters; i++) {
237 if (ctx->delimiters[i] == *from) {
238 found = 1;
239 break;
240 }
241 }
242 }
243 if (found)
244 *to = '.';
245 else
246 *to = *from;
247 from++;
248 to++;
249 tolen--;
250 }
251
252 if (tolen < 1) {
253 r = idn_buffer_overflow;
254 goto ret;
255 }
256 *to = '\0';
257 r = idn_success;
258
259 ret:
260 if (r == idn_success) {
261 TRACE(("idn_delimitermap_map(): success (to=\"%s\")\n",
262 idn__debug_ucs4xstring(to_org, 50)));
263 } else {
264 TRACE(("idn_delimitermap_map(): %s\n",
265 idn_result_tostring(r)));
266 }
267 return (r);
268 }
269