zend_smart_str.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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: Dmitry Stogov <dmitry@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <zend.h>
  17. #include "zend_smart_str.h"
  18. #include "zend_smart_string.h"
  19. #define SMART_STR_OVERHEAD (ZEND_MM_OVERHEAD + _ZSTR_HEADER_SIZE + 1)
  20. #define SMART_STR_START_SIZE 256
  21. #define SMART_STR_START_LEN (SMART_STR_START_SIZE - SMART_STR_OVERHEAD)
  22. #define SMART_STR_PAGE 4096
  23. #define SMART_STR_NEW_LEN(len) \
  24. (ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STR_OVERHEAD, SMART_STR_PAGE) - SMART_STR_OVERHEAD)
  25. ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len)
  26. {
  27. if (UNEXPECTED(!str->s)) {
  28. str->a = len <= SMART_STR_START_LEN
  29. ? SMART_STR_START_LEN
  30. : SMART_STR_NEW_LEN(len);
  31. str->s = zend_string_alloc(str->a, 0);
  32. ZSTR_LEN(str->s) = 0;
  33. } else {
  34. str->a = SMART_STR_NEW_LEN(len);
  35. str->s = (zend_string *) erealloc2(str->s, str->a + _ZSTR_HEADER_SIZE + 1, _ZSTR_HEADER_SIZE + ZSTR_LEN(str->s));
  36. }
  37. }
  38. ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len)
  39. {
  40. if (UNEXPECTED(!str->s)) {
  41. str->a = len <= SMART_STR_START_LEN
  42. ? SMART_STR_START_LEN
  43. : SMART_STR_NEW_LEN(len);
  44. str->s = zend_string_alloc(str->a, 1);
  45. ZSTR_LEN(str->s) = 0;
  46. } else {
  47. str->a = SMART_STR_NEW_LEN(len);
  48. str->s = (zend_string *) perealloc(str->s, str->a + _ZSTR_HEADER_SIZE + 1, 1);
  49. }
  50. }
  51. /* Windows uses VK_ESCAPE instead of \e */
  52. #ifndef VK_ESCAPE
  53. #define VK_ESCAPE '\e'
  54. #endif
  55. static size_t zend_compute_escaped_string_len(const char *s, size_t l) {
  56. size_t i, len = l;
  57. for (i = 0; i < l; ++i) {
  58. char c = s[i];
  59. if (c == '\n' || c == '\r' || c == '\t' ||
  60. c == '\f' || c == '\v' || c == '\\' || c == VK_ESCAPE) {
  61. len += 1;
  62. } else if (c < 32 || c > 126) {
  63. len += 3;
  64. }
  65. }
  66. return len;
  67. }
  68. ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l) {
  69. char *res;
  70. size_t i, len = zend_compute_escaped_string_len(s, l);
  71. smart_str_alloc(str, len, 0);
  72. res = &ZSTR_VAL(str->s)[ZSTR_LEN(str->s)];
  73. ZSTR_LEN(str->s) += len;
  74. for (i = 0; i < l; ++i) {
  75. unsigned char c = s[i];
  76. if (c < 32 || c == '\\' || c > 126) {
  77. *res++ = '\\';
  78. switch (c) {
  79. case '\n': *res++ = 'n'; break;
  80. case '\r': *res++ = 'r'; break;
  81. case '\t': *res++ = 't'; break;
  82. case '\f': *res++ = 'f'; break;
  83. case '\v': *res++ = 'v'; break;
  84. case '\\': *res++ = '\\'; break;
  85. case VK_ESCAPE: *res++ = 'e'; break;
  86. default:
  87. *res++ = 'x';
  88. if ((c >> 4) < 10) {
  89. *res++ = (c >> 4) + '0';
  90. } else {
  91. *res++ = (c >> 4) + 'A' - 10;
  92. }
  93. if ((c & 0xf) < 10) {
  94. *res++ = (c & 0xf) + '0';
  95. } else {
  96. *res++ = (c & 0xf) + 'A' - 10;
  97. }
  98. }
  99. } else {
  100. *res++ = c;
  101. }
  102. }
  103. }
  104. ZEND_API void ZEND_FASTCALL smart_str_append_double(
  105. smart_str *str, double num, int precision, bool zero_fraction) {
  106. char buf[ZEND_DOUBLE_MAX_LENGTH];
  107. /* Model snprintf precision behavior. */
  108. zend_gcvt(num, precision ? precision : 1, '.', 'E', buf);
  109. smart_str_appends(str, buf);
  110. if (zero_fraction && zend_finite(num) && !strchr(buf, '.')) {
  111. smart_str_appendl(str, ".0", 2);
  112. }
  113. }
  114. ZEND_API void smart_str_append_printf(smart_str *dest, const char *format, ...) {
  115. va_list arg;
  116. va_start(arg, format);
  117. zend_printf_to_smart_str(dest, format, arg);
  118. va_end(arg);
  119. }
  120. #define SMART_STRING_OVERHEAD (ZEND_MM_OVERHEAD + 1)
  121. #define SMART_STRING_START_SIZE 256
  122. #define SMART_STRING_START_LEN (SMART_STRING_START_SIZE - SMART_STRING_OVERHEAD)
  123. #define SMART_STRING_PAGE 4096
  124. ZEND_API void ZEND_FASTCALL _smart_string_alloc_persistent(smart_string *str, size_t len)
  125. {
  126. if (!str->c) {
  127. str->len = 0;
  128. if (len <= SMART_STRING_START_LEN) {
  129. str->a = SMART_STRING_START_LEN;
  130. } else {
  131. str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
  132. }
  133. str->c = pemalloc(str->a + 1, 1);
  134. } else {
  135. if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
  136. zend_error(E_ERROR, "String size overflow");
  137. }
  138. len += str->len;
  139. str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
  140. str->c = perealloc(str->c, str->a + 1, 1);
  141. }
  142. }
  143. ZEND_API void ZEND_FASTCALL _smart_string_alloc(smart_string *str, size_t len)
  144. {
  145. if (!str->c) {
  146. str->len = 0;
  147. if (len <= SMART_STRING_START_LEN) {
  148. str->a = SMART_STRING_START_LEN;
  149. str->c = emalloc(SMART_STRING_START_LEN + 1);
  150. } else {
  151. str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
  152. if (EXPECTED(str->a < (ZEND_MM_CHUNK_SIZE - SMART_STRING_OVERHEAD))) {
  153. str->c = emalloc_large(str->a + 1);
  154. } else {
  155. /* allocate a huge chunk */
  156. str->c = emalloc(str->a + 1);
  157. }
  158. }
  159. } else {
  160. if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
  161. zend_error(E_ERROR, "String size overflow");
  162. }
  163. len += str->len;
  164. str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
  165. str->c = erealloc2(str->c, str->a + 1, str->len);
  166. }
  167. }
  168. ZEND_API void ZEND_FASTCALL smart_str_append_escaped_truncated(smart_str *str, zend_string *value, size_t length)
  169. {
  170. smart_str_append_escaped(str, ZSTR_VAL(value), MIN(length, ZSTR_LEN(value)));
  171. if (ZSTR_LEN(value) > length) {
  172. smart_str_appendl(str, "...", sizeof("...")-1);
  173. }
  174. }
  175. ZEND_API void ZEND_FASTCALL smart_str_append_scalar(smart_str *dest, zval *value, size_t truncate) {
  176. ZEND_ASSERT(Z_TYPE_P(value) <= IS_STRING);
  177. switch (Z_TYPE_P(value)) {
  178. case IS_UNDEF:
  179. case IS_NULL:
  180. smart_str_appendl(dest, "NULL", sizeof("NULL")-1);
  181. break;
  182. case IS_TRUE:
  183. case IS_FALSE:
  184. smart_str_appends(dest, Z_TYPE_P(value) == IS_TRUE ? "true" : "false");
  185. break;
  186. case IS_DOUBLE:
  187. smart_str_append_double(dest, Z_DVAL_P(value), (int) EG(precision), true);
  188. break;
  189. case IS_LONG:
  190. smart_str_append_long(dest, Z_LVAL_P(value));
  191. break;
  192. case IS_STRING:
  193. smart_str_appendc(dest, '\'');
  194. smart_str_append_escaped_truncated(dest, Z_STR_P(value), truncate);
  195. smart_str_appendc(dest, '\'');
  196. break;
  197. EMPTY_SWITCH_DEFAULT_CASE();
  198. }
  199. }