@@ -947,6 +947,7 @@ static int getFileInfo(fileInfo_t* info, const char* inFileName){
947
947
return 3 ;
948
948
}
949
949
info -> compressedSize = (unsigned long long )UTIL_getFileSize (inFileName );
950
+
950
951
/* begin analyzing frame */
951
952
for ( ; ; ) {
952
953
BYTE headerBuffer [ZSTD_FRAMEHEADERSIZE_MAX ];
@@ -966,37 +967,31 @@ static int getFileInfo(fileInfo_t* info, const char* inFileName){
966
967
break ;
967
968
}
968
969
}
969
- {
970
- U32 const magicNumber = MEM_readLE32 ( headerBuffer );
970
+ { U32 const magicNumber = MEM_readLE32 ( headerBuffer );
971
+ /* Zstandard frame */
971
972
if (magicNumber == ZSTD_MAGICNUMBER ) {
972
973
U64 const frameContentSize = ZSTD_getFrameContentSize (headerBuffer , numBytesRead );
973
974
if (frameContentSize == ZSTD_CONTENTSIZE_ERROR || frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN ) {
974
975
info -> decompUnavailable = 1 ;
975
- }
976
- else {
976
+ } else {
977
977
info -> decompressedSize += frameContentSize ;
978
978
}
979
- {
980
- /* move to the end of the frame header */
981
- size_t const headerSize = ZSTD_frameHeaderSize (headerBuffer , numBytesRead );
979
+ /* move to the end of the frame header */
980
+ { size_t const headerSize = ZSTD_frameHeaderSize (headerBuffer , numBytesRead );
982
981
if (ZSTD_isError (headerSize )) {
983
982
DISPLAY ("Error: could not determine frame header size\n" );
984
983
detectError = 1 ;
985
984
break ;
986
985
}
987
- {
988
- int const ret = fseek (srcFile , ((long )headerSize )- ((long )numBytesRead ), SEEK_CUR );
986
+ { int const ret = fseek (srcFile , ((long )headerSize )- ((long )numBytesRead ), SEEK_CUR );
989
987
if (ret != 0 ) {
990
988
DISPLAY ("Error: could not move to end of frame header\n" );
991
989
detectError = 1 ;
992
990
break ;
993
- }
994
- }
995
- }
991
+ } } }
996
992
997
993
/* skip the rest of the blocks in the frame */
998
- {
999
- int lastBlock = 0 ;
994
+ { int lastBlock = 0 ;
1000
995
do {
1001
996
BYTE blockHeaderBuffer [3 ];
1002
997
U32 blockHeader ;
@@ -1009,24 +1004,20 @@ static int getFileInfo(fileInfo_t* info, const char* inFileName){
1009
1004
}
1010
1005
blockHeader = MEM_readLE24 (blockHeaderBuffer );
1011
1006
lastBlock = blockHeader & 1 ;
1012
- blockSize = blockHeader >> 3 ;
1013
- {
1014
- int const ret = fseek (srcFile , blockSize , SEEK_CUR );
1007
+ blockSize = blockHeader >> 3 ; /* Warning : does not work when block is RLE type */
1008
+ { int const ret = fseek (srcFile , blockSize , SEEK_CUR );
1015
1009
if (ret != 0 ) {
1016
1010
DISPLAY ("Error: could not skip to end of block\n" );
1017
1011
detectError = 1 ;
1018
1012
break ;
1019
- }
1020
- }
1013
+ } }
1021
1014
} while (lastBlock != 1 );
1022
1015
1023
- if (detectError ) {
1024
- break ;
1025
- }
1016
+ if (detectError ) break ;
1026
1017
}
1027
- {
1028
- /* check if checksum is used */
1029
- BYTE const frameHeaderDescriptor = headerBuffer [4 ];
1018
+
1019
+ /* check if checksum is used */
1020
+ { BYTE const frameHeaderDescriptor = headerBuffer [4 ];
1030
1021
int const contentChecksumFlag = (frameHeaderDescriptor & (1 << 2 )) >> 2 ;
1031
1022
if (contentChecksumFlag ) {
1032
1023
int const ret = fseek (srcFile , 4 , SEEK_CUR );
@@ -1035,64 +1026,60 @@ static int getFileInfo(fileInfo_t* info, const char* inFileName){
1035
1026
DISPLAY ("Error: could not skip past checksum\n" );
1036
1027
detectError = 1 ;
1037
1028
break ;
1038
- }
1039
- }
1040
- }
1029
+ } } }
1041
1030
info -> numActualFrames ++ ;
1042
1031
}
1043
- else if (magicNumber == ZSTD_MAGIC_SKIPPABLE_START ) {
1044
- BYTE frameSizeBuffer [4 ];
1045
- size_t const readBytes = fread (frameSizeBuffer , 1 , 4 , srcFile );
1046
- if (readBytes != 4 ) {
1047
- DISPLAY ("There was an error reading skippable frame size" );
1032
+ /* Skippable frame */
1033
+ else if ((magicNumber & 0xFFFFFFF0U ) == ZSTD_MAGIC_SKIPPABLE_START ) {
1034
+ U32 const frameSize = MEM_readLE32 (headerBuffer + 4 );
1035
+ long const seek = - numBytesRead + 8 + frameSize ;
1036
+ int const ret = LONG_SEEK (srcFile , seek , SEEK_CUR );
1037
+ if (ret != 0 ) {
1038
+ DISPLAY ("Error: could not find end of skippable frame\n" );
1048
1039
detectError = 1 ;
1049
1040
break ;
1050
1041
}
1051
- {
1052
- U32 const frameSize = MEM_readLE32 (frameSizeBuffer );
1053
- int const ret = LONG_SEEK (srcFile , frameSize , SEEK_CUR );
1054
- if (ret != 0 ) {
1055
- DISPLAY ("Error: could not find end of skippable frame\n" );
1056
- detectError = 1 ;
1057
- break ;
1058
- }
1059
- }
1060
1042
info -> numSkippableFrames ++ ;
1061
1043
}
1044
+ /* unknown content */
1062
1045
else {
1063
1046
detectError = 2 ;
1064
1047
break ;
1065
1048
}
1066
1049
}
1067
- }
1050
+ } /* end analyzing frame */
1068
1051
fclose (srcFile );
1069
1052
return detectError ;
1070
1053
}
1071
1054
1072
1055
static void displayInfo (const char * inFileName , fileInfo_t * info , int displayLevel ){
1073
- double const compressedSizeMB = (double )info -> compressedSize /(1 MB );
1074
- double const decompressedSizeMB = (double )info -> decompressedSize /(1 MB );
1056
+ unsigned const unit = info -> compressedSize < (1 MB ) ? (1 KB ) : (1 MB );
1057
+ const char * const unitStr = info -> compressedSize < (1 MB ) ? "KB" : "MB" ;
1058
+ double const compressedSizeUnit = (double )info -> compressedSize / unit ;
1059
+ double const decompressedSizeUnit = (double )info -> decompressedSize / unit ;
1075
1060
double const ratio = (info -> compressedSize == 0 ) ? 0 : ((double )info -> decompressedSize )/info -> compressedSize ;
1076
1061
const char * const checkString = (info -> usesCheck ? "XXH64" : "None" );
1077
1062
if (displayLevel <= 2 ) {
1078
1063
if (!info -> decompUnavailable ) {
1079
1064
DISPLAYOUT ("Skippable Non-Skippable Compressed Uncompressed Ratio Check Filename\n" );
1080
- DISPLAYOUT ("%9d %13d %7.2f MB %9.2f MB %5.3f %5s %s\n" ,
1081
- info -> numSkippableFrames , info -> numActualFrames , compressedSizeMB , decompressedSizeMB ,
1065
+ DISPLAYOUT ("%9d %13d %7.2f %2s %9.2f %2s %5.3f %5s %s\n" ,
1066
+ info -> numSkippableFrames , info -> numActualFrames ,
1067
+ compressedSizeUnit , unitStr , decompressedSizeUnit , unitStr ,
1082
1068
ratio , checkString , inFileName );
1083
- }
1084
- else {
1069
+ } else {
1085
1070
DISPLAYOUT ("Skippable Non-Skippable Compressed Check Filename\n" );
1086
1071
DISPLAYOUT ("%9d %13d %7.2f MB %5s %s\n" ,
1087
- info -> numSkippableFrames , info -> numActualFrames , compressedSizeMB , checkString , inFileName );
1072
+ info -> numSkippableFrames , info -> numActualFrames ,
1073
+ compressedSizeUnit , checkString , inFileName );
1088
1074
}
1089
- }
1090
- else {
1075
+ } else {
1091
1076
DISPLAYOUT ("# Zstandard Frames: %d\n" , info -> numActualFrames );
1092
1077
DISPLAYOUT ("# Skippable Frames: %d\n" , info -> numSkippableFrames );
1093
- DISPLAYOUT ("Compressed Size: %.2f MB (%llu B)\n" , compressedSizeMB , info -> compressedSize );
1078
+ DISPLAYOUT ("Compressed Size: %.2f %2s (%llu B)\n" ,
1079
+ compressedSizeUnit , unitStr , info -> compressedSize );
1094
1080
if (!info -> decompUnavailable ) {
1095
- DISPLAYOUT ("Decompressed Size: %.2f MB (%llu B)\n" , decompressedSizeMB , info -> decompressedSize );
1081
+ DISPLAYOUT ("Decompressed Size: %.2f %2s (%llu B)\n" ,
1082
+ decompressedSizeUnit , unitStr , info -> decompressedSize );
1096
1083
DISPLAYOUT ("Ratio: %.4f\n" , ratio );
1097
1084
}
1098
1085
DISPLAYOUT ("Check: %s\n" , checkString );
0 commit comments