@@ -37,7 +37,7 @@ limitations under the License.
3737using namespace std ;
3838using namespace kanzi ;
3939
40- static const int BS_VERSION = 6 ;
40+ static const int BS_VERSION = 7 ;
4141
4242static void writeInt16LE (kanzi::byte buf[], int value)
4343{
@@ -632,40 +632,73 @@ static int testTransformCapacityValidation()
632632 {
633633 LZXCodec<false > tf;
634634 kanzi::byte lzSrc[128 ];
635- kanzi::byte lzEncoded[256 ];
636- kanzi::byte lzDecoded[128 ];
635+ kanzi::byte lzDst[256 ];
637636
638637 for (int i = 0 ; i < 128 ; i++)
639- lzSrc[i] = kanzi::byte (i & 3 );
638+ lzSrc[i] = kanzi::byte (i);
640639
641- memset (lzEncoded, 0 , sizeof (lzEncoded) );
642- memset (lzDecoded, 0x7E , sizeof (lzDecoded) );
643- SliceArray<kanzi::byte> input (lzSrc, 128 , 0 ) ;
644- SliceArray<kanzi::byte> encoded (lzEncoded, int ( sizeof (lzEncoded)), 0 ) ;
640+ SliceArray<kanzi::byte> input (lzSrc, 128 , 1 );
641+ SliceArray<kanzi::byte> output (lzDst, 256 , 0 );
642+ const int savedIIdx = input. _index ;
643+ const int savedOIdx = output. _index ;
645644
646- if (tf.forward (input, encoded , 128 ) = = false ) {
647- cout << " LZX setup encoding failed " << endl;
645+ if (tf.forward (input, output , 128 ) ! = false ) {
646+ cout << " LZX forward should reject oversized remaining input count " << endl;
648647 return 1 ;
649648 }
650649
651- const int encodedSize = encoded._index ;
652- SliceArray<kanzi::byte> exactInput (lzEncoded, encodedSize, 0 );
653- SliceArray<kanzi::byte> output (lzDecoded, 128 , 0 );
650+ if ((input._index != savedIIdx) || (output._index != savedOIdx)) {
651+ cout << " LZX forward input capacity failure moved indexes" << endl;
652+ return 1 ;
653+ }
654+ }
654655
655- if (tf.inverse (exactInput, output, encodedSize) != false ) {
656- cout << " LZX should reject input without the read-length guard" << endl;
656+ {
657+ LZXCodec<false > tf;
658+ kanzi::byte lzSrc[128 ];
659+ kanzi::byte lzDst[176 ];
660+
661+ for (int i = 0 ; i < 128 ; i++)
662+ lzSrc[i] = kanzi::byte (i);
663+
664+ memset (lzDst, 0x7E , sizeof (lzDst));
665+ SliceArray<kanzi::byte> input (lzSrc, 128 , 0 );
666+ SliceArray<kanzi::byte> output (lzDst, int (sizeof (lzDst)), 32 );
667+
668+ if (tf.forward (input, output, 128 ) != false ) {
669+ cout << " LZX forward should reject incompressible input" << endl;
657670 return 1 ;
658671 }
659672
660- if ((exactInput ._index != 0 ) || (output. _index != 0 )) {
661- cout << " LZX guard failure moved slice indexes " << endl;
673+ if ((output ._index != 32 ) || (lzDst[ 32 ] != kanzi::byte ( 0x7E ) )) {
674+ cout << " LZX final-size check wrote output on failure " << endl;
662675 return 1 ;
663676 }
677+ }
664678
665- SliceArray<kanzi::byte> paddedInput (lzEncoded, int (sizeof (lzEncoded)), 0 );
679+ {
680+ Context v7ctx;
681+ v7ctx.putInt (" bsVersion" , 7 );
682+ LZXCodec<false > encoder (v7ctx);
683+ LZXCodec<false > decoder (v7ctx);
684+ kanzi::byte lzSrc[256 ];
685+ vector<kanzi::byte> lzEncoded (encoder.getMaxEncodedLength (256 ));
686+ kanzi::byte lzDecoded[512 ];
687+
688+ for (int i = 0 ; i < 256 ; i++)
689+ lzSrc[i] = kanzi::byte (i & 3 );
690+
691+ SliceArray<kanzi::byte> input (lzSrc, 256 , 0 );
692+ SliceArray<kanzi::byte> encoded (&lzEncoded[0 ], int (lzEncoded.size ()), 0 );
693+ SliceArray<kanzi::byte> output (lzDecoded, 512 , 0 );
694+
695+ const bool encodedOk = encoder.forward (input, encoded, 256 );
696+ const int encodedSize = encoded._index ;
697+ SliceArray<kanzi::byte> encodedInput (&lzEncoded[0 ], encodedSize, 0 );
698+ const bool decodedOk = decoder.inverse (encodedInput, output, encodedSize);
666699
667- if (tf. inverse (paddedInput, output, encodedSize) == false ) {
668- cout << " LZX should accept input with the read-length guard " << endl;
700+ if (!encodedOk || !decodedOk || (output. _index != 256 ) || ( memcmp (lzSrc, lzDecoded, 256 ) != 0 ) ) {
701+ cout << " LZX v7 round trip failed " << endl;
669702 return 1 ;
670703 }
671704 }
0 commit comments