Annotation of parser3/src/lib/punycode/pa_punycode.c, revision 1.4

1.1       moko        1: /* punycode.c --- Implementation of punycode used to ASCII encode IDN's.
                      2:    Copyright (C) 2002-2013 Simon Josefsson
                      3: 
                      4:    This file is part of GNU Libidn.
                      5: 
                      6:    GNU Libidn is free software: you can redistribute it and/or
                      7:    modify it under the terms of either:
                      8: 
                      9:      * the GNU Lesser General Public License as published by the Free
                     10:        Software Foundation; either version 3 of the License, or (at
                     11:        your option) any later version.
                     12: 
                     13:    or
                     14: 
                     15:      * the GNU General Public License as published by the Free
                     16:        Software Foundation; either version 2 of the License, or (at
                     17:        your option) any later version.
                     18: 
                     19:    or both in parallel, as here.
                     20: 
                     21:    GNU Libidn is distributed in the hope that it will be useful,
                     22:    but WITHOUT ANY WARRANTY; without even the implied warranty of
                     23:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
                     24:    General Public License for more details.
                     25: 
                     26:    You should have received copies of the GNU General Public License and
                     27:    the GNU Lesser General Public License along with this program.  If
                     28:    not, see <http://www.gnu.org/licenses/>. */
                     29: 
                     30: /*
                     31:  * This file is derived from RFC 3492bis written by Adam M. Costello.
                     32:  *
                     33:  * Disclaimer and license: Regarding this entire document or any
                     34:  * portion of it (including the pseudocode and C code), the author
                     35:  * makes no guarantees and is not responsible for any damage resulting
                     36:  * from its use.  The author grants irrevocable permission to anyone
                     37:  * to use, modify, and distribute it in any way that does not diminish
                     38:  * the rights of anyone else to use, modify, and distribute it,
                     39:  * provided that redistributed derivative works do not contain
                     40:  * misleading author or version information.  Derivative works need
                     41:  * not be licensed under similar terms.
                     42:  *
                     43:  * Copyright (C) The Internet Society (2003).  All Rights Reserved.
                     44:  *
                     45:  * This document and translations of it may be copied and furnished to
                     46:  * others, and derivative works that comment on or otherwise explain it
                     47:  * or assist in its implementation may be prepared, copied, published
                     48:  * and distributed, in whole or in part, without restriction of any
                     49:  * kind, provided that the above copyright notice and this paragraph are
                     50:  * included on all such copies and derivative works.  However, this
                     51:  * document itself may not be modified in any way, such as by removing
                     52:  * the copyright notice or references to the Internet Society or other
                     53:  * Internet organizations, except as needed for the purpose of
                     54:  * developing Internet standards in which case the procedures for
                     55:  * copyrights defined in the Internet Standards process must be
                     56:  * followed, or as required to translate it into languages other than
                     57:  * English.
                     58:  *
                     59:  * The limited permissions granted above are perpetual and will not be
                     60:  * revoked by the Internet Society or its successors or assigns.
                     61:  *
                     62:  * This document and the information contained herein is provided on an
                     63:  * "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
                     64:  * TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
                     65:  * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
                     66:  * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
                     67:  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
                     68:  */
                     69: 
                     70: #include "pa_punycode.h"
                     71: 
                     72: /*** Bootstring parameters for Punycode ***/
                     73: 
                     74: enum
                     75: { base = 36, tmin = 1, tmax = 26, skew = 38, damp = 700,
                     76:   initial_bias = 72, initial_n = 0x80, delimiter = 0x2D
                     77: };
                     78: 
                     79: /* basic(cp) tests whether cp is a basic code point: */
                     80: #define basic(cp) ((punycode_uint)(cp) < 0x80)
                     81: 
                     82: /* delim(cp) tests whether cp is a delimiter: */
                     83: #define delim(cp) ((cp) == delimiter)
                     84: 
                     85: /* decode_digit(cp) returns the numeric value of a basic code */
                     86: /* point (for use in representing integers) in the range 0 to */
                     87: /* base-1, or base if cp does not represent a value.          */
                     88: 
