quot_print.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Kirill Maximov <kir@actimind.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include <stdlib.h>
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "php.h"
  25. #include "quot_print.h"
  26. #include <stdio.h>
  27. /*
  28. * Converting HEX char to INT value
  29. */
  30. static char php_hex2int(int c) /* {{{ */
  31. {
  32. if (isdigit(c)) {
  33. return c - '0';
  34. }
  35. else if (c >= 'A' && c <= 'F') {
  36. return c - 'A' + 10;
  37. }
  38. else if (c >= 'a' && c <= 'f') {
  39. return c - 'a' + 10;
  40. }
  41. else {
  42. return -1;
  43. }
  44. }
  45. /* }}} */
  46. PHPAPI zend_string *php_quot_print_decode(const unsigned char *str, size_t length, int replace_us_by_ws) /* {{{ */
  47. {
  48. register size_t i;
  49. register unsigned const char *p1;
  50. register unsigned char *p2;
  51. register unsigned int h_nbl, l_nbl;
  52. size_t decoded_len, buf_size;
  53. zend_string *retval;
  54. static unsigned int hexval_tbl[256] = {
  55. 64, 64, 64, 64, 64, 64, 64, 64, 64, 32, 16, 64, 64, 16, 64, 64,
  56. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  57. 32, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  58. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 64, 64, 64, 64, 64, 64,
  59. 64, 10, 11, 12, 13, 14, 15, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  60. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  61. 64, 10, 11, 12, 13, 14, 15, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  62. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  63. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  64. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  65. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  66. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  67. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  68. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  69. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  70. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  71. };
  72. if (replace_us_by_ws) {
  73. replace_us_by_ws = '_';
  74. }
  75. i = length, p1 = str; buf_size = length;
  76. while (i > 1 && *p1 != '\0') {
  77. if (*p1 == '=') {
  78. buf_size -= 2;
  79. p1++;
  80. i--;
  81. }
  82. p1++;
  83. i--;
  84. }
  85. retval = zend_string_alloc(buf_size, 0);
  86. i = length; p1 = str; p2 = (unsigned char*)ZSTR_VAL(retval);
  87. decoded_len = 0;
  88. while (i > 0 && *p1 != '\0') {
  89. if (*p1 == '=') {
  90. i--, p1++;
  91. if (i == 0 || *p1 == '\0') {
  92. break;
  93. }
  94. h_nbl = hexval_tbl[*p1];
  95. if (h_nbl < 16) {
  96. /* next char should be a hexadecimal digit */
  97. if ((--i) == 0 || (l_nbl = hexval_tbl[*(++p1)]) >= 16) {
  98. efree(retval);
  99. return NULL;
  100. }
  101. *(p2++) = (h_nbl << 4) | l_nbl, decoded_len++;
  102. i--, p1++;
  103. } else if (h_nbl < 64) {
  104. /* soft line break */
  105. while (h_nbl == 32) {
  106. if (--i == 0 || (h_nbl = hexval_tbl[*(++p1)]) == 64) {
  107. efree(retval);
  108. return NULL;
  109. }
  110. }
  111. if (p1[0] == '\r' && i >= 2 && p1[1] == '\n') {
  112. i--, p1++;
  113. }
  114. i--, p1++;
  115. } else {
  116. efree(retval);
  117. return NULL;
  118. }
  119. } else {
  120. *(p2++) = (replace_us_by_ws == *p1 ? '\x20': *p1);
  121. i--, p1++, decoded_len++;
  122. }
  123. }
  124. *p2 = '\0';
  125. ZSTR_LEN(retval) = decoded_len;
  126. return retval;
  127. }
  128. /* }}} */
  129. #define PHP_QPRINT_MAXL 75
  130. PHPAPI zend_string *php_quot_print_encode(const unsigned char *str, size_t length) /* {{{ */
  131. {
  132. zend_ulong lp = 0;
  133. unsigned char c, *d;
  134. char *hex = "0123456789ABCDEF";
  135. zend_string *ret;
  136. ret = zend_string_safe_alloc(3, (length + (((3 * length)/(PHP_QPRINT_MAXL-9)) + 1)), 0, 0);
  137. d = (unsigned char*)ZSTR_VAL(ret);
  138. while (length--) {
  139. if (((c = *str++) == '\015') && (*str == '\012') && length > 0) {
  140. *d++ = '\015';
  141. *d++ = *str++;
  142. length--;
  143. lp = 0;
  144. } else {
  145. if (iscntrl (c) || (c == 0x7f) || (c & 0x80) || (c == '=') || ((c == ' ') && (*str == '\015'))) {
  146. if ((((lp+= 3) > PHP_QPRINT_MAXL) && (c <= 0x7f))
  147. || ((c > 0x7f) && (c <= 0xdf) && ((lp + 3) > PHP_QPRINT_MAXL))
  148. || ((c > 0xdf) && (c <= 0xef) && ((lp + 6) > PHP_QPRINT_MAXL))
  149. || ((c > 0xef) && (c <= 0xf4) && ((lp + 9) > PHP_QPRINT_MAXL))) {
  150. *d++ = '=';
  151. *d++ = '\015';
  152. *d++ = '\012';
  153. lp = 3;
  154. }
  155. *d++ = '=';
  156. *d++ = hex[c >> 4];
  157. *d++ = hex[c & 0xf];
  158. } else {
  159. if ((++lp) > PHP_QPRINT_MAXL) {
  160. *d++ = '=';
  161. *d++ = '\015';
  162. *d++ = '\012';
  163. lp = 1;
  164. }
  165. *d++ = c;
  166. }
  167. }
  168. }
  169. *d = '\0';
  170. ret = zend_string_truncate(ret, d - (unsigned char*)ZSTR_VAL(ret), 0);
  171. return ret;
  172. }
  173. /* }}} */
  174. /*
  175. *
  176. * Decoding Quoted-printable string.
  177. *
  178. */
  179. /* {{{ proto string quoted_printable_decode(string str)
  180. Convert a quoted-printable string to an 8 bit string */
  181. PHP_FUNCTION(quoted_printable_decode)
  182. {
  183. zend_string *arg1;
  184. char *str_in;
  185. zend_string *str_out;
  186. size_t i = 0, j = 0, k;
  187. ZEND_PARSE_PARAMETERS_START(1, 1)
  188. Z_PARAM_STR(arg1)
  189. ZEND_PARSE_PARAMETERS_END();
  190. if (ZSTR_LEN(arg1) == 0) {
  191. /* shortcut */
  192. RETURN_EMPTY_STRING();
  193. }
  194. str_in = ZSTR_VAL(arg1);
  195. str_out = zend_string_alloc(ZSTR_LEN(arg1), 0);
  196. while (str_in[i]) {
  197. switch (str_in[i]) {
  198. case '=':
  199. if (str_in[i + 1] && str_in[i + 2] &&
  200. isxdigit((int) str_in[i + 1]) &&
  201. isxdigit((int) str_in[i + 2]))
  202. {
  203. ZSTR_VAL(str_out)[j++] = (php_hex2int((int) str_in[i + 1]) << 4)
  204. + php_hex2int((int) str_in[i + 2]);
  205. i += 3;
  206. } else /* check for soft line break according to RFC 2045*/ {
  207. k = 1;
  208. while (str_in[i + k] && ((str_in[i + k] == 32) || (str_in[i + k] == 9))) {
  209. /* Possibly, skip spaces/tabs at the end of line */
  210. k++;
  211. }
  212. if (!str_in[i + k]) {
  213. /* End of line reached */
  214. i += k;
  215. }
  216. else if ((str_in[i + k] == 13) && (str_in[i + k + 1] == 10)) {
  217. /* CRLF */
  218. i += k + 2;
  219. }
  220. else if ((str_in[i + k] == 13) || (str_in[i + k] == 10)) {
  221. /* CR or LF */
  222. i += k + 1;
  223. }
  224. else {
  225. ZSTR_VAL(str_out)[j++] = str_in[i++];
  226. }
  227. }
  228. break;
  229. default:
  230. ZSTR_VAL(str_out)[j++] = str_in[i++];
  231. }
  232. }
  233. ZSTR_VAL(str_out)[j] = '\0';
  234. ZSTR_LEN(str_out) = j;
  235. RETVAL_NEW_STR(str_out);
  236. }
  237. /* }}} */
  238. /* {{{ proto string quoted_printable_encode(string str) */
  239. PHP_FUNCTION(quoted_printable_encode)
  240. {
  241. zend_string *str;
  242. zend_string *new_str;
  243. ZEND_PARSE_PARAMETERS_START(1, 1)
  244. Z_PARAM_STR(str)
  245. ZEND_PARSE_PARAMETERS_END();
  246. if (!ZSTR_LEN(str)) {
  247. RETURN_EMPTY_STRING();
  248. }
  249. new_str = php_quot_print_encode((unsigned char *)ZSTR_VAL(str), ZSTR_LEN(str));
  250. RETURN_STR(new_str);
  251. }
  252. /* }}} */
  253. /*
  254. * Local variables:
  255. * tab-width: 4
  256. * c-basic-offset: 4
  257. * End:
  258. * vim600: sw=4 ts=4 fdm=marker
  259. * vim<600: sw=4 ts=4
  260. */