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"
36 #include <sys/param.h>
38 #include "parser_proc.h"
42 size_t psi_token_alloc_size(size_t token_len
, size_t fname_len
) {
43 return sizeof(struct psi_token
) + token_len
+ fname_len
+ 2;
46 struct psi_token
*psi_token_alloc(struct psi_parser
*P
) {
48 size_t token_len
, fname_len
;
51 if (P
->cur
< P
->tok
) {
56 token_len
= P
->cur
- P
->tok
;
57 fname_len
= strlen(P
->psi
.file
.fn
);
59 T
= calloc(1, psi_token_alloc_size(token_len
, fname_len
));
63 T
->file
= &T
->buf
[token_len
+ 1];
67 memcpy(T
->text
, P
->tok
, token_len
);
68 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
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 struct psi_token
*psi_token_cat(unsigned argc
, ...) {
88 struct psi_token
*T
= NULL
;
91 for (i
= 0; i
< argc
; ++i
) {
92 struct psi_token
*arg
= va_arg(argv
, struct psi_token
*);
95 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
96 struct psi_token
*tmp
= realloc(T
, psi_token_alloc_size(T
->size
+= arg
->size
+ 1, fname_len
));
106 T
->text
= &T
->buf
[0];
107 T
->file
= &T
->buf
[T
->size
+ 1];
108 T
->buf
[token_len
] = ' ';
109 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
110 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
112 T
= psi_token_copy(arg
);
113 T
->type
= PSI_T_NAME
;
121 struct psi_token
*psi_token_append(struct psi_token
*T
, unsigned argc
, ...) {
125 va_start(argv
, argc
);
126 for (i
= 0; i
< argc
; ++i
) {
127 char *str
= va_arg(argv
, char *);
128 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
130 T
= realloc(T
, psi_token_alloc_size(T
->size
+= str_len
+ 1, fname_len
));
131 T
->text
= &T
->buf
[0];
132 T
->file
= &T
->buf
[T
->size
+ 1];
133 T
->buf
[token_len
] = ' ';
134 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
135 memcpy(&T
->buf
[token_len
+ 1], str
, str_len
+ 1);
142 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
143 struct psi_token
*psi_token_translit(struct psi_token
*T
, char *from
, char *to
) {
144 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
148 static inline uint64_t psi_hash(char *digest_buf
, ...)
150 uint64_t hash
= 5381;
155 va_start(argv
, digest_buf
);
156 while ((ptr
= va_arg(argv
, const uint8_t *))) {
157 while ((c
= *ptr
++)) {
158 hash
= ((hash
<< 5) + hash
) + c
;
164 sprintf(digest_buf
, "%" PRIx64
, hash
);
170 uint64_t psi_token_hash(struct psi_token
*t
, char *digest_buf
) {
173 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
174 return psi_hash(digest_buf
, t
->file
, loc_buf
, (char *) NULL
);