Compare commits

...

10 commits

Author SHA1 Message Date
Agorise
1472282e31
Update LICENSE 2019-01-01 10:37:37 -06:00
Agorise
dceaa02082
Update LICENSE 2018-06-17 16:31:40 +03:00
Agorise
66bb509071
Update LICENSE 2017-11-15 13:21:57 +02:00
Agorise
308e520537 Update LICENSE 2017-10-16 13:59:38 +03:00
John Jones
d690e2093d minor change to makefile 2017-05-11 19:51:22 +00:00
John Jones
b1845aad5f compiler switch for c99 2017-03-21 14:23:31 +00:00
ken Code
19e32f9416 Update LICENSE 2017-01-31 17:10:42 +01:00
jmjatlanta
3b49ac2c5d Better allocation of memory for strings with zero bytes 2016-12-23 20:10:52 -05:00
jmjatlanta
8d966178f4 minor change to aide in reverse engineering protobufs 2016-12-22 13:40:11 -05:00
jmjatlanta
5eeae56378 Fixed bug with field type and added program to disassemble a protobuf 2016-12-22 13:29:09 -05:00
6 changed files with 149 additions and 21 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@
*.o
.settings/language.settings.xml
test_protobuf
protobuf_reader

View file

@ -1,6 +1,9 @@
MIT License
Copyright (c) 2016 BitShares Munich IVS
Copyright (c) 2019 AGORISE, LTD.
An International Business Company, Cyprus Reg# ΗΕ375959
Contains works from BitShares Munich IVS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -1,20 +1,24 @@
CC = gcc
CFLAGS = -O0 -g3 -Wall
CFLAGS = -O0 -g3 -Wall -std=c99
LFLAGS =
DEPS = protobuf.h
OBJS = protobuf.o varint.o
all: protobuf_reader
cd test; make all;
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
protobuf_reader: $(OBJS) main.o
$(CC) -o $@ $^
test_protobuf: $(OBJS)
$(CC) -o $@ $^ $(LFLAGS)
all: $(OBJS)
cd test; make all;
clean:
rm -f *.o
rm -f protobuf_reader;
cd test; make clean;
rebuild: clean all
rebuild: clean all

120
main.c Normal file
View file

@ -0,0 +1,120 @@
#include <string.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "protobuf.h"
/**
* opens a file to give a list of fields
*/
size_t os_utils_file_size(const char* path) {
size_t file_size = 0;
// open file
FILE* in_file = fopen(path, "r");
if (in_file == NULL)
return 0;
// determine size
fseek(in_file, 0L, SEEK_END);
file_size = ftell(in_file);
fclose(in_file);
return file_size;
}
char* getWireType(enum WireType type) {
switch(type) {
case(WIRETYPE_VARINT):
return "Varint";
case (WIRETYPE_LENGTH_DELIMITED):
return "Length Delimited";
case (WIRETYPE_64BIT):
return "64 Bit";
case (WIRETYPE_START_GROUP):
return "Start Group";
case (WIRETYPE_END_GROUP):
return "End Group";
case (WIRETYPE_32BIT):
return "32 Bit";
}
return "Wire Type Invalid";
}
void stripit(int argc, char** argv) {
char tmp[strlen(argv[argc])];
strcpy(tmp, &argv[argc][1]);
tmp[strlen(tmp)-1] = 0;
strcpy(argv[argc], tmp);
return;
}
void strip_quotes(int argc, char** argv) {
for(int i = 0; i < argc; i++) {
if (argv[i][0] == '\'' && argv[i][strlen(argv[i])-1] == '\'') {
stripit(i, argv);
}
}
}
int main(int argc, char** argv) {
strip_quotes(argc, argv);
if (argc <= 1) {
printf("Syntax: %s [filename] [start_pos]\n", argv[0]);
return 0;
}
char* fileName = argv[1];
size_t numBytes = os_utils_file_size(fileName);
printf("Number of bytes in file: %lu\n", numBytes);
if (numBytes == 0)
return 0;
FILE* file = fopen(fileName, "rb");
unsigned char buffer[numBytes];
if (fread(buffer, 1, numBytes, file) != numBytes) {
printf("Unexpected number of bytes.\n");
return 0;
}
//skip ahead
size_t skip = 0;
if (argc > 2) {
skip = atoi(argv[2]);
if (skip > 0) {
printf("Skipping first %lu bytes\n", skip);
}
}
int fieldCounter = 0;
size_t pos = skip;
while(pos < numBytes) {
size_t bytes_read = 0;
int field_no = 0;
enum WireType wire_type = WIRETYPE_64BIT;
if (protobuf_decode_field_and_type(&buffer[pos], numBytes - pos, &field_no, &wire_type, &bytes_read) == 0) {
printf("Unexpected return value from protobuf_decode_field_and_type at pos %lu\n", pos);
return 0;
}
printf("Field counter: %d at position %lu, Field Number: %d, Wire Type: %s", fieldCounter, pos, field_no, getWireType(wire_type));
pos += bytes_read;
// read the value
switch(wire_type) {
case(WIRETYPE_VARINT): {
unsigned long long varint = 0;
protobuf_decode_varint(&buffer[pos], numBytes-pos, &varint, &bytes_read);
pos += bytes_read;
printf(" Value: %llu. Next read position at %lu\n", varint, pos);
break;
}
case (WIRETYPE_LENGTH_DELIMITED): {
unsigned long long varint = 0;
protobuf_decode_varint(&buffer[pos], numBytes-pos, &varint, &bytes_read);
pos += bytes_read;
printf(" Field width: %llu. Next read position at %lu\n", varint, pos);
pos += varint;
break;
}
default:
pos++;
}
fieldCounter++;
}
return 0;
}

View file

@ -116,21 +116,21 @@ int protobuf_decode_string(const unsigned char* buffer, size_t buffer_length, ch
// grab the field size
int length = varint_decode(&buffer[pos], buffer_length - pos, bytes_read);
pos += *bytes_read;
*bytes_read += length;
// allocate memory
*results = malloc(sizeof(char) * length + 1);
if ((*results) == NULL)
return 0;
// allocate memory (if neccesary)
if (length > 0) {
*results = malloc(sizeof(char) * length + 1);
if ((*results) == NULL)
return 0;
memset(*results, 0, length+1);
// copy the string
memcpy((*results), &buffer[pos], length);
// don't forget the null
(*results)[length] = 0;
pos += length;
*bytes_read = pos;
memset(*results, 0, length+1);
// copy the string
memcpy((*results), &buffer[pos], length);
// don't forget the null
(*results)[length] = 0;
}
return 1;
}
@ -146,7 +146,7 @@ int protobuf_decode_field_and_type(const unsigned char* buffer, int buffer_lengt
*bytes_read = 0;
unsigned long long field = varint_decode(buffer, buffer_length, bytes_read);
*field_no = field >> 3;
*field_type = field & *field_no;
*field_type = field - (*field_no << 3);
return 1;
}

View file

@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -O0 -g3 -Wall -I../
CFLAGS = -O0 -g3 -Wall -I../ -std=c99
LFLAGS =
DEPS = ../protobuf.h
OBJS = testit.o ../protobuf.o ../varint.o Test1_protobuf.o Test2_protobuf.o Test3_protobuf.o
@ -16,4 +16,4 @@ clean:
rm -f *.o
rm -f test_protobuf
rebuild: clean all
rebuild: clean all