Skip to content

Commit 8b47811

Browse files
Move stream compression code to lib; fix max block size reduction bug
1 parent af0b1a5 commit 8b47811

File tree

12 files changed

+1099
-833
lines changed

12 files changed

+1099
-833
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ $(OBJDIR)/%.o: src/../%.c
1111
APP := lz4ultra
1212

1313
OBJS := $(OBJDIR)/src/lz4ultra.o
14-
OBJS += $(OBJDIR)/src/frame.o
1514
OBJS += $(OBJDIR)/src/lib.o
15+
OBJS += $(OBJDIR)/src/stream.o
16+
OBJS += $(OBJDIR)/src/frame.o
1617
OBJS += $(OBJDIR)/src/matchfinder.o
1718
OBJS += $(OBJDIR)/src/shrink.o
1819
OBJS += $(OBJDIR)/src/expand.o

VS2017/lz4ultra.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
<ClInclude Include="..\src\libdivsufsort\include\divsufsort_private.h" />
187187
<ClInclude Include="..\src\matchfinder.h" />
188188
<ClInclude Include="..\src\shrink.h" />
189+
<ClInclude Include="..\src\stream.h" />
189190
<ClInclude Include="..\src\xxhash\xxhash.h" />
190191
<ClInclude Include="pch.h" />
191192
</ItemGroup>
@@ -200,6 +201,7 @@
200201
<ClCompile Include="..\src\lz4ultra.c" />
201202
<ClCompile Include="..\src\matchfinder.c" />
202203
<ClCompile Include="..\src\shrink.c" />
204+
<ClCompile Include="..\src\stream.c" />
203205
<ClCompile Include="..\src\xxhash\xxhash.c" />
204206
</ItemGroup>
205207
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

VS2017/lz4ultra.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
<ClInclude Include="..\src\frame.h">
6161
<Filter>Fichiers sources</Filter>
6262
</ClInclude>
63+
<ClInclude Include="..\src\stream.h">
64+
<Filter>Fichiers sources</Filter>
65+
</ClInclude>
6366
</ItemGroup>
6467
<ItemGroup>
6568
<ClCompile Include="..\src\libdivsufsort\lib\divsufsort.c">
@@ -95,5 +98,8 @@
9598
<ClCompile Include="..\src\frame.c">
9699
<Filter>Fichiers sources</Filter>
97100
</ClCompile>
101+
<ClCompile Include="..\src\stream.c">
102+
<Filter>Fichiers sources</Filter>
103+
</ClCompile>
98104
</ItemGroup>
99105
</Project>

