From a8d503137e35575090a06e0eed219c78feb8d3a4 Mon Sep 17 00:00:00 2001 From: fumiama Date: Tue, 18 May 2021 23:09:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=9E=E9=99=85=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 8196 bytes protobuf.c | 5 ++++- simple_protobuf.h | 2 +- test.c | 1 + 4 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9729ef4b379de740b646207fa03647140f4c39ef GIT binary patch literal 8196 zcmeHMTWl0n7(QPqFvAY*6j0h_!G&_w2D;D^E>V_tsl}GGuq`bYS!Q=em@u8GGqYQu z5*rim(fFW|U`#|2V$hhNpe8Zxp;UxDm4qOF#`OG+Bo!Pudw>kHPHL4>9A_neB2Kf6SNf*a-h$8}nR|i!=3P8wH zfS^#D@_@i68Dcub5rKg##T3~CLRW+-2827=lYBT~I>Zry!kt05GlVlE%uo=_PJYQ? zI73=sR7VU%3`}HzKR%OLo)wwNtTFlf_57e^dVXW$B~(nRoIGW!Bu|UUv8q_?@$7zQ zDC=u}(QDD%oSMj*x{=r1K2!Ji_Zapj%P1Lkm)oiNhP#Uo?r|K828gzn-)PSO!IF)Kz(~w-B2}nU5W-HvDSFgyBvR=oTVr`iEZ0ApD+!#cZhG> z4$UhVwr<+{`bwr}<}IVg)&>mqk}L;KwRQ1$e=%=9Sv37aYP}={y>sWqmEAf`w8hbR zFkL+($)hvWnrdZUe4#Au;^JrYyT-1wzu4aZra{4URADw2VlkFrDNgu@yU!gN8f` zu;9W+5hXm0=kX$5!pnFaZ{r;t$GbR<5AhMc#07khAMq0|ViZ^LD}Ki{af20QA#W+K zL?Heo94qAT!(7E{w-$1u?a{XO&3CboFINfm-gp1(F~=&USt4<(lZADSvQ8%XtChm~u(ClSm8(;PwMEIu^c3NTUO!&mkPb zVLXLr@f=kct6NyMq;`TwTdfB(NjvqfPd1|kOTMg~xs$!1#U&TjX<<+*l} zuDx{e!pDsW3|y!R(s9Bd9Vfi;hat6-G?e?KLmUxEJyibp4*`db==_h)|3G{9a`i8k CG%Cmd literal 0 HcmV?d00001 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