punycode.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* punycode.h --- Declarations for punycode functions.
  2. Copyright (C) 2002-2022 Simon Josefsson
  3. This file is part of GNU Libidn.
  4. GNU Libidn is free software: you can redistribute it and/or
  5. modify it under the terms of either:
  6. * the GNU Lesser General Public License as published by the Free
  7. Software Foundation; either version 3 of the License, or (at
  8. your option) any later version.
  9. or
  10. * the GNU General Public License as published by the Free
  11. Software Foundation; either version 2 of the License, or (at
  12. your option) any later version.
  13. or both in parallel, as here.
  14. GNU Libidn is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. General Public License for more details.
  18. You should have received copies of the GNU General Public License and
  19. the GNU Lesser General Public License along with this program. If
  20. not, see <https://www.gnu.org/licenses/>. */
  21. /*
  22. * This file is derived from RFC 3492bis written by Adam M. Costello,
  23. * downloaded from http://www.nicemice.net/idn/punycode-spec.gz on
  24. * 2015-03-02 with SHA1 a966a8017f6be579d74a50a226accc7607c40133, a
  25. * copy of which is stored in the GNU Libidn version controlled
  26. * repository under doc/specification/punycode-spec.gz.
  27. *
  28. * The changes compared to Adam's file include: re-indentation, adding
  29. * the license boilerplate and this comment, adding the #ifndef
  30. * PUNYCODE_H and IDNAPI blocks, changing the return code of
  31. * punycode_encode and punycode_decode from enum to int, simplifying
  32. * the definition of punycode_uint by #include'ing idn-int.h and using
  33. * uint32_t instead of limit.h-based code, adding Punycode_status and
  34. * punycode_strerror, adding 'extern IDNAPI' declarations to function
  35. * prototypes, and mentioning variable names in function prototypes.
  36. *
  37. * Adam's file contains the following:
  38. *
  39. * punycode-sample.c 2.0.0 (2004-Mar-21-Sun)
  40. * http://www.nicemice.net/idn/
  41. * Adam M. Costello
  42. * http://www.nicemice.net/amc/
  43. *
  44. * This is ANSI C code (C89) implementing Punycode 1.0.x.
  45. *
  46. * Disclaimer and license: Regarding this entire document or any
  47. * portion of it (including the pseudocode and C code), the author
  48. * makes no guarantees and is not responsible for any damage resulting
  49. * from its use. The author grants irrevocable permission to anyone
  50. * to use, modify, and distribute it in any way that does not diminish
  51. * the rights of anyone else to use, modify, and distribute it,
  52. * provided that redistributed derivative works do not contain
  53. * misleading author or version information. Derivative works need
  54. * not be licensed under similar terms.
  55. */
  56. #ifndef PUNYCODE_H
  57. # define PUNYCODE_H
  58. /**
  59. * SECTION:punycode
  60. * @title: punycode.h
  61. * @short_description: Punycode-related functions
  62. *
  63. * Punycode-related functions.
  64. */
  65. # ifndef IDNAPI
  66. # define IDNAPI
  67. # endif
  68. # ifdef __cplusplus
  69. extern "C"
  70. {
  71. # endif
  72. /************************************************************/
  73. /* Public interface (would normally go in its own .h file): */
  74. # include <stddef.h> /* size_t */
  75. # include <idn-int.h> /* uint32_t */
  76. enum punycode_status
  77. {
  78. punycode_success = 0,
  79. punycode_bad_input = 1, /* Input is invalid. */
  80. punycode_big_output = 2, /* Output would exceed the space provided. */
  81. punycode_overflow = 3 /* Wider integers needed to process input. */
  82. };
  83. typedef enum
  84. {
  85. PUNYCODE_SUCCESS = punycode_success,
  86. PUNYCODE_BAD_INPUT = punycode_bad_input,
  87. PUNYCODE_BIG_OUTPUT = punycode_big_output,
  88. PUNYCODE_OVERFLOW = punycode_overflow
  89. } Punycode_status;
  90. extern IDNAPI const char *punycode_strerror (Punycode_status rc);
  91. /* punycode_uint needs to be unsigned and needs to be */
  92. /* at least 26 bits wide. The particular type can be */
  93. /* specified by defining PUNYCODE_UINT, otherwise a */
  94. /* suitable type will be chosen automatically. */
  95. typedef uint32_t punycode_uint;
  96. extern IDNAPI int punycode_encode (size_t input_length,
  97. const punycode_uint input[],
  98. const unsigned char case_flags[],
  99. size_t *output_length, char output[]);
  100. /*
  101. punycode_encode() converts a sequence of code points (presumed to be
  102. Unicode code points) to Punycode.
  103. Input arguments (to be supplied by the caller):
  104. input_length
  105. The number of code points in the input array and the number
  106. of flags in the case_flags array.
  107. input
  108. An array of code points. They are presumed to be Unicode
  109. code points, but that is not strictly necessary. The
  110. array contains code points, not code units. UTF-16 uses
  111. code units D800 through DFFF to refer to code points
  112. 10000..10FFFF. The code points D800..DFFF do not occur in
  113. any valid Unicode string. The code points that can occur in
  114. Unicode strings (0..D7FF and E000..10FFFF) are also called
  115. Unicode scalar values.
  116. case_flags
  117. A null pointer or an array of boolean values parallel to
  118. the input array. Nonzero (true, flagged) suggests that the
  119. corresponding Unicode character be forced to uppercase after
  120. being decoded (if possible), and zero (false, unflagged)
  121. suggests that it be forced to lowercase (if possible).
  122. ASCII code points (0..7F) are encoded literally, except that
  123. ASCII letters are forced to uppercase or lowercase according
  124. to the corresponding case flags. If case_flags is a null
  125. pointer then ASCII letters are left as they are, and other
  126. code points are treated as unflagged.
  127. Output arguments (to be filled in by the function):
  128. output
  129. An array of ASCII code points. It is *not* null-terminated;
  130. it will contain zeros if and only if the input contains
  131. zeros. (Of course the caller can leave room for a
  132. terminator and add one if needed.)
  133. Input/output arguments (to be supplied by the caller and overwritten
  134. by the function):
  135. output_length
  136. The caller passes in the maximum number of ASCII code points
  137. that it can receive. On successful return it will contain
  138. the number of ASCII code points actually output.
  139. Return value:
  140. Can be any of the punycode_status values defined above except
  141. punycode_bad_input. If not punycode_success, then output_size
  142. and output might contain garbage.
  143. */
  144. extern int punycode_decode (size_t input_length,
  145. const char input[],
  146. size_t *output_length,
  147. punycode_uint output[],
  148. unsigned char case_flags[]);
  149. /*
  150. punycode_decode() converts Punycode to a sequence of code points
  151. (presumed to be Unicode code points).
  152. Input arguments (to be supplied by the caller):
  153. input_length
  154. The number of ASCII code points in the input array.
  155. input
  156. An array of ASCII code points (0..7F).
  157. Output arguments (to be filled in by the function):
  158. output
  159. An array of code points like the input argument of
  160. punycode_encode() (see above).
  161. case_flags
  162. A null pointer (if the flags are not needed by the caller)
  163. or an array of boolean values parallel to the output array.
  164. Nonzero (true, flagged) suggests that the corresponding
  165. Unicode character be forced to uppercase by the caller (if
  166. possible), and zero (false, unflagged) suggests that it
  167. be forced to lowercase (if possible). ASCII code points
  168. (0..7F) are output already in the proper case, but their
  169. flags will be set appropriately so that applying the flags
  170. would be harmless.
  171. Input/output arguments (to be supplied by the caller and overwritten
  172. by the function):
  173. output_length
  174. The caller passes in the maximum number of code points
  175. that it can receive into the output array (which is also
  176. the maximum number of flags that it can receive into the
  177. case_flags array, if case_flags is not a null pointer). On
  178. successful return it will contain the number of code points
  179. actually output (which is also the number of flags actually
  180. output, if case_flags is not a null pointer). The decoder
  181. will never need to output more code points than the number
  182. of ASCII code points in the input, because of the way the
  183. encoding is defined. The number of code points output
  184. cannot exceed the maximum possible value of a punycode_uint,
  185. even if the supplied output_length is greater than that.
  186. Return value:
  187. Can be any of the punycode_status values defined above. If not
  188. punycode_success, then output_length, output, and case_flags
  189. might contain garbage.
  190. */
  191. # ifdef __cplusplus
  192. }
  193. # endif
  194. #endif /* PUNYCODE_H */