From da9402c6f563ded59a41c772afb9307962833d60 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Mon, 16 Mar 2015 22:35:02 +0100 Subject: minor lz4frame refactoring --- lib/lz4.h | 14 +++++++------- lib/lz4frame.c | 49 +++++++++++++++++++------------------------------ programs/lz4io.c | 8 ++++---- 3 files changed, 30 insertions(+), 41 deletions(-) diff --git a/lib/lz4.h b/lib/lz4.h index f04344b..db55b11 100644 --- a/lib/lz4.h +++ b/lib/lz4.h @@ -45,7 +45,7 @@ extern "C" { */ /************************************** - Version +* Version **************************************/ #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */ #define LZ4_VERSION_MINOR 5 /* for new (non-breaking) interface capabilities */ @@ -54,7 +54,7 @@ extern "C" { int LZ4_versionNumber (void); /************************************** - Tuning parameter +* Tuning parameter **************************************/ /* * 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); @@ -96,7 +96,7 @@ LZ4_decompress_safe() : /************************************** - Advanced Functions +* Advanced Functions **************************************/ #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) @@ -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) @@ -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 @@ -286,7 +286,7 @@ int LZ4_decompress_fast_usingDict (const char* source, char* dest, int originalS /************************************** - Obsolete Functions +* Obsolete Functions **************************************/ /* Obsolete decompression functions diff --git a/lib/lz4frame.c b/lib/lz4frame.c index d08cf94..ed47c0a 100644 --- a/lib/lz4frame.c +++ b/lib/lz4frame.c @@ -208,6 +208,21 @@ static BYTE LZ4F_headerChecksum (const BYTE* header, size_t length) /************************************** * 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) { 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; else memset(&prefs, 0, sizeof(prefs)); - { - 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.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize); prefs.autoFlush = 1; 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() -* 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. * 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) -* 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 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; else memset(&prefs, 0, sizeof(prefs)); - { - 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.frameInfo.blockSizeID = LZ4F_optimalBSID(prefs.frameInfo.blockSizeID, srcSize); prefs.autoFlush = 1; if (srcSize <= LZ4F_getBlockSize(prefs.frameInfo.blockSizeID)) prefs.frameInfo.blockMode = blockIndependent; /* no need for linked blocks */ diff --git a/programs/lz4io.c b/programs/lz4io.c index 13d1887..67769d2 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -627,18 +627,18 @@ static unsigned long long decodeLZ4S(FILE* finput, FILE* foutput) if (storedSkips > 1 GB) { 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; } if (skippedLength != seg0Size) { 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; seg0Size -= skippedLength; oBuffPos += skippedLength; 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; } @@ -646,7 +646,7 @@ static unsigned long long decodeLZ4S(FILE* finput, FILE* foutput) else { 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"); } } -- cgit v0.12