Bug or misunderstanding

delphi package - madRes, madTools, madStrings, ...
Post Reply
Paul van der Eijk
Posts: 7
Joined: Tue Mar 07, 2006 10:36 pm

Bug or misunderstanding

Post by Paul van der Eijk »

Madshi,

Trying to use compress / uncompress; the versions with two pointers.
When I do compress --> uncompress the uncompressed buffer contains
the correct data but the uncompress function returns 0. How do I get
the uncompressed length? I expected uncompress to return that value.
See code below what I try to do:

--Paul

Code: Select all

program Project1;

{$APPTYPE CONSOLE}

uses
  sysutils,
  madzip;

const
   bufsrc = 100;
   bufdest = 120;
var
   psrc, pdest: pbytearray;
   n, nl: integer;
begin
GetMem(psrc, bufsrc);
for n := 0 to bufsrc - 1
do psrc^[n] := n mod 4;
GetMem(pdest, bufdest);
nl := madzip.Compress(psrc, pdest, bufsrc, bufdest);
WriteLn('nl = ', nl);
for n := 0 to bufsrc - 1
do psrc^[n] := 0;
n := madZip.Uncompress(pdest, psrc, bufsrc, bufdest);
WriteLn('n = ', n);
for n := 0 to bufsrc - 1
do if psrc[n] <> n mod 4
   then
      WriteLn('Error, n = ', n, ' value = ', psrc[n], ' expected ', n mod 4);
end.
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

In the Uncompress call shouldn't the "buffer size" parameters be swapped?
Paul van der Eijk
Posts: 7
Joined: Tue Mar 07, 2006 10:36 pm

Post by Paul van der Eijk »

Madshi,

I made that correction but no difference.

--Paul
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Wait a moment, when calling "Uncompress" the uncompressor of course needs to know how much data the compressed stream has. So the 3rd parameter has to be what "Compress" returned. Both "bufsrc" and "bufdest" are incorrect.
Paul van der Eijk
Posts: 7
Joined: Tue Mar 07, 2006 10:36 pm

Post by Paul van der Eijk »

Yes, I found that too a minute ago by stepping through the code.
Doing so, I think there could be a problem in line 3607; my change
(z.TotalInput = dword(srcLen)) and (z.TotalOutput <= dword(dstLen)) then //!!

Output size should be less/equal to capcatity, not equal??

--Paul
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

No, it must be equal. Why should the uncompressed data be shorter than the original data? That doesn't make any sense.
Paul van der Eijk
Posts: 7
Joined: Tue Mar 07, 2006 10:36 pm

Post by Paul van der Eijk »

Madshi,

Respectfully disagree.

I have two buffers, original data and compressed data. Compressed data goes to a file, preceeded with the length of the compressed data. But I do not always write the the same amount of uncompressed data. So when reading the data back, I know that my buffer of uncompressed data is large enough, but it will not always fill to capacity.

--Paul
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Ok, but you do know how long the original data was? You can use a bigger buffer, if you like, but why not giving in the size of the original data?
Paul van der Eijk
Posts: 7
Joined: Tue Mar 07, 2006 10:36 pm

Post by Paul van der Eijk »

The original design (ZLib) does not need to know the uncompressed
length. The uncompress function returns an indication that there was not enough memory. This function takes a var parameter which is length of output buffer allocated, on returns the length of the uncompressed data.

No big deal, but I wanted to stay with my original design not to write the ucompressed length to the file.

--Paul
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Well, I guess I could relax the length checking.
Post Reply