text
stringlengths
0
234
z : Natural:= 0; -- number of entries in current table
el : Integer; -- length of eob code=code 256
no_copy_length_array: constant Boolean:= d'Length=0 or e'Length=0;
begin
if full_trace then
Ada.Text_IO.Put("[HufT_Build...");
end if;
tl:= null;
if b'Length > 256 then -- set length of EOB code, if any
el := Natural(b(256));
else
el := b_max;
end if;
-- Generate counts for each bit length
for k in b'Range loop
if b(k) > b_max then
-- m := 0; -- GNAT 2005 doesn't like it (warning).
raise huft_error;
end if;
count( Natural(b(k)) ):= count( Natural(b(k)) ) + 1;
end loop;
if count(0) = b'Length then
m := 0;
huft_incomplete:= False; -- spotted by Tucker Taft, 19-Aug-2004
return; -- complete
end if;
-- Find minimum and maximum length, bound m by those
j := 1;
while j <= b_max and then count(j) = 0 loop
j:= j + 1;
end loop;
kcc := j;
if m < j then
m := j;
end if;
i := b_max;
while i > 0 and then count(i) = 0 loop
i:= i - 1;
end loop;
g := i;
if m > i then
m := i;
end if;
-- Adjust last length count to fill out codes, if needed
y := Integer( Shift_Left(Unsigned_32'(1), j) ); -- y:= 2 ** j;
while j < i loop
y := y - count(j);
if y < 0 then
raise huft_error;
end if;
y:= y * 2;
j:= j + 1;
end loop;
y:= y - count(i);
if y < 0 then
raise huft_error;
end if;
count(i):= count(i) + y;
-- Generate starting offsets into the value table for each length
offset(1) := 0;
j:= 0;
for idx in 2..i loop
j:= j + count( idx-1 );
offset( idx ) := j;
end loop;
-- Make table of values in order of bit length
for idx in b'Range loop
j := Natural(b(idx));
if j /= 0 then
v( offset(j) ) := idx-b'First;
offset(j):= offset(j) + 1;
end if;
end loop;
-- Generate huffman codes and for each, make the table entries
code_stack(0) := 0;
i := 0;
v_idx:= v'First;
bits(-1) := 0;
-- go through the bit lengths (kcc already is bits in shortest code)
for k in kcc .. g loop
for am1 in reverse 0 .. count(k)-1 loop -- a counts codes of length k