1.2       moko       89: static punycode_uint decode_digit (punycode_uint cp)
1.1       moko       90: {
                     91:   return cp - 48 < 10 ? cp - 22 : cp - 65 < 26 ? cp - 65 :
                     92:     cp - 97 < 26 ? cp - 97 : base;
                     93: }
                     94: 
                     95: /* encode_digit(d,flag) returns the basic code point whose value      */
                     96: /* (when used for representing integers) is d, which needs to be in   */
                     97: /* the range 0 to base-1.  The lowercase form is used unless flag is  */
                     98: /* nonzero, in which case the uppercase form is used.  The behavior   */
                     99: /* is undefined if flag is nonzero and digit d has no uppercase form. */
                    100: 
1.2       moko      101: static char encode_digit (punycode_uint d, int flag)
1.1       moko      102: {
1.2       moko      103:   return (char)(d + 22 + 75 * (d < 26) - ((flag != 0) << 5));
1.1       moko      104:   /*  0..25 map to ASCII a..z or A..Z */
                    105:   /* 26..35 map to ASCII 0..9         */
                    106: }
                    107: 
                    108: /* flagged(bcp) tests whether a basic code point is flagged */
                    109: /* (uppercase).  The behavior is undefined if bcp is not a  */
                    110: /* basic code point.                                        */
                    111: 
1.4     ! moko      112: #define flagged(bcp) (unsigned char)((punycode_uint)(bcp) - 65 < 26)
1.1       moko      113: 
                    114: /* encode_basic(bcp,flag) forces a basic code point to lowercase */
                    115: /* if flag is zero, uppercase if flag is nonzero, and returns    */
                    116: /* the resulting code point.  The code point is unchanged if it  */
                    117: /* is caseless.  The behavior is undefined if bcp is not a basic */
                    118: /* code point.                                                   */
                    119: 
1.2       moko      120: static char encode_basic (punycode_uint bcp, int flag)
1.1       moko      121: {
                    122:   bcp -= (bcp - 97 < 26) << 5;
1.2       moko      123:   return (char)(bcp + ((!flag && (bcp - 65 < 26)) << 5));
1.1       moko      124: }
                    125: 
                    126: /*** Platform-specific constants ***/
                    127: 
                    128: /* maxint is the maximum value of a punycode_uint variable: */
1.2       moko      129: static const punycode_uint maxint = 0xFFFFFFFF;
1.1       moko      130: /* Because maxint is unsigned, -1 becomes the maximum value. */
                    131: 
                    132: /*** Bias adaptation function ***/
                    133: 
1.2       moko      134: static punycode_uint adapt (punycode_uint delta, punycode_uint numpoints, int firsttime)
1.1       moko      135: {
                    136:   punycode_uint k;
                    137: 
                    138:   delta = firsttime ? delta / damp : delta >> 1;
                    139:   /* delta >> 1 is a faster way of doing delta / 2 */
                    140:   delta += delta / numpoints;
                    141: 
                    142:   for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base)
                    143:     {
                    144:       delta /= base - tmin;
                    145:     }
                    146: 
                    147:   return k + (base - tmin + 1) * delta / (delta + skew);
                    148: }
                    149: 
                    150: /*** Main encode function ***/
                    151: 
                    152: /**
                    153:  * punycode_encode:
                    154:  * @input_length: The number of code points in the @input array and
                    155:  *   the number of flags in the @case_flags array.
                    156:  * @input: An array of code points.  They are presumed to be Unicode
                    157:  *   code points, but that is not strictly REQUIRED.  The array
                    158:  *   contains code points, not code units.  UTF-16 uses code units
                    159:  *   D800 through DFFF to refer to code points 10000..10FFFF.  The
                    160:  *   code points D800..DFFF do not occur in any valid Unicode string.
                    161:  *   The code points that can occur in Unicode strings (0..D7FF and
                    162:  *   E000..10FFFF) are also called Unicode scalar values.
                    163:  * @case_flags: A %NULL pointer or an array of boolean values parallel
                    164:  *   to the @input array.  Nonzero (true, flagged) suggests that the
                    165:  *   corresponding Unicode character be forced to uppercase after
                    166:  *   being decoded (if possible), and zero (false, unflagged) suggests
                    167:  *   that it be forced to lowercase (if possible).  ASCII code points
                    168:  *   (0..7F) are encoded literally, except that ASCII letters are
                    169:  *   forced to uppercase or lowercase according to the corresponding
                    170:  *   case flags.  If @case_flags is a %NULL pointer then ASCII letters
                    171:  *   are left as they are, and other code points are treated as
                    172:  *   unflagged.
                    173:  * @output_length: The caller passes in the maximum number of ASCII
                    174:  *   code points that it can receive.  On successful return it will
                    175:  *   contain the number of ASCII code points actually output.
                    176:  * @output: An array of ASCII code points.  It is *not*
                    177:  *   null-terminated; it will contain zeros if and only if the @input
                    178:  *   contains zeros.  (Of course the caller can leave room for a
                    179:  *   terminator and add one if needed.)
                    180:  *
                    181:  * Converts a sequence of code points (presumed to be Unicode code
                    182:  * points) to Punycode.
                    183:  *
                    184:  * Return value: The return value can be any of the #Punycode_status
                    185:  *   values defined above except %PUNYCODE_BAD_INPUT.  If not
                    186:  *   %PUNYCODE_SUCCESS, then @output_size and @output might contain
                    187:  *   garbage.
                    188:  **/
