127b27ec6Sopenharmony_ci# LZ4 API Example : Dictionary Random Access 227b27ec6Sopenharmony_ci 327b27ec6Sopenharmony_ci`dictionaryRandomAccess.c` is LZ4 API example which implements dictionary compression and random access decompression. 427b27ec6Sopenharmony_ci 527b27ec6Sopenharmony_ciPlease note that the output file is not compatible with lz4frame and is platform dependent. 627b27ec6Sopenharmony_ci 727b27ec6Sopenharmony_ci 827b27ec6Sopenharmony_ci## What's the point of this example ? 927b27ec6Sopenharmony_ci 1027b27ec6Sopenharmony_ci - Dictionary based compression for homogeneous files. 1127b27ec6Sopenharmony_ci - Random access to compressed blocks. 1227b27ec6Sopenharmony_ci 1327b27ec6Sopenharmony_ci 1427b27ec6Sopenharmony_ci## How the compression works 1527b27ec6Sopenharmony_ci 1627b27ec6Sopenharmony_ciReads the dictionary from a file, and uses it as the history for each block. 1727b27ec6Sopenharmony_ciThis allows each block to be independent, but maintains compression ratio. 1827b27ec6Sopenharmony_ci 1927b27ec6Sopenharmony_ci``` 2027b27ec6Sopenharmony_ci Dictionary 2127b27ec6Sopenharmony_ci + 2227b27ec6Sopenharmony_ci | 2327b27ec6Sopenharmony_ci v 2427b27ec6Sopenharmony_ci +---------+ 2527b27ec6Sopenharmony_ci | Block#1 | 2627b27ec6Sopenharmony_ci +----+----+ 2727b27ec6Sopenharmony_ci | 2827b27ec6Sopenharmony_ci v 2927b27ec6Sopenharmony_ci {Out#1} 3027b27ec6Sopenharmony_ci 3127b27ec6Sopenharmony_ci 3227b27ec6Sopenharmony_ci Dictionary 3327b27ec6Sopenharmony_ci + 3427b27ec6Sopenharmony_ci | 3527b27ec6Sopenharmony_ci v 3627b27ec6Sopenharmony_ci +---------+ 3727b27ec6Sopenharmony_ci | Block#2 | 3827b27ec6Sopenharmony_ci +----+----+ 3927b27ec6Sopenharmony_ci | 4027b27ec6Sopenharmony_ci v 4127b27ec6Sopenharmony_ci {Out#2} 4227b27ec6Sopenharmony_ci``` 4327b27ec6Sopenharmony_ci 4427b27ec6Sopenharmony_ciAfter writing the magic bytes `TEST` and then the compressed blocks, write out the jump table. 4527b27ec6Sopenharmony_ciThe last 4 bytes is an integer containing the number of blocks in the stream. 4627b27ec6Sopenharmony_ciIf there are `N` blocks, then just before the last 4 bytes is `N + 1` 4 byte integers containing the offsets at the beginning and end of each block. 4727b27ec6Sopenharmony_ciLet `Offset#K` be the total number of bytes written after writing out `Block#K` *including* the magic bytes for simplicity. 4827b27ec6Sopenharmony_ci 4927b27ec6Sopenharmony_ci``` 5027b27ec6Sopenharmony_ci+------+---------+ +---------+---+----------+ +----------+-----+ 5127b27ec6Sopenharmony_ci| TEST | Block#1 | ... | Block#N | 4 | Offset#1 | ... | Offset#N | N+1 | 5227b27ec6Sopenharmony_ci+------+---------+ +---------+---+----------+ +----------+-----+ 5327b27ec6Sopenharmony_ci``` 5427b27ec6Sopenharmony_ci 5527b27ec6Sopenharmony_ci## How the decompression works 5627b27ec6Sopenharmony_ci 5727b27ec6Sopenharmony_ciDecompression will do reverse order. 5827b27ec6Sopenharmony_ci 5927b27ec6Sopenharmony_ci - Seek to the last 4 bytes of the file and read the number of offsets. 6027b27ec6Sopenharmony_ci - Read each offset into an array. 6127b27ec6Sopenharmony_ci - Seek to the first block containing data we want to read. 6227b27ec6Sopenharmony_ci We know where to look because we know each block contains a fixed amount of uncompressed data, except possibly the last. 6327b27ec6Sopenharmony_ci - Decompress it and write what data we need from it to the file. 6427b27ec6Sopenharmony_ci - Read the next block. 6527b27ec6Sopenharmony_ci - Decompress it and write that page to the file. 6627b27ec6Sopenharmony_ci 6727b27ec6Sopenharmony_ciContinue these procedures until all the required data has been read. 68