1 /*****************************************************************************
2 
3 Copyright (c) 2017, 2020, Oracle and/or its affiliates.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8 
9 This program is also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 
25 *****************************************************************************/
26 
27 #ifndef DICT_SDI_DECOMPRESS_H
28 #define DICT_SDI_DECOMPRESS_H
29 
30 #include <zlib.h>
31 
32 /** Decompress SDI record */
33 class Sdi_Decompressor {
34  public:
Sdi_Decompressor(byte * uncomp_sdi,uint32_t uncomp_len,byte * comp_sdi,uint32_t comp_len)35   Sdi_Decompressor(byte *uncomp_sdi, uint32_t uncomp_len, byte *comp_sdi,
36                    uint32_t comp_len)
37       : m_uncomp_sdi(uncomp_sdi),
38         m_uncomp_len(uncomp_len),
39         m_comp_sdi(comp_sdi),
40         m_comp_len(comp_len) {
41     ut_ad(m_uncomp_sdi != nullptr);
42     ut_ad(m_comp_sdi != nullptr);
43   }
44 
~Sdi_Decompressor()45   ~Sdi_Decompressor() {}
46 
47   /** Decompress the SDI and store in the buffer passed. */
decompress()48   inline void decompress() {
49     int ret;
50     uLongf dest_len = m_uncomp_len;
51     ret = uncompress(m_uncomp_sdi, &dest_len, m_comp_sdi, m_comp_len);
52 
53     if (ret != Z_OK) {
54 #ifdef UNIV_NO_ERR_MSGS
55       ib::error()
56 #else
57       ib::error(ER_IB_ERR_ZLIB_UNCOMPRESS_FAILED)
58 #endif
59           << "ZLIB uncompress() failed:"
60           << " compressed len: " << m_comp_len
61           << ", original_len: " << m_uncomp_len;
62 
63       switch (ret) {
64         case Z_BUF_ERROR:
65 #ifdef UNIV_NO_ERR_MSGS
66           ib::fatal()
67 #else
68           ib::fatal(ER_IB_ERR_ZLIB_BUF_ERROR)
69 #endif
70 
71               << "retval = Z_BUF_ERROR";
72           break;
73 
74         case Z_MEM_ERROR:
75 #ifdef UNIV_NO_ERR_MSGS
76           ib::fatal()
77 #else
78           ib::fatal(ER_IB_ERR_ZLIB_MEM_ERROR)
79 #endif
80               << "retval = Z_MEM_ERROR";
81           break;
82 
83         case Z_DATA_ERROR:
84 #ifdef UNIV_NO_ERR_MSGS
85           ib::fatal()
86 #else
87           ib::fatal(ER_IB_ERR_ZLIB_DATA_ERROR)
88 #endif
89               << "retval = Z_DATA_ERROR";
90           break;
91 
92         default:
93 #ifdef UNIV_NO_ERR_MSGS
94           ib::fatal()
95 #else
96           ib::fatal(ER_IB_ERR_ZLIB_UNKNOWN_ERROR)
97 #endif
98               << "retval = UNKNOWN_ERROR";
99           break;
100       }
101     }
102   }
103 
104   /** @return the uncompressed sdi */
get_data()105   byte *get_data() const { return (m_uncomp_sdi); }
106 
107  private:
108   /** Buffer to hold uncompressed SDI. memory allocated by caller */
109   byte *m_uncomp_sdi;
110   /** Length of Outbuf Buffer */
111   uint32_t m_uncomp_len;
112   /** Input Compressed SDI */
113   byte *m_comp_sdi;
114   /** Length of Compressed SDI */
115   uint32_t m_comp_len;
116 };
117 #endif
118