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(const char *sep
, unsigned argc
, ...) {
92 size_t sep_len
= sep
? strlen(sep
) : 0;
93 struct psi_token
*T
= NULL
;
96 for (i
= 0; i
< argc
; ++i
) {
97 struct psi_token
*arg
= va_arg(argv
, struct psi_token
*);
100 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
101 struct psi_token
*tmp
= realloc(T
, psi_token_alloc_size(T
->size
+= arg
->size
+ sep_len
, fname_len
));
111 T
->text
= &T
->buf
[0];
112 T
->file
= &T
->buf
[T
->size
+ 1];
113 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ sep_len
], fname_len
+ 1);
114 memcpy(&T
->buf
[token_len
], sep
, sep_len
);
115 memcpy(&T
->buf
[token_len
+ sep_len
], arg
->text
, arg
->size
+ 1);
117 T
= psi_token_copy(arg
);
118 T
->type
= PSI_T_NAME
;
126 struct psi_token
*psi_token_prepend(const char *sep
, struct psi_token
*T
, unsigned argc
, ...) {
129 size_t sep_len
= sep
? strlen(sep
) : 0;
131 va_start(argv
, argc
);
132 for (i
= 0; i
< argc
; ++i
) {
133 char *str
= va_arg(argv
, char *);
134 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
136 T
= realloc(T
, psi_token_alloc_size(T
->size
+= str_len
+ sep_len
, fname_len
));
137 T
->text
= &T
->buf
[0];
138 T
->file
= &T
->buf
[T
->size
+ 1];
139 memmove(&T
->buf
[str_len
+ sep_len
], &T
->buf
[0], T
->size
+ 1 + fname_len
+ 1);
140 memcpy(&T
->buf
[0], str
, str_len
);
141 memcpy(&T
->buf
[str_len
], sep
, sep_len
);
142 T
->buf
[T
->size
] = '\0';
148 struct psi_token
*psi_token_append(const char *sep
, struct psi_token
*T
, unsigned argc
, ...) {
151 size_t sep_len
= sep
? strlen(sep
) : 0;
153 va_start(argv
, argc
);
154 for (i
= 0; i
< argc
; ++i
) {
155 char *str
= va_arg(argv
, char *);
156 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
158 T
= realloc(T
, psi_token_alloc_size(T
->size
+= str_len
+ sep_len
, fname_len
));
159 T
->text
= &T
->buf
[0];
160 T
->file
= &T
->buf
[T
->size
+ 1];
161 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ sep_len
], fname_len
+ 1);
162 memcpy(&T
->buf
[token_len
], sep
, sep_len
);
163 memcpy(&T
->buf
[token_len
+ sep_len
], str
, str_len
+ 1);
170 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
171 struct psi_token
*psi_token_translit(struct psi_token
*T
, char *from
, char *to
) {
172 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
176 static inline uint64_t psi_hash(char *digest_buf
, ...)
178 uint64_t hash
= 5381;
183 va_start(argv
, digest_buf
);
184 while ((ptr
= va_arg(argv
, const uint8_t *))) {
185 while ((c
= *ptr
++)) {
186 hash
= ((hash
<< 5) + hash
) + c
;
192 sprintf(digest_buf
, "%" PRIx64
, hash
);
198 uint64_t psi_token_hash(struct psi_token
*t
, char *digest_buf
) {
201 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
202 return psi_hash(digest_buf
, t
->file
, loc_buf
, (char *) NULL
);
205 void psi_token_dump(int fd
, struct psi_token
*t
)
209 dprintf(fd
, "TOKEN %p (%d) \"", t
, t
->type
);
210 for (i
= 0; i
< MIN(t
->size
, 16); ++i
) {
211 switch (t
->text
[i
]) {
240 if (isprint(t
->text
[i
])) {
241 dprintf(fd
, "%c", t
->text
[i
]);
243 dprintf(fd
, "\\%03hho", t
->text
[i
]);
248 dprintf(fd
, "\" at col %u in %s on line %u\n", t
->col
, t
->file
, t
->line
);