[fpc-pascal] freepascal lz4 library?

José Mejuto joshyfun at gmail.com
Mon Dec 2 19:19:37 CET 2024


El 02/12/2024 a las 13:16, wkitty42--- via fpc-pascal escribió:
>
> i can do this in python3 but i would rather do it in Pascal because i 
> have an idea for a project that i think i want to write in Pascal...  
> Freepascal and possibly Lazarus are my weapons and the only thing i 
> cannot find is the lz4 ammo library i can use... once i figure this 
> out, the next goal is figuring out how to read and write the contained 
> JSON data...
>
>

Hello,


If you lz4 blocks does not use dictionary you can try this code to 
decompress each block:


*** (TOTALLY UNTESTED CODE) ***

Converted from

https://github.com/jart/cosmopolitan/blob/467504308a103865c058f9a0ac858cc22e72240e/libc/nexgen32e/lz4cpy.c

With this notes:

/**
  * Decompresses LZ4 block.
  *
  * This is a 103 byte implementation of the LZ4 algorithm. Please note
  * LZ4 files are comprised of multiple frames, which may be decoded
  * together using the wrapper function lz4decode().
  *
  * @see rldecode() for a 16-byte decompressor
  */


***--------------------------------------------------***

function Lz4Cpy(dest: Pointer; const blockdata: Pointer; blocksize: 
SizeUInt): Pointer;
var
   op, ip, ipe, match: PByte;
   token, length, fifteen, offset, matchlen: Byte;
begin
   op := dest;
   ip := blockdata;
   ipe := ip + blocksize;

   fifteen := 15; // Equivalente a pushpop(15) en este caso.

   while True do
   begin
     token := ip^;
     Inc(ip);
     length := token shr 4;

     if length = fifteen then
     begin
       repeat
         Inc(length, ip^);
         Inc(ip);
       until ip^ <> 255;
     end;

     Move(ip^, op^, length);
     Inc(op, length);
     Inc(ip, length);

     if NativeUInt(ip) >= NativeUInt(ipe) then
       Break;

     offset := PWord(ip)^;
     Inc(ip, 2);

     matchlen := token and fifteen;
     if matchlen = fifteen then
     begin
       repeat
         Inc(matchlen, ip^);
         Inc(ip);
       until ip^ <> 255;
     end;

     match := op - offset;
     Inc(matchlen, 4);

     Move(match^, op^, matchlen);
     Inc(op, matchlen);
   end;

   Result := op;
end;



More information about the fpc-pascal mailing list