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.   type Local_File_Header is record 
  63.     -- PK34                                --  1.. 4 
  64.     needed_extract_version : Unsigned_16;  --  5.. 6 
  65.     bit_flag, 
  66.     zip_type               : Unsigned_16; 
  67.     file_timedate          : Time; 
  68.     dd                     : Data_descriptor; 
  69.     filename_length, 
  70.     extra_field_length     : Unsigned_16; 
  71.   end record; 
  72.  
  73.   local_header_length: constant:= 30; 
  74.  
  75.   procedure Read_and_check( 
  76.     stream : in  Zipstream_Class; 
  77.     header : out Local_File_Header 
  78.   ); 
  79.  
  80.   bad_local_header: exception; 
  81.  
  82.   procedure Write( 
  83.     stream : in Zipstream_Class; 
  84.     header : in Local_File_Header 
  85.   ); 
  86.  
  87.   ------------------------------------------------------- 
  88.   -- PKZIP file header, as in central directory - PK12 -- 
  89.   ------------------------------------------------------- 
  90.   -- NB: a central header contains a local header in the middle 
  91.  
  92.   type Central_File_Header is record 
  93.     -- PK12                                  --  1.. 4 
  94.     made_by_version     : Unsigned_16;       --  5.. 6 
  95.     short_info          : Local_File_Header; --  7..32 
  96.     comment_length      : Unsigned_16;       -- 33..34 
  97.     disk_number_start   : Unsigned_16; 
  98.     internal_attributes : Unsigned_16; -- internal properties of data 
  99.     external_attributes : Unsigned_32; -- 1st byte if MS-DOS: see below 
  100.     local_header_offset : Unsigned_32; 
  101.   end record; 
  102.  
  103.   -- MS-DOS external attributes: 
  104.   -- 
  105.   --   Bit 0     Read-Only 
  106.   --   Bit 1     Hidden 
  107.   --   Bit 2     System 
  108.   --   Bit 3     Volume Label 
  109.   --   Bit 4     Directory 
  110.   --   Bit 5     Archive 
  111.  
  112.   central_header_length: constant:= 46; 
  113.  
  114.   procedure Read_and_check( 
  115.     stream : in     Zipstream_Class; 
  116.     header :    out Central_File_Header 
  117.   ); 
  118.  
  119.   bad_central_header: exception; 
  120.  
  121.   procedure Write( 
  122.     stream : in     Zipstream_Class; 
  123.     header : in     Central_File_Header 
  124.   ); 
  125.  
  126.   ------------------------------------------- 
  127.   -- PKZIP end-of-central-directory - PK56 -- 
  128.   ------------------------------------------- 
  129.  
  130.   type End_of_Central_Dir is record 
  131.     -- PK56                           --  1.. 4 
  132.     disknum            : Unsigned_16; --  5.. 6 
  133.     disknum_with_start : Unsigned_16; 
  134.     disk_total_entries : Unsigned_16; 
  135.     total_entries      : Unsigned_16; 
  136.     central_dir_size   : Unsigned_32; 
  137.     central_dir_offset : Unsigned_32; 
  138.     main_comment_length: Unsigned_16; 
  139.   end record; 
  140.  
  141.   end_of_central_dir_length : constant:= 22; 
  142.  
  143.   -- This header needs to be read in special 
  144.   -- ways (see Load) -> access to a buffer 
  145.   procedure Copy_and_check( 
  146.     buffer  : in     Byte_Buffer; 
  147.     the_end :    out End_of_Central_Dir 
  148.   ); 
  149.  
  150.   procedure Read_and_check( 
  151.     stream  : in     Zipstream_Class; 
  152.     the_end :    out End_of_Central_Dir 
  153.   ); 
  154.  
  155.   bad_end: exception; 
  156.  
  157.   -- A bit more elaborated: from an open file (not a stream), 
  158.   -- find the End-of-Central-dir and load it; keep the file open. 
  159.   procedure Load( 
  160.     stream : in     Zipstream_Class; 
  161.     the_end:    out End_of_Central_Dir 
  162.     ); 
  163.  
  164.   procedure Write( 
  165.     stream  : in     Zipstream_Class; 
  166.     the_end : in     End_of_Central_Dir 
  167.   ); 
  168.  
  169. end Zip.Headers;