Back to index

lightning-sunbird  0.9+nobinonly
punycode.h
Go to the documentation of this file.
00001 /*
00002 punycode.c from RFC 3492
00003 http://www.nicemice.net/idn/
00004 Adam M. Costello
00005 http://www.nicemice.net/amc/
00006 
00007 This is ANSI C code (C89) implementing Punycode (RFC 3492).
00008 
00009 
00010 
00011 C. Disclaimer and license
00012 
00013     Regarding this entire document or any portion of it (including
00014     the pseudocode and C code), the author makes no guarantees and
00015     is not responsible for any damage resulting from its use.  The
00016     author grants irrevocable permission to anyone to use, modify,
00017     and distribute it in any way that does not diminish the rights
00018     of anyone else to use, modify, and distribute it, provided that
00019     redistributed derivative works do not contain misleading author or
00020     version information.  Derivative works need not be licensed under
00021     similar terms.
00022 */
00023 
00024 #ifdef __cplusplus
00025 extern "C" {
00026 #endif /* __cplusplus */
00027 
00028 /************************************************************/
00029 /* Public interface (would normally go in its own .h file): */
00030 
00031 #include <limits.h>
00032 
00033 enum punycode_status {
00034   punycode_success,
00035   punycode_bad_input,   /* Input is invalid.                       */
00036   punycode_big_output,  /* Output would exceed the space provided. */
00037   punycode_overflow     /* Input needs wider integers to process.  */
00038 };
00039 
00040 #if UINT_MAX >= (1 << 26) - 1
00041 typedef unsigned int punycode_uint;
00042 #else
00043 typedef unsigned long punycode_uint;
00044 #endif
00045 
00046 enum punycode_status punycode_encode(
00047   punycode_uint input_length,
00048   const punycode_uint input[],
00049   const unsigned char case_flags[],
00050   punycode_uint *output_length,
00051   char output[] );
00052 
00053     /* punycode_encode() converts Unicode to Punycode.  The input     */
00054     /* is represented as an array of Unicode code points (not code    */
00055     /* units; surrogate pairs are not allowed), and the output        */
00056     /* will be represented as an array of ASCII code points.  The     */
00057     /* output string is *not* null-terminated; it will contain        */
00058     /* zeros if and only if the input contains zeros.  (Of course     */
00059     /* the caller can leave room for a terminator and add one if      */
00060     /* needed.)  The input_length is the number of code points in     */
00061     /* the input.  The output_length is an in/out argument: the       */
00062     /* caller passes in the maximum number of code points that it     */
00063     /* can receive, and on successful return it will contain the      */
00064     /* number of code points actually output.  The case_flags array   */
00065     /* holds input_length boolean values, where nonzero suggests that */
00066     /* the corresponding Unicode character be forced to uppercase     */
00067     /* after being decoded (if possible), and zero suggests that      */
00068     /* it be forced to lowercase (if possible).  ASCII code points    */
00069     /* are encoded literally, except that ASCII letters are forced    */
00070     /* to uppercase or lowercase according to the corresponding       */
00071     /* uppercase flags.  If case_flags is a null pointer then ASCII   */
00072     /* letters are left as they are, and other code points are        */
00073     /* treated as if their uppercase flags were zero.  The return     */
00074     /* value can be any of the punycode_status values defined above   */
00075     /* except punycode_bad_input; if not punycode_success, then       */
00076     /* output_size and output might contain garbage.                  */
00077 
00078 enum punycode_status punycode_decode(
00079   punycode_uint input_length,
00080   const char input[],
00081   punycode_uint *output_length,
00082   punycode_uint output[],
00083   unsigned char case_flags[] );
00084 
00085     /* punycode_decode() converts Punycode to Unicode.  The input is  */
00086     /* represented as an array of ASCII code points, and the output   */
00087     /* will be represented as an array of Unicode code points.  The   */
00088     /* input_length is the number of code points in the input.  The   */
00089     /* output_length is an in/out argument: the caller passes in      */
00090     /* the maximum number of code points that it can receive, and     */
00091     /* on successful return it will contain the actual number of      */
00092     /* code points output.  The case_flags array needs room for at    */
00093     /* least output_length values, or it can be a null pointer if the */
00094     /* case information is not needed.  A nonzero flag suggests that  */
00095     /* the corresponding Unicode character be forced to uppercase     */
00096     /* by the caller (if possible), while zero suggests that it be    */
00097     /* forced to lowercase (if possible).  ASCII code points are      */
00098     /* output already in the proper case, but their flags will be set */
00099     /* appropriately so that applying the flags would be harmless.    */
00100     /* The return value can be any of the punycode_status values      */
00101     /* defined above; if not punycode_success, then output_length,    */
00102     /* output, and case_flags might contain garbage.  On success, the */
00103     /* decoder will never need to write an output_length greater than */
00104     /* input_length, because of how the encoding is defined.          */
00105 
00106 #ifdef __cplusplus
00107 }
00108 #endif /* __cplusplus */