quicklz.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef QLZ_HEADER
  2. #define QLZ_HEADER
  3. #include <rtconfig.h>
  4. /* Compiler Related Definitions */
  5. #ifdef __CC_ARM /* ARM Compiler */
  6. #elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
  7. #define __inline inline
  8. #elif defined (__GNUC__) /* GNU GCC Compiler */
  9. #else
  10. #error not supported tool chain
  11. #endif
  12. // Fast data compression library
  13. // Copyright (C) 2006-2011 Lasse Mikkel Reinhold
  14. // lar@quicklz.com
  15. //
  16. // QuickLZ can be used for free under the GPL 1, 2 or 3 license (where anything
  17. // released into public must be open source) or under a commercial license if such
  18. // has been acquired (see http://www.quicklz.com/order.html). The commercial license
  19. // does not cover derived or ported versions created by third parties under GPL.
  20. // You can edit following user settings. Data must be decompressed with the same
  21. // setting of QLZ_COMPRESSION_LEVEL and QLZ_STREAMING_BUFFER as it was compressed
  22. // (see manual). If QLZ_STREAMING_BUFFER > 0, scratch buffers must be initially
  23. // zeroed out (see manual). First #ifndef makes it possible to define settings from
  24. // the outside like the compiler command line.
  25. // 1.5.0 final
  26. // 1 gives fastest compression speed. 3 gives fastest decompression speed and best
  27. // compression ratio.
  28. #ifndef QLZ_COMPRESSION_LEVEL
  29. #define QLZ_COMPRESSION_LEVEL 1
  30. //#define QLZ_COMPRESSION_LEVEL 2
  31. //#define QLZ_COMPRESSION_LEVEL 3
  32. #endif
  33. // If > 0, zero out both states prior to first call to qlz_compress() or qlz_decompress()
  34. // and decompress packets in the same order as they were compressed
  35. #ifndef QLZ_STREAMING_BUFFER
  36. #define QLZ_STREAMING_BUFFER 0
  37. //#define QLZ_STREAMING_BUFFER 1000000
  38. //#define QLZ_STREAMING_BUFFER 1000000
  39. #endif
  40. // Guarantees that decompression of corrupted data cannot crash. Decreases decompression
  41. // speed 10-20%. Compression speed not affected.
  42. #define QLZ_MEMORY_SAFE
  43. #define QLZ_VERSION_MAJOR 1
  44. #define QLZ_VERSION_MINOR 5
  45. #define QLZ_VERSION_REVISION 0
  46. // Buffer padding for destination buffer, least size + 400 bytes large because incompressible data may increase in size.
  47. #define QLZ_BUFFER_PADDING 400
  48. // Using size_t, memset() and memcpy()
  49. #include <string.h>
  50. // Verify compression level
  51. #if QLZ_COMPRESSION_LEVEL != 1 && QLZ_COMPRESSION_LEVEL != 2 && QLZ_COMPRESSION_LEVEL != 3
  52. #error QLZ_COMPRESSION_LEVEL must be 1, 2 or 3
  53. #endif
  54. typedef unsigned int ui32;
  55. typedef unsigned short int ui16;
  56. // Decrease QLZ_POINTERS for level 3 to increase compression speed. Do not touch any other values!
  57. #if QLZ_COMPRESSION_LEVEL == 1
  58. #define QLZ_POINTERS 1
  59. #define QLZ_HASH_VALUES 4096
  60. #elif QLZ_COMPRESSION_LEVEL == 2
  61. #define QLZ_POINTERS 4
  62. #define QLZ_HASH_VALUES 2048
  63. #elif QLZ_COMPRESSION_LEVEL == 3
  64. #define QLZ_POINTERS 16
  65. #define QLZ_HASH_VALUES 4096
  66. #endif
  67. // Detect if pointer size is 64-bit. It's not fatal if some 64-bit target is not detected because this is only for adding an optional 64-bit optimization.
  68. #if defined _LP64 || defined __LP64__ || defined __64BIT__ || _ADDR64 || defined _WIN64 || defined __arch64__ || __WORDSIZE == 64 || (defined __sparc && defined __sparcv9) || defined __x86_64 || defined __amd64 || defined __x86_64__ || defined _M_X64 || defined _M_IA64 || defined __ia64 || defined __IA64__
  69. #define QLZ_PTR_64
  70. #endif
  71. // hash entry
  72. typedef struct
  73. {
  74. #if QLZ_COMPRESSION_LEVEL == 1
  75. ui32 cache;
  76. #if defined QLZ_PTR_64 && QLZ_STREAMING_BUFFER == 0
  77. unsigned int offset;
  78. #else
  79. const unsigned char *offset;
  80. #endif
  81. #else
  82. const unsigned char *offset[QLZ_POINTERS];
  83. #endif
  84. } qlz_hash_compress;
  85. typedef struct
  86. {
  87. #if QLZ_COMPRESSION_LEVEL == 1
  88. const unsigned char *offset;
  89. #else
  90. const unsigned char *offset[QLZ_POINTERS];
  91. #endif
  92. } qlz_hash_decompress;
  93. // states
  94. typedef struct
  95. {
  96. #if QLZ_STREAMING_BUFFER > 0
  97. unsigned char stream_buffer[QLZ_STREAMING_BUFFER];
  98. #endif
  99. size_t stream_counter;
  100. qlz_hash_compress hash[QLZ_HASH_VALUES];
  101. unsigned char hash_counter[QLZ_HASH_VALUES];
  102. } qlz_state_compress;
  103. #if QLZ_COMPRESSION_LEVEL == 1 || QLZ_COMPRESSION_LEVEL == 2
  104. typedef struct
  105. {
  106. #if QLZ_STREAMING_BUFFER > 0
  107. unsigned char stream_buffer[QLZ_STREAMING_BUFFER];
  108. #endif
  109. qlz_hash_decompress hash[QLZ_HASH_VALUES];
  110. unsigned char hash_counter[QLZ_HASH_VALUES];
  111. size_t stream_counter;
  112. } qlz_state_decompress;
  113. #elif QLZ_COMPRESSION_LEVEL == 3
  114. typedef struct
  115. {
  116. #if QLZ_STREAMING_BUFFER > 0
  117. unsigned char stream_buffer[QLZ_STREAMING_BUFFER];
  118. #endif
  119. #if QLZ_COMPRESSION_LEVEL <= 2
  120. qlz_hash_decompress hash[QLZ_HASH_VALUES];
  121. #endif
  122. size_t stream_counter;
  123. } qlz_state_decompress;
  124. #endif
  125. #if defined (__cplusplus)
  126. extern "C" {
  127. #endif
  128. // Public functions of QuickLZ
  129. size_t qlz_size_decompressed(const char *source);
  130. size_t qlz_size_compressed(const char *source);
  131. size_t qlz_compress(const void *source, char *destination, size_t size, qlz_state_compress *state);
  132. size_t qlz_decompress(const char *source, void *destination, qlz_state_decompress *state);
  133. int qlz_get_setting(int setting);
  134. #if defined (__cplusplus)
  135. }
  136. #endif
  137. #endif