File: zlibut.cpp

package info (click to toggle)
recoll 1.43.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,512 kB
  • sloc: cpp: 104,170; python: 9,500; xml: 7,248; ansic: 6,447; sh: 1,212; perl: 130; makefile: 72
file content (172 lines) | stat: -rw-r--r-- 5,186 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 2017 J.F.Dockes
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "zlibut.h"

#include <zlib.h>

#include "log.h"

using namespace std;

static void *allocmem(
    void *cp,    /* The array to grow. may be NULL */
    size_t  sz,     /* Unit size in bytes */
    size_t  *np,    /* Pointer to current allocation number */
    size_t  min,    /* Number to allocate the first time */
    size_t  maxinc) /* Maximum increment */
{
    if (nullptr == cp) {
        cp = malloc(min * sz);
        *np = cp ? min : 0;
        return cp;
    }

    auto inc = (*np > maxinc) ?  maxinc : *np;
    if ((cp = realloc(cp, (*np + inc) * sz)) != nullptr) {
        *np += inc;
    }
    return cp;
}

class ZLibUtBuf::Internal {
public:
    Internal() {}
    ~Internal() {
        if (buf && dofree) {
            free(buf);
        }
    }
    bool grow(size_t n) {
        if (!initsz)
            initsz = n;
        buf = (char *)allocmem(buf, initsz, &alloc, 1, 20);
        return nullptr != buf;
    }
    size_t getAlloc() {
        return alloc * initsz;
    }
    char *buf{nullptr};
    size_t initsz{0}; // Set to first alloc size
    size_t alloc{0}; // Allocation count (allocmem()). Capa is alloc*inisz
    size_t datacnt{0}; // Data count
    bool dofree{true}; // Does buffer belong to me ?
    friend bool inflateToBuf(void* inp, size_t inlen, ZLibUtBuf& buf);
};

ZLibUtBuf::ZLibUtBuf()
{
    m = new Internal;
}
ZLibUtBuf::~ZLibUtBuf()
{
    delete m;
}

char *ZLibUtBuf::getBuf() const
{
    return m->buf;
}            
char *ZLibUtBuf::takeBuf()
{
    m->dofree = false;
    return m->buf;
}
size_t ZLibUtBuf::getCnt()
{
    return m->datacnt;
}

bool inflateToBuf(const void* inp, size_t inlen, ZLibUtBuf& buf)
{
    LOGDEB1("inflateToBuf: inlen " << inlen << "\n");

    z_stream d_stream; /* decompression stream */

    d_stream.zalloc = (alloc_func)0;
    d_stream.zfree = (free_func)0;
    d_stream.opaque = (voidpf)0;
    d_stream.next_in  = (Bytef*)inp;
    d_stream.avail_in = static_cast<unsigned int>(inlen);
    d_stream.next_out = nullptr;
    d_stream.avail_out = 0;

    int err;
    if ((err = inflateInit(&d_stream)) != Z_OK) {
        LOGERR("Inflate: inflateInit: err " << err << " msg "  <<
               d_stream.msg << "\n");
        return false;
    }

    for (;;) {
        LOGDEB2("InflateToDynBuf: avail_in " << d_stream.avail_in <<
                " total_in " << d_stream.total_in << " avail_out " <<
                d_stream.avail_out << " total_out " << d_stream.total_out <<
                "\n");
        if (d_stream.avail_out == 0) {
            if (!buf.m->grow(inlen)) {
                LOGERR("Inflate: out of memory, current alloc " <<
                       buf.m->getAlloc() << "\n");
                inflateEnd(&d_stream);
                return false;
            }
            d_stream.avail_out = static_cast<unsigned int>(buf.m->getAlloc() - d_stream.total_out);
            d_stream.next_out = (Bytef*)(buf.getBuf() + d_stream.total_out);
        }
        err = inflate(&d_stream, Z_NO_FLUSH);
        if (err == Z_STREAM_END) {
            break;
        }
        if (err != Z_OK) {
            LOGERR("Inflate: error " << err << " msg " <<
                   (d_stream.msg ? d_stream.msg : "") << endl);
            inflateEnd(&d_stream);
            return false;
        }
    }
    if ((err = inflateEnd(&d_stream)) != Z_OK) {
        LOGERR("Inflate: inflateEnd error " << err << " msg " <<
               (d_stream.msg ? d_stream.msg : "") << endl);
        return false;
    }
    buf.m->datacnt = d_stream.total_out;
    LOGDEB1("inflateToBuf: ok, output size " << buf.getCnt() << endl);
    return true;
}


bool deflateToBuf(const void* inp, size_t inlen, ZLibUtBuf& buf)
{
    uLongf len = compressBound(static_cast<uLong>(inlen));
    // This needs cleanup: because the buffer is reused inside
    // e.g. circache, we want a minimum size in case the 1st doc size,
    // which sets the grow increment is small. It would be better to
    // let the user set a min size hint.
    if (len < 500 *1024)
        len = 500 * 1024;

    while (buf.m->getAlloc() < len) {
        if (!buf.m->grow(len)) {
            LOGERR("deflateToBuf: can't get buffer for " << len << " bytes\n");
            return false;
        }
    }
    bool ret = compress((Bytef*)buf.getBuf(), &len, (Bytef*)inp,
                        static_cast<uLong>(inlen)) == Z_OK;
    buf.m->datacnt = len;
    return ret;
}