Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gzip_wrapper.h « src « Alohalytics « 3party - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 83cff7a44f502fc67a5625772040b9c543591fd6 (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
/*******************************************************************************
The MIT License (MIT)

Copyright (c) 2015 Alexander Zolotarev <me@alex.bio> from Minsk, Belarus

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*******************************************************************************/
#ifndef GZIP_WRAPPER_H
#define GZIP_WRAPPER_H

#include <cstring>  // std::memset
#include <string>
#include <vector>
#include <zlib.h>

#include "logger.h"

namespace alohalytics {

static constexpr size_t kGzipBufferSize = 32768;

struct GzipErrorException : public std::exception {
  std::string msg_;
  GzipErrorException(int err, const char * msg) {
    msg_ = std::string("ERROR ") + std::to_string(err) + " while gzipping with zlib. " + (msg ? msg : "");
  }
  virtual char const * what() const noexcept { return msg_.c_str(); }
};

// Throws GzipErrorException on any gzip processing error.
inline std::string Gzip(const std::string & data_to_compress) {
  z_stream z;
  std::memset(&z, 0, sizeof(z));
  int res = ::deflateInit2(&z, Z_BEST_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
  if (Z_OK == res) {
    z.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data_to_compress.data()));
    // TODO(AlexZ): Check situation when uInt is < than size of the data to compress.
    z.avail_in = static_cast<uInt>(data_to_compress.size());
    std::string compressed;
    std::vector<Bytef> buffer;
    buffer.resize(std::min(data_to_compress.size(), kGzipBufferSize));
    do {
      z.next_out = buffer.data();
      z.avail_out = static_cast<uInt>(buffer.size());
      res = ::deflate(&z, Z_FINISH);
      if (compressed.size() < z.total_out) {
        compressed.append(reinterpret_cast<const char *>(buffer.data()), z.total_out - compressed.size());
      }
    } while (Z_OK == res);
    ::deflateEnd(&z);
    if (Z_STREAM_END == res) {
      return compressed;
    }
  }
  throw GzipErrorException(res, z.msg);
}

struct GunzipErrorException : public std::exception {
  std::string msg_;
  GunzipErrorException(int err, const char * msg) {
    msg_ = std::string("ERROR ") + std::to_string(err) + " while gunzipping with zlib. " + (msg ? msg : "");
  }
  virtual char const * what() const noexcept { return msg_.c_str(); }
};

// Throws GunzipErrorException on any gunzip processing error.
inline std::string Gunzip(const std::string & data_to_decompress) {
  z_stream z;
  std::memset(&z, 0, sizeof(z));
  int res = ::inflateInit2(&z, 16 + MAX_WBITS);
  if (Z_OK == res) {
    z.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data_to_decompress.data()));
    z.avail_in = static_cast<uInt>(data_to_decompress.size());
    std::string decompressed;
    std::vector<Bytef> buffer;
    buffer.resize(std::min(data_to_decompress.size(), kGzipBufferSize));
    do {
      z.next_out = buffer.data();
      z.avail_out = static_cast<uInt>(buffer.size());
      res = ::inflate(&z, Z_NO_FLUSH);
      if (decompressed.size() < z.total_out) {
        decompressed.append(reinterpret_cast<char const *>(buffer.data()), z.total_out - decompressed.size());
      }
    } while (Z_OK == res);
    ::inflateEnd(&z);
    if (Z_STREAM_END == res) {
      return decompressed;
    }
  }
  throw GunzipErrorException(res, z.msg);
}

}  // namespace alohalytics
#endif  // GZIP_WRAPPER_H