Framework to test IFUNC implementations on target
[glibc.git] / libidn / stringprep.c
blob201489f36aed2b04248434ca782c18de225169ec
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>
27 #include "stringprep.h"
29 static ssize_t
30 stringprep_find_character_in_table (uint32_t ucs4,
31 const Stringprep_table_element * table)
33 ssize_t i;
35 /* This is where typical uses of Libidn spends very close to all CPU
36 time and causes most cache misses. One could easily do a binary
37 search instead. Before rewriting this, I want hard evidence this
38 slowness is at all relevant in typical applications. (I don't
39 dispute optimization may improve matters significantly, I'm
40 mostly interested in having someone give real-world benchmark on
41 the impact of libidn.) */
43 for (i = 0; table[i].start || table[i].end; i++)
44 if (ucs4 >= table[i].start &&
45 ucs4 <= (table[i].end ? table[i].end : table[i].start))
46 return i;
48 return -1;
51 static ssize_t
52 stringprep_find_string_in_table (uint32_t * ucs4,
53 size_t ucs4len,
54 size_t * tablepos,
55 const Stringprep_table_element * table)
57 size_t j;
58 ssize_t pos;
60 for (j = 0; j < ucs4len; j++)
61 if ((pos = stringprep_find_character_in_table (ucs4[j], table)) != -1)
63 if (tablepos)
64 *tablepos = pos;
65 return j;
68 return -1;
71 static int
72 stringprep_apply_table_to_string (uint32_t * ucs4,
73 size_t * ucs4len,
74 size_t maxucs4len,
75 const Stringprep_table_element * table)
77 ssize_t pos;
78 size_t i, maplen;
80 while ((pos = stringprep_find_string_in_table (ucs4, *ucs4len,
81 &i, table)) != -1)
83 for (maplen = STRINGPREP_MAX_MAP_CHARS;
84 maplen > 0 && table[i].map[maplen - 1] == 0; maplen--)
87 if (*ucs4len - 1 + maplen >= maxucs4len)
88 return STRINGPREP_TOO_SMALL_BUFFER;
90 memmove (&ucs4[pos + maplen], &ucs4[pos + 1],
91 sizeof (uint32_t) * (*ucs4len - pos - 1));
92 memcpy (&ucs4[pos], table[i].map, sizeof (uint32_t) * maplen);
93 *ucs4len = *ucs4len - 1 + maplen;
96 return STRINGPREP_OK;
99 #define INVERTED(x) ((x) & ((~0UL) >> 1))
100 #define UNAPPLICAPLEFLAGS(flags, profileflags) \
101 ((!INVERTED(profileflags) && !(profileflags & flags) && profileflags) || \
102 ( INVERTED(profileflags) && (profileflags & flags)))
105 * stringprep_4i:
106 * @ucs4: input/output array with string to prepare.
107 * @len: on input, length of input array with Unicode code points,
108 * on exit, length of output array with Unicode code points.
109 * @maxucs4len: maximum length of input/output array.
110 * @flags: stringprep profile flags, or 0.
111 * @profile: pointer to stringprep profile to use.
113 * Prepare the input UCS-4 string according to the stringprep profile,
114 * and write back the result to the input string.
116 * The input is not required to be zero terminated (@ucs4[@len] = 0).
117 * The output will not be zero terminated unless @ucs4[@len] = 0.
118 * Instead, see stringprep_4zi() if your input is zero terminated or
119 * if you want the output to be.
121 * Since the stringprep operation can expand the string, @maxucs4len
122 * indicate how large the buffer holding the string is. This function
123 * will not read or write to code points outside that size.
125 * The @flags are one of Stringprep_profile_flags, or 0.
127 * The @profile contain the instructions to perform. Your application
128 * can define new profiles, possibly re-using the generic stringprep
129 * tables that always will be part of the library, or use one of the
130 * currently supported profiles.
132 * Return value: Returns %STRINGPREP_OK iff successful, or an error code.
135 stringprep_4i (uint32_t * ucs4, size_t * len, size_t maxucs4len,
136 Stringprep_profile_flags flags,
137 const Stringprep_profile * profile)
139 size_t i, j;
140 ssize_t k;
141 size_t ucs4len = *len;
142 int rc;
144 for (i = 0; profile[i].operation; i++)
146 switch (profile[i].operation)
148 case STRINGPREP_NFKC:
150 uint32_t *q = 0;
152 if (UNAPPLICAPLEFLAGS (flags, profile[i].flags))
153 break;
155 if (flags & STRINGPREP_NO_NFKC && !profile[i].flags)
156 /* Profile requires NFKC, but callee asked for no NFKC. */
157 return STRINGPREP_FLAG_ERROR;
159 q = stringprep_ucs4_nfkc_normalize (ucs4, ucs4len);
160 if (!q)
161 return STRINGPREP_NFKC_FAILED;
163 for (ucs4len = 0; q[ucs4len]; ucs4len++)
166 if (ucs4len >= maxucs4len)
168 free (q);
169 return STRINGPREP_TOO_SMALL_BUFFER;
172 memcpy (ucs4, q, ucs4len * sizeof (ucs4[0]));
174 free (q);
176 break;
178 case STRINGPREP_PROHIBIT_TABLE:
179 k = stringprep_find_string_in_table (ucs4, ucs4len,
180 NULL, profile[i].table);
181 if (k != -1)
182 return STRINGPREP_CONTAINS_PROHIBITED;
183 break;
185 case STRINGPREP_UNASSIGNED_TABLE:
186 if (UNAPPLICAPLEFLAGS (flags, profile[i].flags))
187 break;
188 if (flags & STRINGPREP_NO_UNASSIGNED)
190 k = stringprep_find_string_in_table
191 (ucs4, ucs4len, NULL, profile[i].table);
192 if (k != -1)
193 return STRINGPREP_CONTAINS_UNASSIGNED;
195 break;
197 case STRINGPREP_MAP_TABLE:
198 if (UNAPPLICAPLEFLAGS (flags, profile[i].flags))
199 break;
200 rc = stringprep_apply_table_to_string
201 (ucs4, &ucs4len, maxucs4len, profile[i].table);
202 if (rc != STRINGPREP_OK)
203 return rc;
204 break;
206 case STRINGPREP_BIDI_PROHIBIT_TABLE:
207 case STRINGPREP_BIDI_RAL_TABLE:
208 case STRINGPREP_BIDI_L_TABLE:
209 break;
211 case STRINGPREP_BIDI:
213 int done_prohibited = 0;
214 int done_ral = 0;
215 int done_l = 0;
216 int contains_ral = -1;
217 int contains_l = -1;
219 for (j = 0; profile[j].operation; j++)
220 if (profile[j].operation == STRINGPREP_BIDI_PROHIBIT_TABLE)
222 done_prohibited = 1;
223 k = stringprep_find_string_in_table (ucs4, ucs4len,
224 NULL,
225 profile[j].table);
226 if (k != -1)
227 return STRINGPREP_BIDI_CONTAINS_PROHIBITED;
229 else if (profile[j].operation == STRINGPREP_BIDI_RAL_TABLE)
231 done_ral = 1;
232 if (stringprep_find_string_in_table
233 (ucs4, ucs4len, NULL, profile[j].table) != -1)
234 contains_ral = j;
236 else if (profile[j].operation == STRINGPREP_BIDI_L_TABLE)
238 done_l = 1;
239 if (stringprep_find_string_in_table
240 (ucs4, ucs4len, NULL, profile[j].table) != -1)
241 contains_l = j;
244 if (!done_prohibited || !done_ral || !done_l)
245 return STRINGPREP_PROFILE_ERROR;
247 if (contains_ral != -1 && contains_l != -1)
248 return STRINGPREP_BIDI_BOTH_L_AND_RAL;
250 if (contains_ral != -1)
252 if (!(stringprep_find_character_in_table
253 (ucs4[0], profile[contains_ral].table) != -1 &&
254 stringprep_find_character_in_table
255 (ucs4[ucs4len - 1], profile[contains_ral].table) != -1))
256 return STRINGPREP_BIDI_LEADTRAIL_NOT_RAL;
259 break;
261 default:
262 return STRINGPREP_PROFILE_ERROR;
263 break;
267 *len = ucs4len;
269 return STRINGPREP_OK;
272 static int
273 stringprep_4zi_1 (uint32_t * ucs4, size_t ucs4len, size_t maxucs4len,
274 Stringprep_profile_flags flags,
275 const Stringprep_profile * profile)
277 int rc;
279 rc = stringprep_4i (ucs4, &ucs4len, maxucs4len, flags, profile);
280 if (rc != STRINGPREP_OK)
281 return rc;
283 if (ucs4len >= maxucs4len)
284 return STRINGPREP_TOO_SMALL_BUFFER;
286 ucs4[ucs4len] = 0;
288 return STRINGPREP_OK;
292 * stringprep_4zi:
293 * @ucs4: input/output array with zero terminated string to prepare.
294 * @maxucs4len: maximum length of input/output array.
295 * @flags: stringprep profile flags, or 0.
296 * @profile: pointer to stringprep profile to use.
298 * Prepare the input zero terminated UCS-4 string according to the
299 * stringprep profile, and write back the result to the input string.
301 * Since the stringprep operation can expand the string, @maxucs4len
302 * indicate how large the buffer holding the string is. This function
303 * will not read or write to code points outside that size.
305 * The @flags are one of Stringprep_profile_flags, or 0.
307 * The @profile contain the instructions to perform. Your application
308 * can define new profiles, possibly re-using the generic stringprep
309 * tables that always will be part of the library, or use one of the
310 * currently supported profiles.
312 * Return value: Returns %STRINGPREP_OK iff successful, or an error code.
315 stringprep_4zi (uint32_t * ucs4, size_t maxucs4len,
316 Stringprep_profile_flags flags,
317 const Stringprep_profile * profile)
319 size_t ucs4len;
321 for (ucs4len = 0; ucs4len < maxucs4len && ucs4[ucs4len] != 0; ucs4len++)
324 return stringprep_4zi_1 (ucs4, ucs4len, maxucs4len, flags, profile);
328 * stringprep:
329 * @in: input/ouput array with string to prepare.
330 * @maxlen: maximum length of input/output array.
331 * @flags: stringprep profile flags, or 0.
332 * @profile: pointer to stringprep profile to use.
334 * Prepare the input zero terminated UTF-8 string according to the
335 * stringprep profile, and write back the result to the input string.
337 * Note that you must convert strings entered in the systems locale
338 * into UTF-8 before using this function, see
339 * stringprep_locale_to_utf8().
341 * Since the stringprep operation can expand the string, @maxlen
342 * indicate how large the buffer holding the string is. This function
343 * will not read or write to characters outside that size.
345 * The @flags are one of Stringprep_profile_flags, or 0.
347 * The @profile contain the instructions to perform. Your application
348 * can define new profiles, possibly re-using the generic stringprep
349 * tables that always will be part of the library, or use one of the
350 * currently supported profiles.
352 * Return value: Returns %STRINGPREP_OK iff successful, or an error code.
355 stringprep (char *in,
356 size_t maxlen,
357 Stringprep_profile_flags flags,
358 const Stringprep_profile * profile)
360 int rc;
361 char *utf8 = NULL;
362 uint32_t *ucs4 = NULL;
363 size_t ucs4len, maxucs4len, adducs4len = 50;
367 free (ucs4);
368 ucs4 = stringprep_utf8_to_ucs4 (in, -1, &ucs4len);
369 maxucs4len = ucs4len + adducs4len;
370 uint32_t *newp = realloc (ucs4, maxucs4len * sizeof (uint32_t));
371 if (!newp)
373 free (ucs4);
374 return STRINGPREP_MALLOC_ERROR;
376 ucs4 = newp;
378 rc = stringprep_4i (ucs4, &ucs4len, maxucs4len, flags, profile);
379 adducs4len += 50;
381 while (rc == STRINGPREP_TOO_SMALL_BUFFER);
382 if (rc != STRINGPREP_OK)
384 free (ucs4);
385 return rc;
388 utf8 = stringprep_ucs4_to_utf8 (ucs4, ucs4len, 0, 0);
389 free (ucs4);
390 if (!utf8)
391 return STRINGPREP_MALLOC_ERROR;
393 if (strlen (utf8) >= maxlen)
395 free (utf8);
396 return STRINGPREP_TOO_SMALL_BUFFER;
399 strcpy (in, utf8); /* flawfinder: ignore */
401 free (utf8);
403 return STRINGPREP_OK;
407 * stringprep_profile:
408 * @in: input array with UTF-8 string to prepare.
409 * @out: output variable with pointer to newly allocate string.
410 * @profile: name of stringprep profile to use.
411 * @flags: stringprep profile flags, or 0.
413 * Prepare the input zero terminated UTF-8 string according to the
414 * stringprep profile, and return the result in a newly allocated
415 * variable.
417 * Note that you must convert strings entered in the systems locale
418 * into UTF-8 before using this function, see
419 * stringprep_locale_to_utf8().
421 * The output @out variable must be deallocated by the caller.
423 * The @flags are one of Stringprep_profile_flags, or 0.
425 * The @profile specifies the name of the stringprep profile to use.
426 * It must be one of the internally supported stringprep profiles.
428 * Return value: Returns %STRINGPREP_OK iff successful, or an error code.
431 stringprep_profile (const char *in,
432 char **out,
433 const char *profile, Stringprep_profile_flags flags)
435 const Stringprep_profiles *p;
436 char *str = NULL;
437 size_t len = strlen (in) + 1;
438 int rc;
440 for (p = &stringprep_profiles[0]; p->name; p++)
441 if (strcmp (p->name, profile) == 0)
442 break;
444 if (!p || !p->name || !p->tables)
445 return STRINGPREP_UNKNOWN_PROFILE;
449 free (str);
450 str = (char *) malloc (len);
451 if (str == NULL)
452 return STRINGPREP_MALLOC_ERROR;
454 strcpy (str, in);
456 rc = stringprep (str, len, flags, p->tables);
457 len += 50;
459 while (rc == STRINGPREP_TOO_SMALL_BUFFER);
461 if (rc == STRINGPREP_OK)
462 *out = str;
463 else
464 free (str);
466 return rc;
469 /*! \mainpage GNU Internationalized Domain Name Library
471 * \section intro Introduction
473 * GNU Libidn is an implementation of the Stringprep, Punycode and IDNA
474 * specifications defined by the IETF Internationalized Domain Names
475 * (IDN) working group, used for internationalized domain names. The
476 * package is available under the GNU Lesser General Public License.
478 * The library contains a generic Stringprep implementation that does
479 * Unicode 3.2 NFKC normalization, mapping and prohibitation of
480 * characters, and bidirectional character handling. Profiles for
481 * Nameprep, iSCSI, SASL and XMPP are included. Punycode and ASCII
482 * Compatible Encoding (ACE) via IDNA are supported. A mechanism to
483 * define Top-Level Domain (TLD) specific validation tables, and to
484 * compare strings against those tables, is included. Default tables
485 * for some TLDs are also included.
487 * The Stringprep API consists of two main functions, one for
488 * converting data from the system's native representation into UTF-8,
489 * and one function to perform the Stringprep processing. Adding a
490 * new Stringprep profile for your application within the API is
491 * straightforward. The Punycode API consists of one encoding
492 * function and one decoding function. The IDNA API consists of the
493 * ToASCII and ToUnicode functions, as well as an high-level interface
494 * for converting entire domain names to and from the ACE encoded
495 * form. The TLD API consists of one set of functions to extract the
496 * TLD name from a domain string, one set of functions to locate the
497 * proper TLD table to use based on the TLD name, and core functions
498 * to validate a string against a TLD table, and some utility wrappers
499 * to perform all the steps in one call.
501 * The library is used by, e.g., GNU SASL and Shishi to process user
502 * names and passwords. Libidn can be built into GNU Libc to enable a
503 * new system-wide getaddrinfo() flag for IDN processing.
505 * Libidn is developed for the GNU/Linux system, but runs on over 20 Unix
506 * platforms (including Solaris, IRIX, AIX, and Tru64) and Windows.
507 * Libidn is written in C and (parts of) the API is accessible from C,
508 * C++, Emacs Lisp, Python and Java.
510 * The project web page:\n
511 * http://www.gnu.org/software/libidn/
513 * The software archive:\n
514 * ftp://alpha.gnu.org/pub/gnu/libidn/
516 * For more information see:\n
517 * http://www.ietf.org/html.charters/idn-charter.html\n
518 * http://www.ietf.org/rfc/rfc3454.txt (stringprep specification)\n
519 * http://www.ietf.org/rfc/rfc3490.txt (idna specification)\n
520 * http://www.ietf.org/rfc/rfc3491.txt (nameprep specification)\n
521 * http://www.ietf.org/rfc/rfc3492.txt (punycode specification)\n
522 * http://www.ietf.org/internet-drafts/draft-ietf-ips-iscsi-string-prep-04.txt\n
523 * http://www.ietf.org/internet-drafts/draft-ietf-krb-wg-utf8-profile-01.txt\n
524 * http://www.ietf.org/internet-drafts/draft-ietf-sasl-anon-00.txt\n
525 * http://www.ietf.org/internet-drafts/draft-ietf-sasl-saslprep-00.txt\n
526 * http://www.ietf.org/internet-drafts/draft-ietf-xmpp-nodeprep-01.txt\n
527 * http://www.ietf.org/internet-drafts/draft-ietf-xmpp-resourceprep-01.txt\n
529 * Further information and paid contract development:\n
530 * Simon Josefsson <simon@josefsson.org>
532 * \section examples Examples
534 * \include example.c
535 * \include example3.c
536 * \include example4.c
537 * \include example5.c
541 * STRINGPREP_VERSION
543 * String defined via CPP denoting the header file version number.
544 * Used together with stringprep_check_version() to verify header file
545 * and run-time library consistency.
549 * STRINGPREP_MAX_MAP_CHARS
551 * Maximum number of code points that can replace a single code point,
552 * during stringprep mapping.
556 * Stringprep_rc:
557 * @STRINGPREP_OK: Successful operation. This value is guaranteed to
558 * always be zero, the remaining ones are only guaranteed to hold
559 * non-zero values, for logical comparison purposes.
560 * @STRINGPREP_CONTAINS_UNASSIGNED: String contain unassigned Unicode
561 * code points, which is forbidden by the profile.
562 * @STRINGPREP_CONTAINS_PROHIBITED: String contain code points
563 * prohibited by the profile.
564 * @STRINGPREP_BIDI_BOTH_L_AND_RAL: String contain code points with
565 * conflicting bidirection category.
566 * @STRINGPREP_BIDI_LEADTRAIL_NOT_RAL: Leading and trailing character
567 * in string not of proper bidirectional category.
568 * @STRINGPREP_BIDI_CONTAINS_PROHIBITED: Contains prohibited code
569 * points detected by bidirectional code.
570 * @STRINGPREP_TOO_SMALL_BUFFER: Buffer handed to function was too
571 * small. This usually indicate a problem in the calling
572 * application.
573 * @STRINGPREP_PROFILE_ERROR: The stringprep profile was inconsistent.
574 * This usually indicate an internal error in the library.
575 * @STRINGPREP_FLAG_ERROR: The supplied flag conflicted with profile.
576 * This usually indicate a problem in the calling application.
577 * @STRINGPREP_UNKNOWN_PROFILE: The supplied profile name was not
578 * known to the library.
579 * @STRINGPREP_NFKC_FAILED: The Unicode NFKC operation failed. This
580 * usually indicate an internal error in the library.
581 * @STRINGPREP_MALLOC_ERROR: The malloc() was out of memory. This is
582 * usually a fatal error.
584 * Enumerated return codes of stringprep(), stringprep_profile()
585 * functions (and macros using those functions). The value 0 is
586 * guaranteed to always correspond to success.
590 * Stringprep_profile_flags:
591 * @STRINGPREP_NO_NFKC: Disable the NFKC normalization, as well as
592 * selecting the non-NFKC case folding tables. Usually the profile
593 * specifies BIDI and NFKC settings, and applications should not
594 * override it unless in special situations.
595 * @STRINGPREP_NO_BIDI: Disable the BIDI step. Usually the profile
596 * specifies BIDI and NFKC settings, and applications should not
597 * override it unless in special situations.
598 * @STRINGPREP_NO_UNASSIGNED: Make the library return with an error if
599 * string contains unassigned characters according to profile.
601 * Stringprep profile flags.
605 * Stringprep_profile_steps:
607 * Various steps in the stringprep algorithm. You really want to
608 * study the source code to understand this one. Only useful if you
609 * want to add another profile.
613 * stringprep_nameprep:
614 * @in: input/ouput array with string to prepare.
615 * @maxlen: maximum length of input/output array.
617 * Prepare the input UTF-8 string according to the nameprep profile.
618 * The AllowUnassigned flag is true, use
619 * stringprep_nameprep_no_unassigned() if you want a false
620 * AllowUnassigned. Returns 0 iff successful, or an error code.
624 * stringprep_nameprep_no_unassigned:
625 * @in: input/ouput array with string to prepare.
626 * @maxlen: maximum length of input/output array.
628 * Prepare the input UTF-8 string according to the nameprep profile.
629 * The AllowUnassigned flag is false, use stringprep_nameprep() for
630 * true AllowUnassigned. Returns 0 iff successful, or an error code.
634 * stringprep_iscsi:
635 * @in: input/ouput array with string to prepare.
636 * @maxlen: maximum length of input/output array.
638 * Prepare the input UTF-8 string according to the draft iSCSI
639 * stringprep profile. Returns 0 iff successful, or an error code.
643 * stringprep_plain:
644 * @in: input/ouput array with string to prepare.
645 * @maxlen: maximum length of input/output array.
647 * Prepare the input UTF-8 string according to the draft SASL
648 * ANONYMOUS profile. Returns 0 iff successful, or an error code.
652 * stringprep_xmpp_nodeprep:
653 * @in: input/ouput array with string to prepare.
654 * @maxlen: maximum length of input/output array.
656 * Prepare the input UTF-8 string according to the draft XMPP node
657 * identifier profile. Returns 0 iff successful, or an error code.
661 * stringprep_xmpp_resourceprep:
662 * @in: input/ouput array with string to prepare.
663 * @maxlen: maximum length of input/output array.
665 * Prepare the input UTF-8 string according to the draft XMPP resource
666 * identifier profile. Returns 0 iff successful, or an error code.