Skip to content

Instantly share code, notes, and snippets.

@mrdomino
Created January 3, 2015 23:17
Show Gist options
  • Save mrdomino/c6bc91a7cb3b9817edb5 to your computer and use it in GitHub Desktop.
Save mrdomino/c6bc91a7cb3b9817edb5 to your computer and use it in GitHub Desktop.
weird endianness
#include <hammer/glue.h>
#include <hammer/hammer.h>
#include <stdlib.h>
#include <stdio.h>
int main() {
HParseResult* p;
const uint8_t *input = (const uint8_t*)"\x69\x5A\x6A\x7A\x8A\x9A";
#define MY_ENDIAN (BIT_BIG_ENDIAN | BYTE_LITTLE_ENDIAN)
H_RULE(nibble, h_with_endianness(MY_ENDIAN, h_bits(4, false)));
H_RULE(sample, h_with_endianness(MY_ENDIAN, h_bits(10, false)));
#undef MY_ENDIAN
H_RULE(samples, h_sequence(h_repeat_n(sample, 3), h_ignore(h_bits(2, false)), NULL));
H_RULE(header_ok, h_sequence(nibble, nibble, NULL));
H_RULE(header_weird, h_sequence(nibble, nibble, nibble, NULL));
H_RULE(parser_ok, h_sequence(header_ok, samples, NULL));
H_RULE(parser_weird, h_sequence(header_weird, samples, NULL));
p = h_parse(parser_weird, input, 6);
fprintf(stderr, "length:%zu\n", p->bit_length); /* 36 ?! */
h_parse_result_free(p);
p = h_parse(parser_ok, input, 6);
fprintf(stderr, "length:%zu\n", p->bit_length); /* 40 */
h_parse_result_free(p);
return 0;
}
@mrdomino
Copy link
Author

mrdomino commented Jan 3, 2015

This isn't obvious since I reordered the parses from the rules to rule out state corruption, but parser_weird ought to be 4 bits longer, not shorter, than parser_ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment