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

zlib.hpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f710246293ddeb709ef6e5b8b9daefd8a6bb3b1 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once

#include "base/assert.hpp"
#include "base/macros.hpp"

#include "std/algorithm.hpp"
#include "std/string.hpp"

#include "zlib.h"

namespace coding
{
// Following classes are wrappers around ZLib routines.
//
// *NOTE* All Inflate() and Deflate() methods may return false in case
// of errors. In this case the output sequence may be already
// partially formed, so the user needs to implement their own
// roll-back strategy.
class ZLib
{
public:
  class Inflate
  {
  public:
    enum class Format
    {
      ZLib,
      GZip,
      Both
    };

    explicit Inflate(Format format) noexcept : m_format(format) {}

    template <typename OutIt>
    bool operator()(void const * data, size_t size, OutIt out) const
    {
      if (data == nullptr)
        return false;
      InflateProcessor processor(m_format, data, size);
      return Process(processor, out);
    }

    template <typename OutIt>
    bool operator()(string const & s, OutIt out) const
    {
      return (*this)(s.c_str(), s.size(), out);
    }

  private:
    Format const m_format;
  };

  class Deflate
  {
  public:
    enum class Format
    {
      ZLib,
      GZip
    };

    enum class Level
    {
      NoCompression,
      BestSpeed,
      BestCompression,
      DefaultCompression
    };

    Deflate(Format format, Level level) noexcept : m_format(format), m_level(level) {}

    template <typename OutIt>
    bool operator()(void const * data, size_t size, OutIt out) const
    {
      if (data == nullptr)
        return false;
      DeflateProcessor processor(m_format, m_level, data, size);
      return Process(processor, out);
    }

    template <typename OutIt>
    bool operator()(string const & s, OutIt out) const
    {
      return (*this)(s.c_str(), s.size(), out);
    }

  private:
    Format const m_format;
    Level const m_level;
  };

private:
  class Processor
  {
  public:
    static size_t constexpr kBufferSize = 1024;

    Processor(void const * data, size_t size) noexcept;
    virtual ~Processor() noexcept = default;

    inline bool IsInit() const noexcept { return m_init; }
    bool ConsumedAll() const;
    bool BufferIsFull() const;

    template <typename OutIt>
    void MoveOut(OutIt out)
    {
      ASSERT(IsInit(), ());
      copy(m_buffer, m_buffer + kBufferSize - m_stream.avail_out, out);
      m_stream.next_out = m_buffer;
      m_stream.avail_out = kBufferSize;
    }

  protected:
    z_stream m_stream;
    bool m_init;
    unsigned char m_buffer[kBufferSize];

    DISALLOW_COPY_AND_MOVE(Processor);
  };

  class DeflateProcessor final : public Processor
  {
  public:
    DeflateProcessor(Deflate::Format format, Deflate::Level level, void const * data,
                     size_t size) noexcept;
    virtual ~DeflateProcessor() noexcept override;

    int Process(int flush);

    DISALLOW_COPY_AND_MOVE(DeflateProcessor);
  };

  class InflateProcessor final : public Processor
  {
  public:
    InflateProcessor(Inflate::Format format, void const * data, size_t size) noexcept;
    virtual ~InflateProcessor() noexcept override;

    int Process(int flush);

    DISALLOW_COPY_AND_MOVE(InflateProcessor);
  };

  template <typename Processor, typename OutIt>
  static bool Process(Processor & processor, OutIt out)
  {
    if (!processor.IsInit())
      return false;

    while (true)
    {
      int const flush = processor.ConsumedAll() ? Z_FINISH : Z_NO_FLUSH;
      int ret = Z_OK;

      while (true)
      {
        ret = processor.Process(flush);
        if (ret != Z_OK && ret != Z_STREAM_END)
          return false;

        if (!processor.BufferIsFull())
          break;

        processor.MoveOut(out);
      }

      if (flush == Z_FINISH && ret == Z_STREAM_END)
        break;
    }

    processor.MoveOut(out);
    return true;
  }
};
}  // namespace coding