uuencode.c 6.4 KB

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