📄 rfc3492.txt
字号:
Costello Standards Track [Page 20]RFC 3492 IDNA Punycode March 20039. References9.1 Normative References [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, March 1997.9.2 Informative References [RFC952] Harrenstien, K., Stahl, M. and E. Feinler, "DOD Internet Host Table Specification", RFC 952, October 1985. [RFC1034] Mockapetris, P., "Domain Names - Concepts and Facilities", STD 13, RFC 1034, November 1987. [IDNA] Faltstrom, P., Hoffman, P. and A. Costello, "Internationalizing Domain Names in Applications (IDNA)", RFC 3490, March 2003. [NAMEPREP] Hoffman, P. and M. Blanchet, "Nameprep: A Stringprep Profile for Internationalized Domain Names (IDN)", RFC 3491, March 2003. [ASCII] Cerf, V., "ASCII format for Network Interchange", RFC 20, October 1969. [PROVINCIAL] Kaplan, M., "The 'anyone can be provincial!' page", http://www.trigeminal.com/samples/provincial.html. [UNICODE] The Unicode Consortium, "The Unicode Standard", http://www.unicode.org/unicode/standard/standard.html.Costello Standards Track [Page 21]RFC 3492 IDNA Punycode March 2003A. Mixed-case annotation In order to use Punycode to represent case-insensitive strings, higher layers need to case-fold the strings prior to Punycode encoding. The encoded string can use mixed case as an annotation telling how to convert the folded string into a mixed-case string for display purposes. Note, however, that mixed-case annotation is not used by the ToASCII and ToUnicode operations specified in [IDNA], and therefore implementors of IDNA can disregard this appendix. Basic code points can use mixed case directly, because the decoder copies them verbatim, leaving lowercase code points lowercase, and leaving uppercase code points uppercase. Each non-basic code point is represented by a delta, which is represented by a sequence of basic code points, the last of which provides the annotation. If it is uppercase, it is a suggestion to map the non-basic code point to uppercase (if possible); if it is lowercase, it is a suggestion to map the non-basic code point to lowercase (if possible). These annotations do not alter the code points returned by decoders; the annotations are returned separately, for the caller to use or ignore. Encoders can accept annotations in addition to code points, but the annotations do not alter the output, except to influence the uppercase/lowercase form of ASCII letters. Punycode encoders and decoders need not support these annotations, and higher layers need not use them.B. Disclaimer and license Regarding this entire document or any portion of it (including the pseudocode and C code), the author makes no guarantees and is not responsible for any damage resulting from its use. The author grants irrevocable permission to anyone to use, modify, and distribute it in any way that does not diminish the rights of anyone else to use, modify, and distribute it, provided that redistributed derivative works do not contain misleading author or version information. Derivative works need not be licensed under similar terms.Costello Standards Track [Page 22]RFC 3492 IDNA Punycode March 2003C. Punycode sample implementation/*punycode.c from RFC 3492http://www.nicemice.net/idn/Adam M. Costellohttp://www.nicemice.net/amc/This is ANSI C code (C89) implementing Punycode (RFC 3492).*//************************************************************//* Public interface (would normally go in its own .h file): */#include <limits.h>enum punycode_status { punycode_success, punycode_bad_input, /* Input is invalid. */ punycode_big_output, /* Output would exceed the space provided. */ punycode_overflow /* Input needs wider integers to process. */};#if UINT_MAX >= (1 << 26) - 1typedef unsigned int punycode_uint;#elsetypedef unsigned long punycode_uint;#endifenum punycode_status punycode_encode( punycode_uint input_length, const punycode_uint input[], const unsigned char case_flags[], punycode_uint *output_length, char output[] ); /* punycode_encode() converts Unicode to Punycode. The input */ /* is represented as an array of Unicode code points (not code */ /* units; surrogate pairs are not allowed), and the output */ /* will be represented as an array of ASCII code points. The */ /* output string is *not* null-terminated; it will contain */ /* zeros if and only if the input contains zeros. (Of course */ /* the caller can leave room for a terminator and add one if */ /* needed.) The input_length is the number of code points in */ /* the input. The output_length is an in/out argument: the */ /* caller passes in the maximum number of code points that it */Costello Standards Track [Page 23]RFC 3492 IDNA Punycode March 2003 /* can receive, and on successful return it will contain the */ /* number of code points actually output. The case_flags array */ /* holds input_length boolean values, where nonzero suggests that */ /* the corresponding Unicode character be forced to uppercase */ /* after being decoded (if possible), and zero suggests that */ /* it be forced to lowercase (if possible). ASCII code points */ /* are encoded literally, except that ASCII letters are forced */ /* to uppercase or lowercase according to the corresponding */ /* uppercase flags. If case_flags is a null pointer then ASCII */ /* letters are left as they are, and other code points are */ /* treated as if their uppercase flags were zero. The return */ /* value can be any of the punycode_status values defined above */ /* except punycode_bad_input; if not punycode_success, then */ /* output_size and output might contain garbage. */enum punycode_status punycode_decode( punycode_uint input_length, const char input[], punycode_uint *output_length, punycode_uint output[], unsigned char case_flags[] ); /* punycode_decode() converts Punycode to Unicode. The input is */ /* represented as an array of ASCII code points, and the output */ /* will be represented as an array of Unicode code points. The */ /* input_length is the number of code points in the input. The */ /* output_length is an in/out argument: the caller passes in */ /* the maximum number of code points that it can receive, and */ /* on successful return it will contain the actual number of */ /* code points output. The case_flags array needs room for at */ /* least output_length values, or it can be a null pointer if the */ /* case information is not needed. A nonzero flag suggests that */ /* the corresponding Unicode character be forced to uppercase */ /* by the caller (if possible), while zero suggests that it be */ /* forced to lowercase (if possible). ASCII code points are */ /* output already in the proper case, but their flags will be set */ /* appropriately so that applying the flags would be harmless. */ /* The return value can be any of the punycode_status values */ /* defined above; if not punycode_success, then output_length, */ /* output, and case_flags might contain garbage. On success, the */ /* decoder will never need to write an output_length greater than */ /* input_length, because of how the encoding is defined. *//**********************************************************//* Implementation (would normally go in its own .c file): */#include <string.h>Costello Standards Track [Page 24]RFC 3492 IDNA Punycode March 2003/*** Bootstring parameters for Punycode ***/enum { base = 36, tmin = 1, tmax = 26, skew = 38, damp = 700, initial_bias = 72, initial_n = 0x80, delimiter = 0x2D };/* basic(cp) tests whether cp is a basic code point: */#define basic(cp) ((punycode_uint)(cp) < 0x80)/* delim(cp) tests whether cp is a delimiter: */#define delim(cp) ((cp) == delimiter)/* decode_digit(cp) returns the numeric value of a basic code *//* point (for use in representing integers) in the range 0 to *//* base-1, or base if cp is does not represent a value. */static punycode_uint decode_digit(punycode_uint cp){ return cp - 48 < 10 ? cp - 22 : cp - 65 < 26 ? cp - 65 : cp - 97 < 26 ? cp - 97 : base;}/* encode_digit(d,flag) returns the basic code point whose value *//* (when used for representing integers) is d, which needs to be in *//* the range 0 to base-1. The lowercase form is used unless flag is *//* nonzero, in which case the uppercase form is used. The behavior *//* is undefined if flag is nonzero and digit d has no uppercase form. */static char encode_digit(punycode_uint d, int flag){ return d + 22 + 75 * (d < 26) - ((flag != 0) << 5); /* 0..25 map to ASCII a..z or A..Z */ /* 26..35 map to ASCII 0..9 */}/* flagged(bcp) tests whether a basic code point is flagged *//* (uppercase). The behavior is undefined if bcp is not a *//* basic code point. */#define flagged(bcp) ((punycode_uint)(bcp) - 65 < 26)/* encode_basic(bcp,flag) forces a basic code point to lowercase *//* if flag is zero, uppercase if flag is nonzero, and returns *//* the resulting code point. The code point is unchanged if it *//* is caseless. The behavior is undefined if bcp is not a basic *//* code point. */static char encode_basic(punycode_uint bcp, int flag){Costello Standards Track [Page 25]RFC 3492 IDNA Punycode March 2003 bcp -= (bcp - 97 < 26) << 5; return bcp + ((!flag && (bcp - 65 < 26)) << 5);}/*** Platform-specific constants ***//* maxint is the maximum value of a punycode_uint variable: */static const punycode_uint maxint = -1;/* Because maxint is unsigned, -1 becomes the maximum value. *//*** Bias adaptation function ***/static punycode_uint adapt( punycode_uint delta, punycode_uint numpoints, int firsttime ){ punycode_uint k; delta = firsttime ? delta / damp : delta >> 1; /* delta >> 1 is a faster way of doing delta / 2 */ delta += delta / numpoints; for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base) { delta /= base - tmin; } return k + (base - tmin + 1) * delta / (delta + skew);}/*** Main encode function ***/enum punycode_status punycode_encode( punycode_uint input_length, const punycode_uint input[], const unsigned char case_flags[], punycode_uint *output_length, char output[] ){ punycode_uint n, delta, h, b, out, max_out, bias, j, m, q, k, t; /* Initialize the state: */ n = initial_n; delta = out = 0; max_out = *output_length; bias = initial_bias; /* Handle the basic code points: */Costello Standards Track [Page 26]RFC 3492 IDNA Punycode March 2003 for (j = 0; j < input_length; ++j) { if (basic(input[j])) { if (max_out - out < 2) return punycode_big_output; output[out++] = case_flags ? encode_basic(input[j], case_flags[j]) : input[j]; } /* else if (input[j] < n) return punycode_bad_input; */ /* (not needed for Punycode with unsigned code points) */ } h = b = out; /* h is the number of code points that have been handled, b is the */ /* number of basic code points, and out is the number of characters */ /* that have been output. */ if (b > 0) output[out++] = delimiter; /* Main encoding loop: */ while (h < input_length) { /* All non-basic code points < n have been */ /* handled already. Find the next larger one: */ for (m = maxint, j = 0; j < input_length; ++j) { /* if (basic(input[j])) continue; */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -