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