adding pesco's base64 example
This commit is contained in:
parent
44440c8347
commit
a68b93c618
3 changed files with 66 additions and 3 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,4 +4,5 @@
|
||||||
src/test_suite
|
src/test_suite
|
||||||
lib/hush
|
lib/hush
|
||||||
examples/dns
|
examples/dns
|
||||||
|
examples/base64
|
||||||
TAGS
|
TAGS
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
|
|
||||||
OUTPUTS := dns.o \
|
OUTPUTS := dns.o \
|
||||||
dns
|
dns \
|
||||||
|
base64.o \
|
||||||
|
base64
|
||||||
|
|
||||||
TOPLEVEL := ../
|
TOPLEVEL := ../
|
||||||
|
|
||||||
include ../common.mk
|
include ../common.mk
|
||||||
|
|
||||||
|
|
||||||
all: dns
|
all: dns base64
|
||||||
|
|
||||||
dns: LDFLAGS:=-L../src -lhammer $(LDFLAGS)
|
dns: LDFLAGS:=-L../src -lhammer $(LDFLAGS)
|
||||||
dns: dns.o rr.o dns_common.o
|
dns: dns.o rr.o dns_common.o
|
||||||
|
|
@ -18,3 +20,9 @@ dns.o: ../src/hammer.h dns_common.h
|
||||||
rr.o: ../src/hammer.h rr.h dns_common.h
|
rr.o: ../src/hammer.h rr.h dns_common.h
|
||||||
|
|
||||||
dns_common.o: ../src/hammer.h dns_common.h
|
dns_common.o: ../src/hammer.h dns_common.h
|
||||||
|
|
||||||
|
base64: LDFLAGS:=-L../src -lhammer $(LDFLAGS)
|
||||||
|
base64: base64.o
|
||||||
|
$(call hush, "Linking $@") $(CC) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
|
base64.o: ../src/hammer.h
|
||||||
|
|
|
||||||
54
examples/base64.c
Normal file
54
examples/base64.c
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
#include "../src/hammer.h"
|
||||||
|
|
||||||
|
const HParser* document = NULL;
|
||||||
|
|
||||||
|
void init_parser(void)
|
||||||
|
{
|
||||||
|
// CORE
|
||||||
|
const HParser *digit = h_ch_range(0x30, 0x39);
|
||||||
|
const HParser *alpha = h_choice(h_ch_range(0x41, 0x5a), h_ch_range(0x61, 0x7a), NULL);
|
||||||
|
|
||||||
|
// AUX.
|
||||||
|
const HParser *plus = h_ch('+');
|
||||||
|
const HParser *slash = h_ch('/');
|
||||||
|
const HParser *equals = h_ch('=');
|
||||||
|
|
||||||
|
const HParser *bsfdig = h_choice(alpha, digit, plus, slash, NULL);
|
||||||
|
const HParser *bsfdig_4bit = h_choice(
|
||||||
|
h_ch('A'), h_ch('E'), h_ch('I'), h_ch('M'), h_ch('Q'), h_ch('U'),
|
||||||
|
h_ch('Y'), h_ch('c'), h_ch('g'), h_ch('k'), h_ch('o'), h_ch('s'),
|
||||||
|
h_ch('w'), h_ch('0'), h_ch('4'), h_ch('8'), NULL);
|
||||||
|
const HParser *bsfdig_2bit = h_choice(h_ch('A'), h_ch('Q'), h_ch('g'), h_ch('w'), NULL);
|
||||||
|
const HParser *base64_2 = h_sequence(bsfdig, bsfdig, bsfdig_4bit, equals, NULL);
|
||||||
|
const HParser *base64_1 = h_sequence(bsfdig, bsfdig_2bit, equals, equals, NULL);
|
||||||
|
const HParser *base64 = h_choice(base64_2, base64_1, NULL);
|
||||||
|
// why does this parse "A=="?!
|
||||||
|
// why does this parse "aaA=" but not "aA=="?!
|
||||||
|
|
||||||
|
document = base64;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
uint8_t input[102400];
|
||||||
|
size_t inputsize;
|
||||||
|
const HParseResult *result;
|
||||||
|
|
||||||
|
init_parser();
|
||||||
|
|
||||||
|
inputsize = fread(input, 1, sizeof(input), stdin);
|
||||||
|
fprintf(stderr, "inputsize=%lu\ninput=", inputsize);
|
||||||
|
fwrite(input, 1, inputsize, stderr);
|
||||||
|
result = h_parse(document, input, inputsize);
|
||||||
|
|
||||||
|
if(result) {
|
||||||
|
fprintf(stderr, "parsed=%lld bytes\n", result->bit_length/8);
|
||||||
|
h_pprint(stdout, result->ast, 0, 0);
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue