PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/standard/uuencode.c

http://github.com/php/php-src
C | 231 lines | 131 code | 36 blank | 64 comment | 26 complexity | ddbbeeafc313ea4108f86926a32dfdd8 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: 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(char *src, size_t src_len) /* {{{ */
  59. {
  60. size_t len = 45;
  61. unsigned char *p, *s, *e, *ee;
  62. zend_string *dest;
  63. /* encoded length is ~ 38% greater than the original
  64. Use 1.5 for easier calculation.
  65. */
  66. dest = zend_string_safe_alloc(src_len/2, 3, 46, 0);
  67. p = (unsigned char *) ZSTR_VAL(dest);
  68. s = (unsigned char *) src;
  69. e = s + src_len;
  70. while ((s + 3) < e) {
  71. ee = s + len;
  72. if (ee > e) {
  73. ee = e;
  74. len = ee - s;
  75. if (len % 3) {
  76. ee = s + (int) (floor((double)len / 3) * 3);
  77. }
  78. }
  79. *p++ = PHP_UU_ENC(len);
  80. while (s < ee) {
  81. *p++ = PHP_UU_ENC(*s >> 2);
  82. *p++ = PHP_UU_ENC_C2(s);
  83. *p++ = PHP_UU_ENC_C3(s);
  84. *p++ = PHP_UU_ENC(*(s + 2) & 077);
  85. s += 3;
  86. }
  87. if (len == 45) {
  88. *p++ = '\n';
  89. }
  90. }
  91. if (s < e) {
  92. if (len == 45) {
  93. *p++ = PHP_UU_ENC(e - s);
  94. len = 0;
  95. }
  96. *p++ = PHP_UU_ENC(*s >> 2);
  97. *p++ = PHP_UU_ENC_C2(s);
  98. *p++ = ((e - s) > 1) ? PHP_UU_ENC_C3(s) : PHP_UU_ENC('\0');
  99. *p++ = ((e - s) > 2) ? PHP_UU_ENC(*(s + 2) & 077) : PHP_UU_ENC('\0');
  100. }
  101. if (len < 45) {
  102. *p++ = '\n';
  103. }
  104. *p++ = PHP_UU_ENC('\0');
  105. *p++ = '\n';
  106. *p = '\0';
  107. dest = zend_string_truncate(dest, (char *) p - ZSTR_VAL(dest), 0);
  108. return dest;
  109. }
  110. /* }}} */
  111. PHPAPI zend_string *php_uudecode(char *src, size_t src_len) /* {{{ */
  112. {
  113. size_t len, total_len=0;
  114. char *s, *e, *p, *ee;
  115. zend_string *dest;
  116. dest = zend_string_alloc((size_t) ceil(src_len * 0.75), 0);
  117. p = ZSTR_VAL(dest);
  118. s = src;
  119. e = src + src_len;
  120. while (s < e) {
  121. if ((len = PHP_UU_DEC(*s++)) == 0) {
  122. break;
  123. }
  124. /* sanity check */
  125. if (len > src_len) {
  126. goto err;
  127. }
  128. total_len += len;
  129. ee = s + (len == 45 ? 60 : (int) floor(len * 1.33));
  130. /* sanity check */
  131. if (ee > e) {
  132. goto err;
  133. }
  134. while (s < ee) {
  135. if(s+4 > e) {
  136. goto err;
  137. }
  138. *p++ = PHP_UU_DEC(*s) << 2 | PHP_UU_DEC(*(s + 1)) >> 4;
  139. *p++ = PHP_UU_DEC(*(s + 1)) << 4 | PHP_UU_DEC(*(s + 2)) >> 2;
  140. *p++ = PHP_UU_DEC(*(s + 2)) << 6 | PHP_UU_DEC(*(s + 3));
  141. s += 4;
  142. }
  143. if (len < 45) {
  144. break;
  145. }
  146. /* skip \n */
  147. s++;
  148. }
  149. assert(p >= ZSTR_VAL(dest));
  150. if ((len = total_len) > (size_t)(p - ZSTR_VAL(dest))) {
  151. *p++ = PHP_UU_DEC(*s) << 2 | PHP_UU_DEC(*(s + 1)) >> 4;
  152. if (len > 1) {
  153. *p++ = PHP_UU_DEC(*(s + 1)) << 4 | PHP_UU_DEC(*(s + 2)) >> 2;
  154. if (len > 2) {
  155. *p++ = PHP_UU_DEC(*(s + 2)) << 6 | PHP_UU_DEC(*(s + 3));
  156. }
  157. }
  158. }
  159. ZSTR_LEN(dest) = total_len;
  160. ZSTR_VAL(dest)[ZSTR_LEN(dest)] = '\0';
  161. return dest;
  162. err:
  163. zend_string_efree(dest);
  164. return NULL;
  165. }
  166. /* }}} */
  167. /* {{{ proto string|false convert_uuencode(string data)
  168. uuencode a string */
  169. PHP_FUNCTION(convert_uuencode)
  170. {
  171. zend_string *src;
  172. ZEND_PARSE_PARAMETERS_START(1, 1)
  173. Z_PARAM_STR(src)
  174. ZEND_PARSE_PARAMETERS_END();
  175. if (ZSTR_LEN(src) < 1) { RETURN_FALSE; }
  176. RETURN_STR(php_uuencode(ZSTR_VAL(src), ZSTR_LEN(src)));
  177. }
  178. /* }}} */
  179. /* {{{ proto string|false convert_uudecode(string data)
  180. decode a uuencoded string */
  181. PHP_FUNCTION(convert_uudecode)
  182. {
  183. zend_string *src;
  184. zend_string *dest;
  185. ZEND_PARSE_PARAMETERS_START(1, 1)
  186. Z_PARAM_STR(src)
  187. ZEND_PARSE_PARAMETERS_END();
  188. if (ZSTR_LEN(src) < 1) { RETURN_FALSE; }
  189. if ((dest = php_uudecode(ZSTR_VAL(src), ZSTR_LEN(src))) == NULL) {
  190. php_error_docref(NULL, E_WARNING, "The given parameter is not a valid uuencoded string");
  191. RETURN_FALSE;
  192. }
  193. RETURN_STR(dest);
  194. }
  195. /* }}} */