mirror of https://github.com/bol-van/zapret/
30 changed files with 364 additions and 121 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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,75 @@ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include "gzip.h" |
|||
|
|||
#define ZCHUNK 16384 |
|||
#define BUFMIN 128 |
|||
#define BUFCHUNK (1024*128) |
|||
|
|||
int z_readfile(FILE *F,char **buf,size_t *size) |
|||
{ |
|||
z_stream zs; |
|||
int r; |
|||
unsigned char in[ZCHUNK]; |
|||
size_t bufsize; |
|||
void *newbuf; |
|||
|
|||
memset(&zs,0,sizeof(zs)); |
|||
|
|||
*buf = NULL; |
|||
bufsize=*size=0; |
|||
|
|||
r=inflateInit2(&zs,47); |
|||
if (r != Z_OK) return r; |
|||
|
|||
do |
|||
{ |
|||
zs.avail_in = fread(in, 1, sizeof(in), F); |
|||
if (ferror(F)) |
|||
{ |
|||
r = Z_ERRNO; |
|||
goto zerr; |
|||
} |
|||
if (!zs.avail_in) break; |
|||
zs.next_in = in; |
|||
do |
|||
{ |
|||
if ((bufsize-*size)<BUFMIN) |
|||
{ |
|||
bufsize += BUFCHUNK; |
|||
newbuf = *buf ? realloc(*buf,bufsize) : malloc(bufsize); |
|||
if (newbuf==*buf) |
|||
if (!newbuf) |
|||
{ |
|||
r = Z_MEM_ERROR; |
|||
goto zerr; |
|||
} |
|||
*buf = newbuf; |
|||
} |
|||
zs.avail_out = bufsize - *size; |
|||
zs.next_out = (unsigned char*)(*buf + *size); |
|||
r = inflate(&zs, Z_NO_FLUSH); |
|||
if (r!=Z_OK && r!=Z_STREAM_END) goto zerr; |
|||
*size = bufsize - zs.avail_out; |
|||
} while (r==Z_OK && zs.avail_in); |
|||
} while (r==Z_OK); |
|||
|
|||
if (*size<bufsize) |
|||
{ |
|||
// free extra space
|
|||
if (newbuf = realloc(*buf,*size)) *buf=newbuf; |
|||
} |
|||
|
|||
inflateEnd(&zs); |
|||
return Z_OK; |
|||
|
|||
zerr: |
|||
inflateEnd(&zs); |
|||
if (*buf) |
|||
{ |
|||
free(*buf); |
|||
*buf = NULL; |
|||
} |
|||
return r; |
|||
} |
@ -0,0 +1,6 @@ |
|||
#pragma once |
|||
|
|||
#include <stdio.h> |
|||
#include <zlib.h> |
|||
|
|||
int z_readfile(FILE *F,char **buf,size_t *size); |
@ -0,0 +1,82 @@ |
|||
#include <stdio.h> |
|||
#include "hostlist.h" |
|||
#include "gzip.h" |
|||
|
|||
|
|||
static bool addpool(strpool **hostlist, char **s, char *end) |
|||
{ |
|||
char *p; |
|||
|
|||
// advance until eol lowering all chars
|
|||
for (p = *s; p<end && *p && *p!='\r' && *p != '\n'; p++) *p=tolower(*p); |
|||
if (!StrPoolAddStrLen(hostlist, *s, p-*s)) |
|||
{ |
|||
StrPoolDestroy(hostlist); |
|||
*hostlist = NULL; |
|||
return false; |
|||
} |
|||
// advance to the next line
|
|||
for (; p<end && (!*p || *p=='\r' || *p=='\n') ; p++); |
|||
*s = p; |
|||
return true; |
|||
} |
|||
|
|||
|
|||
bool LoadHostList(strpool **hostlist, char *filename) |
|||
{ |
|||
char *p, *e, s[256], *zbuf; |
|||
size_t zsize; |
|||
int ct = 0; |
|||
FILE *F; |
|||
|
|||
if (*hostlist) |
|||
{ |
|||
StrPoolDestroy(hostlist); |
|||
*hostlist = NULL; |
|||
} |
|||
|
|||
if (!(F = fopen(filename, "rb"))) |
|||
{ |
|||
fprintf(stderr, "Could not open %s\n", filename); |
|||
return false; |
|||
} |
|||
if (z_readfile(F,&zbuf,&zsize)==Z_OK) |
|||
{ |
|||
|
|||
printf("libz compression detected. uncompressed size : %zu\n", zsize); |
|||
fclose(F); |
|||
|
|||
p = zbuf; |
|||
e = zbuf + zsize; |
|||
while(p<e) |
|||
{ |
|||
if (!addpool(hostlist,&p,e)) |
|||
{ |
|||
fprintf(stderr, "Not enough memory to store host list : %s\n", filename); |
|||
free(zbuf); |
|||
return false; |
|||
} |
|||
ct++; |
|||
} |
|||
free(zbuf); |
|||
} |
|||
else |
|||
{ |
|||
fseek(F,0,SEEK_SET); |
|||
|
|||
while (fgets(s, 256, F)) |
|||
{ |
|||
p = s; |
|||
if (!addpool(hostlist,&p,p+strlen(p))) |
|||
{ |
|||
fprintf(stderr, "Not enough memory to store host list : %s\n", filename); |
|||
fclose(F); |
|||
return false; |
|||
} |
|||
ct++; |
|||
} |
|||
fclose(F); |
|||
} |
|||
printf("Loaded %d hosts from %s\n", ct, filename); |
|||
return true; |
|||
} |
@ -0,0 +1,5 @@ |
|||
#pragma once |
|||
|
|||
#include "strpool.h" |
|||
|
|||
bool LoadHostList(strpool **hostlist, char *filename); |
Loading…
Reference in new issue