quicklz.h 4.4 KB

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