| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <unistd.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <string.h> |
| 8 |
#ifdef _WIN32 |
| 9 |
#include <winsock.h> |
| 10 |
#endif |
| 11 |
#include <zlib.h> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
void *prepare_sock_gz_r(FILE *sockr); |
| 16 |
int z_getc(void *v); |
| 17 |
char *z_gets(void *v, char *line, size_t len); |
| 18 |
char *z_read_sock(void *v); |
| 19 |
int close_sock_gz_r(void *v); |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#define ZBSIZE 100000 |
| 24 |
typedef struct { |
| 25 |
z_stream stream; |
| 26 |
char z_buffer[ZBSIZE]; |
| 27 |
char text_buffer[4 * ZBSIZE]; |
| 28 |
char *pos, *endbuf; |
| 29 |
#ifdef _WIN32 |
| 30 |
SOCKET fd; |
| 31 |
#else |
| 32 |
int fd; |
| 33 |
#endif |
| 34 |
} sock_gz_r; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
void *prepare_sock_gz_r(FILE *sockr) |
| 39 |
{ |
| 40 |
int err; |
| 41 |
sock_gz_r *big; |
| 42 |
|
| 43 |
big = (sock_gz_r *)malloc(sizeof(sock_gz_r)); |
| 44 |
if(big == NULL) return NULL; |
| 45 |
big->stream.next_in = Z_NULL; |
| 46 |
big->stream.avail_in = 0; |
| 47 |
big->stream.avail_out = 0; |
| 48 |
big->stream.zalloc = Z_NULL; |
| 49 |
big->stream.zfree = Z_NULL; |
| 50 |
big->stream.opaque = NULL; |
| 51 |
big->pos = big->text_buffer; |
| 52 |
big->endbuf = big->pos; |
| 53 |
#ifdef _WIN32 |
| 54 |
big->fd = (SOCKET)sockr; |
| 55 |
#else |
| 56 |
big->fd = fileno(sockr); |
| 57 |
#endif |
| 58 |
err = inflateInit(&big->stream); |
| 59 |
return err == Z_OK ? (void *)big : NULL; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
int z_getc(void *v) |
| 64 |
{ |
| 65 |
int q; |
| 66 |
sock_gz_r *big = (sock_gz_r *)v; |
| 67 |
z_streamp zs; |
| 68 |
|
| 69 |
if(big->pos < big->endbuf) { |
| 70 |
return *(big->pos++); |
| 71 |
} |
| 72 |
zs = &(big->stream); |
| 73 |
zs->next_out = (Bytef *)big->text_buffer; |
| 74 |
zs->avail_out = sizeof(big->text_buffer); |
| 75 |
big->pos = (char *)zs->next_out; |
| 76 |
do { |
| 77 |
if(zs->avail_in == 0) { |
| 78 |
int lu; |
| 79 |
#ifdef _WIN32 |
| 80 |
lu = recv( big->fd , big->z_buffer, ZBSIZE, 0 ); |
| 81 |
#else |
| 82 |
lu = read( big->fd , big->z_buffer, ZBSIZE ); |
| 83 |
#endif |
| 84 |
if(lu == -1) return EOF; |
| 85 |
zs->next_in = (Bytef *)big->z_buffer; |
| 86 |
zs->avail_in = lu; |
| 87 |
} |
| 88 |
q = inflate(zs, Z_NO_FLUSH); |
| 89 |
if(q == Z_STREAM_END) break; |
| 90 |
if(q != Z_OK) { |
| 91 |
break; |
| 92 |
} |
| 93 |
} |
| 94 |
while ( (char *)zs->next_out == big->pos); |
| 95 |
big->endbuf = (char *)zs->next_out; |
| 96 |
if(big->pos < big->endbuf) return *(big->pos++); |
| 97 |
else |
| 98 |
return EOF; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
char *z_gets(void *v, char *line, size_t len) |
| 103 |
{ |
| 104 |
int c; |
| 105 |
char *p; |
| 106 |
|
| 107 |
p = line; |
| 108 |
while(len > 1) { |
| 109 |
c = z_getc( v ); |
| 110 |
if(c == EOF) { |
| 111 |
if(p == line) return NULL; |
| 112 |
break; |
| 113 |
} |
| 114 |
*(p++) = c; |
| 115 |
if(c == '\n') break; |
| 116 |
len--; |
| 117 |
} |
| 118 |
*p = 0; |
| 119 |
return line; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
char *z_read_sock(void *v) |
| 124 |
{ |
| 125 |
static char line[500]; |
| 126 |
char *p; |
| 127 |
int l; |
| 128 |
|
| 129 |
p = z_gets(v, line, sizeof(line)); |
| 130 |
if(p == NULL) return NULL; |
| 131 |
l = strlen(line); |
| 132 |
if(l > 0 && line[l-1] == '\n') line[l-1] = 0; |
| 133 |
return line; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
int close_sock_gz_r(void *v) |
| 138 |
{ |
| 139 |
sock_gz_r *big = (sock_gz_r *)v; |
| 140 |
int val; |
| 141 |
|
| 142 |
val = inflateEnd(&(big->stream)); |
| 143 |
free(big); |
| 144 |
return val; |
| 145 |
} |
| 146 |
|