|
kusano |
7d535a |
============================================================================
|
|
kusano |
7d535a |
LZO Frequently Asked Questions
|
|
kusano |
7d535a |
============================================================================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
I hate reading docs - just tell me how to add compression to my program
|
|
kusano |
7d535a |
=======================================================================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
This is for the impatient: take a look at examples/simple.c and
|
|
kusano |
7d535a |
examples/lzopack.c and see how easy this is.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
But you will come back to read the documentation later, won't you ?
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Can you explain the naming conventions of the algorithms ?
|
|
kusano |
7d535a |
==========================================================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Let's take a look at LZO1X:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The algorithm name is LZO1X.
|
|
kusano |
7d535a |
The algorithm category is LZO1.
|
|
kusano |
7d535a |
Various compression levels are implemented.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
LZO1X-999
|
|
kusano |
7d535a |
!---------- algorithm category
|
|
kusano |
7d535a |
!--------- algorithm type
|
|
kusano |
7d535a |
!!!----- compression level (1-9, 99, 999)
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
LZO1X-1(11)
|
|
kusano |
7d535a |
!---------- algorithm category
|
|
kusano |
7d535a |
!--------- algorithm type
|
|
kusano |
7d535a |
!------- compression level (1-9, 99, 999)
|
|
kusano |
7d535a |
!!---- memory level (memory requirements for compression)
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
All compression/memory levels generate the same compressed data format,
|
|
kusano |
7d535a |
so e.g. the LZO1X decompressor handles all LZO1X-* compression levels
|
|
kusano |
7d535a |
(for more information about the decompressors see below).
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Category LZO1 algorithms: compressed data format is strictly byte aligned
|
|
kusano |
7d535a |
Category LZO2 algorithms: uses bit-shifting, slower decompression
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Why are there so many algorithms ?
|
|
kusano |
7d535a |
==================================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Because of historical reasons - I want to support unlimited
|
|
kusano |
7d535a |
backward compatibility.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Don't get misled by the size of the library - using one algorithm
|
|
kusano |
7d535a |
increases the size of your application by only a few kB.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
If you just want to add a little bit of data compression to your
|
|
kusano |
7d535a |
application you may be looking for miniLZO.
|
|
kusano |
7d535a |
See minilzo/README.LZO for more information.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Which algorithm should I use ?
|
|
kusano |
7d535a |
==============================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
LZO1X seems to be best choice in many cases, so:
|
|
kusano |
7d535a |
- when going for speed use LZO1X-1
|
|
kusano |
7d535a |
- when generating pre-compressed data use LZO1X-999
|
|
kusano |
7d535a |
- if you have little memory available for compression use LZO1X-1(11)
|
|
kusano |
7d535a |
or LZO1X-1(12)
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Of course, your mileage may vary, and you are encouraged to run your
|
|
kusano |
7d535a |
own experiments. Try LZO1Y and LZO1F next.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
What's the difference between the decompressors per algorithm ?
|
|
kusano |
7d535a |
===============================================================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Once again let's use LZO1X for explanation:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
- lzo1x_decompress
|
|
kusano |
7d535a |
The `standard' decompressor. Pretty fast - use this whenever possible.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
This decompressor expects valid compressed data.
|
|
kusano |
7d535a |
If the compressed data gets corrupted somehow (e.g. transmission
|
|
kusano |
7d535a |
via an erroneous channel, disk errors, ...) it will probably crash
|
|
kusano |
7d535a |
your application because absolutely no additional checks are done.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
- lzo1x_decompress_safe
|
|
kusano |
7d535a |
The `safe' decompressor. Somewhat slower.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
This decompressor will catch all compressed data violations and
|
|
kusano |
7d535a |
return an error code in this case - it will never crash.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
- lzo1x_decompress_asm
|
|
kusano |
7d535a |
Same as lzo1x_decompress - written in assembler.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
- lzo1x_decompress_asm_safe
|
|
kusano |
7d535a |
Same as lzo1x_decompress_safe - written in assembler.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
- lzo1x_decompress_asm_fast
|
|
kusano |
7d535a |
Similar to lzo1x_decompress_asm - but even faster.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
For reasons of speed this decompressor can write up to 3 bytes
|
|
kusano |
7d535a |
past the end of the decompressed (output) block.
|
|
kusano |
7d535a |
[ technical note: because data is transferred in 32-bit units ]
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Use this when you are decompressing from one memory block to
|
|
kusano |
7d535a |
another memory block - just provide output space for 3 extra bytes.
|
|
kusano |
7d535a |
You shouldn't use it if e.g. you are directly decompressing to video
|
|
kusano |
7d535a |
memory (because the extra bytes will be show up on the screen).
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
- lzo1x_decompress_asm_fast_safe
|
|
kusano |
7d535a |
This is the safe version of lzo1x_decompress_asm_fast.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Notes:
|
|
kusano |
7d535a |
------
|
|
kusano |
7d535a |
- When using a safe decompressor you must pass the number of
|
|
kusano |
7d535a |
bytes available in `dst' via the parameter `dst_len'.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
- If you want to be sure that your data is not corrupted you must
|
|
kusano |
7d535a |
use a checksum - just using the safe decompressor is not enough,
|
|
kusano |
7d535a |
because many data errors will not result in a compressed data violation.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
- Assembler versions are only available for the i386 family yet.
|
|
kusano |
7d535a |
Please see also asm/i386/00README.TXT
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
- You should test if the assembler versions are actually faster
|
|
kusano |
7d535a |
than the C version on your machine - some compilers can do a very
|
|
kusano |
7d535a |
good optimization job and they also can optimize the code
|
|
kusano |
7d535a |
for a specific processor.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
What is this optimization thing ?
|
|
kusano |
7d535a |
=================================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
The compressors use a heuristic approach - they sometimes code
|
|
kusano |
7d535a |
information that doesn't improve compression ratio.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Optimization removes this superfluos information in order to
|
|
kusano |
7d535a |
increase decompression speed.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Optimization works similar to decompression except that the
|
|
kusano |
7d535a |
compressed data is modified as well. The length of the compressed
|
|
kusano |
7d535a |
data block will not change - only the compressed data-bytes will
|
|
kusano |
7d535a |
get rearranged a little bit.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Don't expect too much, though - my tests have shown that the
|
|
kusano |
7d535a |
optimization step improves decompression speed by about 1-3%.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
I need even more decompression speed...
|
|
kusano |
7d535a |
=======================================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Many RISC processors (like MIPS) can transfer 32-bit words much
|
|
kusano |
7d535a |
faster than bytes - this can significantly speed up decompression.
|
|
kusano |
7d535a |
So after verifying that everything works fine you can try if activating
|
|
kusano |
7d535a |
the LZO_ALIGNED_OK_4 macro improves LZO1X and LZO1Y decompression
|
|
kusano |
7d535a |
performance. Change the file config.h accordingly and recompile everything.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
On a i386 architecture you should evaluate the assembler versions.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
How can I reduce memory requirements when (de)compressing ?
|
|
kusano |
7d535a |
===========================================================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
If you cleverly arrange your data, you can do an overlapping (in-place)
|
|
kusano |
7d535a |
decompression which means that you can decompress to the *same*
|
|
kusano |
7d535a |
block where the compressed data resides. This effectively removes
|
|
kusano |
7d535a |
the space requirements for holding the compressed data block.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
This technique is essential e.g. for usage in an executable packer.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
You can also partly overlay the buffers when doing compression.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
See examples/overlap.c for a working example.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Can you give a cookbook for using pre-compressed data ?
|
|
kusano |
7d535a |
=======================================================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Let's assume you use LZO1X-999.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
1) pre-compression step
|
|
kusano |
7d535a |
- call lzo_init()
|
|
kusano |
7d535a |
- call lzo1x_999_compress()
|
|
kusano |
7d535a |
- call lzo1x_optimize()
|
|
kusano |
7d535a |
- compute an adler32 checksum of the *compressed* data
|
|
kusano |
7d535a |
- store the compressed data and the checksum in a file
|
|
kusano |
7d535a |
- if you are paranoid you should verify decompression now
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
2) decompression step within your application
|
|
kusano |
7d535a |
- call lzo_init()
|
|
kusano |
7d535a |
- load your compressed data and the checksum
|
|
kusano |
7d535a |
- optionally verify the checksum of the compressed data
|
|
kusano |
7d535a |
(so that you can use the standard decompressor)
|
|
kusano |
7d535a |
- decompress
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
See examples/precomp.c and examples/precomp2.c for a working example.
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
How much can my data expand during compression ?
|
|
kusano |
7d535a |
================================================
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
LZO will expand incompressible data by a little amount.
|
|
kusano |
7d535a |
I still haven't computed the exact values, but I suggest using
|
|
kusano |
7d535a |
these formulas for a worst-case expansion calculation:
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Algorithm LZO1, LZO1A, LZO1B, LZO1C, LZO1F, LZO1X, LZO1Y, LZO1Z:
|
|
kusano |
7d535a |
----------------------------------------------------------------
|
|
kusano |
7d535a |
output_block_size = input_block_size + (input_block_size / 16) + 64 + 3
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
[This is about 106% for a large block size.]
|
|
kusano |
7d535a |
|
|
kusano |
7d535a |
Algorithm LZO2A:
|
|
kusano |
7d535a |
----------------
|
|
kusano |
7d535a |
output_block_size = input_block_size + (input_block_size / 8) + 128 + 3
|
|
kusano |
7d535a |
|