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