Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* CFS zlib-wrapped DEFLATE binding with explicit stream preparation. */
#include "postgres.h"
 
#include <limits.h>
 
#ifdef HAVE_LIBZ
#include <zlib.h>
#endif
 
#include "storage/cfs/cfs_codec.h"
 
#ifdef HAVE_LIBZ
/* Backend-local, non-reentrant streams. No allocator activity during I/O. */
static z_stream zlib_encoder;
static z_stream zlib_decoder;
static bool zlib_prepared;
static bool zlib_may_allocate;
 
static voidpf
cfs_zlib_alloc(voidpf opaque, uInt count, uInt size)
{
    (void) opaque;
    if (!zlib_may_allocate || size == 0 ||
        count > SIZE_MAX / size)
        return Z_NULL;
    return malloc((size_t) count * size);
}
 
static void
cfs_zlib_free(voidpf opaque, voidpf ptr)
{
    (void) opaque;
    free(ptr);
}
 
bool
cfs_codec_zlib_prepare(void)
{
    Bytef *raw = NULL;
    Bytef *restored = NULL;
    Bytef *compressed = NULL;
    Size capacity;
    const uInt warm_len = 32768;
    int rc;
 
    if (zlib_prepared)
        return true;
 
    memset(&zlib_encoder, 0, sizeof(zlib_encoder));
    memset(&zlib_decoder, 0, sizeof(zlib_decoder));
    zlib_encoder.zalloc = cfs_zlib_alloc;
    zlib_encoder.zfree = cfs_zlib_free;
    zlib_decoder.zalloc = cfs_zlib_alloc;
    zlib_decoder.zfree = cfs_zlib_free;
 
    zlib_may_allocate = true;
    if (deflateInit(&zlib_encoder, Z_DEFAULT_COMPRESSION) != Z_OK)
        goto failed;
    if (inflateInit(&zlib_decoder) != Z_OK)
        goto failed;
 
    /* Exercise full-window inflate while allocations are still permitted. */
    capacity = (Size) compressBound(warm_len);
    raw = calloc(warm_len, 1);
    restored = malloc(warm_len);
    compressed = malloc(capacity);
    if (raw == NULL || restored == NULL || compressed == NULL)
        goto failed;
 
    zlib_encoder.next_in = raw;
    zlib_encoder.avail_in = warm_len;
    zlib_encoder.next_out = compressed;
    zlib_encoder.avail_out = (uInt) capacity;
    rc = deflate(&zlib_encoder, Z_FINISH);
    if (rc != Z_STREAM_END)
        goto failed;
 
    zlib_decoder.next_in = compressed;
    zlib_decoder.avail_in = (uInt) zlib_encoder.total_out;
    zlib_decoder.next_out = restored;
    zlib_decoder.avail_out = warm_len;
    rc = inflate(&zlib_decoder, Z_FINISH);
    if (rc != Z_STREAM_END || zlib_decoder.total_out != warm_len)
        goto failed;
 
    if (deflateReset(&zlib_encoder) != Z_OK ||
        inflateReset(&zlib_decoder) != Z_OK)
        goto failed;
 
    zlib_may_allocate = false;
    zlib_prepared = true;
    free(raw);
    free(restored);
    free(compressed);
    return true;
 
failed:
    if (zlib_encoder.state != NULL)
        deflateEnd(&zlib_encoder);
    if (zlib_decoder.state != NULL)
        inflateEnd(&zlib_decoder);
    zlib_may_allocate = false;
    free(raw);
    free(restored);
    free(compressed);
    memset(&zlib_encoder, 0, sizeof(zlib_encoder));
    memset(&zlib_decoder, 0, sizeof(zlib_decoder));
    return false;
}
#endif
 
bool
cfs_codec_zlib_available(void)
{
#ifdef HAVE_LIBZ
    return true;
#else
    return false;
#endif
}
 
Size
cfs_codec_zlib_bound(Size srclen)
{
#ifdef HAVE_LIBZ
    if (srclen > (Size) BLCKSZ)
        return 0;
    return (Size) compressBound((uLong) srclen);
#else
    (void) srclen;
    return 0;
#endif
}
 
