punycode.h 9.2 KB

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