src/expand.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static inline FORCE_INLINE int lz4ultra_expand_match_slow(const unsigned char **
147147
*
148148
* @return size of decompressed data in bytes, or -1 for error
149149
*/
150-
int lz4ultra_expand_block_lz4(const unsigned char *pInBlock, int nBlockSize, unsigned char *pOutData, int nOutDataOffset, int nBlockMaxSize) {
150+
int lz4ultra_decompressor_expand_block_lz4(const unsigned char *pInBlock, int nBlockSize, unsigned char *pOutData, int nOutDataOffset, int nBlockMaxSize) {
151151
const unsigned char *pInBlockEnd = pInBlock + nBlockSize;
152152
const unsigned char *pInBlockFastEnd = pInBlock + nBlockSize - 16;
153153
unsigned char *pCurOutData = pOutData + nOutDataOffset;

src/expand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@
4444
*
4545
* @return size of decompressed data in bytes, or -1 for error
4646
*/
47-
int lz4ultra_expand_block_lz4(const unsigned char *pInBlock, int nBlockSize, unsigned char *pOutData, int nOutDataOffset, int nBlockMaxSize);
47+
int lz4ultra_decompressor_expand_block_lz4(const unsigned char *pInBlock, int nBlockSize, unsigned char *pOutData, int nOutDataOffset, int nBlockMaxSize);
4848

4949
#endif /* _EXPAND_H */

src/frame.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@
3939
* @param pFrameData encoding buffer
4040
* @param nMaxFrameDataSize max encoding buffer size, in bytes
4141
* @param nBlockMaxCode max block size code (4-7)
42-
* @param bIndependentBlocks true if the stream contains independently compressed blocks, false if blocks back-reference the previous block
42+
* @param nIsIndependentBlocks nonzero if the stream contains independently compressed blocks, 0 if blocks back-reference the previous block
4343
*
4444
* @return number of encoded bytes, or -1 for failure
4545
*/
46-
int lz4ultra_encode_header(unsigned char *pFrameData, const int nMaxFrameDataSize, int nBlockMaxCode, bool bIndependentBlocks) {
46+
int lz4ultra_encode_header(unsigned char *pFrameData, const int nMaxFrameDataSize, int nBlockMaxCode, int nIsIndependentBlocks) {
4747
if (nMaxFrameDataSize >= 7) {
4848
pFrameData[0] = 0x04; /* Magic number: 0x184D2204 */
4949
pFrameData[1] = 0x22;
5050
pFrameData[2] = 0x4D;
5151
pFrameData[3] = 0x18;
5252

5353
pFrameData[4] = 0b01000000; /* Version.Hi Version.Lo !B.Indep B.Checksum Content.Size Content.Checksum Reserved.Hi Reserved.Lo */
54-
if (bIndependentBlocks)
54+
if (nIsIndependentBlocks)
5555
pFrameData[4] |= 0b00100000; /* B.Indep */
5656
pFrameData[5] = nBlockMaxCode << 4; /* Block MaxSize */
5757

@@ -136,11 +136,11 @@ int lz4ultra_encode_footer_frame(unsigned char *pFrameData, const int nMaxFrameD
136136
* @param pFrameData data bytes
137137
* @param nFrameDataSize number of bytes to decode
138138
* @param nBlockMaxCode pointer to max block size code (4-7), updated if this function succeeds
139-
* @param bIndependentBlocks returned flag that indicates if the stream contains independently compressed blocks
139+
* @param nIsIndependentBlocks returned flag that indicates if the stream contains independently compressed blocks
140140
*
141141
* @return LZ4ULTRA_DECODE_OK for success, or LZ4ULTRA_DECODE_ERR_xxx for failure
142142
*/
143-
int lz4ultra_decode_header(const unsigned char *pFrameData, const int nFrameDataSize, int *nBlockMaxCode, bool *bIndependentBlocks) {
143+
int lz4ultra_decode_header(const unsigned char *pFrameData, const int nFrameDataSize, int *nBlockMaxCode, int *nIsIndependentBlocks) {
144144
if (nFrameDataSize == 7) {
145145
if (pFrameData[0] != 0x04 ||
146146
pFrameData[1] != 0x22 ||
@@ -156,7 +156,7 @@ int lz4ultra_decode_header(const unsigned char *pFrameData, const int nFrameData
156156
return LZ4ULTRA_DECODE_ERR_SUM;
157157
}
158158

159-
*bIndependentBlocks = (pFrameData[4] & 0x20) != 0;
159+
*nIsIndependentBlocks = (pFrameData[4] & 0x20) ? 1 : 0;
160160
*nBlockMaxCode = (pFrameData[5] >> 4);
161161

162162
return LZ4ULTRA_DECODE_OK;
@@ -176,14 +176,14 @@ int lz4ultra_decode_header(const unsigned char *pFrameData, const int nFrameData
176176
*
177177
* @return LZ4ULTRA_DECODE_OK for success, or LZ4ULTRA_DECODE_ERR_FORMAT for failure
178178
*/
179-
int lz4ultra_decode_frame(const unsigned char *pFrameData, const int nFrameDataSize, unsigned int *nBlockSize, bool *bIsUncompressed) {
179+
int lz4ultra_decode_frame(const unsigned char *pFrameData, const int nFrameDataSize, unsigned int *nBlockSize, int *nIsUncompressed) {
180180
if (nFrameDataSize == 4) {
181181
*nBlockSize = ((unsigned int)pFrameData[0]) |
182182
(((unsigned int)pFrameData[1]) << 8) |
183183
(((unsigned int)pFrameData[2]) << 16) |
184184
(((unsigned int)pFrameData[3]) << 24);
185185

186-
*bIsUncompressed = ((*nBlockSize) & 0x80000000) != 0;
186+
*nIsUncompressed = ((*nBlockSize) & 0x80000000) ? 1 : 0;
187187
(*nBlockSize) &= 0x7fffffff;
188188
return 0;
189189
}

src/frame.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#define _FRAME_H
3535

3636
#include <stdio.h>
37-
#include <stdbool.h>
3837

3938
#define LZ4ULTRA_HEADER_SIZE 7
4039
#define LZ4ULTRA_FRAME_SIZE 4
@@ -51,11 +50,11 @@
5150
* @param pFrameData encoding buffer
5251
* @param nMaxFrameDataSize max encoding buffer size, in bytes
5352
* @param nBlockMaxCode max block size code (4-7)
54-
* @param bIndependentBlocks true if the stream contains independently compressed blocks, false if blocks back-reference the previous block
53+
* @param nIsIndependentBlocks nonzero if the stream contains independently compressed blocks, 0 if blocks back-reference the previous block
5554
*
5655
* @return number of encoded bytes, or -1 for failure
5756
*/
58-
int lz4ultra_encode_header(unsigned char *pFrameData, const int nMaxFrameDataSize, int nBlockMaxCode, bool bIndependentBlocks);
57+
int lz4ultra_encode_header(unsigned char *pFrameData, const int nMaxFrameDataSize, int nBlockMaxCode, int nIsIndependentBlocks);
5958

6059
/**
6160
* Encode compressed block frame header
@@ -95,11 +94,11 @@ int lz4ultra_encode_footer_frame(unsigned char *pFrameData, const int nMaxFrameD
9594
* @param pFrameData data bytes
9695
* @param nFrameDataSize number of bytes to decode
9796
* @param nBlockMaxCode pointer to max block size code (4-7), updated if this function succeeds
98-
* @param bIndependentBlocks returned flag that indicates if the stream contains independently compressed blocks
97+
* @param nIsIndependentBlocks returned flag that indicates if the stream contains independently compressed blocks
9998
*
10099
* @return LZ4ULTRA_DECODE_OK for success, or LZ4ULTRA_DECODE_ERR_xxx for failure
101100
*/
102-
int lz4ultra_decode_header(const unsigned char *pFrameData, const int nFrameDataSize, int *nBlockMaxCode, bool *bIndependentBlocks);
101+
int lz4ultra_decode_header(const unsigned char *pFrameData, const int nFrameDataSize, int *nBlockMaxCode, int *nIsIndependentBlocks);
103102

104103
/**
105104
* Decode frame header
@@ -111,6 +110,6 @@ int lz4ultra_decode_header(const unsigned char *pFrameData, const int nFrameData
111110
*
112111
* @return LZ4ULTRA_DECODE_OK for success, or LZ4ULTRA_DECODE_ERR_FORMAT for failure
113112
*/
114-
int lz4ultra_decode_frame(const unsigned char *pFrameData, const int nFrameDataSize, unsigned int *nBlockSize, bool *bIsUncompressed);
113+
int lz4ultra_decode_frame(const unsigned char *pFrameData, const int nFrameDataSize, unsigned int *nBlockSize, int *nIsUncompressed);
115114

116115
#endif /* _FRAME_H */

0 commit comments

Comments
 (0)