Blame doc/LZO.FAQ

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