1 /*
2  *                           TERMS AND CONDITIONS
3  *                                   FOR
4  *                         OPEN SOURCE CODE LICENSE
5  *                               Version 1.1
6  *
7  * Japan Registry Services Co., Ltd. ("JPRS"), a Japanese corporation
8  * having its head office at Chiyoda First Bldg. East 13F 3-8-1 Nishi-Kanda,
9  * Chiyoda-ku, Tokyo 101-0065, Japan, grants you the license for open source
10  * code specified in EXHIBIT A the "Code" subject to the following Terms and
11  * Conditions ("OSCL").
12  *
13  * 1. License Grant.
14  *   JPRS hereby grants you a worldwide, royalty-free, non-exclusive
15  *   license, subject to third party intellectual property claims:
16  *   (a) under intellectual property rights (other than patent or
17  *       trademark) licensable by JPRS to use, reproduce, modify, display,
18  *       perform, sublicense and distribute the Code (or portions thereof)
19  *       with or without modifications, and/or as part of a derivative work;
20  *       or
21  *   (b) under claims of the infringement through the making, using,
22  *       offering to sell and/or otherwise disposing the JPRS Revised Code
23  *       (or portions thereof);
24  *   (c) the licenses granted in this Section 1(a) and (b) are effective on
25  *       the date JPRS first distributes the Code to you under the terms of
26  *       this OSCL;
27  *   (d) Notwithstanding the above stated terms, no patent license is
28  *       granted:
29  *       1)  for a code that you delete from the Code;
30  *       2)  separate from the Code; or
31  *       3)  for infringements caused by:
32  *            i) modification of the Code; or
33  *           ii) combination of the Code with other software or devices.
34  *
35  * 2. Consents.
36  *   You agree that:
37  *   (a) you must include a copy of this OSCL and the notice set forth in
38  *       EXHIBIT A with every copy of the Code you distribute;
39  *   (b) you must include a copy of this OSCL and the notice set forth in
40  *       EXHIBIT A with every copy of binary form of the Code in the
41  *       documentation and/or other materials provided with the distribution;
42  *   (c) you may not offer or impose any terms on any source code version
43  *       that alters or restricts the applicable version of this OSCL or
44  *       the recipients' rights hereunder.
45  *   (d) If the terms and conditions are set forth in EXHIBIT A, you must
46  *       comply with those terms and conditions.
47  *
48  * 3. Proprietary Information.
49  *   All trademarks, service marks, patents, copyrights, trade secrets, and
50  *   other proprietary rights in or related to the Code are and will remain
51  *   the exclusive property of JPRS or its licensors, whether or not
52  *   specifically recognized or perfected under local law except specified
53  *   in this OSCL; provided however you agree and understand that the JPRS
54  *   name may not be used to endorse or promote this Code without prior
55  *   written approval of JPRS.
56  *
57  * 4. WARRANTY DISCLAIMER.
58  *   JPRS MAKES NO REPRESENTATIONS AND WARRANTIES REGARDING THE USE OF THE
59  *   CODE, NOR DOES JPRS MAKE ANY REPRESENTATIONS THAT THE CODE WILL BECOME
60  *   COMMERCIALLY AVAILABLE. JPRS, ITS AFFILIATES, AND ITS SUPPLIERS DO NOT
61  *   WARRANT OR REPRESENT THAT THE CODE IS FREE OF ERRORS OR THAT THE CODE
62  *   IS SUITABLE FOR TRANSLATION AND/OR LOCALIZATION. THE CODE IS PROVIDED
63  *   ON AN "AS IS" BASIS AND JPRS AND ITS SUPPLIERS HAVE NO OBLIGATION TO
64  *   CORRECT ERRORS OR TO SUPPORT THE CODE UNDER THIS OSCL FOR ANY REASON.
65  *   TO THE FULL EXTENT PERMITTED BY LAW, ALL OBLIGATIONS ARE HEREBY
66  *   EXCLUDED WHETHER EXPRESS, STATUTORY OR IMPLIED UNDER LAW, COURSE OF
67  *   DEALING, CUSTOM, TRADE USAGE, ORAL OR WRITTEN STATEMENT OR OTHERWISE,
68  *   INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY
69  *   OR FITNESS FOR A PARTICULAR PURPOSE CONCERNING THE CODE.
70  *
71  * 5. NO LIABILITY.
72  *   UNDER NO CIRCUMSTANCES SHALL JPRS AND/OR ITS AFFILIATES, LICENSORS, OR
73  *   REPRESENTATIVES BE LIABLE FOR ANY DAMAGES INCLUDING BUT NOT LIMITED TO
74  *   CONSEQUENTIAL, INDIRECT, SPECIAL, PUNITIVE OR INCIDENTAL DAMAGES,
75  *   WHETHER FORESEEABLE OR UNFORESEEABLE, BASED ON YOUR CLAIMS, INCLUDING,
76  *   BUT NOT LIMITED TO, CLAIMS FOR LOSS OF DATA, GOODWILL, PROFITS, USE OF
77  *   MONEY, INTERRUPTION IN USE OR AVAILABILITY OF DATA, STOPPAGE, IMPLIED
78  *   WARRANTY, BREACH OF CONTRACT, MISREPRESENTATION, NEGLIGENCE, STRICT
79  *   LIABILITY IN TORT, OR OTHERWISE.
80  *
81  * 6. Indemnification.
82  *   You hereby agree to indemnify, defend, and hold harmless JPRS for any
83  *   liability incurred by JRPS due to your terms of warranty, support,
84  *   indemnity, or liability offered by you to any third party.
85  *
86  * 7. Termination.
87  * 7.1 This OSCL shall be automatically terminated in the events that:
88  *   (a) You fail to comply with the terms herein and fail to cure such
89  *       breach within 30 days of becoming aware of the breach;
90  *   (b) You initiate patent or copyright infringement litigation against
91  *       any party (including a cross-claim or counterclaim in a lawsuit)
92  *       alleging that the Code constitutes a direct or indirect patent or
93  *       copyright infringement, in such case, this OSCL to you shall
94  *       terminate as of the date such litigation is filed;
95  * 7.2 In the event of termination under Sections 7.1(a) or 7.1(b) above,
96  *     all end user license agreements (excluding distributors and
97  *     resellers) which have been validly granted by You or any distributor
98  *     hereunder prior to termination shall survive termination.
99  *
100  *
101  * 8. General.
102  *   This OSCL shall be governed by, and construed and enforced in
103  *   accordance with, the laws of Japan. Any litigation or arbitration
104  *   between the parties shall be conducted exclusively in Tokyo, Japan
105  *   except written consent of JPRS provides other venue.
106  *
107  *
108  *                                EXHIBIT A
109  *
110  * The original open source code of idnkit-2 is idnkit-1.0 developed and
111  * conceived by Japan Network Information Center ("JPNIC"), a Japanese
112  * association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
113  * Chiyoda-ku, Tokyo 101-0047, Japan, and JPRS modifies above original code
114  * under following Terms and Conditions set forth by JPNIC.
115  *
116  *                                  JPNIC
117  *
118  * Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved.
119  *
120  * By using this file, you agree to the terms and conditions set forth bellow.
121  *
122  *                       LICENSE TERMS AND CONDITIONS
123  *
124  * The following License Terms and Conditions apply, unless a different
125  * license is obtained from Japan Network Information Center ("JPNIC"),
126  * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
127  * Chiyoda-ku, Tokyo 101-0047, Japan.
128  *
129  * 1. Use, Modification and Redistribution (including distribution of any
130  *    modified or derived work) in source and/or binary forms is permitted
131  *    under this License Terms and Conditions.
132  *
133  * 2. Redistribution of source code must retain the copyright notices as they
134  *    appear in each source code file, this License Terms and Conditions.
135  *
136  * 3. Redistribution in binary form must reproduce the Copyright Notice,
137  *    this License Terms and Conditions, in the documentation and/or other
138  *    materials provided with the distribution. For the purposes of binary
139  *    distribution the "Copyright Notice" refers to the following language:
140  *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
141  *
142  * 4. The name of JPNIC may not be used to endorse or promote products
143  *    derived from this Software without specific prior written approval of
144  *    JPNIC.
145  *
146  * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
147  *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
148  *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
149  *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
150  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
151  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
152  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
153  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
154  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
155  *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
156  *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
157  *
158  *
159  *                        JPRS Public License Notice
160  *                                   For
161  *                                idnkit-2.
162  *
163  * The contents of this file are subject to the Terms and Conditions for
164  * the Open Source Code License (the "OSCL"). You may not use this file
165  * except in compliance with above terms and conditions. A copy of the OSCL
166  * is available at <http://jprs.co.jp/idn/>.
167  * The JPRS Revised Code is idnkit-2.
168  * The Initial Developer of the JPRS Revised Code is Japan Network
169  * Information Center ("JPNIC"), a Japanese association,
170  * Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda, Chiyoda-ku, Tokyo
171  * 101-0047, Japan.
172  * "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
173  * "Copyright (c) 2010-2012 Japan Registry Services Co., Ltd.  All rights reserved."
174  * Contributor(s): ______________________________________.
175  *
176  * If you wish to allow use of your version of this file only under the
177  * above License(s) and not to allow others to use your version of this
178  * file, please indicate your decision by deleting the relevant provisions
179  * above and replacing them with the notice and other provisions required
180  * by the above License(s). If you do not delete the relevant provisions,
181  * a recipient may use your version of this file under either the above
182  * License(s).
183  */
184 
185 #include <config.h>
186 
187 #include <stddef.h>
188 #include <stdlib.h>
189 #include <string.h>
190 
191 #include <idn/assert.h>
192 #include <idn/debug.h>
193 #include <idn/logmacro.h>
194 #include <idn/result.h>
195 #include <idn/delimitermap.h>
196 #include <idn/utf32.h>
197 
198 /*
199  * Mapper object type.
200  */
201 struct idn__delimitermap {
202 	int ndelimiters;
203 	int delimiter_size;
204 	unsigned long *delimiters;
205 };
206 
207 #define DELIMITERMAP_INITIAL_DELIMITER_SIZE	8
208 
209 /*
210  * Create a delimitermap object.
211  */
212 idn_result_t
idn__delimitermap_create(idn__delimitermap_t * ctxp)213 idn__delimitermap_create(idn__delimitermap_t *ctxp) {
214 	idn_result_t r = idn_success;
215 	idn__delimitermap_t ctx = NULL;
216 
217 	assert(ctxp != NULL);
218 	TRACE(("idn__delimitermap_create()\n"));
219 
220 	ctx = (idn__delimitermap_t) malloc(sizeof(struct idn__delimitermap));
221 	if (ctx == NULL) {
222 		WARNING(("idn__delimitermap_create: malloc failed\n"));
223 		r = idn_nomemory;
224 		goto ret;
225 	}
226 
227 	ctx->delimiters = (unsigned long *) malloc(sizeof(unsigned long)
228 		* DELIMITERMAP_INITIAL_DELIMITER_SIZE);
229 	if (ctx->delimiters == NULL) {
230 		r = idn_nomemory;
231 		goto ret;
232 	}
233 	ctx->ndelimiters = 0;
234 	ctx->delimiter_size = DELIMITERMAP_INITIAL_DELIMITER_SIZE;
235 	*ctxp = ctx;
236 
237 ret:
238 	if (r != idn_success)
239 		free(ctx);
240 	TRACE(("idn__delimitermap_create(): %s\n", idn_result_tostring(r)));
241 	return (r);
242 }
243 
244 /*
245  * Release all the memory allocated to the delimitermap object.
246  */
247 void
idn__delimitermap_destroy(idn__delimitermap_t ctx)248 idn__delimitermap_destroy(idn__delimitermap_t ctx) {
249 	assert(ctx != NULL);
250 
251 	TRACE(("idn__delimitermap_destroy()\n"));
252 
253 	TRACE(("idn__delimitermap_destroy(): the object is destroyed\n"));
254 	free(ctx->delimiters);
255 	free(ctx);
256 }
257 
258 /*
259  * Add a delimiter.
260  */
261 idn_result_t
idn__delimitermap_add(idn__delimitermap_t ctx,unsigned long delimiter)262 idn__delimitermap_add(idn__delimitermap_t ctx, unsigned long delimiter) {
263 	idn_result_t r = idn_success;
264 
265 	assert(ctx != NULL && ctx->ndelimiters <= ctx->delimiter_size);
266 	TRACE(("idn__delimitermap_add(delimiter=\\x%04lx)\n", delimiter));
267 
268 	if (delimiter == 0 || delimiter > UTF32_MAX || IS_SURROGATE(delimiter)) {
269 		r = idn_invalid_codepoint;
270 		goto ret;
271 	}
272 
273 	if (ctx->ndelimiters == ctx->delimiter_size) {
274 		unsigned long *new_delimiters;
275 
276 		new_delimiters = (unsigned long *) realloc(ctx->delimiters,
277 			sizeof(unsigned long) * ctx->delimiter_size * 2);
278 		if (new_delimiters == NULL) {
279 			r = idn_nomemory;
280 			goto ret;
281 		}
282 		ctx->delimiters = new_delimiters;
283 		ctx->delimiter_size *= 2;
284 	}
285 
286 	ctx->delimiters[ctx->ndelimiters] = delimiter;
287 	ctx->ndelimiters++;
288 
289 ret:
290 	TRACE(("idn__delimitermap_add(): %s\n", idn_result_tostring(r)));
291 	return (r);
292 }
293 
294 /*
295  * Add delimiters at once.
296  */
297 idn_result_t
idn__delimitermap_addall(idn__delimitermap_t ctx,unsigned long * delimiters,int ndelimiters)298 idn__delimitermap_addall(idn__delimitermap_t ctx, unsigned long *delimiters,
299 			 int ndelimiters) {
300 	idn_result_t r = idn_success;
301 	int i;
302 
303 	assert(ctx != NULL && delimiters != NULL);
304 
305 	TRACE(("idn__delimitermap_addall(ndelimiters=%d)\n", ndelimiters));
306 
307 	for (i = 0; i < ndelimiters; i++) {
308 		r = idn__delimitermap_add(ctx, *delimiters);
309 		if (r != idn_success)
310 			goto ret;
311 		delimiters++;
312 	}
313 
314 ret:
315 	TRACE(("idn__delimitermap_addall(): %s\n", idn_result_tostring(r)));
316 	return (r);
317 }
318 
319 /*
320  * Clear delimiters registered in 'ctx'.
321  */
322 void
idn__delimitermap_clear(idn__delimitermap_t ctx)323 idn__delimitermap_clear(idn__delimitermap_t ctx) {
324 	assert(ctx != NULL);
325 
326 	TRACE(("idn__delimitermap_clear()\n"));
327 	ctx->ndelimiters = 0;
328 	TRACE(("idn__delimitermap_clear(): success\n"));
329 }
330 
331 /*
332  * Map local delimiters in 'from' to '.' (U+002E; FULL STOP).
333  */
334 idn_result_t
idn__delimitermap_map(idn__delimitermap_t ctx,const unsigned long * from,unsigned long * to,size_t tolen)335 idn__delimitermap_map(idn__delimitermap_t ctx, const unsigned long *from,
336 		      unsigned long *to, size_t tolen) {
337 	idn_result_t r = idn_success;
338 	unsigned long *to_org = to;
339 	int i;
340 	int found;
341 
342 	assert(ctx != NULL && from != NULL && to != NULL);
343 
344 	TRACE(("idn__delimitermap_map(from=\"%s\", tolen=%d)\n",
345 		idn__debug_utf32xstring(from), (int)tolen));
346 
347 	/*
348 	 * Map.
349 	 */
350 	while (*from != '\0') {
351 		found = 0;
352 		if (tolen <= 0) {
353 			r = idn_buffer_overflow;
354 			goto ret;
355 		}
356 		for (i = 0; i < ctx->ndelimiters; i++) {
357 			if (ctx->delimiters[i] == *from) {
358 				found = 1;
359 				break;
360 			}
361 		}
362 		if (found)
363 			*to = '.';
364 		else
365 			*to = *from;
366 		from++;
367 		to++;
368 		tolen--;
369 	}
370 
371 	if (tolen <= 0) {
372 		r = idn_buffer_overflow;
373 		goto ret;
374 	}
375 	*to = '\0';
376 
377 ret:
378 	if (r == idn_success) {
379 		TRACE(("idn__delimitermap_map(): success (to=\"%s\")\n",
380 		       idn__debug_utf32xstring(to_org)));
381 	} else {
382 		TRACE(("idn__delimitermap_map(): %s\n",
383 		       idn_result_tostring(r)));
384 	}
385 	return (r);
386 }
387 
388 idn_result_t
idn__delimitermap_mapproc(void * ctx,const unsigned long * from,unsigned long * to,size_t tolen)389 idn__delimitermap_mapproc(void *ctx, const unsigned long *from,
390 			  unsigned long *to, size_t tolen) {
391 	idn_result_t r = idn_success;
392 
393 	r = idn__delimitermap_map((idn__delimitermap_t)ctx, from, to, tolen);
394 	return (r);
395 }
396 
397 void
idn__delimitermap_destroyproc(void * ctx)398 idn__delimitermap_destroyproc(void *ctx) {
399 	idn__delimitermap_destroy((idn__delimitermap_t)ctx);
400 }
401