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