/modules/libjar/appnote.txt

http://github.com/zpao/v8monkey · Plain Text · 1192 lines · 936 code · 256 blank · 0 comment · 0 complexity · a8908b2416f0147a52f20d514010b8ca MD5 · raw file

  1. Revised: 03/01/1999
  2. Disclaimer
  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. General Format of a ZIP file
  19. ----------------------------
  20. Files stored in arbitrary order. Large zipfiles can span multiple
  21. diskette media.
  22. Overall zipfile format:
  23. [local file header + file data + data_descriptor] . . .
  24. [central directory] end of central directory record
  25. A. Local file header:
  26. local file header signature 4 bytes (0x04034b50)
  27. version needed to extract 2 bytes
  28. general purpose bit flag 2 bytes
  29. compression method 2 bytes
  30. last mod file time 2 bytes
  31. last mod file date 2 bytes
  32. crc-32 4 bytes
  33. compressed size 4 bytes
  34. uncompressed size 4 bytes
  35. filename length 2 bytes
  36. extra field length 2 bytes
  37. filename (variable size)
  38. extra field (variable size)
  39. B. Data descriptor:
  40. crc-32 4 bytes
  41. compressed size 4 bytes
  42. uncompressed size 4 bytes
  43. This descriptor exists only if bit 3 of the general
  44. purpose bit flag is set (see below). It is byte aligned
  45. and immediately follows the last byte of compressed data.
  46. This descriptor is used only when it was not possible to
  47. seek in the output zip file, e.g., when the output zip file
  48. was standard output or a non seekable device.
  49. C. Central directory structure:
  50. [file header] . . . end of central dir record
  51. File header:
  52. central file header signature 4 bytes (0x02014b50)
  53. version made by 2 bytes
  54. version needed to extract 2 bytes
  55. general purpose bit flag 2 bytes
  56. compression method 2 bytes
  57. last mod file time 2 bytes
  58. last mod file date 2 bytes
  59. crc-32 4 bytes
  60. compressed size 4 bytes
  61. uncompressed size 4 bytes
  62. filename length 2 bytes
  63. extra field length 2 bytes
  64. file comment length 2 bytes
  65. disk number start 2 bytes
  66. internal file attributes 2 bytes
  67. external file attributes 4 bytes
  68. relative offset of local header 4 bytes
  69. filename (variable size)
  70. extra field (variable size)
  71. file comment (variable size)
  72. End of central dir record:
  73. end of central dir signature 4 bytes (0x06054b50)
  74. number of this disk 2 bytes
  75. number of the disk with the
  76. start of the central directory 2 bytes
  77. total number of entries in
  78. the central dir on this disk 2 bytes
  79. total number of entries in
  80. the central dir 2 bytes
  81. size of the central directory 4 bytes
  82. offset of start of central
  83. directory with respect to
  84. the starting disk number 4 bytes
  85. zipfile comment length 2 bytes
  86. zipfile comment (variable size)
  87. D. Explanation of fields:
  88. version made by (2 bytes)
  89. The upper byte indicates the compatibility of the file
  90. attribute information. If the external file attributes
  91. are compatible with MS-DOS and can be read by PKZIP for
  92. DOS version 2.04g then this value will be zero. If these
  93. attributes are not compatible, then this value will
  94. identify the host system on which the attributes are
  95. compatible. Software can use this information to determine
  96. the line record format for text files etc. The current
  97. mappings are:
  98. 0 - MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)
  99. 1 - Amiga 2 - VAX/VMS
  100. 3 - Unix 4 - VM/CMS
  101. 5 - Atari ST 6 - OS/2 H.P.F.S.
  102. 7 - Macintosh 8 - Z-System
  103. 9 - CP/M 10 - Windows NTFS
  104. 11 thru 255 - unused
  105. The lower byte indicates the version number of the
  106. software used to encode the file. The value/10
  107. indicates the major version number, and the value
  108. mod 10 is the minor version number.
  109. version needed to extract (2 bytes)
  110. The minimum software version needed to extract the
  111. file, mapped as above.
  112. general purpose bit flag: (2 bytes)
  113. Bit 0: If set, indicates that the file is encrypted.
  114. (For Method 6 - Imploding)
  115. Bit 1: If the compression method used was type 6,
  116. Imploding, then this bit, if set, indicates
  117. an 8K sliding dictionary was used. If clear,
  118. then a 4K sliding dictionary was used.
  119. Bit 2: If the compression method used was type 6,
  120. Imploding, then this bit, if set, indicates
  121. 3 Shannon-Fano trees were used to encode the
  122. sliding dictionary output. If clear, then 2
  123. Shannon-Fano trees were used.
  124. (For Method 8 - Deflating)
  125. Bit 2 Bit 1
  126. 0 0 Normal (-en) compression option was used.
  127. 0 1 Maximum (-ex) compression option was used.
  128. 1 0 Fast (-ef) compression option was used.
  129. 1 1 Super Fast (-es) compression option was used.
  130. Note: Bits 1 and 2 are undefined if the compression
  131. method is any other.
  132. Bit 3: If this bit is set, the fields crc-32, compressed
  133. size and uncompressed size are set to zero in the
  134. local header. The correct values are put in the
  135. data descriptor immediately following the compressed
  136. data. (Note: PKZIP version 2.04g for DOS only
  137. recognizes this bit for method 8 compression, newer
  138. versions of PKZIP recognize this bit for any
  139. compression method.)
  140. Bit 4: Reserved for use with method 8, for enhanced
  141. deflating.
  142. Bit 5: If this bit is set, this indicates that the file is
  143. compressed patched data. (Note: Requires PKZIP
  144. version 2.70 or greater)
  145. Bit 6: Currently unused.
  146. Bit 7: Currently unused.
  147. Bit 8: Currently unused.
  148. Bit 9: Currently unused.
  149. Bit 10: Currently unused.
  150. Bit 11: Currently unused.
  151. Bit 12: Reserved by PKWARE for enhanced compression.
  152. Bit 13: Reserved by PKWARE.
  153. Bit 14: Reserved by PKWARE.
  154. Bit 15: Reserved by PKWARE.
  155. compression method: (2 bytes)
  156. (see accompanying documentation for algorithm
  157. descriptions)
  158. 0 - The file is stored (no compression)
  159. 1 - The file is Shrunk
  160. 2 - The file is Reduced with compression factor 1
  161. 3 - The file is Reduced with compression factor 2
  162. 4 - The file is Reduced with compression factor 3
  163. 5 - The file is Reduced with compression factor 4
  164. 6 - The file is Imploded
  165. 7 - Reserved for Tokenizing compression algorithm
  166. 8 - The file is Deflated
  167. 9 - Reserved for enhanced Deflating
  168. 10 - PKWARE Date Compression Library Imploding
  169. date and time fields: (2 bytes each)
  170. The date and time are encoded in standard MS-DOS format.
  171. If input came from standard input, the date and time are
  172. those at which compression was started for this data.
  173. CRC-32: (4 bytes)
  174. The CRC-32 algorithm was generously contributed by
  175. David Schwaderer and can be found in his excellent
  176. book "C Programmers Guide to NetBIOS" published by
  177. Howard W. Sams & Co. Inc. The 'magic number' for
  178. the CRC is 0xdebb20e3. The proper CRC pre and post
  179. conditioning is used, meaning that the CRC register
  180. is pre-conditioned with all ones (a starting value
  181. of 0xffffffff) and the value is post-conditioned by
  182. taking the one's complement of the CRC residual.
  183. If bit 3 of the general purpose flag is set, this
  184. field is set to zero in the local header and the correct
  185. value is put in the data descriptor and in the central
  186. directory.
  187. compressed size: (4 bytes)
  188. uncompressed size: (4 bytes)
  189. The size of the file compressed and uncompressed,
  190. respectively. If bit 3 of the general purpose bit flag
  191. is set, these fields are set to zero in the local header
  192. and the correct values are put in the data descriptor and
  193. in the central directory.
  194. filename length: (2 bytes)
  195. extra field length: (2 bytes)
  196. file comment length: (2 bytes)
  197. The length of the filename, extra field, and comment
  198. fields respectively. The combined length of any
  199. directory record and these three fields should not
  200. generally exceed 65,535 bytes. If input came from standard
  201. input, the filename length is set to zero.
  202. disk number start: (2 bytes)
  203. The number of the disk on which this file begins.
  204. internal file attributes: (2 bytes)
  205. The lowest bit of this field indicates, if set, that
  206. the file is apparently an ASCII or text file. If not
  207. set, that the file apparently contains binary data.
  208. The remaining bits are unused in version 1.0.
  209. Bits 1 and 2 are reserved for use by PKWARE.
  210. external file attributes: (4 bytes)
  211. The mapping of the external attributes is
  212. host-system dependent (see 'version made by'). For
  213. MS-DOS, the low order byte is the MS-DOS directory
  214. attribute byte. If input came from standard input, this
  215. field is set to zero.
  216. relative offset of local header: (4 bytes)
  217. This is the offset from the start of the first disk on
  218. which this file appears, to where the local header should
  219. be found.
  220. filename: (Variable)
  221. The name of the file, with optional relative path.
  222. The path stored should not contain a drive or
  223. device letter, or a leading slash. All slashes
  224. should be forward slashes '/' as opposed to
  225. backwards slashes '\' for compatibility with Amiga
  226. and Unix file systems etc. If input came from standard
  227. input, there is no filename field.
  228. extra field: (Variable)
  229. This is for future expansion. If additional information
  230. needs to be stored in the future, it should be stored
  231. here. Earlier versions of the software can then safely
  232. skip this file, and find the next file or header. This
  233. field will be 0 length in version 1.0.
  234. In order to allow different programs and different types
  235. of information to be stored in the 'extra' field in .ZIP
  236. files, the following structure should be used for all
  237. programs storing data in this field:
  238. header1+data1 + header2+data2 . . .
  239. Each header should consist of:
  240. Header ID - 2 bytes
  241. Data Size - 2 bytes
  242. Note: all fields stored in Intel low-byte/high-byte order.
  243. The Header ID field indicates the type of data that is in
  244. the following data block.
  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. The current Header ID mappings defined by PKWARE are:
  249. 0x0007 AV Info
  250. 0x0009 OS/2
  251. 0x000a NTFS
  252. 0x000c VAX/VMS
  253. 0x000d Unix
  254. 0x000f Patch Descriptor
  255. Several third party mappings commonly used are:
  256. 0x4b46 FWKCS MD5 (see below)
  257. 0x07c8 Macintosh
  258. 0x4341 Acorn/SparkFS
  259. 0x4453 Windows NT security descriptor (binary ACL)
  260. 0x4704 VM/CMS
  261. 0x470f MVS
  262. 0x4c41 OS/2 access control list (text ACL)
  263. 0x4d49 Info-ZIP VMS (VAX or Alpha)
  264. 0x5455 extended timestamp
  265. 0x5855 Info-ZIP Unix (original, also OS/2, NT, etc)
  266. 0x6542 BeOS/BeBox
  267. 0x756e ASi Unix
  268. 0x7855 Info-ZIP Unix (new)
  269. 0xfd4a SMS/QDOS
  270. The Data Size field indicates the size of the following
  271. data block. Programs can use this value to skip to the
  272. next header block, passing over any data blocks that are
  273. not of interest.
  274. Note: As stated above, the size of the entire .ZIP file
  275. header, including the filename, comment, and extra
  276. field should not exceed 64K in size.
  277. In case two different programs should appropriate the same
  278. Header ID value, it is strongly recommended that each
  279. program place a unique signature of at least two bytes in
  280. size (and preferably 4 bytes or bigger) at the start of
  281. each data area. Every program should verify that its
  282. unique signature is present, in addition to the Header ID
  283. value being correct, before assuming that it is a block of
  284. known type.
  285. -OS/2 Extra Field:
  286. The following is the layout of the OS/2 attributes "extra"
  287. block. (Last Revision 09/05/95)
  288. Note: all fields stored in Intel low-byte/high-byte order.
  289. Value Size Description
  290. ----- ---- -----------
  291. (OS/2) 0x0009 2 bytes Tag for this "extra" block type
  292. TSize 2 bytes Size for the following data block
  293. BSize 4 bytes Uncompressed Block Size
  294. CType 2 bytes Compression type
  295. EACRC 4 bytes CRC value for uncompress block
  296. (var) variable Compressed block
  297. The OS/2 extended attribute structure (FEA2LIST) is
  298. compressed and then stored in it's entirety within this
  299. structure. There will only ever be one "block" of data in
  300. VarFields[].
  301. -UNIX Extra Field:
  302. The following is the layout of the Unix "extra" block.
  303. Note: all fields are stored in Intel low-byte/high-byte
  304. order.
  305. Value Size Description
  306. ----- ---- -----------
  307. (UNIX) 0x000d 2 bytes Tag for this "extra" block type
  308. TSize 2 bytes Size for the following data block
  309. Atime 4 bytes File last access time
  310. Mtime 4 bytes File last modification time
  311. Uid 2 bytes File user ID
  312. Gid 2 bytes File group ID
  313. (var) variable Variable length data field
  314. The variable length data field will contain file type
  315. specific data. Currently the only values allowed are
  316. the original "linked to" file names for hard or symbolic
  317. links.
  318. -VAX/VMS Extra Field:
  319. The following is the layout of the VAX/VMS attributes
  320. "extra" block.
  321. Note: all fields stored in Intel low-byte/high-byte order.
  322. Value Size Description
  323. ----- ---- -----------
  324. (VMS) 0x000c 2 bytes Tag for this "extra" block type
  325. TSize 2 bytes Size of the total "extra" block
  326. CRC 4 bytes 32-bit CRC for remainder of the block
  327. Tag1 2 bytes VMS attribute tag value #1
  328. Size1 2 bytes Size of attribute #1, in bytes
  329. (var.) Size1 Attribute #1 data
  330. .
  331. .
  332. .
  333. TagN 2 bytes VMS attribute tage value #N
  334. SizeN 2 bytes Size of attribute #N, in bytes
  335. (var.) SizeN Attribute #N data
  336. Rules:
  337. 1. There will be one or more of attributes present, which
  338. will each be preceded by the above TagX & SizeX values.
  339. These values are identical to the ATR$C_XXXX and
  340. ATR$S_XXXX constants which are defined in ATR.H under
  341. VMS C. Neither of these values will ever be zero.
  342. 2. No word alignment or padding is performed.
  343. 3. A well-behaved PKZIP/VMS program should never produce
  344. more than one sub-block with the same TagX value. Also,
  345. there will never be more than one "extra" block of type
  346. 0x000c in a particular directory record.
  347. -NTFS Extra Field:
  348. The following is the layout of the NTFS attributes
  349. "extra" block.
  350. Note: all fields stored in Intel low-byte/high-byte order.
  351. Value Size Description
  352. ----- ---- -----------
  353. (NTFS) 0x000a 2 bytes Tag for this "extra" block type
  354. TSize 2 bytes Size of the total "extra" block
  355. Reserved 4 bytes Reserved for future use
  356. Tag1 2 bytes NTFS attribute tag value #1
  357. Size1 2 bytes Size of attribute #1, in bytes
  358. (var.) Size1 Attribute #1 data
  359. .
  360. .
  361. .
  362. TagN 2 bytes NTFS attribute tage value #N
  363. SizeN 2 bytes Size of attribute #N, in bytes
  364. (var.) SizeN Attribute #N data
  365. For NTFS, values for Tag1 through TagN are as follows:
  366. (currently only one set of attributes is defined for NTFS)
  367. Tag Size Description
  368. ----- ---- -----------
  369. 0x0001 2 bytes Tag for attribute #1
  370. Size1 2 bytes Size of attribute #1, in bytes
  371. Mtime 8 bytes File last modification time
  372. Atime 8 bytes File last access time
  373. Ctime 8 bytes File creation time
  374. -PATCH Descriptor Extra Field:
  375. The following is the layout of the Patch Descriptor "extra"
  376. block.
  377. Note: all fields stored in Intel low-byte/high-byte order.
  378. Value Size Description
  379. ----- ---- -----------
  380. (Patch) 0x000f 2 bytes Tag for this "extra" block type
  381. TSize 2 bytes Size of the total "extra" block
  382. Version 2 bytes Version of the descriptor
  383. Flags 4 bytes Actions and reactions (see below)
  384. OldSize 4 bytes Size of the file about to be patched
  385. OldCRC 4 bytes 32-bit CRC of the file to be patched
  386. NewSize 4 bytes Size of the resulting file
  387. NewCRC 4 bytes 32-bit CRC of the resulting file
  388. Actions and reactions
  389. Bits Description
  390. ---- ----------------
  391. 0 Use for autodetection
  392. 1 Treat as selfpatch
  393. 2-3 RESERVED
  394. 4-5 Action (see below)
  395. 6-7 RESERVED
  396. 8-9 Reaction (see below) to absent file
  397. 10-11 Reaction (see below) to newer file
  398. 12-13 Reaction (see below) to unknown file
  399. 14-15 RESERVED
  400. 16-31 RESERVED
  401. Actions
  402. Action Value
  403. ------ -----
  404. none 0
  405. add 1
  406. delete 2
  407. patch 3
  408. Reactions
  409. Reaction Value
  410. -------- -----
  411. ask 0
  412. skip 1
  413. ignore 2
  414. fail 3
  415. - FWKCS MD5 Extra Field:
  416. The FWKCS Contents_Signature System, used in
  417. automatically identifying files independent of filename,
  418. optionally adds and uses an extra field to support the
  419. rapid creation of an enhanced contents_signature:
  420. Header ID = 0x4b46
  421. Data Size = 0x0013
  422. Preface = 'M','D','5'
  423. followed by 16 bytes containing the uncompressed file's
  424. 128_bit MD5 hash(1), low byte first.
  425. When FWKCS revises a zipfile central directory to add
  426. this extra field for a file, it also replaces the
  427. central directory entry for that file's uncompressed
  428. filelength with a measured value.
  429. FWKCS provides an option to strip this extra field, if
  430. present, from a zipfile central directory. In adding
  431. this extra field, FWKCS preserves Zipfile Authenticity
  432. Verification; if stripping this extra field, FWKCS
  433. preserves all versions of AV through PKZIP version 2.04g.
  434. FWKCS, and FWKCS Contents_Signature System, are
  435. trademarks of Frederick W. Kantor.
  436. (1) R. Rivest, RFC1321.TXT, MIT Laboratory for Computer
  437. Science and RSA Data Security, Inc., April 1992.
  438. ll.76-77: "The MD5 algorithm is being placed in the
  439. public domain for review and possible adoption as a
  440. standard."
  441. file comment: (Variable)
  442. The comment for this file.
  443. number of this disk: (2 bytes)
  444. The number of this disk, which contains central
  445. directory end record.
  446. number of the disk with the start of the central
  447. directory: (2 bytes)
  448. The number of the disk on which the central
  449. directory starts.
  450. total number of entries in the central dir on
  451. this disk: (2 bytes)
  452. The number of central directory entries on this disk.
  453. total number of entries in the central dir: (2 bytes)
  454. The total number of files in the zipfile.
  455. size of the central directory: (4 bytes)
  456. The size (in bytes) of the entire central directory.
  457. offset of start of central directory with respect to
  458. the starting disk number: (4 bytes)
  459. Offset of the start of the central directory on the
  460. disk on which the central directory starts.
  461. zipfile comment length: (2 bytes)
  462. The length of the comment for this zipfile.
  463. zipfile comment: (Variable)
  464. The comment for this zipfile.
  465. D. General notes:
  466. 1) All fields unless otherwise noted are unsigned and stored
  467. in Intel low-byte:high-byte, low-word:high-word order.
  468. 2) String fields are not null terminated, since the
  469. length is given explicitly.
  470. 3) Local headers should not span disk boundaries. Also, even
  471. though the central directory can span disk boundaries, no
  472. single record in the central directory should be split
  473. across disks.
  474. 4) The entries in the central directory may not necessarily
  475. be in the same order that files appear in the zipfile.
  476. UnShrinking - Method 1
  477. ----------------------
  478. Shrinking is a Dynamic Ziv-Lempel-Welch compression algorithm
  479. with partial clearing. The initial code size is 9 bits, and
  480. the maximum code size is 13 bits. Shrinking differs from
  481. conventional Dynamic Ziv-Lempel-Welch implementations in several
  482. respects:
  483. 1) The code size is controlled by the compressor, and is not
  484. automatically increased when codes larger than the current
  485. code size are created (but not necessarily used). When
  486. the decompressor encounters the code sequence 256
  487. (decimal) followed by 1, it should increase the code size
  488. read from the input stream to the next bit size. No
  489. blocking of the codes is performed, so the next code at
  490. the increased size should be read from the input stream
  491. immediately after where the previous code at the smaller
  492. bit size was read. Again, the decompressor should not
  493. increase the code size used until the sequence 256,1 is
  494. encountered.
  495. 2) When the table becomes full, total clearing is not
  496. performed. Rather, when the compressor emits the code
  497. sequence 256,2 (decimal), the decompressor should clear
  498. all leaf nodes from the Ziv-Lempel tree, and continue to
  499. use the current code size. The nodes that are cleared
  500. from the Ziv-Lempel tree are then re-used, with the lowest
  501. code value re-used first, and the highest code value
  502. re-used last. The compressor can emit the sequence 256,2
  503. at any time.
  504. Expanding - Methods 2-5
  505. -----------------------
  506. The Reducing algorithm is actually a combination of two
  507. distinct algorithms. The first algorithm compresses repeated
  508. byte sequences, and the second algorithm takes the compressed
  509. stream from the first algorithm and applies a probabilistic
  510. compression method.
  511. The probabilistic compression stores an array of 'follower
  512. sets' S(j), for j=0 to 255, corresponding to each possible
  513. ASCII character. Each set contains between 0 and 32
  514. characters, to be denoted as S(j)[0],...,S(j)[m], where m<32.
  515. The sets are stored at the beginning of the data area for a
  516. Reduced file, in reverse order, with S(255) first, and S(0)
  517. last.
  518. The sets are encoded as { N(j), S(j)[0],...,S(j)[N(j)-1] },
  519. where N(j) is the size of set S(j). N(j) can be 0, in which
  520. case the follower set for S(j) is empty. Each N(j) value is
  521. encoded in 6 bits, followed by N(j) eight bit character values
  522. corresponding to S(j)[0] to S(j)[N(j)-1] respectively. If
  523. N(j) is 0, then no values for S(j) are stored, and the value
  524. for N(j-1) immediately follows.
  525. Immediately after the follower sets, is the compressed data
  526. stream. The compressed data stream can be interpreted for the
  527. probabilistic decompression as follows:
  528. let Last-Character <- 0.
  529. loop until done
  530. if the follower set S(Last-Character) is empty then
  531. read 8 bits from the input stream, and copy this
  532. value to the output stream.
  533. otherwise if the follower set S(Last-Character) is non-empty then
  534. read 1 bit from the input stream.
  535. if this bit is not zero then
  536. read 8 bits from the input stream, and copy this
  537. value to the output stream.
  538. otherwise if this bit is zero then
  539. read B(N(Last-Character)) bits from the input
  540. stream, and assign this value to I.
  541. Copy the value of S(Last-Character)[I] to the
  542. output stream.
  543. assign the last value placed on the output stream to
  544. Last-Character.
  545. end loop
  546. B(N(j)) is defined as the minimal number of bits required to
  547. encode the value N(j)-1.
  548. The decompressed stream from above can then be expanded to
  549. re-create the original file as follows:
  550. let State <- 0.
  551. loop until done
  552. read 8 bits from the input stream into C.
  553. case State of
  554. 0: if C is not equal to DLE (144 decimal) then
  555. copy C to the output stream.
  556. otherwise if C is equal to DLE then
  557. let State <- 1.
  558. 1: if C is non-zero then
  559. let V <- C.
  560. let Len <- L(V)
  561. let State <- F(Len).
  562. otherwise if C is zero then
  563. copy the value 144 (decimal) to the output stream.
  564. let State <- 0
  565. 2: let Len <- Len + C
  566. let State <- 3.
  567. 3: move backwards D(V,C) bytes in the output stream
  568. (if this position is before the start of the output
  569. stream, then assume that all the data before the
  570. start of the output stream is filled with zeros).
  571. copy Len+3 bytes from this position to the output stream.
  572. let State <- 0.
  573. end case
  574. end loop
  575. The functions F,L, and D are dependent on the 'compression
  576. factor', 1 through 4, and are defined as follows:
  577. For compression factor 1:
  578. L(X) equals the lower 7 bits of X.
  579. F(X) equals 2 if X equals 127 otherwise F(X) equals 3.
  580. D(X,Y) equals the (upper 1 bit of X) * 256 + Y + 1.
  581. For compression factor 2:
  582. L(X) equals the lower 6 bits of X.
  583. F(X) equals 2 if X equals 63 otherwise F(X) equals 3.
  584. D(X,Y) equals the (upper 2 bits of X) * 256 + Y + 1.
  585. For compression factor 3:
  586. L(X) equals the lower 5 bits of X.
  587. F(X) equals 2 if X equals 31 otherwise F(X) equals 3.
  588. D(X,Y) equals the (upper 3 bits of X) * 256 + Y + 1.
  589. For compression factor 4:
  590. L(X) equals the lower 4 bits of X.
  591. F(X) equals 2 if X equals 15 otherwise F(X) equals 3.
  592. D(X,Y) equals the (upper 4 bits of X) * 256 + Y + 1.
  593. Imploding - Method 6
  594. --------------------
  595. The Imploding algorithm is actually a combination of two distinct
  596. algorithms. The first algorithm compresses repeated byte
  597. sequences using a sliding dictionary. The second algorithm is
  598. used to compress the encoding of the sliding dictionary output,
  599. using multiple Shannon-Fano trees.
  600. The Imploding algorithm can use a 4K or 8K sliding dictionary
  601. size. The dictionary size used can be determined by bit 1 in the
  602. general purpose flag word; a 0 bit indicates a 4K dictionary
  603. while a 1 bit indicates an 8K dictionary.
  604. The Shannon-Fano trees are stored at the start of the compressed
  605. file. The number of trees stored is defined by bit 2 in the
  606. general purpose flag word; a 0 bit indicates two trees stored, a
  607. 1 bit indicates three trees are stored. If 3 trees are stored,
  608. the first Shannon-Fano tree represents the encoding of the
  609. Literal characters, the second tree represents the encoding of
  610. the Length information, the third represents the encoding of the
  611. Distance information. When 2 Shannon-Fano trees are stored, the
  612. Length tree is stored first, followed by the Distance tree.
  613. The Literal Shannon-Fano tree, if present is used to represent
  614. the entire ASCII character set, and contains 256 values. This
  615. tree is used to compress any data not compressed by the sliding
  616. dictionary algorithm. When this tree is present, the Minimum
  617. Match Length for the sliding dictionary is 3. If this tree is
  618. not present, the Minimum Match Length is 2.
  619. The Length Shannon-Fano tree is used to compress the Length part
  620. of the (length,distance) pairs from the sliding dictionary
  621. output. The Length tree contains 64 values, ranging from the
  622. Minimum Match Length, to 63 plus the Minimum Match Length.
  623. The Distance Shannon-Fano tree is used to compress the Distance
  624. part of the (length,distance) pairs from the sliding dictionary
  625. output. The Distance tree contains 64 values, ranging from 0 to
  626. 63, representing the upper 6 bits of the distance value. The
  627. distance values themselves will be between 0 and the sliding
  628. dictionary size, either 4K or 8K.
  629. The Shannon-Fano trees themselves are stored in a compressed
  630. format. The first byte of the tree data represents the number of
  631. bytes of data representing the (compressed) Shannon-Fano tree
  632. minus 1. The remaining bytes represent the Shannon-Fano tree
  633. data encoded as:
  634. High 4 bits: Number of values at this bit length + 1. (1 - 16)
  635. Low 4 bits: Bit Length needed to represent value + 1. (1 - 16)
  636. The Shannon-Fano codes can be constructed from the bit lengths
  637. using the following algorithm:
  638. 1) Sort the Bit Lengths in ascending order, while retaining the
  639. order of the original lengths stored in the file.
  640. 2) Generate the Shannon-Fano trees:
  641. Code <- 0
  642. CodeIncrement <- 0
  643. LastBitLength <- 0
  644. i <- number of Shannon-Fano codes - 1 (either 255 or 63)
  645. loop while i >= 0
  646. Code = Code + CodeIncrement
  647. if BitLength(i) <> LastBitLength then
  648. LastBitLength=BitLength(i)
  649. CodeIncrement = 1 shifted left (16 - LastBitLength)
  650. ShannonCode(i) = Code
  651. i <- i - 1
  652. end loop
  653. 3) Reverse the order of all the bits in the above ShannonCode()
  654. vector, so that the most significant bit becomes the least
  655. significant bit. For example, the value 0x1234 (hex) would
  656. become 0x2C48 (hex).
  657. 4) Restore the order of Shannon-Fano codes as originally stored
  658. within the file.
  659. Example:
  660. This example will show the encoding of a Shannon-Fano tree
  661. of size 8. Notice that the actual Shannon-Fano trees used
  662. for Imploding are either 64 or 256 entries in size.
  663. Example: 0x02, 0x42, 0x01, 0x13
  664. The first byte indicates 3 values in this table. Decoding the
  665. bytes:
  666. 0x42 = 5 codes of 3 bits long
  667. 0x01 = 1 code of 2 bits long
  668. 0x13 = 2 codes of 4 bits long
  669. This would generate the original bit length array of:
  670. (3, 3, 3, 3, 3, 2, 4, 4)
  671. There are 8 codes in this table for the values 0 thru 7. Using
  672. the algorithm to obtain the Shannon-Fano codes produces:
  673. Reversed Order Original
  674. Val Sorted Constructed Code Value Restored Length
  675. --- ------ ----------------- -------- -------- ------
  676. 0: 2 1100000000000000 11 101 3
  677. 1: 3 1010000000000000 101 001 3
  678. 2: 3 1000000000000000 001 110 3
  679. 3: 3 0110000000000000 110 010 3
  680. 4: 3 0100000000000000 010 100 3
  681. 5: 3 0010000000000000 100 11 2
  682. 6: 4 0001000000000000 1000 1000 4
  683. 7: 4 0000000000000000 0000 0000 4
  684. The values in the Val, Order Restored and Original Length columns
  685. now represent the Shannon-Fano encoding tree that can be used for
  686. decoding the Shannon-Fano encoded data. How to parse the
  687. variable length Shannon-Fano values from the data stream is beyond
  688. the scope of this document. (See the references listed at the end of
  689. this document for more information.) However, traditional decoding
  690. schemes used for Huffman variable length decoding, such as the
  691. Greenlaw algorithm, can be successfully applied.
  692. The compressed data stream begins immediately after the
  693. compressed Shannon-Fano data. The compressed data stream can be
  694. interpreted as follows:
  695. loop until done
  696. read 1 bit from input stream.
  697. if this bit is non-zero then (encoded data is literal data)
  698. if Literal Shannon-Fano tree is present
  699. read and decode character using Literal Shannon-Fano tree.
  700. otherwise
  701. read 8 bits from input stream.
  702. copy character to the output stream.
  703. otherwise (encoded data is sliding dictionary match)
  704. if 8K dictionary size
  705. read 7 bits for offset Distance (lower 7 bits of offset).
  706. otherwise
  707. read 6 bits for offset Distance (lower 6 bits of offset).
  708. using the Distance Shannon-Fano tree, read and decode the
  709. upper 6 bits of the Distance value.
  710. using the Length Shannon-Fano tree, read and decode
  711. the Length value.
  712. Length <- Length + Minimum Match Length
  713. if Length = 63 + Minimum Match Length
  714. read 8 bits from the input stream,
  715. add this value to Length.
  716. move backwards Distance+1 bytes in the output stream, and
  717. copy Length characters from this position to the output
  718. stream. (if this position is before the start of the output
  719. stream, then assume that all the data before the start of
  720. the output stream is filled with zeros).
  721. end loop
  722. Tokenizing - Method 7
  723. --------------------
  724. This method is not used by PKZIP.
  725. Deflating - Method 8
  726. -----------------
  727. The Deflate algorithm is similar to the Implode algorithm using
  728. a sliding dictionary of up to 32K with secondary compression
  729. from Huffman/Shannon-Fano codes.
  730. The compressed data is stored in blocks with a header describing
  731. the block and the Huffman codes used in the data block. The header
  732. format is as follows:
  733. Bit 0: Last Block bit This bit is set to 1 if this is the last
  734. compressed block in the data.
  735. Bits 1-2: Block type
  736. 00 (0) - Block is stored - All stored data is byte aligned.
  737. Skip bits until next byte, then next word = block
  738. length, followed by the ones compliment of the block
  739. length word. Remaining data in block is the stored
  740. data.
  741. 01 (1) - Use fixed Huffman codes for literal and distance codes.
  742. Lit Code Bits Dist Code Bits
  743. --------- ---- --------- ----
  744. 0 - 143 8 0 - 31 5
  745. 144 - 255 9
  746. 256 - 279 7
  747. 280 - 287 8
  748. Literal codes 286-287 and distance codes 30-31 are
  749. never used but participate in the huffman construction.
  750. 10 (2) - Dynamic Huffman codes. (See expanding Huffman codes)
  751. 11 (3) - Reserved - Flag a "Error in compressed data" if seen.
  752. Expanding Huffman Codes
  753. -----------------------
  754. If the data block is stored with dynamic Huffman codes, the Huffman
  755. codes are sent in the following compressed format:
  756. 5 Bits: # of Literal codes sent - 256 (256 - 286)
  757. All other codes are never sent.
  758. 5 Bits: # of Dist codes - 1 (1 - 32)
  759. 4 Bits: # of Bit Length codes - 3 (3 - 19)
  760. The Huffman codes are sent as bit lengths and the codes are built as
  761. described in the implode algorithm. The bit lengths themselves are
  762. compressed with Huffman codes. There are 19 bit length codes:
  763. 0 - 15: Represent bit lengths of 0 - 15
  764. 16: Copy the previous bit length 3 - 6 times.
  765. The next 2 bits indicate repeat length (0 = 3, ... ,3 = 6)
  766. Example: Codes 8, 16 (+2 bits 11), 16 (+2 bits 10) will
  767. expand to 12 bit lengths of 8 (1 + 6 + 5)
  768. 17: Repeat a bit length of 0 for 3 - 10 times. (3 bits of length)
  769. 18: Repeat a bit length of 0 for 11 - 138 times (7 bits of length)
  770. The lengths of the bit length codes are sent packed 3 bits per value
  771. (0 - 7) in the following order:
  772. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  773. The Huffman codes should be built as described in the Implode algorithm
  774. except codes are assigned starting at the shortest bit length, i.e. the
  775. shortest code should be all 0's rather than all 1's. Also, codes with
  776. a bit length of zero do not participate in the tree construction. The
  777. codes are then used to decode the bit lengths for the literal and
  778. distance tables.
  779. The bit lengths for the literal tables are sent first with the number
  780. of entries sent described by the 5 bits sent earlier. There are up
  781. to 286 literal characters; the first 256 represent the respective 8
  782. bit character, code 256 represents the End-Of-Block code, the remaining
  783. 29 codes represent copy lengths of 3 thru 258. There are up to 30
  784. distance codes representing distances from 1 thru 32k as described
  785. below.
  786. Length Codes
  787. ------------
  788. Extra Extra Extra Extra
  789. Code Bits Length Code Bits Lengths Code Bits Lengths Code Bits Length(s)
  790. ---- ---- ------ ---- ---- ------- ---- ---- ------- ---- ---- ---------
  791. 257 0 3 265 1 11,12 273 3 35-42 281 5 131-162
  792. 258 0 4 266 1 13,14 274 3 43-50 282 5 163-194
  793. 259 0 5 267 1 15,16 275 3 51-58 283 5 195-226
  794. 260 0 6 268 1 17,18 276 3 59-66 284 5 227-257
  795. 261 0 7 269 2 19-22 277 4 67-82 285 0 258
  796. 262 0 8 270 2 23-26 278 4 83-98
  797. 263 0 9 271 2 27-30 279 4 99-114
  798. 264 0 10 272 2 31-34 280 4 115-130
  799. Distance Codes
  800. --------------
  801. Extra Extra Extra Extra
  802. Code Bits Dist Code Bits Dist Code Bits Distance Code Bits Distance
  803. ---- ---- ---- ---- ---- ------ ---- ---- -------- ---- ---- --------
  804. 0 0 1 8 3 17-24 16 7 257-384 24 11 4097-6144
  805. 1 0 2 9 3 25-32 17 7 385-512 25 11 6145-8192
  806. 2 0 3 10 4 33-48 18 8 513-768 26 12 8193-12288
  807. 3 0 4 11 4 49-64 19 8 769-1024 27 12 12289-16384
  808. 4 1 5,6 12 5 65-96 20 9 1025-1536 28 13 16385-24576
  809. 5 1 7,8 13 5 97-128 21 9 1537-2048 29 13 24577-32768
  810. 6 2 9-12 14 6 129-192 22 10 2049-3072
  811. 7 2 13-16 15 6 193-256 23 10 3073-4096
  812. The compressed data stream begins immediately after the
  813. compressed header data. The compressed data stream can be
  814. interpreted as follows:
  815. do
  816. read header from input stream.
  817. if stored block
  818. skip bits until byte aligned
  819. read count and 1's compliment of count
  820. copy count bytes data block
  821. otherwise
  822. loop until end of block code sent
  823. decode literal character from input stream
  824. if literal < 256
  825. copy character to the output stream
  826. otherwise
  827. if literal = end of block
  828. break from loop
  829. otherwise
  830. decode distance from input stream
  831. move backwards distance bytes in the output stream, and
  832. copy length characters from this position to the output
  833. stream.
  834. end loop
  835. while not last block
  836. if data descriptor exists
  837. skip bits until byte aligned
  838. read crc and sizes
  839. endif
  840. Decryption
  841. ----------
  842. The encryption used in PKZIP was generously supplied by Roger
  843. Schlafly. PKWARE is grateful to Mr. Schlafly for his expert
  844. help and advice in the field of data encryption.
  845. PKZIP encrypts the compressed data stream. Encrypted files must
  846. be decrypted before they can be extracted.
  847. Each encrypted file has an extra 12 bytes stored at the start of
  848. the data area defining the encryption header for that file. The
  849. encryption header is originally set to random values, and then
  850. itself encrypted, using three, 32-bit keys. The key values are
  851. initialized using the supplied encryption password. After each byte
  852. is encrypted, the keys are then updated using pseudo-random number
  853. generation techniques in combination with the same CRC-32 algorithm
  854. used in PKZIP and described elsewhere in this document.
  855. The following is the basic steps required to decrypt a file:
  856. 1) Initialize the three 32-bit keys with the password.
  857. 2) Read and decrypt the 12-byte encryption header, further
  858. initializing the encryption keys.
  859. 3) Read and decrypt the compressed data stream using the
  860. encryption keys.
  861. Step 1 - Initializing the encryption keys
  862. -----------------------------------------
  863. Key(0) <- 305419896
  864. Key(1) <- 591751049
  865. Key(2) <- 878082192
  866. loop for i <- 0 to length(password)-1
  867. update_keys(password(i))
  868. end loop
  869. Where update_keys() is defined as:
  870. update_keys(char):
  871. Key(0) <- crc32(key(0),char)
  872. Key(1) <- Key(1) + (Key(0) & 000000ffH)
  873. Key(1) <- Key(1) * 134775813 + 1
  874. Key(2) <- crc32(key(2),key(1) >> 24)
  875. end update_keys
  876. Where crc32(old_crc,char) is a routine that given a CRC value and a
  877. character, returns an updated CRC value after applying the CRC-32
  878. algorithm described elsewhere in this document.
  879. Step 2 - Decrypting the encryption header
  880. -----------------------------------------
  881. The purpose of this step is to further initialize the encryption
  882. keys, based on random data, to render a plaintext attack on the
  883. data ineffective.
  884. Read the 12-byte encryption header into Buffer, in locations
  885. Buffer(0) thru Buffer(11).
  886. loop for i <- 0 to 11
  887. C <- buffer(i) ^ decrypt_byte()
  888. update_keys(C)
  889. buffer(i) <- C
  890. end loop
  891. Where decrypt_byte() is defined as:
  892. unsigned char decrypt_byte()
  893. local unsigned short temp
  894. temp <- Key(2) | 2
  895. decrypt_byte <- (temp * (temp ^ 1)) >> 8
  896. end decrypt_byte
  897. After the header is decrypted, the last 1 or 2 bytes in Buffer
  898. should be the high-order word/byte of the CRC for the file being
  899. decrypted, stored in Intel low-byte/high-byte order. Versions of
  900. PKZIP prior to 2.0 used a 2 byte CRC check; a 1 byte CRC check is
  901. used on versions after 2.0. This can be used to test if the password
  902. supplied is correct or not.
  903. Step 3 - Decrypting the compressed data stream
  904. ----------------------------------------------
  905. The compressed data stream can be decrypted as follows:
  906. loop until done
  907. read a character into C
  908. Temp <- C ^ decrypt_byte()
  909. update_keys(temp)
  910. output Temp
  911. end loop
  912. In addition to the above mentioned contributors to PKZIP and PKUNZIP,
  913. I would like to extend special thanks to Robert Mahoney for suggesting
  914. the extension .ZIP for this software.
  915. References:
  916. Fiala, Edward R., and Greene, Daniel H., "Data compression with
  917. finite windows", Communications of the ACM, Volume 32, Number 4,
  918. April 1989, pages 490-505.
  919. Held, Gilbert, "Data Compression, Techniques and Applications,
  920. Hardware and Software Considerations", John Wiley & Sons, 1987.
  921. Huffman, D.A., "A method for the construction of minimum-redundancy
  922. codes", Proceedings of the IRE, Volume 40, Number 9, September 1952,
  923. pages 1098-1101.
  924. Nelson, Mark, "LZW Data Compression", Dr. Dobbs Journal, Volume 14,
  925. Number 10, October 1989, pages 29-37.
  926. Nelson, Mark, "The Data Compression Book", M&T Books, 1991.
  927. Storer, James A., "Data Compression, Methods and Theory",
  928. Computer Science Press, 1988
  929. Welch, Terry, "A Technique for High-Performance Data Compression",
  930. IEEE Computer, Volume 17, Number 6, June 1984, pages 8-19.
  931. Ziv, J. and Lempel, A., "A universal algorithm for sequential data
  932. compression", Communications of the ACM, Volume 30, Number 6,
  933. June 1987, pages 520-540.
  934. Ziv, J. and Lempel, A., "Compression of individual sequences via
  935. variable-rate coding", IEEE Transactions on Information Theory,
  936. Volume 24, Number 5, September 1978, pages 530-536.