zend_smart_str.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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: Dmitry Stogov <dmitry@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include <zend.h>
  19. #include "zend_smart_str.h"
  20. #include "zend_smart_string.h"
  21. #define SMART_STR_OVERHEAD (ZEND_MM_OVERHEAD + _ZSTR_HEADER_SIZE + 1)
  22. #define SMART_STR_START_SIZE 256
  23. #define SMART_STR_START_LEN (SMART_STR_START_SIZE - SMART_STR_OVERHEAD)
  24. #define SMART_STR_PAGE 4096
  25. #define SMART_STR_NEW_LEN(len) \
  26. (ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STR_OVERHEAD, SMART_STR_PAGE) - SMART_STR_OVERHEAD)
  27. ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len)
  28. {
  29. if (UNEXPECTED(!str->s)) {
  30. str->a = len <= SMART_STR_START_LEN
  31. ? SMART_STR_START_LEN
  32. : SMART_STR_NEW_LEN(len);
  33. str->s = zend_string_alloc(str->a, 0);
  34. ZSTR_LEN(str->s) = 0;
  35. } else {
  36. str->a = SMART_STR_NEW_LEN(len);
  37. str->s = (zend_string *) erealloc2(str->s, str->a + _ZSTR_HEADER_SIZE + 1, _ZSTR_HEADER_SIZE + ZSTR_LEN(str->s));
  38. }
  39. }
  40. ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len)
  41. {
  42. if (UNEXPECTED(!str->s)) {
  43. str->a = len <= SMART_STR_START_SIZE
  44. ? SMART_STR_START_LEN
  45. : SMART_STR_NEW_LEN(len);
  46. str->s = zend_string_alloc(str->a, 1);
  47. ZSTR_LEN(str->s) = 0;
  48. } else {
  49. str->a = SMART_STR_NEW_LEN(len);
  50. str->s = (zend_string *) perealloc(str->s, str->a + _ZSTR_HEADER_SIZE + 1, 1);
  51. }
  52. }
  53. /* Windows uses VK_ESCAPE instead of \e */
  54. #ifndef VK_ESCAPE
  55. #define VK_ESCAPE '\e'
  56. #endif
  57. static size_t zend_compute_escaped_string_len(const char *s, size_t l) {
  58. size_t i, len = l;
  59. for (i = 0; i < l; ++i) {
  60. char c = s[i];
  61. if (c == '\n' || c == '\r' || c == '\t' ||
  62. c == '\f' || c == '\v' || c == '\\' || c == VK_ESCAPE) {
  63. len += 1;
  64. } else if (c < 32 || c > 126) {
  65. len += 3;
  66. }
  67. }
  68. return len;
  69. }
  70. ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l) {
  71. char *res;
  72. size_t i, len = zend_compute_escaped_string_len(s, l);
  73. smart_str_alloc(str, len, 0);
  74. res = &ZSTR_VAL(str->s)[ZSTR_LEN(str->s)];
  75. ZSTR_LEN(str->s) += len;
  76. for (i = 0; i < l; ++i) {
  77. unsigned char c = s[i];
  78. if (c < 32 || c == '\\' || c > 126) {
  79. *res++ = '\\';
  80. switch (c) {
  81. case '\n': *res++ = 'n'; break;
  82. case '\r': *res++ = 'r'; break;
  83. case '\t': *res++ = 't'; break;
  84. case '\f': *res++ = 'f'; break;
  85. case '\v': *res++ = 'v'; break;
  86. case '\\': *res++ = '\\'; break;
  87. case VK_ESCAPE: *res++ = 'e'; break;
  88. default:
  89. *res++ = 'x';
  90. if ((c >> 4) < 10) {
  91. *res++ = (c >> 4) + '0';
  92. } else {
  93. *res++ = (c >> 4) + 'A' - 10;
  94. }
  95. if ((c & 0xf) < 10) {
  96. *res++ = (c & 0xf) + '0';
  97. } else {
  98. *res++ = (c & 0xf) + 'A' - 10;
  99. }
  100. }
  101. } else {
  102. *res++ = c;
  103. }
  104. }
  105. }
  106. ZEND_API void smart_str_append_printf(smart_str *dest, const char *format, ...) {
  107. va_list arg;
  108. va_start(arg, format);
  109. zend_printf_to_smart_str(dest, format, arg);
  110. va_end(arg);
  111. }
  112. #define SMART_STRING_OVERHEAD (ZEND_MM_OVERHEAD + 1)
  113. #define SMART_STRING_START_SIZE 256
  114. #define SMART_STRING_START_LEN (SMART_STRING_START_SIZE - SMART_STRING_OVERHEAD)
  115. #define SMART_STRING_PAGE 4096
  116. ZEND_API void ZEND_FASTCALL _smart_string_alloc_persistent(smart_string *str, size_t len)
  117. {
  118. if (!str->c) {
  119. str->len = 0;
  120. if (len <= SMART_STRING_START_LEN) {
  121. str->a = SMART_STRING_START_LEN;
  122. } else {
  123. str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
  124. }
  125. str->c = pemalloc(str->a + 1, 1);
  126. } else {
  127. if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
  128. zend_error(E_ERROR, "String size overflow");
  129. }
  130. len += str->len;
  131. str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
  132. str->c = perealloc(str->c, str->a + 1, 1);
  133. }
  134. }
  135. ZEND_API void ZEND_FASTCALL _smart_string_alloc(smart_string *str, size_t len)
  136. {
  137. if (!str->c) {
  138. str->len = 0;
  139. if (len <= SMART_STRING_START_LEN) {
  140. str->a = SMART_STRING_START_LEN;
  141. str->c = emalloc(SMART_STRING_START_LEN + 1);
  142. } else {
  143. str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
  144. if (EXPECTED(str->a < (ZEND_MM_CHUNK_SIZE - SMART_STRING_OVERHEAD))) {
  145. str->c = emalloc_large(str->a + 1);
  146. } else {
  147. /* allocate a huge chunk */
  148. str->c = emalloc(str->a + 1);
  149. }
  150. }
  151. } else {
  152. if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
  153. zend_error(E_ERROR, "String size overflow");
  154. }
  155. len += str->len;
  156. str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
  157. str->c = erealloc2(str->c, str->a + 1, str->len);
  158. }
  159. }
  160. /*
  161. * Local variables:
  162. * tab-width: 4
  163. * c-basic-offset: 4
  164. * indent-tabs-mode: t
  165. * End:
  166. * vim600: sw=4 ts=4 fdm=marker
  167. * vim<600: sw=4 ts=4
  168. */