Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 6ff3f87

Browse files
kasper3stephentoub
kasper3
authored andcommitted
Convert S.IO.Compression.Native to C (#30179)
* Convert S.IO.Compression.Native to C * Add DLLEXPORT
1 parent c1c7485 commit 6ff3f87

File tree

3 files changed

+63
-62
lines changed

3 files changed

+63
-62
lines changed

src/Native/Unix/System.IO.Compression.Native/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project(System.IO.Compression.Native)
33
find_package(ZLIB REQUIRED)
44

55
set(NATIVECOMPRESSION_SOURCES
6-
pal_zlib.cpp
6+
pal_zlib.c
77
)
88

99
#Include Brotli include files

src/Native/Unix/System.IO.Compression.Native/pal_zlib.cpp renamed to src/Native/Unix/System.IO.Compression.Native/pal_zlib.c

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,37 @@
66
#include "pal_utilities.h"
77

88
#include <assert.h>
9-
#include <memory>
9+
#include <stdlib.h>
1010
#include <zlib.h>
1111

12-
static_assert(PAL_Z_NOFLUSH == Z_NO_FLUSH, "");
13-
static_assert(PAL_Z_FINISH == Z_FINISH, "");
12+
c_static_assert(PAL_Z_NOFLUSH == Z_NO_FLUSH);
13+
c_static_assert(PAL_Z_FINISH == Z_FINISH);
1414

15-
static_assert(PAL_Z_OK == Z_OK, "");
16-
static_assert(PAL_Z_STREAMEND == Z_STREAM_END, "");
17-
static_assert(PAL_Z_STREAMERROR == Z_STREAM_ERROR, "");
18-
static_assert(PAL_Z_DATAERROR == Z_DATA_ERROR, "");
19-
static_assert(PAL_Z_MEMERROR == Z_MEM_ERROR, "");
20-
static_assert(PAL_Z_BUFERROR == Z_BUF_ERROR, "");
21-
static_assert(PAL_Z_VERSIONERROR == Z_VERSION_ERROR, "");
15+
c_static_assert(PAL_Z_OK == Z_OK);
16+
c_static_assert(PAL_Z_STREAMEND == Z_STREAM_END);
17+
c_static_assert(PAL_Z_STREAMERROR == Z_STREAM_ERROR);
18+
c_static_assert(PAL_Z_DATAERROR == Z_DATA_ERROR);
19+
c_static_assert(PAL_Z_MEMERROR == Z_MEM_ERROR);
20+
c_static_assert(PAL_Z_BUFERROR == Z_BUF_ERROR);
21+
c_static_assert(PAL_Z_VERSIONERROR == Z_VERSION_ERROR);
2222

23-
static_assert(PAL_Z_NOCOMPRESSION == Z_NO_COMPRESSION, "");
24-
static_assert(PAL_Z_BESTSPEED == Z_BEST_SPEED, "");
25-
static_assert(PAL_Z_DEFAULTCOMPRESSION == Z_DEFAULT_COMPRESSION, "");
23+
c_static_assert(PAL_Z_NOCOMPRESSION == Z_NO_COMPRESSION);
24+
c_static_assert(PAL_Z_BESTSPEED == Z_BEST_SPEED);
25+
c_static_assert(PAL_Z_DEFAULTCOMPRESSION == Z_DEFAULT_COMPRESSION);
2626

27-
static_assert(PAL_Z_DEFAULTSTRATEGY == Z_DEFAULT_STRATEGY, "");
27+
c_static_assert(PAL_Z_DEFAULTSTRATEGY == Z_DEFAULT_STRATEGY);
2828

29-
static_assert(PAL_Z_DEFLATED == Z_DEFLATED, "");
29+
c_static_assert(PAL_Z_DEFLATED == Z_DEFLATED);
3030

3131
/*
3232
Initializes the PAL_ZStream by creating and setting its underlying z_stream.
3333
*/
3434
static int32_t Init(PAL_ZStream* stream)
3535
{
36-
z_stream* zStream = new (std::nothrow) z_stream;
36+
z_stream* zStream = (z_stream*)malloc(sizeof(z_stream));
3737
stream->internalState = zStream;
3838

39-
if (zStream != nullptr)
39+
if (zStream != NULL)
4040
{
4141
zStream->zalloc = Z_NULL;
4242
zStream->zfree = Z_NULL;
@@ -54,19 +54,19 @@ Frees any memory on the PAL_ZStream that was created by Init.
5454
*/
5555
static void End(PAL_ZStream* stream)
5656
{
57-
z_stream* zStream = reinterpret_cast<z_stream*>(stream->internalState);
58-
assert(zStream != nullptr);
59-
if (zStream != nullptr)
57+
z_stream* zStream = (z_stream*)(stream->internalState);
58+
assert(zStream != NULL);
59+
if (zStream != NULL)
6060
{
61-
delete zStream;
62-
stream->internalState = nullptr;
61+
free(zStream);
62+
stream->internalState = NULL;
6363
}
6464
}
6565

6666
/*
6767
Transfers the output values from the underlying z_stream to the PAL_ZStream.
6868
*/
69-
static void TransferState(z_stream* from, PAL_ZStream* to)
69+
static void TransferStateToPalZStream(z_stream* from, PAL_ZStream* to)
7070
{
7171
to->nextIn = from->next_in;
7272
to->availIn = from->avail_in;
@@ -80,7 +80,7 @@ static void TransferState(z_stream* from, PAL_ZStream* to)
8080
/*
8181
Transfers the input values from the PAL_ZStream to the underlying z_stream object.
8282
*/
83-
static void TransferState(PAL_ZStream* from, z_stream* to)
83+
static void TransferStateFromPalZStream(PAL_ZStream* from, z_stream* to)
8484
{
8585
to->next_in = from->nextIn;
8686
to->avail_in = from->availIn;
@@ -97,43 +97,43 @@ since the current values are always needed.
9797
*/
9898
static z_stream* GetCurrentZStream(PAL_ZStream* stream)
9999
{
100-
z_stream* zStream = reinterpret_cast<z_stream*>(stream->internalState);
101-
assert(zStream != nullptr);
100+
z_stream* zStream = (z_stream*)(stream->internalState);
101+
assert(zStream != NULL);
102102

103-
TransferState(stream, zStream);
103+
TransferStateFromPalZStream(stream, zStream);
104104
return zStream;
105105
}
106106

107-
extern "C" int32_t CompressionNative_DeflateInit2_(
107+
int32_t CompressionNative_DeflateInit2_(
108108
PAL_ZStream* stream, int32_t level, int32_t method, int32_t windowBits, int32_t memLevel, int32_t strategy)
109109
{
110-
assert(stream != nullptr);
110+
assert(stream != NULL);
111111

112112
int32_t result = Init(stream);
113113
if (result == PAL_Z_OK)
114114
{
115115
z_stream* zStream = GetCurrentZStream(stream);
116116
result = deflateInit2(zStream, level, method, windowBits, memLevel, strategy);
117-
TransferState(zStream, stream);
117+
TransferStateToPalZStream(zStream, stream);
118118
}
119119

120120
return result;
121121
}
122122

123-
extern "C" int32_t CompressionNative_Deflate(PAL_ZStream* stream, int32_t flush)
123+
int32_t CompressionNative_Deflate(PAL_ZStream* stream, int32_t flush)
124124
{
125-
assert(stream != nullptr);
125+
assert(stream != NULL);
126126

127127
z_stream* zStream = GetCurrentZStream(stream);
128128
int32_t result = deflate(zStream, flush);
129-
TransferState(zStream, stream);
129+
TransferStateToPalZStream(zStream, stream);
130130

131131
return result;
132132
}
133133

134-
extern "C" int32_t CompressionNative_DeflateEnd(PAL_ZStream* stream)
134+
int32_t CompressionNative_DeflateEnd(PAL_ZStream* stream)
135135
{
136-
assert(stream != nullptr);
136+
assert(stream != NULL);
137137

138138
z_stream* zStream = GetCurrentZStream(stream);
139139
int32_t result = deflateEnd(zStream);
@@ -142,35 +142,35 @@ extern "C" int32_t CompressionNative_DeflateEnd(PAL_ZStream* stream)
142142
return result;
143143
}
144144

145-
extern "C" int32_t CompressionNative_InflateInit2_(PAL_ZStream* stream, int32_t windowBits)
145+
int32_t CompressionNative_InflateInit2_(PAL_ZStream* stream, int32_t windowBits)
146146
{
147-
assert(stream != nullptr);
147+
assert(stream != NULL);
148148

149149
int32_t result = Init(stream);
150150
if (result == PAL_Z_OK)
151151
{
152152
z_stream* zStream = GetCurrentZStream(stream);
153153
result = inflateInit2(zStream, windowBits);
154-
TransferState(zStream, stream);
154+
TransferStateToPalZStream(zStream, stream);
155155
}
156156

157157
return result;
158158
}
159159

160-
extern "C" int32_t CompressionNative_Inflate(PAL_ZStream* stream, int32_t flush)
160+
int32_t CompressionNative_Inflate(PAL_ZStream* stream, int32_t flush)
161161
{
162-
assert(stream != nullptr);
162+
assert(stream != NULL);
163163

164164
z_stream* zStream = GetCurrentZStream(stream);
165165
int32_t result = inflate(zStream, flush);
166-
TransferState(zStream, stream);
166+
TransferStateToPalZStream(zStream, stream);
167167

168168
return result;
169169
}
170170

171-
extern "C" int32_t CompressionNative_InflateEnd(PAL_ZStream* stream)
171+
int32_t CompressionNative_InflateEnd(PAL_ZStream* stream)
172172
{
173-
assert(stream != nullptr);
173+
assert(stream != NULL);
174174

175175
z_stream* zStream = GetCurrentZStream(stream);
176176
int32_t result = inflateEnd(zStream);
@@ -179,11 +179,11 @@ extern "C" int32_t CompressionNative_InflateEnd(PAL_ZStream* stream)
179179
return result;
180180
}
181181

182-
extern "C" uint32_t CompressionNative_Crc32(uint32_t crc, uint8_t* buffer, int32_t len)
182+
uint32_t CompressionNative_Crc32(uint32_t crc, uint8_t* buffer, int32_t len)
183183
{
184-
assert(buffer != nullptr);
184+
assert(buffer != NULL);
185185

186-
unsigned long result = crc32(crc, buffer, UnsignedCast(len));
186+
unsigned long result = crc32(crc, buffer, len);
187187
assert(result <= UINT32_MAX);
188-
return static_cast<uint32_t>(result);
188+
return (uint32_t)result;
189189
}

src/Native/Unix/System.IO.Compression.Native/pal_zlib.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
// See the LICENSE file in the project root for more information.
44

55
#include "pal_types.h"
6+
#include "pal_compiler.h"
67

78
/*
89
A structure that holds the input and output values for the zlib functions.
910
*/
10-
struct PAL_ZStream
11+
typedef struct PAL_ZStream
1112
{
1213
uint8_t* nextIn; // next input byte
1314
uint8_t* nextOut; // next output byte should be put there
@@ -19,12 +20,12 @@ struct PAL_ZStream
1920

2021
uint32_t availIn; // number of bytes available at nextIn
2122
uint32_t availOut; // remaining free space at nextOut
22-
};
23+
} PAL_ZStream;
2324

2425
/*
2526
Allowed flush values for the Deflate and Inflate functions.
2627
*/
27-
enum PAL_FlushCode : int32_t
28+
enum PAL_FlushCode
2829
{
2930
PAL_Z_NOFLUSH = 0,
3031
PAL_Z_FINISH = 4,
@@ -33,7 +34,7 @@ enum PAL_FlushCode : int32_t
3334
/*
3435
Error codes from the zlib functions.
3536
*/
36-
enum PAL_ErrorCode : int32_t
37+
enum PAL_ErrorCode
3738
{
3839
PAL_Z_OK = 0,
3940
PAL_Z_STREAMEND = 1,
@@ -47,7 +48,7 @@ enum PAL_ErrorCode : int32_t
4748
/*
4849
Compression levels
4950
*/
50-
enum PAL_CompressionLevel : int32_t
51+
enum PAL_CompressionLevel
5152
{
5253
PAL_Z_NOCOMPRESSION = 0,
5354
PAL_Z_BESTSPEED = 1,
@@ -57,15 +58,15 @@ enum PAL_CompressionLevel : int32_t
5758
/*
5859
Compression strategy
5960
*/
60-
enum PAL_CompressionStrategy : int32_t
61+
enum PAL_CompressionStrategy
6162
{
6263
PAL_Z_DEFAULTSTRATEGY = 0
6364
};
6465

6566
/*
6667
The deflate compression method (the only one supported in this version)
6768
*/
68-
enum PAL_CompressionMethod : int32_t
69+
enum PAL_CompressionMethod
6970
{
7071
PAL_Z_DEFLATED = 8
7172
};
@@ -75,7 +76,7 @@ Initializes the PAL_ZStream so the Deflate function can be invoked on it.
7576
7677
Returns a PAL_ErrorCode indicating success or an error number on failure.
7778
*/
78-
extern "C" int32_t CompressionNative_DeflateInit2_(
79+
DLLEXPORT int32_t CompressionNative_DeflateInit2_(
7980
PAL_ZStream* stream, int32_t level, int32_t method, int32_t windowBits, int32_t memLevel, int32_t strategy);
8081

8182
/*
@@ -84,41 +85,41 @@ compressed bytes in nextOut.
8485
8586
Returns a PAL_ErrorCode indicating success or an error number on failure.
8687
*/
87-
extern "C" int32_t CompressionNative_Deflate(PAL_ZStream* stream, int32_t flush);
88+
DLLEXPORT int32_t CompressionNative_Deflate(PAL_ZStream* stream, int32_t flush);
8889

8990
/*
9091
All dynamically allocated data structures for this stream are freed.
9192
9293
Returns a PAL_ErrorCode indicating success or an error number on failure.
9394
*/
94-
extern "C" int32_t CompressionNative_DeflateEnd(PAL_ZStream* stream);
95+
DLLEXPORT int32_t CompressionNative_DeflateEnd(PAL_ZStream* stream);
9596

9697
/*
9798
Initializes the PAL_ZStream so the Inflate function can be invoked on it.
9899
99100
Returns a PAL_ErrorCode indicating success or an error number on failure.
100101
*/
101-
extern "C" int32_t CompressionNative_InflateInit2_(PAL_ZStream* stream, int32_t windowBits);
102+
DLLEXPORT int32_t CompressionNative_InflateInit2_(PAL_ZStream* stream, int32_t windowBits);
102103

103104
/*
104105
Inflates (uncompresses) the bytes in the PAL_ZStream's nextIn buffer and puts the
105106
uncompressed bytes in nextOut.
106107
107108
Returns a PAL_ErrorCode indicating success or an error number on failure.
108109
*/
109-
extern "C" int32_t CompressionNative_Inflate(PAL_ZStream* stream, int32_t flush);
110+
DLLEXPORT int32_t CompressionNative_Inflate(PAL_ZStream* stream, int32_t flush);
110111

111112
/*
112113
All dynamically allocated data structures for this stream are freed.
113114
114115
Returns a PAL_ErrorCode indicating success or an error number on failure.
115116
*/
116-
extern "C" int32_t CompressionNative_InflateEnd(PAL_ZStream* stream);
117+
DLLEXPORT int32_t CompressionNative_InflateEnd(PAL_ZStream* stream);
117118

118119
/*
119120
Update a running CRC-32 with the bytes buffer[0..len-1] and return the
120121
updated CRC-32.
121122
122123
Returns the updated CRC-32.
123124
*/
124-
extern "C" uint32_t CompressionNative_Crc32(uint32_t crc, uint8_t* buffer, int32_t len);
125+
DLLEXPORT uint32_t CompressionNative_Crc32(uint32_t crc, uint8_t* buffer, int32_t len);

0 commit comments

Comments
 (0)