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