quot_print.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. /* $Id$ */
  19. #include <stdlib.h>
  20. #ifdef HAVE_UNISTD_H
  21. #include <unistd.h>
  22. #endif
  23. #include <string.h>
  24. #include <errno.h>
  25. #include "php.h"
  26. #include "quot_print.h"
  27. #include <stdio.h>
  28. /*
  29. * Converting HEX char to INT value
  30. */
  31. static char php_hex2int(int c) /* {{{ */
  32. {
  33. if (isdigit(c)) {
  34. return c - '0';
  35. }
  36. else if (c >= 'A' && c <= 'F') {
  37. return c - 'A' + 10;
  38. }
  39. else if (c >= 'a' && c <= 'f') {
  40. return c - 'a' + 10;
  41. }
  42. else {
  43. return -1;
  44. }
  45. }
  46. /* }}} */
  47. PHPAPI unsigned char *php_quot_print_decode(const unsigned char *str, size_t length, size_t *ret_length, int replace_us_by_ws) /* {{{ */
  48. {
  49. register unsigned int i;
  50. register unsigned const char *p1;
  51. register unsigned char *p2;
  52. register unsigned int h_nbl, l_nbl;
  53. size_t decoded_len, buf_size;
  54. unsigned char *retval;
  55. static unsigned int hexval_tbl[256] = {
  56. 64, 64, 64, 64, 64, 64, 64, 64, 64, 32, 16, 64, 64, 16, 64, 64,
  57. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  58. 32, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  59. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 64, 64, 64, 64, 64, 64,
  60. 64, 10, 11, 12, 13, 14, 15, 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, 10, 11, 12, 13, 14, 15, 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. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  72. };
  73. if (replace_us_by_ws) {
  74. replace_us_by_ws = '_';
  75. }
  76. i = length, p1 = str; buf_size = length;
  77. while (i > 1 && *p1 != '\0') {
  78. if (*p1 == '=') {
  79. buf_size -= 2;
  80. p1++;
  81. i--;
  82. }
  83. p1++;
  84. i--;
  85. }
  86. retval = emalloc(buf_size + 1);
  87. i = length; p1 = str; p2 = retval;
  88. decoded_len = 0;
  89. while (i > 0 && *p1 != '\0') {
  90. if (*p1 == '=') {
  91. i--, p1++;
  92. if (i == 0 || *p1 == '\0') {
  93. break;
  94. }
  95. h_nbl = hexval_tbl[*p1];
  96. if (h_nbl < 16) {
  97. /* next char should be a hexadecimal digit */
  98. if ((--i) == 0 || (l_nbl = hexval_tbl[*(++p1)]) >= 16) {
  99. efree(retval);
  100. return NULL;
  101. }
  102. *(p2++) = (h_nbl << 4) | l_nbl, decoded_len++;
  103. i--, p1++;
  104. } else if (h_nbl < 64) {
  105. /* soft line break */
  106. while (h_nbl == 32) {
  107. if (--i == 0 || (h_nbl = hexval_tbl[*(++p1)]) == 64) {
  108. efree(retval);
  109. return NULL;
  110. }
  111. }
  112. if (p1[0] == '\r' && i >= 2 && p1[1] == '\n') {
  113. i--, p1++;
  114. }
  115. i--, p1++;
  116. } else {
  117. efree(retval);
  118. return NULL;
  119. }
  120. } else {
  121. *(p2++) = (replace_us_by_ws == *p1 ? '\x20': *p1);
  122. i--, p1++, decoded_len++;
  123. }
  124. }
  125. *p2 = '\0';
  126. *ret_length = decoded_len;
  127. return retval;
  128. }
  129. /* }}} */
  130. #define PHP_QPRINT_MAXL 75
  131. PHPAPI unsigned char *php_quot_print_encode(const unsigned char *str, size_t length, size_t *ret_length) /* {{{ */
  132. {
  133. unsigned long lp = 0;
  134. unsigned char c, *ret, *d;
  135. char *hex = "0123456789ABCDEF";
  136. ret = safe_emalloc(3, length + (((3 * length)/(PHP_QPRINT_MAXL-9)) + 1), 1);
  137. d = 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_length = d - ret;
  171. ret = erealloc(ret, *ret_length + 1);
  172. return ret;
  173. }
  174. /* }}} */
  175. /*
  176. *
  177. * Decoding Quoted-printable string.
  178. *
  179. */
  180. /* {{{ proto string quoted_printable_decode(string str)
  181. Convert a quoted-printable string to an 8 bit string */
  182. PHP_FUNCTION(quoted_printable_decode)
  183. {
  184. char *arg1, *str_in, *str_out;
  185. int arg1_len, i = 0, j = 0, k;
  186. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg1, &arg1_len) == FAILURE) {
  187. return;
  188. }
  189. if (arg1_len == 0) {
  190. /* shortcut */
  191. RETURN_EMPTY_STRING();
  192. }
  193. str_in = arg1;
  194. str_out = emalloc(arg1_len + 1);
  195. while (str_in[i]) {
  196. switch (str_in[i]) {
  197. case '=':
  198. if (str_in[i + 1] && str_in[i + 2] &&
  199. isxdigit((int) str_in[i + 1]) &&
  200. isxdigit((int) str_in[i + 2]))
  201. {
  202. str_out[j++] = (php_hex2int((int) str_in[i + 1]) << 4)
  203. + php_hex2int((int) str_in[i + 2]);
  204. i += 3;
  205. } else /* check for soft line break according to RFC 2045*/ {
  206. k = 1;
  207. while (str_in[i + k] && ((str_in[i + k] == 32) || (str_in[i + k] == 9))) {
  208. /* Possibly, skip spaces/tabs at the end of line */
  209. k++;
  210. }
  211. if (!str_in[i + k]) {
  212. /* End of line reached */
  213. i += k;
  214. }
  215. else if ((str_in[i + k] == 13) && (str_in[i + k + 1] == 10)) {
  216. /* CRLF */
  217. i += k + 2;
  218. }
  219. else if ((str_in[i + k] == 13) || (str_in[i + k] == 10)) {
  220. /* CR or LF */
  221. i += k + 1;
  222. }
  223. else {
  224. str_out[j++] = str_in[i++];
  225. }
  226. }
  227. break;
  228. default:
  229. str_out[j++] = str_in[i++];
  230. }
  231. }
  232. str_out[j] = '\0';
  233. RETVAL_STRINGL(str_out, j, 0);
  234. }
  235. /* }}} */
  236. /* {{{ proto string quoted_printable_encode(string str) */
  237. PHP_FUNCTION(quoted_printable_encode)
  238. {
  239. char *str, *new_str;
  240. int str_len;
  241. size_t new_str_len;
  242. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) != SUCCESS) {
  243. return;
  244. }
  245. if (!str_len) {
  246. RETURN_EMPTY_STRING();
  247. }
  248. new_str = (char *)php_quot_print_encode((unsigned char *)str, (size_t)str_len, &new_str_len);
  249. if (new_str_len > INT_MAX) {
  250. php_error_docref(NULL TSRMLS_CC, E_WARNING, "String too long, max length is %d", INT_MAX);
  251. efree(new_str);
  252. RETURN_FALSE;
  253. }
  254. RETURN_STRINGL(new_str, new_str_len, 0);
  255. }
  256. /* }}} */
  257. /*
  258. * Local variables:
  259. * tab-width: 4
  260. * c-basic-offset: 4
  261. * End:
  262. * vim600: sw=4 ts=4 fdm=marker
  263. * vim<600: sw=4 ts=4
  264. */