Electroneum
zlib_helper.h
Go to the documentation of this file.
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 
28 #pragma once
29 extern "C" {
30 #include "zlib/zlib.h"
31 }
32 #pragma comment(lib, "zlibstat.lib")
33 
34 namespace epee
35 {
36 namespace zlib_helper
37 {
38  inline
39  bool pack(std::string& target){
40  std::string result_packed_buff;
41 
42  z_stream zstream = {0};
43  int ret = deflateInit(&zstream, Z_DEFAULT_COMPRESSION);
44  if(target.size())
45  {
46 
47 
48  result_packed_buff.resize(target.size()*2, 'X');
49 
50  zstream.next_in = (Bytef*)target.data();
51  zstream.avail_in = (uInt)target.size();
52  zstream.next_out = (Bytef*)result_packed_buff.data();
53  zstream.avail_out = (uInt)result_packed_buff.size();
54 
55  ret = deflate(&zstream, Z_FINISH);
56  CHECK_AND_ASSERT_MES(ret>=0, false, "Failed to deflate. err = " << ret);
57 
58  if(result_packed_buff.size() != zstream.avail_out)
59  result_packed_buff.resize(result_packed_buff.size()-zstream.avail_out);
60 
61 
62  result_packed_buff.erase(0, 2);
63  target.swap(result_packed_buff);
64  }
65 
66  deflateEnd(& zstream );
67  return true;
68  }
69 
70  inline bool unpack(std::string& target)
71  {
72  z_stream zstream = {0};
73  int ret = inflateInit(&zstream);//
74 
75  std::string decode_summary_buff;
76  size_t ungzip_buff_size = target.size() * 0x30;
77  std::string current_decode_buff(ungzip_buff_size, 'X');
78 
79  while(target.size())
80  {
81 
82 
83  zstream.next_out = (Bytef*)current_decode_buff.data();
84  zstream.avail_out = (uInt)ungzip_buff_size;
85 
86  int flag = Z_SYNC_FLUSH;
87 
88  static char dummy_head[2] =
89  {
90  0x8 + 0x7 * 0x10,
91  (((0x8 + 0x7 * 0x10) * 0x100 + 30) / 31 * 31) & 0xFF,
92  };
93  zstream.next_in = (Bytef*) dummy_head;
94  zstream.avail_in = sizeof(dummy_head);
95  ret = inflate(&zstream, Z_NO_FLUSH);
96  if (ret != Z_OK)
97  {
98  LOCAL_ASSERT(0);
99  return false;
100  }
101 
102  zstream.next_in = (Bytef*)target.data();
103  zstream.avail_in = (uInt)target.size();
104 
105  ret = inflate(&zstream, Z_SYNC_FLUSH);
106  if (ret != Z_OK && ret != Z_STREAM_END)
107  {
108  LOCAL_ASSERT(0);
109  return false;
110  }
111 
112 
113  target.erase(0, target.size()-zstream.avail_in);
114 
115 
116  if(ungzip_buff_size == zstream.avail_out)
117  {
118  LOG_ERROR("Can't unpack buffer");
119  return false;
120  }
121 
122 
123  current_decode_buff.resize(ungzip_buff_size - zstream.avail_out);
124  if(decode_summary_buff.size())
125  decode_summary_buff += current_decode_buff;
126  else
127  current_decode_buff.swap(decode_summary_buff);
128 
129  current_decode_buff.resize(ungzip_buff_size);
130  }
131 
132  inflateEnd(&zstream );
133 
134  decode_summary_buff.swap(target);
135  return 1;
136  }
137 
138 };
139 }//namespace epee
#define LOCAL_ASSERT(expr)
Definition: misc_log_ex.h:122
::std::string string
Definition: gtest-port.h:1097
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
bool pack(std::string &target)
Definition: zlib_helper.h:39
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
bool unpack(std::string &target)
Definition: zlib_helper.h:70