tpws: check broken gzip and zlib errors

This commit is contained in:
bolvan
2019-05-09 22:32:41 +03:00
parent 8141625e01
commit f8dd9f3d23
11 changed files with 24 additions and 3 deletions

View File

@@ -73,3 +73,9 @@ zerr:
}
return r;
}
bool is_gzip(FILE* F)
{
unsigned char magic[2];
return !fseek(F,0,SEEK_SET) && fread(magic, 1, 2, F)==2 && magic[0]==0x1F && magic[1]==0x8B;
}

View File

@@ -2,5 +2,7 @@
#include <stdio.h>
#include <zlib.h>
#include <stdbool.h>
int z_readfile(FILE *F,char **buf,size_t *size);
bool is_gzip(FILE* F);

View File

@@ -28,6 +28,7 @@ bool LoadHostList(strpool **hostlist, char *filename)
size_t zsize;
int ct = 0;
FILE *F;
int r;
if (*hostlist)
{
@@ -40,10 +41,10 @@ bool LoadHostList(strpool **hostlist, char *filename)
fprintf(stderr, "Could not open %s\n", filename);
return false;
}
if (z_readfile(F,&zbuf,&zsize)==Z_OK)
if ((r=z_readfile(F,&zbuf,&zsize))==Z_OK)
{
printf("libz compression detected. uncompressed size : %zu\n", zsize);
printf("zlib compression detected. uncompressed size : %zu\n", zsize);
fclose(F);
p = zbuf;
@@ -60,10 +61,22 @@ bool LoadHostList(strpool **hostlist, char *filename)
}
free(zbuf);
}
else if (r!=Z_DATA_ERROR)
{
fprintf(stderr, "zlib decompression failed : result %d\n",r);
return false;
}
else
{
if (is_gzip(F))
{
fprintf(stderr, "hostlist is gzip but is broken : %s\n",filename);
return false;
}
fseek(F,0,SEEK_SET);
printf("loading plain text list\n",r);
while (fgets(s, 256, F))
{
p = s;