minor lz4frame refactoring

This commit is contained in:
Yann Collet 2015-03-16 22:35:02 +01:00
parent 859fe3bb1d
commit da9402c6f5
3 changed files with 30 additions and 41 deletions

View File

@ -45,7 +45,7 @@ extern "C" {
*/ */
/************************************** /**************************************
Version * Version
**************************************/ **************************************/
#define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
#define LZ4_VERSION_MINOR 5 /* for new (non-breaking) interface capabilities */ #define LZ4_VERSION_MINOR 5 /* for new (non-breaking) interface capabilities */
@ -54,7 +54,7 @@ extern "C" {
int LZ4_versionNumber (void); int LZ4_versionNumber (void);
/************************************** /**************************************
Tuning parameter * Tuning parameter
**************************************/ **************************************/
/* /*
* LZ4_MEMORY_USAGE : * LZ4_MEMORY_USAGE :
@ -67,7 +67,7 @@ int LZ4_versionNumber (void);
/************************************** /**************************************
Simple Functions * Simple Functions
**************************************/ **************************************/
int LZ4_compress (const char* source, char* dest, int sourceSize); int LZ4_compress (const char* source, char* dest, int sourceSize);
@ -96,7 +96,7 @@ LZ4_decompress_safe() :
/************************************** /**************************************
Advanced Functions * Advanced Functions
**************************************/ **************************************/
#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */ #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
#define LZ4_COMPRESSBOUND(isize) ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16) #define LZ4_COMPRESSBOUND(isize) ((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
@ -170,7 +170,7 @@ int LZ4_decompress_safe_partial (const char* source, char* dest, int compressedS
/*********************************************** /***********************************************
Streaming Compression Functions * Streaming Compression Functions
***********************************************/ ***********************************************/
#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4) #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4)
@ -235,7 +235,7 @@ int LZ4_saveDict (LZ4_stream_t* LZ4_streamPtr, char* safeBuffer, int dictSize);
/************************************************ /************************************************
Streaming Decompression Functions * Streaming Decompression Functions
************************************************/ ************************************************/
#define LZ4_STREAMDECODESIZE_U64 4 #define LZ4_STREAMDECODESIZE_U64 4
@ -286,7 +286,7 @@ int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalS
/************************************** /**************************************
Obsolete Functions * Obsolete Functions
**************************************/ **************************************/
/* /*
Obsolete decompression functions Obsolete decompression functions

View File

@ -208,6 +208,21 @@ static BYTE LZ4F_headerChecksum (const BYTE* header, size_t length)
/************************************** /**************************************
* Simple compression functions * Simple compression functions
**************************************/ **************************************/
static blockSizeID_t LZ4F_optimalBSID(const blockSizeID_t requestedBSID, const size_t srcSize)
{
blockSizeID_t proposedBSID = max64KB;
size_t maxBlockSize = 64 KB;
while (requestedBSID > proposedBSID)
{
if (srcSize <= maxBlockSize)
return proposedBSID;
proposedBSID = (blockSizeID_t)((int)proposedBSID + 1);
maxBlockSize <<= 2;
}
return requestedBSID;
}
size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr) size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* preferencesPtr)
{ {
LZ4F_preferences_t prefs; LZ4F_preferences_t prefs;
@ -217,20 +232,7 @@ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* prefere
if (preferencesPtr!=NULL) prefs = *preferencesPtr; if (preferencesPtr!=NULL) prefs = *preferencesPtr;
else memset(&prefs, 0, sizeof(prefs)); else memset(&prefs, 0, sizeof(prefs));
{ prefs.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize);
blockSizeID_t proposedBSID = max64KB;
size_t maxBlockSize = 64 KB;
while (prefs.frameInfo.blockSizeID > proposedBSID)
{
if (srcSize <= maxBlockSize)
{
prefs.frameInfo.blockSizeID = proposedBSID;
break;
}
proposedBSID = (blockSizeID_t)( ((int)proposedBSID) + 1);
maxBlockSize <<= 2;
}
}
prefs.autoFlush = 1; prefs.autoFlush = 1;
headerSize = 7; /* basic header size (no option) including magic number */ headerSize = 7; /* basic header size (no option) including magic number */
@ -241,11 +243,11 @@ size_t LZ4F_compressFrameBound(size_t srcSize, const LZ4F_preferences_t* prefere
/* LZ4F_compressFrame() /* LZ4F_compressFrame()
* Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.4.1, in a single step. * Compress an entire srcBuffer into a valid LZ4 frame, as defined by specification v1.5.0, in a single step.
* The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case. * The most important rule is that dstBuffer MUST be large enough (dstMaxSize) to ensure compression completion even in worst case.
* You can get the minimum value of dstMaxSize by using LZ4F_compressFrameBound() * You can get the minimum value of dstMaxSize by using LZ4F_compressFrameBound()
* If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode) * If this condition is not respected, LZ4F_compressFrame() will fail (result is an errorCode)
* The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will be set to default. * The LZ4F_preferences_t structure is optional : you can provide NULL as argument. All preferences will then be set to default.
* The result of the function is the number of bytes written into dstBuffer. * The result of the function is the number of bytes written into dstBuffer.
* The function outputs an error code if it fails (can be tested using LZ4F_isError()) * The function outputs an error code if it fails (can be tested using LZ4F_isError())
*/ */
@ -268,20 +270,7 @@ size_t LZ4F_compressFrame(void* dstBuffer, size_t dstMaxSize, const void* srcBuf
if (preferencesPtr!=NULL) prefs = *preferencesPtr; if (preferencesPtr!=NULL) prefs = *preferencesPtr;
else memset(&prefs, 0, sizeof(prefs)); else memset(&prefs, 0, sizeof(prefs));
{ prefs.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize);
blockSizeID_t proposedBSID = max64KB;
size_t maxBlockSize = 64 KB;
while (prefs.frameInfo.blockSizeID > proposedBSID)
{
if (srcSize <= maxBlockSize)
{
prefs.frameInfo.blockSizeID = proposedBSID;
break;
}
proposedBSID = (blockSizeID_t)((int)proposedBSID + 1);
maxBlockSize <<= 2;
}
}
prefs.autoFlush = 1; prefs.autoFlush = 1;
if (srcSize <= LZ4F_getBlockSize(prefs.frameInfo.blockSizeID)) if (srcSize <= LZ4F_getBlockSize(prefs.frameInfo.blockSizeID))
prefs.frameInfo.blockMode = blockIndependent; /* no need for linked blocks */ prefs.frameInfo.blockMode = blockIndependent; /* no need for linked blocks */

View File

@ -627,18 +627,18 @@ static unsigned long long decodeLZ4S(FILE* finput, FILE* foutput)
if (storedSkips > 1 GB) if (storedSkips > 1 GB)
{ {
seekResult = fseek(foutput, 1 GB, SEEK_CUR); seekResult = fseek(foutput, 1 GB, SEEK_CUR);
if (seekResult != 0) EXM_THROW(68, "1 GB skip error (sparse file)\n"); if (seekResult != 0) EXM_THROW(68, "1 GB skip error (sparse file)");
storedSkips -= 1 GB; storedSkips -= 1 GB;
} }
if (skippedLength != seg0Size) if (skippedLength != seg0Size)
{ {
seekResult = fseek(foutput, storedSkips, SEEK_CUR); seekResult = fseek(foutput, storedSkips, SEEK_CUR);
if (seekResult != 0) EXM_THROW(68, "Skip error (sparse file)\n"); if (seekResult) EXM_THROW(68, "Skip error (sparse file)");
storedSkips = 0; storedSkips = 0;
seg0Size -= skippedLength; seg0Size -= skippedLength;
oBuffPos += skippedLength; oBuffPos += skippedLength;
sizeCheck = fwrite(oBuffPos, 1, seg0Size, foutput); sizeCheck = fwrite(oBuffPos, 1, seg0Size, foutput);
if (sizeCheck != seg0Size) EXM_THROW(68, "Write error : cannot write decoded block\n"); if (sizeCheck != seg0Size) EXM_THROW(68, "Write error : cannot write decoded block");
} }
oBuffPos += seg0Size; oBuffPos += seg0Size;
} }
@ -646,7 +646,7 @@ static unsigned long long decodeLZ4S(FILE* finput, FILE* foutput)
else else
{ {
sizeCheck = fwrite(outBuff, 1, decodedBytes, foutput); sizeCheck = fwrite(outBuff, 1, decodedBytes, foutput);
if (sizeCheck != decodedBytes) EXM_THROW(68, "Write error : cannot write decoded block\n"); if (sizeCheck != decodedBytes) EXM_THROW(68, "Write error : cannot write decoded block");
} }
} }