1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/* Copyright (C) 2017 MariaDB Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <my_global.h>
#include "sql_string.h"
#include "sql_class.h"
#include "field_comp.h"
#include <zlib.h>
static uint compress_zlib(THD *thd, char *to, const char *from, uint length)
{
uint level= thd->variables.column_compression_zlib_level;
if (level > 0)
{
z_stream stream;
int wbits= thd->variables.column_compression_zlib_wrap ? MAX_WBITS :
-MAX_WBITS;
uint strategy= thd->variables.column_compression_zlib_strategy;
/* Store only meaningful bytes of original data length. */
uchar original_pack_length= number_storage_requirement(length);
*to= 0x80 + original_pack_length + (wbits < 0 ? 8 : 0);
store_bigendian(length, (uchar*) to + 1, original_pack_length);
stream.avail_in= length;
stream.next_in= (Bytef*) from;
stream.avail_out= length - original_pack_length - 1;
stream.next_out= (Bytef*) to + original_pack_length + 1;
stream.zalloc= 0;
stream.zfree= 0;
stream.opaque= 0;
if (deflateInit2(&stream, level, Z_DEFLATED, wbits, 8, strategy) == Z_OK &&
deflate(&stream, Z_FINISH) == Z_STREAM_END &&
deflateEnd(&stream) == Z_OK)
return (uint) (stream.next_out - (Bytef*) to);
}
return 0;
}
static int uncompress_zlib(String *to, const uchar *from, uint from_length,
uint field_length)
{
z_stream stream;
uchar original_pack_length;
int wbits;
ulonglong avail_out;
original_pack_length= *from & 0x07;
wbits= *from & 8 ? -MAX_WBITS : MAX_WBITS;
from++;
from_length--;
if (from_length < original_pack_length)
{
my_error(ER_ZLIB_Z_DATA_ERROR, MYF(0));
return 1;
}
avail_out= (ulonglong)read_bigendian(from, original_pack_length);
if (avail_out > field_length)
{
my_error(ER_ZLIB_Z_DATA_ERROR, MYF(0));
return 1;
}
stream.avail_out= (uint)avail_out;
if (to->alloc(stream.avail_out))
return 1;
stream.next_out= (Bytef*) to->ptr();
stream.avail_in= from_length - original_pack_length;
stream.next_in= (Bytef*) from + original_pack_length;
stream.zalloc= 0;
stream.zfree= 0;
stream.opaque= 0;
if (inflateInit2(&stream, wbits) == Z_OK &&
inflate(&stream, Z_FINISH) == Z_STREAM_END &&
inflateEnd(&stream) == Z_OK)
{
to->length(stream.total_out);
return 0;
}
my_error(ER_ZLIB_Z_DATA_ERROR, MYF(0));
return 1;
}
Compression_method compression_methods[MAX_COMPRESSION_METHODS]=
{
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ "zlib", compress_zlib, uncompress_zlib },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ 0, 0, 0 }
};
|