/src/yara/libyara/compiler.c
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2013-2018. The YARA Authors. All Rights Reserved. |
3 | | |
4 | | Redistribution and use in source and binary forms, with or without modification, |
5 | | are permitted provided that the following conditions are met: |
6 | | |
7 | | 1. Redistributions of source code must retain the above copyright notice, this |
8 | | list of conditions and the following disclaimer. |
9 | | |
10 | | 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | this list of conditions and the following disclaimer in the documentation and/or |
12 | | other materials provided with the distribution. |
13 | | |
14 | | 3. Neither the name of the copyright holder nor the names of its contributors |
15 | | may be used to endorse or promote products derived from this software without |
16 | | specific prior written permission. |
17 | | |
18 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
19 | | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
22 | | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
25 | | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | |
30 | | #include <assert.h> |
31 | | #include <fcntl.h> |
32 | | #include <limits.h> |
33 | | #include <stddef.h> |
34 | | #include <stdint.h> |
35 | | #include <stdio.h> |
36 | | #include <string.h> |
37 | | #include <sys/stat.h> |
38 | | |
39 | | #ifdef _MSC_VER |
40 | | #include <io.h> |
41 | | #include <share.h> |
42 | | #else |
43 | | #include <unistd.h> |
44 | | #endif |
45 | | |
46 | | #include <yara/compiler.h> |
47 | | #include <yara/error.h> |
48 | | #include <yara/exec.h> |
49 | | #include <yara/lexer.h> |
50 | | #include <yara/libyara.h> |
51 | | #include <yara/mem.h> |
52 | | #include <yara/object.h> |
53 | | #include <yara/strutils.h> |
54 | | #include <yara/utils.h> |
55 | | |
56 | | static void _yr_compiler_default_include_free( |
57 | | const char* callback_result_ptr, |
58 | | void* user_data) |
59 | 0 | { |
60 | 0 | if (callback_result_ptr != NULL) |
61 | 0 | { |
62 | 0 | yr_free((void*) callback_result_ptr); |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | const char* _yr_compiler_default_include_callback( |
67 | | const char* include_name, |
68 | | const char* calling_rule_filename, |
69 | | const char* calling_rule_namespace, |
70 | | void* user_data) |
71 | 0 | { |
72 | 0 | #ifndef _MSC_VER |
73 | 0 | struct stat stbuf; |
74 | 0 | #endif |
75 | |
|
76 | 0 | char* file_buffer; |
77 | |
|
78 | | #ifdef _MSC_VER |
79 | | long file_size; |
80 | | #else |
81 | 0 | off_t file_size; |
82 | 0 | #endif |
83 | |
|
84 | 0 | int fd = -1; |
85 | |
|
86 | | #if defined(_MSC_VER) |
87 | | _sopen_s(&fd, include_name, _O_RDONLY | _O_BINARY, _SH_DENYWR, _S_IREAD); |
88 | | #elif defined(_WIN32) || defined(__CYGWIN__) |
89 | | fd = open(include_name, O_RDONLY | O_BINARY); |
90 | | #else |
91 | 0 | fd = open(include_name, O_RDONLY); |
92 | 0 | #endif |
93 | |
|
94 | 0 | if (fd == -1) |
95 | 0 | return NULL; |
96 | | |
97 | | #ifdef _MSC_VER |
98 | | file_size = _filelength(fd); |
99 | | if (file_size == -1) |
100 | | { |
101 | | _close(fd); |
102 | | return NULL; |
103 | | } |
104 | | #else |
105 | 0 | if ((fstat(fd, &stbuf) != 0) || (!S_ISREG(stbuf.st_mode))) |
106 | 0 | { |
107 | 0 | close(fd); |
108 | 0 | return NULL; |
109 | 0 | } |
110 | 0 | file_size = stbuf.st_size; |
111 | 0 | #endif |
112 | | |
113 | | // Reject negative sizes and files too large to fit in memory with a null |
114 | | // terminator or to be passed to flex's yy_scan_string() / yy_scan_bytes(), |
115 | | // which take a signed int length. On 32-bit builds with 64-bit off_t, a size |
116 | | // of 2^32 - 1 would otherwise wrap (size_t) file_size + 1 around to 0. |
117 | 0 | if (file_size < 0 || (uintmax_t) file_size >= INT_MAX || |
118 | 0 | (uintmax_t) file_size >= SIZE_MAX) |
119 | 0 | { |
120 | | #ifdef _MSC_VER |
121 | | _close(fd); |
122 | | #else |
123 | 0 | close(fd); |
124 | 0 | #endif |
125 | |
|
126 | 0 | return NULL; |
127 | 0 | } |
128 | | |
129 | 0 | file_buffer = (char*) yr_malloc((size_t) file_size + 1); |
130 | |
|
131 | 0 | if (file_buffer == NULL) |
132 | 0 | { |
133 | | #ifdef _MSC_VER |
134 | | _close(fd); |
135 | | #else |
136 | 0 | close(fd); |
137 | 0 | #endif |
138 | |
|
139 | 0 | return NULL; |
140 | 0 | } |
141 | | |
142 | 0 | if (file_size != read(fd, file_buffer, (size_t) file_size)) |
143 | 0 | { |
144 | 0 | yr_free(file_buffer); |
145 | |
|
146 | | #ifdef _MSC_VER |
147 | | _close(fd); |
148 | | #else |
149 | 0 | close(fd); |
150 | 0 | #endif |
151 | |
|
152 | 0 | return NULL; |
153 | 0 | } |
154 | 0 | else |
155 | 0 | { |
156 | 0 | file_buffer[file_size] = '\0'; |
157 | 0 | } |
158 | | |
159 | | #ifdef _MSC_VER |
160 | | _close(fd); |
161 | | #else |
162 | 0 | close(fd); |
163 | 0 | #endif |
164 | |
|
165 | 0 | return file_buffer; |
166 | 0 | } |
167 | | |
168 | | //////////////////////////////////////////////////////////////////////////////// |
169 | | // Returns a rule given its index in the rules table. |
170 | | // |
171 | | // The returned pointer is valid as long as no other rule is written to the |
172 | | // table. This is because the write operation may cause the table to be moved to |
173 | | // a different location in memory. Use the pointer only in a limited scope where |
174 | | // you can be sure that no other rule is being written during the pointer's |
175 | | // lifetime. |
176 | | // |
177 | | YR_RULE* _yr_compiler_get_rule_by_idx(YR_COMPILER* compiler, uint32_t rule_idx) |
178 | 0 | { |
179 | 0 | return (YR_RULE*) yr_arena_get_ptr( |
180 | 0 | compiler->arena, YR_RULES_TABLE, rule_idx * sizeof(YR_RULE)); |
181 | 0 | } |
182 | | |
183 | | //////////////////////////////////////////////////////////////////////////////// |
184 | | // Stores some data in the YR_SZ_POOL and returns a reference to it. |
185 | | // |
186 | | // If the same data was already stored in a previous call to this function the |
187 | | // data is not written again, a reference to the existing data is returned |
188 | | // instead. |
189 | | // |
190 | | int _yr_compiler_store_data( |
191 | | YR_COMPILER* compiler, |
192 | | const void* data, |
193 | | size_t data_length, |
194 | | YR_ARENA_REF* ref) |
195 | 24 | { |
196 | | // Check if the data is already in YR_SZ_POOL by using a hash table. |
197 | 24 | uint32_t offset = yr_hash_table_lookup_uint32_raw_key( |
198 | 24 | compiler->sz_table, data, data_length, NULL); |
199 | | |
200 | 24 | if (offset == UINT32_MAX) |
201 | 16 | { |
202 | | // The data was not previously written to YR_SZ_POOL, write it and store |
203 | | // the reference's offset in the hash table. Storing the buffer number |
204 | | // is not necessary, it's always YR_SZ_POOL. |
205 | 16 | FAIL_ON_ERROR(yr_arena_write_data( |
206 | 16 | compiler->arena, YR_SZ_POOL, data, data_length, ref)); |
207 | | |
208 | 16 | FAIL_ON_ERROR(yr_hash_table_add_uint32_raw_key( |
209 | 16 | compiler->sz_table, data, data_length, NULL, ref->offset)); |
210 | 16 | } |
211 | 8 | else |
212 | 8 | { |
213 | 8 | ref->buffer_id = YR_SZ_POOL; |
214 | 8 | ref->offset = offset; |
215 | 8 | } |
216 | | |
217 | 24 | return ERROR_SUCCESS; |
218 | 24 | } |
219 | | |
220 | | //////////////////////////////////////////////////////////////////////////////// |
221 | | // Similar to _yr_compiler_store_data, but receives a null-terminated string. |
222 | | // |
223 | | int _yr_compiler_store_string( |
224 | | YR_COMPILER* compiler, |
225 | | const char* string, |
226 | | YR_ARENA_REF* ref) |
227 | 24 | { |
228 | 24 | return _yr_compiler_store_data( |
229 | 24 | compiler, |
230 | 24 | (void*) string, |
231 | 24 | strlen(string) + 1, // include the null terminator |
232 | 24 | ref); |
233 | 24 | } |
234 | | |
235 | | YR_API int yr_compiler_create(YR_COMPILER** compiler) |
236 | 2 | { |
237 | 2 | int result; |
238 | 2 | YR_COMPILER* new_compiler; |
239 | | |
240 | 2 | new_compiler = (YR_COMPILER*) yr_calloc(1, sizeof(YR_COMPILER)); |
241 | | |
242 | 2 | if (new_compiler == NULL) |
243 | 0 | return ERROR_INSUFFICIENT_MEMORY; |
244 | | |
245 | 2 | new_compiler->current_rule_idx = UINT32_MAX; |
246 | 2 | new_compiler->next_rule_idx = 0; |
247 | 2 | new_compiler->current_string_idx = 0; |
248 | 2 | new_compiler->current_namespace_idx = 0; |
249 | 2 | new_compiler->current_meta_idx = 0; |
250 | 2 | new_compiler->num_namespaces = 0; |
251 | 2 | new_compiler->errors = 0; |
252 | 2 | new_compiler->callback = NULL; |
253 | 2 | new_compiler->rules = NULL; |
254 | 2 | new_compiler->include_callback = _yr_compiler_default_include_callback; |
255 | 2 | new_compiler->incl_clbk_user_data = NULL; |
256 | 2 | new_compiler->include_free = _yr_compiler_default_include_free; |
257 | 2 | new_compiler->re_ast_callback = NULL; |
258 | 2 | new_compiler->re_ast_clbk_user_data = NULL; |
259 | 2 | new_compiler->last_error = ERROR_SUCCESS; |
260 | 2 | new_compiler->last_error_line = 0; |
261 | 2 | new_compiler->strict_escape = false; |
262 | 2 | new_compiler->current_line = 0; |
263 | 2 | new_compiler->file_name_stack_ptr = 0; |
264 | 2 | new_compiler->fixup_stack_head = NULL; |
265 | 2 | new_compiler->loop_index = -1; |
266 | 2 | new_compiler->loop_for_of_var_index = -1; |
267 | | |
268 | 2 | new_compiler->atoms_config.get_atom_quality = yr_atoms_heuristic_quality; |
269 | 2 | new_compiler->atoms_config.quality_warning_threshold = |
270 | 2 | YR_ATOM_QUALITY_WARNING_THRESHOLD; |
271 | | |
272 | 2 | result = yr_hash_table_create(5000, &new_compiler->rules_table); |
273 | | |
274 | 2 | if (result == ERROR_SUCCESS) |
275 | 2 | result = yr_hash_table_create(1000, &new_compiler->objects_table); |
276 | | |
277 | 2 | if (result == ERROR_SUCCESS) |
278 | 2 | result = yr_hash_table_create(10000, &new_compiler->strings_table); |
279 | | |
280 | 2 | if (result == ERROR_SUCCESS) |
281 | 2 | result = yr_hash_table_create( |
282 | 2 | 1000, &new_compiler->wildcard_identifiers_table); |
283 | | |
284 | 2 | if (result == ERROR_SUCCESS) |
285 | 2 | result = yr_hash_table_create(10000, &new_compiler->sz_table); |
286 | | |
287 | 2 | if (result == ERROR_SUCCESS) |
288 | 2 | result = yr_arena_create(YR_NUM_SECTIONS, 1048576, &new_compiler->arena); |
289 | | |
290 | 2 | if (result == ERROR_SUCCESS) |
291 | 2 | result = yr_ac_automaton_create( |
292 | 2 | new_compiler->arena, &new_compiler->automaton); |
293 | | |
294 | 2 | if (result == ERROR_SUCCESS) |
295 | 2 | { |
296 | 2 | *compiler = new_compiler; |
297 | 2 | } |
298 | 0 | else // if error, do cleanup |
299 | 0 | { |
300 | 0 | yr_compiler_destroy(new_compiler); |
301 | 0 | } |
302 | | |
303 | 2 | return result; |
304 | 2 | } |
305 | | |
306 | | YR_API void yr_compiler_destroy(YR_COMPILER* compiler) |
307 | 2 | { |
308 | 2 | if (compiler->arena != NULL) |
309 | 2 | yr_arena_release(compiler->arena); |
310 | | |
311 | 2 | if (compiler->automaton != NULL) |
312 | 2 | yr_ac_automaton_destroy(compiler->automaton); |
313 | | |
314 | 2 | if (compiler->rules_table != NULL) |
315 | 2 | yr_hash_table_destroy(compiler->rules_table, NULL); |
316 | | |
317 | 2 | if (compiler->strings_table != NULL) |
318 | 2 | yr_hash_table_destroy(compiler->strings_table, NULL); |
319 | | |
320 | 2 | if (compiler->wildcard_identifiers_table != NULL) |
321 | 2 | yr_hash_table_destroy(compiler->wildcard_identifiers_table, NULL); |
322 | | |
323 | 2 | if (compiler->sz_table != NULL) |
324 | 2 | yr_hash_table_destroy(compiler->sz_table, NULL); |
325 | | |
326 | 2 | if (compiler->objects_table != NULL) |
327 | 2 | yr_hash_table_destroy( |
328 | 2 | compiler->objects_table, |
329 | 2 | (YR_HASH_TABLE_FREE_VALUE_FUNC) yr_object_destroy); |
330 | | |
331 | 2 | if (compiler->atoms_config.free_quality_table) |
332 | 0 | yr_free(compiler->atoms_config.quality_table); |
333 | | |
334 | 2 | for (int i = 0; i < compiler->file_name_stack_ptr; i++) |
335 | 0 | yr_free(compiler->file_name_stack[i]); |
336 | | |
337 | 2 | YR_FIXUP* fixup = compiler->fixup_stack_head; |
338 | | |
339 | 2 | while (fixup != NULL) |
340 | 0 | { |
341 | 0 | YR_FIXUP* next_fixup = fixup->next; |
342 | 0 | yr_free(fixup); |
343 | 0 | fixup = next_fixup; |
344 | 0 | } |
345 | | |
346 | 2 | yr_free(compiler); |
347 | 2 | } |
348 | | |
349 | | YR_API void yr_compiler_set_callback( |
350 | | YR_COMPILER* compiler, |
351 | | YR_COMPILER_CALLBACK_FUNC callback, |
352 | | void* user_data) |
353 | 0 | { |
354 | 0 | compiler->callback = callback; |
355 | 0 | compiler->user_data = user_data; |
356 | 0 | } |
357 | | |
358 | | YR_API void yr_compiler_set_include_callback( |
359 | | YR_COMPILER* compiler, |
360 | | YR_COMPILER_INCLUDE_CALLBACK_FUNC include_callback, |
361 | | YR_COMPILER_INCLUDE_FREE_FUNC include_free, |
362 | | void* user_data) |
363 | 0 | { |
364 | 0 | compiler->include_callback = include_callback; |
365 | 0 | compiler->include_free = include_free; |
366 | 0 | compiler->incl_clbk_user_data = user_data; |
367 | 0 | } |
368 | | |
369 | | YR_API void yr_compiler_set_re_ast_callback( |
370 | | YR_COMPILER* compiler, |
371 | | YR_COMPILER_RE_AST_CALLBACK_FUNC re_ast_callback, |
372 | | void* user_data) |
373 | 0 | { |
374 | 0 | compiler->re_ast_callback = re_ast_callback; |
375 | 0 | compiler->re_ast_clbk_user_data = user_data; |
376 | 0 | } |
377 | | |
378 | | //////////////////////////////////////////////////////////////////////////////// |
379 | | // This function allows to specify an atom quality table to be used by the |
380 | | // compiler for choosing the best atoms from regular expressions and strings. |
381 | | // When a quality table is set, the compiler uses yr_atoms_table_quality |
382 | | // instead of yr_atoms_heuristic_quality for computing atom quality. The table |
383 | | // has an arbitrary number of entries, each composed of YR_MAX_ATOM_LENGTH + 1 |
384 | | // bytes. The first YR_MAX_ATOM_LENGTH bytes from each entry are the atom's |
385 | | // ones, and the remaining byte is a value in the range 0-255 determining the |
386 | | // atom's quality. Entries must be lexicographically sorted by atom in ascending |
387 | | // order. |
388 | | // |
389 | | // [ atom (YR_MAX_ATOM_LENGTH bytes) ] [ quality (1 byte) ] |
390 | | // |
391 | | // [ 00 00 .. 00 00 ] [ 00 ] |
392 | | // [ 00 00 .. 00 01 ] [ 45 ] |
393 | | // [ 00 00 .. 00 02 ] [ 13 ] |
394 | | // ... |
395 | | // [ FF FF .. FF FF ] [ 03 ] |
396 | | // |
397 | | // The "table" argument must point to a buffer containing the quality in |
398 | | // the format explained above, and "entries" must contain the number of entries |
399 | | // in the table. The table can not be freed while the compiler is in use, the |
400 | | // caller is responsible for freeing the table. |
401 | | // |
402 | | // The "warning_threshold" argument must be a number between 0 and 255, if some |
403 | | // atom chosen for a string have a quality below the specified threshold a |
404 | | // warning like "<string> is slowing down scanning" is shown. |
405 | | // |
406 | | YR_API void yr_compiler_set_atom_quality_table( |
407 | | YR_COMPILER* compiler, |
408 | | const void* table, |
409 | | int entries, |
410 | | unsigned char warning_threshold) |
411 | 0 | { |
412 | 0 | compiler->atoms_config.free_quality_table = false; |
413 | 0 | compiler->atoms_config.quality_warning_threshold = warning_threshold; |
414 | 0 | compiler->atoms_config.get_atom_quality = yr_atoms_table_quality; |
415 | 0 | compiler->atoms_config.quality_table_entries = entries; |
416 | 0 | compiler->atoms_config.quality_table = (YR_ATOM_QUALITY_TABLE_ENTRY*) table; |
417 | 0 | } |
418 | | |
419 | | //////////////////////////////////////////////////////////////////////////////// |
420 | | // Load an atom quality table from a file. The file's content must have the |
421 | | // format explained in the description for yr_compiler_set_atom_quality_table. |
422 | | // |
423 | | YR_API int yr_compiler_load_atom_quality_table( |
424 | | YR_COMPILER* compiler, |
425 | | const char* filename, |
426 | | unsigned char warning_threshold) |
427 | 0 | { |
428 | 0 | long file_size; |
429 | 0 | int entries; |
430 | 0 | void* table; |
431 | |
|
432 | 0 | FILE* fh = fopen(filename, "rb"); |
433 | |
|
434 | 0 | if (fh == NULL) |
435 | 0 | return ERROR_COULD_NOT_OPEN_FILE; |
436 | | |
437 | 0 | fseek(fh, 0L, SEEK_END); |
438 | 0 | file_size = ftell(fh); |
439 | 0 | fseek(fh, 0L, SEEK_SET); |
440 | |
|
441 | 0 | if (file_size == -1L) |
442 | 0 | { |
443 | 0 | fclose(fh); |
444 | 0 | return ERROR_COULD_NOT_READ_FILE; |
445 | 0 | } |
446 | | |
447 | 0 | table = yr_malloc(file_size); |
448 | |
|
449 | 0 | if (table == NULL) |
450 | 0 | { |
451 | 0 | fclose(fh); |
452 | 0 | return ERROR_INSUFFICIENT_MEMORY; |
453 | 0 | } |
454 | | |
455 | 0 | entries = (int) file_size / sizeof(YR_ATOM_QUALITY_TABLE_ENTRY); |
456 | |
|
457 | 0 | if (fread(table, sizeof(YR_ATOM_QUALITY_TABLE_ENTRY), entries, fh) != entries) |
458 | 0 | { |
459 | 0 | fclose(fh); |
460 | 0 | yr_free(table); |
461 | 0 | return ERROR_COULD_NOT_READ_FILE; |
462 | 0 | } |
463 | | |
464 | 0 | fclose(fh); |
465 | |
|
466 | 0 | yr_compiler_set_atom_quality_table( |
467 | 0 | compiler, table, entries, warning_threshold); |
468 | |
|
469 | 0 | compiler->atoms_config.free_quality_table = true; |
470 | |
|
471 | 0 | return ERROR_SUCCESS; |
472 | 0 | } |
473 | | |
474 | | int _yr_compiler_push_file_name(YR_COMPILER* compiler, const char* file_name) |
475 | 0 | { |
476 | 0 | char* str; |
477 | 0 | int i; |
478 | |
|
479 | 0 | for (i = 0; i < compiler->file_name_stack_ptr; i++) |
480 | 0 | { |
481 | 0 | if (strcmp(file_name, compiler->file_name_stack[i]) == 0) |
482 | 0 | return ERROR_INCLUDES_CIRCULAR_REFERENCE; |
483 | 0 | } |
484 | | |
485 | 0 | if (compiler->file_name_stack_ptr == YR_MAX_INCLUDE_DEPTH) |
486 | 0 | return ERROR_INCLUDE_DEPTH_EXCEEDED; |
487 | | |
488 | 0 | str = yr_strdup(file_name); |
489 | |
|
490 | 0 | if (str == NULL) |
491 | 0 | return ERROR_INSUFFICIENT_MEMORY; |
492 | | |
493 | 0 | compiler->file_name_stack[compiler->file_name_stack_ptr] = str; |
494 | 0 | compiler->file_name_stack_ptr++; |
495 | |
|
496 | 0 | return ERROR_SUCCESS; |
497 | 0 | } |
498 | | |
499 | | void _yr_compiler_pop_file_name(YR_COMPILER* compiler) |
500 | 0 | { |
501 | 0 | if (compiler->file_name_stack_ptr > 0) |
502 | 0 | { |
503 | 0 | compiler->file_name_stack_ptr--; |
504 | 0 | yr_free(compiler->file_name_stack[compiler->file_name_stack_ptr]); |
505 | 0 | compiler->file_name_stack[compiler->file_name_stack_ptr] = NULL; |
506 | 0 | } |
507 | 0 | } |
508 | | |
509 | | int _yr_compiler_get_var_frame(YR_COMPILER* compiler) |
510 | 0 | { |
511 | 0 | int i, result = 0; |
512 | |
|
513 | 0 | for (i = 0; i < compiler->loop_index; i++) |
514 | 0 | { |
515 | 0 | result += compiler->loop[i].vars_count + |
516 | 0 | compiler->loop[i].vars_internal_count; |
517 | 0 | } |
518 | |
|
519 | 0 | return result; |
520 | 0 | } |
521 | | |
522 | | YR_API char* yr_compiler_get_current_file_name(YR_COMPILER* compiler) |
523 | 0 | { |
524 | 0 | if (compiler->file_name_stack_ptr > 0) |
525 | 0 | { |
526 | 0 | return compiler->file_name_stack[compiler->file_name_stack_ptr - 1]; |
527 | 0 | } |
528 | 0 | else |
529 | 0 | { |
530 | 0 | return NULL; |
531 | 0 | } |
532 | 0 | } |
533 | | |
534 | | static int _yr_compiler_set_namespace( |
535 | | YR_COMPILER* compiler, |
536 | | const char* namespace_) |
537 | 2 | { |
538 | 2 | YR_NAMESPACE* ns = (YR_NAMESPACE*) yr_arena_get_ptr( |
539 | 2 | compiler->arena, YR_NAMESPACES_TABLE, 0); |
540 | | |
541 | 2 | bool found = false; |
542 | | |
543 | 2 | for (int i = 0; i < compiler->num_namespaces; i++, ns++) |
544 | 0 | { |
545 | 0 | if (strcmp(ns->name, namespace_) == 0) |
546 | 0 | { |
547 | 0 | found = true; |
548 | 0 | compiler->current_namespace_idx = i; |
549 | 0 | break; |
550 | 0 | } |
551 | 0 | } |
552 | | |
553 | 2 | if (!found) |
554 | 2 | { |
555 | 2 | YR_ARENA_REF ref; |
556 | | |
557 | 2 | FAIL_ON_ERROR(yr_arena_allocate_struct( |
558 | 2 | compiler->arena, |
559 | 2 | YR_NAMESPACES_TABLE, |
560 | 2 | sizeof(YR_NAMESPACE), |
561 | 2 | &ref, |
562 | 2 | offsetof(YR_NAMESPACE, name), |
563 | 2 | EOL)); |
564 | | |
565 | 2 | ns = (YR_NAMESPACE*) yr_arena_ref_to_ptr(compiler->arena, &ref); |
566 | | |
567 | 2 | FAIL_ON_ERROR(_yr_compiler_store_string(compiler, namespace_, &ref)); |
568 | | |
569 | 2 | ns->name = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref); |
570 | 2 | ns->idx = compiler->num_namespaces; |
571 | | |
572 | 2 | compiler->current_namespace_idx = compiler->num_namespaces; |
573 | 2 | compiler->num_namespaces++; |
574 | 2 | } |
575 | | |
576 | 2 | return ERROR_SUCCESS; |
577 | 2 | } |
578 | | |
579 | | YR_API int yr_compiler_add_file( |
580 | | YR_COMPILER* compiler, |
581 | | FILE* rules_file, |
582 | | const char* namespace_, |
583 | | const char* file_name) |
584 | 0 | { |
585 | 0 | int result; |
586 | | |
587 | | // Don't allow yr_compiler_add_file() after |
588 | | // yr_compiler_get_rules() has been called. |
589 | |
|
590 | 0 | assert(compiler->rules == NULL); |
591 | | |
592 | | // Don't allow calls to yr_compiler_add_file() if a previous call to |
593 | | // yr_compiler_add_XXXX failed. |
594 | |
|
595 | 0 | assert(compiler->errors == 0); |
596 | |
|
597 | 0 | if (namespace_ != NULL) |
598 | 0 | compiler->last_error = _yr_compiler_set_namespace(compiler, namespace_); |
599 | 0 | else |
600 | 0 | compiler->last_error = _yr_compiler_set_namespace(compiler, "default"); |
601 | |
|
602 | 0 | if (compiler->last_error == ERROR_SUCCESS && file_name != NULL) |
603 | 0 | compiler->last_error = _yr_compiler_push_file_name(compiler, file_name); |
604 | |
|
605 | 0 | if (compiler->last_error != ERROR_SUCCESS) |
606 | 0 | return ++compiler->errors; |
607 | | |
608 | 0 | result = yr_lex_parse_rules_file(rules_file, compiler); |
609 | |
|
610 | 0 | if (file_name != NULL) |
611 | 0 | _yr_compiler_pop_file_name(compiler); |
612 | |
|
613 | 0 | return result; |
614 | 0 | } |
615 | | |
616 | | YR_API int yr_compiler_add_fd( |
617 | | YR_COMPILER* compiler, |
618 | | YR_FILE_DESCRIPTOR rules_fd, |
619 | | const char* namespace_, |
620 | | const char* file_name) |
621 | 0 | { |
622 | 0 | int result; |
623 | | |
624 | | // Don't allow yr_compiler_add_fd() after |
625 | | // yr_compiler_get_rules() has been called. |
626 | 0 | assert(compiler->rules == NULL); |
627 | | |
628 | | // Don't allow calls to yr_compiler_add_fd() if a previous call to |
629 | | // yr_compiler_add_XXXX failed. |
630 | 0 | assert(compiler->errors == 0); |
631 | |
|
632 | 0 | if (namespace_ != NULL) |
633 | 0 | compiler->last_error = _yr_compiler_set_namespace(compiler, namespace_); |
634 | 0 | else |
635 | 0 | compiler->last_error = _yr_compiler_set_namespace(compiler, "default"); |
636 | |
|
637 | 0 | if (compiler->last_error == ERROR_SUCCESS && file_name != NULL) |
638 | 0 | compiler->last_error = _yr_compiler_push_file_name(compiler, file_name); |
639 | |
|
640 | 0 | if (compiler->last_error != ERROR_SUCCESS) |
641 | 0 | return ++compiler->errors; |
642 | | |
643 | 0 | result = yr_lex_parse_rules_fd(rules_fd, compiler); |
644 | |
|
645 | 0 | if (file_name != NULL) |
646 | 0 | _yr_compiler_pop_file_name(compiler); |
647 | |
|
648 | 0 | return result; |
649 | 0 | } |
650 | | |
651 | | YR_API int yr_compiler_add_bytes( |
652 | | YR_COMPILER* compiler, |
653 | | const void* rules_data, |
654 | | size_t rules_size, |
655 | | const char* namespace_) |
656 | 0 | { |
657 | | // Don't allow calls to yr_compiler_add_bytes() after |
658 | | // yr_compiler_get_rules() has been called. |
659 | 0 | assert(compiler->rules == NULL); |
660 | | |
661 | | // Don't allow calls to yr_compiler_add_bytes() if a previous call to |
662 | | // yr_compiler_add_XXXX failed. |
663 | 0 | assert(compiler->errors == 0); |
664 | |
|
665 | 0 | if (namespace_ != NULL) |
666 | 0 | compiler->last_error = _yr_compiler_set_namespace(compiler, namespace_); |
667 | 0 | else |
668 | 0 | compiler->last_error = _yr_compiler_set_namespace(compiler, "default"); |
669 | |
|
670 | 0 | if (compiler->last_error != ERROR_SUCCESS) |
671 | 0 | return ++compiler->errors; |
672 | | |
673 | 0 | return yr_lex_parse_rules_bytes(rules_data, rules_size, compiler); |
674 | 0 | } |
675 | | |
676 | | YR_API int yr_compiler_add_string( |
677 | | YR_COMPILER* compiler, |
678 | | const char* rules_string, |
679 | | const char* namespace_) |
680 | 2 | { |
681 | | // Don't allow calls to yr_compiler_add_string() after |
682 | | // yr_compiler_get_rules() has been called. |
683 | 2 | assert(compiler->rules == NULL); |
684 | | |
685 | | // Don't allow calls to yr_compiler_add_string() if a previous call to |
686 | | // yr_compiler_add_XXXX failed. |
687 | 2 | assert(compiler->errors == 0); |
688 | | |
689 | 2 | if (namespace_ != NULL) |
690 | 0 | compiler->last_error = _yr_compiler_set_namespace(compiler, namespace_); |
691 | 2 | else |
692 | 2 | compiler->last_error = _yr_compiler_set_namespace(compiler, "default"); |
693 | | |
694 | 2 | if (compiler->last_error != ERROR_SUCCESS) |
695 | 0 | return ++compiler->errors; |
696 | | |
697 | 2 | return yr_lex_parse_rules_string(rules_string, compiler); |
698 | 2 | } |
699 | | |
700 | | static int _yr_compiler_compile_rules(YR_COMPILER* compiler) |
701 | 2 | { |
702 | 2 | YR_RULE null_rule; |
703 | 2 | YR_EXTERNAL_VARIABLE null_external; |
704 | | |
705 | 2 | uint8_t halt = OP_HALT; |
706 | | |
707 | | // Write halt instruction at the end of code. |
708 | 2 | FAIL_ON_ERROR(yr_arena_write_data( |
709 | 2 | compiler->arena, YR_CODE_SECTION, &halt, sizeof(uint8_t), NULL)); |
710 | | |
711 | | // Write a null rule indicating the end. |
712 | 2 | memset(&null_rule, 0xFA, sizeof(YR_RULE)); |
713 | 2 | null_rule.flags = RULE_FLAGS_NULL; |
714 | | |
715 | 2 | FAIL_ON_ERROR(yr_arena_write_data( |
716 | 2 | compiler->arena, YR_RULES_TABLE, &null_rule, sizeof(YR_RULE), NULL)); |
717 | | |
718 | | // Write a null external indicating the end. |
719 | 2 | memset(&null_external, 0xFA, sizeof(YR_EXTERNAL_VARIABLE)); |
720 | 2 | null_external.type = EXTERNAL_VARIABLE_TYPE_NULL; |
721 | | |
722 | 2 | FAIL_ON_ERROR(yr_arena_write_data( |
723 | 2 | compiler->arena, |
724 | 2 | YR_EXTERNAL_VARIABLES_TABLE, |
725 | 2 | &null_external, |
726 | 2 | sizeof(YR_EXTERNAL_VARIABLE), |
727 | 2 | NULL)); |
728 | | |
729 | | // Write Aho-Corasick automaton to arena. |
730 | 2 | FAIL_ON_ERROR(yr_ac_compile(compiler->automaton, compiler->arena)); |
731 | | |
732 | 2 | YR_ARENA_REF ref; |
733 | | |
734 | 2 | FAIL_ON_ERROR(yr_arena_allocate_struct( |
735 | 2 | compiler->arena, YR_SUMMARY_SECTION, sizeof(YR_SUMMARY), &ref, EOL)); |
736 | | |
737 | 2 | YR_SUMMARY* summary = (YR_SUMMARY*) yr_arena_ref_to_ptr( |
738 | 2 | compiler->arena, &ref); |
739 | | |
740 | 2 | summary->num_namespaces = compiler->num_namespaces; |
741 | 2 | summary->num_rules = compiler->next_rule_idx; |
742 | 2 | summary->num_strings = compiler->current_string_idx; |
743 | | |
744 | 2 | return yr_rules_from_arena(compiler->arena, &compiler->rules); |
745 | 2 | } |
746 | | |
747 | | YR_API int yr_compiler_get_rules(YR_COMPILER* compiler, YR_RULES** rules) |
748 | 2 | { |
749 | | // Don't allow calls to yr_compiler_get_rules() if a previous call to |
750 | | // yr_compiler_add_XXXX failed. |
751 | 2 | assert(compiler->errors == 0); |
752 | | |
753 | 2 | *rules = NULL; |
754 | | |
755 | 2 | if (compiler->rules == NULL) |
756 | 2 | FAIL_ON_ERROR(_yr_compiler_compile_rules(compiler)); |
757 | | |
758 | 2 | *rules = compiler->rules; |
759 | | |
760 | 2 | return ERROR_SUCCESS; |
761 | 2 | } |
762 | | |
763 | | static int _yr_compiler_define_variable( |
764 | | YR_COMPILER* compiler, |
765 | | YR_EXTERNAL_VARIABLE* external) |
766 | 0 | { |
767 | 0 | YR_EXTERNAL_VARIABLE* ext; |
768 | 0 | YR_OBJECT* object; |
769 | |
|
770 | 0 | if (external->identifier == NULL) |
771 | 0 | return ERROR_INVALID_ARGUMENT; |
772 | | |
773 | 0 | object = (YR_OBJECT*) yr_hash_table_lookup( |
774 | 0 | compiler->objects_table, external->identifier, NULL); |
775 | |
|
776 | 0 | if (object != NULL) |
777 | 0 | return ERROR_DUPLICATED_EXTERNAL_VARIABLE; |
778 | | |
779 | 0 | YR_ARENA_REF ext_ref; |
780 | 0 | YR_ARENA_REF ref; |
781 | |
|
782 | 0 | FAIL_ON_ERROR(yr_arena_allocate_struct( |
783 | 0 | compiler->arena, |
784 | 0 | YR_EXTERNAL_VARIABLES_TABLE, |
785 | 0 | sizeof(YR_EXTERNAL_VARIABLE), |
786 | 0 | &ext_ref, |
787 | 0 | offsetof(YR_EXTERNAL_VARIABLE, identifier), |
788 | 0 | EOL)); |
789 | |
|
790 | 0 | ext = (YR_EXTERNAL_VARIABLE*) yr_arena_ref_to_ptr(compiler->arena, &ext_ref); |
791 | |
|
792 | 0 | FAIL_ON_ERROR( |
793 | 0 | _yr_compiler_store_string(compiler, external->identifier, &ref)); |
794 | |
|
795 | 0 | ext->identifier = (const char*) yr_arena_ref_to_ptr(compiler->arena, &ref); |
796 | |
|
797 | 0 | ext->type = external->type; |
798 | 0 | ext->value = external->value; |
799 | |
|
800 | 0 | if (external->type == EXTERNAL_VARIABLE_TYPE_STRING) |
801 | 0 | { |
802 | 0 | if (external->value.s == NULL) |
803 | 0 | return ERROR_INVALID_ARGUMENT; |
804 | | |
805 | 0 | FAIL_ON_ERROR(_yr_compiler_store_string(compiler, external->value.s, &ref)); |
806 | |
|
807 | 0 | FAIL_ON_ERROR(yr_arena_make_ptr_relocatable( |
808 | 0 | compiler->arena, |
809 | 0 | YR_EXTERNAL_VARIABLES_TABLE, |
810 | 0 | ext_ref.offset + offsetof(YR_EXTERNAL_VARIABLE, value.s), |
811 | 0 | EOL)); |
812 | |
|
813 | 0 | ext->value.s = (char*) yr_arena_ref_to_ptr(compiler->arena, &ref); |
814 | 0 | } |
815 | | |
816 | 0 | FAIL_ON_ERROR(yr_object_from_external_variable(external, &object)); |
817 | |
|
818 | 0 | FAIL_ON_ERROR_WITH_CLEANUP( |
819 | 0 | yr_hash_table_add( |
820 | 0 | compiler->objects_table, external->identifier, NULL, (void*) object), |
821 | 0 | yr_object_destroy(object)); |
822 | |
|
823 | 0 | return ERROR_SUCCESS; |
824 | 0 | } |
825 | | |
826 | | YR_API int yr_compiler_define_integer_variable( |
827 | | YR_COMPILER* compiler, |
828 | | const char* identifier, |
829 | | int64_t value) |
830 | 0 | { |
831 | 0 | YR_EXTERNAL_VARIABLE external; |
832 | |
|
833 | 0 | external.type = EXTERNAL_VARIABLE_TYPE_INTEGER; |
834 | 0 | external.identifier = identifier; |
835 | 0 | external.value.i = value; |
836 | |
|
837 | 0 | FAIL_ON_ERROR(_yr_compiler_define_variable(compiler, &external)); |
838 | |
|
839 | 0 | return ERROR_SUCCESS; |
840 | 0 | } |
841 | | |
842 | | YR_API int yr_compiler_define_boolean_variable( |
843 | | YR_COMPILER* compiler, |
844 | | const char* identifier, |
845 | | int value) |
846 | 0 | { |
847 | 0 | YR_EXTERNAL_VARIABLE external; |
848 | |
|
849 | 0 | external.type = EXTERNAL_VARIABLE_TYPE_BOOLEAN; |
850 | 0 | external.identifier = identifier; |
851 | 0 | external.value.i = value; |
852 | |
|
853 | 0 | FAIL_ON_ERROR(_yr_compiler_define_variable(compiler, &external)); |
854 | |
|
855 | 0 | return ERROR_SUCCESS; |
856 | 0 | } |
857 | | |
858 | | YR_API int yr_compiler_define_float_variable( |
859 | | YR_COMPILER* compiler, |
860 | | const char* identifier, |
861 | | double value) |
862 | 0 | { |
863 | 0 | YR_EXTERNAL_VARIABLE external; |
864 | |
|
865 | 0 | external.type = EXTERNAL_VARIABLE_TYPE_FLOAT; |
866 | 0 | external.identifier = identifier; |
867 | 0 | external.value.f = value; |
868 | |
|
869 | 0 | FAIL_ON_ERROR(_yr_compiler_define_variable(compiler, &external)); |
870 | |
|
871 | 0 | return ERROR_SUCCESS; |
872 | 0 | } |
873 | | |
874 | | YR_API int yr_compiler_define_string_variable( |
875 | | YR_COMPILER* compiler, |
876 | | const char* identifier, |
877 | | const char* value) |
878 | 0 | { |
879 | 0 | YR_EXTERNAL_VARIABLE external; |
880 | |
|
881 | 0 | external.type = EXTERNAL_VARIABLE_TYPE_STRING; |
882 | 0 | external.identifier = identifier; |
883 | 0 | external.value.s = (char*) value; |
884 | |
|
885 | 0 | FAIL_ON_ERROR(_yr_compiler_define_variable(compiler, &external)); |
886 | |
|
887 | 0 | return ERROR_SUCCESS; |
888 | 0 | } |
889 | | |
890 | | YR_API char* yr_compiler_get_error_message( |
891 | | YR_COMPILER* compiler, |
892 | | char* buffer, |
893 | | int buffer_size) |
894 | 0 | { |
895 | 0 | uint32_t max_strings_per_rule; |
896 | |
|
897 | 0 | switch (compiler->last_error) |
898 | 0 | { |
899 | 0 | case ERROR_INSUFFICIENT_MEMORY: |
900 | 0 | snprintf(buffer, buffer_size, "not enough memory"); |
901 | 0 | break; |
902 | 0 | case ERROR_DUPLICATED_IDENTIFIER: |
903 | 0 | snprintf( |
904 | 0 | buffer, |
905 | 0 | buffer_size, |
906 | 0 | "duplicated identifier \"%s\"", |
907 | 0 | compiler->last_error_extra_info); |
908 | 0 | break; |
909 | 0 | case ERROR_DUPLICATED_STRING_IDENTIFIER: |
910 | 0 | snprintf( |
911 | 0 | buffer, |
912 | 0 | buffer_size, |
913 | 0 | "duplicated string identifier \"%s\"", |
914 | 0 | compiler->last_error_extra_info); |
915 | 0 | break; |
916 | 0 | case ERROR_DUPLICATED_TAG_IDENTIFIER: |
917 | 0 | snprintf( |
918 | 0 | buffer, |
919 | 0 | buffer_size, |
920 | 0 | "duplicated tag identifier \"%s\"", |
921 | 0 | compiler->last_error_extra_info); |
922 | 0 | break; |
923 | 0 | case ERROR_DUPLICATED_META_IDENTIFIER: |
924 | 0 | snprintf( |
925 | 0 | buffer, |
926 | 0 | buffer_size, |
927 | 0 | "duplicated metadata identifier \"%s\"", |
928 | 0 | compiler->last_error_extra_info); |
929 | 0 | break; |
930 | 0 | case ERROR_DUPLICATED_LOOP_IDENTIFIER: |
931 | 0 | snprintf( |
932 | 0 | buffer, |
933 | 0 | buffer_size, |
934 | 0 | "duplicated loop identifier \"%s\"", |
935 | 0 | compiler->last_error_extra_info); |
936 | 0 | break; |
937 | 0 | case ERROR_UNDEFINED_STRING: |
938 | 0 | snprintf( |
939 | 0 | buffer, |
940 | 0 | buffer_size, |
941 | 0 | "undefined string \"%s\"", |
942 | 0 | compiler->last_error_extra_info); |
943 | 0 | break; |
944 | 0 | case ERROR_UNDEFINED_IDENTIFIER: |
945 | 0 | snprintf( |
946 | 0 | buffer, |
947 | 0 | buffer_size, |
948 | 0 | "undefined identifier \"%s\"", |
949 | 0 | compiler->last_error_extra_info); |
950 | 0 | break; |
951 | 0 | case ERROR_UNREFERENCED_STRING: |
952 | 0 | snprintf( |
953 | 0 | buffer, |
954 | 0 | buffer_size, |
955 | 0 | "unreferenced string \"%s\"", |
956 | 0 | compiler->last_error_extra_info); |
957 | 0 | break; |
958 | 0 | case ERROR_EMPTY_STRING: |
959 | 0 | snprintf( |
960 | 0 | buffer, |
961 | 0 | buffer_size, |
962 | 0 | "empty string \"%s\"", |
963 | 0 | compiler->last_error_extra_info); |
964 | 0 | break; |
965 | 0 | case ERROR_NOT_A_STRUCTURE: |
966 | 0 | snprintf( |
967 | 0 | buffer, |
968 | 0 | buffer_size, |
969 | 0 | "\"%s\" is not a structure", |
970 | 0 | compiler->last_error_extra_info); |
971 | 0 | break; |
972 | 0 | case ERROR_NOT_INDEXABLE: |
973 | 0 | snprintf( |
974 | 0 | buffer, |
975 | 0 | buffer_size, |
976 | 0 | "\"%s\" is not an array or dictionary", |
977 | 0 | compiler->last_error_extra_info); |
978 | 0 | break; |
979 | 0 | case ERROR_NOT_A_FUNCTION: |
980 | 0 | snprintf( |
981 | 0 | buffer, |
982 | 0 | buffer_size, |
983 | 0 | "\"%s\" is not a function", |
984 | 0 | compiler->last_error_extra_info); |
985 | 0 | break; |
986 | 0 | case ERROR_INVALID_FIELD_NAME: |
987 | 0 | snprintf( |
988 | 0 | buffer, |
989 | 0 | buffer_size, |
990 | 0 | "invalid field name \"%s\"", |
991 | 0 | compiler->last_error_extra_info); |
992 | 0 | break; |
993 | 0 | case ERROR_MISPLACED_ANONYMOUS_STRING: |
994 | 0 | snprintf(buffer, buffer_size, "wrong use of anonymous string"); |
995 | 0 | break; |
996 | 0 | case ERROR_INCLUDES_CIRCULAR_REFERENCE: |
997 | 0 | snprintf(buffer, buffer_size, "include circular reference"); |
998 | 0 | break; |
999 | 0 | case ERROR_INCLUDE_DEPTH_EXCEEDED: |
1000 | 0 | snprintf(buffer, buffer_size, "too many levels of included rules"); |
1001 | 0 | break; |
1002 | 0 | case ERROR_LOOP_NESTING_LIMIT_EXCEEDED: |
1003 | 0 | snprintf(buffer, buffer_size, "loop nesting limit exceeded"); |
1004 | 0 | break; |
1005 | 0 | case ERROR_NESTED_FOR_OF_LOOP: |
1006 | 0 | snprintf( |
1007 | 0 | buffer, |
1008 | 0 | buffer_size, |
1009 | 0 | "'for <quantifier> of <string set>' loops can't be nested"); |
1010 | 0 | break; |
1011 | 0 | case ERROR_UNKNOWN_MODULE: |
1012 | 0 | snprintf( |
1013 | 0 | buffer, |
1014 | 0 | buffer_size, |
1015 | 0 | "unknown module \"%s\"", |
1016 | 0 | compiler->last_error_extra_info); |
1017 | 0 | break; |
1018 | 0 | case ERROR_INVALID_MODULE_NAME: |
1019 | 0 | snprintf( |
1020 | 0 | buffer, |
1021 | 0 | buffer_size, |
1022 | 0 | "invalid module name \"%s\"", |
1023 | 0 | compiler->last_error_extra_info); |
1024 | 0 | break; |
1025 | 0 | case ERROR_DUPLICATED_STRUCTURE_MEMBER: |
1026 | 0 | snprintf(buffer, buffer_size, "duplicated structure member"); |
1027 | 0 | break; |
1028 | 0 | case ERROR_WRONG_ARGUMENTS: |
1029 | 0 | snprintf( |
1030 | 0 | buffer, |
1031 | 0 | buffer_size, |
1032 | 0 | "wrong arguments for function \"%s\"", |
1033 | 0 | compiler->last_error_extra_info); |
1034 | 0 | break; |
1035 | 0 | case ERROR_WRONG_RETURN_TYPE: |
1036 | 0 | snprintf(buffer, buffer_size, "wrong return type for overloaded function"); |
1037 | 0 | break; |
1038 | 0 | case ERROR_INVALID_HEX_STRING: |
1039 | 0 | case ERROR_INVALID_REGULAR_EXPRESSION: |
1040 | 0 | case ERROR_SYNTAX_ERROR: |
1041 | 0 | case ERROR_WRONG_TYPE: |
1042 | 0 | case ERROR_INVALID_MODIFIER: |
1043 | 0 | case ERROR_INVALID_PERCENTAGE: |
1044 | 0 | snprintf(buffer, buffer_size, "%s", compiler->last_error_extra_info); |
1045 | 0 | break; |
1046 | 0 | case ERROR_INTERNAL_FATAL_ERROR: |
1047 | 0 | snprintf(buffer, buffer_size, "internal fatal error"); |
1048 | 0 | break; |
1049 | 0 | case ERROR_DIVISION_BY_ZERO: |
1050 | 0 | snprintf(buffer, buffer_size, "division by zero"); |
1051 | 0 | break; |
1052 | 0 | case ERROR_REGULAR_EXPRESSION_TOO_LARGE: |
1053 | 0 | snprintf(buffer, buffer_size, "regular expression is too large"); |
1054 | 0 | break; |
1055 | 0 | case ERROR_REGULAR_EXPRESSION_TOO_COMPLEX: |
1056 | 0 | snprintf(buffer, buffer_size, "regular expression is too complex"); |
1057 | 0 | break; |
1058 | 0 | case ERROR_TOO_MANY_STRINGS: |
1059 | 0 | yr_get_configuration_uint32( |
1060 | 0 | YR_CONFIG_MAX_STRINGS_PER_RULE, &max_strings_per_rule); |
1061 | 0 | snprintf( |
1062 | 0 | buffer, |
1063 | 0 | buffer_size, |
1064 | 0 | "too many strings in rule \"%s\" (limit: %d)", |
1065 | 0 | compiler->last_error_extra_info, |
1066 | 0 | max_strings_per_rule); |
1067 | 0 | break; |
1068 | 0 | case ERROR_INTEGER_OVERFLOW: |
1069 | 0 | snprintf( |
1070 | 0 | buffer, |
1071 | 0 | buffer_size, |
1072 | 0 | "integer overflow in \"%s\"", |
1073 | 0 | compiler->last_error_extra_info); |
1074 | 0 | break; |
1075 | 0 | case ERROR_COULD_NOT_READ_FILE: |
1076 | 0 | snprintf(buffer, buffer_size, "could not read file"); |
1077 | 0 | break; |
1078 | 0 | case ERROR_DUPLICATED_MODIFIER: |
1079 | 0 | snprintf(buffer, buffer_size, "duplicated modifier"); |
1080 | 0 | break; |
1081 | 0 | case ERROR_IDENTIFIER_MATCHES_WILDCARD: |
1082 | 0 | snprintf( |
1083 | 0 | buffer, |
1084 | 0 | buffer_size, |
1085 | 0 | "rule identifier \"%s\" matches previously used wildcard rule set", |
1086 | 0 | compiler->last_error_extra_info); |
1087 | 0 | break; |
1088 | 0 | case ERROR_INVALID_VALUE: |
1089 | 0 | snprintf( |
1090 | 0 | buffer, |
1091 | 0 | buffer_size, |
1092 | 0 | "invalid value in condition: \"%s\"", |
1093 | 0 | compiler->last_error_extra_info); |
1094 | 0 | break; |
1095 | 0 | } |
1096 | | |
1097 | 0 | return buffer; |
1098 | 0 | } |
1099 | | |
1100 | | int yr_string_set_destroy(YR_STRING_SET string_set) |
1101 | 0 | { |
1102 | 0 | YR_STRING_SET_ELEMENT* head = string_set.head; |
1103 | 0 | while (head != NULL) { |
1104 | 0 | YR_STRING_SET_ELEMENT* next_head = head->next; |
1105 | 0 | yr_free(head); |
1106 | 0 | head = next_head; |
1107 | 0 | } |
1108 | 0 | return ERROR_SUCCESS; |
1109 | 0 | } |