CfsCodecResult
cfs_codec_zlib_compress(const char *src, Size srclen,
                        char *dst, Size dstlen, int level,
                        Size *result_len)
{
#ifdef HAVE_LIBZ
    int rc;
 
    if (src == NULL || dst == NULL || result_len == NULL ||
        srclen > (Size) BLCKSZ ||
        dstlen > (Size) UINT_MAX || level > 9)
        return CFS_CODEC_ERROR;
    if (!zlib_prepared)
        return CFS_CODEC_ERROR; /* cfs_codec_prepare() required */
    if (srclen == 0 || dstlen == 0)
        return CFS_CODEC_OVERFLOW;
 
    if (level <= 0)
        level = Z_DEFAULT_COMPRESSION;
    rc = deflateReset(&zlib_encoder);
    if (rc != Z_OK)
        return CFS_CODEC_ERROR;
 
    zlib_encoder.next_in = Z_NULL;
    zlib_encoder.avail_in = 0;
    zlib_encoder.next_out = (Bytef *) dst;
    zlib_encoder.avail_out = (uInt) dstlen;
    rc = deflateParams(&zlib_encoder, level, Z_DEFAULT_STRATEGY);
    if (rc == Z_BUF_ERROR)
        return CFS_CODEC_OVERFLOW;
    if (rc != Z_OK)
        return CFS_CODEC_ERROR;
 
    zlib_encoder.next_in = (Bytef *) src;
    zlib_encoder.avail_in = (uInt) srclen;
    rc = deflate(&zlib_encoder, Z_FINISH);
    if (rc != Z_STREAM_END)
        return rc == Z_OK || rc == Z_BUF_ERROR ?
            CFS_CODEC_OVERFLOW : CFS_CODEC_ERROR;
 
    if (zlib_encoder.total_in != srclen ||
        zlib_encoder.total_out >= srclen)
        return CFS_CODEC_OVERFLOW;
    *result_len = (Size) zlib_encoder.total_out;
    return CFS_CODEC_OK;
#else
    (void) src; (void) srclen; (void) dst; (void) dstlen;
    (void) level; (void) result_len;
    return CFS_CODEC_UNAVAIL;
#endif
}
 
CfsCodecResult
cfs_codec_zlib_decompress(const char *src, Size srclen,
                          char *dst, Size dstlen)
{
#ifdef HAVE_LIBZ
    int rc;
 
    if (src == NULL || dst == NULL || srclen == 0 ||
        srclen > (Size) BLCKSZ || dstlen > (Size) BLCKSZ ||
        !zlib_prepared)
        return CFS_CODEC_ERROR;
 
    if (inflateReset(&zlib_decoder) != Z_OK)
        return CFS_CODEC_ERROR;
    zlib_decoder.next_in = (Bytef *) src;
    zlib_decoder.avail_in = (uInt) srclen;
    zlib_decoder.next_out = (Bytef *) dst;
    zlib_decoder.avail_out = (uInt) dstlen;
    rc = inflate(&zlib_decoder, Z_FINISH);
    return rc == Z_STREAM_END &&
        zlib_decoder.total_in == srclen &&
        zlib_decoder.total_out == dstlen ?
        CFS_CODEC_OK : CFS_CODEC_ERROR;
#else
    (void) src; (void) srclen; (void) dst; (void) dstlen;
    return CFS_CODEC_UNAVAIL;
#endif
}
 
/* Release the backend-local prepared streams at worker/backend exit. */
void
cfs_codec_zlib_release(void)
{
#ifdef HAVE_LIBZ
    if (zlib_encoder.state != NULL)
    deflateEnd(&zlib_encoder);
    if (zlib_decoder.state != NULL)
    inflateEnd(&zlib_decoder);
    memset(&zlib_encoder, 0, sizeof(zlib_encoder));
    memset(&zlib_decoder, 0, sizeof(zlib_decoder));
    zlib_prepared = false;
    zlib_may_allocate = false;
#endif
}