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