xref: /PHP-8.0/Zend/zend_smart_str.c (revision 549cb440)
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    | http://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_printf(smart_str * dest,const char * format,...)115 ZEND_API void smart_str_append_printf(smart_str *dest, const char *format, ...) {
116 	va_list arg;
117 	va_start(arg, format);
118 	zend_printf_to_smart_str(dest, format, arg);
119 	va_end(arg);
120 }
121 
122 #define SMART_STRING_OVERHEAD   (ZEND_MM_OVERHEAD + 1)
123 #define SMART_STRING_START_SIZE 256
124 #define SMART_STRING_START_LEN  (SMART_STRING_START_SIZE - SMART_STRING_OVERHEAD)
125 #define SMART_STRING_PAGE       4096
126 
_smart_string_alloc_persistent(smart_string * str,size_t len)127 ZEND_API void ZEND_FASTCALL _smart_string_alloc_persistent(smart_string *str, size_t len)
128 {
129 	if (!str->c) {
130 		str->len = 0;
131 		if (len <= SMART_STRING_START_LEN) {
132 			str->a = SMART_STRING_START_LEN;
133 		} else {
134 			str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
135 		}
136 		str->c = pemalloc(str->a + 1, 1);
137 	} else {
138 		if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
139 			zend_error(E_ERROR, "String size overflow");
140 		}
141 		len += str->len;
142 		str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
143 		str->c = perealloc(str->c, str->a + 1, 1);
144 	}
145 }
146 
_smart_string_alloc(smart_string * str,size_t len)147 ZEND_API void ZEND_FASTCALL _smart_string_alloc(smart_string *str, size_t len)
148 {
149 	if (!str->c) {
150 		str->len = 0;
151 		if (len <= SMART_STRING_START_LEN) {
152 			str->a = SMART_STRING_START_LEN;
153 			str->c = emalloc(SMART_STRING_START_LEN + 1);
154 		} else {
155 			str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
156 			if (EXPECTED(str->a < (ZEND_MM_CHUNK_SIZE - SMART_STRING_OVERHEAD))) {
157 				str->c = emalloc_large(str->a + 1);
158 			} else {
159 				/* allocate a huge chunk */
160 				str->c = emalloc(str->a + 1);
161 			}
162 		}
163 	} else {
164 		if (UNEXPECTED((size_t) len > SIZE_MAX - str->len)) {
165 			zend_error(E_ERROR, "String size overflow");
166 		}
167 		len += str->len;
168 		str->a = ZEND_MM_ALIGNED_SIZE_EX(len + SMART_STRING_OVERHEAD, SMART_STRING_PAGE) - SMART_STRING_OVERHEAD;
169 		str->c = erealloc2(str->c, str->a + 1, str->len);
170 	}
171 }
172