1. --  ________  ___   ______       ______     ___ 
  2. -- /___..._/  |.|   |.___.\     /. __ .\  __|.|   ____ 
  3. --    /../    |.|   |.____/     |.|__|.| /....|  __\..\ 
  4. --  _/../___  |.|   |.|    ===  |..__..||. = .| | = ..| 
  5. -- /_______/  |_|  /__|        /__|  |_| \__\_|  \__\_| 
  6.  
  7. -- This package provides: 
  8. -- 
  9. -- * Definiton of PKZIP information structures (cf appnote.txt), 
  10. -- * Reading a header from a data stream (Read_and_check), 
  11. -- * Copying a header from a buffer (Copy_and_check) 
  12. -- * Writing a header to a data stream (Write) 
  13.  
  14. -- Change log: 
  15. -- ========== 
  16. -- 16-Nov-2009: GdM: Replaced Ada.Calendar.Time by Zip.Time in headers, due to 
  17. --                   perf. issues in some run-times' Ada.Calendar.Time_Of 
  18.  
  19. with Interfaces; 
  20. with Zip_Streams;  use Zip_Streams; 
  21.  
  22. package Zip.Headers is 
  23.  
  24.   use Interfaces; 
  25.  
  26.   ---------------------------------------------------------------------- 
  27.   -- PKZIP data descriptor, put after streamed compressed data - PK78 -- 
  28.   ---------------------------------------------------------------------- 
  29.  
  30.   type Data_descriptor is record 
  31.     -- PK78                           --  1.. 4 
  32.     crc_32             : Unsigned_32; --  5.. 8 
  33.     compressed_size, 
  34.     uncompressed_size  : Unsigned_32; 
  35.   end record; 
  36.  
  37.   data_descriptor_length : constant:= 16; 
  38.  
  39.   -- This header needs to be read in continuation of 
  40.   -- the compressed data -> access to a buffer 
  41.   procedure Copy_and_check( 
  42.     buffer        : in     Byte_Buffer; 
  43.     the_data_desc :    out Data_descriptor 
  44.   ); 
  45.  
  46.   procedure Read_and_check( 
  47.     stream        : in     Zipstream_Class; 
  48.     the_data_desc :    out Data_descriptor 
  49.   ); 
  50.  
  51.   bad_data_descriptor: exception; 
  52.  
  53.   procedure Write( 
  54.     stream        : in     Zipstream_Class; 
  55.     the_data_desc : in     Data_descriptor 
  56.   ); 
  57.  
  58.   ----------------------------------------------------------------------- 
  59.   -- PKZIP local file header, in front of every file in archive - PK34 -- 
  60.   ----------------------------------------------------------------------- 
  61.  
  62.   Language_Encoding_Flag_Bit : constant := 2**11; 
  63.  
  64.   type Local_File_Header is record 
  65.     -- PK34                                --  1.. 4 
  66.     needed_extract_version : Unsigned_16;  --  5.. 6 
  67.     bit_flag, 
  68.     zip_type               : Unsigned_16; 
  69.     file_timedate          : Time; 
  70.     dd                     : Data_descriptor; 
  71.     filename_length, 
  72.     extra_field_length     : Unsigned_16; 
  73.   end record; 
  74.  
  75.   local_header_length: constant:= 30; 
  76.  
  77.   procedure Read_and_check( 
  78.     stream : in  Zipstream_Class; 
  79.     header : out Local_File_Header 
  80.   ); 
  81.  
  82.   bad_local_header: exception; 
  83.  
  84.   procedure Write( 
  85.     stream : in Zipstream_Class; 
  86.     header : in Local_File_Header 
  87.   ); 
  88.  
  89.   ------------------------------------------------------- 
  90.   -- PKZIP file header, as in central directory - PK12 -- 
  91.   ------------------------------------------------------- 
  92.   -- NB: a central header contains a local header in the middle 
  93.  
  94.   type Central_File_Header is record 
  95.     -- PK12                                  --  1.. 4 
  96.     made_by_version     : Unsigned_16;       --  5.. 6 
  97.     short_info          : Local_File_Header; --  7..32 
  98.     comment_length      : Unsigned_16;       -- 33..34 
  99.     disk_number_start   : Unsigned_16; 
  100.     internal_attributes : Unsigned_16; -- internal properties of data 
  101.     external_attributes : Unsigned_32; -- 1st byte if MS-DOS: see below 
  102.     local_header_offset : Unsigned_32; 
  103.   end record; 
  104.  
  105.   -- MS-DOS external attributes: 
  106.   -- 
  107.   --   Bit 0     Read-Only 
  108.   --   Bit 1     Hidden 
  109.   --   Bit 2     System 
  110.   --   Bit 3     Volume Label 
  111.   --   Bit 4     Directory 
  112.   --   Bit 5     Archive 
  113.  
  114.   central_header_length: constant:= 46; 
  115.  
  116.   procedure Read_and_check( 
  117.     stream : in     Zipstream_Class; 
  118.     header :    out Central_File_Header 
  119.   ); 
  120.  
  121.   bad_central_header: exception; 
  122.  
  123.   procedure Write( 
  124.     stream : in     Zipstream_Class; 
  125.     header : in     Central_File_Header 
  126.   ); 
  127.  
  128.   ------------------------------------------- 
  129.   -- PKZIP end-of-central-directory - PK56 -- 
  130.   ------------------------------------------- 
  131.  
  132.   type End_of_Central_Dir is record 
  133.     -- PK56                           --  1.. 4 
  134.     disknum            : Unsigned_16; --  5.. 6 
  135.     disknum_with_start : Unsigned_16; 
  136.     disk_total_entries : Unsigned_16; 
  137.     total_entries      : Unsigned_16; 
  138.     central_dir_size   : Unsigned_32; 
  139.     central_dir_offset : Unsigned_32; 
  140.     main_comment_length: Unsigned_16; 
  141.   end record; 
  142.  
  143.   end_of_central_dir_length : constant:= 22; 
  144.  
  145.   -- This header needs to be read in special 
  146.   -- ways (see Load) -> access to a buffer 
  147.   procedure Copy_and_check( 
  148.     buffer  : in     Byte_Buffer; 
  149.     the_end :    out End_of_Central_Dir 
  150.   ); 
  151.  
  152.   procedure Read_and_check( 
  153.     stream  : in     Zipstream_Class; 
  154.     the_end :    out End_of_Central_Dir 
  155.   ); 
  156.  
  157.   bad_end: exception; 
  158.  
  159.   -- A bit more elaborated: from an open file (not a stream), 
  160.   -- find the End-of-Central-dir and load it; keep the file open. 
  161.   procedure Load( 
  162.     stream : in     Zipstream_Class; 
  163.     the_end:    out End_of_Central_Dir 
  164.     ); 
  165.  
  166.   procedure Write( 
  167.     stream  : in     Zipstream_Class; 
  168.     the_end : in     End_of_Central_Dir 
  169.   ); 
  170.  
  171. end Zip.Headers;