mirror of
https://github.com/rkd77/elinks.git
synced 2024-12-04 14:46:47 -05:00
[brotli] brotli_read similar to zstd_read.
First read all data, then decompress at once.
This commit is contained in:
parent
6bd5928d8f
commit
8d97cc7641
@ -32,9 +32,11 @@ struct br_enc_data {
|
|||||||
size_t avail_out;
|
size_t avail_out;
|
||||||
size_t total_out;
|
size_t total_out;
|
||||||
uint8_t *buffer;
|
uint8_t *buffer;
|
||||||
|
size_t sent_pos;
|
||||||
|
|
||||||
/* The file descriptor from which we read. */
|
/* The file descriptor from which we read. */
|
||||||
int fdread;
|
int fdread;
|
||||||
|
int decoded:1;
|
||||||
int after_end:1;
|
int after_end:1;
|
||||||
int last_read:1;
|
int last_read:1;
|
||||||
unsigned char buf[ELINKS_BROTLI_BUFFER_LENGTH];
|
unsigned char buf[ELINKS_BROTLI_BUFFER_LENGTH];
|
||||||
@ -61,55 +63,58 @@ brotli_open(struct stream_encoded *stream, int fd)
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
static char *brotli_decode_buffer(struct stream_encoded *st, char *datac, int len, int *new_len);
|
||||||
|
|
||||||
static int
|
static int
|
||||||
brotli_read(struct stream_encoded *stream, char *bufc, int len)
|
brotli_read(struct stream_encoded *stream, char *buf, int len)
|
||||||
{
|
{
|
||||||
uint8_t *buf = (uint8_t*)bufc;
|
|
||||||
struct br_enc_data *data = (struct br_enc_data *) stream->data;
|
struct br_enc_data *data = (struct br_enc_data *) stream->data;
|
||||||
int err = 0;
|
|
||||||
|
|
||||||
if (!data) return -1;
|
if (!data) return -1;
|
||||||
|
|
||||||
assert(len > 0);
|
assert(len > 0);
|
||||||
|
|
||||||
if (data->last_read) return 0;
|
if (!data->decoded) {
|
||||||
|
size_t read_pos = 0;
|
||||||
|
char *tmp_buf = (char *)mem_alloc(len);
|
||||||
|
int new_len;
|
||||||
|
|
||||||
data->avail_out = len;
|
if (!tmp_buf) {
|
||||||
data->next_out = (uint8_t *)buf;
|
return 0;
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
int l = safe_read(data->fdread, tmp_buf + read_pos, len - read_pos);
|
||||||
|
|
||||||
do {
|
if (!l) break;
|
||||||
if (data->avail_in == 0) {
|
|
||||||
int l = safe_read(data->fdread, data->buf,
|
|
||||||
ELINKS_BROTLI_BUFFER_LENGTH);
|
|
||||||
|
|
||||||
|
if (l == -1 && errno == EAGAIN) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (l == -1) {
|
if (l == -1) {
|
||||||
if (errno == EAGAIN)
|
|
||||||
break;
|
|
||||||
else
|
|
||||||
return -1; /* I/O error */
|
|
||||||
} else if (l == 0) {
|
|
||||||
/* EOF. It is error: we wait for more bytes */
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
read_pos += l;
|
||||||
|
} while (1);
|
||||||
|
|
||||||
data->next_in = data->buf;
|
if (brotli_decode_buffer(stream, tmp_buf, len, &new_len)) {
|
||||||
data->avail_in = l;
|
data->decoded = 1;
|
||||||
|
}
|
||||||
|
mem_free(tmp_buf);
|
||||||
|
}
|
||||||
|
if (data->decoded) {
|
||||||
|
int length = len < (data->total_out - data->sent_pos) ? len : (data->total_out - data->sent_pos);
|
||||||
|
|
||||||
|
if (length <= 0) {
|
||||||
|
mem_free_set(&data->buffer, NULL);
|
||||||
|
} else {
|
||||||
|
memcpy(buf, (void *)((char *)(data->buffer) + data->sent_pos), length);
|
||||||
|
data->sent_pos += length;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = BrotliDecoderDecompressStream(data->state, &data->avail_in, &data->next_in,
|
return length;
|
||||||
&data->avail_out, &data->next_out, &data->total_out);
|
}
|
||||||
|
|
||||||
if (err == BROTLI_DECODER_RESULT_SUCCESS) {
|
return -1;
|
||||||
data->last_read = 1;
|
|
||||||
break;
|
|
||||||
} else if (err == BROTLI_DECODER_RESULT_ERROR) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
} while (data->avail_out > 0);
|
|
||||||
|
|
||||||
assert(len - data->avail_out == data->next_out - buf);
|
|
||||||
return len - data->avail_out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
|
Loading…
Reference in New Issue
Block a user