2012-04-23 00:02:42 +01:00
|
|
|
/* Parser combinators for binary formats.
|
|
|
|
|
* Copyright (C) 2012 Meredith L. Patterson, Dan "TQ" Hirsch
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation, version 2.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-05-01 00:33:47 +01:00
|
|
|
#include <assert.h>
|
2012-05-12 01:28:52 +01:00
|
|
|
#include <ctype.h>
|
2012-11-13 22:51:31 -05:00
|
|
|
#include <err.h>
|
2012-05-24 13:22:43 +02:00
|
|
|
#include <limits.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <string.h>
|
2012-05-26 16:00:43 +02:00
|
|
|
#include "hammer.h"
|
|
|
|
|
#include "internal.h"
|
|
|
|
|
#include "allocator.h"
|
|
|
|
|
#include "parsers/parser_internal.h"
|
2012-04-22 04:47:08 +01:00
|
|
|
|
2012-10-10 16:24:12 +02:00
|
|
|
static uint32_t djbhash(const uint8_t *buf, size_t len) {
|
|
|
|
|
uint32_t hash = 5381;
|
2012-05-03 01:58:09 +01:00
|
|
|
while (len--) {
|
|
|
|
|
hash = hash * 33 + *buf++;
|
2012-04-30 03:44:10 +01:00
|
|
|
}
|
|
|
|
|
return hash;
|
2012-04-22 04:47:08 +01:00
|
|
|
}
|
2012-04-22 05:15:40 +01:00
|
|
|
|
2012-05-01 00:33:47 +01:00
|
|
|
/* Helper function, since these lines appear in every parser */
|
2012-05-01 03:21:14 +01:00
|
|
|
|
2012-05-01 03:59:49 +01:00
|
|
|
typedef struct {
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParser *p1;
|
|
|
|
|
const HParser *p2;
|
|
|
|
|
} HTwoParsers;
|
2012-05-01 03:59:49 +01:00
|
|
|
|
2012-04-30 03:44:10 +01:00
|
|
|
|
2012-10-10 16:24:12 +02:00
|
|
|
static uint32_t cache_key_hash(const void* key) {
|
2012-05-26 13:13:41 +02:00
|
|
|
return djbhash(key, sizeof(HParserCacheKey));
|
2012-05-03 01:58:09 +01:00
|
|
|
}
|
2012-10-10 16:24:12 +02:00
|
|
|
static bool cache_key_equal(const void* key1, const void* key2) {
|
2012-05-26 13:13:41 +02:00
|
|
|
return memcmp(key1, key2, sizeof(HParserCacheKey)) == 0;
|
2012-05-03 01:58:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-10-10 15:58:03 +02:00
|
|
|
HParseResult* h_parse(const HParser* parser, const uint8_t* input, size_t length) {
|
|
|
|
|
return h_parse__m(&system_allocator, parser, input, length);
|
|
|
|
|
}
|
|
|
|
|
HParseResult* h_parse__m(HAllocator* mm__, const HParser* parser, const uint8_t* input, size_t length) {
|
2012-05-03 01:58:09 +01:00
|
|
|
// Set up a parse state...
|
2012-10-10 15:58:03 +02:00
|
|
|
HArena * arena = h_new_arena(mm__, 0);
|
2012-05-26 12:03:58 +02:00
|
|
|
HParseState *parse_state = a_new_(arena, HParseState, 1);
|
2012-10-08 21:12:56 +02:00
|
|
|
parse_state->cache = h_hashtable_new(arena, cache_key_equal, // key_equal_func
|
|
|
|
|
cache_key_hash); // hash_func
|
2012-05-03 01:58:09 +01:00
|
|
|
parse_state->input_stream.input = input;
|
2012-05-12 21:20:38 +01:00
|
|
|
parse_state->input_stream.index = 0;
|
2012-05-03 01:58:09 +01:00
|
|
|
parse_state->input_stream.bit_offset = 8; // bit big endian
|
2012-05-12 21:20:38 +01:00
|
|
|
parse_state->input_stream.overrun = 0;
|
2012-05-03 01:58:09 +01:00
|
|
|
parse_state->input_stream.endianness = BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN;
|
|
|
|
|
parse_state->input_stream.length = length;
|
2012-10-08 18:16:18 +02:00
|
|
|
parse_state->lr_stack = h_slist_new(arena);
|
2012-10-08 21:12:56 +02:00
|
|
|
parse_state->recursion_heads = h_hashtable_new(arena, cache_key_equal,
|
|
|
|
|
cache_key_hash);
|
2012-05-12 21:20:38 +01:00
|
|
|
parse_state->arena = arena;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *res = h_do_parse(parser, parse_state);
|
2012-10-08 18:16:18 +02:00
|
|
|
h_slist_free(parse_state->lr_stack);
|
2012-10-08 21:12:56 +02:00
|
|
|
h_hashtable_free(parse_state->recursion_heads);
|
2012-05-17 15:51:19 +02:00
|
|
|
// tear down the parse state
|
2012-10-08 21:12:56 +02:00
|
|
|
h_hashtable_free(parse_state->cache);
|
2012-05-17 15:51:19 +02:00
|
|
|
if (!res)
|
2012-05-26 14:06:52 +02:00
|
|
|
h_delete_arena(parse_state->arena);
|
2012-05-03 01:58:09 +01:00
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2012-05-11 15:14:30 +01:00
|
|
|
|
2012-07-27 15:06:40 -07:00
|
|
|
void h_parse_result_free(HParseResult *result) {
|
|
|
|
|
h_delete_arena(result->arena);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-11 15:14:30 +01:00
|
|
|
|