PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/release/src/router/php/ext/mbstring/libmbfl/filters/mbfilter_utf7imap.c

https://gitlab.com/envieidoc/advancedtomato2
C | 372 lines | 299 code | 30 blank | 43 comment | 100 complexity | 5ec09279a61aa319bd73f7af6cc5b6b6 MD5 | raw file
  1. /*
  2. * "streamable kanji code filter and converter"
  3. * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
  4. *
  5. * LICENSE NOTICES
  6. *
  7. * This file is part of "streamable kanji code filter and converter",
  8. * which is distributed under the terms of GNU Lesser General Public
  9. * License (version 2) as published by the Free Software Foundation.
  10. *
  11. * This software is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with "streamable kanji code filter and converter";
  18. * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  19. * Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * The author of this file:
  22. *
  23. */
  24. /*
  25. * The source code included in this files was separated from mbfilter.c
  26. * by moriyoshi koizumi <moriyoshi@php.net> on 4 dec 2002.
  27. *
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #include "mbfilter.h"
  33. #include "mbfilter_utf7imap.h"
  34. const mbfl_encoding mbfl_encoding_utf7imap = {
  35. mbfl_no_encoding_utf7imap,
  36. "UTF7-IMAP",
  37. NULL,
  38. NULL,
  39. NULL,
  40. MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_SHFTCODE
  41. };
  42. const struct mbfl_convert_vtbl vtbl_utf7imap_wchar = {
  43. mbfl_no_encoding_utf7imap,
  44. mbfl_no_encoding_wchar,
  45. mbfl_filt_conv_common_ctor,
  46. mbfl_filt_conv_common_dtor,
  47. mbfl_filt_conv_utf7imap_wchar,
  48. mbfl_filt_conv_common_flush };
  49. const struct mbfl_convert_vtbl vtbl_wchar_utf7imap = {
  50. mbfl_no_encoding_wchar,
  51. mbfl_no_encoding_utf7imap,
  52. mbfl_filt_conv_common_ctor,
  53. mbfl_filt_conv_common_dtor,
  54. mbfl_filt_conv_wchar_utf7imap,
  55. mbfl_filt_conv_wchar_utf7imap_flush };
  56. #define CK(statement) do { if ((statement) < 0) return (-1); } while (0)
  57. /*
  58. * UTF7-IMAP => wchar
  59. */
  60. int mbfl_filt_conv_utf7imap_wchar(int c, mbfl_convert_filter *filter)
  61. {
  62. int s, n;
  63. n = -1;
  64. if (filter->status != 0) { /* Modified Base64 */
  65. if (c >= 0x41 && c <= 0x5a) { /* A - Z */
  66. n = c - 65;
  67. } else if (c >= 0x61 && c <= 0x7a) { /* a - z */
  68. n = c - 71;
  69. } else if (c >= 0x30 && c <= 0x39) { /* 0 - 9 */
  70. n = c + 4;
  71. } else if (c == 0x2b) { /* '+' */
  72. n = 62;
  73. } else if (c == 0x2c) { /* ',' */
  74. n = 63;
  75. }
  76. if (n < 0 || n > 63) {
  77. if (c == 0x2d) {
  78. if (filter->status == 1) { /* "&-" -> "&" */
  79. CK((*filter->output_function)(0x26, filter->data));
  80. }
  81. } else if (c >= 0 && c < 0x80) { /* ASCII exclude '-' */
  82. CK((*filter->output_function)(c, filter->data));
  83. } else { /* illegal character */
  84. s = c & MBFL_WCSGROUP_MASK;
  85. s |= MBFL_WCSGROUP_THROUGH;
  86. CK((*filter->output_function)(s, filter->data));
  87. }
  88. filter->cache = 0;
  89. filter->status = 0;
  90. return c;
  91. }
  92. }
  93. switch (filter->status) {
  94. /* directly encoded characters */
  95. case 0:
  96. if (c == 0x26) { /* '&' shift character */
  97. filter->status++;
  98. } else if (c >= 0 && c < 0x80) { /* ASCII */
  99. CK((*filter->output_function)(c, filter->data));
  100. } else { /* illegal character */
  101. s = c & MBFL_WCSGROUP_MASK;
  102. s |= MBFL_WCSGROUP_THROUGH;
  103. CK((*filter->output_function)(s, filter->data));
  104. }
  105. break;
  106. /* decode Modified Base64 */
  107. case 1:
  108. case 2:
  109. filter->cache |= n << 10;
  110. filter->status = 3;
  111. break;
  112. case 3:
  113. filter->cache |= n << 4;
  114. filter->status = 4;
  115. break;
  116. case 4:
  117. s = ((n >> 2) & 0xf) | (filter->cache & 0xffff);
  118. n = (n & 0x3) << 14;
  119. filter->status = 5;
  120. if (s >= 0xd800 && s < 0xdc00) {
  121. s = (((s & 0x3ff) << 16) + 0x400000) | n;
  122. filter->cache = s;
  123. } else if (s >= 0xdc00 && s < 0xe000) {
  124. s &= 0x3ff;
  125. s |= (filter->cache & 0xfff0000) >> 6;
  126. filter->cache = n;
  127. if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
  128. CK((*filter->output_function)(s, filter->data));
  129. } else { /* illegal character */
  130. s &= MBFL_WCSGROUP_MASK;
  131. s |= MBFL_WCSGROUP_THROUGH;
  132. CK((*filter->output_function)(s, filter->data));
  133. }
  134. } else {
  135. filter->cache = n;
  136. CK((*filter->output_function)(s, filter->data));
  137. }
  138. break;
  139. case 5:
  140. filter->cache |= n << 8;
  141. filter->status = 6;
  142. break;
  143. case 6:
  144. filter->cache |= n << 2;
  145. filter->status = 7;
  146. break;
  147. case 7:
  148. s = ((n >> 4) & 0x3) | (filter->cache & 0xffff);
  149. n = (n & 0xf) << 12;
  150. filter->status = 8;
  151. if (s >= 0xd800 && s < 0xdc00) {
  152. s = (((s & 0x3ff) << 16) + 0x400000) | n;
  153. filter->cache = s;
  154. } else if (s >= 0xdc00 && s < 0xe000) {
  155. s &= 0x3ff;
  156. s |= (filter->cache & 0xfff0000) >> 6;
  157. filter->cache = n;
  158. if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
  159. CK((*filter->output_function)(s, filter->data));
  160. } else { /* illegal character */
  161. s &= MBFL_WCSGROUP_MASK;
  162. s |= MBFL_WCSGROUP_THROUGH;
  163. CK((*filter->output_function)(s, filter->data));
  164. }
  165. } else {
  166. filter->cache = n;
  167. CK((*filter->output_function)(s, filter->data));
  168. }
  169. break;
  170. case 8:
  171. filter->cache |= n << 6;
  172. filter->status = 9;
  173. break;
  174. case 9:
  175. s = n | (filter->cache & 0xffff);
  176. filter->status = 2;
  177. if (s >= 0xd800 && s < 0xdc00) {
  178. s = (((s & 0x3ff) << 16) + 0x400000);
  179. filter->cache = s;
  180. } else if (s >= 0xdc00 && s < 0xe000) {
  181. s &= 0x3ff;
  182. s |= (filter->cache & 0xfff0000) >> 6;
  183. filter->cache = 0;
  184. if (s >= MBFL_WCSPLANE_SUPMIN && s < MBFL_WCSPLANE_SUPMAX) {
  185. CK((*filter->output_function)(s, filter->data));
  186. } else { /* illegal character */
  187. s &= MBFL_WCSGROUP_MASK;
  188. s |= MBFL_WCSGROUP_THROUGH;
  189. CK((*filter->output_function)(s, filter->data));
  190. }
  191. } else {
  192. filter->cache = 0;
  193. CK((*filter->output_function)(s, filter->data));
  194. }
  195. break;
  196. default:
  197. filter->status = 0;
  198. break;
  199. }
  200. return c;
  201. }
  202. static const unsigned char mbfl_utf7imap_base64_table[] =
  203. {
  204. /* 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', */
  205. 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,
  206. /* 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
  207. 0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,
  208. /* 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', */
  209. 0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,
  210. /* 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
  211. 0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
  212. /* '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', ',', '\0' */
  213. 0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2c,0x00
  214. };
  215. /*
  216. * wchar => UTF7-IMAP
  217. */
  218. int mbfl_filt_conv_wchar_utf7imap(int c, mbfl_convert_filter *filter)
  219. {
  220. int n, s;
  221. n = 0;
  222. if (c == 0x26) {
  223. n = 1;
  224. } else if ((c >= 0x20 && c <= 0x7e) || c == 0) {
  225. n = 2;
  226. } else if (c >= 0 && c < MBFL_WCSPLANE_UCS2MAX) {
  227. ;
  228. } else if (c >= MBFL_WCSPLANE_SUPMIN && c < MBFL_WCSPLANE_SUPMAX) {
  229. s = ((c >> 10) - 0x40) | 0xd800;
  230. CK((*filter->filter_function)(s, filter));
  231. s = (c & 0x3ff) | 0xdc00;
  232. CK((*filter->filter_function)(s, filter));
  233. return c;
  234. } else {
  235. if (filter->illegal_mode != MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE) {
  236. CK(mbfl_filt_conv_illegal_output(c, filter));
  237. }
  238. return c;
  239. }
  240. switch (filter->status) {
  241. case 0:
  242. if (n != 0) { /* directly encode characters */
  243. CK((*filter->output_function)(c, filter->data));
  244. if (n == 1) {
  245. CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
  246. }
  247. } else { /* Modified Base64 */
  248. CK((*filter->output_function)(0x26, filter->data)); /* '&' */
  249. filter->status = 1;
  250. filter->cache = c;
  251. }
  252. break;
  253. /* encode Modified Base64 */
  254. case 1:
  255. s = filter->cache;
  256. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 10) & 0x3f], filter->data));
  257. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 4) & 0x3f], filter->data));
  258. if (n != 0) {
  259. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s << 2) & 0x3c], filter->data));
  260. CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
  261. CK((*filter->output_function)(c, filter->data));
  262. if (n == 1) {
  263. CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
  264. }
  265. filter->status = 0;
  266. } else {
  267. filter->status = 2;
  268. filter->cache = ((s & 0xf) << 16) | c;
  269. }
  270. break;
  271. case 2:
  272. s = filter->cache;
  273. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 14) & 0x3f], filter->data));
  274. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 8) & 0x3f], filter->data));
  275. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 2) & 0x3f], filter->data));
  276. if (n != 0) {
  277. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s << 4) & 0x30], filter->data));
  278. CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
  279. CK((*filter->output_function)(c, filter->data));
  280. if (n == 1) {
  281. CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
  282. }
  283. filter->status = 0;
  284. } else {
  285. filter->status = 3;
  286. filter->cache = ((s & 0x3) << 16) | c;
  287. }
  288. break;
  289. case 3:
  290. s = filter->cache;
  291. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 12) & 0x3f], filter->data));
  292. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(s >> 6) & 0x3f], filter->data));
  293. CK((*filter->output_function)(mbfl_utf7imap_base64_table[s & 0x3f], filter->data));
  294. if (n != 0) {
  295. CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
  296. CK((*filter->output_function)(c, filter->data));
  297. if (n == 1) {
  298. CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
  299. }
  300. filter->status = 0;
  301. } else {
  302. filter->status = 1;
  303. filter->cache = c;
  304. }
  305. break;
  306. default:
  307. filter->status = 0;
  308. break;
  309. }
  310. return c;
  311. }
  312. int mbfl_filt_conv_wchar_utf7imap_flush(mbfl_convert_filter *filter)
  313. {
  314. int status, cache;
  315. status = filter->status;
  316. cache = filter->cache;
  317. filter->status = 0;
  318. filter->cache = 0;
  319. /* flush fragments */
  320. switch (status) {
  321. case 1:
  322. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 10) & 0x3f], filter->data));
  323. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 4) & 0x3f], filter->data));
  324. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache << 2) & 0x3c], filter->data));
  325. CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
  326. break;
  327. case 2:
  328. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 14) & 0x3f], filter->data));
  329. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 8) & 0x3f], filter->data));
  330. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 2) & 0x3f], filter->data));
  331. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache << 4) & 0x30], filter->data));
  332. CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
  333. break;
  334. case 3:
  335. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 12) & 0x3f], filter->data));
  336. CK((*filter->output_function)(mbfl_utf7imap_base64_table[(cache >> 6) & 0x3f], filter->data));
  337. CK((*filter->output_function)(mbfl_utf7imap_base64_table[cache & 0x3f], filter->data));
  338. CK((*filter->output_function)(0x2d, filter->data)); /* '-' */
  339. break;
  340. }
  341. return 0;
  342. }