Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2024-12

freepascal lz4 library?

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

TLDR; i'm looking for a (small?) Pascal library to handle lz4 data blocks contained inside a data file...

i've looked for information on how to create and open blocks of lz4 compressed data in data files... i haven't found much and i'm not sure i'm searching properly... asking uncle google about "freepascal library to decompress lz4 data blocks" seemed like it would be specific enough but nope...

i have a data file provided by another party... this file is made of multiple lz4 blocks with a custom header at the beginning of each block... to process each block, we read the custom header which contains a 4 byte magic number, 4 byte compressed block size, 4 byte decompressed block size, and a 4 byte "separator"... the data for the compressed block follows till the end of the compressed block size... then we simply loop through each of the blocks until the end of the file...

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...

it has been over a decade, now, since i last wrote any Pascal code and that was 35+ years of on and off writing command line text mode stuff... i plan to start this project as a command line text mode application and then, hopefully, easily add a clicky GUI to it or wrap a clicky GUI around the guts of the file handling and (de)compressing code...

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list where it belongs!*

Reply by email

Wayne Sherman
to Unknown author
TLDR; i'm looking for a (small?) Pascal library to handle lz4 data blocks contained inside a data file...

These use the lz4 library with pascal bindings (.a and.o files which can be compiled into your executable). One is written for Delphi but might work with some changes:

LZ4 for Lazarus, Windows only, no DLL
https://forum.lazarus.freepascal.org/index.php/topic,66391.0.html

lz4-delphi - Delphi bindings for lz4
https://github.com/Hugie/lz4-delphi
https://stackoverflow.com/questions/42672056/decompress-lz4-stream-of-unknown-size

Reply by email

Unknown author
to Wayne Sherman
Wayne Sherman wrote 14 lines
On Mon, Dec 2, 2024 at 4:16 AM wkitty42--- wrote: > TLDR; i'm looking for a (small?) Pascal library to handle lz4 data blocks > contained inside a data file... These use the lz4 library with pascal bindings (.a and.o files which can be compiled into your executable). One is written for Delphi but might work with some changes: LZ4 for Lazarus, Windows only, no DLL https://forum.lazarus.freepascal.org/index.php/topic,66391.0.html lz4-delphi - Delphi bindings for lz4 https://github.com/Hugie/lz4-delphi https://stackoverflow.com/questions/42672056/decompress-lz4-stream-of-unknown-size

thanks... i've seen the stackoverflow one and others from there but none have been very helpful...

i should have also mentioned that i need a solution for linux since that's my main OS... targets for windows and macOS may come later, though...

it may be possible for the Delphi converter in Lazarus to convert Hugie's Delphi stuff to something i can use... hummm... that may be another rabbit hole i don't want to get into, though...

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list where it belongs!*

Reply by email

José Mejuto
to Unknown author: TLDR; i'm looking for a (small?) Pascal library to handle lz4 data blocks contained…
wkitty42--- wrote 9 lines
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;

Reply by email