aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/beam/erl_zlib.c
blob: 8e33144f96deb51b6bac238f147f97dff7e9aca1 (plain) (blame)
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
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2009-2013. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */

/* A sparse wrapper around zlib with erts memory allocation.
 *
 * erl_zlib_compress2 and erl_zlib_uncompress are erts-adapted versions
 * of the original compress2 and uncompress from zlib-1.2.3. 
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "erl_zlib.h"

#include "sys.h"
#include "erl_alloc.h"

voidpf erl_zlib_zalloc_callback (voidpf opaque, unsigned items, unsigned size)
{
    (void) opaque; /* make compiler happy */
    return erts_alloc_fnf(ERTS_ALC_T_ZLIB, items * size);
}

void erl_zlib_zfree_callback (voidpf opaque, voidpf ptr)
{
    (void) opaque; /* make compiler happy */
    erts_free(ERTS_ALC_T_ZLIB, ptr);
}

/*
 * Initialize a z_stream with a source, to later *chunk* data into a destination 
 * Returns Z_OK or Error.
 */
int ZEXPORT erl_zlib_deflate_start(z_stream *streamp, const Bytef* source, 
				   uLong sourceLen, int level) 
{
    streamp->next_in = (Bytef*)source;
    streamp->avail_in = (uInt)sourceLen;
    streamp->total_out = streamp->avail_out = 0;
    streamp->next_out = NULL;
    erl_zlib_alloc_init(streamp);
    return deflateInit(streamp, level);
}
/* 
 * Deflate a chunk, The destination length is the limit.
 * Returns Z_OK if more to process, Z_STREAM_END if we are done.
 */
int ZEXPORT erl_zlib_deflate_chunk(z_stream *streamp, Bytef* dest, uLongf* destLen) 
{
    int err;
    uLongf last_tot = streamp->total_out;

    streamp->next_out = dest;
    streamp->avail_out = (uInt)*destLen;
    
    if ((uLong)streamp->avail_out != *destLen) return Z_BUF_ERROR;
    
    err = deflate(streamp, Z_FINISH);
    *destLen = streamp->total_out - last_tot;
    return err; 
}


/*
 * When we are done, free up the deflate structure
 * Retyurns Z_OK or Error
 */
int ZEXPORT erl_zlib_deflate_finish(z_stream *streamp)
{
    return deflateEnd(streamp);
}

int ZEXPORT erl_zlib_inflate_start(z_stream *streamp, const Bytef* source,
                                   uLong sourceLen)
{
    streamp->next_in = (Bytef*)source;
    streamp->avail_in = (uInt)sourceLen;
    streamp->total_out = streamp->avail_out = 0;
    streamp->next_out = NULL;
    erl_zlib_alloc_init(streamp);
    return inflateInit(streamp);
}
/*
 * Inflate a chunk, The destination length is the limit.
 * Returns Z_OK if more to process, Z_STREAM_END if we are done.
 */
int ZEXPORT erl_zlib_inflate_chunk(z_stream *streamp, Bytef* dest, uLongf* destLen)
{
    int err;
    uLongf last_tot = streamp->total_out;

    streamp->next_out = dest;
    streamp->avail_out = (uInt)*destLen;

    if ((uLong)streamp->avail_out != *destLen) return Z_BUF_ERROR;

    err = inflate(streamp, Z_NO_FLUSH);
    ASSERT(err != Z_STREAM_ERROR);
    *destLen = streamp->total_out - last_tot;
    return err;
}

/*
 * When we are done, free up the inflate structure
 * Retyurns Z_OK or Error
 */
int ZEXPORT erl_zlib_inflate_finish(z_stream *streamp)
{
    return inflateEnd(streamp);
}


int ZEXPORT erl_zlib_compress2 (Bytef* dest, uLongf* destLen,
				const Bytef* source, uLong sourceLen,
				int level)
{
    z_stream stream;
    int err;

    stream.next_in = (Bytef*)source;
    stream.avail_in = (uInt)sourceLen;
#ifdef MAXSEG_64K
    /* Check for source > 64K on 16-bit machine: */
    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
#endif
    stream.next_out = dest;
    stream.avail_out = (uInt)*destLen;
    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;

    erl_zlib_alloc_init(&stream);

    err = deflateInit(&stream, level);
    if (err != Z_OK) return err;

    err = deflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END) {
        deflateEnd(&stream);
        return err == Z_OK ? Z_BUF_ERROR : err;
    }
    *destLen = stream.total_out;

    err = deflateEnd(&stream);
    return err;
}

int ZEXPORT erl_zlib_uncompress (Bytef* dest, uLongf* destLen,
				 const Bytef* source, uLong sourceLen)
{
    z_stream stream;
    int err;

    stream.next_in = (Bytef*)source;
    stream.avail_in = (uInt)sourceLen;
    /* Check for source > 64K on 16-bit machine: */
    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;

    stream.next_out = dest;
    stream.avail_out = (uInt)*destLen;
    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;

    erl_zlib_alloc_init(&stream);

    err = inflateInit(&stream);
    if (err != Z_OK) return err;

    err = inflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END) {
        inflateEnd(&stream);
        if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
            return Z_DATA_ERROR;
        return err;
    }
    *destLen = stream.total_out;

    err = inflateEnd(&stream);
    return err;
}