Blame doc/LZO.FAQ

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