cleaning up code

This commit is contained in:
Paul Cruz 2017-06-15 12:27:32 -07:00
parent fc428ab350
commit e208992529
2 changed files with 51 additions and 39 deletions

View File

@ -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,9 +887,10 @@ 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);
@ -905,7 +899,7 @@ int getFileInfo(fileInfo_t* info, const char* inFileName){
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;
@ -919,19 +913,35 @@ int getFileInfo(fileInfo_t* info, const char* inFileName){
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 */
@ -970,6 +980,7 @@ int getFileInfo(fileInfo_t* info, const char* inFileName){
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];
@ -983,13 +994,13 @@ int getFileInfo(fileInfo_t* info, const char* inFileName){
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){

View File

@ -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);