LZO.FAQ 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ============================================================================
  2. LZO Frequently Asked Questions
  3. ============================================================================
  4. I hate reading docs - just tell me how to add compression to my program
  5. =======================================================================
  6. This is for the impatient: take a look at examples/simple.c and
  7. examples/lzopack.c and see how easy this is.
  8. But you will come back to read the documentation later, won't you ?
  9. Can you explain the naming conventions of the algorithms ?
  10. ==========================================================
  11. Let's take a look at LZO1X:
  12. The algorithm name is LZO1X.
  13. The algorithm category is LZO1.
  14. Various compression levels are implemented.
  15. LZO1X-999
  16. !---------- algorithm category
  17. !--------- algorithm type
  18. !!!----- compression level (1-9, 99, 999)
  19. LZO1X-1(11)
  20. !---------- algorithm category
  21. !--------- algorithm type
  22. !------- compression level (1-9, 99, 999)
  23. !!---- memory level (memory requirements for compression)
  24. All compression/memory levels generate the same compressed data format,
  25. so e.g. the LZO1X decompressor handles all LZO1X-* compression levels
  26. (for more information about the decompressors see below).
  27. Category LZO1 algorithms: compressed data format is strictly byte aligned
  28. Category LZO2 algorithms: uses bit-shifting, slower decompression
  29. Why are there so many algorithms ?
  30. ==================================
  31. Because of historical reasons - I want to support unlimited
  32. backward compatibility.
  33. Don't get misled by the size of the library - using one algorithm
  34. increases the size of your application by only a few KiB.
  35. If you just want to add a little bit of data compression to your
  36. application you may be looking for miniLZO.
  37. See minilzo/README.LZO for more information.
  38. Which algorithm should I use ?
  39. ==============================
  40. LZO1X seems to be best choice in many cases, so:
  41. - when going for speed use LZO1X-1
  42. - when generating pre-compressed data use LZO1X-999
  43. - if you have little memory available for compression use LZO1X-1(11)
  44. or LZO1X-1(12)
  45. Of course, your mileage may vary, and you are encouraged to run your
  46. own experiments. Try LZO1Y and LZO1F next.
  47. What's the difference between the decompressors per algorithm ?
  48. ===============================================================
  49. Once again let's use LZO1X for explanation:
  50. - lzo1x_decompress
  51. The 'standard' decompressor. Pretty fast - use this whenever possible.
  52. This decompressor expects valid compressed data.
  53. If the compressed data gets corrupted somehow (e.g. transmission
  54. via an erroneous channel, disk errors, ...) it will probably crash
  55. your application because absolutely no additional checks are done.
  56. - lzo1x_decompress_safe
  57. The 'safe' decompressor. Somewhat slower.
  58. This decompressor will catch all compressed data violations and
  59. return an error code in this case - it will never crash.
  60. - lzo1x_decompress_asm
  61. Same as lzo1x_decompress - written in assembler.
  62. - lzo1x_decompress_asm_safe
  63. Same as lzo1x_decompress_safe - written in assembler.
  64. - lzo1x_decompress_asm_fast
  65. Similar to lzo1x_decompress_asm - but even faster.
  66. For reasons of speed this decompressor can write up to 3 bytes
  67. past the end of the decompressed (output) block.
  68. [ technical note: because data is transferred in 32-bit units ]
  69. Use this when you are decompressing from one memory block to
  70. another memory block - just provide output space for 3 extra bytes.
  71. You shouldn't use it if e.g. you are directly decompressing to video
  72. memory (because the extra bytes will be show up on the screen).
  73. - lzo1x_decompress_asm_fast_safe
  74. This is the safe version of lzo1x_decompress_asm_fast.
  75. Notes:
  76. ------
  77. - When using a safe decompressor you must pass the number of
  78. bytes available in 'dst' via the parameter 'dst_len'.
  79. - If you want to be sure that your data is not corrupted you must
  80. use a checksum - just using the safe decompressor is not enough,
  81. because many data errors will not result in a compressed data violation.
  82. - Assembler versions are only available for the i386 family yet.
  83. Please see also asm/i386/00README.TXT
  84. - You should test if the assembler versions are actually faster
  85. than the C version on your machine - some compilers can do a very
  86. good optimization job and they also can optimize the code
  87. for a specific processor.
  88. What is this optimization thing ?
  89. =================================
  90. The compressors use a heuristic approach - they sometimes code
  91. information that doesn't improve compression ratio.
  92. Optimization removes this superfluos information in order to
  93. increase decompression speed.
  94. Optimization works similar to decompression except that the
  95. compressed data is modified as well. The length of the compressed
  96. data block will not change - only the compressed data-bytes will
  97. get rearranged a little bit.
  98. Don't expect too much, though - my tests have shown that the
  99. optimization step improves decompression speed by about 1-3%.
  100. I need even more decompression speed...
  101. =======================================
  102. Many RISC processors (like MIPS) can transfer 32-bit words much
  103. faster than bytes - this can significantly speed up decompression.
  104. So after verifying that everything works fine you can try if activating
  105. the LZO_ALIGNED_OK_4 macro improves LZO1X and LZO1Y decompression
  106. performance. Change the file config.h accordingly and recompile everything.
  107. On an i386 architecture you should evaluate the assembler versions.
  108. How can I reduce memory requirements when (de)compressing ?
  109. ===========================================================
  110. If you cleverly arrange your data, you can do an overlapping (in-place)
  111. decompression which means that you can decompress to the *same*
  112. block where the compressed data resides. This effectively removes
  113. the space requirements for holding the compressed data block.
  114. This technique is essential e.g. for usage in an executable packer.
  115. You can also partly overlay the buffers when doing compression.
  116. See examples/overlap.c for a working example.
  117. Can you give a cookbook for using pre-compressed data ?
  118. =======================================================
  119. Let's assume you use LZO1X-999.
  120. 1) pre-compression step
  121. - call lzo_init()
  122. - call lzo1x_999_compress()
  123. - call lzo1x_optimize()
  124. - compute an adler32 checksum of the *compressed* data
  125. - store the compressed data and the checksum in a file
  126. - if you are paranoid you should verify decompression now
  127. 2) decompression step within your application
  128. - call lzo_init()
  129. - load your compressed data and the checksum
  130. - optionally verify the checksum of the compressed data
  131. (so that you can use the standard decompressor)
  132. - decompress
  133. See examples/precomp.c and examples/precomp2.c for a working example.
  134. How much can my data expand during compression ?
  135. ================================================
  136. LZO will expand incompressible data by a little amount.
  137. I still haven't computed the exact values, but I suggest using
  138. these formulas for a worst-case expansion calculation:
  139. Algorithm LZO1, LZO1A, LZO1B, LZO1C, LZO1F, LZO1X, LZO1Y, LZO1Z:
  140. ----------------------------------------------------------------
  141. output_block_size = input_block_size + (input_block_size / 16) + 64 + 3
  142. [This is about 106% for a large block size.]
  143. Algorithm LZO2A:
  144. ----------------
  145. output_block_size = input_block_size + (input_block_size / 8) + 128 + 3