summaryrefslogblamecommitdiffstats
path: root/utils/bencoding.c
blob: ad6ded0983f92d595ac5b677880add4e6277c46b (plain) (tree)
1
2
3
4
5
6
7
8
9








                                   
                       
                                                                                                                                                                                                                                                                                                                                                                                                                                         

                                                                     





                                                                                     
                                                           


                                                    
                                                            






                                                    
                                                                                                                       








                                                                    
                                                                                                                       

                                                  
                                
                                                                     
                                
                                                                          


                                     
                                                                                                                       

                                                  
                                
                                             




                                                             
                                                                                                                               





                                                          
                                                                                                                                


                                                             














                                                                                                    
                                                                                                                       























                                                                                



                                  
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <error.h>
#include <signal.h>
#include <cjson/cJSON.h>
#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 || %s path path/to/obj < bencoding || %s foreach < bencoding || %s val value < bencoding (should output null or value as JSON string if found in array) || %s insert bencoding [replace] < bencoding || %s compare bencoding < bencoding", S0(argv[0]), S0(argv[0]), S0(argv[0]), 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+len, 1, size-len-1, stdin);
		if ((size - len) < 1024)
			in = realloc(in, size *= 2);
	}
	struct bencoding * bencoding = bdecode(in, size, 0);
	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 %td 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 %td instead of %d bytes.", end-out, len);
		fprintf(stderr, "len: %d\n", len);
	}
	if (argv[1][0] == 'v') {
		len = b2json_length(bval(bencoding, bstrs(argv[2])));
		char out[len+1];
		char * end = b2json(out, bval(bencoding, bstrs(argv[2])));
		*end = '\0';
		puts(out);
		if (end - out != len)
			error_at_line(4, 0, __FILE__, __LINE__, "b2json wrote %td instead of %d bytes.", end-out, len);
		fprintf(stderr, "len: %d\n", len);
	}
	if (argv[1][0] == 'f') {
		bforeach (bencoding, value) {
			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 %td 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 %td instead of %d bytes.", end-out2, len);
			printf("value(%d): %s\n", len, out2);
		}
	}
	if (argv[1][0] == 'i') {
		if (argc < 1+2)
			error_at_line(5, 0, __FILE__, __LINE__, "missing element argument");
		struct bencoding * elem = bdecode(argv[2], -2, argv[3] ? replace : 0);
		if (!elem)
			error_at_line(4, 0, __FILE__, __LINE__, "bdecode of element returned NULL");
		binsert(bencoding, elem->child);
		elem->child = NULL;
		free_bencoding(elem);
		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 %td instead of %d bytes.", end-out, len);
		fprintf(stderr, "len: %d\n", len);
	}
	if (argv[1][0] == 'c') {
		struct bencoding * elem = bdecode(argv[2], -2, 0);
		switch (bcompare(bencoding, elem)) {
			case -1:
				fprintf(stdout, "stdin is less than argv\n");
				break;
			case 0:
				fprintf(stdout, "stdin is same as argv\n");
				break;
			case 1:
				fprintf(stdout, "stdin is greater than argv\n");
				break;
		}
		free_bencoding(elem);
	}
	if (argv[1][0] == 'e') {
		int len = bencode_length(bencoding);
		char bencoded[len+1];
		bencoded[len] = '\0';
		bencode(bencoded, bencoding);
		puts(bencoded);
	}
	free_bencoding(bencoding);
	free(in);
	return 0;
}