OR-Tools  8.2
gzipstring.h
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_BASE_GZIPSTRING_H_
15 #define OR_TOOLS_BASE_GZIPSTRING_H_
16 
17 #include <string>
18 
19 #include "ortools/base/logging.h"
20 #include "zlib.h"
21 
22 bool GunzipString(const std::string& str, std::string* out) {
23  z_stream zs;
24  zs.zalloc = Z_NULL;
25  zs.zfree = Z_NULL;
26  zs.opaque = Z_NULL;
27  zs.next_in = Z_NULL;
28  zs.avail_in = 0;
29  zs.next_out = Z_NULL;
30  if (inflateInit2(&zs, /*window_bits=*/15 + 32) != Z_OK) {
31  return false;
32  }
33 
34  zs.next_in = (Bytef*)str.data();
35  zs.avail_in = str.size();
36 
37  int status;
38  char buffer[32768];
39 
40  // Decompress string by block.
41  do {
42  zs.next_out = reinterpret_cast<Bytef*>(buffer);
43  zs.avail_out = sizeof(buffer);
44 
45  status = inflate(&zs, 0);
46 
47  if (out->size() < zs.total_out) {
48  out->append(buffer, zs.total_out - out->size());
49  }
50  } while (status == Z_OK);
51 
52  inflateEnd(&zs);
53 
54  if (status != Z_STREAM_END) { // an error occurred that was not EOF
55  VLOG(1) << "Exception during zlib decompression: (" << status << ") "
56  << zs.msg;
57  return false;
58  }
59 
60  return true;
61 }
62 
63 void GzipString(absl::string_view uncompressed, std::string* compressed) {
64  z_stream zs;
65  zs.zalloc = Z_NULL;
66  zs.zfree = Z_NULL;
67  zs.opaque = Z_NULL;
68  zs.next_in = Z_NULL;
69  zs.avail_in = 0;
70  zs.next_out = Z_NULL;
71 
72  if (deflateInit(&zs, Z_BEST_COMPRESSION) != Z_OK) {
73  VLOG(1) << "Cannot initialize zlib compression.";
74  return;
75  }
76 
77  zs.next_in = (Bytef*)uncompressed.data();
78  zs.avail_in = uncompressed.size(); // set the z_stream's input
79 
80  int status;
81  char buffer[32768];
82 
83  // compress block by block.
84  do {
85  zs.next_out = reinterpret_cast<Bytef*>(buffer);
86  zs.avail_out = sizeof(buffer);
87 
88  status = deflate(&zs, Z_FINISH);
89 
90  if (compressed->size() < zs.total_out) {
91  compressed->append(buffer, zs.total_out - compressed->size());
92  }
93  } while (status == Z_OK);
94 
95  deflateEnd(&zs);
96 
97  if (status != Z_STREAM_END) { // an error occurred that was not EOF
98  VLOG(1) << "Exception during zlib compression: (" << status << ") "
99  << zs.msg;
100  }
101 }
102 
103 #endif // OR_TOOLS_BASE_GZIPSTRING_H_
#define VLOG(verboselevel)
Definition: base/logging.h:978
std::string compressed
void GzipString(absl::string_view uncompressed, std::string *compressed)
Definition: gzipstring.h:63
bool GunzipString(const std::string &str, std::string *out)
Definition: gzipstring.h:22