PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/standard/quot_print.c

http://github.com/php/php-src
C | 277 lines | 212 code | 27 blank | 38 comment | 98 complexity | dd180ad05ec164e71783b789dfe50919 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  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. | http://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. register size_t i;
  47. register unsigned const char *p1;
  48. register unsigned char *p2;
  49. register 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. /* {{{ proto string quoted_printable_decode(string str)
  178. Convert a quoted-printable string to an 8 bit string */
  179. PHP_FUNCTION(quoted_printable_decode)
  180. {
  181. zend_string *arg1;
  182. char *str_in;
  183. zend_string *str_out;
  184. size_t i = 0, j = 0, k;
  185. ZEND_PARSE_PARAMETERS_START(1, 1)
  186. Z_PARAM_STR(arg1)
  187. ZEND_PARSE_PARAMETERS_END();
  188. if (ZSTR_LEN(arg1) == 0) {
  189. /* shortcut */
  190. RETURN_EMPTY_STRING();
  191. }
  192. str_in = ZSTR_VAL(arg1);
  193. str_out = zend_string_alloc(ZSTR_LEN(arg1), 0);
  194. while (str_in[i]) {
  195. switch (str_in[i]) {
  196. case '=':
  197. if (str_in[i + 1] && str_in[i + 2] &&
  198. isxdigit((int) str_in[i + 1]) &&
  199. isxdigit((int) str_in[i + 2]))
  200. {
  201. ZSTR_VAL(str_out)[j++] = (php_hex2int((int) str_in[i + 1]) << 4)
  202. + php_hex2int((int) str_in[i + 2]);
  203. i += 3;
  204. } else /* check for soft line break according to RFC 2045*/ {
  205. k = 1;
  206. while (str_in[i + k] && ((str_in[i + k] == 32) || (str_in[i + k] == 9))) {
  207. /* Possibly, skip spaces/tabs at the end of line */
  208. k++;
  209. }
  210. if (!str_in[i + k]) {
  211. /* End of line reached */
  212. i += k;
  213. }
  214. else if ((str_in[i + k] == 13) && (str_in[i + k + 1] == 10)) {
  215. /* CRLF */
  216. i += k + 2;
  217. }
  218. else if ((str_in[i + k] == 13) || (str_in[i + k] == 10)) {
  219. /* CR or LF */
  220. i += k + 1;
  221. }
  222. else {
  223. ZSTR_VAL(str_out)[j++] = str_in[i++];
  224. }
  225. }
  226. break;
  227. default:
  228. ZSTR_VAL(str_out)[j++] = str_in[i++];
  229. }
  230. }
  231. ZSTR_VAL(str_out)[j] = '\0';
  232. ZSTR_LEN(str_out) = j;
  233. RETVAL_NEW_STR(str_out);
  234. }
  235. /* }}} */
  236. /* {{{ proto string quoted_printable_encode(string str) */
  237. PHP_FUNCTION(quoted_printable_encode)
  238. {
  239. zend_string *str;
  240. zend_string *new_str;
  241. ZEND_PARSE_PARAMETERS_START(1, 1)
  242. Z_PARAM_STR(str)
  243. ZEND_PARSE_PARAMETERS_END();
  244. if (!ZSTR_LEN(str)) {
  245. RETURN_EMPTY_STRING();
  246. }
  247. new_str = php_quot_print_encode((unsigned char *)ZSTR_VAL(str), ZSTR_LEN(str));
  248. RETURN_STR(new_str);
  249. }
  250. /* }}} */