1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/dns/punycode.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +/* 1.5 +punycode.c from RFC 3492 1.6 +http://www.nicemice.net/idn/ 1.7 +Adam M. Costello 1.8 +http://www.nicemice.net/amc/ 1.9 + 1.10 +This is ANSI C code (C89) implementing Punycode (RFC 3492). 1.11 + 1.12 + 1.13 + 1.14 +C. Disclaimer and license 1.15 + 1.16 + Regarding this entire document or any portion of it (including 1.17 + the pseudocode and C code), the author makes no guarantees and 1.18 + is not responsible for any damage resulting from its use. The 1.19 + author grants irrevocable permission to anyone to use, modify, 1.20 + and distribute it in any way that does not diminish the rights 1.21 + of anyone else to use, modify, and distribute it, provided that 1.22 + redistributed derivative works do not contain misleading author or 1.23 + version information. Derivative works need not be licensed under 1.24 + similar terms. 1.25 +*/ 1.26 + 1.27 +#ifdef __cplusplus 1.28 +extern "C" { 1.29 +#endif /* __cplusplus */ 1.30 + 1.31 +/************************************************************/ 1.32 +/* Public interface (would normally go in its own .h file): */ 1.33 + 1.34 +#include <limits.h> 1.35 + 1.36 +enum punycode_status { 1.37 + punycode_success, 1.38 + punycode_bad_input, /* Input is invalid. */ 1.39 + punycode_big_output, /* Output would exceed the space provided. */ 1.40 + punycode_overflow /* Input needs wider integers to process. */ 1.41 +}; 1.42 + 1.43 +#if UINT_MAX >= (1 << 26) - 1 1.44 +typedef unsigned int punycode_uint; 1.45 +#else 1.46 +typedef unsigned long punycode_uint; 1.47 +#endif 1.48 + 1.49 +enum punycode_status punycode_encode( 1.50 + punycode_uint input_length, 1.51 + const punycode_uint input[], 1.52 + const unsigned char case_flags[], 1.53 + punycode_uint *output_length, 1.54 + char output[] ); 1.55 + 1.56 + /* punycode_encode() converts Unicode to Punycode. The input */ 1.57 + /* is represented as an array of Unicode code points (not code */ 1.58 + /* units; surrogate pairs are not allowed), and the output */ 1.59 + /* will be represented as an array of ASCII code points. The */ 1.60 + /* output string is *not* null-terminated; it will contain */ 1.61 + /* zeros if and only if the input contains zeros. (Of course */ 1.62 + /* the caller can leave room for a terminator and add one if */ 1.63 + /* needed.) The input_length is the number of code points in */ 1.64 + /* the input. The output_length is an in/out argument: the */ 1.65 + /* caller passes in the maximum number of code points that it */ 1.66 + /* can receive, and on successful return it will contain the */ 1.67 + /* number of code points actually output. The case_flags array */ 1.68 + /* holds input_length boolean values, where nonzero suggests that */ 1.69 + /* the corresponding Unicode character be forced to uppercase */ 1.70 + /* after being decoded (if possible), and zero suggests that */ 1.71 + /* it be forced to lowercase (if possible). ASCII code points */ 1.72 + /* are encoded literally, except that ASCII letters are forced */ 1.73 + /* to uppercase or lowercase according to the corresponding */ 1.74 + /* uppercase flags. If case_flags is a null pointer then ASCII */ 1.75 + /* letters are left as they are, and other code points are */ 1.76 + /* treated as if their uppercase flags were zero. The return */ 1.77 + /* value can be any of the punycode_status values defined above */ 1.78 + /* except punycode_bad_input; if not punycode_success, then */ 1.79 + /* output_size and output might contain garbage. */ 1.80 + 1.81 +enum punycode_status punycode_decode( 1.82 + punycode_uint input_length, 1.83 + const char input[], 1.84 + punycode_uint *output_length, 1.85 + punycode_uint output[], 1.86 + unsigned char case_flags[] ); 1.87 + 1.88 + /* punycode_decode() converts Punycode to Unicode. The input is */ 1.89 + /* represented as an array of ASCII code points, and the output */ 1.90 + /* will be represented as an array of Unicode code points. The */ 1.91 + /* input_length is the number of code points in the input. The */ 1.92 + /* output_length is an in/out argument: the caller passes in */ 1.93 + /* the maximum number of code points that it can receive, and */ 1.94 + /* on successful return it will contain the actual number of */ 1.95 + /* code points output. The case_flags array needs room for at */ 1.96 + /* least output_length values, or it can be a null pointer if the */ 1.97 + /* case information is not needed. A nonzero flag suggests that */ 1.98 + /* the corresponding Unicode character be forced to uppercase */ 1.99 + /* by the caller (if possible), while zero suggests that it be */ 1.100 + /* forced to lowercase (if possible). ASCII code points are */ 1.101 + /* output already in the proper case, but their flags will be set */ 1.102 + /* appropriately so that applying the flags would be harmless. */ 1.103 + /* The return value can be any of the punycode_status values */ 1.104 + /* defined above; if not punycode_success, then output_length, */ 1.105 + /* output, and case_flags might contain garbage. On success, the */ 1.106 + /* decoder will never need to write an output_length greater than */ 1.107 + /* input_length, because of how the encoding is defined. */ 1.108 + 1.109 +#ifdef __cplusplus 1.110 +} 1.111 +#endif /* __cplusplus */