Skip to content

Commit bc064bd

Browse files
update zlib header
1 parent 941c918 commit bc064bd

4 files changed

Lines changed: 152 additions & 40 deletions

File tree

include/ramdisk.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ const char* init_ramdisk_from_memory(uint8_t* memory, size_t blocks, size_t bloc
7575
*/
7676
bool decompress_gzip(uint8_t *compressed_image, size_t compressed_size, uint8_t** out, uint32_t* out_size);
7777

78+
bool compress_gzip(uint8_t *data, size_t data_size, uint8_t **out, uint32_t *out_size, int level);
79+
7880
/**
7981
* @brief Inflate and mount the initial ramdisk supplied by the bootloader
8082
*

include/zlib/zlib.h

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* zlib.h -- interface of the 'zlib' general purpose compression library
2-
version 1.3.1.1, January xxth, 2024
2+
version 1.3.1, January 22nd, 2024
33
44
Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
55
@@ -31,22 +31,18 @@
3131
#ifndef ZLIB_H
3232
#define ZLIB_H
3333

34-
#ifdef ZLIB_BUILD
35-
# include <zconf.h>
36-
#else
37-
# include "zconf.h"
38-
#endif
34+
#include "zconf.h"
3935

4036
#ifdef __cplusplus
4137
extern "C" {
4238
#endif
4339

44-
#define ZLIB_VERSION "1.3.1.1-motley"
45-
#define ZLIB_VERNUM 0x1311
40+
#define ZLIB_VERSION "1.3.1"
41+
#define ZLIB_VERNUM 0x1310
4642
#define ZLIB_VER_MAJOR 1
4743
#define ZLIB_VER_MINOR 3
4844
#define ZLIB_VER_REVISION 1
49-
#define ZLIB_VER_SUBREVISION 1
45+
#define ZLIB_VER_SUBREVISION 0
5046

5147
/*
5248
The 'zlib' compression library provides in-memory compression and
@@ -591,21 +587,18 @@ ZEXTERN int ZEXPORT deflateInit2(z_streamp strm,
591587
592588
The strategy parameter is used to tune the compression algorithm. Use the
593589
value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
594-
filter (or predictor), Z_RLE to limit match distances to one (run-length
595-
encoding), or Z_HUFFMAN_ONLY to force Huffman encoding only (no string
596-
matching). Filtered data consists mostly of small values with a somewhat
597-
random distribution, as produced by the PNG filters. In this case, the
598-
compression algorithm is tuned to compress them better. The effect of
599-
Z_FILTERED is to force more Huffman coding and less string matching than the
600-
default; it is intermediate between Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY.
601-
Z_RLE is almost as fast as Z_HUFFMAN_ONLY, but should give better
602-
compression for PNG image data than Huffman only. The degree of string
603-
matching from most to none is: Z_DEFAULT_STRATEGY, Z_FILTERED, Z_RLE, then
604-
Z_HUFFMAN_ONLY. The strategy parameter affects the compression ratio but
605-
never the correctness of the compressed output, even if it is not set
606-
optimally for the given data. Z_FIXED uses the default string matching, but
607-
prevents the use of dynamic Huffman codes, allowing for a simpler decoder
608-
for special applications.
590+
filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
591+
string match), or Z_RLE to limit match distances to one (run-length
592+
encoding). Filtered data consists mostly of small values with a somewhat
593+
random distribution. In this case, the compression algorithm is tuned to
594+
compress them better. The effect of Z_FILTERED is to force more Huffman
595+
coding and less string matching; it is somewhat intermediate between
596+
Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as
597+
fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The
598+
strategy parameter only affects the compression ratio but not the
599+
correctness of the compressed output even if it is not set appropriately.
600+
Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler
601+
decoder for special applications.
609602
610603
deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
611604
memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid
@@ -795,18 +788,6 @@ ZEXTERN int ZEXPORT deflatePending(z_streamp strm,
795788
stream state was inconsistent.
796789
*/
797790

798-
ZEXTERN int ZEXPORT deflateUsed(z_streamp strm,
799-
int *bits);
800-
/*
801-
deflateUsed() returns in *bits the most recent number of deflate bits used
802-
in the last byte when flushing to a byte boundary. The result is in 1..8, or
803-
0 if there has not yet been a flush. This helps determine the location of
804-
the last bit of a deflate stream.
805-
806-
deflateUsed returns Z_OK if success, or Z_STREAM_ERROR if the source
807-
stream state was inconsistent.
808-
*/
809-
810791
ZEXTERN int ZEXPORT deflatePrime(z_streamp strm,
811792
int bits,
812793
int value);
@@ -1907,9 +1888,9 @@ ZEXTERN int ZEXPORT gzgetc_(gzFile file); /* backward compatibility */
19071888
ZEXTERN z_off_t ZEXPORT gzseek64(gzFile, z_off_t, int);
19081889
ZEXTERN z_off_t ZEXPORT gztell64(gzFile);
19091890
ZEXTERN z_off_t ZEXPORT gzoffset64(gzFile);
1910-
ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off64_t);
1911-
ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off64_t);
1912-
ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off64_t);
1891+
ZEXTERN uLong ZEXPORT adler32_combine64(uLong, uLong, z_off_t);
1892+
ZEXTERN uLong ZEXPORT crc32_combine64(uLong, uLong, z_off_t);
1893+
ZEXTERN uLong ZEXPORT crc32_combine_gen64(z_off_t);
19131894
# endif
19141895
#else
19151896
ZEXTERN gzFile ZEXPORT gzopen(const char *, const char *);

