Electroneum
epee::zlib_helper Namespace Reference

Functions

bool pack (std::string &target)
 
bool unpack (std::string &target)
 

Function Documentation

◆ pack()

bool epee::zlib_helper::pack ( std::string &  target)
inline

Definition at line 39 of file zlib_helper.h.

39  {
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  }
::std::string string
Definition: gtest-port.h:1097
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
Here is the caller graph for this function:

◆ unpack()

bool epee::zlib_helper::unpack ( std::string &  target)
inline

Definition at line 70 of file zlib_helper.h.

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  }
#define LOCAL_ASSERT(expr)
Definition: misc_log_ex.h:122
::std::string string
Definition: gtest-port.h:1097
#define LOG_ERROR(x)
Definition: misc_log_ex.h:98
Here is the caller graph for this function: