utf7.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (C) 1999-2001, 2008 Free Software Foundation, Inc.
  3. * This file is part of the GNU LIBICONV Library.
  4. *
  5. * The GNU LIBICONV Library is free software; you can redistribute it
  6. * and/or modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * The GNU LIBICONV Library is distributed in the hope that it will be
  11. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  17. * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
  18. * Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /*
  21. * UTF-7
  22. */
  23. /* Specification: RFC 2152 (and old RFC 1641, RFC 1642) */
  24. /* The original Base64 encoding is defined in RFC 2045. */
  25. /* Set of direct characters:
  26. * A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
  27. */
  28. static const unsigned char direct_tab[128/8] = {
  29. 0x00, 0x26, 0x00, 0x00, 0x81, 0xf3, 0xff, 0x87,
  30. 0xfe, 0xff, 0xff, 0x07, 0xfe, 0xff, 0xff, 0x07,
  31. };
  32. #define isdirect(ch) ((ch) < 128 && ((direct_tab[(ch)>>3] >> (ch & 7)) & 1))
  33. /* Set of direct and optional direct characters:
  34. * A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
  35. * ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
  36. */
  37. static const unsigned char xdirect_tab[128/8] = {
  38. 0x00, 0x26, 0x00, 0x00, 0xff, 0xf7, 0xff, 0xff,
  39. 0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0xff, 0x3f,
  40. };
  41. #define isxdirect(ch) ((ch) < 128 && ((xdirect_tab[(ch)>>3] >> (ch & 7)) & 1))
  42. /* Set of base64 characters, extended:
  43. * A-Z a-z 0-9 + / -
  44. */
  45. static const unsigned char xbase64_tab[128/8] = {
  46. 0x00, 0x00, 0x00, 0x00, 0x00, 0xa8, 0xff, 0x03,
  47. 0xfe, 0xff, 0xff, 0x07, 0xfe, 0xff, 0xff, 0x07,
  48. };
  49. #define isxbase64(ch) ((ch) < 128 && ((xbase64_tab[(ch)>>3] >> (ch & 7)) & 1))
  50. /*
  51. * The state is structured as follows:
  52. * bit 1..0: shift
  53. * bit 7..2: data
  54. * Precise meaning:
  55. * shift data
  56. * 0 0 not inside base64 encoding
  57. * 1 0 inside base64, no pending bits
  58. * 2 XXXX00 inside base64, 4 bits remain from 2nd byte
  59. * 3 XX0000 inside base64, 2 bits remain from 3rd byte
  60. */
  61. static int
  62. utf7_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  63. {
  64. state_t state = conv->istate;
  65. int count = 0; /* number of input bytes already read */
  66. if (state & 3)
  67. goto active;
  68. else
  69. goto inactive;
  70. inactive:
  71. {
  72. /* Here (state & 3) == 0 */
  73. if (n < count+1)
  74. goto none;
  75. {
  76. unsigned char c = *s;
  77. if (isxdirect(c)) {
  78. *pwc = (ucs4_t) c;
  79. conv->istate = state;
  80. return count+1;
  81. }
  82. if (c == '+') {
  83. if (n < count+2)
  84. goto none;
  85. if (s[1] == '-') {
  86. *pwc = (ucs4_t) '+';
  87. conv->istate = state;
  88. return count+2;
  89. }
  90. s++; count++;
  91. state = 1;
  92. goto active;
  93. }
  94. goto ilseq;
  95. }
  96. }
  97. active:
  98. {
  99. /* base64 encoding active */
  100. unsigned int wc = 0;
  101. state_t base64state = state;
  102. unsigned int kmax = 2; /* number of payload bytes to read */
  103. unsigned int k = 0; /* number of payload bytes already read */
  104. unsigned int base64count = 0; /* number of base64 bytes already read */
  105. for (;;) {
  106. unsigned char c = *s;
  107. unsigned int i;
  108. if (c >= 'A' && c <= 'Z')
  109. i = c-'A';
  110. else if (c >= 'a' && c <= 'z')
  111. i = c-'a'+26;
  112. else if (c >= '0' && c <= '9')
  113. i = c-'0'+52;
  114. else if (c == '+')
  115. i = 62;
  116. else if (c == '/')
  117. i = 63;
  118. else {
  119. /* c terminates base64 encoding */
  120. if (base64state & -4)
  121. goto ilseq; /* data must be 0, otherwise illegal */
  122. if (base64count)
  123. goto ilseq; /* partial UTF-16 characters are invalid */
  124. if (c == '-') {
  125. s++; count++;
  126. }
  127. state = 0;
  128. goto inactive;
  129. }
  130. s++; base64count++;
  131. /* read 6 bits: 0 <= i < 64 */
  132. switch (base64state & 3) {
  133. case 1: /* inside base64, no pending bits */
  134. base64state = (i << 2) | 0; break;
  135. case 0: /* inside base64, 6 bits remain from 1st byte */
  136. wc = (wc << 8) | (base64state & -4) | (i >> 4); k++;
  137. base64state = ((i & 15) << 4) | 2; break;
  138. case 2: /* inside base64, 4 bits remain from 2nd byte */
  139. wc = (wc << 8) | (base64state & -4) | (i >> 2); k++;
  140. base64state = ((i & 3) << 6) | 3; break;
  141. case 3: /* inside base64, 2 bits remain from 3rd byte */
  142. wc = (wc << 8) | (base64state & -4) | i; k++;
  143. base64state = 1; break;
  144. }
  145. if (k == kmax) {
  146. /* UTF-16: When we see a High Surrogate, we must also decode
  147. the following Low Surrogate. */
  148. if (kmax == 2 && (wc >= 0xd800 && wc < 0xdc00))
  149. kmax = 4;
  150. else
  151. break;
  152. }
  153. if (n < count+base64count+1)
  154. goto none;
  155. }
  156. /* Here k = kmax > 0, hence base64count > 0. */
  157. if ((base64state & 3) == 0) abort();
  158. if (kmax == 4) {
  159. ucs4_t wc1 = wc >> 16;
  160. ucs4_t wc2 = wc & 0xffff;
  161. if (!(wc1 >= 0xd800 && wc1 < 0xdc00)) abort();
  162. if (!(wc2 >= 0xdc00 && wc2 < 0xe000)) goto ilseq;
  163. *pwc = 0x10000 + ((wc1 - 0xd800) << 10) + (wc2 - 0xdc00);
  164. } else {
  165. *pwc = wc;
  166. }
  167. conv->istate = base64state;
  168. return count+base64count;
  169. }
  170. none:
  171. conv->istate = state;
  172. return RET_TOOFEW(count);
  173. ilseq:
  174. conv->istate = state;
  175. return RET_SHIFT_ILSEQ(count);
  176. }
  177. /*
  178. * The state is structured as follows:
  179. * bit 1..0: shift
  180. * bit 7..2: data
  181. * Precise meaning:
  182. * shift data
  183. * 0 0 not inside base64 encoding
  184. * 1 0 inside base64, no pending bits
  185. * 2 XX00 inside base64, 2 bits known for 2nd byte
  186. * 3 XXXX inside base64, 4 bits known for 3rd byte
  187. */
  188. /* Define this to 1 if you want the so-called "optional direct" characters
  189. ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
  190. to be encoded. Define to 0 if you want them to be passed straight through,
  191. like the so-called "direct" characters.
  192. We set this to 1 because it's safer.
  193. */
  194. #define UTF7_ENCODE_OPTIONAL_CHARS 1
  195. static int
  196. utf7_wctomb (conv_t conv, unsigned char *r, ucs4_t iwc, int n)
  197. {
  198. state_t state = conv->ostate;
  199. unsigned int wc = iwc;
  200. int count = 0;
  201. if (state & 3)
  202. goto active;
  203. /*inactive:*/
  204. {
  205. if (UTF7_ENCODE_OPTIONAL_CHARS ? isdirect(wc) : isxdirect(wc)) {
  206. r[0] = (unsigned char) wc;
  207. /*conv->ostate = state;*/
  208. return 1;
  209. } else {
  210. *r++ = '+';
  211. if (wc == '+') {
  212. if (n < 2)
  213. return RET_TOOSMALL;
  214. *r = '-';
  215. /*conv->ostate = state;*/
  216. return 2;
  217. }
  218. count = 1;
  219. state = 1;
  220. goto active;
  221. }
  222. }
  223. active:
  224. {
  225. /* base64 encoding active */
  226. if (UTF7_ENCODE_OPTIONAL_CHARS ? isdirect(wc) : isxdirect(wc)) {
  227. /* deactivate base64 encoding */
  228. count += ((state & 3) >= 2 ? 1 : 0) + (isxbase64(wc) ? 1 : 0) + 1;
  229. if (n < count)
  230. return RET_TOOSMALL;
  231. if ((state & 3) >= 2) {
  232. unsigned int i = state & -4;
  233. unsigned char c;
  234. if (i < 26)
  235. c = i+'A';
  236. else if (i < 52)
  237. c = i-26+'a';
  238. else if (i < 62)
  239. c = i-52+'0';
  240. else if (i == 62)
  241. c = '+';
  242. else if (i == 63)
  243. c = '/';
  244. else
  245. abort();
  246. *r++ = c;
  247. }
  248. if (isxbase64(wc))
  249. *r++ = '-';
  250. state = 0;
  251. *r++ = (unsigned char) wc;
  252. conv->ostate = state;
  253. return count;
  254. } else {
  255. unsigned int k; /* number of payload bytes to write */
  256. if (wc < 0x10000) {
  257. k = 2;
  258. count += ((state & 3) >= 2 ? 3 : 2);
  259. } else if (wc < 0x110000) {
  260. unsigned int wc1 = 0xd800 + ((wc - 0x10000) >> 10);
  261. unsigned int wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
  262. wc = (wc1 << 16) | wc2;
  263. k = 4;
  264. count += ((state & 3) >= 3 ? 6 : 5);
  265. } else
  266. return RET_ILUNI;
  267. if (n < count)
  268. return RET_TOOSMALL;
  269. for (;;) {
  270. unsigned int i;
  271. unsigned char c;
  272. switch (state & 3) {
  273. case 0: /* inside base64, 6 bits known for 4th byte */
  274. c = (state & -4) >> 2; state = 1; break;
  275. case 1: /* inside base64, no pending bits */
  276. i = (wc >> (8 * --k)) & 0xff;
  277. c = i >> 2; state = ((i & 3) << 4) | 2; break;
  278. case 2: /* inside base64, 2 bits known for 2nd byte */
  279. i = (wc >> (8 * --k)) & 0xff;
  280. c = (state & -4) | (i >> 4); state = ((i & 15) << 2) | 3; break;
  281. case 3: /* inside base64, 4 bits known for 3rd byte */
  282. i = (wc >> (8 * --k)) & 0xff;
  283. c = (state & -4) | (i >> 6); state = ((i & 63) << 2) | 0; break;
  284. default: abort(); /* stupid gcc */
  285. }
  286. if (c < 26)
  287. c = c+'A';
  288. else if (c < 52)
  289. c = c-26+'a';
  290. else if (c < 62)
  291. c = c-52+'0';
  292. else if (c == 62)
  293. c = '+';
  294. else if (c == 63)
  295. c = '/';
  296. else
  297. abort();
  298. *r++ = c;
  299. if ((state & 3) && (k == 0))
  300. break;
  301. }
  302. conv->ostate = state;
  303. return count;
  304. }
  305. }
  306. }
  307. static int
  308. utf7_reset (conv_t conv, unsigned char *r, int n)
  309. {
  310. state_t state = conv->ostate;
  311. if (state & 3) {
  312. /* deactivate base64 encoding */
  313. unsigned int count = ((state & 3) >= 2 ? 1 : 0) + 1;
  314. if (n < count)
  315. return RET_TOOSMALL;
  316. if ((state & 3) >= 2) {
  317. unsigned int i = state & -4;
  318. unsigned char c;
  319. if (i < 26)
  320. c = i+'A';
  321. else if (i < 52)
  322. c = i-26+'a';
  323. else if (i < 62)
  324. c = i-52+'0';
  325. else if (i == 62)
  326. c = '+';
  327. else if (i == 63)
  328. c = '/';
  329. else
  330. abort();
  331. *r++ = c;
  332. }
  333. *r++ = '-';
  334. /* conv->ostate = 0; will be done by the caller */
  335. return count;
  336. } else
  337. return 0;
  338. }