#include #include #include #include #include "pbc.h" static void read_file(const char *filename , struct pbc_slice *slice) { FILE *f = fopen(filename, "rb"); if (f == NULL) { fprintf(stderr, "Can't open file %s\n", filename); exit(1); } fseek(f,0,SEEK_END); slice->len = ftell(f); fseek(f,0,SEEK_SET); slice->buffer = malloc(slice->len); fread(slice->buffer, 1 , slice->len , f); fclose(f); } static void dump_bytes(const char *data, size_t len) { size_t i; for (i = 0; i < len; i++) if (i == 0) fprintf(stdout, "%02x", 0xff & data[i]); else fprintf(stdout, " %02x", 0xff & data[i]); } static void dump_message(struct pbc_rmessage *m, int level); static void dump_value(struct pbc_rmessage *m, const char *key, int type, int idx, int level) { int i; for (i=0;i= data->len) { data->len *= 2; data->buffer = realloc(data->buffer, data->len); } ((uint8_t *)data->buffer)[idx] = (uint8_t)byte; } static void read_stdin(int mode, struct pbc_slice *data) { data->len = 128; data->buffer = malloc(data->len); int idx = 0; while(!feof(stdin)) { int byte; int r = scanf("%d" , &byte); if (r == 0) { break; } push_byte(byte, data, idx); ++idx; } data->len = idx; } static void usage(const char *argv0) { printf(" -h help.\n" " -p protobuf file\n" " -m \n" " -d \n" " -D input from stdin (DEC number)\n" ); } int main(int argc , char * argv[]) { int ch; const char * proto = NULL; const char * message = NULL; const char * datafile = NULL; int mode = 0; while ((ch = getopt(argc, argv, "hDp:m:d:")) != -1) { switch(ch) { case 'h': usage(argv[0]); return 0; case 'p': proto = optarg; break; case 'm': message = optarg; break; case 'd': datafile = optarg; break; case 'D': mode = 10; break; default: usage(argv[0]); return 1; } } if (proto == NULL || message == NULL) { usage(argv[0]); return 1; } struct pbc_slice data; if (datafile == NULL) { read_stdin(mode, &data); } else { read_file(datafile , &data); } dump(proto , message , &data); return 0; }