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