-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
pal_zlib.c
218 lines (174 loc) · 5.36 KB
/
pal_zlib.c
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include <assert.h>
#include <stdlib.h>
#include "pal_zlib.h"
#ifdef INTERNAL_ZLIB
#ifdef _WIN32
#define c_static_assert(e) static_assert((e),"")
#endif
#ifdef INTERNAL_ZLIB_INTEL
#include <external/zlib-intel/zlib.h>
#else
#include <external/zlib/zlib.h>
#endif
#else
#include "pal_utilities.h"
#include <zlib.h>
#endif
c_static_assert(PAL_Z_NOFLUSH == Z_NO_FLUSH);
c_static_assert(PAL_Z_FINISH == Z_FINISH);
c_static_assert(PAL_Z_OK == Z_OK);
c_static_assert(PAL_Z_STREAMEND == Z_STREAM_END);
c_static_assert(PAL_Z_STREAMERROR == Z_STREAM_ERROR);
c_static_assert(PAL_Z_DATAERROR == Z_DATA_ERROR);
c_static_assert(PAL_Z_MEMERROR == Z_MEM_ERROR);
c_static_assert(PAL_Z_BUFERROR == Z_BUF_ERROR);
c_static_assert(PAL_Z_VERSIONERROR == Z_VERSION_ERROR);
c_static_assert(PAL_Z_NOCOMPRESSION == Z_NO_COMPRESSION);
c_static_assert(PAL_Z_BESTSPEED == Z_BEST_SPEED);
c_static_assert(PAL_Z_DEFAULTCOMPRESSION == Z_DEFAULT_COMPRESSION);
c_static_assert(PAL_Z_DEFAULTSTRATEGY == Z_DEFAULT_STRATEGY);
c_static_assert(PAL_Z_DEFLATED == Z_DEFLATED);
/*
Initializes the PAL_ZStream by creating and setting its underlying z_stream.
*/
static int32_t Init(PAL_ZStream* stream)
{
z_stream* zStream = (z_stream*)calloc(1, sizeof(z_stream));
stream->internalState = zStream;
if (zStream != NULL)
{
return PAL_Z_OK;
}
else
{
return PAL_Z_MEMERROR;
}
}
/*
Frees any memory on the PAL_ZStream that was created by Init.
*/
static void End(PAL_ZStream* stream)
{
z_stream* zStream = (z_stream*)(stream->internalState);
assert(zStream != NULL);
if (zStream != NULL)
{
free(zStream);
stream->internalState = NULL;
}
}
/*
Transfers the output values from the underlying z_stream to the PAL_ZStream.
*/
static void TransferStateToPalZStream(z_stream* from, PAL_ZStream* to)
{
to->nextIn = from->next_in;
to->availIn = from->avail_in;
to->nextOut = from->next_out;
to->availOut = from->avail_out;
to->msg = from->msg;
}
/*
Transfers the input values from the PAL_ZStream to the underlying z_stream object.
*/
static void TransferStateFromPalZStream(PAL_ZStream* from, z_stream* to)
{
to->next_in = from->nextIn;
to->avail_in = from->availIn;
to->next_out = from->nextOut;
to->avail_out = from->availOut;
}
/*
Gets the current z_stream object for the specified PAL_ZStream.
This ensures any inputs are transferred from the PAL_ZStream to the underlying z_stream,
since the current values are always needed.
*/
static z_stream* GetCurrentZStream(PAL_ZStream* stream)
{
z_stream* zStream = (z_stream*)(stream->internalState);
assert(zStream != NULL);
TransferStateFromPalZStream(stream, zStream);
return zStream;
}
int32_t CompressionNative_DeflateInit2_(
PAL_ZStream* stream, int32_t level, int32_t method, int32_t windowBits, int32_t memLevel, int32_t strategy)
{
assert(stream != NULL);
int32_t result = Init(stream);
if (result == PAL_Z_OK)
{
z_stream* zStream = GetCurrentZStream(stream);
result = deflateInit2(zStream, level, method, windowBits, memLevel, strategy);
TransferStateToPalZStream(zStream, stream);
}
return result;
}
int32_t CompressionNative_Deflate(PAL_ZStream* stream, int32_t flush)
{
assert(stream != NULL);
z_stream* zStream = GetCurrentZStream(stream);
int32_t result = deflate(zStream, flush);
TransferStateToPalZStream(zStream, stream);
return result;
}
int32_t CompressionNative_DeflateReset(PAL_ZStream* stream)
{
assert(stream != NULL);
z_stream* zStream = GetCurrentZStream(stream);
int32_t result = deflateReset(zStream);
TransferStateToPalZStream(zStream, stream);
return result;
}
int32_t CompressionNative_DeflateEnd(PAL_ZStream* stream)
{
assert(stream != NULL);
z_stream* zStream = GetCurrentZStream(stream);
int32_t result = deflateEnd(zStream);
End(stream);
return result;
}
int32_t CompressionNative_InflateInit2_(PAL_ZStream* stream, int32_t windowBits)
{
assert(stream != NULL);
int32_t result = Init(stream);
if (result == PAL_Z_OK)
{
z_stream* zStream = GetCurrentZStream(stream);
result = inflateInit2(zStream, windowBits);
TransferStateToPalZStream(zStream, stream);
}
return result;
}
int32_t CompressionNative_Inflate(PAL_ZStream* stream, int32_t flush)
{
assert(stream != NULL);
z_stream* zStream = GetCurrentZStream(stream);
int32_t result = inflate(zStream, flush);
TransferStateToPalZStream(zStream, stream);
return result;
}
int32_t CompressionNative_InflateReset(PAL_ZStream* stream)
{
assert(stream != NULL);
z_stream* zStream = GetCurrentZStream(stream);
int32_t result = inflateReset(zStream);
TransferStateToPalZStream(zStream, stream);
return result;
}
int32_t CompressionNative_InflateEnd(PAL_ZStream* stream)
{
assert(stream != NULL);
z_stream* zStream = GetCurrentZStream(stream);
int32_t result = inflateEnd(zStream);
End(stream);
return result;
}
uint32_t CompressionNative_Crc32(uint32_t crc, uint8_t* buffer, int32_t len)
{
assert(buffer != NULL);
unsigned long result = crc32(crc, buffer, len);
assert(result <= UINT32_MAX);
return (uint32_t)result;
}