punycode.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* punycode.h --- Declarations for punycode functions.
  2. * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
  3. *
  4. * This file is part of GNU Libidn.
  5. *
  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.
  10. *
  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.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with GNU Libidn; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. *
  20. */
  21. /*
  22. * This file is derived from RFC 3492bis written by Adam M. Costello.
  23. *
  24. * Disclaimer and license: Regarding this entire document or any
  25. * portion of it (including the pseudocode and C code), the author
  26. * makes no guarantees and is not responsible for any damage resulting
  27. * from its use. The author grants irrevocable permission to anyone
  28. * to use, modify, and distribute it in any way that does not diminish
  29. * the rights of anyone else to use, modify, and distribute it,
  30. * provided that redistributed derivative works do not contain
  31. * misleading author or version information. Derivative works need
  32. * not be licensed under similar terms.
  33. *
  34. * Copyright (C) The Internet Society (2003). All Rights Reserved.
  35. *
  36. * This document and translations of it may be copied and furnished to
  37. * others, and derivative works that comment on or otherwise explain it
  38. * or assist in its implementation may be prepared, copied, published
  39. * and distributed, in whole or in part, without restriction of any
  40. * kind, provided that the above copyright notice and this paragraph are
  41. * included on all such copies and derivative works. However, this
  42. * document itself may not be modified in any way, such as by removing
  43. * the copyright notice or references to the Internet Society or other
  44. * Internet organizations, except as needed for the purpose of
  45. * developing Internet standards in which case the procedures for
  46. * copyrights defined in the Internet Standards process must be
  47. * followed, or as required to translate it into languages other than
  48. * English.
  49. *
  50. * The limited permissions granted above are perpetual and will not be
  51. * revoked by the Internet Society or its successors or assigns.
  52. *
  53. * This document and the information contained herein is provided on an
  54. * "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
  55. * TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
  56. * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
  57. * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
  58. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  59. */
  60. #ifndef _PUNYCODE_H
  61. #define _PUNYCODE_H
  62. #ifdef __cplusplus
  63. extern "C"
  64. {
  65. #endif
  66. #include <stddef.h> /* size_t */
  67. #include "idn-int.h" /* uint32_t */
  68. enum punycode_status
  69. {
  70. punycode_success = 0,
  71. punycode_bad_input = 1, /* Input is invalid. */
  72. punycode_big_output = 2, /* Output would exceed the space provided. */
  73. punycode_overflow = 3 /* Wider integers needed to process input. */
  74. };
  75. typedef enum
  76. {
  77. PUNYCODE_SUCCESS = punycode_success,
  78. PUNYCODE_BAD_INPUT = punycode_bad_input,
  79. PUNYCODE_BIG_OUTPUT = punycode_big_output,
  80. PUNYCODE_OVERFLOW = punycode_overflow
  81. } Punycode_status;
  82. extern const char *punycode_strerror (Punycode_status rc);
  83. /* punycode_uint needs to be unsigned and needs to be */
  84. /* at least 26 bits wide. */
  85. typedef uint32_t punycode_uint;
  86. extern int punycode_encode (size_t input_length,
  87. const punycode_uint input[],
  88. const unsigned char case_flags[],
  89. size_t * output_length, char output[]);
  90. /*
  91. punycode_encode() converts a sequence of code points (presumed to be
  92. Unicode code points) to Punycode.
  93. Input arguments (to be supplied by the caller):
  94. input_length
  95. The number of code points in the input array and the number
  96. of flags in the case_flags array.
  97. input
  98. An array of code points. They are presumed to be Unicode
  99. code points, but that is not strictly REQUIRED. The
  100. array contains code points, not code units. UTF-16 uses
  101. code units D800 through DFFF to refer to code points
  102. 10000..10FFFF. The code points D800..DFFF do not occur in
  103. any valid Unicode string. The code points that can occur in
  104. Unicode strings (0..D7FF and E000..10FFFF) are also called
  105. Unicode scalar values.
  106. case_flags
  107. A null pointer or an array of boolean values parallel to
  108. the input array. Nonzero (true, flagged) suggests that the
  109. corresponding Unicode character be forced to uppercase after
  110. being decoded (if possible), and zero (false, unflagged)
  111. suggests that it be forced to lowercase (if possible).
  112. ASCII code points (0..7F) are encoded literally, except that
  113. ASCII letters are forced to uppercase or lowercase according
  114. to the corresponding case flags. If case_flags is a null
  115. pointer then ASCII letters are left as they are, and other
  116. code points are treated as unflagged.
  117. Output arguments (to be filled in by the function):
  118. output
  119. An array of ASCII code points. It is *not* null-terminated;
  120. it will contain zeros if and only if the input contains
  121. zeros. (Of course the caller can leave room for a
  122. terminator and add one if needed.)
  123. Input/output arguments (to be supplied by the caller and overwritten
  124. by the function):
  125. output_length
  126. The caller passes in the maximum number of ASCII code points
  127. that it can receive. On successful return it will contain
  128. the number of ASCII code points actually output.
  129. Return value:
  130. Can be any of the punycode_status values defined above except
  131. punycode_bad_input. If not punycode_success, then output_size
  132. and output might contain garbage.
  133. */
  134. extern int punycode_decode (size_t input_length,
  135. const char input[],
  136. size_t * output_length,
  137. punycode_uint output[],
  138. unsigned char case_flags[]);
  139. /*
  140. punycode_decode() converts Punycode to a sequence of code points
  141. (presumed to be Unicode code points).
  142. Input arguments (to be supplied by the caller):
  143. input_length
  144. The number of ASCII code points in the input array.
  145. input
  146. An array of ASCII code points (0..7F).
  147. Output arguments (to be filled in by the function):
  148. output
  149. An array of code points like the input argument of
  150. punycode_encode() (see above).
  151. case_flags
  152. A null pointer (if the flags are not needed by the caller)
  153. or an array of boolean values parallel to the output array.
  154. Nonzero (true, flagged) suggests that the corresponding
  155. Unicode character be forced to uppercase by the caller (if
  156. possible), and zero (false, unflagged) suggests that it
  157. be forced to lowercase (if possible). ASCII code points
  158. (0..7F) are output already in the proper case, but their
  159. flags will be set appropriately so that applying the flags
  160. would be harmless.
  161. Input/output arguments (to be supplied by the caller and overwritten
  162. by the function):
  163. output_length
  164. The caller passes in the maximum number of code points
  165. that it can receive into the output array (which is also
  166. the maximum number of flags that it can receive into the
  167. case_flags array, if case_flags is not a null pointer). On
  168. successful return it will contain the number of code points
  169. actually output (which is also the number of flags actually
  170. output, if case_flags is not a null pointer). The decoder
  171. will never need to output more code points than the number
  172. of ASCII code points in the input, because of the way the
  173. encoding is defined. The number of code points output
  174. cannot exceed the maximum possible value of a punycode_uint,
  175. even if the supplied output_length is greater than that.
  176. Return value:
  177. Can be any of the punycode_status values defined above. If not
  178. punycode_success, then output_length, output, and case_flags
  179. might contain garbage.
  180. */
  181. #ifdef __cplusplus
  182. }
  183. #endif
  184. #endif /* _PUNYCODE_H */