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 *******************************************************************************/
26 #include "php_psi_stdinc.h"
30 #include <Zend/zend_smart_str.h>
35 size_t psi_token_alloc_size(size_t token_len
, size_t fname_len
) {
36 return sizeof(struct psi_token
) + token_len
+ fname_len
+ 2;
39 struct psi_token
*psi_token_init(token_t token_typ
, const char *token_txt
,
40 size_t token_len
, unsigned col
, unsigned line
, zend_string
*file
)
44 T
= calloc(1, sizeof(*T
));
48 T
->file
= zend_string_copy(file
);
49 T
->text
= zend_string_init(token_txt
, token_len
, 1);
54 void psi_token_free(struct psi_token
**token_ptr
) {
56 struct psi_token
*token
= *token_ptr
;
59 zend_string_release(token
->text
);
60 zend_string_release(token
->file
);
65 struct psi_token
*psi_token_copy(struct psi_token
*src
) {
66 struct psi_token
*ptr
= malloc(sizeof(*ptr
));
70 ptr
->text
= zend_string_copy(ptr
->text
);
71 ptr
->file
= zend_string_copy(ptr
->file
);
76 void psi_token_copy_ctor(struct psi_token
**tok
) {
77 *tok
= psi_token_copy(*tok
);
80 /* concatenate `argc` number of tokens separated by `sep` into a newly allocated token */
81 struct psi_token
*psi_token_cat(const char *sep
, unsigned argc
, ...) {
84 size_t sep_len
= sep
? strlen(sep
) : 0;
85 struct psi_token
*T
= malloc(sizeof(*T
));
90 *T
= *(struct psi_token
*) va_arg(argv
, struct psi_token
*);
92 T
->file
= zend_string_copy(T
->file
);
94 smart_str_append_ex(&text
, T
->text
, 1);
96 for (i
= 1; i
< argc
; ++i
) {
97 struct psi_token
*arg
= va_arg(argv
, struct psi_token
*);
99 smart_str_appendl_ex(&text
, sep
, sep_len
, 1);
100 smart_str_append_ex(&text
, arg
->text
, 1);
104 T
->text
= smart_str_extract(&text
);
109 /* append `argc` number of C strings separated by `sep` to token `T` */
110 struct psi_token
*psi_token_append(const char *sep
, struct psi_token
*T
, unsigned argc
, ...) {
113 size_t sep_len
= sep
? strlen(sep
) : 0;
114 smart_str text
= {0};
116 smart_str_append_ex(&text
, T
->text
, 1);
118 va_start(argv
, argc
);
119 for (i
= 0; i
< argc
; ++i
) {
120 char *str
= va_arg(argv
, char *);
121 size_t str_len
= strlen(str
);
123 if (sep_len
&& text
.a
) {
124 smart_str_appendl_ex(&text
, sep
, sep_len
, 1);
127 smart_str_appendl_ex(&text
, str
, str_len
, 1);
131 zend_string_release(T
->text
);
132 T
->text
= smart_str_extract(&text
);
137 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
138 struct psi_token
*psi_token_translit(struct psi_token
*T
, char *from
, char *to
) {
139 zend_string
*tmp
= zend_string_init(T
->text
->val
, T
->text
->len
, 1);
141 zend_string_release(T
->text
);
144 php_strtr(T
->text
->val
, T
->text
->len
, from
, to
, MIN(strlen(from
), strlen(to
)));
145 zend_string_forget_hash_val(T
->text
);
150 static inline uint64_t psi_hash(char *digest_buf
, ...)
152 uint64_t hash
= 5381;
157 va_start(argv
, digest_buf
);
158 while ((ptr
= va_arg(argv
, const uint8_t *))) {
159 while ((c
= *ptr
++)) {
160 hash
= ((hash
<< 5) + hash
) + c
;
166 sprintf(digest_buf
, "%" PRIx64
, hash
);
172 uint64_t psi_token_hash(struct psi_token
*t
, char *digest_buf
) {
175 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
176 return psi_hash(digest_buf
, t
->file
->val
, loc_buf
, (char *) NULL
);
179 void psi_token_dump(int fd
, struct psi_token
*t
)
183 dprintf(fd
, "TOKEN %p (%u) ", t
, t
->type
);
184 if (t
->type
== PSI_T_EOF
) {
188 for (i
= 0; i
< t
->text
->len
; ++i
) {
189 switch (t
->text
->val
[i
]) {
218 if (isprint(t
->text
->val
[i
])) {
219 dprintf(fd
, "%c", t
->text
->val
[i
]);
221 dprintf(fd
, "\\x%02hhX", t
->text
->val
[i
]);
228 dprintf(fd
, " at col %u in %s on line %u\n", t
->col
, t
->file
->val
, t
->line
);