1.2       moko      189: int punycode_encode (size_t input_length,
1.1       moko      190:                 const punycode_uint input[],
                    191:                 const unsigned char case_flags[],
                    192:                 size_t * output_length, char output[])
                    193: {
                    194:   punycode_uint input_len, n, delta, h, b, bias, j, m, q, k, t;
                    195:   size_t out, max_out;
                    196: 
                    197:   /* The Punycode spec assumes that the input length is the same type */
                    198:   /* of integer as a code point, so we need to convert the size_t to  */
                    199:   /* a punycode_uint, which could overflow.                           */
                    200: 
                    201:   if (input_length > maxint)
                    202:     return punycode_overflow;
                    203:   input_len = (punycode_uint) input_length;
                    204: 
                    205:   /* Initialize the state: */
                    206: 
                    207:   n = initial_n;
                    208:   delta = 0;
                    209:   out = 0;
                    210:   max_out = *output_length;
                    211:   bias = initial_bias;
                    212: 
                    213:   /* Handle the basic code points: */
                    214: 
                    215:   for (j = 0; j < input_len; ++j)
                    216:     {
                    217:       if (basic (input[j]))
                    218:        {
                    219:          if (max_out - out < 2)
                    220:            return punycode_big_output;
                    221:          output[out++] = case_flags ?
                    222:            encode_basic (input[j], case_flags[j]) : (char) input[j];
                    223:        }
                    224:       /* else if (input[j] < n) return punycode_bad_input; */
                    225:       /* (not needed for Punycode with unsigned code points) */
                    226:     }
                    227: 
                    228:   h = b = (punycode_uint) out;
                    229:   /* cannot overflow because out <= input_len <= maxint */
                    230: 
                    231:   /* h is the number of code points that have been handled, b is the  */
                    232:   /* number of basic code points, and out is the number of ASCII code */
                    233:   /* points that have been output.                                    */
                    234: 
                    235:   if (b > 0)
                    236:     output[out++] = delimiter;
                    237: 
                    238:   /* Main encoding loop: */
                    239: 
                    240:   while (h < input_len)
                    241:     {
                    242:       /* All non-basic code points < n have been     */
                    243:       /* handled already.  Find the next larger one: */
                    244: 
                    245:       for (m = maxint, j = 0; j < input_len; ++j)
                    246:        {
                    247:          /* if (basic(input[j])) continue; */
                    248:          /* (not needed for Punycode) */
                    249:          if (input[j] >= n && input[j] < m)
                    250:            m = input[j];
                    251:        }
                    252: 
                    253:       /* Increase delta enough to advance the decoder's    */
                    254:       /* <n,i> state to <m,0>, but guard against overflow: */
                    255: 
                    256:       if (m - n > (maxint - delta) / (h + 1))
                    257:        return punycode_overflow;
                    258:       delta += (m - n) * (h + 1);
                    259:       n = m;
                    260: 
                    261:       for (j = 0; j < input_len; ++j)
                    262:        {
                    263:          /* Punycode does not need to check whether input[j] is basic: */
                    264:          if (input[j] < n /* || basic(input[j]) */ )
                    265:            {
                    266:              if (++delta == 0)
                    267:                return punycode_overflow;
                    268:            }
                    269: 
                    270:          if (input[j] == n)
                    271:            {
                    272:              /* Represent delta as a generalized variable-length integer: */
                    273: 
                    274:              for (q = delta, k = base;; k += base)
                    275:                {
                    276:                  if (out >= max_out)
                    277:                    return punycode_big_output;
                    278:                  t = k <= bias /* + tmin */ ? tmin :   /* +tmin not needed */
                    279:                    k >= bias + tmax ? tmax : k - bias;
                    280:                  if (q < t)
                    281:                    break;
                    282:                  output[out++] = encode_digit (t + (q - t) % (base - t), 0);
                    283:                  q = (q - t) / (base - t);
                    284:                }
                    285: 
                    286:              output[out++] = encode_digit (q, case_flags && case_flags[j]);
                    287:              bias = adapt (delta, h + 1, h == b);
                    288:              delta = 0;
                    289:              ++h;
                    290:            }
                    291:        }
                    292: 
                    293:       ++delta, ++n;
                    294:     }
                    295: 
                    296:   *output_length = out;
                    297:   return punycode_success;
                    298: }
                    299: 
                    300: /*** Main decode function ***/
                    301: 
                    302: /**
                    303:  * punycode_decode:
                    304:  * @input_length: The number of ASCII code points in the @input array.
                    305:  * @input: An array of ASCII code points (0..7F).
                    306:  * @output_length: The caller passes in the maximum number of code
                    307:  *   points that it can receive into the @output array (which is also
                    308:  *   the maximum number of flags that it can receive into the
                    309:  *   @case_flags array, if @case_flags is not a %NULL pointer).  On
                    310:  *   successful return it will contain the number of code points
                    311:  *   actually output (which is also the number of flags actually
                    312:  *   output, if case_flags is not a null pointer).  The decoder will
                    313:  *   never need to output more code points than the number of ASCII
                    314:  *   code points in the input, because of the way the encoding is
                    315:  *   defined.  The number of code points output cannot exceed the
                    316:  *   maximum possible value of a punycode_uint, even if the supplied
                    317:  *   @output_length is greater than that.
                    318:  * @output: An array of code points like the input argument of
                    319:  *   punycode_encode() (see above).
                    320:  * @case_flags: A %NULL pointer (if the flags are not needed by the
                    321:  *   caller) or an array of boolean values parallel to the @output
                    322:  *   array.  Nonzero (true, flagged) suggests that the corresponding
                    323:  *   Unicode character be forced to uppercase by the caller (if
                    324:  *   possible), and zero (false, unflagged) suggests that it be forced
                    325:  *   to lowercase (if possible).  ASCII code points (0..7F) are output
                    326:  *   already in the proper case, but their flags will be set
                    327:  *   appropriately so that applying the flags would be harmless.
                    328:  *
                    329:  * Converts Punycode to a sequence of code points (presumed to be
                    330:  * Unicode code points).
                    331:  *
                    332:  * Return value: The return value can be any of the #Punycode_status
                    333:  *   values defined above.  If not %PUNYCODE_SUCCESS, then
                    334:  *   @output_length, @output, and @case_flags might contain garbage.
                    335:  *
                    336:  **/
