From 792780a4e65faaa40075b682df49ab1bacc9a4f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Mon, 4 Apr 2022 15:49:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- protobuf.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/protobuf.c b/protobuf.c index fd64d2c..b4d18a1 100644 --- a/protobuf.c +++ b/protobuf.c @@ -36,25 +36,19 @@ 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 + 2 * sizeof(uint32_t)); - if(spb) { - spb->struct_len = struct_len; - spb->real_len = 0; - char* p = spb->target; - char* end = p + struct_len; - memset(p, 0, struct_len); - while(p < end) { - uint32_t offset = read_num(fp); - uint32_t data_len = read_num(fp); - if(data_len > 0) fread(p, data_len, 1, fp); - p += offset; - } - spb->real_len = ftell(fp) - init_pos; - return spb; - } + if(struct_len <= 1) return NULL; + SIMPLE_PB* spb = malloc(struct_len + 2 * sizeof(uint32_t)); + if(!spb) return NULL; + spb->struct_len = struct_len; + memset(spb->target, 0, struct_len); + uint32_t offset, data_len; + for(char* p = spb->target; p < spb->target+struct_len; p += offset) { + offset = read_num(fp); + data_len = read_num(fp); + if(data_len > 0) fread(p, data_len, 1, fp); } - return NULL; + spb->real_len = ftell(fp) - init_pos; + return spb; } int set_pb(FILE* fp, uint32_t* items_len, uint32_t struct_len, void* target) {