text
stringlengths
0
234
Lz_Expanded : Expanded_Data;
end record;
-- *Tuned*. Min: 2**14, = 16384 (min half buffer 8192)
-- Optimal so far: 2**17
Lz_Buffer_Size : constant := 2**17;
type Lz_Buffer_Index_Type is mod Lz_Buffer_Size;
type Lz_Buffer_Type is array (Lz_Buffer_Index_Type range <>) of Lz_Atom;
Empty_Lit_Len_Stat : constant Stats_Lit_Len_Type := (End_Of_Block => 1, others => 0);
-- End_Of_Block will have to happen once, but never appears in the LZ statistics...
Empty_Dis_Stat : constant Stats_Dis_Type := (others => 0);
-- Compute statistics for both Literal-length, and Distance alphabets,
-- from a LZ buffer
procedure Get_Statistics
(Lzb : in Lz_Buffer_Type;
Stats_Lit_Len : out Stats_Lit_Len_Type;
Stats_Dis : out Stats_Dis_Type)
is
Lit_Len : Alphabet_Lit_Len;
Dis : Alphabet_Dis;
begin
Stats_Lit_Len := Empty_Lit_Len_Stat;
Stats_Dis := Empty_Dis_Stat;
for I in Lzb'Range loop
case Lzb (I).Kind is
when Plain_Byte =>
Lit_Len := Alphabet_Lit_Len (Lzb (I).Plain);
Stats_Lit_Len (Lit_Len) :=
Stats_Lit_Len (Lit_Len) + 1; -- +1 for this literal
when Distance_Length =>
Lit_Len := Deflate_Code_For_Lz_Length (Lzb (I).Lz_Length);
Stats_Lit_Len (Lit_Len) :=
Stats_Lit_Len (Lit_Len) + 1; -- +1 for this length code
Dis := Deflate_Code_For_Lz_Distance (Lzb (I).Lz_Distance);
Stats_Dis (Dis) :=
Stats_Dis (Dis) + 1; -- +1 for this distance code
end case;
end loop;
end Get_Statistics;
-- Send a LZ buffer using currently defined Huffman codes
procedure Put_Lz_Buffer (Lzb : Lz_Buffer_Type) is
begin
for I in Lzb'Range loop
case Lzb (I).Kind is
when Plain_Byte =>
Put_Literal_Byte (Lzb (I).Plain);
when Distance_Length =>
Put_Dl_Code (Lzb (I).Lz_Distance, Lzb (I).Lz_Length);
end case;
end loop;
end Put_Lz_Buffer;
Block_To_Finish : Boolean := False;
Last_Block_Marked : Boolean := False;
type Block_Type is (Stored, Fixed, Dynamic, Reserved); -- Appnote, 5.5.2
-- If last_block_type = dynamic, we may recycle previous block's Huffman codes
Last_Block_Type : Block_Type := Reserved;
procedure Mark_New_Block (Last_Block_For_Stream : Boolean) is
begin
if Block_To_Finish and Last_Block_Type in Fixed .. Dynamic then
Put_Huffman_Code (Curr_Descr.Lit_Len (End_Of_Block)); -- Finish previous block
end if;
Block_To_Finish := True;
Put_Code (Code => Boolean'Pos (Last_Block_For_Stream), Code_Size => 1);
Last_Block_Marked := Last_Block_For_Stream;
end Mark_New_Block;
-- Send a LZ buffer completely decoded as literals (LZ compression is discarded)
procedure Expand_Lz_Buffer (Lzb : Lz_Buffer_Type; Last_Block : Boolean) is
B1, B2 : Byte;
To_Be_Sent : Natural_M32 := 0;
-- To_Be_Sent is not always equal to lzb'Length: sometimes you have a DL code
Mid : Lz_Buffer_Index_Type;
begin
for I in Lzb'Range loop
case Lzb (I).Kind is
when Plain_Byte =>
To_Be_Sent := To_Be_Sent + 1;
when Distance_Length =>
To_Be_Sent := To_Be_Sent + Natural_M32 (Lzb (I).Lz_Length);
end case;
end loop;
if To_Be_Sent > 16#FFFF# then -- Ow, cannot send all that in one chunk.
-- Instead of a tedious block splitting, just divide and conquer:
Mid := Lz_Buffer_Index_Type ((Natural_M32 (Lzb'First) + Natural_M32 (Lzb'Last)) / 2);
Expand_Lz_Buffer (Lzb (Lzb'First .. Mid), Last_Block => False);
Expand_Lz_Buffer (Lzb (Mid + 1 .. Lzb'Last), Last_Block => Last_Block);
return;
end if;
B1 := Byte (To_Be_Sent mod 256);
B2 := Byte (To_Be_Sent / 256);
Mark_New_Block (Last_Block_For_Stream => Last_Block);
Last_Block_Type := Stored;
Put_Code (Code => 0, Code_Size => 2); -- Signals a "stored" block
Flush_Bit_Buffer; -- Go to byte boundary
Put_Byte (B1);