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_init(token_t token_typ
, const char *token_txt
,
38 size_t token_len
, unsigned col
, unsigned line
, const char *file
)
41 size_t file_len
= strlen(file
);
43 T
= calloc(1, psi_token_alloc_size(token_len
, file_len
));
47 T
->file
= &T
->buf
[token_len
+ 1];
51 memcpy(T
->text
, token_txt
, token_len
);
52 memcpy(T
->file
, file
, file_len
);
57 void psi_token_free(struct psi_token
**token_ptr
) {
59 struct psi_token
*token
= *token_ptr
;
66 struct psi_token
*psi_token_copy(struct psi_token
*src
) {
67 size_t strct_len
= psi_token_alloc_size(src
->size
, strlen(src
->file
));
68 struct psi_token
*ptr
= malloc(strct_len
);
70 memcpy(ptr
, src
, strct_len
);
72 ptr
->text
= &ptr
->buf
[0];
73 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
78 void psi_token_copy_ctor(struct psi_token
**tok
) {
79 *tok
= psi_token_copy(*tok
);
82 struct psi_token
*psi_token_cat(const char *sep
, unsigned argc
, ...) {
85 size_t sep_len
= sep
? strlen(sep
) : 0;
86 struct psi_token
*T
= NULL
;
89 for (i
= 0; i
< argc
; ++i
) {
90 struct psi_token
*arg
= va_arg(argv
, struct psi_token
*);
93 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
94 struct psi_token
*tmp
= realloc(T
, psi_token_alloc_size(T
->size
+= arg
->size
+ sep_len
, fname_len
));
104 T
->text
= &T
->buf
[0];
105 T
->file
= &T
->buf
[T
->size
+ 1];
106 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
107 memcpy(&T
->buf
[token_len
], sep
, sep_len
);
108 memcpy(&T
->buf
[token_len
+ sep_len
], arg
->text
, arg
->size
+ 1);
110 T
= psi_token_copy(arg
);
111 T
->type
= PSI_T_NAME
;
119 struct psi_token
*psi_token_prepend(const char *sep
, struct psi_token
*T
, unsigned argc
, ...) {
122 size_t sep_len
= sep
? strlen(sep
) : 0;
124 va_start(argv
, argc
);
125 for (i
= 0; i
< argc
; ++i
) {
126 char *str
= va_arg(argv
, char *);
127 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
129 T
= realloc(T
, psi_token_alloc_size(T
->size
+= str_len
+ sep_len
, fname_len
));
130 T
->text
= &T
->buf
[0];
131 T
->file
= &T
->buf
[T
->size
+ 1];
132 memmove(&T
->buf
[str_len
+ sep_len
], &T
->buf
[0], token_len
+ 1 + fname_len
+ 1);
133 memcpy(&T
->buf
[0], str
, str_len
);
134 memcpy(&T
->buf
[str_len
], sep
, sep_len
);
135 T
->buf
[T
->size
] = '\0';
141 struct psi_token
*psi_token_append(const char *sep
, struct psi_token
*T
, unsigned argc
, ...) {
144 size_t sep_len
= sep
? strlen(sep
) : 0;
146 va_start(argv
, argc
);
147 for (i
= 0; i
< argc
; ++i
) {
148 char *str
= va_arg(argv
, char *);
149 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
151 T
= realloc(T
, psi_token_alloc_size(T
->size
+= str_len
+ sep_len
, fname_len
));
152 T
->text
= &T
->buf
[0];
153 T
->file
= &T
->buf
[T
->size
+ 1];
154 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
155 memcpy(&T
->buf
[token_len
], sep
, sep_len
);
156 memcpy(&T
->buf
[token_len
+ sep_len
], str
, str_len
+ 1);
163 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
164 struct psi_token
*psi_token_translit(struct psi_token
*T
, char *from
, char *to
) {
165 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
169 static inline uint64_t psi_hash(char *digest_buf
, ...)
171 uint64_t hash
= 5381;
176 va_start(argv
, digest_buf
);
177 while ((ptr
= va_arg(argv
, const uint8_t *))) {
178 while ((c
= *ptr
++)) {
179 hash
= ((hash
<< 5) + hash
) + c
;
185 sprintf(digest_buf
, "%" PRIx64
, hash
);
191 uint64_t psi_token_hash(struct psi_token
*t
, char *digest_buf
) {
194 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
195 return psi_hash(digest_buf
, t
->file
, loc_buf
, (char *) NULL
);
198 void psi_token_dump(int fd
, struct psi_token
*t
)
202 dprintf(fd
, "TOKEN %p (%d) ", t
, t
->type
);
203 if (t
->type
== PSI_T_EOF
) {
207 for (i
= 0; i
< t
->size
; ++i
) {
208 switch (t
->text
[i
]) {
237 if (isprint(t
->text
[i
])) {
238 dprintf(fd
, "%c", t
->text
[i
]);
240 dprintf(fd
, "\\x%02hhX", t
->text
[i
]);
247 dprintf(fd
, " at col %u in %s on line %u\n", t
->col
, t
->file
, t
->line
);