From ed59157a4b2b5c23fa0d7641cd7d51f7a080969d Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 18 Mar 2018 12:31:50 -0600 Subject: [PATCH] libc/lzf: Define structures to represent LZF headers. --- include/lzf.h | 31 +++++++++++++++++++++++++++++- libc/lzf/lzf_c.c | 50 ++++++++++++++++++++++++++++++------------------ 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/include/lzf.h b/include/lzf.h index 9de19ddf03..118aa04101 100644 --- a/include/lzf.h +++ b/include/lzf.h @@ -38,8 +38,12 @@ #define LZF_VERSION 0x0105 /* 1.5, API version */ #define HLOG CONFIG_LIBC_LZF_HLOG +#define LZF_TYPE0_HDR 0 +#define LZF_TYPE1_HDR 1 + #define LZF_TYPE0_HDR_SIZE 5 #define LZF_TYPE1_HDR_SIZE 7 + #define LZF_MAX_HDR_SIZE 7 #define LZF_MIN_HDR_SIZE 5 @@ -47,6 +51,31 @@ * Public Types ****************************************************************************/ +/* LZF headers */ + +struct lzf_header_s /* Common data header */ +{ + uint8_t lzf_magic[2]; /* [0]='Z', [1]='V' */ + uint8_t lzf_type; /* LZF_TYPE0_HDR or LZF_TYPE1_HDR */ +}; + +struct lzf_type0_header_s /* Uncompressed data header */ +{ + uint8_t lzf_magic[2]; /* [0]='Z', [1]='V' */ + uint8_t lzf_type; /* LZF_TYPE0_HDR */ + uint8_t lzf_len[2]; /* Data length (big-endian) */ +}; + +struct lzf_type1_header_s /* Compressed data header */ +{ + uint8_t lzf_magic[2]; /* [0]='Z', [1]='V' */ + uint8_t lzf_type; /* LZF_TYPE1_HDR */ + uint8_t lzf_clen[2]; /* Compressed data length (big-endian) */ + uint8_t lzf_ulen[2]; /* Unompressed data length (big-endian) */ +}; + +/* LZF hash table */ + #if LZF_USE_OFFSETS # define LZF_HSLOT_BIAS ((const uint8_t *)in_data) typedef unsigned int lzf_hslot_t; @@ -95,7 +124,7 @@ typedef lzf_hslot_t lzf_state_t[1 << HLOG]; size_t lzf_compress(FAR const void *const in_data, unsigned int in_len, FAR void *out_data, unsigned int out_len, lzf_state_t htab, - FAR uint8_t **reshdr); + FAR struct lzf_header_s **reshdr); /**************************************************************************** * Name: lzf_decompress diff --git a/libc/lzf/lzf_c.c b/libc/lzf/lzf_c.c index 1191923fa0..7204ce2ec8 100644 --- a/libc/lzf/lzf_c.c +++ b/libc/lzf/lzf_c.c @@ -126,14 +126,13 @@ size_t lzf_compress(FAR const void *const in_data, unsigned int in_len, FAR void *out_data, unsigned int out_len, lzf_state_t htab, - uint8_t **reshdr) + FAR struct lzf_header_s **reshdr) { FAR const uint8_t *ip = (const uint8_t *)in_data; FAR uint8_t *op = (uint8_t *)out_data; FAR const uint8_t *in_end = ip + in_len; FAR uint8_t *out_end = op + out_len; FAR const uint8_t *ref; - FAR uint8_t *header; ssize_t cs; ssize_t retlen; @@ -420,30 +419,43 @@ size_t lzf_compress(FAR const void *const in_data, genhdr: if (cs) { - header = (uint8_t *)out_data - LZF_TYPE1_HDR_SIZE; - header[0] = 'Z'; - header[1] = 'V'; - header[2] = 1; - header[3] = cs >> 8; - header[4] = cs & 0xff; - header[5] = in_len >> 8; - header[6] = in_len & 0xff; - retlen = cs + LZF_TYPE1_HDR_SIZE; + FAR struct lzf_type1_header_s *header; + + /* Write compressed */ + + header = (FAR struct lzf_type1_header_s *) + ((uintptr_t)out_data - LZF_TYPE1_HDR_SIZE); + + header->lzf_magic[0] = 'Z'; + header->lzf_magic[1] = 'V'; + header->lzf_type = LZF_TYPE1_HDR; + header->lzf_clen[0] = cs >> 8; + header->lzf_clen[1] = cs & 0xff; + header->lzf_ulen[0] = in_len >> 8; + header->lzf_ulen[1] = in_len & 0xff; + + *reshdr = (FAR struct lzf_header_s *)header; + retlen = cs + LZF_TYPE1_HDR_SIZE; } else { + FAR struct lzf_type0_header_s *header; + /* Write uncompressed */ - header = (uint8_t *)in_data - LZF_TYPE0_HDR_SIZE; - header[0] = 'Z'; - header[1] = 'V'; - header[2] = 0; - header[3] = in_len >> 8; - header[4] = in_len & 0xff; - retlen = in_len + LZF_TYPE0_HDR_SIZE; + header = (FAR struct lzf_type0_header_s *) + ((uintptr_t)in_data - LZF_TYPE0_HDR_SIZE); + + header->lzf_magic[0] = 'Z'; + header->lzf_magic[1] = 'V'; + header->lzf_type = LZF_TYPE0_HDR; + header->lzf_len[0] = in_len >> 8; + header->lzf_len[1] = in_len & 0xff; + + *reshdr = (FAR struct lzf_header_s *)header; + retlen = in_len + LZF_TYPE0_HDR_SIZE; } - *reshdr = header; return retlen; }