PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext_zend_compat/php-src/ext/standard/base64.cpp

https://gitlab.com/Blueprint-Marketing/hhvm
C++ | 267 lines | 168 code | 25 blank | 74 comment | 46 complexity | 0cbdce4241d215d0011841a3ae355256 MD5 | raw file
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2013 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: Jim Winstead <jimw@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include <string.h>
  20. #include "php.h"
  21. #include "base64.h"
  22. /* {{{ base64 tables */
  23. static const char base64_table[] = {
  24. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  25. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  26. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  27. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  28. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
  29. };
  30. static const char base64_pad = '=';
  31. static const short base64_reverse_table[256] = {
  32. -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
  33. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  34. -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
  35. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
  36. -2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  37. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
  38. -2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  39. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
  40. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  41. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  42. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  43. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  44. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  45. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  46. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  47. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
  48. };
  49. /* }}} */
  50. PHPAPI unsigned char *php_base64_encode(const unsigned char *str, int length, int *ret_length) /* {{{ */
  51. {
  52. const unsigned char *current = str;
  53. unsigned char *p;
  54. unsigned char *result;
  55. if (length < 0) {
  56. if (ret_length != NULL) {
  57. *ret_length = 0;
  58. }
  59. return NULL;
  60. }
  61. result = (unsigned char *) safe_emalloc((length + 2) / 3, 4 * sizeof(char), 1);
  62. p = result;
  63. while (length > 2) { /* keep going until we have less than 24 bits */
  64. *p++ = base64_table[current[0] >> 2];
  65. *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
  66. *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
  67. *p++ = base64_table[current[2] & 0x3f];
  68. current += 3;
  69. length -= 3; /* we just handle 3 octets of data */
  70. }
  71. /* now deal with the tail end of things */
  72. if (length != 0) {
  73. *p++ = base64_table[current[0] >> 2];
  74. if (length > 1) {
  75. *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
  76. *p++ = base64_table[(current[1] & 0x0f) << 2];
  77. *p++ = base64_pad;
  78. } else {
  79. *p++ = base64_table[(current[0] & 0x03) << 4];
  80. *p++ = base64_pad;
  81. *p++ = base64_pad;
  82. }
  83. }
  84. if (ret_length != NULL) {
  85. *ret_length = (int)(p - result);
  86. }
  87. *p = '\0';
  88. return result;
  89. }
  90. /* }}} */
  91. /* {{{ */
  92. /* generate reverse table (do not set index 0 to 64)
  93. static unsigned short base64_reverse_table[256];
  94. #define rt base64_reverse_table
  95. void php_base64_init(void)
  96. {
  97. char *s = emalloc(10240), *sp;
  98. char *chp;
  99. short idx;
  100. for(ch = 0; ch < 256; ch++) {
  101. chp = strchr(base64_table, ch);
  102. if(ch && chp) {
  103. idx = chp - base64_table;
  104. if (idx >= 64) idx = -1;
  105. rt[ch] = idx;
  106. } else {
  107. rt[ch] = -1;
  108. }
  109. }
  110. sp = s;
  111. sprintf(sp, "static const short base64_reverse_table[256] = {\n");
  112. for(ch =0; ch < 256;) {
  113. sp = s+strlen(s);
  114. sprintf(sp, "\t% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,\n", rt[ch+0], rt[ch+1], rt[ch+2], rt[ch+3], rt[ch+4], rt[ch+5], rt[ch+6], rt[ch+7], rt[ch+8], rt[ch+9], rt[ch+10], rt[ch+11], rt[ch+12], rt[ch+13], rt[ch+14], rt[ch+15]);
  115. ch += 16;
  116. }
  117. sprintf(sp, "};");
  118. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Reverse_table:\n%s", s);
  119. efree(s);
  120. }
  121. */
  122. /* }}} */
  123. PHPAPI unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length) /* {{{ */
  124. {
  125. return php_base64_decode_ex(str, length, ret_length, 0);
  126. }
  127. /* }}} */
  128. PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length, int *ret_length, zend_bool strict) /* {{{ */
  129. {
  130. const unsigned char *current = str;
  131. int ch, i = 0, j = 0, k;
  132. /* this sucks for threaded environments */
  133. unsigned char *result;
  134. result = (unsigned char *)safe_emalloc(length, 1, 1);
  135. /* run through the whole string, converting as we go */
  136. while ((ch = *current++) != '\0' && length-- > 0) {
  137. if (ch == base64_pad) {
  138. if (*current != '=' && ((i % 4) == 1 || (strict && length > 0))) {
  139. if ((i % 4) != 1) {
  140. while (isspace(*(++current))) {
  141. continue;
  142. }
  143. if (*current == '\0') {
  144. continue;
  145. }
  146. }
  147. efree(result);
  148. return NULL;
  149. }
  150. continue;
  151. }
  152. ch = base64_reverse_table[ch];
  153. if ((!strict && ch < 0) || ch == -1) { /* a space or some other separator character, we simply skip over */
  154. continue;
  155. } else if (ch == -2) {
  156. efree(result);
  157. return NULL;
  158. }
  159. switch(i % 4) {
  160. case 0:
  161. result[j] = ch << 2;
  162. break;
  163. case 1:
  164. result[j++] |= ch >> 4;
  165. result[j] = (ch & 0x0f) << 4;
  166. break;
  167. case 2:
  168. result[j++] |= ch >>2;
  169. result[j] = (ch & 0x03) << 6;
  170. break;
  171. case 3:
  172. result[j++] |= ch;
  173. break;
  174. }
  175. i++;
  176. }
  177. k = j;
  178. /* mop things up if we ended on a boundary */
  179. if (ch == base64_pad) {
  180. switch(i % 4) {
  181. case 1:
  182. efree(result);
  183. return NULL;
  184. case 2:
  185. k++;
  186. case 3:
  187. result[k] = 0;
  188. }
  189. }
  190. if(ret_length) {
  191. *ret_length = j;
  192. }
  193. result[j] = '\0';
  194. return result;
  195. }
  196. /* }}} */
  197. /* {{{ proto string base64_encode(string str)
  198. Encodes string using MIME base64 algorithm */
  199. PHP_FUNCTION(base64_encode)
  200. {
  201. char *str;
  202. unsigned char *result;
  203. int str_len, ret_length;
  204. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
  205. return;
  206. }
  207. result = php_base64_encode((unsigned char*)str, str_len, &ret_length);
  208. if (result != NULL) {
  209. RETVAL_STRINGL((char*)result, ret_length, 0);
  210. } else {
  211. RETURN_FALSE;
  212. }
  213. }
  214. /* }}} */
  215. /* {{{ proto string base64_decode(string str[, bool strict])
  216. Decodes string using MIME base64 algorithm */
  217. PHP_FUNCTION(base64_decode)
  218. {
  219. char *str;
  220. unsigned char *result;
  221. zend_bool strict = 0;
  222. int str_len, ret_length;
  223. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &str, &str_len, &strict) == FAILURE) {
  224. return;
  225. }
  226. result = php_base64_decode_ex((unsigned char*)str, str_len, &ret_length, strict);
  227. if (result != NULL) {
  228. RETVAL_STRINGL((char*)result, ret_length, 0);
  229. } else {
  230. RETURN_FALSE;
  231. }
  232. }
  233. /* }}} */
  234. /*
  235. * Local variables:
  236. * tab-width: 4
  237. * c-basic-offset: 4
  238. * End:
  239. * vim600: sw=4 ts=4 fdm=marker
  240. * vim<600: sw=4 ts=4
  241. */