utf7.h 10.0 KB

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