mirror of
https://github.com/coderforlife/ms-compress
synced 2026-06-08 13:34:55 +00:00
Avoid ODR violations.
GCC 5.2 with LTO will emit warnings when linking due to ODR violations which occur across translation units.
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
|
||||
size_t lznt1_max_compressed_size(size_t in_len) { return in_len + 3 + 2 * ((in_len + CHUNK_SIZE - 1) / CHUNK_SIZE); }
|
||||
|
||||
struct _mscomp_internal_state
|
||||
typedef struct
|
||||
{ // 8,218 - 8,238 bytes (+padding) + dictionary memory
|
||||
bool finished; // means fully finished
|
||||
LZNT1Dictionary d;
|
||||
@@ -34,7 +34,7 @@ struct _mscomp_internal_state
|
||||
size_t in_needed, in_avail;
|
||||
byte out[CHUNK_SIZE+2];
|
||||
size_t out_pos, out_avail;
|
||||
};
|
||||
} mscomp_lznt1_compress_state;
|
||||
|
||||
#ifdef MSCOMP_WITH_LZNT1_SA_DICT
|
||||
#define RETURN_IF_NOT_SA_DICT_AND_OUT_ZERO(x)
|
||||
@@ -94,7 +94,7 @@ FORCE_INLINE static uint_fast16_t lznt1_compress_chunk(const_rest_bytes const in
|
||||
}
|
||||
static bool lznt1_compress_chunk_write(mscomp_stream* RESTRICT const stream, const_rest_bytes const in, const uint_fast16_t in_len)
|
||||
{
|
||||
mscomp_internal_state* RESTRICT state = stream->state;
|
||||
mscomp_lznt1_compress_state* RESTRICT state = (mscomp_lznt1_compress_state*) stream->state;
|
||||
bool out_buffering = stream->out_avail < in_len+2u;
|
||||
rest_bytes out = out_buffering ? state->out : stream->out;
|
||||
|
||||
@@ -137,7 +137,7 @@ MSCompStatus lznt1_deflate_init(mscomp_stream* RESTRICT const stream)
|
||||
{
|
||||
INIT_STREAM(stream, true, MSCOMP_LZNT1);
|
||||
|
||||
mscomp_internal_state* RESTRICT state = (mscomp_internal_state*)malloc(sizeof(mscomp_internal_state));
|
||||
mscomp_lznt1_compress_state* RESTRICT state = (mscomp_lznt1_compress_state*)malloc(sizeof(mscomp_lznt1_compress_state));
|
||||
if (UNLIKELY(state == NULL)) { SET_ERROR(stream, "LZNT1 Compression Error: Unable to allocate buffer memory"); return MSCOMP_MEM_ERROR; }
|
||||
state->finished = false;
|
||||
state->in_needed = 0;
|
||||
@@ -146,14 +146,14 @@ MSCompStatus lznt1_deflate_init(mscomp_stream* RESTRICT const stream)
|
||||
state->out_avail = 0;
|
||||
new (&state->d) LZNT1Dictionary();
|
||||
|
||||
stream->state = state;
|
||||
stream->state = (mscomp_internal_state*) state;
|
||||
return MSCOMP_OK;
|
||||
}
|
||||
ENTRY_POINT MSCompStatus lznt1_deflate(mscomp_stream* RESTRICT const stream, const MSCompFlush flush)
|
||||
{
|
||||
CHECK_STREAM_PLUS(stream, true, MSCOMP_LZNT1, stream->state == NULL || stream->state->finished);
|
||||
mscomp_lznt1_compress_state* RESTRICT state = (mscomp_lznt1_compress_state*) stream->state;
|
||||
|
||||
mscomp_internal_state* RESTRICT state = stream->state;
|
||||
CHECK_STREAM_PLUS(stream, true, MSCOMP_LZNT1, state == NULL || state->finished);
|
||||
|
||||
DUMP_OUT(state, stream);
|
||||
APPEND_IN(state, stream,
|
||||
@@ -217,7 +217,7 @@ MSCompStatus lznt1_deflate_end(mscomp_stream* RESTRICT stream)
|
||||
{
|
||||
CHECK_STREAM_PLUS(stream, true, MSCOMP_LZNT1, stream->state == NULL);
|
||||
|
||||
mscomp_internal_state* RESTRICT state = stream->state;
|
||||
mscomp_lznt1_compress_state* RESTRICT state = (mscomp_lznt1_compress_state*) stream->state;
|
||||
|
||||
MSCompStatus status = MSCOMP_OK;
|
||||
if (UNLIKELY(!state->finished || stream->in_avail || state->in_avail || state->out_avail)) { SET_ERROR(stream, "LZNT1 Compression Error: End prematurely called"); status = MSCOMP_DATA_ERROR; }
|
||||
@@ -262,7 +262,7 @@ ENTRY_POINT MSCompStatus lznt1_compress(const_rest_bytes in, size_t in_len, rest
|
||||
out_pos += out_size+2;
|
||||
in_pos += in_size;
|
||||
}
|
||||
|
||||
|
||||
// Return insufficient buffer or the compressed size
|
||||
if (UNLIKELY(in_pos < in_len)) { return MSCOMP_BUF_ERROR; }
|
||||
// https://msdn.microsoft.com/library/jj679084.aspx: If an End_of_buffer terminal is added, the
|
||||
|
||||
+10
-10
@@ -23,14 +23,14 @@
|
||||
|
||||
#define CHUNK_SIZE 0x1000 // to be compatible with all known forms of Windows
|
||||
|
||||
struct _mscomp_internal_state
|
||||
typedef struct
|
||||
{ // 8,214 - 8,230 bytes (+padding)
|
||||
bool end_of_stream;
|
||||
byte in[CHUNK_SIZE+2];
|
||||
size_t in_needed, in_avail;
|
||||
byte out[CHUNK_SIZE];
|
||||
size_t out_pos, out_avail;
|
||||
};
|
||||
} mscomp_lznt1_decompress_state;
|
||||
|
||||
|
||||
/////////////////// Decompression Functions ///////////////////////////////////
|
||||
@@ -39,7 +39,7 @@ static MSCompStatus lznt1_decompress_chunk(const_rest_bytes in, const const_byte
|
||||
const const_bytes in_endx = in_end -0x11; // 1 + 8 * 2 from the end
|
||||
const const_bytes out_start = out, out_endx = out_end-8*FAST_COPY_ROOM;
|
||||
byte flags, flagged;
|
||||
|
||||
|
||||
uint_fast16_t pow2 = 0x10, mask = 0xFFF, shift = 12;
|
||||
const_bytes pow2_target = out_start + 0x10;
|
||||
uint_fast16_t len, off;
|
||||
@@ -72,7 +72,7 @@ static MSCompStatus lznt1_decompress_chunk(const_rest_bytes in, const const_byte
|
||||
flags >>= 1;
|
||||
} while (LIKELY(flags));
|
||||
}
|
||||
|
||||
|
||||
// Slower decompression but with full bounds checking
|
||||
while (LIKELY(in < in_end))
|
||||
{
|
||||
@@ -121,7 +121,7 @@ CHECKED_COPY: for (end = out + len; out < end; ++out) { *out = *(out-off); }
|
||||
}
|
||||
MSCompStatus lznt1_decompress_chunk_read(mscomp_stream* RESTRICT const stream, const_rest_bytes const in, size_t* RESTRICT const in_len)
|
||||
{
|
||||
mscomp_internal_state* RESTRICT state = stream->state;
|
||||
mscomp_lznt1_decompress_state* RESTRICT state = (mscomp_lznt1_decompress_state*) stream->state;
|
||||
|
||||
// Read chunk header
|
||||
const uint16_t header = GET_UINT16(in);
|
||||
@@ -211,7 +211,7 @@ MSCompStatus lznt1_inflate_init(mscomp_stream* RESTRICT stream)
|
||||
{
|
||||
INIT_STREAM(stream, false, MSCOMP_LZNT1);
|
||||
|
||||
mscomp_internal_state* RESTRICT state = (mscomp_internal_state*)malloc(sizeof(mscomp_internal_state));
|
||||
mscomp_lznt1_decompress_state* RESTRICT state = (mscomp_lznt1_decompress_state*)malloc(sizeof(mscomp_lznt1_decompress_state));
|
||||
if (UNLIKELY(state == NULL)) { SET_ERROR(stream, "LZNT1 Decompression Error: Unable to allocate buffer memory"); return MSCOMP_MEM_ERROR; }
|
||||
state->end_of_stream = false;
|
||||
state->in_needed = 0;
|
||||
@@ -219,14 +219,14 @@ MSCompStatus lznt1_inflate_init(mscomp_stream* RESTRICT stream)
|
||||
state->out_pos = 0;
|
||||
state->out_avail = 0;
|
||||
|
||||
stream->state = state;
|
||||
stream->state = (mscomp_internal_state*) state;
|
||||
return MSCOMP_OK;
|
||||
}
|
||||
ENTRY_POINT MSCompStatus lznt1_inflate(mscomp_stream* RESTRICT stream)
|
||||
{
|
||||
CHECK_STREAM_PLUS(stream, false, MSCOMP_LZNT1, stream->state == NULL);
|
||||
|
||||
mscomp_internal_state* RESTRICT state = stream->state;
|
||||
mscomp_lznt1_decompress_state* RESTRICT state = (mscomp_lznt1_decompress_state*) stream->state;
|
||||
|
||||
DUMP_OUT(state, stream);
|
||||
if (UNLIKELY(state->end_of_stream))
|
||||
@@ -244,7 +244,7 @@ ENTRY_POINT MSCompStatus lznt1_inflate(mscomp_stream* RESTRICT stream)
|
||||
else if (UNLIKELY(state->end_of_stream)) { return MSCOMP_STREAM_END; }
|
||||
else if (state->in_needed) { continue; } // we only had enough to read the header this time
|
||||
);
|
||||
|
||||
|
||||
// Decompress full chunks while there is room in the output buffer
|
||||
while (stream->out_avail && stream->in_avail >= 2)
|
||||
{
|
||||
@@ -270,7 +270,7 @@ MSCompStatus lznt1_inflate_end(mscomp_stream* RESTRICT stream)
|
||||
{
|
||||
CHECK_STREAM_PLUS(stream, false, MSCOMP_LZNT1, stream->state == NULL);
|
||||
|
||||
mscomp_internal_state* RESTRICT state = stream->state;
|
||||
mscomp_lznt1_decompress_state* RESTRICT state = (mscomp_lznt1_decompress_state*) stream->state;
|
||||
|
||||
MSCompStatus status = MSCOMP_OK;
|
||||
if (UNLIKELY(stream->in_avail || state->in_avail || state->out_avail)) { SET_ERROR(stream, "LZNT1 Decompression Error: End prematurely called"); status = MSCOMP_DATA_ERROR; }
|
||||
|
||||
@@ -29,20 +29,20 @@ typedef XpressDictionary<0x2000> Dictionary;
|
||||
|
||||
size_t xpress_max_compressed_size(size_t in_len) { return in_len + 4 + 4 * (in_len / 32); }
|
||||
|
||||
struct _mscomp_internal_state
|
||||
typedef struct
|
||||
{ // ?-? bytes
|
||||
bool finished;
|
||||
|
||||
|
||||
uint32_t flags, *out_flags;
|
||||
byte flag_count;
|
||||
byte* half_byte;
|
||||
|
||||
|
||||
//byte in[10];
|
||||
//size_t in_avail;
|
||||
|
||||
byte out[10];
|
||||
size_t out_avail;
|
||||
};
|
||||
} mscomp_xpress_compress_state;
|
||||
|
||||
////////////////////////////// Compression Functions ///////////////////////////////////////////////
|
||||
#define PRINT_ERROR(...) // TODO: remove
|
||||
@@ -52,7 +52,7 @@ MSCompStatus xpress_deflate_init(mscomp_stream* stream)
|
||||
#ifdef _DEBUG
|
||||
INIT_STREAM(stream, true, MSCOMP_XPRESS);
|
||||
|
||||
mscomp_internal_state* state = (mscomp_internal_state*)malloc(sizeof(mscomp_internal_state));
|
||||
mscomp_xpress_compress_state* state = (mscomp_xpress_compress_state*)malloc(sizeof(mscomp_xpress_compress_state));
|
||||
if (UNLIKELY(state == NULL)) { SET_ERROR(stream, "XPRESS Compression Error: Unable to allocate buffer memory"); return MSCOMP_MEM_ERROR; }
|
||||
state->finished = false;
|
||||
state->flags = 0;
|
||||
@@ -83,7 +83,7 @@ ENTRY_POINT MSCompStatus xpress_deflate(mscomp_stream* stream, MSCompFlush flush
|
||||
#ifdef _XDEBUG
|
||||
CHECK_STREAM_PLUS(stream, true, MSCOMP_XPRESS, stream->state == NULL || stream->state->finished);
|
||||
|
||||
mscomp_internal_state *state = stream->state;
|
||||
mscomp_xpress_compress_state *state = (mscomp_xpress_compress_state*) stream->state;
|
||||
|
||||
const size_t out_avail = stream->out_avail;
|
||||
const_bytes in = stream->in; const const_bytes in_end = in +stream->in_avail;
|
||||
@@ -247,7 +247,7 @@ ENTRY_POINT MSCompStatus xpress_compress(const_bytes in, size_t in_len, bytes ou
|
||||
uint32_t flags = 0, *out_flags = (uint32_t*)out;
|
||||
byte flag_count;
|
||||
byte* half_byte = NULL;
|
||||
|
||||
|
||||
Dictionary d(in, in_end);
|
||||
|
||||
if (in_len == 0)
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
#include "../include/mscomp/CircularBuffer.h"
|
||||
typedef CircularBuffer<0x2000> Buffer;
|
||||
|
||||
struct _mscomp_internal_state
|
||||
typedef struct
|
||||
{ // 38-42 bytes (+padding) + buffer
|
||||
uint32_t flagged, flags;
|
||||
byte half_byte;
|
||||
@@ -35,7 +35,7 @@ struct _mscomp_internal_state
|
||||
Buffer buffer;
|
||||
uint_fast16_t copy_off;
|
||||
uint32_t copy_len;
|
||||
};
|
||||
} mscomp_xpress_decompress_state;
|
||||
|
||||
// This function checks that a number is of the form 1..10..0 - basically all 1 bits are more significant than all 0 bits (also allowed are all 1s and all 0s)
|
||||
static FORCE_INLINE bool set_bits_are_highest(uint32_t x) { x = ~x; return !((x+1) & x); }
|
||||
@@ -46,7 +46,7 @@ MSCompStatus xpress_inflate_init(mscomp_stream* stream)
|
||||
{
|
||||
INIT_STREAM(stream, false, MSCOMP_XPRESS);
|
||||
|
||||
mscomp_internal_state* state = (mscomp_internal_state*)malloc(sizeof(mscomp_internal_state));
|
||||
mscomp_xpress_decompress_state* state = (mscomp_xpress_decompress_state*)malloc(sizeof(mscomp_xpress_decompress_state));
|
||||
if (UNLIKELY(state == NULL)) { SET_ERROR(stream, "XPRESS Decompression Error: Unable to allocate state memory"); return MSCOMP_MEM_ERROR; }
|
||||
|
||||
state->flagged = 1;
|
||||
@@ -56,7 +56,7 @@ MSCompStatus xpress_inflate_init(mscomp_stream* stream)
|
||||
state->copy_len = 0;
|
||||
new (&state->buffer) Buffer();
|
||||
|
||||
stream->state = state;
|
||||
stream->state = (mscomp_internal_state*) state;
|
||||
return MSCOMP_OK;
|
||||
}
|
||||
#define _READ_SYMBOL(ERROR, LABEL) \
|
||||
@@ -248,7 +248,7 @@ ENTRY_POINT MSCompStatus xpress_inflate(mscomp_stream* stream)
|
||||
{
|
||||
CHECK_STREAM_PLUS(stream, false, MSCOMP_XPRESS, stream->state == NULL);
|
||||
|
||||
mscomp_internal_state *state = stream->state;
|
||||
mscomp_xpress_decompress_state *state = (mscomp_xpress_decompress_state*) stream->state;
|
||||
Buffer* const buf = &state->buffer;
|
||||
const bytes out_start = stream->out;
|
||||
|
||||
@@ -389,7 +389,7 @@ MSCompStatus xpress_inflate_end(mscomp_stream* stream)
|
||||
{
|
||||
CHECK_STREAM_PLUS(stream, false, MSCOMP_XPRESS, stream->state == NULL);
|
||||
|
||||
mscomp_internal_state* state = stream->state;
|
||||
mscomp_xpress_decompress_state* state = (mscomp_xpress_decompress_state*) stream->state;
|
||||
|
||||
MSCompStatus status = MSCOMP_OK;
|
||||
if (UNLIKELY(stream->in_avail || state->in_avail || !state->flagged || !set_bits_are_highest(state->flags))) { SET_ERROR(stream, "XPRESS Decompression Error: End prematurely called"); status = MSCOMP_DATA_ERROR; }
|
||||
|
||||
Reference in New Issue
Block a user