1/*
2   +----------------------------------------------------------------------+
3   | PHP Version 5                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2014 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)->s) {                                                   \
33        (x)->s->val[(x)->s->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
46#ifdef SMART_STR_USE_REALLOC
47#define SMART_STR_DO_REALLOC(b, w) do {                             \
48    int oldlen = (b)->s->len;                                       \
49    (b)->s = erealloc((buf)->s, _STR_HEADER_SIZE + (b)->a + 1);     \
50    (b)->s->len = oldlen;                                           \
51} while (0)
52#else
53#define SMART_STR_DO_REALLOC(b, w) do {                             \
54    (b)->s = perealloc((b)->s, _STR_HEADER_SIZE + (b)->a + 1, (w)); \
55} while (0)
56#endif
57
58
59#define smart_str_alloc4(d, n, what, newlen) do {                   \
60    if (!(d)->s) {                                                  \
61        newlen = (n);                                               \
62        (d)->a = newlen < SMART_STR_START_SIZE                      \
63                ? SMART_STR_START_SIZE                              \
64                : newlen + SMART_STR_PREALLOC;                      \
65        (d)->s = STR_ALLOC((d)->a, (what));                         \
66        (d)->s->len = 0;                                            \
67    } else {                                                        \
68        newlen = (d)->s->len + (n);                                 \
69        if (newlen >= (d)->a) {                                     \
70            (d)->a = newlen + SMART_STR_PREALLOC;                   \
71            SMART_STR_DO_REALLOC((d), (what));                      \
72        }                                                           \
73    }                                                               \
74} while (0)
75
76#define smart_str_alloc(d, n, what) \
77    smart_str_alloc4((d), (n), (what), newlen)
78
79/* wrapper */
80
81#define smart_str_appends_ex(dest, src, what) \
82    smart_str_appendl_ex((dest), (src), strlen(src), (what))
83#define smart_str_appends(dest, src) \
84    smart_str_appendl((dest), (src), strlen(src))
85
86#define smart_str_appendc(dest, c) \
87    smart_str_appendc_ex((dest), (c), 0)
88#define smart_str_free(s) \
89    smart_str_free_ex((s), 0)
90#define smart_str_appendl(dest, src, len) \
91    smart_str_appendl_ex((dest), (src), (len), 0)
92#define smart_str_append(dest, src) \
93    smart_str_append_ex((dest), (src), 0)
94#define smart_str_append_long(dest, val) \
95    smart_str_append_long_ex((dest), (val), 0)
96#define smart_str_append_off_t(dest, val) \
97    smart_str_append_off_t_ex((dest), (val), 0)
98#define smart_str_append_unsigned(dest, val) \
99    smart_str_append_unsigned_ex((dest), (val), 0)
100
101#define smart_str_appendc_ex(dest, ch, what) do {                  \
102    register size_t __nl;                                          \
103    smart_str_alloc4((dest), 1, (what), __nl);                     \
104    (dest)->s->len = __nl;                                         \
105    ((unsigned char *) (dest)->s->val)[(dest)->s->len - 1] = (ch); \
106} while (0)
107
108#define smart_str_free_ex(buf, what) do {                           \
109    smart_str *__s = (smart_str *) (buf);                           \
110    if (__s->s) {                                                   \
111        STR_RELEASE(__s->s);                                        \
112        __s->s = NULL;                                              \
113    }                                                               \
114    __s->a = 0;                                                     \
115} while (0)
116
117#define smart_str_appendl_ex(dest, src, nlen, what) do {            \
118    register size_t __nl;                                           \
119    smart_str *__dest = (smart_str *) (dest);                       \
120                                                                    \
121    smart_str_alloc4(__dest, (nlen), (what), __nl);                 \
122    memcpy(__dest->s->val + __dest->s->len, (src), (nlen));         \
123    __dest->s->len = __nl;                                          \
124} while (0)
125
126/*
127 * these could be replaced using a braced-group inside an expression
128 * for GCC compatible compilers, e.g.
129 *
130 * #define f(..) ({char *r;..;__r;})
131 */
132
133static inline char *smart_str_print_long(char *buf, long num) {
134    char *r;
135    _zend_print_signed_to_buf(buf, num, long, r);
136    return r;
137}
138
139static inline char *smart_str_print_unsigned(char *buf, long num) {
140    char *r;
141    _zend_print_unsigned_to_buf(buf, num, unsigned long, r);
142    return r;
143}
144
145#define smart_str_append_generic_ex(dest, num, type, vartype, func) do {    \
146    char __b[32];                                                           \
147    char *__t;                                                              \
148    _zend_print##func##_to_buf (__b + sizeof(__b) - 1, (num), vartype, __t);    \
149    smart_str_appendl_ex((dest), __t, __b + sizeof(__b) - 1 - __t, (type)); \
150} while (0)
151
152#define smart_str_append_unsigned_ex(dest, num, type) \
153    smart_str_append_generic_ex((dest), (num), (type), unsigned long, _unsigned)
154
155#define smart_str_append_long_ex(dest, num, type) \
156    smart_str_append_generic_ex((dest), (num), (type), unsigned long, _signed)
157
158#define smart_str_append_off_t_ex(dest, num, type) \
159    smart_str_append_generic_ex((dest), (num), (type), off_t, _signed)
160
161#define smart_str_append_ex(dest, src, what)                        \
162    smart_str_appendl_ex((dest), ((smart_str *)(src))->s->val,      \
163        ((smart_str *)(src))->s->len, (what));
164
165#define smart_str_setl(dest, src, nlen) do {                        \
166    smart_str_free((dest));                                         \
167    smart_str_appendl_ex((dest), (src), (nlen), 0);                 \
168} while (0)
169
170#define smart_str_sets(dest, src) \
171    smart_str_setl((dest), (src), strlen(src));
172
173#endif
174