mirror of
https://github.com/facebook/zstd.git
synced 2025-10-07 00:12:40 -04:00
cleaning up code
This commit is contained in:
parent
fc428ab350
commit
e208992529
@ -91,6 +91,7 @@
|
|||||||
* Macros
|
* Macros
|
||||||
***************************************/
|
***************************************/
|
||||||
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
|
#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
|
||||||
|
#define DISPLAYOUT(...) fprintf(stdout, __VA_ARGS__)
|
||||||
#define DISPLAYLEVEL(l, ...) { if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
|
#define DISPLAYLEVEL(l, ...) { if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
|
||||||
static int g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
|
static int g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
|
||||||
void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
|
void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
|
||||||
@ -872,21 +873,13 @@ typedef struct {
|
|||||||
} fileInfo_t;
|
} fileInfo_t;
|
||||||
|
|
||||||
|
|
||||||
int calcFrameHeaderSize(BYTE frameHeaderDescriptor){
|
static int calcFrameHeaderSize(BYTE frameHeaderDescriptor){
|
||||||
int frameContentSizeBytes = 0;
|
const int frameContentSizeFlag = frameHeaderDescriptor >> 6;
|
||||||
int windowDescriptorBytes;
|
const int singleSegmentFlag = (frameHeaderDescriptor & (1 << 5)) >> 5;
|
||||||
int dictionaryIDBytes;
|
const int dictionaryIDFlag = frameHeaderDescriptor & 3;
|
||||||
int frameContentSizeFlag = frameHeaderDescriptor >> 6;
|
const int windowDescriptorBytes = singleSegmentFlag ? 0 : 1;
|
||||||
int singleSegmentFlag = (frameHeaderDescriptor & (1 << 5)) >> 5;
|
const int frameContentSizeBytes = (frameContentSizeFlag != 0) ? (1 << frameContentSizeFlag) : (singleSegmentFlag ? 1 : 0);
|
||||||
int dictionaryIDFlag = frameHeaderDescriptor & 3;
|
const int dictionaryIDBytes = dictionaryIDFlag ? 1 << (dictionaryIDFlag - 1): 0;
|
||||||
if(frameContentSizeFlag!=0){
|
|
||||||
frameContentSizeBytes = 1 << frameContentSizeFlag;
|
|
||||||
}
|
|
||||||
else if(singleSegmentFlag){
|
|
||||||
frameContentSizeBytes = 1;
|
|
||||||
}
|
|
||||||
windowDescriptorBytes = singleSegmentFlag ? 0 : 1;
|
|
||||||
dictionaryIDBytes = dictionaryIDFlag ? 1 << (dictionaryIDFlag - 1): 0;
|
|
||||||
return 4 + 1 + windowDescriptorBytes + frameContentSizeBytes + dictionaryIDBytes;
|
return 4 + 1 + windowDescriptorBytes + frameContentSizeBytes + dictionaryIDBytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -894,50 +887,67 @@ int calcFrameHeaderSize(BYTE frameHeaderDescriptor){
|
|||||||
* Reads information from file, stores in *info
|
* Reads information from file, stores in *info
|
||||||
* if successful, returns 0, otherwise returns 1
|
* if successful, returns 0, otherwise returns 1
|
||||||
*/
|
*/
|
||||||
int getFileInfo(fileInfo_t* info, const char* inFileName){
|
static int getFileInfo(fileInfo_t* info, const char* inFileName){
|
||||||
FILE* srcFile = FIO_openSrcFile(inFileName);
|
FILE* const srcFile = FIO_openSrcFile(inFileName);
|
||||||
if(srcFile==NULL){
|
if (srcFile == NULL) {
|
||||||
|
DISPLAY("Error: could not open source file %s\n", inFileName);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
info->compressedSize = (unsigned long long)UTIL_getFileSize(inFileName);
|
info->compressedSize = (unsigned long long)UTIL_getFileSize(inFileName);
|
||||||
info->decompressedSize = 0;
|
info->decompressedSize = 0;
|
||||||
info->numActualFrames = 0;
|
info->numActualFrames = 0;
|
||||||
info-> numSkippableFrames = 0;
|
info->numSkippableFrames = 0;
|
||||||
info->canComputeDecompSize = 1;
|
info->canComputeDecompSize = 1;
|
||||||
/* begin analyzing frame */
|
/* begin analyzing frame */
|
||||||
while(1){
|
for( ; ; ){
|
||||||
BYTE magicNumberBuffer[4];
|
BYTE magicNumberBuffer[4];
|
||||||
size_t numBytesRead = fread(magicNumberBuffer, 1, 4, srcFile);
|
size_t numBytesRead = fread(magicNumberBuffer, 1, 4, srcFile);
|
||||||
U32 magicNumber;
|
U32 magicNumber;
|
||||||
if(numBytesRead != 4) break;
|
if (numBytesRead != 4) break;
|
||||||
magicNumber = MEM_readLE32(magicNumberBuffer);
|
magicNumber = MEM_readLE32(magicNumberBuffer);
|
||||||
if(magicNumber==ZSTD_MAGICNUMBER){
|
if (magicNumber==ZSTD_MAGICNUMBER) {
|
||||||
BYTE frameHeaderDescriptor;
|
BYTE frameHeaderDescriptor;
|
||||||
int totalFrameHeaderBytes;
|
int totalFrameHeaderBytes;
|
||||||
BYTE* frameHeader;
|
BYTE* frameHeader;
|
||||||
int lastBlock = 0;
|
int lastBlock = 0;
|
||||||
size_t readBytes = fread(&frameHeaderDescriptor, 1, 1, srcFile);
|
size_t readBytes = fread(&frameHeaderDescriptor, 1, 1, srcFile);
|
||||||
info->numActualFrames++;
|
info->numActualFrames++;
|
||||||
if(readBytes != 1){
|
if (readBytes != 1) {
|
||||||
DISPLAY("There was an error with reading frame header descriptor\n");
|
DISPLAY("Error: could not read frame header descriptor\n");
|
||||||
exit(1);
|
fclose(srcFile);
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
/* calculate actual frame header size */
|
/* calculate actual frame header size */
|
||||||
totalFrameHeaderBytes = calcFrameHeaderSize(frameHeaderDescriptor);
|
totalFrameHeaderBytes = calcFrameHeaderSize(frameHeaderDescriptor);
|
||||||
|
|
||||||
/* reset to beginning of from and read entire header */
|
/* reset to beginning of from and read entire header */
|
||||||
fseek(srcFile, -5, SEEK_CUR);
|
{
|
||||||
|
int returnVal = fseek(srcFile, -5, SEEK_CUR);
|
||||||
|
if (returnVal!=0) {
|
||||||
|
DISPLAY("Error: could not reset to the beginning of the frame header\n");
|
||||||
|
fclose(srcFile);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
frameHeader = (BYTE*)malloc(totalFrameHeaderBytes);
|
frameHeader = (BYTE*)malloc(totalFrameHeaderBytes);
|
||||||
|
if (frameHeader==NULL) {
|
||||||
|
DISPLAY("Error: could not allocate space for frameHeader\n");
|
||||||
|
fclose(srcFile);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
readBytes = fread(frameHeader, 1, totalFrameHeaderBytes, srcFile);
|
readBytes = fread(frameHeader, 1, totalFrameHeaderBytes, srcFile);
|
||||||
if(readBytes != (size_t)totalFrameHeaderBytes){
|
if (readBytes != (size_t)totalFrameHeaderBytes) {
|
||||||
DISPLAY("There was an error reading the frame header\n");
|
DISPLAY("Error: could not read frame header\n");
|
||||||
exit(1);
|
fclose(srcFile);
|
||||||
|
free(frameHeader);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get decompressed file size */
|
/* get decompressed file size */
|
||||||
{
|
{
|
||||||
U64 additional = ZSTD_getFrameContentSize(frameHeader, totalFrameHeaderBytes);
|
U64 additional = ZSTD_getFrameContentSize(frameHeader, totalFrameHeaderBytes);
|
||||||
if(additional!=ZSTD_CONTENTSIZE_UNKNOWN && additional!=ZSTD_CONTENTSIZE_ERROR){
|
if (additional!=ZSTD_CONTENTSIZE_UNKNOWN && additional!=ZSTD_CONTENTSIZE_ERROR) {
|
||||||
info->decompressedSize += additional;
|
info->decompressedSize += additional;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@ -951,7 +961,7 @@ int getFileInfo(fileInfo_t* info, const char* inFileName){
|
|||||||
U32 blockHeader;
|
U32 blockHeader;
|
||||||
int blockSize;
|
int blockSize;
|
||||||
readBytes = fread(blockHeaderBuffer, 1, 3, srcFile);
|
readBytes = fread(blockHeaderBuffer, 1, 3, srcFile);
|
||||||
if(readBytes != 3){
|
if (readBytes != 3) {
|
||||||
DISPLAY("There was a problem reading the block header\n");
|
DISPLAY("There was a problem reading the block header\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@ -959,37 +969,38 @@ int getFileInfo(fileInfo_t* info, const char* inFileName){
|
|||||||
lastBlock = blockHeader & 1;
|
lastBlock = blockHeader & 1;
|
||||||
blockSize = (blockHeader - (blockHeader & 7)) >> 3;
|
blockSize = (blockHeader - (blockHeader & 7)) >> 3;
|
||||||
fseek(srcFile, blockSize, SEEK_CUR);
|
fseek(srcFile, blockSize, SEEK_CUR);
|
||||||
}while(lastBlock != 1);
|
}while (lastBlock != 1);
|
||||||
{
|
{
|
||||||
/* check if checksum is used */
|
/* check if checksum is used */
|
||||||
int contentChecksumFlag = (frameHeaderDescriptor & (1 << 2)) >> 2;
|
int contentChecksumFlag = (frameHeaderDescriptor & (1 << 2)) >> 2;
|
||||||
if(contentChecksumFlag){
|
if (contentChecksumFlag) {
|
||||||
info->usesCheck = 1;
|
info->usesCheck = 1;
|
||||||
}
|
}
|
||||||
if(contentChecksumFlag){
|
if (contentChecksumFlag) {
|
||||||
fseek(srcFile, 4, SEEK_CUR);
|
fseek(srcFile, 4, SEEK_CUR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
free(frameHeader);
|
||||||
}
|
}
|
||||||
else if(magicNumber==ZSTD_MAGIC_SKIPPABLE_START){
|
else if (magicNumber == ZSTD_MAGIC_SKIPPABLE_START) {
|
||||||
BYTE frameSizeBuffer[4];
|
BYTE frameSizeBuffer[4];
|
||||||
long frameSize;
|
long frameSize;
|
||||||
size_t readBytes = fread(frameSizeBuffer, 1, 4, srcFile);
|
size_t readBytes = fread(frameSizeBuffer, 1, 4, srcFile);
|
||||||
info->numSkippableFrames++;
|
info->numSkippableFrames++;
|
||||||
if(readBytes != 4){
|
if (readBytes != 4) {
|
||||||
DISPLAY("There was an error reading skippable frame size");
|
DISPLAY("There was an error reading skippable frame size");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
frameSize = MEM_readLE32(frameSizeBuffer);
|
frameSize = MEM_readLE32(frameSizeBuffer);
|
||||||
fseek(srcFile, frameSize, SEEK_CUR);
|
fseek(srcFile, frameSize, SEEK_CUR);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
fclose(srcFile);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
void displayInfo(const char* inFileName, fileInfo_t* info, int displayLevel){
|
void displayInfo(const char* inFileName, fileInfo_t* info, int displayLevel){
|
||||||
double compressedSizeMB = (double)info->compressedSize/(1 MB);
|
double const compressedSizeMB = (double)info->compressedSize/(1 MB);
|
||||||
double decompressedSizeMB = (double)info->decompressedSize/(1 MB);
|
double const decompressedSizeMB = (double)info->decompressedSize/(1 MB);
|
||||||
|
|
||||||
if(displayLevel<=2){
|
if(displayLevel<=2){
|
||||||
if(info->usesCheck && info->canComputeDecompSize){
|
if(info->usesCheck && info->canComputeDecompSize){
|
||||||
|
@ -674,6 +674,7 @@ int main(int argCount, const char* argv[])
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if(operation==zom_list){
|
if(operation==zom_list){
|
||||||
|
g_displayOut = stdout;
|
||||||
if(filenameIdx==0){
|
if(filenameIdx==0){
|
||||||
DISPLAY("No files given\n");
|
DISPLAY("No files given\n");
|
||||||
CLEAN_RETURN(0);
|
CLEAN_RETURN(0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user