summaryrefslogtreecommitdiffstats
path: root/sql/field_comp.cc
blob: ab97c8ccf08da7454440a8465b658214a180a842 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* 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>


/**
  Compresses string using zlib

  @param[out]    to         destination buffer for compressed data
  @param[in]     from       data to compress
  @param[in]     length     from length

  Requirement is such that string stored at `to' must not exceed `from' length.
  Otherwise 0 is returned and caller stores string uncompressed.

  `to' must be large enough to hold `length' bytes.

  length == 1 is an edge case that may break stream.avail_out calculation: at
  least 2 bytes required to store metadata.
*/

static uint compress_zlib(THD *thd, char *to, const char *from, uint length)
{
  uint level= thd->variables.column_compression_zlib_level;

  /* Caller takes care of empty strings. */
  DBUG_ASSERT(length);

  if (level > 0 && length > 1)
  {
    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;

    DBUG_ASSERT(length >= static_cast<uint>(original_pack_length) + 1);
    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)
    {
      int res= deflate(&stream, Z_FINISH);
      if (deflateEnd(&stream) == Z_OK && res == Z_STREAM_END)
        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)
  {
    int res= inflate(&stream, Z_FINISH);
    if (inflateEnd(&stream) == Z_OK && res == Z_STREAM_END)
    {
      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 }
};