Compare commits
10 commits
391bfce39b
...
1472282e31
Author | SHA1 | Date | |
---|---|---|---|
|
1472282e31 | ||
|
dceaa02082 | ||
|
66bb509071 | ||
|
308e520537 | ||
|
d690e2093d | ||
|
b1845aad5f | ||
|
19e32f9416 | ||
|
3b49ac2c5d | ||
|
8d966178f4 | ||
|
5eeae56378 |
6 changed files with 149 additions and 21 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -7,3 +7,4 @@
|
|||
*.o
|
||||
.settings/language.settings.xml
|
||||
test_protobuf
|
||||
protobuf_reader
|
||||
|
|
5
LICENSE
5
LICENSE
|
@ -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
|
||||
|
|
14
Makefile
14
Makefile
|
@ -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
120
main.c
Normal 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;
|
||||
}
|
26
protobuf.c
26
protobuf.c
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue