diff options
author | Anton Luka Šijanec <anton@sijanec.eu> | 2022-11-21 22:13:04 +0100 |
---|---|---|
committer | Anton Luka Šijanec <anton@sijanec.eu> | 2022-11-21 22:13:04 +0100 |
commit | a36d9a1b09a6632fd66ad67c7ba0c9692b058608 (patch) | |
tree | f8c4bb98ed1dfa04d983035a4299166eba774342 /utils/bencoding.c | |
parent | created utils/bencoding.c, fixed bencoding (for now decode only). TODO: fuzz (diff) | |
download | travnik-a36d9a1b09a6632fd66ad67c7ba0c9692b058608.tar travnik-a36d9a1b09a6632fd66ad67c7ba0c9692b058608.tar.gz travnik-a36d9a1b09a6632fd66ad67c7ba0c9692b058608.tar.bz2 travnik-a36d9a1b09a6632fd66ad67c7ba0c9692b058608.tar.lz travnik-a36d9a1b09a6632fd66ad67c7ba0c9692b058608.tar.xz travnik-a36d9a1b09a6632fd66ad67c7ba0c9692b058608.tar.zst travnik-a36d9a1b09a6632fd66ad67c7ba0c9692b058608.zip |
Diffstat (limited to 'utils/bencoding.c')
-rw-r--r-- | utils/bencoding.c | 54 |
1 files changed, 43 insertions, 11 deletions
diff --git a/utils/bencoding.c b/utils/bencoding.c index a60e1b3..ae80546 100644 --- a/utils/bencoding.c +++ b/utils/bencoding.c @@ -7,29 +7,61 @@ #include <bencoding.c> #define S0(x) (x ? x : "") int main (int argc, char ** argv) { - if (argc != 1+1) - error_at_line(1, 0, __FILE__, __LINE__, "%s encode < json || %s decode < bencoding", S0(argv[0]), S0(argv[0])); + if (argc < 1+1) + error_at_line(1, 0, __FILE__, __LINE__, "%s encode < json || %s decode < bencoding || %s path path/to/obj < bencoding || %s foreach < bencoding", S0(argv[0]), S0(argv[0]), S0(argv[0]), S0(argv[0])); + if (argv[1][0] == 'p' && argc != 1+2) + error_at_line(1, 0, __FILE__, __LINE__, "set path!"); int size = 2048; int len = 0; char * in = malloc(size); while (!feof(stdin) && !ferror(stdin)) { if (!in) error_at_line(2, 0, __FILE__, __LINE__, "heap alloc failed"); - len += fread(in, 1, size-len-1, stdin); + len += fread(in+len, 1, size-len-1, stdin); if ((size - len) < 1024) in = realloc(in, size *= 2); } if (argv[1][0] == 'e') error_at_line(3, 0, __FILE__, __LINE__, "N/I"); struct bencoding * bencoding = bdecode(in, size, terminate); - len = b2json_length(bencoding); - char out[len+1]; - char * end = b2json(out, bencoding); - *end = '\0'; - puts(out); - if (end - out != len) - error_at_line(4, 0, __FILE__, __LINE__, "b2json wrote %ld instead of %d bytes.", end-out, len); - fprintf(stderr, "len: %d\n", len); + if (argv[1][0] == 'd') { + len = b2json_length(bencoding); + char out[len+1]; + char * end = b2json(out, bencoding); + *end = '\0'; + puts(out); + if (end - out != len) + error_at_line(4, 0, __FILE__, __LINE__, "b2json wrote %ld instead of %d bytes.", end-out, len); + fprintf(stderr, "len: %d\n", len); + } + if (argv[1][0] == 'p') { + len = b2json_length(bpath(bencoding, argv[2])); + char out[len+1]; + char * end = b2json(out, bpath(bencoding, argv[2])); + *end = '\0'; + puts(out); + if (end - out != len) + error_at_line(4, 0, __FILE__, __LINE__, "b2json wrote %ld instead of %d bytes.", end-out, len); + fprintf(stderr, "len: %d\n", len); + } + if (argv[1][0] == 'f') { + bforeach (value, bencoding) { + len = b2json_length(value->key); + char out[len+1]; + char * end = b2json(out, value->key); + *end = '\0'; + if (end - out != len) + error_at_line(4, 0, __FILE__, __LINE__, "b2json wrote %ld instead of %d bytes.", end-out, len); + printf("key(%d): %s\n", len, out); + len = b2json_length(value); + char out2[len+1]; + end = b2json(out2, value); + *end = '\0'; + if (end - out2 != len) + error_at_line(4, 0, __FILE__, __LINE__, "b2json wrote %ld instead of %d bytes.", end-out2, len); + printf("value(%d): %s\n", len, out2); + } + } free_bencoding(bencoding); free(in); return 0; |