uuencode.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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: Ilia Alshanetsky <ilia@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /*
  19. * Portions of this code are based on Berkeley's uuencode/uudecode
  20. * implementation.
  21. *
  22. * Copyright (c) 1983, 1993
  23. * The Regents of the University of California. All rights reserved.
  24. *
  25. * Redistribution and use in source and binary forms, with or without
  26. * modification, are permitted provided that the following conditions
  27. * are met:
  28. * 1. Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in the
  32. * documentation and/or other materials provided with the distribution.
  33. * 3. All advertising materials mentioning features or use of this software
  34. * must display the following acknowledgement:
  35. * This product includes software developed by the University of
  36. * California, Berkeley and its contributors.
  37. * 4. Neither the name of the University nor the names of its contributors
  38. * may be used to endorse or promote products derived from this software
  39. * without specific prior written permission.
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. */
  53. #include <math.h>
  54. #include "php.h"
  55. #include "php_uuencode.h"
  56. #define PHP_UU_ENC(c) ((c) ? ((c) & 077) + ' ' : '`')
  57. #define PHP_UU_ENC_C2(c) PHP_UU_ENC(((*(c) << 4) & 060) | ((*((c) + 1) >> 4) & 017))
  58. #define PHP_UU_ENC_C3(c) PHP_UU_ENC(((*(c + 1) << 2) & 074) | ((*((c) + 2) >> 6) & 03))
  59. #define PHP_UU_DEC(c) (((c) - ' ') & 077)
  60. PHPAPI zend_string *php_uuencode(char *src, size_t src_len) /* {{{ */
  61. {
  62. size_t len = 45;
  63. char *p, *s, *e, *ee;
  64. zend_string *dest;
  65. /* encoded length is ~ 38% greater than the original
  66. Use 1.5 for easier calculation.
  67. */
  68. dest = zend_string_safe_alloc(src_len/2, 3, 46, 0);
  69. p = ZSTR_VAL(dest);
  70. s = src;
  71. e = src + src_len;
  72. while ((s + 3) < e) {
  73. ee = s + len;
  74. if (ee > e) {
  75. ee = e;
  76. len = ee - s;
  77. if (len % 3) {
  78. ee = s + (int) (floor((double)len / 3) * 3);
  79. }
  80. }
  81. *p++ = PHP_UU_ENC(len);
  82. while (s < ee) {
  83. *p++ = PHP_UU_ENC(*s >> 2);
  84. *p++ = PHP_UU_ENC_C2(s);
  85. *p++ = PHP_UU_ENC_C3(s);
  86. *p++ = PHP_UU_ENC(*(s + 2) & 077);
  87. s += 3;
  88. }
  89. if (len == 45) {
  90. *p++ = '\n';
  91. }
  92. }
  93. if (s < e) {
  94. if (len == 45) {
  95. *p++ = PHP_UU_ENC(e - s);
  96. len = 0;
  97. }
  98. *p++ = PHP_UU_ENC(*s >> 2);
  99. *p++ = PHP_UU_ENC_C2(s);
  100. *p++ = ((e - s) > 1) ? PHP_UU_ENC_C3(s) : PHP_UU_ENC('\0');
  101. *p++ = ((e - s) > 2) ? PHP_UU_ENC(*(s + 2) & 077) : PHP_UU_ENC('\0');
  102. }
  103. if (len < 45) {
  104. *p++ = '\n';
  105. }
  106. *p++ = PHP_UU_ENC('\0');
  107. *p++ = '\n';
  108. *p = '\0';
  109. dest = zend_string_truncate(dest, p - ZSTR_VAL(dest), 0);
  110. return dest;
  111. }
  112. /* }}} */
  113. PHPAPI zend_string *php_uudecode(char *src, size_t src_len) /* {{{ */
  114. {
  115. size_t len, total_len=0;
  116. char *s, *e, *p, *ee;
  117. zend_string *dest;
  118. dest = zend_string_alloc((size_t) ceil(src_len * 0.75), 0);
  119. p = ZSTR_VAL(dest);
  120. s = src;
  121. e = src + src_len;
  122. while (s < e) {
  123. if ((len = PHP_UU_DEC(*s++)) == 0) {
  124. break;
  125. }
  126. /* sanity check */
  127. if (len > src_len) {
  128. goto err;
  129. }
  130. total_len += len;
  131. ee = s + (len == 45 ? 60 : (int) floor(len * 1.33));
  132. /* sanity check */
  133. if (ee > e) {
  134. goto err;
  135. }
  136. while (s < ee) {
  137. if(s+4 > e) {
  138. goto err;
  139. }
  140. *p++ = PHP_UU_DEC(*s) << 2 | PHP_UU_DEC(*(s + 1)) >> 4;
  141. *p++ = PHP_UU_DEC(*(s + 1)) << 4 | PHP_UU_DEC(*(s + 2)) >> 2;
  142. *p++ = PHP_UU_DEC(*(s + 2)) << 6 | PHP_UU_DEC(*(s + 3));
  143. s += 4;
  144. }
  145. if (len < 45) {
  146. break;
  147. }
  148. /* skip \n */
  149. s++;
  150. }
  151. assert(p >= ZSTR_VAL(dest));
  152. if ((len = total_len) > (size_t)(p - ZSTR_VAL(dest))) {
  153. *p++ = PHP_UU_DEC(*s) << 2 | PHP_UU_DEC(*(s + 1)) >> 4;
  154. if (len > 1) {
  155. *p++ = PHP_UU_DEC(*(s + 1)) << 4 | PHP_UU_DEC(*(s + 2)) >> 2;
  156. if (len > 2) {
  157. *p++ = PHP_UU_DEC(*(s + 2)) << 6 | PHP_UU_DEC(*(s + 3));
  158. }
  159. }
  160. }
  161. ZSTR_LEN(dest) = total_len;
  162. ZSTR_VAL(dest)[ZSTR_LEN(dest)] = '\0';
  163. return dest;
  164. err:
  165. zend_string_efree(dest);
  166. return NULL;
  167. }
  168. /* }}} */
  169. /* {{{ proto string convert_uuencode(string data)
  170. uuencode a string */
  171. PHP_FUNCTION(convert_uuencode)
  172. {
  173. zend_string *src;
  174. ZEND_PARSE_PARAMETERS_START(1, 1)
  175. Z_PARAM_STR(src)
  176. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  177. if (ZSTR_LEN(src) < 1) { RETURN_FALSE; }
  178. RETURN_STR(php_uuencode(ZSTR_VAL(src), ZSTR_LEN(src)));
  179. }
  180. /* }}} */
  181. /* {{{ proto string convert_uudecode(string data)
  182. decode a uuencoded string */
  183. PHP_FUNCTION(convert_uudecode)
  184. {
  185. zend_string *src;
  186. zend_string *dest;
  187. ZEND_PARSE_PARAMETERS_START(1, 1)
  188. Z_PARAM_STR(src)
  189. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  190. if (ZSTR_LEN(src) < 1) { RETURN_FALSE; }
  191. if ((dest = php_uudecode(ZSTR_VAL(src), ZSTR_LEN(src))) == NULL) {
  192. php_error_docref(NULL, E_WARNING, "The given parameter is not a valid uuencoded string");
  193. RETURN_FALSE;
  194. }
  195. RETURN_STR(dest);
  196. }
  197. /* }}} */
  198. /*
  199. * Local variables:
  200. * tab-width: 4
  201. * c-basic-offset: 4
  202. * End:
  203. * vim600: noet sw=4 ts=4 fdm=marker
  204. * vim<600: noet sw=4 ts=4
  205. */