18c74375fe0b84eb18cd5fadf0994e7774755762
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
29 # include "php_config.h"
34 #include <Zend/zend_smart_str.h>
39 #ifndef PSI_DEBUG_TOKEN_ALLOC
40 # define PSI_DEBUG_TOKEN_ALLOC 0
43 struct psi_token
*psi_token_init(token_t token_typ
, const char *token_txt
,
44 size_t token_len
, unsigned col
, unsigned line
, zend_string
*file
)
48 T
= pecalloc(1, sizeof(*T
), 1);
52 T
->file
= zend_string_copy(file
);
53 T
->text
= psi_string_init_interned(token_txt
, token_len
, 1);
54 #if PSI_DEBUG_TOKEN_ALLOC
55 fprintf(stderr
, "PSI: token_init %p\t", T
);
56 psi_token_dump(NULL
, T
);
61 void psi_token_free(struct psi_token
**token_ptr
) {
63 struct psi_token
*token
= *token_ptr
;
64 #if PSI_DEBUG_TOKEN_ALLOC
65 fprintf(stderr
, "PSI: token_free %p\t", token
);
66 psi_token_dump(NULL
, token
);
69 zend_string_release(token
->text
);
70 zend_string_release(token
->file
);
75 struct psi_token
*psi_token_copy(struct psi_token
*src
) {
76 struct psi_token
*ptr
= pemalloc(sizeof(*ptr
), 1);
79 #if PSI_DEBUG_TOKEN_ALLOC
80 fprintf(stderr
, "PSI: token_copy %p\t", ptr
);
81 psi_token_dump(NULL
, src
);
83 ptr
->text
= zend_string_copy(ptr
->text
);
84 ptr
->file
= zend_string_copy(ptr
->file
);
89 void psi_token_copy_ctor(struct psi_token
**tok
) {
90 *tok
= psi_token_copy(*tok
);
93 /* concatenate `argc` number of tokens separated by `sep` into a newly allocated token */
94 struct psi_token
*psi_token_cat(const char *sep
, unsigned argc
, ...) {
97 size_t sep_len
= sep
? strlen(sep
) : 0;
98 struct psi_token
*T
= pemalloc(sizeof(*T
), 1);
101 va_start(argv
, argc
);
103 *T
= *(struct psi_token
*) va_arg(argv
, struct psi_token
*);
104 T
->type
= PSI_T_NAME
;
105 T
->file
= zend_string_copy(T
->file
);
107 smart_str_append_ex(&text
, T
->text
, 1);
109 for (i
= 1; i
< argc
; ++i
) {
110 struct psi_token
*arg
= va_arg(argv
, struct psi_token
*);
112 smart_str_appendl_ex(&text
, sep
, sep_len
, 1);
113 smart_str_append_ex(&text
, arg
->text
, 1);
117 T
->text
= smart_str_extract(&text
);
119 #if PSI_DEBUG_TOKEN_ALLOC
120 fprintf(stderr
, "PSI: token_cat %p\n", T
);
125 /* append `argc` number of C strings separated by `sep` to token `T` */
126 struct psi_token
*psi_token_append(const char *sep
, struct psi_token
*T
, unsigned argc
, ...) {
129 size_t sep_len
= sep
? strlen(sep
) : 0;
130 smart_str text
= {0};
132 smart_str_append_ex(&text
, T
->text
, 1);
134 va_start(argv
, argc
);
135 for (i
= 0; i
< argc
; ++i
) {
136 char *str
= va_arg(argv
, char *);
137 size_t str_len
= strlen(str
);
139 if (sep_len
&& text
.a
) {
140 smart_str_appendl_ex(&text
, sep
, sep_len
, 1);
143 smart_str_appendl_ex(&text
, str
, str_len
, 1);
147 zend_string_release(T
->text
);
148 T
->text
= smart_str_extract(&text
);
153 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
154 struct psi_token
*psi_token_translit(struct psi_token
*T
, char *from
, char *to
) {
155 zend_string
*tmp
= zend_string_init(T
->text
->val
, T
->text
->len
, 1);
157 zend_string_release(T
->text
);
160 php_strtr(T
->text
->val
, T
->text
->len
, from
, to
, MIN(strlen(from
), strlen(to
)));
161 zend_string_forget_hash_val(T
->text
);
166 static inline uint64_t psi_hash(char *digest_buf
, ...)
168 uint64_t hash
= 5381;
173 va_start(argv
, digest_buf
);
174 while ((ptr
= va_arg(argv
, const uint8_t *))) {
175 while ((c
= *ptr
++)) {
176 hash
= ((hash
<< 5) + hash
) + c
;
182 sprintf(digest_buf
, "%" PRIx64
, hash
);
188 uint64_t psi_token_hash(struct psi_token
*t
, char *digest_buf
) {
191 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
192 return psi_hash(digest_buf
, t
->file
->val
, loc_buf
, (char *) NULL
);
195 void psi_token_dump(struct psi_dump
*dump
, struct psi_token
*t
)
200 PSI_DUMP(dump
, "TOKEN deleted\n");
204 PSI_DUMP(dump
, "TOKEN %p (%u) ", t
, t
->type
);
205 if (t
->type
== PSI_T_EOF
) {
206 PSI_DUMP(dump
, "EOF");
208 PSI_DUMP(dump
, "\"");
209 for (i
= 0; i
< t
->text
->len
; ++i
) {
210 switch (t
->text
->val
[i
]) {
212 PSI_DUMP(dump
, "\\0");
215 PSI_DUMP(dump
, "\\a");
218 PSI_DUMP(dump
, "\\b");
221 PSI_DUMP(dump
, "\\f");
224 PSI_DUMP(dump
, "\\n");
227 PSI_DUMP(dump
, "\\r");
230 PSI_DUMP(dump
, "\\t");
233 PSI_DUMP(dump
, "\\v");
236 PSI_DUMP(dump
, "\\\"");
239 if (isprint(t
->text
->val
[i
])) {
240 PSI_DUMP(dump
, "%c", t
->text
->val
[i
]);
242 PSI_DUMP(dump
, "\\x%02hhX", t
->text
->val
[i
]);
247 PSI_DUMP(dump
, "\"");
249 PSI_DUMP(dump
, " at col %u in %s on line %u\n", t
->col
, t
->file
->val
, t
->line
);