8000 Avoid ODR violations. · coderforlife/ms-compress@778e8bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 778e8bb

Browse files
committed
Avoid ODR violations.
GCC 5.2 with LTO will emit warnings when linking due to ODR violations which occur across translation units.
1 parent 73ce174 commit 778e8bb

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

src/lznt1_compress.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626

2727
size_t lznt1_max_compressed_size(size_t in_len) { return in_len + 3 + 2 * ((in_len + CHUNK_SIZE - 1) / CHUNK_SIZE); }
2828

29-
struct _mscomp_internal_state
29+
typedef struct
3030
{ // 8,218 - 8,238 bytes (+padding) + dictionary memory
3131
bool finished; // means fully finished
3232
LZNT1Dictionary d;
3333
byte in[CHUNK_SIZE];
3434
size_t in_needed, in_avail;
3535
byte out[CHUNK_SIZE+2];
3636
size_t out_pos, out_avail;
37-
};
37+
} mscomp_lznt1_compress_state;
3838

3939
#ifdef MSCOMP_WITH_LZNT1_SA_DICT
4040
#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
9494
}
9595
static bool lznt1_compress_chunk_write(mscomp_stream* RESTRICT const stream, const_rest_bytes const in, const uint_fast16_t in_len)
9696
{
97-
mscomp_internal_state* RESTRICT state = stream->state;
97+
mscomp_lznt1_compress_state* RESTRICT state = (mscomp_lznt1_compress_state*) stream->state;
9898
bool out_buffering = stream->out_avail < in_len+2u;
9999
rest_bytes out = out_buffering ? state->out : stream->out;
100100

@@ -137,7 +137,7 @@ MSCompStatus lznt1_deflate_init(mscomp_stream* RESTRICT const stream)
137137
{
138138
INIT_STREAM(stream, true, MSCOMP_LZNT1);
139139

140-
mscomp_internal_state* RESTRICT state = (mscomp_internal_state*)malloc(sizeof(mscomp_internal_state));
140+
mscomp_lznt1_compress_state* RESTRICT state = (mscomp_lznt1_compress_state*)malloc(sizeof(mscomp_lznt1_compress_state));
141141
if (UNLIKELY(state == NULL)) { SET_ERROR(stream, "LZNT1 Compression Error: Unable to allocate buffer memory"); return MSCOMP_MEM_ERROR; }
142142
state->finished = false;
143143
state->in_needed = 0;
@@ -146,14 +146,14 @@ MSCompStatus lznt1_deflate_init(mscomp_stream* RESTRICT const stream)
146146
state->out_avail = 0;
147147
new (&state->d) LZNT1Dictionary();
148148

149-
stream->state = state;
149+
stream->state = (mscomp_internal_state*) state;
150150
return MSCOMP_OK;
151151
}
152152
ENTRY_POINT MSCompStatus lznt1_deflate(mscomp_stream* RESTRICT const stream, const MSCompFlush flush)
153153
{
154-
CHECK_STREAM_PLUS(stream, true, MSCOMP_LZNT1, stream->state == NULL || stream->state->finished);
154+
mscomp_lznt1_compress_state* RESTRICT state = (mscomp_lznt1_compress_state*) stream->state;
155155

156-
mscomp_internal_state* RESTRICT state = stream->state;
156+
CHECK_STREAM_PLUS(stream, true, MSCOMP_LZNT1, state == NULL || state->finished);
157157

158158
DUMP_OUT(state, stream);
159159
APPEND_IN(state, stream,
@@ -217,7 +217,7 @@ MSCompStatus lznt1_deflate_end(mscomp_stream* RESTRICT stream)
217217
{
218218
CHECK_STREAM_PLUS(stream, true, MSCOMP_LZNT1, stream->state == NULL);
219219

220-
mscomp_internal_state* RESTRICT state = stream->state;
220+
mscomp_lznt1_compress_state* RESTRICT state = (mscomp_lznt1_compress_state*) stream->state;
221221

222222
MSCompStatus status = MSCOMP_OK;
223223
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
262262
out_pos += out_size+2;
263263
in_pos += in_size;
264264
}
265-
265+
266266
// Return insufficient buffer or the compressed size
267267
if (UNLIKELY(in_pos < in_len)) { return MSCOMP_BUF_ERROR; }
268268
// https://msdn.microsoft.com/library/jj679084.aspx: If an End_of_buffer terminal is added, the

src/lznt1_decompress.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323

2424
#define CHUNK_SIZE 0x1000 // to be compatible with all known forms of Windows
2525

26-
struct _mscomp_internal_state
26+
typedef struct
2727
{ // 8,214 - 8,230 bytes (+padding)
2828
bool end_of_stream;
2929
byte in[CHUNK_SIZE+2];
3030
size_t in_needed, in_avail;
3131
byte out[CHUNK_SIZE];
3232
size_t out_pos, out_avail;
33-
};
33+
} mscomp_lznt1_decompress_state;
3434

3535

3636
/////////////////// Decompression Functions ///////////////////////////////////
@@ -39,7 +39,7 @@ static MSCompStatus lznt1_decompress_chunk(const_rest_bytes in, const const_byte
3939
const const_bytes in_endx = in_end -0x11; // 1 + 8 * 2 from the end
4040
const const_bytes out_start = out, out_endx = out_end-8*FAST_COPY_ROOM;
4141
byte flags, flagged;
42-
42+
4343
uint_fast16_t pow2 = 0x10, mask = 0xFFF, shift = 12;
4444
const_bytes pow2_target = out_start + 0x10;
4545
uint_fast16_t len, off;
@@ -72,7 +72,7 @@ static MSCompStatus lznt1_decompress_chunk(const_rest_bytes in, const const_byte
7272
flags >>= 1;
7373
} while (LIKELY(flags));
7474
}
75-
75+
7676
// Slower decompression but with full bounds checking
7777
while (LIKELY(in < in_end))
7878
{
@@ -121,7 +121,7 @@ CHECKED_COPY: for (end = out + len; out < end; ++out) { *out = *(out-off); }
121121
}
122122
MSCompStatus lznt1_decompress_chunk_read(mscomp_stream* RESTRICT const stream, const_rest_bytes const in, size_t* RESTRICT const in_len)
123123
{
124-
mscomp_internal_state* RESTRICT state = stream->state;
124+
mscomp_lznt1_decompress_state* RESTRICT state = (mscomp_lznt1_decompress_state*) stream->state;
125125

126126
// Read chunk header
127127
const uint16_t header = GET_UINT16(in);
@@ -211,22 +211,22 @@ MSCompStatus lznt1_inflate_init(mscomp_stream* RESTRICT stream)
211211
{
212212
INIT_STREAM(stream, false, MSCOMP_LZNT1);
213213

214-
mscomp_internal_state* RESTRICT state = (mscomp_internal_state*)malloc(sizeof(mscomp_internal_state));
214+
mscomp_lznt1_decompress_state* RESTRICT state = (mscomp_lznt1_decompress_state*)malloc(sizeof(mscomp_lznt1_decompress_state));
215215
if (UNLIKELY(state == NULL)) { SET_ERROR(stream, "LZNT1 Decompression Error: Unable to allocate buffer memory"); return MSCOMP_MEM_ERROR; }
216216
state->end_of_stream = false;
217217
state->in_needed = 0;
218218
state->in_avail = 0;
219219
state->out_pos = 0;
220220
state->out_avail = 0;
221221

222-
stream->state = state;
222+
stream->state = (mscomp_internal_state*) state;
223223
return MSCOMP_OK;
224224
}
225225
ENTRY_POINT MSCompStatus lznt1_inflate(mscomp_stream* RESTRICT stream)
226226
{
227227
CHECK_STREAM_PLUS(stream, false, MSCOMP_LZNT1, stream->state == NULL);
228228

229-
mscomp_internal_state* RESTRICT state = stream->state;
229+
mscomp_lznt1_decompress_state* RESTRICT state = (mscomp_lznt1_decompress_state*) stream->state;
230230

231231
DUMP_OUT(state, stream);
232232
if (UNLIKELY(state->end_of_stream))
@@ -244,7 +244,7 @@ ENTRY_POINT MSCompStatus lznt1_inflate(mscomp_stream* RESTRICT stream)
244244
else if (UNLIKELY(state->end_of_stream)) { return MSCOMP_STREAM_END; }
245245
else if (state->in_needed) { continue; } // we only had enough to read the header this time
246246
);
247-
247+
248248
// Decompress full chunks while there is room in the output buffer
249249
while (stream->out_avail && stream->in_avail >= 2)
250250
{
@@ -270,7 +270,7 @@ MSCompStatus lznt1_inflate_end(mscomp_stream* RESTRICT stream)
270270
{
271271
CHECK_STREAM_PLUS(stream, false, MSCOMP_LZNT1, stream->state == NULL);
272272

273-
mscomp_internal_state* RESTRICT state = stream->state;
273+
mscomp_lznt1_decompress_state* RESTRICT state = (mscomp_lznt1_decompress_state*) stream->state;
274274

275275
MSCompStatus status = MSCOMP_OK;
276276
if (UNLIKELY(stream->in_avail || state->in_avail || state->out_avail)) { SET_ERROR(stream, "LZNT1 Decompression Error: End prematurely called"); status = MSCOMP_DATA_ERROR; }

src/xpress_compress.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ typedef XpressDictionary<0x2000> Dictionary;
2929

3030
size_t xpress_max_compressed_size(size_t in_len) { return in_len + 4 + 4 * (in_len / 32); }
3131

32-
struct _mscomp_internal_state
32+
typedef struct
3333
{ // ?-? bytes
3434
bool finished;
35-
35+
3636
uint32_t flags, *out_flags;
3737
byte flag_count;
3838
byte* half_byte;
39-
39+
4040
//byte in[10];
4141
//size_t in_avail;
4242

4343
byte out[10];
4444
size_t out_avail;
45-
};
45+
} mscomp_xpress_compress_state;
4646

4747
////////////////////////////// Compression Functions ///////////////////////////////////////////////
4848
#define PRINT_ERROR(...) // TODO: remove
@@ -52,7 +52,7 @@ MSCompStatus xpress_deflate_init(mscomp_stream* stream)
5252
#ifdef _DEBUG
5353
INIT_STREAM(stream, true, MSCOMP_XPRESS);
5454

55-
mscomp_internal_state* state = (mscomp_internal_state*)malloc(sizeof(mscomp_internal_state));
55+
mscomp_xpress_compress_state* state = (mscomp_xpress_compress_state*)malloc(sizeof(mscomp_xpress_compress_state));
5656
if (UNLIKELY(state == NULL)) { SET_ERROR(stream, "XPRESS Compression Error: Unable to allocate buffer memory"); return MSCOMP_MEM_ERROR; }
5757
state->finished = false;
5858
state->flags = 0;
@@ -83,7 +83,7 @@ ENTRY_POINT MSCompStatus xpress_deflate(mscomp_stream* stream, MSCompFlush flush
8383
#ifdef _XDEBUG
8484
CHECK_STREAM_PLUS(stream, true, MSCOMP_XPRESS, stream->state == NULL || stream->state->finished);
8585

86-
mscomp_internal_state *state = stream->state;
86+
mscomp_xpress_compress_state *state = (mscomp_xpress_compress_state*) stream->state;
8787

8888
const size_t out_avail = stream->out_avail;
8989
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
247247
uint32_t flags = 0, *out_flags = (uint32_t*)out;
248248
byte flag_count;
249249
byte* half_byte = NULL;
250-
250+
251251
Dictionary d(in, in_end);
252252

253253
if (in_len == 0)

src/xpress_decompress.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "../include/mscomp/CircularBuffer.h"
2626
typedef CircularBuffer<0x2000> Buffer;
2727

28-
struct _mscomp_internal_state
28+
typedef struct
2929
{ // 38-42 bytes (+padding) + buffer
3030
uint32_t flagged, flags;
3131
byte half_byte;
@@ -35,7 +35,7 @@ struct _mscomp_internal_state
3535
Buffer buffer;
3636
uint_fast16_t copy_off;
3737
uint32_t copy_len;
38-
};
38+
} mscomp_xpress_decompress_state;
3939

4040
// 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)
4141
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)
4646
{
4747
INIT_STREAM(stream, false, MSCOMP_XPRESS);
4848

49-
mscomp_internal_state* state = (mscomp_internal_state*)malloc(sizeof(mscomp_internal_state));
49+
mscomp_xpress_decompress_state* state = (mscomp_xpress_decompress_state*)malloc(sizeof(mscomp_xpress_decompress_state));
5050
if (UNLIKELY(state == NULL)) { SET_ERROR(stream, "XPRESS Decompression Error: Unable to allocate state memory"); return MSCOMP_MEM_ERROR; }
5151

5252
state->flagged = 1;
@@ -56,7 +56,7 @@ MSCompStatus xpress_inflate_init(mscomp_stream* stream)
5656
state->copy_len = 0;
5757
new (&state->buffer) Buffer();
5858

59-
stream->state = state;
59+
stream->state = (mscomp_internal_state*) state;
6060
return MSCOMP_OK;
6161
}
6262
#define _READ_SYMBOL(ERROR, LABEL) \
@@ -248,7 +248,7 @@ ENTRY_POINT MSCompStatus xpress_inflate(mscomp_stream* stream)
248248
{
249249
CHECK_STREAM_PLUS(stream, false, MSCOMP_XPRESS, stream->state == NULL);
250250

251-
mscomp_internal_state *state = stream->state;
251+
mscomp_xpress_decompress_state *state = (mscomp_xpress_decompress_state*) stream->state;
252252
Buffer* const buf = &state->buffer;
253253
const bytes out_start = stream->out;
254254

@@ -389,7 +389,7 @@ MSCompStatus xpress_inflate_end(mscomp_stream* stream)
389389
{
390390
CHECK_STREAM_PLUS(stream, false, MSCOMP_XPRESS, stream->state == NULL);
391391

392-
mscomp_internal_state* state = stream->state;
392+
mscomp_xpress_decompress_state* state = (mscomp_xpress_decompress_state*) stream->state;
393393

394394
MSCompStatus status = MSCOMP_OK;
395395
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; }

0 commit comments

Comments
 (0)
0