sparc: remove ceil, floor, trunc sparc specific implementations
[glibc.git] / libidn / stringprep.c
blob72a502e5a3f632bcdad93aa36f61a38e497a3920
1 /* stringprep.c --- Core stringprep implementation.
2 * Copyright (C) 2002, 2003, 2004 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * GNU Libidn is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU Libidn; if not, see <http://www.gnu.org/licenses/>.
20 #if HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdint.h>
28 #include "stringprep.h"
30 static ssize_t
31 stringprep_find_character_in_table (uint32_t ucs4,
32 const Stringprep_table_element * table)
34 ssize_t i;
36 /* This is where typical uses of Libidn spends very close to all CPU
37 time and causes most cache misses. One could easily do a binary
38 search instead. Before rewriting this, I want hard evidence this
39 slowness is at all relevant in typical applications. (I don't
40 dispute optimization may improve matters significantly, I'm
41 mostly interested in having someone give real-world benchmark on
42 the impact of libidn.) */
44 for (i = 0; table[i].start || table[i].end; i++)
45 if (ucs4 >= table[i].start &&
46 ucs4 <= (table[i].end ? table[i].end : table[i].start))
47 return i;
49 return -1;
52 static ssize_t
53 stringprep_find_string_in_table (uint32_t * ucs4,
54 size_t ucs4len,
55 size_t * tablepos,
56 const Stringprep_table_element * table)
58 size_t j;
59 ssize_t pos;
61 for (j = 0; j < ucs4len; j++)
62 if ((pos = stringprep_find_character_in_table (ucs4[j], table)) != -1)
64 if (tablepos)
65 *tablepos = pos;
66 return j;
69 return -1;
72 static int
73 stringprep_apply_table_to_string (uint32_t * ucs4,
74 size_t * ucs4len,
75 size_t maxucs4len,
76 const Stringprep_table_element * table)
78 ssize_t pos;
79 size_t i, maplen;
81 while ((pos = stringprep_find_string_in_table (ucs4, *ucs4len,
82 &i, table)) != -1)
84 for (maplen = STRINGPREP_MAX_MAP_CHARS;
85 maplen > 0 && table[i].map[maplen - 1] == 0; maplen--)
88 if (*ucs4len - 1 + maplen >= maxucs4len)
89 return STRINGPREP_TOO_SMALL_BUFFER;
91 memmove (&ucs4[pos + maplen], &ucs4[pos + 1],
92 sizeof (uint32_t) * (*ucs4len - pos - 1));
93 memcpy (&ucs4[pos], table[i].map, sizeof (uint32_t) * maplen);
94 *ucs4len = *ucs4len - 1 + maplen;
97 return STRINGPREP_OK;
100 #define INVERTED(x) ((x) & ((~0UL) >> 1))
101 #define UNAPPLICAPLEFLAGS(flags, profileflags) \
102 ((!INVERTED(profileflags) && !(profileflags & flags) && profileflags) || \
103 ( INVERTED(profileflags) && (profileflags & flags)))
106 * stringprep_4i:
107 * @ucs4: input/output array with string to prepare.
108 * @len: on input, length of input array with Unicode code points,
109 * on exit, length of output array with Unicode code points.
110 * @maxucs4len: maximum length of input/output array.
111 * @flags: stringprep profile flags, or 0.
112 * @profile: pointer to stringprep profile to use.
114 * Prepare the input UCS-4 string according to the stringprep profile,
115 * and write back the result to the input string.
117 * The input is not required to be zero terminated (@ucs4[@len] = 0).
118 * The output will not be zero terminated unless @ucs4[@len] = 0.
119 * Instead, see stringprep_4zi() if your input is zero terminated or
120 * if you want the output to be.
122 * Since the stringprep operation can expand the string, @maxucs4len
123 * indicate how large the buffer holding the string is. This function
124 * will not read or write to code points outside that size.
126 * The @flags are one of Stringprep_profile_flags, or 0.
128 * The @profile contain the instructions to perform. Your application
129 * can define new profiles, possibly re-using the generic stringprep
130 * tables that always will be part of the library, or use one of the
131 * currently supported profiles.
133 * Return value: Returns %STRINGPREP_OK iff successful, or an error code.
136 stringprep_4i (uint32_t * ucs4, size_t * len, size_t maxucs4len,
137 Stringprep_profile_flags flags,
138 const Stringprep_profile * profile)
140 size_t i, j;
141 ssize_t k;
142 size_t ucs4len = *len;
143 int rc;
145 for (i = 0; profile[i].operation; i++)
147 switch (profile[i].operation)
149 case STRINGPREP_NFKC:
151 uint32_t *q = 0;
153 if (UNAPPLICAPLEFLAGS (flags, profile[i].flags))
154 break;
156 if (flags & STRINGPREP_NO_NFKC && !profile[i].flags)
157 /* Profile requires NFKC, but callee asked for no NFKC. */
158 return STRINGPREP_FLAG_ERROR;
160 q = stringprep_ucs4_nfkc_normalize (ucs4, ucs4len);
161 if (!q)
162 return STRINGPREP_NFKC_FAILED;
164 for (ucs4len = 0; q[ucs4len]; ucs4len++)
167 if (ucs4len >= maxucs4len)
169 free (q);
170 return STRINGPREP_TOO_SMALL_BUFFER;
173 memcpy (ucs4, q, ucs4len * sizeof (ucs4[0]));
175 free (q);
177 break;
179 case STRINGPREP_PROHIBIT_TABLE:
180 k = stringprep_find_string_in_table (ucs4, ucs4len,
181 NULL, profile[i].table);
182 if (k != -1)
183 return STRINGPREP_CONTAINS_PROHIBITED;
184 break;
186 case STRINGPREP_UNASSIGNED_TABLE:
187 if (UNAPPLICAPLEFLAGS (flags, profile[i].flags))
188 break;
189 if (flags & STRINGPREP_NO_UNASSIGNED)
191 k = stringprep_find_string_in_table
192 (ucs4, ucs4len, NULL, profile[i].table);
193 if (k != -1)
194 return STRINGPREP_CONTAINS_UNASSIGNED;
196 break;
198 case STRINGPREP_MAP_TABLE:
199 if (UNAPPLICAPLEFLAGS (flags, profile[i].flags))
200 break;
201 rc = stringprep_apply_table_to_string
202 (ucs4, &ucs4len, maxucs4len, profile[i].table);
203 if (rc != STRINGPREP_OK)
204 return rc;
205 break;
207 case STRINGPREP_BIDI_PROHIBIT_TABLE:
208 case STRINGPREP_BIDI_RAL_TABLE:
209 case STRINGPREP_BIDI_L_TABLE:
210 break;
212 case STRINGPREP_BIDI:
214 int done_prohibited = 0;
215 int done_ral = 0;
216 int done_l = 0;
217 int contains_ral = -1;
218 int contains_l = -1;
220 for (j = 0; profile[j].operation; j++)
221 if (profile[j].operation == STRINGPREP_BIDI_PROHIBIT_TABLE)
223 done_prohibited = 1;
224 k = stringprep_find_string_in_table (ucs4, ucs4len,
225 NULL,
226 profile[j].table);
227 if (k != -1)
228 return STRINGPREP_BIDI_CONTAINS_PROHIBITED;
230 else if (profile[j].operation == STRINGPREP_BIDI_RAL_TABLE)
232 done_ral = 1;
233 if (stringprep_find_string_in_table
234 (ucs4, ucs4len, NULL, profile[j].table) != -1)
235 contains_ral = j;
237 else if (profile[j].operation == STRINGPREP_BIDI_L_TABLE)
239 done_l = 1;
240 if (stringprep_find_string_in_table
241 (ucs4, ucs4len, NULL, profile[j].table) != -1)
242 contains_l = j;
245 if (!done_prohibited || !done_ral || !done_l)
246 return STRINGPREP_PROFILE_ERROR;
248 if (contains_ral != -1 && contains_l != -1)
249 return STRINGPREP_BIDI_BOTH_L_AND_RAL;
251 if (contains_ral != -1)
253 if (!(stringprep_find_character_in_table
254 (ucs4[0], profile[contains_ral].table) != -1 &&
255 stringprep_find_character_in_table
256 (ucs4[ucs4len - 1], profile[contains_ral].table) != -1))
257 return STRINGPREP_BIDI_LEADTRAIL_NOT_RAL;
260 break;
262 default:
263 return STRINGPREP_PROFILE_ERROR;
264 break;
268 *len = ucs4len;
270 return STRINGPREP_OK;
273 static int
274 stringprep_4zi_1 (uint32_t * ucs4, size_t ucs4len, size_t maxucs4len,
275 Stringprep_profile_flags flags,
276 const Stringprep_profile * profile)
278 int rc;
280 rc = stringprep_4i (ucs4, &ucs4len, maxucs4len, flags, profile);
281 if (rc != STRINGPREP_OK)
282 return rc;
284 if (ucs4len >= maxucs4len)
285 return STRINGPREP_TOO_SMALL_BUFFER;
287 ucs4[ucs4len] = 0;
289 return STRINGPREP_OK;
293 * stringprep_4zi:
294 * @ucs4: input/output array with zero terminated string to prepare.
295 * @maxucs4len: maximum length of input/output array.
296 * @flags: stringprep profile flags, or 0.
297 * @profile: pointer to stringprep profile to use.
299 * Prepare the input zero terminated UCS-4 string according to the
300 * stringprep profile, and write back the result to the input string.
302 * Since the stringprep operation can expand the string, @maxucs4len
303 * indicate how large the buffer holding the string is. This function
304 * will not read or write to code points outside that size.
306 * The @flags are one of Stringprep_profile_flags, or 0.
308 * The @profile contain the instructions to perform. Your application
309 * can define new profiles, possibly re-using the generic stringprep
310 * tables that always will be part of the library, or use one of the
311 * currently supported profiles.
313 * Return value: Returns %STRINGPREP_OK iff successful, or an error code.
316 stringprep_4zi (uint32_t * ucs4, size_t maxucs4len,
317 Stringprep_profile_flags flags,
318 const Stringprep_profile * profile)
320 size_t ucs4len;
322 for (ucs4len = 0; ucs4len < maxucs4len && ucs4[ucs4len] != 0; ucs4len++)
325 return stringprep_4zi_1 (ucs4, ucs4len, maxucs4len, flags, profile);
329 * stringprep:
330 * @in: input/ouput array with string to prepare.
331 * @maxlen: maximum length of input/output array.
332 * @flags: stringprep profile flags, or 0.
333 * @profile: pointer to stringprep profile to use.
335 * Prepare the input zero terminated UTF-8 string according to the
336 * stringprep profile, and write back the result to the input string.
338 * Note that you must convert strings entered in the systems locale
339 * into UTF-8 before using this function, see
340 * stringprep_locale_to_utf8().
342 * Since the stringprep operation can expand the string, @maxlen
343 * indicate how large the buffer holding the string is. This function
344 * will not read or write to characters outside that size.
346 * The @flags are one of Stringprep_profile_flags, or 0.
348 * The @profile contain the instructions to perform. Your application
349 * can define new profiles, possibly re-using the generic stringprep
350 * tables that always will be part of the library, or use one of the
351 * currently supported profiles.
353 * Return value: Returns %STRINGPREP_OK iff successful, or an error code.
356 stringprep (char *in,
357 size_t maxlen,
358 Stringprep_profile_flags flags,
359 const Stringprep_profile * profile)
361 int rc;
362 char *utf8 = NULL;
363 uint32_t *ucs4 = NULL;
364 size_t ucs4len, maxucs4len, adducs4len = 50;
368 free (ucs4);
369 ucs4 = stringprep_utf8_to_ucs4 (in, -1, &ucs4len);
370 maxucs4len = ucs4len + adducs4len;
371 uint32_t *newp = realloc (ucs4, maxucs4len * sizeof (uint32_t));
372 if (!newp)
374 free (ucs4);
375 return STRINGPREP_MALLOC_ERROR;
377 ucs4 = newp;
379 rc = stringprep_4i (ucs4, &ucs4len, maxucs4len, flags, profile);
380 adducs4len += 50;
382 while (rc == STRINGPREP_TOO_SMALL_BUFFER);
383 if (rc != STRINGPREP_OK)
385 free (ucs4);
386 return rc;
389 utf8 = stringprep_ucs4_to_utf8 (ucs4, ucs4len, 0, 0);
390 free (ucs4);
391 if (!utf8)
392 return STRINGPREP_MALLOC_ERROR;
394 if (strlen (utf8) >= maxlen)
396 free (utf8);
397 return STRINGPREP_TOO_SMALL_BUFFER;
400 strcpy (in, utf8); /* flawfinder: ignore */
402 free (utf8);
404 return STRINGPREP_OK;
408 * stringprep_profile:
409 * @in: input array with UTF-8 string to prepare.
410 * @out: output variable with pointer to newly allocate string.
411 * @profile: name of stringprep profile to use.
412 * @flags: stringprep profile flags, or 0.
414 * Prepare the input zero terminated UTF-8 string according to the
415 * stringprep profile, and return the result in a newly allocated
416 * variable.
418 * Note that you must convert strings entered in the systems locale
419 * into UTF-8 before using this function, see
420 * stringprep_locale_to_utf8().
422 * The output @out variable must be deallocated by the caller.
424 * The @flags are one of Stringprep_profile_flags, or 0.
426 * The @profile specifies the name of the stringprep profile to use.
427 * It must be one of the internally supported stringprep profiles.
429 * Return value: Returns %STRINGPREP_OK iff successful, or an error code.
432 stringprep_profile (const char *in,
433 char **out,
434 const char *profile, Stringprep_profile_flags flags)
436 const Stringprep_profiles *p;
437 char *str = NULL;
438 size_t len = strlen (in) + 1;
439 int rc;
441 for (p = &stringprep_profiles[0]; p->name; p++)
442 if (strcmp (p->name, profile) == 0)
443 break;
445 if (!p || !p->name || !p->tables)
446 return STRINGPREP_UNKNOWN_PROFILE;
450 free (str);
451 str = (char *) malloc (len);
452 if (str == NULL)
453 return STRINGPREP_MALLOC_ERROR;
455 strcpy (str, in);
457 rc = stringprep (str, len, flags, p->tables);
458 len += 50;
460 while (rc == STRINGPREP_TOO_SMALL_BUFFER);
462 if (rc == STRINGPREP_OK)
463 *out = str;
464 else
465 free (str);
467 return rc;
470 /*! \mainpage GNU Internationalized Domain Name Library
472 * \section intro Introduction
474 * GNU Libidn is an implementation of the Stringprep, Punycode and IDNA
475 * specifications defined by the IETF Internationalized Domain Names
476 * (IDN) working group, used for internationalized domain names. The
477 * package is available under the GNU Lesser General Public License.
479 * The library contains a generic Stringprep implementation that does
480 * Unicode 3.2 NFKC normalization, mapping and prohibitation of
481 * characters, and bidirectional character handling. Profiles for
482 * Nameprep, iSCSI, SASL and XMPP are included. Punycode and ASCII
483 * Compatible Encoding (ACE) via IDNA are supported. A mechanism to
484 * define Top-Level Domain (TLD) specific validation tables, and to
485 * compare strings against those tables, is included. Default tables
486 * for some TLDs are also included.
488 * The Stringprep API consists of two main functions, one for
489 * converting data from the system's native representation into UTF-8,
490 * and one function to perform the Stringprep processing. Adding a
491 * new Stringprep profile for your application within the API is
492 * straightforward. The Punycode API consists of one encoding
493 * function and one decoding function. The IDNA API consists of the
494 * ToASCII and ToUnicode functions, as well as an high-level interface
495 * for converting entire domain names to and from the ACE encoded
496 * form. The TLD API consists of one set of functions to extract the
497 * TLD name from a domain string, one set of functions to locate the
498 * proper TLD table to use based on the TLD name, and core functions
499 * to validate a string against a TLD table, and some utility wrappers
500 * to perform all the steps in one call.
502 * The library is used by, e.g., GNU SASL and Shishi to process user
503 * names and passwords. Libidn can be built into GNU Libc to enable a
504 * new system-wide getaddrinfo() flag for IDN processing.
506 * Libidn is developed for the GNU/Linux system, but runs on over 20 Unix
507 * platforms (including Solaris, IRIX, AIX, and Tru64) and Windows.
508 * Libidn is written in C and (parts of) the API is accessible from C,
509 * C++, Emacs Lisp, Python and Java.
511 * The project web page:\n
512 * http://www.gnu.org/software/libidn/
514 * The software archive:\n
515 * ftp://alpha.gnu.org/pub/gnu/libidn/
517 * For more information see:\n
518 * http://www.ietf.org/html.charters/idn-charter.html\n
519 * http://www.ietf.org/rfc/rfc3454.txt (stringprep specification)\n
520 * http://www.ietf.org/rfc/rfc3490.txt (idna specification)\n
521 * http://www.ietf.org/rfc/rfc3491.txt (nameprep specification)\n
522 * http://www.ietf.org/rfc/rfc3492.txt (punycode specification)\n
523 * http://www.ietf.org/internet-drafts/draft-ietf-ips-iscsi-string-prep-04.txt\n
524 * http://www.ietf.org/internet-drafts/draft-ietf-krb-wg-utf8-profile-01.txt\n
525 * http://www.ietf.org/internet-drafts/draft-ietf-sasl-anon-00.txt\n
526 * http://www.ietf.org/internet-drafts/draft-ietf-sasl-saslprep-00.txt\n
527 * http://www.ietf.org/internet-drafts/draft-ietf-xmpp-nodeprep-01.txt\n
528 * http://www.ietf.org/internet-drafts/draft-ietf-xmpp-resourceprep-01.txt\n
530 * Further information and paid contract development:\n
531 * Simon Josefsson <simon@josefsson.org>
533 * \section examples Examples
535 * \include example.c
536 * \include example3.c
537 * \include example4.c
538 * \include example5.c
542 * STRINGPREP_VERSION
544 * String defined via CPP denoting the header file version number.
545 * Used together with stringprep_check_version() to verify header file
546 * and run-time library consistency.
550 * STRINGPREP_MAX_MAP_CHARS
552 * Maximum number of code points that can replace a single code point,
553 * during stringprep mapping.
557 * Stringprep_rc:
558 * @STRINGPREP_OK: Successful operation. This value is guaranteed to
559 * always be zero, the remaining ones are only guaranteed to hold
560 * non-zero values, for logical comparison purposes.
561 * @STRINGPREP_CONTAINS_UNASSIGNED: String contain unassigned Unicode
562 * code points, which is forbidden by the profile.
563 * @STRINGPREP_CONTAINS_PROHIBITED: String contain code points
564 * prohibited by the profile.
565 * @STRINGPREP_BIDI_BOTH_L_AND_RAL: String contain code points with
566 * conflicting bidirectional category.
567 * @STRINGPREP_BIDI_LEADTRAIL_NOT_RAL: Leading and trailing character
568 * in string not of proper bidirectional category.
569 * @STRINGPREP_BIDI_CONTAINS_PROHIBITED: Contains prohibited code
570 * points detected by bidirectional code.
571 * @STRINGPREP_TOO_SMALL_BUFFER: Buffer handed to function was too
572 * small. This usually indicate a problem in the calling
573 * application.
574 * @STRINGPREP_PROFILE_ERROR: The stringprep profile was inconsistent.
575 * This usually indicate an internal error in the library.
576 * @STRINGPREP_FLAG_ERROR: The supplied flag conflicted with profile.
577 * This usually indicate a problem in the calling application.
578 * @STRINGPREP_UNKNOWN_PROFILE: The supplied profile name was not
579 * known to the library.
580 * @STRINGPREP_NFKC_FAILED: The Unicode NFKC operation failed. This
581 * usually indicate an internal error in the library.
582 * @STRINGPREP_MALLOC_ERROR: The malloc() was out of memory. This is
583 * usually a fatal error.
585 * Enumerated return codes of stringprep(), stringprep_profile()
586 * functions (and macros using those functions). The value 0 is
587 * guaranteed to always correspond to success.
591 * Stringprep_profile_flags:
592 * @STRINGPREP_NO_NFKC: Disable the NFKC normalization, as well as
593 * selecting the non-NFKC case folding tables. Usually the profile
594 * specifies BIDI and NFKC settings, and applications should not
595 * override it unless in special situations.
596 * @STRINGPREP_NO_BIDI: Disable the BIDI step. Usually the profile
597 * specifies BIDI and NFKC settings, and applications should not
598 * override it unless in special situations.
599 * @STRINGPREP_NO_UNASSIGNED: Make the library return with an error if
600 * string contains unassigned characters according to profile.
602 * Stringprep profile flags.
606 * Stringprep_profile_steps:
608 * Various steps in the stringprep algorithm. You really want to
609 * study the source code to understand this one. Only useful if you
610 * want to add another profile.
614 * stringprep_nameprep:
615 * @in: input/ouput array with string to prepare.
616 * @maxlen: maximum length of input/output array.
618 * Prepare the input UTF-8 string according to the nameprep profile.
619 * The AllowUnassigned flag is true, use
620 * stringprep_nameprep_no_unassigned() if you want a false
621 * AllowUnassigned. Returns 0 iff successful, or an error code.
625 * stringprep_nameprep_no_unassigned:
626 * @in: input/ouput array with string to prepare.
627 * @maxlen: maximum length of input/output array.
629 * Prepare the input UTF-8 string according to the nameprep profile.
630 * The AllowUnassigned flag is false, use stringprep_nameprep() for
631 * true AllowUnassigned. Returns 0 iff successful, or an error code.
635 * stringprep_iscsi:
636 * @in: input/ouput array with string to prepare.
637 * @maxlen: maximum length of input/output array.
639 * Prepare the input UTF-8 string according to the draft iSCSI
640 * stringprep profile. Returns 0 iff successful, or an error code.
644 * stringprep_plain:
645 * @in: input/ouput array with string to prepare.
646 * @maxlen: maximum length of input/output array.
648 * Prepare the input UTF-8 string according to the draft SASL
649 * ANONYMOUS profile. Returns 0 iff successful, or an error code.
653 * stringprep_xmpp_nodeprep:
654 * @in: input/ouput array with string to prepare.
655 * @maxlen: maximum length of input/output array.
657 * Prepare the input UTF-8 string according to the draft XMPP node
658 * identifier profile. Returns 0 iff successful, or an error code.
662 * stringprep_xmpp_resourceprep:
663 * @in: input/ouput array with string to prepare.
664 * @maxlen: maximum length of input/output array.
666 * Prepare the input UTF-8 string according to the draft XMPP resource
667 * identifier profile. Returns 0 iff successful, or an error code.