Compare commits

..

No commits in common. "1472282e313d65afd2a3884bcab9df7c39928c05" and "391bfce39b19b3f46967708e300d263053a9e54d" have entirely different histories.

6 changed files with 21 additions and 149 deletions

1
.gitignore vendored
View file

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

View file

@ -1,9 +1,6 @@
MIT License
Copyright (c) 2019 AGORISE, LTD.
An International Business Company, Cyprus Reg# ΗΕ375959
Contains works from BitShares Munich IVS
Copyright (c) 2016 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,24 +1,20 @@
CC = gcc
CFLAGS = -O0 -g3 -Wall -std=c99
CFLAGS = -O0 -g3 -Wall
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

120
main.c
View file

@ -1,120 +0,0 @@
#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,10 +116,8 @@ 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 (if neccesary)
if (length > 0) {
// allocate memory
*results = malloc(sizeof(char) * length + 1);
if ((*results) == NULL)
return 0;
@ -130,7 +128,9 @@ int protobuf_decode_string(const unsigned char* buffer, size_t buffer_length, ch
memcpy((*results), &buffer[pos], length);
// don't forget the null
(*results)[length] = 0;
}
pos += length;
*bytes_read = pos;
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 << 3);
*field_type = field & *field_no;
return 1;
}

View file

@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -O0 -g3 -Wall -I../ -std=c99
CFLAGS = -O0 -g3 -Wall -I../
LFLAGS =
DEPS = ../protobuf.h
OBJS = testit.o ../protobuf.o ../varint.o Test1_protobuf.o Test2_protobuf.o Test3_protobuf.o