fx.compression: convert to new assembly build system
This commit is contained in:
+183
-58
@@ -1,5 +1,5 @@
|
||||
#include <fx/compress/compressor.h>
|
||||
#include <fx/compress/cstream.h>
|
||||
#include <fx/compression/compressor.h>
|
||||
#include <fx/compression/cstream.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -17,8 +17,8 @@ struct fx_cstream_p {
|
||||
*
|
||||
* the input buffer holds data that will be provided to the
|
||||
* (de)compression function. in compression mode, this data is provided
|
||||
* by the code using the cstream (via fx_cstream_write). in decompression
|
||||
* mode, this data is read from s_endpoint.
|
||||
* by the code using the cstream (via fx_cstream_write). in
|
||||
* decompression mode, this data is read from s_endpoint.
|
||||
*
|
||||
* the output buffer holds data produced by the (de)compression
|
||||
* function. in compression mode, this data will be written to
|
||||
@@ -27,7 +27,8 @@ struct fx_cstream_p {
|
||||
*
|
||||
* heavy usage of cstream's compressed sections facility can result
|
||||
* in the input buffer holding uncompressed data while the stream is in
|
||||
* decompression mode. this is handled by the uncompressed read code path.
|
||||
* decompression mode. this is handled by the uncompressed read code
|
||||
* path.
|
||||
*/
|
||||
fx_ringbuffer *s_in, *s_out;
|
||||
fx_compressor_mode s_mode;
|
||||
@@ -78,13 +79,23 @@ struct fx_cstream_p {
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static enum fx_status read_cursor(
|
||||
struct fx_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read)
|
||||
struct fx_cstream_p *stream,
|
||||
void *buf,
|
||||
size_t count,
|
||||
size_t *out_nr_read)
|
||||
{
|
||||
return fx_stream_read_bytes(stream->s_endpoint, buf, count, out_nr_read);
|
||||
return fx_stream_read_bytes(
|
||||
stream->s_endpoint,
|
||||
buf,
|
||||
count,
|
||||
out_nr_read);
|
||||
}
|
||||
|
||||
static enum fx_status read_uncompressed(
|
||||
struct fx_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read)
|
||||
struct fx_cstream_p *stream,
|
||||
void *buf,
|
||||
size_t count,
|
||||
size_t *out_nr_read)
|
||||
{
|
||||
size_t remaining = count;
|
||||
unsigned char *dest = buf;
|
||||
@@ -101,7 +112,9 @@ static enum fx_status read_uncompressed(
|
||||
const void *data;
|
||||
size_t available;
|
||||
status = fx_ringbuffer_open_read_buffer(
|
||||
stream->s_in, &data, &available);
|
||||
stream->s_in,
|
||||
&data,
|
||||
&available);
|
||||
if (!FX_OK(status)) {
|
||||
break;
|
||||
}
|
||||
@@ -128,7 +141,10 @@ static enum fx_status read_uncompressed(
|
||||
}
|
||||
|
||||
status = fx_stream_read_bytes(
|
||||
stream->s_endpoint, dest, remaining, &nr_read_from_endpoint);
|
||||
stream->s_endpoint,
|
||||
dest,
|
||||
remaining,
|
||||
&nr_read_from_endpoint);
|
||||
stream->s_tx_bytes_uncompressed += nr_read_from_endpoint;
|
||||
stream->s_tx_bytes += nr_read_from_endpoint;
|
||||
|
||||
@@ -150,14 +166,19 @@ static enum fx_status refill_input_buffer(struct fx_cstream_p *stream)
|
||||
void *data;
|
||||
size_t capacity;
|
||||
status = fx_ringbuffer_open_write_buffer(
|
||||
stream->s_in, &data, &capacity);
|
||||
stream->s_in,
|
||||
&data,
|
||||
&capacity);
|
||||
if (!FX_OK(status)) {
|
||||
break;
|
||||
}
|
||||
|
||||
size_t r = 0;
|
||||
status = fx_stream_read_bytes(
|
||||
stream->s_endpoint, data, capacity, &r);
|
||||
stream->s_endpoint,
|
||||
data,
|
||||
capacity,
|
||||
&r);
|
||||
|
||||
fx_ringbuffer_close_write_buffer(stream->s_in, &data, r);
|
||||
nr_read += r;
|
||||
@@ -196,9 +217,11 @@ static enum fx_status refill_output_buffer(struct fx_cstream_p *stream)
|
||||
return status;
|
||||
}
|
||||
|
||||
size_t bytes_before = fx_ringbuffer_available_data_remaining(stream->s_in);
|
||||
size_t bytes_before
|
||||
= fx_ringbuffer_available_data_remaining(stream->s_in);
|
||||
status = fx_compressor_step(stream->s_compressor);
|
||||
size_t bytes_after = fx_ringbuffer_available_data_remaining(stream->s_in);
|
||||
size_t bytes_after
|
||||
= fx_ringbuffer_available_data_remaining(stream->s_in);
|
||||
|
||||
stream->s_tx_bytes_compressed += (bytes_before - bytes_after);
|
||||
stream->s_tx_bytes += (bytes_before - bytes_after);
|
||||
@@ -207,7 +230,10 @@ static enum fx_status refill_output_buffer(struct fx_cstream_p *stream)
|
||||
}
|
||||
|
||||
static enum fx_status cstream_read(
|
||||
struct fx_cstream_p *stream, void *buf, size_t count, size_t *out_nr_read)
|
||||
struct fx_cstream_p *stream,
|
||||
void *buf,
|
||||
size_t count,
|
||||
size_t *out_nr_read)
|
||||
{
|
||||
if (stream->s_mode != FX_COMPRESSOR_MODE_DECOMPRESS) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
@@ -238,7 +264,9 @@ static enum fx_status cstream_read(
|
||||
const void *data;
|
||||
size_t available;
|
||||
status = fx_ringbuffer_open_read_buffer(
|
||||
stream->s_out, &data, &available);
|
||||
stream->s_out,
|
||||
&data,
|
||||
&available);
|
||||
if (!FX_OK(status)) {
|
||||
break;
|
||||
}
|
||||
@@ -267,13 +295,23 @@ static enum fx_status cstream_read(
|
||||
}
|
||||
|
||||
static enum fx_status write_cursor(
|
||||
struct fx_cstream_p *stream, const void *buf, size_t count, size_t *nr_written)
|
||||
struct fx_cstream_p *stream,
|
||||
const void *buf,
|
||||
size_t count,
|
||||
size_t *nr_written)
|
||||
{
|
||||
return fx_stream_write_bytes(stream->s_endpoint, buf, count, nr_written);
|
||||
return fx_stream_write_bytes(
|
||||
stream->s_endpoint,
|
||||
buf,
|
||||
count,
|
||||
nr_written);
|
||||
}
|
||||
|
||||
static enum fx_status write_uncompressed(
|
||||
struct fx_cstream_p *stream, const void *buf, size_t count, size_t *nr_written)
|
||||
struct fx_cstream_p *stream,
|
||||
const void *buf,
|
||||
size_t count,
|
||||
size_t *nr_written)
|
||||
{
|
||||
size_t w = 0;
|
||||
enum fx_status status
|
||||
@@ -307,14 +345,19 @@ static enum fx_status flush_output_buffer(struct fx_cstream_p *stream)
|
||||
const void *data;
|
||||
size_t capacity;
|
||||
status = fx_ringbuffer_open_read_buffer(
|
||||
stream->s_out, &data, &capacity);
|
||||
stream->s_out,
|
||||
&data,
|
||||
&capacity);
|
||||
if (!FX_OK(status)) {
|
||||
break;
|
||||
}
|
||||
|
||||
size_t w = 0;
|
||||
status = fx_stream_write_bytes(
|
||||
stream->s_endpoint, data, capacity, &w);
|
||||
stream->s_endpoint,
|
||||
data,
|
||||
capacity,
|
||||
&w);
|
||||
|
||||
fx_ringbuffer_close_read_buffer(stream->s_out, &data, w);
|
||||
nr_written += w;
|
||||
@@ -338,7 +381,9 @@ static enum fx_status flush_output_buffer(struct fx_cstream_p *stream)
|
||||
}
|
||||
|
||||
static enum fx_status cstream_write(
|
||||
struct fx_cstream_p *stream, const void *buf, size_t count,
|
||||
struct fx_cstream_p *stream,
|
||||
const void *buf,
|
||||
size_t count,
|
||||
size_t *out_nr_written)
|
||||
{
|
||||
if (stream->s_mode != FX_COMPRESSOR_MODE_COMPRESS) {
|
||||
@@ -374,7 +419,9 @@ static enum fx_status cstream_write(
|
||||
void *data;
|
||||
size_t available;
|
||||
status = fx_ringbuffer_open_write_buffer(
|
||||
stream->s_in, &data, &available);
|
||||
stream->s_in,
|
||||
&data,
|
||||
&available);
|
||||
if (!FX_OK(status)) {
|
||||
break;
|
||||
}
|
||||
@@ -403,7 +450,9 @@ static enum fx_status cstream_write(
|
||||
}
|
||||
|
||||
static enum fx_status skip_uncompressed(
|
||||
struct fx_cstream_p *stream, size_t count, size_t *out_nr_skipped)
|
||||
struct fx_cstream_p *stream,
|
||||
size_t count,
|
||||
size_t *out_nr_skipped)
|
||||
{
|
||||
size_t remaining = count;
|
||||
size_t nr_read_from_buf = 0;
|
||||
@@ -419,7 +468,9 @@ static enum fx_status skip_uncompressed(
|
||||
const void *data;
|
||||
size_t available;
|
||||
status = fx_ringbuffer_open_read_buffer(
|
||||
stream->s_in, &data, &available);
|
||||
stream->s_in,
|
||||
&data,
|
||||
&available);
|
||||
if (!FX_OK(status)) {
|
||||
break;
|
||||
}
|
||||
@@ -447,7 +498,10 @@ static enum fx_status skip_uncompressed(
|
||||
|
||||
size_t cursor = fx_stream_cursor(stream->s_endpoint);
|
||||
|
||||
status = fx_stream_seek(stream->s_endpoint, remaining, FX_STREAM_SEEK_CURRENT);
|
||||
status = fx_stream_seek(
|
||||
stream->s_endpoint,
|
||||
remaining,
|
||||
FX_STREAM_SEEK_CURRENT);
|
||||
nr_read_from_endpoint = fx_stream_cursor(stream->s_endpoint) - cursor;
|
||||
stream->s_tx_bytes_uncompressed += nr_read_from_endpoint;
|
||||
stream->s_tx_bytes += nr_read_from_endpoint;
|
||||
@@ -460,7 +514,9 @@ static enum fx_status skip_uncompressed(
|
||||
}
|
||||
|
||||
static enum fx_status cstream_skip(
|
||||
struct fx_cstream_p *stream, size_t count, size_t *out_nr_skipped)
|
||||
struct fx_cstream_p *stream,
|
||||
size_t count,
|
||||
size_t *out_nr_skipped)
|
||||
{
|
||||
if (stream->s_mode != FX_COMPRESSOR_MODE_DECOMPRESS) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
@@ -499,7 +555,9 @@ static enum fx_status cstream_skip(
|
||||
const void *data;
|
||||
size_t available;
|
||||
status = fx_ringbuffer_open_read_buffer(
|
||||
stream->s_out, &data, &available);
|
||||
stream->s_out,
|
||||
&data,
|
||||
&available);
|
||||
if (!FX_OK(status)) {
|
||||
break;
|
||||
}
|
||||
@@ -554,7 +612,8 @@ static enum fx_status cstream_reset(struct fx_cstream_p *stream)
|
||||
}
|
||||
|
||||
static enum fx_status cstream_begin_compressed_section(
|
||||
struct fx_cstream_p *stream, size_t *tx_uncompressed_bytes)
|
||||
struct fx_cstream_p *stream,
|
||||
size_t *tx_uncompressed_bytes)
|
||||
{
|
||||
if (stream->s_flags & CSTREAM_CURSOR_MOVED) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
@@ -578,7 +637,8 @@ static enum fx_status cstream_begin_compressed_section(
|
||||
}
|
||||
|
||||
static enum fx_status cstream_end_compressed_section(
|
||||
struct fx_cstream_p *stream, size_t *tx_compressed_bytes,
|
||||
struct fx_cstream_p *stream,
|
||||
size_t *tx_compressed_bytes,
|
||||
size_t *tx_uncompressed_bytes)
|
||||
{
|
||||
if (stream->s_flags & CSTREAM_CURSOR_MOVED) {
|
||||
@@ -646,28 +706,33 @@ static bool cstream_in_compressed_section(const struct fx_cstream_p *stream)
|
||||
return stream->s_compression_depth > 0;
|
||||
}
|
||||
|
||||
static enum fx_status cstream_tx_bytes(const struct fx_cstream_p *stream, size_t *out)
|
||||
static enum fx_status cstream_tx_bytes(
|
||||
const struct fx_cstream_p *stream,
|
||||
size_t *out)
|
||||
{
|
||||
*out = stream->s_tx_bytes;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static enum fx_status cstream_tx_bytes_compressed(
|
||||
const struct fx_cstream_p *stream, size_t *out)
|
||||
const struct fx_cstream_p *stream,
|
||||
size_t *out)
|
||||
{
|
||||
*out = stream->s_tx_bytes_compressed;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static enum fx_status cstream_tx_bytes_uncompressed(
|
||||
const struct fx_cstream_p *stream, size_t *out)
|
||||
const struct fx_cstream_p *stream,
|
||||
size_t *out)
|
||||
{
|
||||
*out = stream->s_tx_bytes_uncompressed;
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static enum fx_status cstream_set_cursor_position(
|
||||
struct fx_cstream_p *stream, size_t pos)
|
||||
struct fx_cstream_p *stream,
|
||||
size_t pos)
|
||||
{
|
||||
if (stream->s_compression_depth > 0) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
@@ -691,14 +756,17 @@ static enum fx_status cstream_set_cursor_position(
|
||||
return FX_SUCCESS;
|
||||
}
|
||||
|
||||
static enum fx_status cstream_restore_cursor_position(struct fx_cstream_p *stream)
|
||||
static enum fx_status cstream_restore_cursor_position(
|
||||
struct fx_cstream_p *stream)
|
||||
{
|
||||
if (!(stream->s_flags & CSTREAM_CURSOR_MOVED)) {
|
||||
return FX_ERR_BAD_STATE;
|
||||
}
|
||||
|
||||
enum fx_status status = fx_stream_seek(
|
||||
stream->s_endpoint, stream->s_cursor, FX_STREAM_SEEK_START);
|
||||
stream->s_endpoint,
|
||||
stream->s_cursor,
|
||||
FX_STREAM_SEEK_START);
|
||||
stream->s_cursor = 0;
|
||||
|
||||
if (!FX_OK(status)) {
|
||||
@@ -713,12 +781,17 @@ static enum fx_status cstream_restore_cursor_position(struct fx_cstream_p *strea
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
enum fx_status fx_cstream_open(
|
||||
fx_stream *endpoint, fx_type compressor_type, fx_compressor_mode mode,
|
||||
fx_stream *endpoint,
|
||||
fx_type compressor_type,
|
||||
fx_compressor_mode mode,
|
||||
fx_cstream **out)
|
||||
{
|
||||
size_t inbuf_size = 0, outbuf_size = 0;
|
||||
enum fx_status status = fx_compressor_get_buffer_size(
|
||||
compressor_type, mode, &inbuf_size, &outbuf_size);
|
||||
compressor_type,
|
||||
mode,
|
||||
&inbuf_size,
|
||||
&outbuf_size);
|
||||
if (!FX_OK(status)) {
|
||||
return status;
|
||||
}
|
||||
@@ -735,7 +808,7 @@ enum fx_status fx_cstream_open(
|
||||
p->s_endpoint = endpoint;
|
||||
|
||||
cfg->s_mode = (mode == FX_COMPRESSOR_MODE_COMPRESS) ? FX_STREAM_WRITE
|
||||
: FX_STREAM_READ;
|
||||
: FX_STREAM_READ;
|
||||
|
||||
p->s_in = fx_ringbuffer_create(inbuf_size + 1);
|
||||
if (!FX_OK(status)) {
|
||||
@@ -764,23 +837,46 @@ enum fx_status fx_cstream_open(
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_read(
|
||||
fx_cstream *stream, void *buf, size_t count, size_t *out_nr_read)
|
||||
fx_cstream *stream,
|
||||
void *buf,
|
||||
size_t count,
|
||||
size_t *out_nr_read)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_CSTREAM, cstream_read, stream, buf, count, out_nr_read);
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_read,
|
||||
stream,
|
||||
buf,
|
||||
count,
|
||||
out_nr_read);
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_write(
|
||||
fx_cstream *stream, const void *buf, size_t count, size_t *out_nr_written)
|
||||
fx_cstream *stream,
|
||||
const void *buf,
|
||||
size_t count,
|
||||
size_t *out_nr_written)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_CSTREAM, cstream_write, stream, buf, count, out_nr_written);
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_write,
|
||||
stream,
|
||||
buf,
|
||||
count,
|
||||
out_nr_written);
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_skip(fx_cstream *stream, size_t count, size_t *out_nr_skipped)
|
||||
enum fx_status fx_cstream_skip(
|
||||
fx_cstream *stream,
|
||||
size_t count,
|
||||
size_t *out_nr_skipped)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_CSTREAM, cstream_skip, stream, count, out_nr_skipped);
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_skip,
|
||||
stream,
|
||||
count,
|
||||
out_nr_skipped);
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_reset(fx_cstream *stream)
|
||||
@@ -789,54 +885,83 @@ enum fx_status fx_cstream_reset(fx_cstream *stream)
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_begin_compressed_section(
|
||||
fx_cstream *stream, size_t *tx_uncompressed_bytes)
|
||||
fx_cstream *stream,
|
||||
size_t *tx_uncompressed_bytes)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_CSTREAM, cstream_begin_compressed_section, stream,
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_begin_compressed_section,
|
||||
stream,
|
||||
tx_uncompressed_bytes);
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_end_compressed_section(
|
||||
fx_cstream *stream, size_t *tx_compressed_bytes, size_t *tx_uncompressed_bytes)
|
||||
fx_cstream *stream,
|
||||
size_t *tx_compressed_bytes,
|
||||
size_t *tx_uncompressed_bytes)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_CSTREAM, cstream_end_compressed_section, stream,
|
||||
tx_compressed_bytes, tx_uncompressed_bytes);
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_end_compressed_section,
|
||||
stream,
|
||||
tx_compressed_bytes,
|
||||
tx_uncompressed_bytes);
|
||||
}
|
||||
|
||||
bool fx_cstream_in_compressed_section(const fx_cstream *stream)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_CSTREAM, cstream_in_compressed_section, stream);
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_in_compressed_section,
|
||||
stream);
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_tx_bytes(const fx_cstream *stream, size_t *out)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(FX_TYPE_CSTREAM, cstream_tx_bytes, stream, out);
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_tx_bytes,
|
||||
stream,
|
||||
out);
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_tx_bytes_compressed(const fx_cstream *stream, size_t *out)
|
||||
enum fx_status fx_cstream_tx_bytes_compressed(
|
||||
const fx_cstream *stream,
|
||||
size_t *out)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_CSTREAM, cstream_tx_bytes_compressed, stream, out);
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_tx_bytes_compressed,
|
||||
stream,
|
||||
out);
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_tx_bytes_uncompressed(const fx_cstream *stream, size_t *out)
|
||||
enum fx_status fx_cstream_tx_bytes_uncompressed(
|
||||
const fx_cstream *stream,
|
||||
size_t *out)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_CSTREAM, cstream_tx_bytes_uncompressed, stream, out);
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_tx_bytes_uncompressed,
|
||||
stream,
|
||||
out);
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_set_cursor_position(fx_cstream *stream, size_t pos)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC(
|
||||
FX_TYPE_CSTREAM, cstream_set_cursor_position, stream, pos);
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_set_cursor_position,
|
||||
stream,
|
||||
pos);
|
||||
}
|
||||
|
||||
enum fx_status fx_cstream_restore_cursor_position(fx_cstream *stream)
|
||||
{
|
||||
FX_CLASS_DISPATCH_STATIC_0(
|
||||
FX_TYPE_CSTREAM, cstream_restore_cursor_position, stream);
|
||||
FX_TYPE_CSTREAM,
|
||||
cstream_restore_cursor_position,
|
||||
stream);
|
||||
}
|
||||
|
||||
/*** VIRTUAL FUNCTIONS ********************************************************/
|
||||
|
||||
Reference in New Issue
Block a user