Bug or misunderstanding

delphi package - madRes, madTools, madStrings, ...

Bug or misunderstanding

Postby Paul van der Eijk » Tue Apr 11, 2006 11:17 pm

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.
Paul van der Eijk
 
Posts: 7
Joined: Tue Mar 07, 2006 10:36 pm

Postby madshi » Wed Apr 12, 2006 9:55 am

In the Uncompress call shouldn't the "buffer size" parameters be swapped?
madshi
Site Admin
 
Posts: 9376
Joined: Sun Mar 21, 2004 5:25 pm

Postby Paul van der Eijk » Wed Apr 12, 2006 4:16 pm

Madshi,

I made that correction but no difference.

--Paul
Paul van der Eijk
 
Posts: 7
Joined: Tue Mar 07, 2006 10:36 pm

Postby madshi » Wed Apr 12, 2006 4:21 pm

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.
madshi
Site Admin
 
Posts: 9376
Joined: Sun Mar 21, 2004 5:25 pm

Postby Paul van der Eijk » Wed Apr 12, 2006 4:38 pm

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
Paul van der Eijk
 
Posts: 7
Joined: Tue Mar 07, 2006 10:36 pm

Postby madshi » Wed Apr 12, 2006 5:52 pm

No, it must be equal. Why should the uncompressed data be shorter than the original data? That doesn't make any sense.
madshi
Site Admin
 
Posts: 9376
Joined: Sun Mar 21, 2004 5:25 pm

Postby Paul van der Eijk » Wed Apr 12, 2006 6:14 pm

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
Paul van der Eijk
 
Posts: 7
Joined: Tue Mar 07, 2006 10:36 pm

Postby madshi » Wed Apr 12, 2006 6:16 pm

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?
madshi
Site Admin
 
Posts: 9376
Joined: Sun Mar 21, 2004 5:25 pm

Postby Paul van der Eijk » Mon Apr 24, 2006 6:16 pm

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
Paul van der Eijk
 
Posts: 7
Joined: Tue Mar 07, 2006 10:36 pm

Postby madshi » Wed Apr 26, 2006 7:56 am

Well, I guess I could relax the length checking.
madshi
Site Admin
 
Posts: 9376
Joined: Sun Mar 21, 2004 5:25 pm


Return to madBasic

Who is online

Users browsing this forum: No registered users and 1 guest