xref: /PHP-5.6/ext/standard/php_smart_str.h (revision 1fd18821)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 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: Sascha Schumann <sascha@schumann.cx>                         |
16    +----------------------------------------------------------------------+
17  */
18 
19 /* $Id$ */
20 
21 #ifndef PHP_SMART_STR_H
22 #define PHP_SMART_STR_H
23 
24 #include "php_smart_str_public.h"
25 
26 #include <stdlib.h>
27 #ifndef SMART_STR_USE_REALLOC
28 #include <zend.h>
29 #endif
30 
31 #define smart_str_0(x) do {											\
32 	if ((x)->c) {													\
33 		(x)->c[(x)->len] = '\0';									\
34 	}																\
35 } while (0)
36 
37 #ifndef SMART_STR_PREALLOC
38 #define SMART_STR_PREALLOC 128
39 #endif
40 
41 #ifndef SMART_STR_START_SIZE
42 #define SMART_STR_START_SIZE 78
43 #endif
44 
45 #ifdef SMART_STR_USE_REALLOC
46 #define SMART_STR_REALLOC(a,b,c) realloc((a),(b))
47 #else
48 #define SMART_STR_REALLOC(a,b,c) perealloc((a),(b),(c))
49 #endif
50 
51 #define SMART_STR_DO_REALLOC(d, what) \
52 	(d)->c = SMART_STR_REALLOC((d)->c, (d)->a + 1, (what))
53 
54 #define smart_str_alloc4(d, n, what, newlen) do {					\
55 	if (!(d)->c) {													\
56 		(d)->len = 0;												\
57 		newlen = (n);												\
58 		(d)->a = newlen < SMART_STR_START_SIZE 						\
59 				? SMART_STR_START_SIZE 								\
60 				: (newlen >= (INT_MAX - SMART_STR_PREALLOC)? newlen \
61 							: (newlen + SMART_STR_PREALLOC));		\
62 		SMART_STR_DO_REALLOC(d, what);								\
63 	} else {														\
64 		newlen = (d)->len + (n);									\
65 		if (newlen >= (d)->a) {										\
66 			(d)->a = newlen + SMART_STR_PREALLOC;					\
67 	        if (UNEXPECTED((d)->a >= INT_MAX)) {					\
68                 zend_error(E_ERROR, "String size overflow");		\
69             }														\
70 			SMART_STR_DO_REALLOC(d, what);							\
71 		}															\
72 	}																\
73 } while (0)
74 
75 #define smart_str_alloc(d, n, what) \
76 	smart_str_alloc4((d), (n), (what), newlen)
77 
78 /* wrapper */
79 
80 #define smart_str_appends_ex(dest, src, what) \
81 	smart_str_appendl_ex((dest), (src), strlen(src), (what))
82 #define smart_str_appends(dest, src) \
83 	smart_str_appendl((dest), (src), strlen(src))
84 
85 #define smart_str_appendc(dest, c) \
86 	smart_str_appendc_ex((dest), (c), 0)
87 #define smart_str_free(s) \
88 	smart_str_free_ex((s), 0)
89 #define smart_str_appendl(dest, src, len) \
90 	smart_str_appendl_ex((dest), (src), (len), 0)
91 #define smart_str_append(dest, src) \
92 	smart_str_append_ex((dest), (src), 0)
93 #define smart_str_append_long(dest, val) \
94 	smart_str_append_long_ex((dest), (val), 0)
95 #define smart_str_append_off_t(dest, val) \
96 	smart_str_append_off_t_ex((dest), (val), 0)
97 #define smart_str_append_unsigned(dest, val) \
98 	smart_str_append_unsigned_ex((dest), (val), 0)
99 
100 #define smart_str_appendc_ex(dest, ch, what) do {					\
101 	register size_t __nl;											\
102 	smart_str_alloc4((dest), 1, (what), __nl);						\
103 	(dest)->len = __nl;												\
104 	((unsigned char *) (dest)->c)[(dest)->len - 1] = (ch);			\
105 } while (0)
106 
107 #define smart_str_free_ex(s, what) do {								\
108 	smart_str *__s = (smart_str *) (s);								\
109 	if (__s->c) {													\
110 		pefree(__s->c, what);										\
111 		__s->c = NULL;												\
112 	}																\
113 	__s->a = __s->len = 0;											\
114 } while (0)
115 
116 #define smart_str_appendl_ex(dest, src, nlen, what) do {			\
117 	register size_t __nl;											\
118 	smart_str *__dest = (smart_str *) (dest);						\
119 																	\
120 	smart_str_alloc4(__dest, (nlen), (what), __nl);					\
121 	memcpy(__dest->c + __dest->len, (src), (nlen));					\
122 	__dest->len = __nl;												\
123 } while (0)
124 
125 /* input: buf points to the END of the buffer */
126 #define smart_str_print_unsigned4(buf, num, vartype, result) do {	\
127 	char *__p = (buf);												\
128 	vartype __num = (num);											\
129 	*__p = '\0';													\
130 	do {															\
131 		*--__p = (char) (__num % 10) + '0';							\
132 		__num /= 10;												\
133 	} while (__num > 0);											\
134 	result = __p;													\
135 } while (0)
136 
137 /* buf points to the END of the buffer */
138 #define smart_str_print_long4(buf, num, vartype, result) do {	\
139 	if (num < 0) {													\
140 		/* this might cause problems when dealing with LONG_MIN		\
141 		   and machines which don't support long long.  Works		\
142 		   flawlessly on 32bit x86 */								\
143 		smart_str_print_unsigned4((buf), -(num), vartype, (result));	\
144 		*--(result) = '-';											\
145 	} else {														\
146 		smart_str_print_unsigned4((buf), (num), vartype, (result));	\
147 	}																\
148 } while (0)
149 
150 /*
151  * these could be replaced using a braced-group inside an expression
152  * for GCC compatible compilers, e.g.
153  *
154  * #define f(..) ({char *r;..;__r;})
155  */
156 
smart_str_print_long(char * buf,long num)157 static inline char *smart_str_print_long(char *buf, long num) {
158 	char *r;
159 	smart_str_print_long4(buf, num, unsigned long, r);
160 	return r;
161 }
162 
smart_str_print_unsigned(char * buf,long num)163 static inline char *smart_str_print_unsigned(char *buf, long num) {
164 	char *r;
165 	smart_str_print_unsigned4(buf, num, unsigned long, r);
166 	return r;
167 }
168 
169 #define smart_str_append_generic_ex(dest, num, type, vartype, func) do {	\
170 	char __b[32];															\
171 	char *__t;																\
172    	smart_str_print##func##4 (__b + sizeof(__b) - 1, (num), vartype, __t);	\
173 	smart_str_appendl_ex((dest), __t, __b + sizeof(__b) - 1 - __t, (type));	\
174 } while (0)
175 
176 #define smart_str_append_unsigned_ex(dest, num, type) \
177 	smart_str_append_generic_ex((dest), (num), (type), unsigned long, _unsigned)
178 
179 #define smart_str_append_long_ex(dest, num, type) \
180 	smart_str_append_generic_ex((dest), (num), (type), unsigned long, _long)
181 
182 #define smart_str_append_off_t_ex(dest, num, type) \
183 	smart_str_append_generic_ex((dest), (num), (type), off_t, _long)
184 
185 #define smart_str_append_ex(dest, src, what) \
186 	smart_str_appendl_ex((dest), ((smart_str *)(src))->c, \
187 		((smart_str *)(src))->len, (what));
188 
189 
190 #define smart_str_setl(dest, src, nlen) do {						\
191 	(dest)->len = (nlen);											\
192 	(dest)->a = (nlen) + 1;											\
193 	(dest)->c = (char *) (src);										\
194 } while (0)
195 
196 #define smart_str_sets(dest, src) \
197 	smart_str_setl((dest), (src), strlen(src));
198 
199 #endif
200