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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -73,3 +73,9 @@ zerr:
} }
return r; 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 <stdio.h>
#include <zlib.h> #include <zlib.h>
#include <stdbool.h>
int z_readfile(FILE *F,char **buf,size_t *size); 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; size_t zsize;
int ct = 0; int ct = 0;
FILE *F; FILE *F;
int r;
if (*hostlist) if (*hostlist)
{ {
@ -40,10 +41,10 @@ bool LoadHostList(strpool **hostlist, char *filename)
fprintf(stderr, "Could not open %s\n", filename); fprintf(stderr, "Could not open %s\n", filename);
return false; 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); fclose(F);
p = zbuf; p = zbuf;
@ -60,9 +61,21 @@ bool LoadHostList(strpool **hostlist, char *filename)
} }
free(zbuf); free(zbuf);
} }
else if (r!=Z_DATA_ERROR)
{
fprintf(stderr, "zlib decompression failed : result %d\n",r);
return false;
}
else else
{ {
if (is_gzip(F))
{
fprintf(stderr, "hostlist is gzip but is broken : %s\n",filename);
return false;
}
fseek(F,0,SEEK_SET); fseek(F,0,SEEK_SET);
printf("loading plain text list\n",r);
while (fgets(s, 256, F)) while (fgets(s, 256, F))
{ {