1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 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: Dmitry Stogov <dmitry@zend.com> |
16 +----------------------------------------------------------------------+
17 */
18
19 #include <zend.h>
20 #include "zend_smart_str.h"
21
22 #define SMART_STR_OVERHEAD (ZEND_MM_OVERHEAD + _ZSTR_HEADER_SIZE)
23
24 #ifndef SMART_STR_PAGE
25 # define SMART_STR_PAGE 4096
26 #endif
27
28 #ifndef SMART_STR_START_SIZE
29 # define SMART_STR_START_SIZE (256 - SMART_STR_OVERHEAD - 1)
30 #endif
31
32 #define SMART_STR_NEW_SIZE(len) \
33 (((len + SMART_STR_OVERHEAD + SMART_STR_PAGE) & ~(SMART_STR_PAGE - 1)) - SMART_STR_OVERHEAD - 1)
34
smart_str_erealloc(smart_str * str,size_t len)35 ZEND_API void ZEND_FASTCALL smart_str_erealloc(smart_str *str, size_t len)
36 {
37 if (UNEXPECTED(!str->s)) {
38 str->a = len < SMART_STR_START_SIZE
39 ? SMART_STR_START_SIZE
40 : SMART_STR_NEW_SIZE(len);
41 str->s = zend_string_alloc(str->a, 0);
42 ZSTR_LEN(str->s) = 0;
43 } else {
44 str->a = SMART_STR_NEW_SIZE(len);
45 str->s = (zend_string *) erealloc2(str->s, _ZSTR_HEADER_SIZE + str->a + 1, _ZSTR_HEADER_SIZE + ZSTR_LEN(str->s) + 1);
46 }
47 }
48
smart_str_realloc(smart_str * str,size_t len)49 ZEND_API void ZEND_FASTCALL smart_str_realloc(smart_str *str, size_t len)
50 {
51 if (UNEXPECTED(!str->s)) {
52 str->a = len < SMART_STR_START_SIZE
53 ? SMART_STR_START_SIZE
54 : SMART_STR_NEW_SIZE(len);
55 str->s = zend_string_alloc(str->a, 1);
56 ZSTR_LEN(str->s) = 0;
57 } else {
58 str->a = SMART_STR_NEW_SIZE(len);
59 str->s = (zend_string *) realloc(str->s, _ZSTR_HEADER_SIZE + str->a + 1);
60 }
61 }
62
63 /* Windows uses VK_ESCAPE instead of \e */
64 #ifndef VK_ESCAPE
65 #define VK_ESCAPE '\e'
66 #endif
67
zend_compute_escaped_string_len(const char * s,size_t l)68 static size_t zend_compute_escaped_string_len(const char *s, size_t l) {
69 size_t i, len = l;
70 for (i = 0; i < l; ++i) {
71 char c = s[i];
72 if (c == '\n' || c == '\r' || c == '\t' ||
73 c == '\f' || c == '\v' || c == '\\' || c == VK_ESCAPE) {
74 len += 1;
75 } else if (c < 32 || c > 126) {
76 len += 3;
77 }
78 }
79 return len;
80 }
81
smart_str_append_escaped(smart_str * str,const char * s,size_t l)82 ZEND_API void ZEND_FASTCALL smart_str_append_escaped(smart_str *str, const char *s, size_t l) {
83 char *res;
84 size_t i, len = zend_compute_escaped_string_len(s, l);
85
86 smart_str_alloc(str, len, 0);
87 res = &ZSTR_VAL(str->s)[ZSTR_LEN(str->s)];
88 ZSTR_LEN(str->s) += len;
89
90 for (i = 0; i < l; ++i) {
91 unsigned char c = s[i];
92 if (c < 32 || c == '\\' || c > 126) {
93 *res++ = '\\';
94 switch (c) {
95 case '\n': *res++ = 'n'; break;
96 case '\r': *res++ = 'r'; break;
97 case '\t': *res++ = 't'; break;
98 case '\f': *res++ = 'f'; break;
99 case '\v': *res++ = 'v'; break;
100 case '\\': *res++ = '\\'; break;
101 case VK_ESCAPE: *res++ = 'e'; break;
102 default:
103 *res++ = 'x';
104 if ((c >> 4) < 10) {
105 *res++ = (c >> 4) + '0';
106 } else {
107 *res++ = (c >> 4) + 'A' - 10;
108 }
109 if ((c & 0xf) < 10) {
110 *res++ = (c & 0xf) + '0';
111 } else {
112 *res++ = (c & 0xf) + 'A' - 10;
113 }
114 }
115 } else {
116 *res++ = c;
117 }
118 }
119 }
120
smart_str_append_printf(smart_str * dest,const char * format,...)121 ZEND_API void ZEND_FASTCALL smart_str_append_printf(smart_str *dest, const char *format, ...) {
122 va_list arg;
123 va_start(arg, format);
124 zend_printf_to_smart_str(dest, format, arg);
125 va_end(arg);
126 }
127
128 /*
129 * Local variables:
130 * tab-width: 4
131 * c-basic-offset: 4
132 * indent-tabs-mode: t
133 * End:
134 * vim600: sw=4 ts=4 fdm=marker
135 * vim<600: sw=4 ts=4
136 */
137