src/block/ramdisk.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,60 @@ static void zlib_free(voidpf opaque, voidpf addr) {
213213
}
214214
}
215215

216+
bool compress_gzip(uint8_t *data, size_t data_size, uint8_t **out, uint32_t *out_size, int level) {
217+
if (data == NULL || out == NULL || out_size == NULL) {
218+
return false;
219+
}
220+
221+
if (data_size == 0) {
222+
return false;
223+
}
224+
225+
if (level < Z_BEST_SPEED || level > Z_BEST_COMPRESSION) {
226+
level = Z_BEST_SPEED;
227+
}
228+
229+
z_stream zs;
230+
memset(&zs, 0, sizeof(zs));
231+
zs.zalloc = zlib_alloc;
232+
zs.zfree = zlib_free;
233+
zs.opaque = Z_NULL;
234+
235+
size_t bound = deflateBound(&zs, data_size);
236+
if (bound > UINT32_MAX) {
237+
return false;
238+
}
239+
240+
*out = kmalloc(bound);
241+
if (*out == NULL) {
242+
return false;
243+
}
244+
245+
zs.next_in = (Bytef *)data;
246+
zs.avail_in = (uInt)data_size;
247+
zs.next_out = (Bytef *)*out;
248+
zs.avail_out = (uInt)bound;
249+
250+
int rc = deflateInit2(&zs, level, Z_DEFLATED, 16 + MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
251+
if (rc != Z_OK) {
252+
kfree(*out);
253+
*out = NULL;
254+
return false;
255+
}
256+
257+
rc = deflate(&zs, Z_FINISH);
258+
if (rc != Z_STREAM_END) {
259+
deflateEnd(&zs);
260+
kfree(*out);
261+
*out = NULL;
262+
return false;
263+
}
264+
265+
*out_size = (uint32_t)zs.total_out;
266+
deflateEnd(&zs);
267+
return true;
268+
}
269+
216270
bool decompress_gzip(uint8_t *compressed_image, size_t compressed_size, uint8_t** out, uint32_t* out_size) {
217271
if (compressed_image == NULL) {
218272
return false;
@@ -291,4 +345,4 @@ bool mount_initial_ramdisk(uint8_t *compressed_image, size_t compressed_size) {
291345
}
292346

293347
return true;
294-
}
348+
}

src/zlib/compress.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* compress.c -- compress a memory buffer
2+
* Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
3+
* For conditions of distribution and use, see copyright notice in zlib.h
4+
*/
5+
6+
/* @(#) $Id$ */
7+
8+
#define ZLIB_INTERNAL
9+
#include "zlib.h"
10+
11+
/* ===========================================================================
12+
Compresses the source buffer into the destination buffer. The level
13+
parameter has the same meaning as in deflateInit. sourceLen is the byte
14+
length of the source buffer. Upon entry, destLen is the total size of the
15+
destination buffer, which must be at least 0.1% larger than sourceLen plus
16+
12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
17+
18+
compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
19+
memory, Z_BUF_ERROR if there was not enough room in the output buffer,
20+
Z_STREAM_ERROR if the level parameter is invalid.
21+
*/
22+
int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
23+
uLong sourceLen, int level) {
24+
z_stream stream;
25+
int err;
26+
const uInt max = (uInt)-1;
27+
uLong left;
28+
29+
left = *destLen;
30+
*destLen = 0;
31+
32+
stream.zalloc = (alloc_func)0;
33+
stream.zfree = (free_func)0;
34+
stream.opaque = (voidpf)0;
35+
36+
err = deflateInit(&stream, level);
37+
if (err != Z_OK) return err;
38+
39+
stream.next_out = dest;
40+
stream.avail_out = 0;
41+
stream.next_in = (z_const Bytef *)source;
42+
stream.avail_in = 0;
43+
44+
do {
45+
if (stream.avail_out == 0) {
46+
stream.avail_out = left > (uLong)max ? max : (uInt)left;
47+
left -= stream.avail_out;
48+
}
49+
if (stream.avail_in == 0) {
50+
stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
51+
sourceLen -= stream.avail_in;
52+
}
53+
err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
54+
} while (err == Z_OK);
55+
56+
*destLen = stream.total_out;
57+
deflateEnd(&stream);
58+
return err == Z_STREAM_END ? Z_OK : err;
59+
}
60+
61+
/* ===========================================================================
62+
*/
63+
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source,
64+
uLong sourceLen) {
65+
return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
66+
}
67+
68+
/* ===========================================================================
69+
If the default memLevel or windowBits for deflateInit() is changed, then
70+
this function needs to be updated.
71+
*/
72+
uLong ZEXPORT compressBound(uLong sourceLen) {
73+
return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
74+
(sourceLen >> 25) + 13;
75+
}

0 commit comments

Comments
 (0)