| /* |
| * Copyright (c) 2020 The Fuchsia Authors |
| * |
| * SPDX-License-Identifier: BSD-3-Clause |
| */ |
| |
| #ifndef LZ4_WRAPPER_H_ |
| #define LZ4_WRAPPER_H_ |
| |
| #include <linux/types.h> |
| #include <compiler.h> |
| #include <linux/kernel.h> |
| |
| typedef uint8_t BYTE; |
| typedef uint16_t U16; |
| typedef uint32_t U32; |
| typedef int32_t S32; |
| typedef uint64_t U64; |
| |
| #define FORCE_INLINE static inline __attribute__((always_inline)) |
| |
| struct lz4_frame_header { |
| u32 magic; |
| union { |
| u8 flags; |
| struct { |
| u8 reserved0 : 2; |
| u8 has_content_checksum : 1; |
| u8 has_content_size : 1; |
| u8 has_block_checksum : 1; |
| u8 independent_blocks : 1; |
| u8 version : 2; |
| }; |
| }; |
| union { |
| u8 block_descriptor; |
| struct { |
| u8 reserved1 : 4; |
| u8 max_block_size : 3; |
| u8 reserved2 : 1; |
| }; |
| }; |
| /* + u64 content_size iff has_content_size is set */ |
| /* + u8 header_checksum */ |
| } __packed; |
| |
| /** |
| * Check whether the given buffer in |src| is lz4 compressed. |
| */ |
| bool is_data_lz4_compressed(const void *src, size_t srcn); |
| |
| /** |
| * Return the maximum uncompressed block size in a lz4 frame in bytes. |
| * @param h The pointer to the frame header. |
| * @param out Output pointer of the size in bytes. |
| * |
| * Return 0 on success, other values on failure. |
| */ |
| int get_lz4_max_block_size(const struct lz4_frame_header *h, size_t *out); |
| |
| /** |
| * Validate and extract the header in lz4 compressed data given by |src|. |
| * |
| * @param src The lz4 compressed data. |
| * @param srcn Size of |src|. |
| * @param out Output pointer for storing the extracted header. |
| * @param data_offset Offset of the actual compressed data in |src|. |
| * |
| * Returns 0 on success, other values on failure. |
| */ |
| int check_and_extract_lz4_frame_header(const void *src, size_t srcn, |
| struct lz4_frame_header *out, |
| size_t *data_offset); |
| |
| /** |
| * Decompress one block of data from lz4 data. |
| * |
| * @param src The lz4 compressed data. |
| * @param srcn Size of |src|. |
| * @param dst Output pointer for storing the decompress block. |
| * @param dstn Size of |dst|. |
| * @param block_size Output pointer for size of current compressed block. |
| * @param decompressed_size Output pointer for the amount of data decompressed. |
| * |
| * Returns 0 on success, other values on failure. |
| */ |
| int decompress_block(const void *src, size_t srcn, void *dst, int dstn, |
| size_t *block_size, size_t *decompressed_size); |
| |
| /** |
| * Decompress as much data as possible that can fit into the output buffer. |
| * |
| * @param h Pointer to the lz4 frame header. |
| * @param src Pointer to the lz4 compressed block data. |
| * @param srcn Pointer to a size_t. It shall be the ize of |src| when calling |
| * the function. Upon successful return, the value will be set to the number |
| * of bytes processed from |src|. |
| * @param dst Output buffer |
| * @param dstn Pointer to a size_t. It shall be the size of |dst| when |
| * calling the function. Upon successful return, the value will be set to |
| * the number of bytes decompressed. |
| * @param has_more_data An output boolean pointer for indicating whether |
| * there is more data available to decompress. |
| * |
| * Returns 0 on success, other values on failure. |
| */ |
| int lz4_decompress(const struct lz4_frame_header *h, const uint8_t *src, |
| size_t *srcn, uint8_t *dst, size_t *dstn, |
| bool *has_more_data); |
| |
| #endif |