home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / PKZ102_2.EXE / APPNOTE.TXT next >
Text File  |  1989-07-21  |  26KB  |  752 lines

  1.  
  2. Disclaimer
  3. ----------
  4.  
  5. Although PKWARE will attempt to supply current and accurate
  6. information relating to its file formats, algorithms, and the
  7. subject programs, the possibility of error can not be eliminated.
  8. PKWARE therefore expressly disclaims any warranty that the
  9. information contained in the associated materials relating to the
  10. subject programs and/or the format of the files created or
  11. accessed by the subject programs and/or the algorithms used by
  12. the subject programs, or any other matter, is current, correct or
  13. accurate as delivered.  Any risk of damage due to any possible
  14. inaccurate information is assumed by the user of the information.
  15. Furthermore, the information relating to the subject programs
  16. and/or the file formats created or accessed by the subject
  17. programs and/or the algorithms used by the subject programs is
  18. subject to change without notice.
  19.  
  20.  
  21. General Format of a ZIP file
  22. ----------------------------
  23.  
  24.   Files stored in arbitrary order.  Large zipfiles can span multiple
  25.   diskette media.
  26.  
  27.   Overall zipfile format:
  28.  
  29.     [local file header+file data] . . .
  30.     [central directory] end of central directory record
  31.  
  32.  
  33.   A.  Local file header:
  34.  
  35.     local file header signature    4 bytes  (0x04034b50)
  36.     version needed to extract    2 bytes
  37.     general purpose bit flag    2 bytes
  38.     compression method        2 bytes
  39.     last mod file time         2 bytes
  40.     last mod file date        2 bytes
  41.     crc-32               4 bytes
  42.     compressed size            4 bytes
  43.     uncompressed size        4 bytes
  44.     filename length            2 bytes
  45.     extra field length        2 bytes
  46.  
  47.     filename (variable size)
  48.     extra field (variable size)
  49.  
  50.  
  51.   B.  Central directory structure:
  52.  
  53.       [file header] . . .  end of central dir record
  54.  
  55.       File header:
  56.  
  57.     central file header signature    4 bytes  (0x02014b50)
  58.     version made by            2 bytes
  59.     version needed to extract    2 bytes
  60.     general purpose bit flag    2 bytes
  61.     compression method        2 bytes
  62.     last mod file time         2 bytes
  63.     last mod file date        2 bytes
  64.     crc-32               4 bytes
  65.     compressed size            4 bytes
  66.     uncompressed size        4 bytes
  67.     filename length            2 bytes
  68.     extra field length        2 bytes
  69.     file comment length        2 bytes
  70.     disk number start        2 bytes
  71.     internal file attributes    2 bytes
  72.     external file attributes    4 bytes
  73.     relative offset of local header    4 bytes
  74.  
  75.     filename (variable size)
  76.     extra field (variable size)
  77.     file comment (variable size)
  78.  
  79.       End of central dir record:
  80.  
  81.     end of central dir signature    4 bytes  (0x06054b50)
  82.     number of this disk        2 bytes
  83.     number of the disk with the
  84.     start of the central directory    2 bytes
  85.     total number of entries in
  86.     the central dir on this disk    2 bytes
  87.     total number of entries in
  88.     the central dir            2 bytes
  89.     size of the central directory   4 bytes
  90.     offset of start of central
  91.     directory with respect to
  92.     the starting disk number    4 bytes
  93.     zipfile comment length        2 bytes
  94.     zipfile comment (variable size)
  95.  
  96.  
  97.  
  98.  
  99.   C.  Explanation of fields:
  100.  
  101.       version made by
  102.  
  103.       The upper byte indicates the host system (OS) for the
  104.       file.  Software can use this information to determine
  105.       the line record format for text files etc.  The current
  106.       mappings are:
  107.  
  108.       0 - MS-DOS and OS/2 (F.A.T. file systems)
  109.       1 - Amiga    2 - VMS        3 - *nix    4 - VM/CMS
  110.       5 - Atari ST                  6 - OS/2 1.2 extended file systems
  111.       7 - Macintosh            8 thru 255 - unused
  112.  
  113.       The lower byte indicates the version number of the
  114.       software used to encode the file.  The value/10
  115.       indicates the major version number, and the value
  116.       mod 10 is the minor version number.
  117.  
  118.       version needed to extract
  119.  
  120.       The minimum software version needed to extract the
  121.       file, mapped as above.
  122.  
  123.       general purpose bit flag:
  124.  
  125.           bit 0: If set, indicates that the file is encrypted.
  126.           bit 1: If the compression method used was type 6,
  127.          Imploding, then this bit, if set, indicates
  128.          an 8K sliding dictionary was used.  If clear,
  129.          then a 4K sliding dictionary was used.
  130.           bit 2: If the compression method used was type 6,
  131.          Imploding, then this bit, if set, indicates
  132.          an 3 Shannon-Fano trees were used to encode the
  133.          sliding dictionary output.  If clear, then 2
  134.          Shannon-Fano trees were used.
  135.       Note:  Bits 1 and 2 are undefined if the compression
  136.          method is other than type 6 (Imploding).
  137.  
  138.           The upper three bits are reserved and used internally
  139.       by the software when processing the zipfile.  The
  140.       remaining bits are unused in version 1.0.
  141.  
  142.       compression method:
  143.  
  144.       (see accompanying documentation for algorithm
  145.       descriptions)
  146.  
  147.       0 - The file is stored (no compression)
  148.       1 - The file is Shrunk
  149.       2 - The file is Reduced with compression factor 1
  150.       3 - The file is Reduced with compression factor 2
  151.       4 - The file is Reduced with compression factor 3
  152.       5 - The file is Reduced with compression factor 4
  153.           6 - The file is Imploded
  154.  
  155.       date and time fields:
  156.  
  157.       The date and time are encoded in standard MS-DOS
  158.       format.
  159.  
  160.       CRC-32:
  161.  
  162.       The CRC-32 algorithm was generously contributed by
  163.       David Schwaderer and can be found in his excellent
  164.       book "C Programmers Guide to NetBIOS" published by
  165.       Howard W. Sams & Co. Inc.  The 'magic number' for
  166.       the CRC is 0xdebb20e3.  The proper CRC pre and post
  167.       conditioning is used, meaning that the CRC register
  168.       is pre-conditioned with all ones (a starting value
  169.       of 0xffffffff) and the value is post-conditioned by
  170.       taking the one's complement of the CRC residual.
  171.     
  172.       compressed size:
  173.       uncompressed size:
  174.  
  175.       The size of the file compressed and uncompressed,
  176.       respectively.
  177.  
  178.       filename length:
  179.       extra field length:
  180.       file comment length:
  181.  
  182.       The length of the filename, extra field, and comment
  183.       fields respectively.  The combined length of any
  184.       directory record and these three fields should not
  185.       generally exceed 65,535 bytes.
  186.  
  187.       disk number start:
  188.  
  189.       The number of the disk on which this file begins.
  190.  
  191.       internal file attributes:
  192.  
  193.       The lowest bit of this field indicates, if set, that
  194.       the file is apparently an ASCII or text file.  If not
  195.       set, that the file apparently contains binary data.
  196.       The remaining bits are unused in version 1.0.
  197.  
  198.       external file attributes:
  199.  
  200.       The mapping of the external attributes is
  201.       host-system dependent (see 'version made by').  For
  202.       MS-DOS, the low order byte is the MS-DOS directory
  203.       attribute byte.
  204.  
  205.       relative offset of local header:
  206.  
  207.       This is the offset from the start of the first disk on
  208.       which this file appears, to where the local header should
  209.       be found.
  210.  
  211.       filename:
  212.  
  213.       The name of the file, with optional relative path.
  214.       The path stored should not contain a drive or
  215.       device letter, or a leading slash.  All slashes
  216.       should be forward slashes '/' as opposed to
  217.       backwards slashes '\' for compatibility with Amiga
  218.       and Unix file systems etc.
  219.  
  220.       extra field:
  221.  
  222.       This is for future expansion.  If additional information
  223.       needs to be stored in the future, it should be stored
  224.       here.  Earlier versions of the software can then safely
  225.       skip this file, and find the next file or header.  This
  226.       field will be 0 length in version 1.0.
  227.  
  228.       In order to allow different programs and different types 
  229.       of information to be stored in the 'extra' field in .ZIP 
  230.       files, the following structure should be used for all 
  231.       programs storing data in this field:
  232.  
  233.       header1+data1 + header2+data2 . . .
  234.  
  235.       Each header should consist of:
  236.  
  237.         Header ID - 2 bytes
  238.         Data Size - 2 bytes
  239.  
  240.       Note: all fields stored in Intel low-byte/high-byte order.
  241.  
  242.       The Header ID field indicates the type of data that is in 
  243.       the following data block.
  244.       
  245.       Header ID's of 0 thru 31 are reserved for use by PKWARE.  
  246.       The remaining ID's can be used by third party vendors for 
  247.       proprietary usage.
  248.  
  249.  
  250.       The Data Size field indicates the size of the following 
  251.       data block. Programs can use this value to skip to the 
  252.       next header block, passing over any data blocks that are 
  253.       not of interest.
  254.  
  255.       Note: As stated in the standard PKZIP application notes, 
  256.         the size of the entire .ZIP file header, including the 
  257.         filename, comment, and extra field should not exceed 64K 
  258.         in size.
  259.  
  260.  
  261.       In case two different programs should appropriate the same 
  262.       Header ID value, it is strongly recommended that each 
  263.       program place a unique signature of at least two bytes in 
  264.       size (and preferably 4 bytes or bigger) at the start of 
  265.       each data area.  Every program should verify that it's 
  266.       unique signature is present, in addition to the Header ID 
  267.       value being correct, before assuming that it is a block of 
  268.       known type.
  269.  
  270.  
  271.       file comment:
  272.  
  273.       The comment for this file.
  274.  
  275.  
  276.       number of this disk:
  277.  
  278.       The number of this disk, which contains central
  279.       directory end record.
  280.  
  281.       number of the disk with the start of the central directory:
  282.  
  283.       The number of the disk on which the central
  284.       directory starts.
  285.  
  286.       total number of entries in the central dir on this disk:
  287.  
  288.       The number of central directory entries on this disk.
  289.     
  290.       total number of entries in the central dir:
  291.  
  292.       The total number of files in the zipfile.
  293.  
  294.  
  295.       size of the central directory:
  296.  
  297.       The size (in bytes) of the entire central directory.
  298.  
  299.       offset of start of central directory with respect to
  300.       the starting disk number:
  301.  
  302.       Offset of the start of the central direcory on the
  303.       disk on which the central directory starts.
  304.  
  305.       zipfile comment length:
  306.  
  307.       The length of the comment for this zipfile.
  308.  
  309.       zipfile comment:
  310.  
  311.       The comment for this zipfile.
  312.  
  313.  
  314.   D.  General notes:
  315.  
  316.       1)  All fields unless otherwise noted are unsigned and stored
  317.       in Intel low-byte:high-byte, low-word:high-word order.
  318.  
  319.       2)  String fields are not null terminated, since the
  320.       length is given explicitly.
  321.  
  322.       3)  Local headers should not span disk boundries.  Also, even
  323.       though the central directory can span disk boundries, no
  324.       single record in the central directory should be split
  325.       across disks.
  326.  
  327.       4)  The entries in the central directory may not necessarily
  328.       be in the same order that files appear in the zipfile.
  329.  
  330. UnShrinking
  331. -----------
  332.  
  333. Shrinking is a Dynamic Ziv-Lempel-Welch compression algorithm
  334. with partial clearing.  The initial code size is 9 bits, and
  335. the maximum code size is 13 bits.  Shrinking differs from
  336. conventional Dynamic Ziv-lempel-Welch implementations in several
  337. respects:
  338.  
  339. 1)  The code size is controlled by the compressor, and is not
  340.     automatically increased when codes larger than the current
  341.     code size are created (but not necessarily used).  When
  342.     the decompressor encounters the code sequence 256
  343.     (decimal) followed by 1, it should increase the code size
  344.     read from the input stream to the next bit size.  No
  345.     blocking of the codes is performed, so the next code at
  346.     the increased size should be read from the input stream
  347.     immediately after where the previous code at the smaller
  348.     bit size was read.  Again, the decompressor should not
  349.     increase the code size used until the sequence 256,1 is
  350.     encountered.
  351.  
  352. 2)  When the table becomes full, total clearing is not
  353.     performed.  Rather, when the compresser emits the code
  354.     sequence 256,2 (decimal), the decompressor should clear
  355.     all leaf nodes from the Ziv-Lempel tree, and continue to
  356.     use the current code size.  The nodes that are cleared
  357.     from the Ziv-Lempel tree are then re-used, with the lowest
  358.     code value re-used first, and the highest code value
  359.     re-used last.  The compressor can emit the sequence 256,2
  360.     at any time.
  361.  
  362.  
  363.  
  364. Expanding
  365. ---------
  366.  
  367. The Reducing algorithm is actually a combination of two
  368. distinct algorithms.  The first algorithm compresses repeated
  369. byte sequences, and the second algorithm takes the compressed
  370. stream from the first algorithm and applies a probabilistic
  371. compression method.
  372.  
  373. The probabilistic compression stores an array of 'follower
  374. sets' S(j), for j=0 to 255, corresponding to each possible
  375. ASCII character.  Each set contains between 0 and 32
  376. characters, to be denoted as S(j)[0],...,S(j)[m], where m<32.
  377. The sets are stored at the beginning of the data area for a
  378. Reduced file, in reverse order, with S(255) first, and S(0)
  379. last.
  380.  
  381. The sets are encoded as { N(j), S(j)[0],...,S(j)[N(j)-1] },
  382. where N(j) is the size of set S(j).  N(j) can be 0, in which
  383. case the follower set for S(j) is empty.  Each N(j) value is
  384. encoded in 6 bits, followed by N(j) eight bit character values
  385. corresponding to S(j)[0] to S(j)[N(j)-1] respectively.  If
  386. N(j) is 0, then no values for S(j) are stored, and the value
  387. for N(j-1) immediately follows.
  388.  
  389. Immediately after the follower sets, is the compressed data
  390. stream.  The compressed data stream can be interpreted for the
  391. probabilistic decompression as follows:
  392.  
  393.  
  394. let Last-Character <- 0.
  395. loop until done
  396.     if the follower set S(Last-Character) is empty then
  397.     read 8 bits from the input stream, and copy this
  398.     value to the output stream.
  399.     otherwise if the follower set S(Last-Character) is non-empty then
  400.     read 1 bit from the input stream.
  401.     if this bit is not zero then
  402.         read 8 bits from the input stream, and copy this
  403.         value to the output stream.
  404.     otherwise if this bit is zero then
  405.         read B(N(Last-Character)) bits from the input
  406.         stream, and assign this value to I.
  407.         Copy the value of S(Last-Character)[I] to the
  408.         output stream.
  409.     
  410.     assign the last value placed on the output stream to
  411.     Last-Character.
  412. end loop
  413.  
  414.  
  415. B(N(j)) is defined as the minimal number of bits required to
  416. encode the value N(j)-1.
  417.  
  418.  
  419. The decompressed stream from above can then be expanded to
  420. re-create the original file as follows:
  421.  
  422.  
  423. let State <- 0.
  424.  
  425. loop until done
  426.     read 8 bits from the input stream into C.
  427.     case State of
  428.     0:  if C is not equal to DLE (144 decimal) then
  429.         copy C to the output stream.
  430.         otherwise if C is equal to DLE then
  431.         let State <- 1.
  432.  
  433.     1:  if C is non-zero then
  434.         let V <- C.
  435.         let Len <- L(V)
  436.         let State <- F(Len).
  437.         otherwise if C is zero then
  438.         copy the value 144 (decimal) to the output stream.
  439.         let State <- 0
  440.  
  441.     2:  let Len <- Len + C
  442.         let State <- 3.
  443.  
  444.     3:  move backwards D(V,C) bytes in the output stream
  445.         (if this position is before the start of the output
  446.         stream, then assume that all the data before the
  447.         start of the output stream is filled with zeros).
  448.         copy Len+3 bytes from this position to the output stream.
  449.         let State <- 0.
  450.     end case
  451. end loop
  452.  
  453.  
  454. The functions F,L, and D are dependent on the 'compression
  455. factor', 1 through 4, and are defined as follows:
  456.  
  457. For compression factor 1:
  458.     L(X) equals the lower 7 bits of X.
  459.     F(X) equals 2 if X equals 127 otherwise F(X) equals 3.
  460.     D(X,Y) equals the (upper 1 bit of X) * 256 + Y + 1.
  461. For compression factor 2:
  462.     L(X) equals the lower 6 bits of X.
  463.     F(X) equals 2 if X equals 63 otherwise F(X) equals 3.
  464.     D(X,Y) equals the (upper 2 bits of X) * 256 + Y + 1.
  465. For compression factor 3:
  466.     L(X) equals the lower 5 bits of X.
  467.     F(X) equals 2 if X equals 31 otherwise F(X) equals 3.
  468.     D(X,Y) equals the (upper 3 bits of X) * 256 + Y + 1.
  469. For compression factor 4:
  470.     L(X) equals the lower 4 bits of X.
  471.     F(X) equals 2 if X equals 15 otherwise F(X) equals 3.
  472.     D(X,Y) equals the (upper 4 bits of X) * 256 + Y + 1.
  473.  
  474.  
  475. Imploding
  476. ---------
  477.  
  478. The Imploding algorithm is actually a combination of two distinct
  479. algorithms.  The first algorithm compresses repeated byte
  480. sequences using a sliding dictionary.  The second algorithm is
  481. used to compress the encoding of the sliding dictionary ouput,
  482. using multiple Shannon-Fano trees.
  483.  
  484. The Imploding algorithm can use a 4K or 8K sliding dictionary
  485. size. The dictionary size used can be determined by bit 1 in the
  486. general purpose flag word, a 0 bit indicates a 4K dictionary
  487. while a 1 bit indicates an 8K dictionary.
  488.  
  489. The Shannon-Fano trees are stored at the start of the compressed
  490. file. The number of trees stored is defined by bit 2 in the
  491. general purpose flag word, a 0 bit indicates two trees stored, a
  492. 1 bit indicates three trees are stored.  If 3 trees are stored,
  493. the first Shannon-Fano tree represents the encoding of the
  494. Literal characters, the second tree represents the encoding of
  495. the Length information, the third represents the encoding of the
  496. Distance information.  When 2 Shannon-Fano trees are stored, the
  497. Length tree is stored first, followed by the Distance tree.
  498.  
  499. The Literal Shannon-Fano tree, if present is used to represent
  500. the entire ASCII character set, and contains 256 values.  This
  501. tree is used to compress any data not compressed by the sliding
  502. dictionary algorithm.  When this tree is present, the Minimum
  503. Match Length for the sliding dictionary is 3.  If this tree is
  504. not present, the Minimum Match Length is 2.
  505.  
  506. The Length Shannon-Fano tree is used to compress the Length part
  507. of the (length,distance) pairs from the sliding dictionary
  508. output. The Length tree contains 64 values, ranging from the
  509. Minimum Match Length, to 63 plus the Minimum Match Length.
  510.  
  511. The Distance Shannon-Fano tree is used to compress the Distance
  512. part of the (length,distance) pairs from the sliding dictionary
  513. output. The Distance tree contains 64 values, ranging from 0 to
  514. 63, representing the upper 6 bits of the distance value.  The
  515. distance values themselves will be between 0 and the sliding
  516. dictionary size, either 4K or 8K.
  517.  
  518. The Shannon-Fano trees themselves are stored in a compressed
  519. format. The first byte of the tree data represents the number of
  520. bytes of data representing the (compressed) Shannon-Fano tree
  521. minus 1.  The remaining bytes represent the Shannon-Fano tree
  522. data encoded as:
  523.  
  524.     High 4 bits: Number of values at this bit length + 1. (1 - 16)
  525.     Low  4 bits: Bit Length needed to represent value + 1. (1 - 16)
  526.  
  527. The Shannon-Fano codes can be constructed from the bit lengths
  528. using the following algorithm:
  529.  
  530. 1)  Sort the Bit Lengths in ascending order, while retaining the
  531.     order of the original lengths stored in the file.
  532.  
  533. 2)  Generate the Shannon-Fano trees:
  534.  
  535.     Code <- 0
  536.     CodeIncrement <- 0
  537.     LastBitLength <- 0
  538.     i <- number of Shannon-Fano codes - 1   (either 255 or 63)
  539.  
  540.     loop while i >= 0
  541.     Code = Code + CodeIncrement
  542.     if BitLength(i) <> LastBitLength then
  543.         LastBitLength=BitLength(i)
  544.         CodeIncrement = 1 shifted left (16 - LastBitLength)
  545.     ShannonCode(i) = Code
  546.     i <- i - 1
  547.     end loop
  548.  
  549.  
  550. 3)  Reverse the order of all the bits in the above ShannonCode()
  551.     vector, so that the most significant bit becomes the least
  552.     significant bit.  For example, the value 0x1234 (hex) would
  553.     become 0x2C48 (hex).
  554.  
  555. 4)  Restore the order of Shannon-Fano codes as originally stored
  556.     within the file.
  557.  
  558. Example:
  559.  
  560.     This example will show the encoding of a Shannon-Fano tree
  561.     of size 8.  Notice that the actual Shannon-Fano trees used
  562.     for Imploding are either 64 or 256 entries in size.
  563.  
  564. Example:   0x02, 0x42, 0x01, 0x13
  565.  
  566.     The first byte indicates 3 values in this table.  Decoding the
  567.     bytes:
  568.         0x42 = 5 codes of 3 bits long
  569.         0x01 = 1 code  of 2 bits long
  570.         0x13 = 2 codes of 4 bits long
  571.  
  572.     This would generate the original bit length array of:
  573.     (3, 3, 3, 3, 3, 2, 4, 4)
  574.  
  575.     There are 8 codes in this table for the values 0 thru 7.  Using the
  576.     algorithm to obtain the Shannon-Fano codes produces:
  577.  
  578.                                   Reversed     Order     Original
  579. Val  Sorted   Constructed Code      Value     Restored    Length
  580. ---  ------   -----------------   --------    --------    ------
  581. 0:     2      1100000000000000        11       101          3
  582. 1:     3      1010000000000000       101       001          3
  583. 2:     3      1000000000000000       001       110          3
  584. 3:     3      0110000000000000       110       010          3
  585. 4:     3      0100000000000000       010       100          3
  586. 5:     3      0010000000000000       100        11          2
  587. 6:     4      0001000000000000      1000      1000          4
  588. 7:     4      0000000000000000      0000      0000          4
  589.  
  590.  
  591. The values in the Val, Order Restored and Original Length columns
  592. now represent the Shannon-Fano encoding tree that can be used for
  593. decoding the Shannon-Fano encoded data.  How to parse the
  594. variable length Shannon-Fano values from the data stream is beyond the
  595. scope of this document.  (See the references listed at the end of
  596. this document for more information.)  However, traditional decoding
  597. schemes used for Huffman variable length decoding, such as the
  598. Greenlaw algorithm, can be succesfully applied.
  599.  
  600. The compressed data stream begins immediately after the
  601. compressed Shannon-Fano data.  The compressed data stream can be
  602. interpreted as follows:
  603.  
  604. loop until done
  605.     read 1 bit from input stream.
  606.  
  607.     if this bit is non-zero then       (encoded data is literal data)
  608.     if Literal Shannon-Fano tree is present
  609.         read and decode character using Literal Shannon-Fano tree.
  610.     otherwise
  611.         read 8 bits from input stream.
  612.     copy character to the output stream.
  613.     otherwise                   (encoded data is sliding dictionary match)
  614.     if 8K dictionary size
  615.         read 7 bits for offset Distance (lower 7 bits of offset).
  616.     otherwise
  617.         read 6 bits for offset Distance (lower 6 bits of offset).
  618.     
  619.     using the Distance Shannon-Fano tree, read and decode the
  620.       upper 6 bits of the Distance value.
  621.  
  622.     using the Length Shannon-Fano tree, read and decode
  623.       the Length value.
  624.     
  625.     Length <- Length + Minimum Match Length
  626.     
  627.     if Length = 63 + Minimum Match Length
  628.         read 8 bits from the input stream,
  629.         add this value to Length.
  630.  
  631.     move backwards Distance+1 bytes in the output stream, and
  632.     copy Length characters from this position to the output
  633.     stream.  (if this position is before the start of the output
  634.     stream, then assume that all the data before the start of
  635.     the output stream is filled with zeros).
  636. end loop
  637.  
  638. Decryption
  639. ----------
  640.  
  641. The encryption used in PKZIP was generously supplied by Roger
  642. Schlafly.  PKWARE is grateful to Mr. Schlafly for his expert
  643. help and advice in the field of data encryption.
  644.  
  645. PKZIP encrypts the compressed data stream.  Encrypted files must
  646. be decrypted before they can be extracted.
  647.  
  648. Each encrypted file has an extra 12 bytes stored at the start of
  649. the data area defining the encryption header for that file.  The
  650. encryption header is originally set to random values, and then
  651. itself encrypted, using 3, 32-bit keys.  The key values are 
  652. initialized using the supplied encryption password.  After each byte
  653. is encrypted, the keys are then updated using psuedo-random number
  654. generation techniques in combination with the same CRC-32 algorithm 
  655. used in PKZIP and described elsewhere in this document.
  656.  
  657. The following is the basic steps required to decrypt a file:
  658.  
  659. 1) Initialize the three 32-bit keys with the password.
  660. 2) Read and decrypt the 12-byte encryption header, further
  661.    initializing the encryption keys.
  662. 3) Read and decrypt the compressed data stream using the
  663.    encryption keys.
  664.  
  665.  
  666. Step 1 - Initializing the encryption keys
  667. -----------------------------------------
  668.  
  669. Key(0) <- 305419896
  670. Key(1) <- 591751049
  671. Key(2) <- 878082192
  672.  
  673. loop for i <- 0 to length(password)-1
  674.     update_keys(password(i))
  675. end loop
  676.  
  677.  
  678. Where update_keys() is defined as:
  679.  
  680.  
  681. update_keys(char):
  682.   Key(0) <- crc32(key(0),char)
  683.   Key(1) <- Key(1) + (Key(0) & 000000ffH)
  684.   Key(1) <- Key(1) * 134775813 + 1
  685.   Key(2) <- crc32(key(2),key(1) >> 24)
  686. end update_keys
  687.  
  688.  
  689. Where crc32(old_crc,char) is a routine that given a CRC value and a 
  690. character, returns an updated CRC value after applying the CRC-32 
  691. algorithm described elsewhere in this document.
  692.  
  693.  
  694. Step 2 - Decrypting the encryption header
  695. -----------------------------------------
  696.  
  697. The purpose of this step is to further initialize the encryption
  698. keys, based on random data, to render a plaintext attack on the
  699. data ineffective.
  700.  
  701.  
  702. Read the 12-byte encryption header into Buffer, in locations
  703. Buffer(0) thru Buffer(11).
  704.  
  705. loop for i <- 0 to 11
  706.     C <- buffer(i) ^ decrypt_byte()
  707.     update_keys(C)
  708.     buffer(i) <- C
  709. end loop
  710.  
  711.  
  712. Where decrypt_byte() is defined as:
  713.  
  714.  
  715. unsigned char decrypt_byte()
  716.     local unsigned short temp
  717.     temp <- Key(2) | 2
  718.     decrypt_byte <- (temp * (temp ^ 1)) >> 8
  719. end decrypt_byte
  720.  
  721.  
  722. After the header is decrypted, the last two bytes in Buffer
  723. should be the high-order word of the CRC for the file being
  724. decrypted, stored in Intel low-byte/high-byte order.  This can
  725. be used to test if the password supplied is correct or not.
  726.  
  727.  
  728. Step 3 - Decrypting the compressed data stream
  729. ----------------------------------------------
  730.  
  731. The compressed data stream can be decrypted as follows:
  732.  
  733.  
  734. loop until done
  735.     read a charcter into C
  736.     Temp <- C ^ decrypt_byte()
  737.     update_keys(temp)
  738.     output Temp
  739. end loop
  740.  
  741.  
  742.  
  743. References:
  744.  
  745.     Storer, James A. "Data Compression, Methods and Theory",
  746.        Computer Science Press, 1988
  747.     
  748.     Held, Gilbert  "Data Compression, Techniques and Applications,
  749.             Hardware and Software Considerations"
  750.        John Wiley & Sons, 1987
  751.  
  752.