lzf.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /****************************************************************************
  2. * include/lzf.h
  3. * http://liblzf.plan9.de/
  4. *
  5. * Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de>
  6. * This algorithm is believed to be patent-free.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. ****************************************************************************/
  31. #ifndef __INCLUDE_LZF_H
  32. #define __INCLUDE_LZF_H
  33. /****************************************************************************
  34. * Pre-processor Definitions
  35. ****************************************************************************/
  36. #define LZF_VERSION 0x0105 /* 1.5, API version */
  37. #define HLOG CONFIG_LIBC_LZF_HLOG
  38. #define LZF_TYPE0_HDR 0
  39. #define LZF_TYPE1_HDR 1
  40. #define LZF_TYPE0_HDR_SIZE 5
  41. #define LZF_TYPE1_HDR_SIZE 7
  42. #define LZF_MAX_HDR_SIZE 7
  43. #define LZF_MIN_HDR_SIZE 5
  44. /****************************************************************************
  45. * Public Types
  46. ****************************************************************************/
  47. /* LZF headers */
  48. struct lzf_header_s /* Common data header */
  49. {
  50. uint8_t lzf_magic[2]; /* [0]='Z', [1]='V' */
  51. uint8_t lzf_type; /* LZF_TYPE0_HDR or LZF_TYPE1_HDR */
  52. };
  53. struct lzf_type0_header_s /* Uncompressed data header */
  54. {
  55. uint8_t lzf_magic[2]; /* [0]='Z', [1]='V' */
  56. uint8_t lzf_type; /* LZF_TYPE0_HDR */
  57. uint8_t lzf_len[2]; /* Data length (big-endian) */
  58. };
  59. struct lzf_type1_header_s /* Compressed data header */
  60. {
  61. uint8_t lzf_magic[2]; /* [0]='Z', [1]='V' */
  62. uint8_t lzf_type; /* LZF_TYPE1_HDR */
  63. uint8_t lzf_clen[2]; /* Compressed data length (big-endian) */
  64. uint8_t lzf_ulen[2]; /* Uncompressed data length (big-endian) */
  65. };
  66. /* LZF hash table */
  67. #if LZF_USE_OFFSETS
  68. # define LZF_HSLOT_BIAS ((const uint8_t *)in_data)
  69. typedef unsigned int lzf_hslot_t;
  70. #else
  71. # define LZF_HSLOT_BIAS 0
  72. typedef const uint8_t *lzf_hslot_t;
  73. #endif
  74. typedef lzf_hslot_t lzf_state_t[1 << HLOG];
  75. /****************************************************************************
  76. * Public Function Prototypes
  77. ****************************************************************************/
  78. /****************************************************************************
  79. * Name: lzf_compress
  80. *
  81. * Description:
  82. * Compress in_len bytes stored at the memory block starting at
  83. * in_data and write the result to out_data, up to a maximum length
  84. * of out_len bytes.
  85. *
  86. * If the output buffer is not large enough or any error occurs return 0,
  87. * otherwise return the number of bytes used, which might be considerably
  88. * more than in_len (but less than 104% of the original size), so it
  89. * makes sense to always use out_len == in_len - 1), to ensure _some_
  90. * compression, and store the data uncompressed otherwise (with a flag, of
  91. * course.
  92. *
  93. * lzf_compress might use different algorithms on different systems and
  94. * even different runs, thus might result in different compressed strings
  95. * depending on the phase of the moon or similar factors. However, all
  96. * these strings are architecture-independent and will result in the
  97. * original data when decompressed using lzf_decompress.
  98. *
  99. * The buffers must not be overlapping.
  100. *
  101. * Compressed format:
  102. *
  103. * 000LLLLL <L+1> ; literal, L+1=1..33 octets
  104. * LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset
  105. * 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset
  106. *
  107. ****************************************************************************/
  108. size_t lzf_compress(FAR const void *const in_data,
  109. unsigned int in_len, FAR void *out_data,
  110. unsigned int out_len, lzf_state_t htab,
  111. FAR struct lzf_header_s **reshdr);
  112. /****************************************************************************
  113. * Name: lzf_decompress
  114. *
  115. * Description:
  116. * Decompress data compressed with some version of the lzf_compress
  117. * function and stored at location in_data and length in_len. The result
  118. * will be stored at out_data up to a maximum of out_len characters.
  119. *
  120. * If the output buffer is not large enough to hold the decompressed
  121. * data, a 0 is returned and errno is set to E2BIG. Otherwise the number
  122. * of decompressed bytes (i.e. the original length of the data) is
  123. * returned.
  124. *
  125. * If an error in the compressed data is detected, a zero is returned and
  126. * errno is set to EINVAL.
  127. *
  128. * This function is very fast, about as fast as a copying loop.
  129. *
  130. ****************************************************************************/
  131. unsigned int lzf_decompress(FAR const void *const in_data,
  132. unsigned int in_len, FAR void *out_data,
  133. unsigned int out_len);
  134. #endif /* __INCLUDE_LZF_H */