1.2       moko      337: int punycode_decode (size_t input_length,
1.1       moko      338:                 const char input[],
                    339:                 size_t * output_length,
                    340:                 punycode_uint output[], unsigned char case_flags[])
                    341: {
                    342:   punycode_uint n, out, i, max_out, bias, oldi, w, k, digit, t;
                    343:   size_t b, j, in;
                    344: 
                    345:   /* Initialize the state: */
                    346: 
                    347:   n = initial_n;
                    348:   out = i = 0;
                    349:   max_out = *output_length > maxint ? maxint
                    350:     : (punycode_uint) * output_length;
                    351:   bias = initial_bias;
                    352: 
                    353:   /* Handle the basic code points:  Let b be the number of input code */
                    354:   /* points before the last delimiter, or 0 if there is none, then    */
                    355:   /* copy the first b code points to the output.                      */
                    356: 
                    357:   for (b = j = 0; j < input_length; ++j)
                    358:     if (delim (input[j]))
                    359:       b = j;
                    360:   if (b > max_out)
                    361:     return punycode_big_output;
                    362: 
                    363:   for (j = 0; j < b; ++j)
                    364:     {
                    365:       if (case_flags)
                    366:        case_flags[out] = flagged (input[j]);
                    367:       if (!basic (input[j]))
                    368:        return punycode_bad_input;
                    369:       output[out++] = input[j];
                    370:     }
                    371: 
                    372:   /* Main decoding loop:  Start just after the last delimiter if any  */
                    373:   /* basic code points were copied; start at the beginning otherwise. */
                    374: 
                    375:   for (in = b > 0 ? b + 1 : 0; in < input_length; ++out)
                    376:     {
                    377: 
                    378:       /* in is the index of the next ASCII code point to be consumed, */
                    379:       /* and out is the number of code points in the output array.    */
                    380: 
                    381:       /* Decode a generalized variable-length integer into delta,  */
                    382:       /* which gets added to i.  The overflow checking is easier   */
                    383:       /* if we increase i as we go, then subtract off its starting */
                    384:       /* value at the end to obtain delta.                         */
                    385: 
                    386:       for (oldi = i, w = 1, k = base;; k += base)
                    387:        {
                    388:          if (in >= input_length)
                    389:            return punycode_bad_input;
                    390:          digit = decode_digit (input[in++]);
                    391:          if (digit >= base)
                    392:            return punycode_bad_input;
                    393:          if (digit > (maxint - i) / w)
                    394:            return punycode_overflow;
                    395:          i += digit * w;
                    396:          t = k <= bias /* + tmin */ ? tmin :   /* +tmin not needed */
                    397:            k >= bias + tmax ? tmax : k - bias;
                    398:          if (digit < t)
                    399:            break;
                    400:          if (w > maxint / (base - t))
                    401:            return punycode_overflow;
                    402:          w *= (base - t);
                    403:        }
                    404: 
                    405:       bias = adapt (i - oldi, out + 1, oldi == 0);
                    406: 
                    407:       /* i was supposed to wrap around from out+1 to 0,   */
                    408:       /* incrementing n each time, so we'll fix that now: */
                    409: 
                    410:       if (i / (out + 1) > maxint - n)
                    411:        return punycode_overflow;
                    412:       n += i / (out + 1);
                    413:       i %= (out + 1);
                    414: 
                    415:       /* Insert n at position i of the output: */
                    416: 
                    417:       /* not needed for Punycode: */
                    418:       /* if (basic(n)) return punycode_invalid_input; */
                    419:       if (out >= max_out)
                    420:        return punycode_big_output;
                    421: 
                    422:       if (case_flags)
                    423:        {
                    424:          memmove (case_flags + i + 1, case_flags + i, out - i);
                    425:          /* Case of last ASCII code point determines case flag: */
1.4     ! moko      426:          case_flags[i] = flagged (input[in - 1]);
1.1       moko      427:        }
                    428: 
                    429:       memmove (output + i + 1, output + i, (out - i) * sizeof *output);
                    430:       output[i++] = n;
                    431:     }
                    432: 
                    433:   *output_length = (size_t) out;
                    434:   /* cannot overflow because out <= old value of *output_length */
                    435:   return punycode_success;
                    436: }
                    437: 
                    438: /**
                    439:  * punycode_uint
                    440:  *
                    441:  * Unicode code point data type, this is always a 32 bit unsigned
                    442:  * integer.
                    443:  */
                    444: 
                    445: /**
                    446:  * Punycode_status
                    447:  * @PUNYCODE_SUCCESS: Successful operation.  This value is guaranteed
                    448:  *   to always be zero, the remaining ones are only guaranteed to hold
                    449:  *   non-zero values, for logical comparison purposes.
                    450:  * @PUNYCODE_BAD_INPUT: Input is invalid.
                    451:  * @PUNYCODE_BIG_OUTPUT: Output would exceed the space provided.
                    452:  * @PUNYCODE_OVERFLOW: Input needs wider integers to process.
                    453:  *
                    454:  * Enumerated return codes of punycode_encode() and punycode_decode().
                    455:  * The value 0 is guaranteed to always correspond to success.
                    456:  */

E-mail: