static int cond_some(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) { guint got_so_far = 0; int start = offset; tvbparse_elem_t* ret_tok = NULL; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_SOME) g_warning("cond_some: START"); #endif if ( offset > tt->end_offset ) return -1; if ( wanted->min == 0 ) { ret_tok = new_tok(tt,wanted->id,offset,0,wanted); } while (got_so_far < wanted->max) { tvbparse_elem_t* new_elem = NULL; int consumed; if ( offset > tt->end_offset ) return -1; consumed = wanted->control.subelem->condition(tt, offset, wanted->control.subelem, &new_elem); if(consumed >= 0) { if (ret_tok) { ret_tok->len = (new_elem->offset - ret_tok->offset) + new_elem->len; if (ret_tok->sub) { ret_tok->sub->last->next = new_elem; ret_tok->sub->last = new_elem; } else { ret_tok->sub = new_elem; } } else { ret_tok = new_tok(tt, wanted->id, new_elem->offset, new_elem->len, wanted); ret_tok->sub = new_elem; } } else { break; } offset += consumed; got_so_far++; } #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_SOME) g_warning("cond_some: got num=%u",got_so_far); #endif if(got_so_far < wanted->min) { return -1; } *tok = ret_tok; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_SOME) g_warning("cond_some: GOT len=%i",offset - start); #endif return offset - start; }
static int cond_not_char(tvbparse_t* tt, const int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) { gchar c, t; guint i; gboolean not_matched = FALSE; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_NOT_CHAR) g_warning("cond_not_char: control='%s'",wanted->control.str); #endif if (! offset < tt->end_offset ) { return -1; } t = (gchar) tvb_get_guint8(tt->tvb,offset); for(i = 0; (c = wanted->control.str[i]); i++) { if ( c == t ) { not_matched = TRUE; } } if (not_matched) { return -1; } else { *tok = new_tok(tt,wanted->id,offset,1,wanted); #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_NOT_CHAR) g_warning("cond_not_char: GOT='%c'",t); #endif return 1; } }
static int cond_one_of(tvbparse_t* tt, const int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) { guint i; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_ONEOF) g_warning("cond_one_of: START"); #endif if ( offset > tt->end_offset ) return -1; for(i=0; i < wanted->control.elems->len; i++) { tvbparse_wanted_t* w = (tvbparse_wanted_t *)g_ptr_array_index(wanted->control.elems,i); tvbparse_elem_t* new_elem = NULL; int curr_len; if ( offset + w->len > tt->end_offset ) continue; curr_len = w->condition(tt, offset, w, &new_elem); if (curr_len >= 0) { *tok = new_tok(tt, wanted->id, new_elem->offset, new_elem->len, wanted); (*tok)->sub = new_elem; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_ONEOF) g_warning("cond_one_of: GOT len=%i",curr_len); #endif return curr_len; } } return -1; }
token* number_tok(char* start, int length) { token* tok = new_tok(NUMBER_TOKEN); char* tmp = strndup(start, length); tok->number = atoi(tmp); free(tmp); return tok; }
static int cond_char (tvbparse_t* tt, const int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) { gchar c,t; guint i; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CHAR) g_warning("cond_char: control='%s'",wanted->control.str); #endif if ( offset + 1 > tt->end_offset ) return -1; t = (gchar) tvb_get_guint8(tt->tvb,offset); for(i = 0; (c = wanted->control.str[i]) && offset <= tt->end_offset; i++) { if ( c == t ) { *tok = new_tok(tt,wanted->id,offset,1,wanted); #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_CHAR) g_warning("cond_char: GOT: '%c'",c); #endif return 1; } } return -1; }
static int cond_hash(tvbparse_t* tt, const int offset, const tvbparse_wanted_t* wanted, tvbparse_elem_t** tok) { int key_len; gchar* key = NULL; tvbparse_elem_t* key_elem = NULL; tvbparse_wanted_t* value_wanted = NULL; int value_len; tvbparse_elem_t* value_elem = NULL; int tot_len; tvbparse_elem_t* ret_tok; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_HASH) g_warning("cond_hash: START"); #endif if ( offset > tt->end_offset ) return -1; key_len = wanted->control.hash.key->condition(tt, offset, wanted->control.hash.key, &key_elem); if (key_len < 0) return -1; key = tvb_get_ephemeral_string(key_elem->tvb,key_elem->offset,key_elem->len); #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_HASH) g_warning("cond_hash: got key='%s'",key); #endif if ((value_wanted = (tvbparse_wanted_t *)g_hash_table_lookup(wanted->control.hash.table,key))) { value_len = value_wanted->condition(tt, offset + key_len, value_wanted, &value_elem); } else if (wanted->control.hash.other) { value_len = wanted->control.hash.other->condition(tt, offset+key_len, wanted->control.hash.other, &value_elem); if (value_len < 0) return -1; } else { return -1; } tot_len = key_len + value_len; ret_tok = new_tok(tt, value_elem->id, offset, tot_len, wanted); ret_tok->sub = key_elem; ret_tok->sub->last->next = value_elem; *tok = ret_tok; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_HASH) g_warning("cond_hash: GOT len=%i",tot_len); #endif return tot_len; }
static int cond_seq(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) { guint i; int len = 0; int start = offset; tvbparse_elem_t* ret_tok = NULL; if ( offset > tt->end_offset ) return -1; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_SEQ) g_warning("cond_seq: START"); #endif for(i=0; i < wanted->control.elems->len; i++) { tvbparse_wanted_t* w = (tvbparse_wanted_t *)g_ptr_array_index(wanted->control.elems,i); tvbparse_elem_t* new_elem = NULL; if ( offset + w->len > tt->end_offset ) return -1; len = w->condition(tt, offset, w, &new_elem); if (len >= 0) { if (ret_tok) { ret_tok->len = (new_elem->offset - ret_tok->offset) + new_elem->len; ret_tok->sub->last->next = new_elem; ret_tok->sub->last = new_elem; } else { ret_tok = new_tok(tt, wanted->id, new_elem->offset, new_elem->len, wanted); ret_tok->sub = new_elem; new_elem->last = new_elem; } } else { return -1; } offset += len; offset += ignore_fcn(tt,offset); } *tok = ret_tok; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_SEQ) g_warning("cond_seq: GOT len=%i",offset - start); #endif return offset - start; }
static int cond_string(tvbparse_t* tt, const int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) { int len = wanted->len; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_STRING) g_warning("cond_string: control='%s'",wanted->control.str); #endif if ( offset + wanted->len > tt->end_offset ) return -1; if ( tvb_strneql(tt->tvb, offset, wanted->control.str, len) == 0 ) { *tok = new_tok(tt,wanted->id,offset,len,wanted); #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_STRING) g_warning("cond_string: GOT len=%i",len); #endif return len; } else { return -1; } }
Parser new_parser() { Parser p; p.prev_tk = new_tok(); p.empty_line = 1; p.un_op = 0; p.in_branch = 0; p.parens_closed = 1; p.cur_line = 1; p.paren_depth = 0; p.indent = 0; p.brace_indent = malloc( 10*sizeof(int) ); p.brace_indent[0] = 0; p.stack_size = 10; p.last_brace_indent = 0; return p; }
static int cond_not_chars(tvbparse_t* tt, int offset, const tvbparse_wanted_t * wanted, tvbparse_elem_t** tok) { guint length = 0; int left = tt->end_offset - offset; int start = offset; #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_NOT_CHARS) g_warning("cond_not_chars: control='%s'",wanted->control.str); #endif if ( offset + (int)wanted->min > tt->end_offset ) return -1; if (left < (int)wanted->min) return -1; left = left <= (int)wanted->max ? left : (int)wanted->max; while( left > 0 ) { gchar c; gchar t = (gchar) tvb_get_guint8(tt->tvb,offset); guint i = 0; while ( (c = wanted->control.str[i++]) ) { if (c == t) goto end_not_chars; } offset++; length++; left--; } end_not_chars: if ( length < wanted->min ) { return -1; } else { *tok = new_tok(tt,wanted->id,start,length,wanted); #ifdef TVBPARSE_DEBUG if (TVBPARSE_DEBUG & TVBPARSE_DEBUG_NOT_CHARS) g_warning("cond_not_chars: GOT len=%i",length); #endif return length; } }
int parse( Parser * parser, FileBuf * buf, FILE * out ) { if( !parser || !out ) return 0; Token cur_tk = new_tok(); for(;;) { cur_tk = get_token( buf->head ); if( cur_tk.type == NOTOKEN || cur_tk.type == EOF_TOK ) { break; } if( cur_tk.type == EOL_TOK ) { ++parser->cur_line; } if( cur_tk.type == PREPROC || cur_tk.type == MUL_COMMENT || cur_tk.type == STR_LIT ) { parser->cur_line += how_much_eols( &cur_tk ); } if( !handle_token( &cur_tk, parser, out ) ) return 0; if( cur_tk.type != EOL_TOK ) { parser->prev_tk = cur_tk; } buf->head = cur_tk.end; #ifdef DEBUG_ON check_parser( parser ); #endif // DEBUG_ON } return 1; }
return -1; } } tvbparse_wanted_t* tvbparse_handle(tvbparse_wanted_t** handle) { tvbparse_wanted_t* w = (tvbparse_wanted_t *)g_malloc0(sizeof(tvbparse_wanted_t)); w->condition = cond_handle; w->control.handle = handle; return w; } static int cond_end(tvbparse_t* tt, const int offset, const tvbparse_wanted_t * wanted _U_, tvbparse_elem_t** tok) { if (offset == tt->end_offset) { *tok = new_tok(tt,wanted->id,offset,0,wanted); return 0; } else { return -1; } } tvbparse_wanted_t* tvbparse_end_of_buffer(const int id, const void* data, tvbparse_action_t before_cb, tvbparse_action_t after_cb) { tvbparse_wanted_t* w = (tvbparse_wanted_t *)g_malloc0(sizeof(tvbparse_wanted_t)); w->id = id; w->condition = cond_end; w->after = after_cb;
token* string_tok(char* start, int length) { token* tok = new_tok(STRING_TOKEN); tok->symbol = strndup(start, length); return tok; }
token* symbol_tok(char* start, int length) { token* tok = new_tok(SYMBOL_TOKEN); tok->symbol = strndup(start, length); return tok; }