1/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3#ident "$Id$"
4/*======
5This file is part of PerconaFT.
6
7
8Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35======= */
36
37#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39#pragma once
40
41// Fast data compression library
42// Copyright (C) 2006-2011 Lasse Mikkel Reinhold
43// lar@quicklz.com
44//
45// QuickLZ can be used for free under the GPL 1, 2 or 3 license (where anything
46// released into public must be open source) or under a commercial license if such
47// has been acquired (see http://www.quicklz.com/order.html). The commercial license
48// does not cover derived or ported versions created by third parties under GPL.
49
50// You can edit following user settings. Data must be decompressed with the same
51// setting of QLZ_COMPRESSION_LEVEL and QLZ_STREAMING_BUFFER as it was compressed
52// (see manual). If QLZ_STREAMING_BUFFER > 0, scratch buffers must be initially
53// zeroed out (see manual). First #ifndef makes it possible to define settings from
54// the outside like the compiler command line.
55
56// 1.5.0 final
57
58#ifndef QLZ_COMPRESSION_LEVEL
59 //#define QLZ_COMPRESSION_LEVEL 1
60 //#define QLZ_COMPRESSION_LEVEL 2
61 #define QLZ_COMPRESSION_LEVEL 3
62
63 #define QLZ_STREAMING_BUFFER 0
64 //#define QLZ_STREAMING_BUFFER 100000
65 //#define QLZ_STREAMING_BUFFER 1000000
66
67 //#define QLZ_MEMORY_SAFE
68#endif
69
70#define QLZ_VERSION_MAJOR 1
71#define QLZ_VERSION_MINOR 5
72#define QLZ_VERSION_REVISION 0
73
74// Using size_t, memset() and memcpy()
75#include <string.h>
76
77// Verify compression level
78#if QLZ_COMPRESSION_LEVEL != 1 && QLZ_COMPRESSION_LEVEL != 2 && QLZ_COMPRESSION_LEVEL != 3
79#error QLZ_COMPRESSION_LEVEL must be 1, 2 or 3
80#endif
81
82typedef unsigned int ui32;
83typedef unsigned short int ui16;
84
85// Decrease QLZ_POINTERS for level 3 to increase compression speed. Do not touch any other values!
86#if QLZ_COMPRESSION_LEVEL == 1
87#define QLZ_POINTERS 1
88#define QLZ_HASH_VALUES 4096
89#elif QLZ_COMPRESSION_LEVEL == 2
90#define QLZ_POINTERS 4
91#define QLZ_HASH_VALUES 2048
92#elif QLZ_COMPRESSION_LEVEL == 3
93#define QLZ_POINTERS 16
94#define QLZ_HASH_VALUES 4096
95#endif
96
97// 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.
98#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__
99 #define QLZ_PTR_64
100#endif
101
102// hash entry
103typedef struct
104{
105#if QLZ_COMPRESSION_LEVEL == 1
106 ui32 cache;
107#if defined QLZ_PTR_64 && QLZ_STREAMING_BUFFER == 0
108 unsigned int offset;
109#else
110 const unsigned char *offset;
111#endif
112#else
113 const unsigned char *offset[QLZ_POINTERS];
114#endif
115
116} qlz_hash_compress;
117
118typedef struct
119{
120#if QLZ_COMPRESSION_LEVEL == 1
121 const unsigned char *offset;
122#else
123 const unsigned char *offset[QLZ_POINTERS];
124#endif
125} qlz_hash_decompress;
126
127
128// states
129typedef struct
130{
131 #if QLZ_STREAMING_BUFFER > 0
132 unsigned char stream_buffer[QLZ_STREAMING_BUFFER];
133 #endif
134 size_t stream_counter;
135 qlz_hash_compress hash[QLZ_HASH_VALUES];
136 unsigned char hash_counter[QLZ_HASH_VALUES];
137} qlz_state_compress;
138
139
140#if QLZ_COMPRESSION_LEVEL == 1 || QLZ_COMPRESSION_LEVEL == 2
141 typedef struct
142 {
143#if QLZ_STREAMING_BUFFER > 0
144 unsigned char stream_buffer[QLZ_STREAMING_BUFFER];
145#endif
146 qlz_hash_decompress hash[QLZ_HASH_VALUES];
147 unsigned char hash_counter[QLZ_HASH_VALUES];
148 size_t stream_counter;
149 } qlz_state_decompress;
150#elif QLZ_COMPRESSION_LEVEL == 3
151 typedef struct
152 {
153#if QLZ_STREAMING_BUFFER > 0
154 unsigned char stream_buffer[QLZ_STREAMING_BUFFER];
155#endif
156#if QLZ_COMPRESSION_LEVEL <= 2
157 qlz_hash_decompress hash[QLZ_HASH_VALUES];
158#endif
159 size_t stream_counter;
160 } qlz_state_decompress;
161#endif
162
163
164#if defined (__cplusplus)
165extern "C" {
166#endif
167
168// Public functions of QuickLZ
169size_t qlz_size_decompressed(const char *source);
170size_t qlz_size_compressed(const char *source);
171size_t qlz_compress(const void *source, char *destination, size_t size, qlz_state_compress *state);
172size_t qlz_decompress(const char *source, void *destination, qlz_state_decompress *state);
173int qlz_get_setting(int setting);
174
175#if defined (__cplusplus)
176}
177#endif
178