@@ -80,8 +80,9 @@ int32_t td5(const unsigned char *inVals, unsigned char *outVals, const uint32_t
80
80
// inVals input data
81
81
// outVals compressed data
82
82
// nValues number of values to compress
83
- // returns number of bits compressed, 0 if not compressible, or -1 if error
83
+ // returns number of bits compressed, 0 if not compressible, or negative value if error
84
84
{
85
+ outVals [0 ] = 1 ; // default uncompressed reason is general failure
85
86
switch (nValues )
86
87
{
87
88
case 1 :
@@ -221,7 +222,7 @@ int32_t td5(const unsigned char *inVals, unsigned char *outVals, const uint32_t
221
222
const int32_t ival2 = (unsigned char )inVals [1 ];
222
223
const int32_t ival3 = (unsigned char )inVals [2 ];
223
224
const int32_t ival4 = (unsigned char )inVals [3 ];
224
- if (ival1 == ival2 == ival3 == ival4 )
225
+ if (ival1 == ival2 && ival1 == ival3 && ival1 == ival4 )
225
226
{
226
227
if (nValues == 4 )
227
228
{
@@ -230,7 +231,7 @@ int32_t td5(const unsigned char *inVals, unsigned char *outVals, const uint32_t
230
231
outVals [1 ] = (unsigned char )ival1 >> 6 ;
231
232
return 10 ;
232
233
}
233
- else if (( nValues == 5 ) && ( ival1 == inVals [4 ]) )
234
+ else if (ival1 == inVals [4 ])
234
235
{
235
236
// all 5 values equal
236
237
outVals [0 ] = (unsigned char )(ival1 << 2 ) | 1 ;
@@ -266,7 +267,7 @@ int32_t td5(const unsigned char *inVals, unsigned char *outVals, const uint32_t
266
267
int32_t otherVal = -1 ;
267
268
if (ival2 != ival1 )
268
269
{
269
- otherVal = ival2 ;
270
+ otherVal = ival2 ;
270
271
outBits |= 2 ;
271
272
}
272
273
if (ival3 != ival1 )
@@ -336,9 +337,9 @@ int32_t td5d(const unsigned char *inVals, unsigned char *outVals, const uint32_t
336
337
if ((firstByte & 3 ) == 1 )
337
338
{
338
339
// process single unique
339
- uint32_t unique = (firstByte >> 2 ) | (inVals [1 ] >> 6 );
340
+ uint32_t unique = (firstByte >> 2 ) | (inVals [1 ] << 6 );
340
341
memset (outVals , (unsigned char )unique , nOriginalValues );
341
- * bytesProcessed = ( firstByte & 2 ) ? 1 : 2 ;
342
+ * bytesProcessed = 2 ;
342
343
return (int )nOriginalValues ;
343
344
}
344
345
uint32_t val1 ;
@@ -410,7 +411,7 @@ int32_t td5d(const unsigned char *inVals, unsigned char *outVals, const uint32_t
410
411
outVals [1 ] = (unsigned char )textChars [(firstByte >>6 ) | ((secondByte <<2 ) & 0xf )];
411
412
outVals [2 ] = (unsigned char )textChars [(secondByte >>2 ) & 0xf ];
412
413
outVals [3 ] = (unsigned char )textChars [(secondByte >>6 ) | ((inVals [2 ]<<2 ) & 0xf )];
413
- return 3 ;
414
+ return 4 ;
414
415
}
415
416
const int32_t thirdByte = (unsigned char )inVals [2 ];
416
417
val1 = (unsigned char )(firstByte >> 4 ) | (unsigned char )(secondByte << 4 );
@@ -435,7 +436,7 @@ int32_t td5d(const unsigned char *inVals, unsigned char *outVals, const uint32_t
435
436
if (thirdByte & 0x4 )
436
437
{
437
438
// fifth byte is text char
438
- outVals [4 ] = (unsigned char )(thirdByte >> 3 ) & 0xf ;
439
+ outVals [4 ] = (unsigned char )textChars [ (thirdByte >> 3 ) & 0xf ] ;
439
440
}
440
441
else
441
442
{
@@ -867,7 +868,7 @@ int32_t td64(unsigned char *inVals, unsigned char *outVals, const uint32_t nValu
867
868
uint32_t nUniqueVals = 0 ; // count of unique vals encountered
868
869
unsigned char val256 [256 ]= {0 }; // init characters found to 0
869
870
const uint32_t uniqueLimit = uniqueLimits25 [nValues ]; // if exceeded, return uncompressible by fixed bit coding
870
- const uint32_t nValsInitLoop = (nValues * 5 /16 )+ 1 ;
871
+ const uint32_t nValsInitLoop = nValues < 24 ? nValues / 2 + 1 : (nValues * 5 /16 )+ 1 ; // 1-23 use 1/2 nValues, 24+ use 1/3 nValues
871
872
// save uniques for use after check for text mode
872
873
unsigned char saveUniques [MAX_TD64_BYTES ];
873
874
uint32_t textModeCalled = 0 ; // need to restore uniques for some modes after text mode called
@@ -891,9 +892,9 @@ int32_t td64(unsigned char *inVals, unsigned char *outVals, const uint32_t nValu
891
892
highBitCheck |= inVal ; // keep watch on high bit of unique values
892
893
}
893
894
}
894
- if (nUniqueVals > uniqueLimit + 1 )
895
+ if (nUniqueVals > uniqueLimit + 1 && nValues >= MIN_VALUES_RECOGNIZE_RANDOM_DATA )
895
896
{
896
- // supported unique values exceeded
897
+ // supported unique values exceeded--skip this for < 16 values
897
898
if ((highBitCheck & 0x80 ) == 0 && nValues >= MIN_VALUES_7_BIT_MODE )
898
899
{
899
900
// attempt to compress based on high bit clear across all values
@@ -972,7 +973,7 @@ int32_t td64(unsigned char *inVals, unsigned char *outVals, const uint32_t nValu
972
973
if (textModeCalled )
973
974
memcpy (outVals + 1 , saveUniques , textModeCalled ); // restore uniques to outVals starting in second byte
974
975
// max bits set to 12% if high bit clear and enough input values, else 6%
975
- uint32_t maxBits = ((highBitCheck & 0x80 ) == 0 && nValues >= MIN_VALUE_7_BIT_MODE_6_PERCENT ) ? nValues * 7 : nValues * 7 + nValues /2 ;
976
+ uint32_t maxBits = ((highBitCheck & 0x80 ) == 0 && nValues >= MIN_VALUE_7_BIT_MODE_12_PERCENT ) ? nValues * 7 : nValues * 7 + nValues /2 ;
976
977
if ((retBits = encodeStringMode (inVals , outVals , nValues , nUniqueVals , uniqueOccurrence , maxBits )))
977
978
return retBits ;
978
979
}
0 commit comments