home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / imap / src / c-client / utf8.h < prev   
C/C++ Source or Header  |  1998-09-16  |  9KB  |  251 lines

  1. /*
  2.  * Program:    UTF-8 routines
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *        Internet: MRC@CAC.Washington.EDU
  11.  *
  12.  * Date:    11 June 1997
  13.  * Last Edited:    26 June 1998
  14.  *
  15.  * Copyright 1998 by the University of Washington
  16.  *
  17.  *  Permission to use, copy, modify, and distribute this software and its
  18.  * documentation for any purpose and without fee is hereby granted, provided
  19.  * that the above copyright notices appear in all copies and that both the
  20.  * above copyright notices and this permission notice appear in supporting
  21.  * documentation, and that the name of the University of Washington not be
  22.  * used in advertising or publicity pertaining to distribution of the software
  23.  * without specific, written prior permission.  This software is made
  24.  * available "as is", and
  25.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  26.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  27.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  28.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  29.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  30.  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  31.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN
  32.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  33.  *
  34.  */
  35.  
  36. /* UTF-8 size and conversion routines from UCS-2 values.  This will need to
  37.  * be changed if UTF-16 data (surrogate pairs) are ever an issue.
  38.  */
  39.  
  40. #define UTF8_SIZE(c) ((c & 0xff80) ? ((c & 0xf800) ? 3 : 2) : 1)
  41.  
  42. #define UTF8_PUT(b,c) {                    \
  43.   if (c & 0xff80) {        /* non-ASCII? */    \
  44.     if (c & 0xf800) {        /* three byte code */    \
  45.       *b++ = 0xe0 | (c >> 12);                \
  46.       *b++ = 0x80 | ((c >> 6) & 0x3f);            \
  47.     }                            \
  48.     else *b++ = 0xc0 | ((c >> 6) & 0x3f);        \
  49.     *b++ = 0x80 | (c & 0x3f);                 \
  50.   }                            \
  51.   else *b++ = c;                    \
  52. }
  53.  
  54. /* ISO-2022 engine states */
  55.  
  56. #define I2S_CHAR 0        /* character */
  57. #define I2S_ESC 1        /* previous character was ESC */
  58. #define I2S_MUL 2        /* previous character was multi-byte code */
  59. #define I2S_INT 3        /* previous character was intermediate */
  60.  
  61.  
  62. /* ISO-2022 Gn selections */
  63.  
  64. #define I2C_G0 0        /* G0 */
  65. #define I2C_G1 1        /* G1 */
  66. #define I2C_G2 2        /* G2 */
  67. #define I2C_G3 3        /* G3 */
  68. #define I2C_SG2 (2 << 2)    /* single shift G2 */
  69. #define I2C_SG3 (3 << 2)    /* single shift G2 */
  70.  
  71.  
  72. /* ISO-2022 octet definitions */
  73.  
  74. #define I2C_ESC 0x1b        /* ESCape */
  75.  
  76.     /* Intermediate character */
  77. #define I2C_STRUCTURE 0x20    /* announce code structure */
  78. #define I2C_C0 0x21        /* C0 */
  79. #define I2C_C1 0x22        /* C1 */
  80. #define I2C_CONTROL 0x23    /* single control function */
  81. #define I2C_MULTI 0x24        /* multi-byte character set */
  82. #define I2C_OTHER 0x25        /* other coding system */
  83. #define I2C_REVISED 0x26    /* revised registration */
  84. #define I2C_G0_94 0x28        /* G0 94-character set */
  85. #define I2C_G1_94 0x29        /* G1 94-character set */
  86. #define I2C_G2_94 0x2A        /* G2 94-character set */
  87. #define I2C_G3_94 0x2B        /* G3 94-character set */
  88. #define I2C_G0_96 0x2C        /* (not in ISO-2022) G0 96-character set */
  89. #define I2C_G1_96 0x2D        /* G1 96-character set */
  90. #define I2C_G2_96 0x2E        /* G2 96-character set */
  91. #define I2C_G3_96 0x2F        /* G3 96-character set */
  92.  
  93.     /* Locking shifts */
  94. #define I2C_SI 0x0f        /* lock shift to G0 (Shift In) */
  95. #define I2C_SO 0x0e        /* lock shift to G1 (Shift Out) */
  96.     /* prefixed by ESC */
  97. #define I2C_LS2 0x6e        /* lock shift to G2 */
  98. #define I2C_LS3 0x6f        /* lock shift to G3 */
  99. #define I2C_LS1R 0x7e        /* lock shift GR to G1 */
  100. #define I2C_LS2R 0x7d        /* lock shift GR to G2 */
  101. #define I2C_LS3R 0x7c        /* lock shift GR to G3 */
  102.  
  103.     /* Single shifts */
  104. #define I2C_SS2_ALT 0x8e    /* single shift to G2 (SS2) */
  105. #define I2C_SS3_ALT 0x8f    /* single shift to G3 (SS3) */
  106. #define I2C_SS2_ALT_7 0x19    /* single shift to G2 (SS2) */
  107. #define I2C_SS3_ALT_7 0x1d    /* single shift to G3 (SS3) */
  108.     /* prefixed by ESC */
  109. #define I2C_SS2 0x4e        /* single shift to G2 (SS2) */
  110. #define I2C_SS3 0x4f        /* single shift to G3 (SS3) */
  111.  
  112. /* Types of character sets */
  113.  
  114. #define I2CS_94 0x000        /* 94 character set */
  115. #define I2CS_96 0x100        /* 96 character set */
  116. #define I2CS_MUL 0x200        /* multi-byte */
  117. #define I2CS_94x94 (I2CS_MUL | I2CS_94)
  118. #define I2CS_96x96 (I2CS_MUL | I2CS_96)
  119.  
  120.  
  121. /* 94 character sets */
  122.  
  123.                 /* British localized ASCII */
  124. #define I2CS_BRITISH (I2CS_94 | 0x41)
  125.                 /* ASCII */
  126. #define I2CS_ASCII (I2CS_94 | 0x42)
  127.                 /* some buggy software does this */
  128. #define I2CS_JIS_BUGROM (I2CS_94 | 0x48)
  129.                 /* JIS X 0201-1976 right half */
  130. #define I2CS_JIS_KANA (I2CS_94 | 0x49)
  131.                 /* JIS X 0201-1976 left half */
  132. #define I2CS_JIS_ROMAN (I2CS_94 | 0x4a)
  133.                 /* JIS X 0208-1978 */
  134. #define I2CS_JIS_OLD (I2CS_94x94 | 0x40)
  135.                 /* GB 2312 */
  136. #define I2CS_GB (I2CS_94x94 | 0x41)
  137.                 /* JIS X 0208-1983 */
  138. #define I2CS_JIS_NEW (I2CS_94x94 | 0x42)
  139.                 /* KSC 5601 */
  140. #define I2CS_KSC (I2CS_94x94 | 0x43)
  141.                 /* JIS X 0212-1990 */
  142. #define I2CS_JIS_EXT (I2CS_94x94 | 0x44)
  143.                 /* CNS 11643 plane 1 */
  144. #define I2CS_CNS1 (I2CS_94x94 | 0x47)
  145.                 /* CNS 11643 plane 2 */
  146. #define I2CS_CNS2 (I2CS_94x94 | 0x48)
  147.                 /* CNS 11643 plane 3 */
  148. #define I2CS_CNS3 (I2CS_94x94 | 0x49)
  149.                 /* CNS 11643 plane 4 */
  150. #define I2CS_CNS4 (I2CS_94x94 | 0x4a)
  151.                 /* CNS 11643 plane 5 */
  152. #define I2CS_CNS5 (I2CS_94x94 | 0x4b)
  153.                 /* CNS 11643 plane 6 */
  154. #define I2CS_CNS6 (I2CS_94x94 | 0x4c)
  155.                 /* CNS 11643 plane 7 */
  156. #define I2CS_CNS7 (I2CS_94x94 | 0x4d)
  157.  
  158. /* 96 character sets */
  159.                 /* Latin-1 (Western Europe) */
  160. #define I2CS_ISO8859_1 (I2CS_96 | 0x41)
  161.                 /* Latin-2 (Czech, Slovak) */
  162. #define I2CS_ISO8859_2 (I2CS_96 | 0x42)
  163.                 /* Latin-3 (Dutch, Turkish) */
  164. #define I2CS_ISO8859_3 (I2CS_96 | 0x43)
  165.                 /* Latin-4 (Scandinavian) */
  166. #define I2CS_ISO8859_4 (I2CS_96 | 0x44)
  167.                 /* Greek */
  168. #define I2CS_ISO8859_7 (I2CS_96 | 0x46)
  169.                 /* Arabic */
  170. #define I2CS_ISO8859_6 (I2CS_96 | 0x47)
  171.                 /* Hebrew */
  172. #define I2CS_ISO8859_8 (I2CS_96 | 0x48)
  173.                 /* Cyrillic */
  174. #define I2CS_ISO8859_5 (I2CS_96 | 0x4c)
  175.                 /* Latin-5 (Finnish, Portuguese) */
  176. #define I2CS_ISO8859_9 (I2CS_96 | 0x4d)
  177.                 /* TIS 620 */
  178. #define I2CS_TIS620 (I2CS_96 | 0x54)
  179.                 /* Latin-6 (Northern Europe) */
  180. #define I2CS_ISO8859_10 (I2CS_96 | 0x56)
  181.                 /* Baltic */
  182. #define I2CS_ISO8859_13 (I2CS_96 | 0x59)
  183.                 /* Vietnamese */
  184. #define I2CS_VSCII (I2CS_96 | 0x5a)
  185.                 /* Euro (6/2 may be incorrect) */
  186. #define I2CS_ISO8859_15 (I2CS_96 | 0x62)
  187.  
  188. /* Miscellaneous definitions */
  189.  
  190. #define EUC_CS2 0x8e        /* single shift CS2 */
  191. #define EUC_CS3 0x8f        /* single shift CS3 */
  192.  
  193. #define BITS7 0x7f        /* 7-bit value mask */
  194. #define BIT8 0x80        /* 8th bit mask */
  195.  
  196.                 /* UCS2 codepoints */
  197. #define UCS2_POUNDSTERLING 0x00a3
  198. #define UCS2_YEN 0x00a5
  199. #define UCS2_OVERLINE 0x203e
  200. #define UCS2_KATAKANA 0xff61
  201. #define BOGON 0xfffd
  202.  
  203.                 /* hankaku katakana parameters */
  204. #define MIN_KANA_7 0x21
  205. #define MAX_KANA_7 0x5f
  206. #define KANA_7 (UCS2_KATAKANA - MIN_KANA_7)
  207. #define MIN_KANA_8 (MIN_KANA_7 | BIT8)
  208. #define MAX_KANA_8 (MAX_KANA_7 | BIT8)
  209. #define KANA_8 (UCS2_KATAKANA - MIN_KANA_8)
  210.  
  211.  
  212. /* Character set table support */
  213.  
  214. typedef void (*cstext_t) (SIZEDTEXT *text,SIZEDTEXT *ret,void *tab);
  215.  
  216. struct utf8_csent {
  217.   char *name;            /* character set name */
  218.   cstext_t dsp;            /* text conversion dispatch */
  219.   void *tab;            /* optional additional data */
  220. };
  221.  
  222.  
  223. struct utf8_eucparam {
  224.   unsigned int base_ku : 8;    /* base row */
  225.   unsigned int base_ten : 8;    /* base column */
  226.   unsigned int max_ku : 8;    /* maximum row */
  227.   unsigned int max_ten : 8;    /* maximum column */
  228.   void *tab;            /* conversion table */
  229. };
  230.  
  231. /* Function prototypes */
  232.  
  233. long utf8_text (SIZEDTEXT *text,char *charset,SIZEDTEXT *ret,long flags);
  234. void utf8_text_8859_1 (SIZEDTEXT *text,SIZEDTEXT *ret,void *tab);
  235. void utf8_text_1byte (SIZEDTEXT *text,SIZEDTEXT *ret,void *tab);
  236. void utf8_text_1byte8 (SIZEDTEXT *text,SIZEDTEXT *ret,void *tab);
  237. void utf8_text_euc (SIZEDTEXT *text,SIZEDTEXT *ret,void *tab);
  238. void utf8_text_dbyte (SIZEDTEXT *text,SIZEDTEXT *ret,void *tab);
  239. void utf8_text_dbyte2 (SIZEDTEXT *text,SIZEDTEXT *ret,void *tab);
  240. void utf8_text_sjis (SIZEDTEXT *text,SIZEDTEXT *ret,void *tab);
  241. void utf8_text_2022 (SIZEDTEXT *text,SIZEDTEXT *ret,void *tab);
  242. void utf8_searchpgm (SEARCHPGM *pgm,char *charset);
  243. void utf8_stringlist (STRINGLIST *st,char *charset);
  244. long utf8_mime2text (SIZEDTEXT *src,SIZEDTEXT *dst);
  245. unsigned char *mime2_token (unsigned char *s,unsigned char *se,
  246.                 unsigned char **t);
  247. unsigned char *mime2_text (unsigned char *s,unsigned char *se,
  248.                unsigned char **t);
  249. long mime2_decode (unsigned char *e,unsigned char *t,unsigned char *te,
  250.            SIZEDTEXT *txt);
  251.