punycode.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. punycode.c from RFC 3492
  3. http://www.nicemice.net/idn/
  4. Adam M. Costello
  5. http://www.nicemice.net/amc/
  6. This is ANSI C code (C89) implementing Punycode (RFC 3492).
  7. C. Disclaimer and license
  8. Regarding this entire document or any portion of it (including
  9. the pseudocode and C code), the author makes no guarantees and
  10. is not responsible for any damage resulting from its use. The
  11. author grants irrevocable permission to anyone to use, modify,
  12. and distribute it in any way that does not diminish the rights
  13. of anyone else to use, modify, and distribute it, provided that
  14. redistributed derivative works do not contain misleading author or
  15. version information. Derivative works need not be licensed under
  16. similar terms.
  17. */
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif /* __cplusplus */
  21. /************************************************************/
  22. /* Public interface (would normally go in its own .h file): */
  23. #include <limits.h>
  24. enum punycode_status {
  25. punycode_success,
  26. punycode_bad_input, /* Input is invalid. */
  27. punycode_big_output, /* Output would exceed the space provided. */
  28. punycode_overflow /* Input needs wider integers to process. */
  29. };
  30. #if UINT_MAX >= (1 << 26) - 1
  31. typedef unsigned int punycode_uint;
  32. #else
  33. typedef unsigned long punycode_uint;
  34. #endif
  35. enum punycode_status punycode_encode(
  36. punycode_uint input_length,
  37. const punycode_uint input[],
  38. const unsigned char case_flags[],
  39. punycode_uint *output_length,
  40. char output[] );
  41. /* punycode_encode() converts Unicode to Punycode. The input */
  42. /* is represented as an array of Unicode code points (not code */
  43. /* units; surrogate pairs are not allowed), and the output */
  44. /* will be represented as an array of ASCII code points. The */
  45. /* output string is *not* null-terminated; it will contain */
  46. /* zeros if and only if the input contains zeros. (Of course */
  47. /* the caller can leave room for a terminator and add one if */
  48. /* needed.) The input_length is the number of code points in */
  49. /* the input. The output_length is an in/out argument: the */
  50. /* caller passes in the maximum number of code points that it */
  51. /* can receive, and on successful return it will contain the */
  52. /* number of code points actually output. The case_flags array */
  53. /* holds input_length boolean values, where nonzero suggests that */
  54. /* the corresponding Unicode character be forced to uppercase */
  55. /* after being decoded (if possible), and zero suggests that */
  56. /* it be forced to lowercase (if possible). ASCII code points */
  57. /* are encoded literally, except that ASCII letters are forced */
  58. /* to uppercase or lowercase according to the corresponding */
  59. /* uppercase flags. If case_flags is a null pointer then ASCII */
  60. /* letters are left as they are, and other code points are */
  61. /* treated as if their uppercase flags were zero. The return */
  62. /* value can be any of the punycode_status values defined above */
  63. /* except punycode_bad_input; if not punycode_success, then */
  64. /* output_size and output might contain garbage. */
  65. enum punycode_status punycode_decode(
  66. punycode_uint input_length,
  67. const char input[],
  68. punycode_uint *output_length,
  69. punycode_uint output[],
  70. unsigned char case_flags[] );
  71. /* punycode_decode() converts Punycode to Unicode. The input is */
  72. /* represented as an array of ASCII code points, and the output */
  73. /* will be represented as an array of Unicode code points. The */
  74. /* input_length is the number of code points in the input. The */
  75. /* output_length is an in/out argument: the caller passes in */
  76. /* the maximum number of code points that it can receive, and */
  77. /* on successful return it will contain the actual number of */
  78. /* code points output. The case_flags array needs room for at */
  79. /* least output_length values, or it can be a null pointer if the */
  80. /* case information is not needed. A nonzero flag suggests that */
  81. /* the corresponding Unicode character be forced to uppercase */
  82. /* by the caller (if possible), while zero suggests that it be */
  83. /* forced to lowercase (if possible). ASCII code points are */
  84. /* output already in the proper case, but their flags will be set */
  85. /* appropriately so that applying the flags would be harmless. */
  86. /* The return value can be any of the punycode_status values */
  87. /* defined above; if not punycode_success, then output_length, */
  88. /* output, and case_flags might contain garbage. On success, the */
  89. /* decoder will never need to write an output_length greater than */
  90. /* input_length, because of how the encoding is defined. */
  91. #ifdef __cplusplus
  92. }
  93. #endif /* __cplusplus */