diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..9729ef4 Binary files /dev/null and b/.DS_Store differ diff --git a/protobuf.c b/protobuf.c index f76e232..061c6d3 100644 --- a/protobuf.c +++ b/protobuf.c @@ -33,11 +33,13 @@ static int write_num(FILE* fp, uint32_t n) { } SIMPLE_PB* get_pb(FILE* fp) { + uint32_t init_pos = ftell(fp); uint32_t struct_len = read_num(fp); if(struct_len > 1) { SIMPLE_PB* spb = malloc(struct_len + sizeof(uint32_t)); if(spb) { - spb->len = struct_len; + spb->struct_len = struct_len; + spb->real_len = 0; char* p = spb->target; char* end = p + struct_len; memset(p, 0, struct_len); @@ -47,6 +49,7 @@ SIMPLE_PB* get_pb(FILE* fp) { fread(p, data_len, 1, fp); p += offset; } + spb->real_len = ftell(fp) - init_pos; return spb; } } diff --git a/simple_protobuf.h b/simple_protobuf.h index cab24b0..f730b4c 100644 --- a/simple_protobuf.h +++ b/simple_protobuf.h @@ -4,7 +4,7 @@ #include struct SIMPLE_PB { - uint32_t len; + uint32_t struct_len, real_len; char target[]; }; typedef struct SIMPLE_PB SIMPLE_PB; diff --git a/test.c b/test.c index 284ae51..13eeeb0 100644 --- a/test.c +++ b/test.c @@ -34,6 +34,7 @@ int main() { SIMPLE_PB* spb = get_pb(fp); memcpy(&t, spb->target, sizeof(struct TEST)); printf("a:%u\nb:%u\nc:%u\nd:%llu\ne:%s\n", t.a, t.b, t.c, t.d, t.e); + printf("Struct size: %u, read: %u\n", spb->struct_len, spb->real_len); } else perror("[SPB]"); } else perror("[SPB]"); } \ No newline at end of file