quot_print.c 7.0 KB

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