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"
33 size_t psi_token_alloc_size(size_t token_len
, size_t fname_len
) {
34 return sizeof(struct psi_token
) + token_len
+ fname_len
+ 2;
37 struct psi_token
*psi_token_alloc(struct psi_parser
*P
) {
39 size_t token_len
, fname_len
;
42 if (P
->cur
< P
->tok
) {
47 token_len
= P
->cur
- P
->tok
;
48 fname_len
= strlen(P
->file
.fn
);
50 T
= calloc(1, psi_token_alloc_size(token_len
, fname_len
));
54 T
->file
= &T
->buf
[token_len
+ 1];
58 memcpy(T
->text
, P
->tok
, token_len
);
59 memcpy(T
->file
, P
->file
.fn
, fname_len
);
64 void psi_token_free(struct psi_token
**token_ptr
) {
66 struct psi_token
*token
= *token_ptr
;
73 struct psi_token
*psi_token_copy(struct psi_token
*src
) {
74 size_t strct_len
= psi_token_alloc_size(src
->size
, strlen(src
->file
));
75 struct psi_token
*ptr
= malloc(strct_len
);
77 memcpy(ptr
, src
, strct_len
);
79 ptr
->text
= &ptr
->buf
[0];
80 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
85 void psi_token_copy_ctor(struct psi_token
**tok
) {
86 *tok
= psi_token_copy(*tok
);
89 struct psi_token
*psi_token_cat(unsigned argc
, ...) {
92 struct psi_token
*T
= NULL
;
95 for (i
= 0; i
< argc
; ++i
) {
96 struct psi_token
*arg
= va_arg(argv
, struct psi_token
*);
99 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
100 struct psi_token
*tmp
= realloc(T
, psi_token_alloc_size(T
->size
+= arg
->size
+ 1, fname_len
));
110 T
->text
= &T
->buf
[0];
111 T
->file
= &T
->buf
[T
->size
+ 1];
112 T
->buf
[token_len
] = ' ';
113 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
114 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
116 T
= psi_token_copy(arg
);
117 T
->type
= PSI_T_NAME
;
125 struct psi_token
*psi_token_append(struct psi_token
*T
, unsigned argc
, ...) {
129 va_start(argv
, argc
);
130 for (i
= 0; i
< argc
; ++i
) {
131 char *str
= va_arg(argv
, char *);
132 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
134 T
= realloc(T
, psi_token_alloc_size(T
->size
+= str_len
+ 1, fname_len
));
135 T
->text
= &T
->buf
[0];
136 T
->file
= &T
->buf
[T
->size
+ 1];
137 T
->buf
[token_len
] = ' ';
138 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
139 memcpy(&T
->buf
[token_len
+ 1], str
, str_len
+ 1);
146 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
147 struct psi_token
*psi_token_translit(struct psi_token
*T
, char *from
, char *to
) {
148 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
152 static inline uint64_t psi_hash(char *digest_buf
, ...)
154 uint64_t hash
= 5381;
159 va_start(argv
, digest_buf
);
160 while ((ptr
= va_arg(argv
, const uint8_t *))) {
161 while ((c
= *ptr
++)) {
162 hash
= ((hash
<< 5) + hash
) + c
;
168 sprintf(digest_buf
, "%" PRIx64
, hash
);
174 uint64_t psi_token_hash(struct psi_token
*t
, char *digest_buf
) {
177 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
178 return psi_hash(digest_buf
, t
->file
, loc_buf
, (char *) NULL
);
181 void psi_token_dump(int fd
, struct psi_token
*t
)
185 dprintf(fd
, "TOKEN %p (%d) \"", t
, t
->type
);
186 for (i
= 0; i
< MIN(t
->size
, 16); ++i
) {
187 switch (t
->text
[i
]) {
216 if (isprint(t
->text
[i
])) {
217 dprintf(fd
, "%c", t
->text
[i
]);
219 dprintf(fd
, "\\%03hho", t
->text
[i
]);
224 dprintf(fd
, "\" at col %u in %s on line %u\n", t
->col
, t
->file
, t
->line
);