text
stringlengths
0
234
end if;
end Resize;
-- Internal - add the catalogue entry corresponding to a
-- compressed file in the Zip archive.
-- The entire catalogue will be written at the end of the zip stream,
-- and the entry as a local header just before the compressed data.
-- The entry's is mostly incomplete in the end (name, size, ...); stream
-- operations on the archive being built are not performed here,
-- see Add_Stream for that.
procedure Add_Catalogue_Entry (Info : in out Zip_Create_Info) is
begin
if Info.Last_Entry = 0 then
Info.Last_Entry := 1;
Resize (Info.Contains, 32);
else
Info.Last_Entry := Info.Last_Entry + 1;
if Info.Last_Entry > Info.Contains'Last then
-- Info.Contains is full, time to resize it!
-- We do nothing less than double the size - better than
-- whatever offer you'd get in your e-mails.
Resize (Info.Contains, Info.Contains'Last * 2);
end if;
end if;
declare
Cfh : Central_File_Header renames Info.Contains (Info.Last_Entry).Head;
begin
-- Administration
Cfh.Made_By_Version := 20; -- Version 2.0
Cfh.Comment_Length := 0;
Cfh.Disk_Number_Start := 0;
Cfh.Internal_Attributes := 0; -- 0: binary; 1: text
Cfh.External_Attributes := 0;
Cfh.Short_Info.Needed_Extract_Version := 10; -- Value put by Zip/PKZip
Cfh.Short_Info.Bit_Flag := 0;
Function Definition: procedure Dispose_Buffer is new Ada.Unchecked_Deallocation (Byte_Buffer, P_Byte_Buffer);
Function Body: Inbuf : P_Byte_Buffer; -- I/O buffers
Outbuf : P_Byte_Buffer;
Inbufidx : Positive; -- Points to next char in buffer to be read
Outbufidx : Positive := 1; -- Points to next free space in output buffer
Maxinbufidx : Natural; -- Count of valid chars in input buffer
Inputeof : Boolean; -- End of file indicator
procedure Read_Block is
begin
Zip.Blockread (Stream => Input, Buffer => Inbuf.all, Actually_Read => Maxinbufidx);
Inputeof := Maxinbufidx = 0;
Inbufidx := 1;
end Read_Block;
-- Exception for the case where compression works but produces
-- a bigger file than the file to be compressed (data is too "random").
Compression_Inefficient : exception;
procedure Write_Block is
Amount : constant Integer := Outbufidx - 1;
begin
Output_Size := Output_Size + File_Size_Type (Integer'Max (0, Amount));
if Output_Size >= Input_Size then
-- The compression so far is obviously inefficient for that file.
-- Useless to go further.
-- Stop immediately before growing the file more than the
-- uncompressed size.
raise Compression_Inefficient;
end if;
Zip.Blockwrite (Output, Outbuf (1 .. Amount));
Outbufidx := 1;
end Write_Block;
procedure Put_Byte (B : Byte) is -- Put a byte, at the byte granularity level
pragma Inline (Put_Byte);
begin
Outbuf (Outbufidx) := B;
Outbufidx := Outbufidx + 1;
if Outbufidx > Outbuf'Last then
Write_Block;
end if;
end Put_Byte;
procedure Flush_Byte_Buffer is
begin
if Outbufidx > 1 then
Write_Block;
end if;
end Flush_Byte_Buffer;
------------------------------------------------------
-- Bit code buffer, for sending data at bit level --
------------------------------------------------------
-- Output buffer. Bits are inserted starting at the right (least
-- significant bits). The width of bit_buffer must be at least 16 bits
subtype U32 is Unsigned_32;
Bit_Buffer : U32 := 0;
-- Number of valid bits in bit_buffer. All bits above the last valid bit are always zero
Valid_Bits : Integer := 0;