Skip to content

Commit 60ba940

Browse files
committed
Complete reorganization of code to support the compression code to be built as a DLL/LIB and the test program to use that as necessary. Migration not entirely complete as the test program cannot use the static LIB version.
1 parent 05c43a0 commit 60ba940

32 files changed

+807
-266
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
*.lznt1
22
*.xpress
33
*.xpress_huff
4+
x86
5+
x64
46

57
# Compiled Object files
68
*.slo

build-c++.bat

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
:: Make sure both mingw-w32\bin and mingw-w64\bin are in the PATH
55

66
::-Werror
7-
set FLAGS=-mconsole -static-libgcc -static-libstdc++ -O3 -march=core2 -Wall -s
8-
set FILES=compression.cpp Dictionary.cpp Bitstream.cpp lznt1.cpp lzx.cpp xpress.cpp xpress_huff.cpp test.cpp
9-
set OUT=compression
7+
set FLAGS=-mconsole -static-libgcc -static-libstdc++ -O3 -march=core2 -Wall -s -Ilibrary
8+
set FILES=test.cpp
9+
set OUT=test
1010

1111
echo Compiling 32-bit...
1212
i686-w64-mingw32-g++ %FLAGS% %FILES% -o %OUT%.exe

build-c.bat

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
:: This builds using MinGW-w64 for 32 and 64 bit (http://mingw-w64.sourceforge.net/)
44
:: Make sure both mingw-w32\bin and mingw-w64\bin are in the PATH
55

6+
::-Werror
7+
set FLAGS=-mconsole -static-libgcc -O3 -march=core2 -Wall -s -Ilibrary
8+
set FILES=test.cpp
9+
set OUT=test
10+
611
echo Compiling 32-bit...
7-
i686-w64-mingw32-gcc -mconsole -static-libgcc -O3 -march=core2 -o compression.exe -s ^
8-
compression.c Dictionary.c Bitstream.c lznt1.c lzx.c xpress.c xpress_huff.c test.c ^
12+
i686-w64-mingw32-gcc %FLAGS% %FILES% -o %OUT%.exe
913

1014
echo.
1115

1216
echo Compiling 64-bit...
1317

14-
x86_64-w64-mingw32-gcc -mconsole -static-libgcc -O3 -march=core2 -o compression64.exe -s ^
15-
compression.c Dictionary.c Bitstream.c lznt1.c lzx.c xpress.c xpress_huff.c test.c
18+
x86_64-w64-mingw32-gcc %FLAGS% %FILES% -o %OUT%64.exe
1619

1720
pause

compression.sln

-26
This file was deleted.

Bitstream.cpp renamed to library/Bitstream.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
#include "stdafx.h"
21
#include "Bitstream.h"
32

3+
#ifdef __cplusplus_cli
4+
#pragma unmanaged
5+
#endif
6+
7+
#if defined(_MSC_VER) && defined(NDEBUG)
8+
#pragma optimize("t", on)
9+
#endif
10+
411

512
// Reading functions:
613
void BSReadInit(InputBitstream* bstr, const_bytes in, size_t len)

Bitstream.h renamed to library/Bitstream.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
#pragma once
2-
31
////////////////////////////// Bitstreams //////////////////////////////////////////////////////////
42
// A bitstream that allows either reading or writing, but not both at the same time.
53
// It reads uint16s for bits and 16 bits can be reliably read at a time
4+
5+
#ifndef BITSTREAM_H
6+
#define BITSTREAM_H
7+
#include "compression-api.h"
8+
69
struct _Bitstream
710
{
811
union
@@ -32,3 +35,5 @@ void BSWriteInit(OutputBitstream* bstr, bytes out, size_t len);
3235
bool BSWriteBits(OutputBitstream* bstr, uint32_t b, byte n);
3336
bool BSWriteByte(OutputBitstream* bstr, byte b);
3437
void BSWriteFinish(OutputBitstream* bstr);
38+
39+
#endif

Dictionary.cpp renamed to library/Dictionary.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "stdafx.h"
21
#include "Dictionary.h"
32

43
// Implementation designed for being extremely fast at the expense of memory
@@ -13,7 +12,9 @@
1312
#pragma unmanaged
1413
#endif
1514

15+
#if defined(_MSC_VER) && defined(NDEBUG)
1616
#pragma optimize("t", on)
17+
#endif
1718

1819
#define MAX_BYTE 0x100 // maximum byte value (+1 for 0)
1920
#define MIN(a, b) (((a) < (b)) ? (a) : (b)) // minimum of 2 values

Dictionary.h renamed to library/Dictionary.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#pragma once
2-
31
/////////////////// Dictionary /////////////////////////////////////////////////
42
// The dictionary system used for LZNT1 and XPRESS compression.
53
//
@@ -9,6 +7,10 @@
97
// completely agnostic to it and any of the function implementations.
108

119

10+
#ifndef DICTIONARY_H
11+
#define DICTIONARY_H
12+
#include "compression-api.h"
13+
1214
struct _Dictionary;
1315
typedef struct _Dictionary Dictionary;
1416

@@ -35,3 +37,5 @@ bool Dictionary_Add(Dictionary* d, const_bytes data, const size_t max_len);
3537
// Returns the length of the string found, or 0 if nothing of length >= 3 was found
3638
// offset is set to the offset from the current position to the string
3739
uint_fast16_t Dictionary_Find(const Dictionary* d, const Dictionary* d2, const_bytes data, const uint_fast16_t max_len, const_bytes search, uint_fast16_t* offset);
40+
41+
#endif

library/build-c++.bat

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@echo off
2+
3+
:: This builds using MinGW-w64 for 32 and 64 bit (http://mingw-w64.sourceforge.net/)
4+
:: Make sure both mingw-w32\bin and mingw-w64\bin are in the PATH
5+
6+
::-Werror
7+
set FLAGS=-static-libgcc -static-libstdc++ -O3 -march=core2 -Wall -s -D UNICODE -D _UNICODE -D COMPRESSION_API_EXPORT
8+
set FILES=compression.cpp Dictionary.cpp Bitstream.cpp lznt1.cpp lzx.cpp xpress.cpp xpress_huff.cpp
9+
set OUT=MSCompression
10+
11+
echo Compiling 32-bit...
12+
i686-w64-mingw32-g++ %FLAGS% -D COMPRESSION_API_DLL -shared %FILES% -o %OUT%.dll -Wl,--out-implib,lib%OUT%.dll.a
13+
14+
i686-w64-mingw32-g++ %FLAGS% -D COMPRESSION_API_LIB -c %FILES%
15+
i686-w64-mingw32-ar rcs lib%OUT%.a *.o
16+
del /F /Q *.o >NUL 2>&1
17+
18+
echo.
19+
20+
echo Compiling 64-bit...
21+
22+
x86_64-w64-mingw32-g++ %FLAGS% -D COMPRESSION_API_DLL -shared %FILES% -o %OUT%64.dll -Wl,--out-implib,lib%OUT%64.dll.a
23+
24+
x86_64-w64-mingw32-g++ %FLAGS% -D COMPRESSION_API_LIB -c %FILES%
25+
x86_64-w64-mingw32-ar rcs lib%OUT%64.a *.o
26+
del /F /Q *.o >NUL 2>&1
27+
28+
pause

library/build-c.bat

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@echo off
2+
3+
:: This builds using MinGW-w64 for 32 and 64 bit (http://mingw-w64.sourceforge.net/)
4+
:: Make sure both mingw-w32\bin and mingw-w64\bin are in the PATH
5+
6+
::-Werror
7+
set FLAGS=-static-libgcc -O3 -march=core2 -Wall -s -D UNICODE -D _UNICODE -D COMPRESSION_API_EXPORT
8+
set FILES=compression.cpp Dictionary.cpp Bitstream.cpp lznt1.cpp lzx.cpp xpress.cpp xpress_huff.cpp
9+
set OUT=MSCompression
10+
11+
echo Compiling 32-bit...
12+
i686-w64-mingw32-gcc %FLAGS% -D COMPRESSION_API_DLL -shared %FILES% -o %OUT%.dll -Wl,--out-implib,lib%OUT%.dll.a
13+
14+
i686-w64-mingw32-gcc %FLAGS% -D COMPRESSION_API_LIB -c %FILES%
15+
i686-w64-mingw32-ar rcs lib%OUT%.a *.o
16+
del /F /Q *.o >NUL 2>&1
17+
18+
echo.
19+
20+
echo Compiling 64-bit...
21+
22+
x86_64-w64-mingw32-gcc %FLAGS% -D COMPRESSION_API_DLL -shared %FILES% -o %OUT%64.dll -Wl,--out-implib,lib%OUT%64.dll.a
23+
24+
x86_64-w64-mingw32-gcc %FLAGS% -D COMPRESSION_API_LIB -c %FILES%
25+
x86_64-w64-mingw32-ar rcs lib%OUT%64.a *.o
26+
del /F /Q *.o >NUL 2>&1
27+
28+
pause

library/compression-api.h

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#ifndef COMPRESSION_API_H
2+
#define COMPRESSION_API_H
3+
4+
#ifdef COMPRESSION_API_EXPORT
5+
6+
// For MSC
7+
#define _CRT_SECURE_NO_WARNINGS
8+
#define _CRT_NON_CONFORMING_SWPRINTFS
9+
10+
// For GCC
11+
#define __STDC_LIMIT_MACROS
12+
13+
#include <stdlib.h>
14+
#include <string.h>
15+
#include <stdint.h>
16+
#include <assert.h>
17+
#include <errno.h>
18+
19+
// Check that it is 8 bits to the byte
20+
#if CHAR_BIT != 8
21+
#error Unsupported char size
22+
#endif
23+
24+
// Determine the endianness of the compilation, however this isn't very accurate
25+
// It would be much better to define LITTLE_ENDIAN, BIG_ENDIAN, or PDP_ENDIAN yourself
26+
// LITTLE_ENDIAN is what the program is developed for and tested with
27+
// BIG_ENDIAN and PDP_ENDIAN are untested
28+
#if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(PDP_ENDIAN) && !defined(_MSC_VER) && !defined(_WIN32) && !defined(__LITTLE_ENDIAN__) && !defined(__IEEE_LITTLE_ENDIAN)
29+
#if defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) || defined(__IEEE_BIG_ENDIAN)
30+
#define BIG_ENDIAN
31+
#else
32+
#include <endian.h>
33+
#if (defined(__PDP_ENDIAN) && !defined(__LITTLE_ENDIAN)) || __BYTE_ORDER == __PDP_ENDIAN
34+
#define PDP_ENDIAN
35+
#elif (defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)) || __BYTE_ORDER == __BIG_ENDIAN
36+
#define BIG_ENDIAN
37+
#endif
38+
#endif
39+
#endif
40+
41+
#if defined(BIG_ENDIAN)
42+
#define GET_UINT16(x) ((x)[0]|((x)[1]<<8))
43+
#define GET_UINT32(x) ((x)[0]|((x)[1]<<8)|((x)[2]<<16)|((x)[3]<<24))
44+
#define SET_UINT16(x,val) (((byte*)(x))[0]=(byte)(val), ((byte*)(x))[1]=(byte)((val) >> 8))
45+
#define SET_UINT32(x,val) (((byte*)(x))[0]=(byte)(val), ((byte*)(x))[1]=(byte)((val) >> 8), ((byte*)(x))[2]=(byte)((val) >> 16), ((byte*)(x))[3]=(byte)((val) >> 24))
46+
#elif defined(PDP_ENDIAN) // for 16-bit ints its the same as little-endian
47+
#define GET_UINT16(x) (*(const uint16_t*)(x))
48+
#define GET_UINT32(x) (*(const uint16_t*)(x)|(*(const uint16_t*)((x)+2)<<16))
49+
#define SET_UINT16(x,val) (*(uint16_t*)(x) = (uint16_t)(val))
50+
#define SET_UINT32(x,val) (*(uint16_t*)(x) = (uint16_t)(val), *(((uint16_t*)(x))+1) = (uint16_t)((val) >> 16))
51+
#else
52+
#ifndef LITTLE_ENDIAN
53+
#define LITTLE_ENDIAN
54+
#endif
55+
#define GET_UINT16(x) (*(const uint16_t*)(x))
56+
#define GET_UINT32(x) (*(const uint32_t*)(x))
57+
#define SET_UINT16(x,val) (*(uint16_t*)(x) = (uint16_t)(val))
58+
#define SET_UINT32(x,val) (*(uint32_t*)(x) = (uint32_t)(val))
59+
#endif
60+
61+
// Determine the number of bits used by pointers
62+
#ifndef PNTR_BITS
63+
#if SIZE_MAX == UINT64_MAX
64+
#define PNTR_BITS 64
65+
#elif SIZE_MAX == UINT32_MAX
66+
#define PNTR_BITS 32
67+
#elif SIZE_MAX == UINT16_MAX
68+
#define PNTR_BITS 16
69+
#else
70+
#error You must define PNTR_BITS to be the number of bits used for pointers
71+
#endif
72+
#endif
73+
74+
// Get ROTL function
75+
#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64))
76+
#include <stdlib.h>
77+
#pragma intrinsic(_rotl)
78+
#define ROTL(x, bits) _rotl(x, bits)
79+
#else
80+
#define ROTL(x, bits) (((x) << (bits)) | ((x) >> (sizeof(x) - (bits))))
81+
#endif
82+
83+
84+
// Error message system (mainly for debugging)
85+
#ifdef _DEBUG
86+
#define PRINT_WARNINGS
87+
#define PRINT_ERRORS
88+
#endif
89+
90+
#if defined(PRINT_WARNINGS) || defined(PRINT_ERRORS)
91+
#include <stdio.h>
92+
#endif
93+
94+
#ifdef PRINT_WARNINGS
95+
#define PRINT_WARNING(...) fprintf(stderr, __VA_ARGS__)
96+
#else
97+
#define PRINT_WARNING(...)
98+
#endif
99+
100+
#ifdef PRINT_ERRORS
101+
#define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)
102+
#else
103+
#define PRINT_ERROR(...)
104+
#endif
105+
106+
// If compiling as C than make sure we have access to some C++ keywords
107+
#ifndef __cplusplus
108+
typedef uint_fast8_t bool;
109+
#define true 1
110+
#define false 0
111+
#define inline __inline
112+
#endif
113+
114+
// DLL linkage
115+
#ifdef COMPRESSION_API_DLL
116+
#define COMPAPI __declspec(dllexport)
117+
#else
118+
#define COMPAPI
119+
#endif
120+
121+
122+
#else // Importing from DLL or LIB
123+
124+
#include <stdint.h>
125+
#ifdef COMPRESSION_API_DLL
126+
#define COMPAPI __declspec(dllimport)
127+
#else
128+
#define COMPAPI
129+
#endif
130+
131+
#endif
132+
133+
#ifndef __cplusplus
134+
#define EXTERN_C
135+
#else
136+
#define EXTERN_C extern "C"
137+
#endif
138+
139+
// Custom errno values
140+
#define E_ILLEGAL_FORMAT 0x101
141+
#define E_INSUFFICIENT_BUFFER 0x102
142+
#define E_INVALID_DATA 0x103
143+
144+
// Define types used
145+
typedef uint8_t byte;
146+
typedef byte* bytes;
147+
typedef const byte const_byte;
148+
typedef const_byte* const_bytes;
149+
150+
#endif

compression.cpp renamed to library/compression.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
#include "stdafx.h"
21
#include "compression.h"
32

43
#include "lznt1.h"
54
#include "lzx.h"
65
#include "xpress.h"
76
#include "xpress_huff.h"
87

8+
#ifdef __cplusplus_cli
9+
#pragma unmanaged
10+
#endif
11+
912
#ifndef ARRAYSIZE
1013
#define ARRAYSIZE(x) sizeof(x)/sizeof(x[0])
1114
#endif
@@ -42,7 +45,7 @@ static compress_func decompressors[] =
4245
xpress_huff_decompress,
4346
};
4447

45-
size_t compress(int format, const_bytes in, size_t in_len, bytes out, size_t out_len)
48+
size_t compress(CompressionFormat format, const_bytes in, size_t in_len, bytes out, size_t out_len)
4649
{
4750
if (format >= ARRAYSIZE(compressors) || !compressors[format])
4851
{
@@ -53,7 +56,7 @@ size_t compress(int format, const_bytes in, size_t in_len, bytes out, size_t out
5356
return compressors[format](in, in_len, out, out_len);
5457
}
5558

56-
size_t decompress(int format, const_bytes in, size_t in_len, bytes out, size_t out_len)
59+
size_t decompress(CompressionFormat format, const_bytes in, size_t in_len, bytes out, size_t out_len)
5760
{
5861
if (format >= ARRAYSIZE(decompressors) || !decompressors[format])
5962
{

0 commit comments

Comments
 (0)