home *** CD-ROM | disk | FTP | other *** search
/ BBS 1 / BBS#1.iso / for-dos / crack.arj / APPNOTE.TXT next >
Encoding:
Text File  |  1992-02-22  |  27.2 KB  |  738 lines

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