text
stringlengths
0
234
type Emission_Mode is (Simulate, Effective);
procedure Emit_Data_Compression_Structures (Mode : Emission_Mode) is
procedure Emit_Data_Compression_Atom (X : Alphabet; Extra_Code : U32 := 0) is
-- x is a bit length (value in 0..15), or a RLE instruction
begin
case Mode is
when Simulate =>
Truc_Freq (X) := Truc_Freq (X) + 1; -- +1 for x's histogram bar
when Effective =>
Put_Huffman_Code (Truc (X));
declare
Extra_Bits : constant Natural := Extra_Bits_Needed (X);
begin
if Extra_Bits > 0 then
Put_Code (Extra_Code, Extra_Bits);
end if;
Function Definition: procedure Llhcl is new Length_Limited_Huffman_Code_Lengths
Function Body: (Alphabet,
Natural,
Alpha_Array,
Alpha_Array,
7);
A_Non_Zero : Alphabet;
begin
Concatenate_All_Bit_Lengths;
Truc_Freq := (others => 0);
Emit_Data_Compression_Structures (Simulate);
-- We have now statistics of all bit lengths occurrences of both Huffman
-- trees used for compressing the data.
-- We turn these counts into bit lengths for the local tree
-- that helps us to store the compression structure in a more compact form.
Llhcl (Truc_Freq, Truc_Bl); -- Call the magic algorithm for setting up Huffman lengths
-- At least lengths for codes 16, 17, 18, 0 will always be sent,
-- even if all other bit lengths are 0 because codes 1 to 15 are unused.
A_Non_Zero := 3;
for A in Alphabet loop
if A > A_Non_Zero and then Truc_Bl (Alphabet_Permutation (A)) > 0 then
A_Non_Zero := A;
end if;
end loop;
if Cost_Analysis then
-- In this mode, no data output: we sum up the exact
-- number of bits needed by the compression header.
Bits := Bits + 14 + Count_Type (1 + A_Non_Zero) * 3;
for A in Alphabet loop
Bits := Bits + Count_Type (Truc_Freq (A) * (Truc_Bl (A) + Extra_Bits_Needed (A)));
end loop;
else
-- We output the compression header to the output stream.
for A in Alphabet loop
Truc (A).Bit_Length := Truc_Bl (A);
end loop;
Prepare_Huffman_Codes (Truc);
-- Output of the compression structure
Put_Code
(U32 (Max_Used_Lln_Code - 256),
5); -- max_used_lln_code is always >= 256 = EOB code
Put_Code (U32 (Max_Used_Dis_Code), 5);
Put_Code (U32 (A_Non_Zero - 3), 4);
-- Save the local alphabet's Huffman lengths. It's the compression structure
-- for compressing the data compression structure. Easy, isn't it ?
for A in 0 .. A_Non_Zero loop
Put_Code (U32 (Truc (Alphabet_Permutation (A)).Bit_Length), 3);
end loop;
-- Emit the Huffman lengths for encoding the data, in the local Huffman-encoded fashion.
Emit_Data_Compression_Structures (Effective);
end if;
end Put_Compression_Structure;
End_Of_Block : constant := 256;
-- Default Huffman trees, for "fixed" blocks, as defined in appnote.txt or RFC 1951
Default_Lit_Len_Bl : constant Bit_Length_Array_Lit_Len :=
(0 .. 143 => 8, -- For literals ("plain text" bytes)
144 .. 255 => 9, -- For more literals ("plain text" bytes)
End_Of_Block => 7, -- For EOB (256)
257 .. 279 => 7, -- For length codes
280 .. 287 => 8); -- For more length codes
Default_Dis_Bl : constant Bit_Length_Array_Dis := (others => 5);
Deflate_Fixed_Descriptors : constant Deflate_Huff_Descriptors :=
Prepare_Huffman_Codes (Build_Descriptors (Default_Lit_Len_Bl, Default_Dis_Bl));
-- Current tree descriptors
Curr_Descr : Deflate_Huff_Descriptors := Deflate_Fixed_Descriptors;
-- Write a normal, "clear-text" (post LZ, pre Huffman), 8-bit character (literal)
procedure Put_Literal_Byte (B : Byte) is
begin
Put_Huffman_Code (Curr_Descr.Lit_Len (Integer (B)));
end Put_Literal_Byte;
-- Possible ranges for distance and length encoding in the Zip-Deflate format:
subtype Length_Range is Integer range 3 .. 258;
subtype Distance_Range is Integer range 1 .. 32768;
-- This is where LZ distance-length tokens are written to the output stream.
-- The Deflate format defines a sort of logarithmic compression, with codes