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