diff options
author | Marcin Bukat <marcin.bukat@gmail.com> | 2010-10-31 12:40:49 +0000 |
---|---|---|
committer | Marcin Bukat <marcin.bukat@gmail.com> | 2010-10-31 12:40:49 +0000 |
commit | e26d0c62e07d1b1c1087a065242ed7ab1a2c522f (patch) | |
tree | b1e27ee113a94784233ca7aaf5b9d670f303b332 | |
parent | c8901b3506ffb2f18798b005552c61b92b9d6a6c (diff) | |
download | rockbox-e26d0c62e07d1b1c1087a065242ed7ab1a2c522f.tar.gz rockbox-e26d0c62e07d1b1c1087a065242ed7ab1a2c522f.zip |
Fix and extend imageviewer png support. FS#11641 by me
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@28413 a1c6a512-1295-4272-9138-f99709370657
24 files changed, 3214 insertions, 6608 deletions
diff --git a/apps/plugins/imageviewer/SUBDIRS b/apps/plugins/imageviewer/SUBDIRS index ca9665a022..9d8da7d53b 100644 --- a/apps/plugins/imageviewer/SUBDIRS +++ b/apps/plugins/imageviewer/SUBDIRS | |||
@@ -1,5 +1,3 @@ | |||
1 | bmp | 1 | bmp |
2 | jpeg | 2 | jpeg |
3 | #ifdef HAVE_LCD_COLOR | ||
4 | png | 3 | png |
5 | #endif | ||
diff --git a/apps/plugins/imageviewer/png/SOURCES b/apps/plugins/imageviewer/png/SOURCES index 19ca69d927..8c278b02b9 100644 --- a/apps/plugins/imageviewer/png/SOURCES +++ b/apps/plugins/imageviewer/png/SOURCES | |||
@@ -1,7 +1,6 @@ | |||
1 | adler32.c | 1 | crc32.c |
2 | crc32_png.c | 2 | tinflate.c |
3 | inffast.c | 3 | tinfzlib.c |
4 | inflate.c | 4 | png_decoder.c |
5 | inftrees.c | ||
6 | png.c | 5 | png.c |
7 | png_ui.c | 6 | png_ui.c |
diff --git a/apps/plugins/imageviewer/png/adler32.c b/apps/plugins/imageviewer/png/adler32.c deleted file mode 100644 index 007ba26277..0000000000 --- a/apps/plugins/imageviewer/png/adler32.c +++ /dev/null | |||
@@ -1,149 +0,0 @@ | |||
1 | /* adler32.c -- compute the Adler-32 checksum of a data stream | ||
2 | * Copyright (C) 1995-2004 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 | #define BASE 65521UL /* largest prime smaller than 65536 */ | ||
12 | #define NMAX 5552 | ||
13 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ | ||
14 | |||
15 | #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} | ||
16 | #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); | ||
17 | #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); | ||
18 | #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); | ||
19 | #define DO16(buf) DO8(buf,0); DO8(buf,8); | ||
20 | |||
21 | /* use NO_DIVIDE if your processor does not do division in hardware */ | ||
22 | #ifdef NO_DIVIDE | ||
23 | # define MOD(a) \ | ||
24 | do { \ | ||
25 | if (a >= (BASE << 16)) a -= (BASE << 16); \ | ||
26 | if (a >= (BASE << 15)) a -= (BASE << 15); \ | ||
27 | if (a >= (BASE << 14)) a -= (BASE << 14); \ | ||
28 | if (a >= (BASE << 13)) a -= (BASE << 13); \ | ||
29 | if (a >= (BASE << 12)) a -= (BASE << 12); \ | ||
30 | if (a >= (BASE << 11)) a -= (BASE << 11); \ | ||
31 | if (a >= (BASE << 10)) a -= (BASE << 10); \ | ||
32 | if (a >= (BASE << 9)) a -= (BASE << 9); \ | ||
33 | if (a >= (BASE << 8)) a -= (BASE << 8); \ | ||
34 | if (a >= (BASE << 7)) a -= (BASE << 7); \ | ||
35 | if (a >= (BASE << 6)) a -= (BASE << 6); \ | ||
36 | if (a >= (BASE << 5)) a -= (BASE << 5); \ | ||
37 | if (a >= (BASE << 4)) a -= (BASE << 4); \ | ||
38 | if (a >= (BASE << 3)) a -= (BASE << 3); \ | ||
39 | if (a >= (BASE << 2)) a -= (BASE << 2); \ | ||
40 | if (a >= (BASE << 1)) a -= (BASE << 1); \ | ||
41 | if (a >= BASE) a -= BASE; \ | ||
42 | } while (0) | ||
43 | # define MOD4(a) \ | ||
44 | do { \ | ||
45 | if (a >= (BASE << 4)) a -= (BASE << 4); \ | ||
46 | if (a >= (BASE << 3)) a -= (BASE << 3); \ | ||
47 | if (a >= (BASE << 2)) a -= (BASE << 2); \ | ||
48 | if (a >= (BASE << 1)) a -= (BASE << 1); \ | ||
49 | if (a >= BASE) a -= BASE; \ | ||
50 | } while (0) | ||
51 | #else | ||
52 | # define MOD(a) a %= BASE | ||
53 | # define MOD4(a) a %= BASE | ||
54 | #endif | ||
55 | |||
56 | /* ========================================================================= */ | ||
57 | uLong ZEXPORT adler32(adler, buf, len) | ||
58 | uLong adler; | ||
59 | const Bytef *buf; | ||
60 | uInt len; | ||
61 | { | ||
62 | unsigned long sum2; | ||
63 | unsigned n; | ||
64 | |||
65 | /* split Adler-32 into component sums */ | ||
66 | sum2 = (adler >> 16) & 0xffff; | ||
67 | adler &= 0xffff; | ||
68 | |||
69 | /* in case user likes doing a byte at a time, keep it fast */ | ||
70 | if (len == 1) { | ||
71 | adler += buf[0]; | ||
72 | if (adler >= BASE) | ||
73 | adler -= BASE; | ||
74 | sum2 += adler; | ||
75 | if (sum2 >= BASE) | ||
76 | sum2 -= BASE; | ||
77 | return adler | (sum2 << 16); | ||
78 | } | ||
79 | |||
80 | /* initial Adler-32 value (deferred check for len == 1 speed) */ | ||
81 | if (buf == Z_NULL) | ||
82 | return 1L; | ||
83 | |||
84 | /* in case short lengths are provided, keep it somewhat fast */ | ||
85 | if (len < 16) { | ||
86 | while (len--) { | ||
87 | adler += *buf++; | ||
88 | sum2 += adler; | ||
89 | } | ||
90 | if (adler >= BASE) | ||
91 | adler -= BASE; | ||
92 | MOD4(sum2); /* only added so many BASE's */ | ||
93 | return adler | (sum2 << 16); | ||
94 | } | ||
95 | |||
96 | /* do length NMAX blocks -- requires just one modulo operation */ | ||
97 | while (len >= NMAX) { | ||
98 | len -= NMAX; | ||
99 | n = NMAX / 16; /* NMAX is divisible by 16 */ | ||
100 | do { | ||
101 | DO16(buf); /* 16 sums unrolled */ | ||
102 | buf += 16; | ||
103 | } while (--n); | ||
104 | MOD(adler); | ||
105 | MOD(sum2); | ||
106 | } | ||
107 | |||
108 | /* do remaining bytes (less than NMAX, still just one modulo) */ | ||
109 | if (len) { /* avoid modulos if none remaining */ | ||
110 | while (len >= 16) { | ||
111 | len -= 16; | ||
112 | DO16(buf); | ||
113 | buf += 16; | ||
114 | } | ||
115 | while (len--) { | ||
116 | adler += *buf++; | ||
117 | sum2 += adler; | ||
118 | } | ||
119 | MOD(adler); | ||
120 | MOD(sum2); | ||
121 | } | ||
122 | |||
123 | /* return recombined sums */ | ||
124 | return adler | (sum2 << 16); | ||
125 | } | ||
126 | |||
127 | /* ========================================================================= */ | ||
128 | uLong ZEXPORT adler32_combine(adler1, adler2, len2) | ||
129 | uLong adler1; | ||
130 | uLong adler2; | ||
131 | z_off_t len2; | ||
132 | { | ||
133 | unsigned long sum1; | ||
134 | unsigned long sum2; | ||
135 | unsigned rem; | ||
136 | |||
137 | /* the derivation of this formula is left as an exercise for the reader */ | ||
138 | rem = (unsigned)(len2 % BASE); | ||
139 | sum1 = adler1 & 0xffff; | ||
140 | sum2 = rem * sum1; | ||
141 | MOD(sum2); | ||
142 | sum1 += (adler2 & 0xffff) + BASE - 1; | ||
143 | sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; | ||
144 | if (sum1 > BASE) sum1 -= BASE; | ||
145 | if (sum1 > BASE) sum1 -= BASE; | ||
146 | if (sum2 > (BASE << 1)) sum2 -= (BASE << 1); | ||
147 | if (sum2 > BASE) sum2 -= BASE; | ||
148 | return sum1 | (sum2 << 16); | ||
149 | } | ||
diff --git a/apps/plugins/imageviewer/png/crc32.c b/apps/plugins/imageviewer/png/crc32.c new file mode 100644 index 0000000000..c7921a028b --- /dev/null +++ b/apps/plugins/imageviewer/png/crc32.c | |||
@@ -0,0 +1,89 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Original source: | ||
11 | * Copyright (c) 2003 by Joergen Ibsen / Jibz | ||
12 | * | ||
13 | * Rockbox adaptation: | ||
14 | * Copyright (c) 2010 by Marcin Bukat | ||
15 | * | ||
16 | * This program is free software; you can redistribute it and/or | ||
17 | * modify it under the terms of the GNU General Public License | ||
18 | * as published by the Free Software Foundation; either version 2 | ||
19 | * of the License, or (at your option) any later version. | ||
20 | * | ||
21 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
22 | * KIND, either express or implied. | ||
23 | * | ||
24 | ****************************************************************************/ | ||
25 | |||
26 | /* | ||
27 | * CRC32 checksum | ||
28 | * | ||
29 | * Copyright (c) 1998-2003 by Joergen Ibsen / Jibz | ||
30 | * All Rights Reserved | ||
31 | * | ||
32 | * http://www.ibsensoftware.com/ | ||
33 | * | ||
34 | * This software is provided 'as-is', without any express | ||
35 | * or implied warranty. In no event will the authors be | ||
36 | * held liable for any damages arising from the use of | ||
37 | * this software. | ||
38 | * | ||
39 | * Permission is granted to anyone to use this software | ||
40 | * for any purpose, including commercial applications, | ||
41 | * and to alter it and redistribute it freely, subject to | ||
42 | * the following restrictions: | ||
43 | * | ||
44 | * 1. The origin of this software must not be | ||
45 | * misrepresented; you must not claim that you | ||
46 | * wrote the original software. If you use this | ||
47 | * software in a product, an acknowledgment in | ||
48 | * the product documentation would be appreciated | ||
49 | * but is not required. | ||
50 | * | ||
51 | * 2. Altered source versions must be plainly marked | ||
52 | * as such, and must not be misrepresented as | ||
53 | * being the original software. | ||
54 | * | ||
55 | * 3. This notice may not be removed or altered from | ||
56 | * any source distribution. | ||
57 | */ | ||
58 | |||
59 | /* | ||
60 | * CRC32 algorithm taken from the zlib source, which is | ||
61 | * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler | ||
62 | */ | ||
63 | |||
64 | #include "tinf.h" | ||
65 | |||
66 | static const unsigned int tinf_crc32tab[16] = { | ||
67 | 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, | ||
68 | 0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344, | ||
69 | 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, | ||
70 | 0xbdbdf21c | ||
71 | }; | ||
72 | |||
73 | unsigned int tinf_crc32(const void *data, unsigned int length) | ||
74 | { | ||
75 | const unsigned char *buf = (const unsigned char *)data; | ||
76 | unsigned int crc = 0xffffffff; | ||
77 | unsigned int i; | ||
78 | |||
79 | if (length == 0) return 0; | ||
80 | |||
81 | for (i = 0; i < length; ++i) | ||
82 | { | ||
83 | crc ^= buf[i]; | ||
84 | crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4); | ||
85 | crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4); | ||
86 | } | ||
87 | |||
88 | return crc ^ 0xffffffff; | ||
89 | } | ||
diff --git a/apps/plugins/imageviewer/png/crc32_png.c b/apps/plugins/imageviewer/png/crc32_png.c deleted file mode 100644 index a76f49d35c..0000000000 --- a/apps/plugins/imageviewer/png/crc32_png.c +++ /dev/null | |||
@@ -1,423 +0,0 @@ | |||
1 | /* crc32.c -- compute the CRC-32 of a data stream | ||
2 | * Copyright (C) 1995-2005 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | * | ||
5 | * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster | ||
6 | * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing | ||
7 | * tables for updating the shift register in one step with three exclusive-ors | ||
8 | * instead of four steps with four exclusive-ors. This results in about a | ||
9 | * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. | ||
10 | */ | ||
11 | |||
12 | /* @(#) $Id$ */ | ||
13 | |||
14 | /* | ||
15 | Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore | ||
16 | protection on the static variables used to control the first-use generation | ||
17 | of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should | ||
18 | first call get_crc_table() to initialize the tables before allowing more than | ||
19 | one thread to use crc32(). | ||
20 | */ | ||
21 | |||
22 | #ifdef MAKECRCH | ||
23 | # include <stdio.h> | ||
24 | # ifndef DYNAMIC_CRC_TABLE | ||
25 | # define DYNAMIC_CRC_TABLE | ||
26 | # endif /* !DYNAMIC_CRC_TABLE */ | ||
27 | #endif /* MAKECRCH */ | ||
28 | |||
29 | #include "zutil.h" /* for STDC and FAR definitions */ | ||
30 | |||
31 | #define local static | ||
32 | |||
33 | /* Find a four-byte integer type for crc32_little() and crc32_big(). */ | ||
34 | #ifndef NOBYFOUR | ||
35 | # ifdef STDC /* need ANSI C limits.h to determine sizes */ | ||
36 | # include <limits.h> | ||
37 | # define BYFOUR | ||
38 | # if (UINT_MAX == 0xffffffffUL) | ||
39 | typedef unsigned int u4; | ||
40 | # else | ||
41 | # if (ULONG_MAX == 0xffffffffUL) | ||
42 | typedef unsigned long u4; | ||
43 | # else | ||
44 | # if (USHRT_MAX == 0xffffffffUL) | ||
45 | typedef unsigned short u4; | ||
46 | # else | ||
47 | # undef BYFOUR /* can't find a four-byte integer type! */ | ||
48 | # endif | ||
49 | # endif | ||
50 | # endif | ||
51 | # endif /* STDC */ | ||
52 | #endif /* !NOBYFOUR */ | ||
53 | |||
54 | /* Definitions for doing the crc four data bytes at a time. */ | ||
55 | #ifdef BYFOUR | ||
56 | # define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \ | ||
57 | (((w)&0xff00)<<8)+(((w)&0xff)<<24)) | ||
58 | local unsigned long crc32_little OF((unsigned long, | ||
59 | const unsigned char FAR *, unsigned)); | ||
60 | local unsigned long crc32_big OF((unsigned long, | ||
61 | const unsigned char FAR *, unsigned)); | ||
62 | # define TBLS 8 | ||
63 | #else | ||
64 | # define TBLS 1 | ||
65 | #endif /* BYFOUR */ | ||
66 | |||
67 | /* Local functions for crc concatenation */ | ||
68 | local unsigned long gf2_matrix_times OF((unsigned long *mat, | ||
69 | unsigned long vec)); | ||
70 | local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); | ||
71 | |||
72 | #ifdef DYNAMIC_CRC_TABLE | ||
73 | |||
74 | local volatile int crc_table_empty = 1; | ||
75 | local unsigned long FAR crc_table[TBLS][256]; | ||
76 | local void make_crc_table OF((void)); | ||
77 | #ifdef MAKECRCH | ||
78 | local void write_table OF((FILE *, const unsigned long FAR *)); | ||
79 | #endif /* MAKECRCH */ | ||
80 | /* | ||
81 | Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: | ||
82 | x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. | ||
83 | |||
84 | Polynomials over GF(2) are represented in binary, one bit per coefficient, | ||
85 | with the lowest powers in the most significant bit. Then adding polynomials | ||
86 | is just exclusive-or, and multiplying a polynomial by x is a right shift by | ||
87 | one. If we call the above polynomial p, and represent a byte as the | ||
88 | polynomial q, also with the lowest power in the most significant bit (so the | ||
89 | byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, | ||
90 | where a mod b means the remainder after dividing a by b. | ||
91 | |||
92 | This calculation is done using the shift-register method of multiplying and | ||
93 | taking the remainder. The register is initialized to zero, and for each | ||
94 | incoming bit, x^32 is added mod p to the register if the bit is a one (where | ||
95 | x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by | ||
96 | x (which is shifting right by one and adding x^32 mod p if the bit shifted | ||
97 | out is a one). We start with the highest power (least significant bit) of | ||
98 | q and repeat for all eight bits of q. | ||
99 | |||
100 | The first table is simply the CRC of all possible eight bit values. This is | ||
101 | all the information needed to generate CRCs on data a byte at a time for all | ||
102 | combinations of CRC register values and incoming bytes. The remaining tables | ||
103 | allow for word-at-a-time CRC calculation for both big-endian and little- | ||
104 | endian machines, where a word is four bytes. | ||
105 | */ | ||
106 | local void make_crc_table() | ||
107 | { | ||
108 | unsigned long c; | ||
109 | int n, k; | ||
110 | unsigned long poly; /* polynomial exclusive-or pattern */ | ||
111 | /* terms of polynomial defining this crc (except x^32): */ | ||
112 | static volatile int first = 1; /* flag to limit concurrent making */ | ||
113 | static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; | ||
114 | |||
115 | /* See if another task is already doing this (not thread-safe, but better | ||
116 | than nothing -- significantly reduces duration of vulnerability in | ||
117 | case the advice about DYNAMIC_CRC_TABLE is ignored) */ | ||
118 | if (first) { | ||
119 | first = 0; | ||
120 | |||
121 | /* make exclusive-or pattern from polynomial (0xedb88320UL) */ | ||
122 | poly = 0UL; | ||
123 | for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) | ||
124 | poly |= 1UL << (31 - p[n]); | ||
125 | |||
126 | /* generate a crc for every 8-bit value */ | ||
127 | for (n = 0; n < 256; n++) { | ||
128 | c = (unsigned long)n; | ||
129 | for (k = 0; k < 8; k++) | ||
130 | c = c & 1 ? poly ^ (c >> 1) : c >> 1; | ||
131 | crc_table[0][n] = c; | ||
132 | } | ||
133 | |||
134 | #ifdef BYFOUR | ||
135 | /* generate crc for each value followed by one, two, and three zeros, | ||
136 | and then the byte reversal of those as well as the first table */ | ||
137 | for (n = 0; n < 256; n++) { | ||
138 | c = crc_table[0][n]; | ||
139 | crc_table[4][n] = REV(c); | ||
140 | for (k = 1; k < 4; k++) { | ||
141 | c = crc_table[0][c & 0xff] ^ (c >> 8); | ||
142 | crc_table[k][n] = c; | ||
143 | crc_table[k + 4][n] = REV(c); | ||
144 | } | ||
145 | } | ||
146 | #endif /* BYFOUR */ | ||
147 | |||
148 | crc_table_empty = 0; | ||
149 | } | ||
150 | else { /* not first */ | ||
151 | /* wait for the other guy to finish (not efficient, but rare) */ | ||
152 | while (crc_table_empty) | ||
153 | ; | ||
154 | } | ||
155 | |||
156 | #ifdef MAKECRCH | ||
157 | /* write out CRC tables to crc32.h */ | ||
158 | { | ||
159 | FILE *out; | ||
160 | |||
161 | out = fopen("crc32_png.h", "w"); | ||
162 | if (out == NULL) return; | ||
163 | fprintf(out, "/* crc32_png.h -- tables for rapid CRC calculation\n"); | ||
164 | fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); | ||
165 | fprintf(out, "local const unsigned long FAR "); | ||
166 | fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); | ||
167 | write_table(out, crc_table[0]); | ||
168 | # ifdef BYFOUR | ||
169 | fprintf(out, "#ifdef BYFOUR\n"); | ||
170 | for (k = 1; k < 8; k++) { | ||
171 | fprintf(out, " },\n {\n"); | ||
172 | write_table(out, crc_table[k]); | ||
173 | } | ||
174 | fprintf(out, "#endif\n"); | ||
175 | # endif /* BYFOUR */ | ||
176 | fprintf(out, " }\n};\n"); | ||
177 | fclose(out); | ||
178 | } | ||
179 | #endif /* MAKECRCH */ | ||
180 | } | ||
181 | |||
182 | #ifdef MAKECRCH | ||
183 | local void write_table(out, table) | ||
184 | FILE *out; | ||
185 | const unsigned long FAR *table; | ||
186 | { | ||
187 | int n; | ||
188 | |||
189 | for (n = 0; n < 256; n++) | ||
190 | fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n], | ||
191 | n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); | ||
192 | } | ||
193 | #endif /* MAKECRCH */ | ||
194 | |||
195 | #else /* !DYNAMIC_CRC_TABLE */ | ||
196 | /* ======================================================================== | ||
197 | * Tables of CRC-32s of all single-byte values, made by make_crc_table(). | ||
198 | */ | ||
199 | #include "crc32_png.h" | ||
200 | #endif /* DYNAMIC_CRC_TABLE */ | ||
201 | |||
202 | /* ========================================================================= | ||
203 | * This function can be used by asm versions of crc32() | ||
204 | */ | ||
205 | const unsigned long FAR * ZEXPORT get_crc_table() | ||
206 | { | ||
207 | #ifdef DYNAMIC_CRC_TABLE | ||
208 | if (crc_table_empty) | ||
209 | make_crc_table(); | ||
210 | #endif /* DYNAMIC_CRC_TABLE */ | ||
211 | return (const unsigned long FAR *)crc_table; | ||
212 | } | ||
213 | |||
214 | /* ========================================================================= */ | ||
215 | #define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) | ||
216 | #define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 | ||
217 | |||
218 | /* ========================================================================= */ | ||
219 | unsigned long ZEXPORT crc32(crc, buf, len) | ||
220 | unsigned long crc; | ||
221 | const unsigned char FAR *buf; | ||
222 | unsigned len; | ||
223 | { | ||
224 | if (buf == Z_NULL) return 0UL; | ||
225 | |||
226 | #ifdef DYNAMIC_CRC_TABLE | ||
227 | if (crc_table_empty) | ||
228 | make_crc_table(); | ||
229 | #endif /* DYNAMIC_CRC_TABLE */ | ||
230 | |||
231 | #ifdef BYFOUR | ||
232 | if (sizeof(void *) == sizeof(ptrdiff_t)) { | ||
233 | u4 endian; | ||
234 | |||
235 | endian = 1; | ||
236 | if (*((unsigned char *)(&endian))) | ||
237 | return crc32_little(crc, buf, len); | ||
238 | else | ||
239 | return crc32_big(crc, buf, len); | ||
240 | } | ||
241 | #endif /* BYFOUR */ | ||
242 | crc = crc ^ 0xffffffffUL; | ||
243 | while (len >= 8) { | ||
244 | DO8; | ||
245 | len -= 8; | ||
246 | } | ||
247 | if (len) do { | ||
248 | DO1; | ||
249 | } while (--len); | ||
250 | return crc ^ 0xffffffffUL; | ||
251 | } | ||
252 | |||
253 | #ifdef BYFOUR | ||
254 | |||
255 | /* ========================================================================= */ | ||
256 | #define DOLIT4 c ^= *buf4++; \ | ||
257 | c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ | ||
258 | crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] | ||
259 | #define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 | ||
260 | |||
261 | /* ========================================================================= */ | ||
262 | local unsigned long crc32_little(crc, buf, len) | ||
263 | unsigned long crc; | ||
264 | const unsigned char FAR *buf; | ||
265 | unsigned len; | ||
266 | { | ||
267 | register u4 c; | ||
268 | register const u4 FAR *buf4; | ||
269 | |||
270 | c = (u4)crc; | ||
271 | c = ~c; | ||
272 | while (len && ((ptrdiff_t)buf & 3)) { | ||
273 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); | ||
274 | len--; | ||
275 | } | ||
276 | |||
277 | buf4 = (const u4 FAR *)(const void FAR *)buf; | ||
278 | while (len >= 32) { | ||
279 | DOLIT32; | ||
280 | len -= 32; | ||
281 | } | ||
282 | while (len >= 4) { | ||
283 | DOLIT4; | ||
284 | len -= 4; | ||
285 | } | ||
286 | buf = (const unsigned char FAR *)buf4; | ||
287 | |||
288 | if (len) do { | ||
289 | c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); | ||
290 | } while (--len); | ||
291 | c = ~c; | ||
292 | return (unsigned long)c; | ||
293 | } | ||
294 | |||
295 | /* ========================================================================= */ | ||
296 | #define DOBIG4 c ^= *++buf4; \ | ||
297 | c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ | ||
298 | crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] | ||
299 | #define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 | ||
300 | |||
301 | /* ========================================================================= */ | ||
302 | local unsigned long crc32_big(crc, buf, len) | ||
303 | unsigned long crc; | ||
304 | const unsigned char FAR *buf; | ||
305 | unsigned len; | ||
306 | { | ||
307 | register u4 c; | ||
308 | register const u4 FAR *buf4; | ||
309 | |||
310 | c = REV((u4)crc); | ||
311 | c = ~c; | ||
312 | while (len && ((ptrdiff_t)buf & 3)) { | ||
313 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); | ||
314 | len--; | ||
315 | } | ||
316 | |||
317 | buf4 = (const u4 FAR *)(const void FAR *)buf; | ||
318 | buf4--; | ||
319 | while (len >= 32) { | ||
320 | DOBIG32; | ||
321 | len -= 32; | ||
322 | } | ||
323 | while (len >= 4) { | ||
324 | DOBIG4; | ||
325 | len -= 4; | ||
326 | } | ||
327 | buf4++; | ||
328 | buf = (const unsigned char FAR *)buf4; | ||
329 | |||
330 | if (len) do { | ||
331 | c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); | ||
332 | } while (--len); | ||
333 | c = ~c; | ||
334 | return (unsigned long)(REV(c)); | ||
335 | } | ||
336 | |||
337 | #endif /* BYFOUR */ | ||
338 | |||
339 | #define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ | ||
340 | |||
341 | /* ========================================================================= */ | ||
342 | local unsigned long gf2_matrix_times(mat, vec) | ||
343 | unsigned long *mat; | ||
344 | unsigned long vec; | ||
345 | { | ||
346 | unsigned long sum; | ||
347 | |||
348 | sum = 0; | ||
349 | while (vec) { | ||
350 | if (vec & 1) | ||
351 | sum ^= *mat; | ||
352 | vec >>= 1; | ||
353 | mat++; | ||
354 | } | ||
355 | return sum; | ||
356 | } | ||
357 | |||
358 | /* ========================================================================= */ | ||
359 | local void gf2_matrix_square(square, mat) | ||
360 | unsigned long *square; | ||
361 | unsigned long *mat; | ||
362 | { | ||
363 | int n; | ||
364 | |||
365 | for (n = 0; n < GF2_DIM; n++) | ||
366 | square[n] = gf2_matrix_times(mat, mat[n]); | ||
367 | } | ||
368 | |||
369 | /* ========================================================================= */ | ||
370 | uLong ZEXPORT crc32_combine(crc1, crc2, len2) | ||
371 | uLong crc1; | ||
372 | uLong crc2; | ||
373 | z_off_t len2; | ||
374 | { | ||
375 | int n; | ||
376 | unsigned long row; | ||
377 | unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ | ||
378 | unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ | ||
379 | |||
380 | /* degenerate case */ | ||
381 | if (len2 == 0) | ||
382 | return crc1; | ||
383 | |||
384 | /* put operator for one zero bit in odd */ | ||
385 | odd[0] = 0xedb88320L; /* CRC-32 polynomial */ | ||
386 | row = 1; | ||
387 | for (n = 1; n < GF2_DIM; n++) { | ||
388 | odd[n] = row; | ||
389 | row <<= 1; | ||
390 | } | ||
391 | |||
392 | /* put operator for two zero bits in even */ | ||
393 | gf2_matrix_square(even, odd); | ||
394 | |||
395 | /* put operator for four zero bits in odd */ | ||
396 | gf2_matrix_square(odd, even); | ||
397 | |||
398 | /* apply len2 zeros to crc1 (first square will put the operator for one | ||
399 | zero byte, eight zero bits, in even) */ | ||
400 | do { | ||
401 | /* apply zeros operator for this bit of len2 */ | ||
402 | gf2_matrix_square(even, odd); | ||
403 | if (len2 & 1) | ||
404 | crc1 = gf2_matrix_times(even, crc1); | ||
405 | len2 >>= 1; | ||
406 | |||
407 | /* if no more bits set, then done */ | ||
408 | if (len2 == 0) | ||
409 | break; | ||
410 | |||
411 | /* another iteration of the loop with odd and even swapped */ | ||
412 | gf2_matrix_square(odd, even); | ||
413 | if (len2 & 1) | ||
414 | crc1 = gf2_matrix_times(odd, crc1); | ||
415 | len2 >>= 1; | ||
416 | |||
417 | /* if no more bits set, then done */ | ||
418 | } while (len2 != 0); | ||
419 | |||
420 | /* return combined crc */ | ||
421 | crc1 ^= crc2; | ||
422 | return crc1; | ||
423 | } | ||
diff --git a/apps/plugins/imageviewer/png/crc32_png.h b/apps/plugins/imageviewer/png/crc32_png.h deleted file mode 100644 index 8053b6117c..0000000000 --- a/apps/plugins/imageviewer/png/crc32_png.h +++ /dev/null | |||
@@ -1,441 +0,0 @@ | |||
1 | /* crc32.h -- tables for rapid CRC calculation | ||
2 | * Generated automatically by crc32.c | ||
3 | */ | ||
4 | |||
5 | local const unsigned long FAR crc_table[TBLS][256] = | ||
6 | { | ||
7 | { | ||
8 | 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL, | ||
9 | 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL, | ||
10 | 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL, | ||
11 | 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL, | ||
12 | 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL, | ||
13 | 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL, | ||
14 | 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL, | ||
15 | 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL, | ||
16 | 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL, | ||
17 | 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL, | ||
18 | 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL, | ||
19 | 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL, | ||
20 | 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL, | ||
21 | 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL, | ||
22 | 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL, | ||
23 | 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL, | ||
24 | 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL, | ||
25 | 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL, | ||
26 | 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL, | ||
27 | 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL, | ||
28 | 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL, | ||
29 | 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL, | ||
30 | 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL, | ||
31 | 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL, | ||
32 | 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL, | ||
33 | 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL, | ||
34 | 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL, | ||
35 | 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL, | ||
36 | 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL, | ||
37 | 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL, | ||
38 | 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL, | ||
39 | 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL, | ||
40 | 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL, | ||
41 | 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL, | ||
42 | 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL, | ||
43 | 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL, | ||
44 | 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL, | ||
45 | 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL, | ||
46 | 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL, | ||
47 | 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL, | ||
48 | 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL, | ||
49 | 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL, | ||
50 | 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL, | ||
51 | 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL, | ||
52 | 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL, | ||
53 | 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL, | ||
54 | 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL, | ||
55 | 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL, | ||
56 | 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL, | ||
57 | 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, | ||
58 | 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, | ||
59 | 0x2d02ef8dUL | ||
60 | #ifdef BYFOUR | ||
61 | }, | ||
62 | { | ||
63 | 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL, | ||
64 | 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL, | ||
65 | 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL, | ||
66 | 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL, | ||
67 | 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL, | ||
68 | 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL, | ||
69 | 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL, | ||
70 | 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL, | ||
71 | 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL, | ||
72 | 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL, | ||
73 | 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL, | ||
74 | 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL, | ||
75 | 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL, | ||
76 | 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL, | ||
77 | 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL, | ||
78 | 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL, | ||
79 | 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL, | ||
80 | 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL, | ||
81 | 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL, | ||
82 | 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL, | ||
83 | 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL, | ||
84 | 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL, | ||
85 | 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL, | ||
86 | 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL, | ||
87 | 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL, | ||
88 | 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL, | ||
89 | 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL, | ||
90 | 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL, | ||
91 | 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL, | ||
92 | 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL, | ||
93 | 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL, | ||
94 | 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL, | ||
95 | 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL, | ||
96 | 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL, | ||
97 | 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL, | ||
98 | 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL, | ||
99 | 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL, | ||
100 | 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL, | ||
101 | 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL, | ||
102 | 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL, | ||
103 | 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL, | ||
104 | 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL, | ||
105 | 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL, | ||
106 | 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL, | ||
107 | 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL, | ||
108 | 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL, | ||
109 | 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL, | ||
110 | 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL, | ||
111 | 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL, | ||
112 | 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL, | ||
113 | 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL, | ||
114 | 0x9324fd72UL | ||
115 | }, | ||
116 | { | ||
117 | 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL, | ||
118 | 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL, | ||
119 | 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL, | ||
120 | 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL, | ||
121 | 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL, | ||
122 | 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL, | ||
123 | 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL, | ||
124 | 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL, | ||
125 | 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL, | ||
126 | 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL, | ||
127 | 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL, | ||
128 | 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL, | ||
129 | 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL, | ||
130 | 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL, | ||
131 | 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL, | ||
132 | 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL, | ||
133 | 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL, | ||
134 | 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL, | ||
135 | 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL, | ||
136 | 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL, | ||
137 | 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL, | ||
138 | 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL, | ||
139 | 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL, | ||
140 | 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL, | ||
141 | 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL, | ||
142 | 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL, | ||
143 | 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL, | ||
144 | 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL, | ||
145 | 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL, | ||
146 | 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL, | ||
147 | 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL, | ||
148 | 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL, | ||
149 | 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL, | ||
150 | 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL, | ||
151 | 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL, | ||
152 | 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL, | ||
153 | 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL, | ||
154 | 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL, | ||
155 | 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL, | ||
156 | 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL, | ||
157 | 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL, | ||
158 | 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL, | ||
159 | 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL, | ||
160 | 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL, | ||
161 | 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL, | ||
162 | 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL, | ||
163 | 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL, | ||
164 | 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL, | ||
165 | 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL, | ||
166 | 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL, | ||
167 | 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL, | ||
168 | 0xbe9834edUL | ||
169 | }, | ||
170 | { | ||
171 | 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL, | ||
172 | 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL, | ||
173 | 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL, | ||
174 | 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL, | ||
175 | 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL, | ||
176 | 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL, | ||
177 | 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL, | ||
178 | 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL, | ||
179 | 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL, | ||
180 | 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL, | ||
181 | 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL, | ||
182 | 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL, | ||
183 | 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL, | ||
184 | 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL, | ||
185 | 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL, | ||
186 | 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL, | ||
187 | 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL, | ||
188 | 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL, | ||
189 | 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL, | ||
190 | 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL, | ||
191 | 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL, | ||
192 | 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL, | ||
193 | 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL, | ||
194 | 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL, | ||
195 | 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL, | ||
196 | 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL, | ||
197 | 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL, | ||
198 | 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL, | ||
199 | 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL, | ||
200 | 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL, | ||
201 | 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL, | ||
202 | 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL, | ||
203 | 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL, | ||
204 | 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL, | ||
205 | 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL, | ||
206 | 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL, | ||
207 | 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL, | ||
208 | 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL, | ||
209 | 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL, | ||
210 | 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL, | ||
211 | 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL, | ||
212 | 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL, | ||
213 | 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL, | ||
214 | 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL, | ||
215 | 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL, | ||
216 | 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL, | ||
217 | 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL, | ||
218 | 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL, | ||
219 | 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL, | ||
220 | 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL, | ||
221 | 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL, | ||
222 | 0xde0506f1UL | ||
223 | }, | ||
224 | { | ||
225 | 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL, | ||
226 | 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL, | ||
227 | 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL, | ||
228 | 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL, | ||
229 | 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL, | ||
230 | 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL, | ||
231 | 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL, | ||
232 | 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL, | ||
233 | 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL, | ||
234 | 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL, | ||
235 | 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL, | ||
236 | 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL, | ||
237 | 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL, | ||
238 | 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL, | ||
239 | 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL, | ||
240 | 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL, | ||
241 | 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL, | ||
242 | 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL, | ||
243 | 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL, | ||
244 | 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL, | ||
245 | 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL, | ||
246 | 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL, | ||
247 | 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL, | ||
248 | 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL, | ||
249 | 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL, | ||
250 | 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL, | ||
251 | 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL, | ||
252 | 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL, | ||
253 | 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL, | ||
254 | 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL, | ||
255 | 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL, | ||
256 | 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL, | ||
257 | 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL, | ||
258 | 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL, | ||
259 | 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL, | ||
260 | 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL, | ||
261 | 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL, | ||
262 | 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL, | ||
263 | 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL, | ||
264 | 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL, | ||
265 | 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL, | ||
266 | 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL, | ||
267 | 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL, | ||
268 | 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL, | ||
269 | 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL, | ||
270 | 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL, | ||
271 | 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL, | ||
272 | 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL, | ||
273 | 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL, | ||
274 | 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL, | ||
275 | 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL, | ||
276 | 0x8def022dUL | ||
277 | }, | ||
278 | { | ||
279 | 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL, | ||
280 | 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL, | ||
281 | 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL, | ||
282 | 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL, | ||
283 | 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL, | ||
284 | 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL, | ||
285 | 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL, | ||
286 | 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL, | ||
287 | 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL, | ||
288 | 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL, | ||
289 | 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL, | ||
290 | 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL, | ||
291 | 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL, | ||
292 | 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL, | ||
293 | 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL, | ||
294 | 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL, | ||
295 | 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL, | ||
296 | 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL, | ||
297 | 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL, | ||
298 | 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL, | ||
299 | 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL, | ||
300 | 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL, | ||
301 | 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL, | ||
302 | 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL, | ||
303 | 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL, | ||
304 | 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL, | ||
305 | 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL, | ||
306 | 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL, | ||
307 | 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL, | ||
308 | 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL, | ||
309 | 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL, | ||
310 | 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL, | ||
311 | 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL, | ||
312 | 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL, | ||
313 | 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL, | ||
314 | 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL, | ||
315 | 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL, | ||
316 | 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL, | ||
317 | 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL, | ||
318 | 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL, | ||
319 | 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL, | ||
320 | 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL, | ||
321 | 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL, | ||
322 | 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL, | ||
323 | 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL, | ||
324 | 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL, | ||
325 | 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL, | ||
326 | 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL, | ||
327 | 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL, | ||
328 | 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL, | ||
329 | 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL, | ||
330 | 0x72fd2493UL | ||
331 | }, | ||
332 | { | ||
333 | 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL, | ||
334 | 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL, | ||
335 | 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL, | ||
336 | 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL, | ||
337 | 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL, | ||
338 | 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL, | ||
339 | 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL, | ||
340 | 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL, | ||
341 | 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL, | ||
342 | 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL, | ||
343 | 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL, | ||
344 | 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL, | ||
345 | 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL, | ||
346 | 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL, | ||
347 | 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL, | ||
348 | 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL, | ||
349 | 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL, | ||
350 | 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL, | ||
351 | 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL, | ||
352 | 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL, | ||
353 | 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL, | ||
354 | 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL, | ||
355 | 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL, | ||
356 | 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL, | ||
357 | 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL, | ||
358 | 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL, | ||
359 | 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL, | ||
360 | 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL, | ||
361 | 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL, | ||
362 | 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL, | ||
363 | 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL, | ||
364 | 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL, | ||
365 | 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL, | ||
366 | 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL, | ||
367 | 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL, | ||
368 | 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL, | ||
369 | 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL, | ||
370 | 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL, | ||
371 | 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL, | ||
372 | 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL, | ||
373 | 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL, | ||
374 | 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL, | ||
375 | 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL, | ||
376 | 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL, | ||
377 | 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL, | ||
378 | 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL, | ||
379 | 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL, | ||
380 | 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL, | ||
381 | 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL, | ||
382 | 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL, | ||
383 | 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL, | ||
384 | 0xed3498beUL | ||
385 | }, | ||
386 | { | ||
387 | 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL, | ||
388 | 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL, | ||
389 | 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL, | ||
390 | 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL, | ||
391 | 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL, | ||
392 | 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL, | ||
393 | 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL, | ||
394 | 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL, | ||
395 | 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL, | ||
396 | 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL, | ||
397 | 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL, | ||
398 | 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL, | ||
399 | 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL, | ||
400 | 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL, | ||
401 | 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL, | ||
402 | 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL, | ||
403 | 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL, | ||
404 | 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL, | ||
405 | 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL, | ||
406 | 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL, | ||
407 | 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL, | ||
408 | 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL, | ||
409 | 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL, | ||
410 | 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL, | ||
411 | 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL, | ||
412 | 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL, | ||
413 | 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL, | ||
414 | 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL, | ||
415 | 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL, | ||
416 | 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL, | ||
417 | 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL, | ||
418 | 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL, | ||
419 | 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL, | ||
420 | 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL, | ||
421 | 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL, | ||
422 | 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL, | ||
423 | 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL, | ||
424 | 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL, | ||
425 | 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL, | ||
426 | 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL, | ||
427 | 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL, | ||
428 | 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL, | ||
429 | 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL, | ||
430 | 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL, | ||
431 | 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL, | ||
432 | 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL, | ||
433 | 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL, | ||
434 | 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL, | ||
435 | 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL, | ||
436 | 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL, | ||
437 | 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL, | ||
438 | 0xf10605deUL | ||
439 | #endif | ||
440 | } | ||
441 | }; | ||
diff --git a/apps/plugins/imageviewer/png/inffast.c b/apps/plugins/imageviewer/png/inffast.c deleted file mode 100644 index bf96323ab2..0000000000 --- a/apps/plugins/imageviewer/png/inffast.c +++ /dev/null | |||
@@ -1,379 +0,0 @@ | |||
1 | /* inffast.c -- fast decoding | ||
2 | * Copyright (C) 1995-2004 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | #include "zutil.h" | ||
7 | #include "inftrees.h" | ||
8 | #include "inflate.h" | ||
9 | #include "inffast.h" | ||
10 | #include "plugin.h" | ||
11 | |||
12 | #ifndef ASMINF | ||
13 | |||
14 | extern struct inflate_state state; | ||
15 | extern unsigned char *memory_max; | ||
16 | |||
17 | /* Allow machine dependent optimization for post-increment or pre-increment. | ||
18 | Based on testing to date, | ||
19 | Pre-increment preferred for: | ||
20 | - PowerPC G3 (Adler) | ||
21 | - MIPS R5000 (Randers-Pehrson) | ||
22 | Post-increment preferred for: | ||
23 | - none | ||
24 | No measurable difference: | ||
25 | - Pentium III (Anderson) | ||
26 | - M68060 (Nikl) | ||
27 | */ | ||
28 | #ifdef POSTINC | ||
29 | # define OFF 0 | ||
30 | # define PUP(a) *(a)++ | ||
31 | #else | ||
32 | # define OFF 1 | ||
33 | # define PUP(a) *++(a) | ||
34 | #endif | ||
35 | |||
36 | /* | ||
37 | Decode literal, length, and distance codes and write out the resulting | ||
38 | literal and match bytes until either not enough input or output is | ||
39 | available, an end-of-block is encountered, or a data error is encountered. | ||
40 | When large enough input and output buffers are supplied to inflate(), for | ||
41 | example, a 16K input buffer and a 64K output buffer, more than 95% of the | ||
42 | inflate execution time is spent in this routine. | ||
43 | |||
44 | Entry assumptions: | ||
45 | |||
46 | state->mode == LEN | ||
47 | strm->avail_in >= 6 | ||
48 | strm->avail_out >= 258 | ||
49 | start >= strm->avail_out | ||
50 | state->bits < 8 | ||
51 | |||
52 | On return, state->mode is one of: | ||
53 | |||
54 | LEN -- ran out of enough output space or enough available input | ||
55 | TYPE -- reached end of block code, inflate() to interpret next block | ||
56 | BAD -- error in block data | ||
57 | |||
58 | Notes: | ||
59 | |||
60 | - The maximum input bits used by a length/distance pair is 15 bits for the | ||
61 | length code, 5 bits for the length extra, 15 bits for the distance code, | ||
62 | and 13 bits for the distance extra. This totals 48 bits, or six bytes. | ||
63 | Therefore if strm->avail_in >= 6, then there is enough input to avoid | ||
64 | checking for available input while decoding. | ||
65 | |||
66 | - The maximum bytes that a single length/distance pair can output is 258 | ||
67 | bytes, which is the maximum length that can be coded. inflate_fast() | ||
68 | requires strm->avail_out >= 258 for each loop to avoid checking for | ||
69 | output space. | ||
70 | */ | ||
71 | void inflate_fast(strm, start) | ||
72 | z_streamp strm; | ||
73 | unsigned start; /* inflate()'s starting value for strm->avail_out */ | ||
74 | { | ||
75 | //struct inflate_state FAR *state; | ||
76 | unsigned char FAR *in; /* local strm->next_in */ | ||
77 | unsigned char FAR *last; /* while in < last, enough input available */ | ||
78 | unsigned char FAR *out; /* local strm->next_out */ | ||
79 | unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ | ||
80 | unsigned char FAR *end; /* while out < end, enough space available */ | ||
81 | #ifdef INFLATE_STRICT | ||
82 | unsigned dmax; /* maximum distance from zlib header */ | ||
83 | #endif | ||
84 | unsigned wsize; /* window size or zero if not using window */ | ||
85 | unsigned whave; /* valid bytes in the window */ | ||
86 | unsigned write; /* window write index */ | ||
87 | unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */ | ||
88 | unsigned long hold; /* local strm->hold */ | ||
89 | unsigned bits; /* local strm->bits */ | ||
90 | code const FAR *lcode; /* local strm->lencode */ | ||
91 | code const FAR *dcode; /* local strm->distcode */ | ||
92 | unsigned lmask; /* mask for first level of length codes */ | ||
93 | unsigned dmask; /* mask for first level of distance codes */ | ||
94 | code this; /* retrieved table entry */ | ||
95 | unsigned op; /* code bits, operation, extra bits, or */ | ||
96 | /* window position, window bytes to copy */ | ||
97 | unsigned len; /* match length, unused bytes */ | ||
98 | unsigned dist; /* match distance */ | ||
99 | unsigned char FAR *from; /* where to copy match from */ | ||
100 | |||
101 | /* copy state to local variables */ | ||
102 | //state = (struct inflate_state FAR *)strm->state; | ||
103 | in = strm->next_in - OFF; | ||
104 | last = in + (strm->avail_in - 5); | ||
105 | out = strm->next_out - OFF; | ||
106 | beg = out - (start - strm->avail_out); | ||
107 | end = out + (strm->avail_out - 257); | ||
108 | #ifdef INFLATE_STRICT | ||
109 | dmax = state->dmax; | ||
110 | #endif | ||
111 | wsize = state.wsize; | ||
112 | whave = state.whave; | ||
113 | write = state.write; | ||
114 | window = state.window; | ||
115 | hold = state.hold; | ||
116 | bits = state.bits; | ||
117 | lcode = state.lencode; | ||
118 | dcode = state.distcode; | ||
119 | lmask = (1U << state.lenbits) - 1; | ||
120 | dmask = (1U << state.distbits) - 1; | ||
121 | |||
122 | /* decode literals and length/distances until end-of-block or not enough | ||
123 | input data or output space */ | ||
124 | do { | ||
125 | if (bits < 15) { | ||
126 | hold += (unsigned long)(PUP(in)) << bits; | ||
127 | bits += 8; | ||
128 | hold += (unsigned long)(PUP(in)) << bits; | ||
129 | bits += 8; | ||
130 | } | ||
131 | this = lcode[hold & lmask]; | ||
132 | dolen: | ||
133 | op = (unsigned)(this.bits); | ||
134 | hold >>= op; | ||
135 | bits -= op; | ||
136 | op = (unsigned)(this.op); | ||
137 | if (op == 0) { /* literal */ | ||
138 | //DEBUGF(this.val >= 0x20 && this.val < 0x7f ? | ||
139 | // "inflate: literal '%c'\n" : | ||
140 | // "inflate: literal 0x%02x\n", this.val); | ||
141 | if (out >= memory_max) { | ||
142 | strm->msg = (char *)"Out of memory"; | ||
143 | state.mode = ZMEM; | ||
144 | break; | ||
145 | } | ||
146 | PUP(out) = (unsigned char)(this.val); | ||
147 | } | ||
148 | else if (op & 16) { /* length base */ | ||
149 | len = (unsigned)(this.val); | ||
150 | op &= 15; /* number of extra bits */ | ||
151 | if (op) { | ||
152 | if (bits < op) { | ||
153 | hold += (unsigned long)(PUP(in)) << bits; | ||
154 | bits += 8; | ||
155 | } | ||
156 | len += (unsigned)hold & ((1U << op) - 1); | ||
157 | hold >>= op; | ||
158 | bits -= op; | ||
159 | } | ||
160 | //DEBUGF("inflate: length %u\n", len); | ||
161 | if (bits < 15) { | ||
162 | hold += (unsigned long)(PUP(in)) << bits; | ||
163 | bits += 8; | ||
164 | hold += (unsigned long)(PUP(in)) << bits; | ||
165 | bits += 8; | ||
166 | } | ||
167 | this = dcode[hold & dmask]; | ||
168 | dodist: | ||
169 | op = (unsigned)(this.bits); | ||
170 | hold >>= op; | ||
171 | bits -= op; | ||
172 | op = (unsigned)(this.op); | ||
173 | if (op & 16) { /* distance base */ | ||
174 | dist = (unsigned)(this.val); | ||
175 | op &= 15; /* number of extra bits */ | ||
176 | if (bits < op) { | ||
177 | hold += (unsigned long)(PUP(in)) << bits; | ||
178 | bits += 8; | ||
179 | if (bits < op) { | ||
180 | hold += (unsigned long)(PUP(in)) << bits; | ||
181 | bits += 8; | ||
182 | } | ||
183 | } | ||
184 | dist += (unsigned)hold & ((1U << op) - 1); | ||
185 | #ifdef INFLATE_STRICT | ||
186 | if (dist > dmax) { | ||
187 | strm->msg = (char *)"invalid distance too far back"; | ||
188 | state->mode = BAD; | ||
189 | break; | ||
190 | } | ||
191 | #endif | ||
192 | hold >>= op; | ||
193 | bits -= op; | ||
194 | //DEBUGF("inflate: distance %u\n", dist); | ||
195 | op = (unsigned)(out - beg); /* max distance in output */ | ||
196 | if (dist > op) { /* see if copy from window */ | ||
197 | op = dist - op; /* distance back in window */ | ||
198 | if (op > whave) { | ||
199 | strm->msg = (char *)"invalid distance too far back"; | ||
200 | state.mode = BAD; | ||
201 | break; | ||
202 | } | ||
203 | from = window - OFF; | ||
204 | if (write == 0) { /* very common case */ | ||
205 | from += wsize - op; | ||
206 | if (op < len) { /* some from window */ | ||
207 | len -= op; | ||
208 | do { | ||
209 | if (out >= memory_max) { | ||
210 | strm->msg = (char *)"Out of memory"; | ||
211 | state.mode = ZMEM; | ||
212 | break; | ||
213 | } | ||
214 | PUP(out) = PUP(from); | ||
215 | } while (--op); | ||
216 | from = out - dist; /* rest from output */ | ||
217 | } | ||
218 | } | ||
219 | else if (write < op) { /* wrap around window */ | ||
220 | from += wsize + write - op; | ||
221 | op -= write; | ||
222 | if (op < len) { /* some from end of window */ | ||
223 | len -= op; | ||
224 | do { | ||
225 | if (out >= memory_max) { | ||
226 | strm->msg = (char *)"Out of memory"; | ||
227 | state.mode = ZMEM; | ||
228 | break; | ||
229 | } | ||
230 | PUP(out) = PUP(from); | ||
231 | } while (--op); | ||
232 | from = window - OFF; | ||
233 | if (write < len) { /* some from start of window */ | ||
234 | op = write; | ||
235 | len -= op; | ||
236 | do { | ||
237 | if (out >= memory_max) { | ||
238 | strm->msg = (char *)"Out of memory"; | ||
239 | state.mode = ZMEM; | ||
240 | break; | ||
241 | } | ||
242 | PUP(out) = PUP(from); | ||
243 | } while (--op); | ||
244 | from = out - dist; /* rest from output */ | ||
245 | } | ||
246 | } | ||
247 | } | ||
248 | else { /* contiguous in window */ | ||
249 | from += write - op; | ||
250 | if (op < len) { /* some from window */ | ||
251 | len -= op; | ||
252 | do { | ||
253 | if (out >= memory_max) { | ||
254 | strm->msg = (char *)"Out of memory"; | ||
255 | state.mode = ZMEM; | ||
256 | break; | ||
257 | } | ||
258 | PUP(out) = PUP(from); | ||
259 | } while (--op); | ||
260 | from = out - dist; /* rest from output */ | ||
261 | } | ||
262 | } | ||
263 | while (len > 2) { | ||
264 | if (out >= memory_max-2) { | ||
265 | strm->msg = (char *)"Out of memory"; | ||
266 | state.mode = ZMEM; | ||
267 | break; | ||
268 | } | ||
269 | PUP(out) = PUP(from); | ||
270 | PUP(out) = PUP(from); | ||
271 | PUP(out) = PUP(from); | ||
272 | len -= 3; | ||
273 | } | ||
274 | if (len) { | ||
275 | if (out >= memory_max) { | ||
276 | strm->msg = (char *)"Out of memory"; | ||
277 | state.mode = ZMEM; | ||
278 | break; | ||
279 | } | ||
280 | PUP(out) = PUP(from); | ||
281 | if (len > 1) { | ||
282 | if (out >= memory_max) { | ||
283 | strm->msg = (char *)"Out of memory"; | ||
284 | state.mode = ZMEM; | ||
285 | break; | ||
286 | } | ||
287 | PUP(out) = PUP(from); | ||
288 | } | ||
289 | } | ||
290 | } | ||
291 | else { | ||
292 | from = out - dist; /* copy direct from output */ | ||
293 | do { /* minimum length is three */ | ||
294 | if (out >= memory_max-2) { | ||
295 | strm->msg = (char *)"Out of memory"; | ||
296 | state.mode = ZMEM; | ||
297 | break; | ||
298 | } | ||
299 | PUP(out) = PUP(from); | ||
300 | PUP(out) = PUP(from); | ||
301 | PUP(out) = PUP(from); | ||
302 | len -= 3; | ||
303 | } while (len > 2); | ||
304 | if (len) { | ||
305 | if (out >= memory_max) { | ||
306 | strm->msg = (char *)"Out of memory"; | ||
307 | state.mode = ZMEM; | ||
308 | break; | ||
309 | } | ||
310 | PUP(out) = PUP(from); | ||
311 | if (len > 1) { | ||
312 | if (out >= memory_max) { | ||
313 | strm->msg = (char *)"Out of memory"; | ||
314 | state.mode = ZMEM; | ||
315 | break; | ||
316 | } | ||
317 | PUP(out) = PUP(from); | ||
318 | } | ||
319 | } | ||
320 | } | ||
321 | } | ||
322 | else if ((op & 64) == 0) { /* 2nd level distance code */ | ||
323 | this = dcode[this.val + (hold & ((1U << op) - 1))]; | ||
324 | goto dodist; | ||
325 | } | ||
326 | else { | ||
327 | strm->msg = (char *)"invalid distance code"; | ||
328 | state.mode = BAD; | ||
329 | break; | ||
330 | } | ||
331 | } | ||
332 | else if ((op & 64) == 0) { /* 2nd level length code */ | ||
333 | this = lcode[this.val + (hold & ((1U << op) - 1))]; | ||
334 | goto dolen; | ||
335 | } | ||
336 | else if (op & 32) { /* end-of-block */ | ||
337 | //DEBUGF("inflate: end of block\n"); | ||
338 | state.mode = TYPE; | ||
339 | break; | ||
340 | } | ||
341 | else { | ||
342 | strm->msg = (char *)"invalid literal/length code"; | ||
343 | state.mode = BAD; | ||
344 | break; | ||
345 | } | ||
346 | } while (in < last && out < end); | ||
347 | |||
348 | /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ | ||
349 | len = bits >> 3; | ||
350 | in -= len; | ||
351 | bits -= len << 3; | ||
352 | hold &= (1U << bits) - 1; | ||
353 | |||
354 | /* update state and return */ | ||
355 | strm->next_in = in + OFF; | ||
356 | strm->next_out = out + OFF; | ||
357 | strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); | ||
358 | strm->avail_out = (unsigned)(out < end ? | ||
359 | 257 + (end - out) : 257 - (out - end)); | ||
360 | state.hold = hold; | ||
361 | state.bits = bits; | ||
362 | return; | ||
363 | } | ||
364 | |||
365 | /* | ||
366 | inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): | ||
367 | - Using bit fields for code structure | ||
368 | - Different op definition to avoid & for extra bits (do & for table bits) | ||
369 | - Three separate decoding do-loops for direct, window, and write == 0 | ||
370 | - Special case for distance > 1 copies to do overlapped load and store copy | ||
371 | - Explicit branch predictions (based on measured branch probabilities) | ||
372 | - Deferring match copy and interspersed it with decoding subsequent codes | ||
373 | - Swapping literal/length else | ||
374 | - Swapping window/direct else | ||
375 | - Larger unrolled copy loops (three is about right) | ||
376 | - Moving len -= 3 statement into middle of loop | ||
377 | */ | ||
378 | |||
379 | #endif /* !ASMINF */ | ||
diff --git a/apps/plugins/imageviewer/png/inffast.h b/apps/plugins/imageviewer/png/inffast.h deleted file mode 100644 index 1e88d2d97b..0000000000 --- a/apps/plugins/imageviewer/png/inffast.h +++ /dev/null | |||
@@ -1,11 +0,0 @@ | |||
1 | /* inffast.h -- header to use inffast.c | ||
2 | * Copyright (C) 1995-2003 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* WARNING: this file should *not* be used by applications. It is | ||
7 | part of the implementation of the compression library and is | ||
8 | subject to change. Applications should only use zlib.h. | ||
9 | */ | ||
10 | |||
11 | void inflate_fast OF((z_streamp strm, unsigned start)); | ||
diff --git a/apps/plugins/imageviewer/png/inffixed.h b/apps/plugins/imageviewer/png/inffixed.h deleted file mode 100644 index 75ed4b5978..0000000000 --- a/apps/plugins/imageviewer/png/inffixed.h +++ /dev/null | |||
@@ -1,94 +0,0 @@ | |||
1 | /* inffixed.h -- table for decoding fixed codes | ||
2 | * Generated automatically by makefixed(). | ||
3 | */ | ||
4 | |||
5 | /* WARNING: this file should *not* be used by applications. It | ||
6 | is part of the implementation of the compression library and | ||
7 | is subject to change. Applications should only use zlib.h. | ||
8 | */ | ||
9 | |||
10 | static const code lenfix[512] = { | ||
11 | {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48}, | ||
12 | {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128}, | ||
13 | {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59}, | ||
14 | {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176}, | ||
15 | {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20}, | ||
16 | {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100}, | ||
17 | {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8}, | ||
18 | {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216}, | ||
19 | {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76}, | ||
20 | {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114}, | ||
21 | {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2}, | ||
22 | {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148}, | ||
23 | {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42}, | ||
24 | {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86}, | ||
25 | {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15}, | ||
26 | {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236}, | ||
27 | {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62}, | ||
28 | {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142}, | ||
29 | {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31}, | ||
30 | {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162}, | ||
31 | {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25}, | ||
32 | {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105}, | ||
33 | {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4}, | ||
34 | {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202}, | ||
35 | {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69}, | ||
36 | {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125}, | ||
37 | {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13}, | ||
38 | {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195}, | ||
39 | {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35}, | ||
40 | {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91}, | ||
41 | {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19}, | ||
42 | {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246}, | ||
43 | {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55}, | ||
44 | {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135}, | ||
45 | {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99}, | ||
46 | {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190}, | ||
47 | {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16}, | ||
48 | {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96}, | ||
49 | {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6}, | ||
50 | {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209}, | ||
51 | {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72}, | ||
52 | {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116}, | ||
53 | {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4}, | ||
54 | {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153}, | ||
55 | {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44}, | ||
56 | {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82}, | ||
57 | {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11}, | ||
58 | {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229}, | ||
59 | {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58}, | ||
60 | {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138}, | ||
61 | {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51}, | ||
62 | {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173}, | ||
63 | {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30}, | ||
64 | {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110}, | ||
65 | {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0}, | ||
66 | {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195}, | ||
67 | {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65}, | ||
68 | {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121}, | ||
69 | {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9}, | ||
70 | {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258}, | ||
71 | {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37}, | ||
72 | {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93}, | ||
73 | {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23}, | ||
74 | {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251}, | ||
75 | {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51}, | ||
76 | {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131}, | ||
77 | {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67}, | ||
78 | {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183}, | ||
79 | {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23}, | ||
80 | {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103}, | ||
81 | {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9}, | ||
82 | {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223}, | ||
83 | {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79}, | ||
84 | {0,9,255} | ||
85 | }; | ||
86 | |||
87 | static const code distfix[32] = { | ||
88 | {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025}, | ||
89 | {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193}, | ||
90 | {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385}, | ||
91 | {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577}, | ||
92 | {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073}, | ||
93 | {22,5,193},{64,5,0} | ||
94 | }; | ||
diff --git a/apps/plugins/imageviewer/png/inflate.c b/apps/plugins/imageviewer/png/inflate.c deleted file mode 100644 index beec74044b..0000000000 --- a/apps/plugins/imageviewer/png/inflate.c +++ /dev/null | |||
@@ -1,1332 +0,0 @@ | |||
1 | /* inflate.c -- zlib decompression | ||
2 | * Copyright (C) 1995-2005 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* | ||
7 | * Change history: | ||
8 | * | ||
9 | * 1.2.beta0 24 Nov 2002 | ||
10 | * - First version -- complete rewrite of inflate to simplify code, avoid | ||
11 | * creation of window when not needed, minimize use of window when it is | ||
12 | * needed, make inffast.c even faster, implement gzip decoding, and to | ||
13 | * improve code readability and style over the previous zlib inflate code | ||
14 | * | ||
15 | * 1.2.beta1 25 Nov 2002 | ||
16 | * - Use pointers for available input and output checking in inffast.c | ||
17 | * - Remove input and output counters in inffast.c | ||
18 | * - Change inffast.c entry and loop from avail_in >= 7 to >= 6 | ||
19 | * - Remove unnecessary second byte pull from length extra in inffast.c | ||
20 | * - Unroll direct copy to three copies per loop in inffast.c | ||
21 | * | ||
22 | * 1.2.beta2 4 Dec 2002 | ||
23 | * - Change external routine names to reduce potential conflicts | ||
24 | * - Correct filename to inffixed.h for fixed tables in inflate.c | ||
25 | * - Make hbuf[] unsigned char to match parameter type in inflate.c | ||
26 | * - Change strm->next_out[-state.offset] to *(strm->next_out - state.offset) | ||
27 | * to avoid negation problem on Alphas (64 bit) in inflate.c | ||
28 | * | ||
29 | * 1.2.beta3 22 Dec 2002 | ||
30 | * - Add comments on state.bits assertion in inffast.c | ||
31 | * - Add comments on op field in inftrees.h | ||
32 | * - Fix bug in reuse of allocated window after inflateReset() | ||
33 | * - Remove bit fields--back to byte structure for speed | ||
34 | * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths | ||
35 | * - Change post-increments to pre-increments in inflate_fast(), PPC biased? | ||
36 | * - Add compile time option, POSTINC, to use post-increments instead (Intel?) | ||
37 | * - Make MATCH copy in inflate() much faster for when inflate_fast() not used | ||
38 | * - Use local copies of stream next and avail values, as well as local bit | ||
39 | * buffer and bit count in inflate()--for speed when inflate_fast() not used | ||
40 | * | ||
41 | * 1.2.beta4 1 Jan 2003 | ||
42 | * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings | ||
43 | * - Move a comment on output buffer sizes from inffast.c to inflate.c | ||
44 | * - Add comments in inffast.c to introduce the inflate_fast() routine | ||
45 | * - Rearrange window copies in inflate_fast() for speed and simplification | ||
46 | * - Unroll last copy for window match in inflate_fast() | ||
47 | * - Use local copies of window variables in inflate_fast() for speed | ||
48 | * - Pull out common write == 0 case for speed in inflate_fast() | ||
49 | * - Make op and len in inflate_fast() unsigned for consistency | ||
50 | * - Add FAR to lcode and dcode declarations in inflate_fast() | ||
51 | * - Simplified bad distance check in inflate_fast() | ||
52 | * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new | ||
53 | * source file infback.c to provide a call-back interface to inflate for | ||
54 | * programs like gzip and unzip -- uses window as output buffer to avoid | ||
55 | * window copying | ||
56 | * | ||
57 | * 1.2.beta5 1 Jan 2003 | ||
58 | * - Improved inflateBack() interface to allow the caller to provide initial | ||
59 | * input in strm. | ||
60 | * - Fixed stored blocks bug in inflateBack() | ||
61 | * | ||
62 | * 1.2.beta6 4 Jan 2003 | ||
63 | * - Added comments in inffast.c on effectiveness of POSTINC | ||
64 | * - Typecasting all around to reduce compiler warnings | ||
65 | * - Changed loops from while (1) or do {} while (1) to for (;;), again to | ||
66 | * make compilers happy | ||
67 | * - Changed type of window in inflateBackInit() to unsigned char * | ||
68 | * | ||
69 | * 1.2.beta7 27 Jan 2003 | ||
70 | * - Changed many types to unsigned or unsigned short to avoid warnings | ||
71 | * - Added inflateCopy() function | ||
72 | * | ||
73 | * 1.2.0 9 Mar 2003 | ||
74 | * - Changed inflateBack() interface to provide separate opaque descriptors | ||
75 | * for the in() and out() functions | ||
76 | * - Changed inflateBack() argument and in_func typedef to swap the length | ||
77 | * and buffer address return values for the input function | ||
78 | * - Check next_in and next_out for Z_NULL on entry to inflate() | ||
79 | * | ||
80 | * The history for versions after 1.2.0 are in ChangeLog in zlib distribution. | ||
81 | */ | ||
82 | |||
83 | #include "zutil.h" | ||
84 | #include "inftrees.h" | ||
85 | #include "inflate.h" | ||
86 | #include "inffast.h" | ||
87 | #include "plugin.h" | ||
88 | #include "png.h" | ||
89 | |||
90 | #ifdef MAKEFIXED | ||
91 | # ifndef BUILDFIXED | ||
92 | # define BUILDFIXED | ||
93 | # endif | ||
94 | #endif | ||
95 | |||
96 | struct inflate_state state; | ||
97 | |||
98 | /* function prototypes */ | ||
99 | local void fixedtables OF((void)); | ||
100 | local int updatewindow OF((z_streamp strm, unsigned out)); | ||
101 | #ifdef BUILDFIXED | ||
102 | void makefixed OF((void)); | ||
103 | #endif | ||
104 | local unsigned syncsearch OF((unsigned FAR *have, unsigned char FAR *buf, | ||
105 | unsigned len)); | ||
106 | |||
107 | int ZEXPORT inflateReset(strm) | ||
108 | z_streamp strm; | ||
109 | { | ||
110 | //struct inflate_state FAR *state; | ||
111 | |||
112 | if (strm == Z_NULL) return Z_STREAM_ERROR; | ||
113 | //state = strm->state; | ||
114 | strm->total_in = strm->total_out = state.total = 0; | ||
115 | strm->msg = Z_NULL; | ||
116 | strm->adler = 1; /* to support ill-conceived Java test suite */ | ||
117 | state.mode = HEAD; | ||
118 | state.last = 0; | ||
119 | state.havedict = 0; | ||
120 | state.dmax = 32768U; | ||
121 | state.head = Z_NULL; | ||
122 | state.wsize = 0; | ||
123 | state.whave = 0; | ||
124 | state.write = 0; | ||
125 | state.hold = 0; | ||
126 | state.bits = 0; | ||
127 | state.lencode = state.distcode = state.next = state.codes; | ||
128 | //DEBUGF("inflate: reset\n"); | ||
129 | return Z_OK; | ||
130 | } | ||
131 | |||
132 | int ZEXPORT inflatePrime(strm, bits, value) | ||
133 | z_streamp strm; | ||
134 | int bits; | ||
135 | int value; | ||
136 | { | ||
137 | //struct inflate_state FAR *state; | ||
138 | |||
139 | if (strm == Z_NULL) return Z_STREAM_ERROR; | ||
140 | //state = (struct inflate_state FAR *)strm->state; | ||
141 | if (bits > 16 || state.bits + bits > 32) return Z_STREAM_ERROR; | ||
142 | value &= (1L << bits) - 1; | ||
143 | state.hold += value << state.bits; | ||
144 | state.bits += bits; | ||
145 | return Z_OK; | ||
146 | } | ||
147 | |||
148 | int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) | ||
149 | z_streamp strm; | ||
150 | int windowBits; | ||
151 | const char *version; | ||
152 | int stream_size; | ||
153 | { | ||
154 | //struct inflate_state FAR *state; | ||
155 | |||
156 | if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || | ||
157 | stream_size != (int)(sizeof(z_stream))) | ||
158 | return Z_VERSION_ERROR; | ||
159 | if (strm == Z_NULL) return Z_STREAM_ERROR; | ||
160 | strm->msg = Z_NULL; /* in case we return an error */ | ||
161 | //if (strm->zalloc == (alloc_func)0) { | ||
162 | // strm->zalloc = zcalloc; | ||
163 | // strm->opaque = (voidpf)0; | ||
164 | //} | ||
165 | //if (strm->zfree == (free_func)0) strm->zfree = zcfree; | ||
166 | //state = (struct inflate_state FAR *) | ||
167 | // ZALLOC(strm, 1, sizeof(struct inflate_state)); | ||
168 | //if (state == Z_NULL) return Z_MEM_ERROR; | ||
169 | //DEBUGF("inflate: allocated\n"); | ||
170 | //strm->state = (struct internal_state FAR *)state; | ||
171 | if (windowBits < 0) { | ||
172 | state.wrap = 0; | ||
173 | windowBits = -windowBits; | ||
174 | } | ||
175 | else { | ||
176 | state.wrap = (windowBits >> 4) + 1; | ||
177 | #ifdef GUNZIP | ||
178 | if (windowBits < 48) windowBits &= 15; | ||
179 | #endif | ||
180 | } | ||
181 | if (windowBits < 8 || windowBits > 15) { | ||
182 | //ZFREE(strm, state); | ||
183 | //strm->state = Z_NULL; | ||
184 | return Z_STREAM_ERROR; | ||
185 | } | ||
186 | state.wbits = (unsigned)windowBits; | ||
187 | state.window = Z_NULL; | ||
188 | return inflateReset(strm); | ||
189 | } | ||
190 | |||
191 | int ZEXPORT inflateInit_(strm, version, stream_size) | ||
192 | z_streamp strm; | ||
193 | const char *version; | ||
194 | int stream_size; | ||
195 | { | ||
196 | return inflateInit2_(strm, DEF_WBITS, version, stream_size); | ||
197 | } | ||
198 | |||
199 | /* | ||
200 | Return state with length and distance decoding tables and index sizes set to | ||
201 | fixed code decoding. Normally this returns fixed tables from inffixed.h. | ||
202 | If BUILDFIXED is defined, then instead this routine builds the tables the | ||
203 | first time it's called, and returns those tables the first time and | ||
204 | thereafter. This reduces the size of the code by about 2K bytes, in | ||
205 | exchange for a little execution time. However, BUILDFIXED should not be | ||
206 | used for threaded applications, since the rewriting of the tables and virgin | ||
207 | may not be thread-safe. | ||
208 | */ | ||
209 | local void fixedtables(void) | ||
210 | //struct inflate_state FAR *state; | ||
211 | { | ||
212 | #ifdef BUILDFIXED | ||
213 | static int virgin = 1; | ||
214 | static code *lenfix, *distfix; | ||
215 | static code fixed[544]; | ||
216 | |||
217 | /* build fixed huffman tables if first call (may not be thread safe) */ | ||
218 | if (virgin) { | ||
219 | unsigned sym, bits; | ||
220 | static code *next; | ||
221 | |||
222 | /* literal/length table */ | ||
223 | sym = 0; | ||
224 | while (sym < 144) state.lens[sym++] = 8; | ||
225 | while (sym < 256) state.lens[sym++] = 9; | ||
226 | while (sym < 280) state.lens[sym++] = 7; | ||
227 | while (sym < 288) state.lens[sym++] = 8; | ||
228 | next = fixed; | ||
229 | lenfix = next; | ||
230 | bits = 9; | ||
231 | inflate_table(LENS, state.lens, 288, &(next), &(bits), state.work); | ||
232 | |||
233 | /* distance table */ | ||
234 | sym = 0; | ||
235 | while (sym < 32) state.lens[sym++] = 5; | ||
236 | distfix = next; | ||
237 | bits = 5; | ||
238 | inflate_table(DISTS, state.lens, 32, &(next), &(bits), state.work); | ||
239 | |||
240 | /* do this just once */ | ||
241 | virgin = 0; | ||
242 | } | ||
243 | #else /* !BUILDFIXED */ | ||
244 | # include "inffixed.h" | ||
245 | #endif /* BUILDFIXED */ | ||
246 | state.lencode = lenfix; | ||
247 | state.lenbits = 9; | ||
248 | state.distcode = distfix; | ||
249 | state.distbits = 5; | ||
250 | } | ||
251 | |||
252 | #ifdef MAKEFIXED | ||
253 | #include <stdio.h> | ||
254 | |||
255 | /* | ||
256 | Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also | ||
257 | defines BUILDFIXED, so the tables are built on the fly. makefixed() writes | ||
258 | those tables to stdout, which would be piped to inffixed.h. A small program | ||
259 | can simply call makefixed to do this: | ||
260 | |||
261 | void makefixed(void); | ||
262 | |||
263 | int main(void) | ||
264 | { | ||
265 | makefixed(); | ||
266 | return 0; | ||
267 | } | ||
268 | |||
269 | Then that can be linked with zlib built with MAKEFIXED defined and run: | ||
270 | |||
271 | a.out > inffixed.h | ||
272 | */ | ||
273 | void makefixed() | ||
274 | { | ||
275 | unsigned low, size; | ||
276 | struct inflate_state state; | ||
277 | |||
278 | fixedtables(&state); | ||
279 | puts(" /* inffixed.h -- table for decoding fixed codes"); | ||
280 | puts(" * Generated automatically by makefixed()."); | ||
281 | puts(" */"); | ||
282 | puts(""); | ||
283 | puts(" /* WARNING: this file should *not* be used by applications."); | ||
284 | puts(" It is part of the implementation of this library and is"); | ||
285 | puts(" subject to change. Applications should only use zlib.h."); | ||
286 | puts(" */"); | ||
287 | puts(""); | ||
288 | size = 1U << 9; | ||
289 | printf(" static const code lenfix[%u] = {", size); | ||
290 | low = 0; | ||
291 | for (;;) { | ||
292 | if ((low % 7) == 0) printf("\n "); | ||
293 | printf("{%u,%u,%d}", state.lencode[low].op, state.lencode[low].bits, | ||
294 | state.lencode[low].val); | ||
295 | if (++low == size) break; | ||
296 | putchar(','); | ||
297 | } | ||
298 | puts("\n };"); | ||
299 | size = 1U << 5; | ||
300 | printf("\n static const code distfix[%u] = {", size); | ||
301 | low = 0; | ||
302 | for (;;) { | ||
303 | if ((low % 6) == 0) printf("\n "); | ||
304 | printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, | ||
305 | state.distcode[low].val); | ||
306 | if (++low == size) break; | ||
307 | putchar(','); | ||
308 | } | ||
309 | puts("\n };"); | ||
310 | } | ||
311 | #endif /* MAKEFIXED */ | ||
312 | |||
313 | /* | ||
314 | Update the window with the last wsize (normally 32K) bytes written before | ||
315 | returning. If window does not exist yet, create it. This is only called | ||
316 | when a window is already in use, or when output has been written during this | ||
317 | inflate call, but the end of the deflate stream has not been reached yet. | ||
318 | It is also called to create a window for dictionary data when a dictionary | ||
319 | is loaded. | ||
320 | |||
321 | Providing output buffers larger than 32K to inflate() should provide a speed | ||
322 | advantage, since only the last 32K of output is copied to the sliding window | ||
323 | upon return from inflate(), and since all distances after the first 32K of | ||
324 | output will fall in the output data, making match copies simpler and faster. | ||
325 | The advantage may be dependent on the size of the processor's data caches. | ||
326 | */ | ||
327 | local int updatewindow(strm, out) | ||
328 | z_streamp strm; | ||
329 | unsigned out; | ||
330 | { | ||
331 | //struct inflate_state FAR *state; | ||
332 | unsigned copy, dist; | ||
333 | |||
334 | //state = (struct inflate_state FAR *)strm->state; | ||
335 | |||
336 | /* if it hasn't been done already, allocate space for the window */ | ||
337 | if (state.window == Z_NULL) { | ||
338 | state.window = (unsigned char FAR *) | ||
339 | ZALLOC(strm, 1U << state.wbits, | ||
340 | sizeof(unsigned char)); | ||
341 | if (state.window == Z_NULL) return 1; | ||
342 | } | ||
343 | |||
344 | /* if window not in use yet, initialize */ | ||
345 | if (state.wsize == 0) { | ||
346 | state.wsize = 1U << state.wbits; | ||
347 | state.write = 0; | ||
348 | state.whave = 0; | ||
349 | } | ||
350 | |||
351 | /* copy state.wsize or less output bytes into the circular window */ | ||
352 | copy = out - strm->avail_out; | ||
353 | if (copy >= state.wsize) { | ||
354 | zmemcpy(state.window, strm->next_out - state.wsize, state.wsize); | ||
355 | state.write = 0; | ||
356 | state.whave = state.wsize; | ||
357 | } | ||
358 | else { | ||
359 | dist = state.wsize - state.write; | ||
360 | if (dist > copy) dist = copy; | ||
361 | zmemcpy(state.window + state.write, strm->next_out - copy, dist); | ||
362 | copy -= dist; | ||
363 | if (copy) { | ||
364 | zmemcpy(state.window, strm->next_out - copy, copy); | ||
365 | state.write = copy; | ||
366 | state.whave = state.wsize; | ||
367 | } | ||
368 | else { | ||
369 | state.write += dist; | ||
370 | if (state.write == state.wsize) state.write = 0; | ||
371 | if (state.whave < state.wsize) state.whave += dist; | ||
372 | } | ||
373 | } | ||
374 | return 0; | ||
375 | } | ||
376 | |||
377 | /* Macros for inflate(): */ | ||
378 | |||
379 | /* check function to use adler32() for zlib or crc32() for gzip */ | ||
380 | #ifdef GUNZIP | ||
381 | # define UPDATE(check, buf, len) \ | ||
382 | (state.flags ? crc32(check, buf, len) : adler32(check, buf, len)) | ||
383 | #else | ||
384 | # define UPDATE(check, buf, len) adler32(check, buf, len) | ||
385 | #endif | ||
386 | |||
387 | /* check macros for header crc */ | ||
388 | #ifdef GUNZIP | ||
389 | # define CRC2(check, word) \ | ||
390 | do { \ | ||
391 | hbuf[0] = (unsigned char)(word); \ | ||
392 | hbuf[1] = (unsigned char)((word) >> 8); \ | ||
393 | check = crc32(check, hbuf, 2); \ | ||
394 | } while (0) | ||
395 | |||
396 | # define CRC4(check, word) \ | ||
397 | do { \ | ||
398 | hbuf[0] = (unsigned char)(word); \ | ||
399 | hbuf[1] = (unsigned char)((word) >> 8); \ | ||
400 | hbuf[2] = (unsigned char)((word) >> 16); \ | ||
401 | hbuf[3] = (unsigned char)((word) >> 24); \ | ||
402 | check = crc32(check, hbuf, 4); \ | ||
403 | } while (0) | ||
404 | #endif | ||
405 | |||
406 | /* Load registers with state in inflate() for speed */ | ||
407 | #define LOAD() \ | ||
408 | do { \ | ||
409 | put = strm->next_out; \ | ||
410 | left = strm->avail_out; \ | ||
411 | next = strm->next_in; \ | ||
412 | have = strm->avail_in; \ | ||
413 | hold = state.hold; \ | ||
414 | bits = state.bits; \ | ||
415 | } while (0) | ||
416 | |||
417 | /* Restore state from registers in inflate() */ | ||
418 | #define RESTORE() \ | ||
419 | do { \ | ||
420 | strm->next_out = put; \ | ||
421 | strm->avail_out = left; \ | ||
422 | strm->next_in = next; \ | ||
423 | strm->avail_in = have; \ | ||
424 | state.hold = hold; \ | ||
425 | state.bits = bits; \ | ||
426 | } while (0) | ||
427 | |||
428 | /* Clear the input bit accumulator */ | ||
429 | #define INITBITS() \ | ||
430 | do { \ | ||
431 | hold = 0; \ | ||
432 | bits = 0; \ | ||
433 | } while (0) | ||
434 | |||
435 | /* Get a byte of input into the bit accumulator, or return from inflate() | ||
436 | if there is no input available. */ | ||
437 | #define PULLBYTE() \ | ||
438 | do { \ | ||
439 | if (have == 0) goto inf_leave; \ | ||
440 | have--; \ | ||
441 | hold += (unsigned long)(*next++) << bits; \ | ||
442 | bits += 8; \ | ||
443 | } while (0) | ||
444 | |||
445 | /* Assure that there are at least n bits in the bit accumulator. If there is | ||
446 | not enough available input to do that, then return from inflate(). */ | ||
447 | #define NEEDBITS(n) \ | ||
448 | do { \ | ||
449 | while (bits < (unsigned)(n)) \ | ||
450 | PULLBYTE(); \ | ||
451 | } while (0) | ||
452 | |||
453 | /* Return the low n bits of the bit accumulator (n < 16) */ | ||
454 | #define BITS(n) \ | ||
455 | ((unsigned)hold & ((1U << (n)) - 1)) | ||
456 | |||
457 | /* Remove n bits from the bit accumulator */ | ||
458 | #define DROPBITS(n) \ | ||
459 | do { \ | ||
460 | hold >>= (n); \ | ||
461 | bits -= (unsigned)(n); \ | ||
462 | } while (0) | ||
463 | |||
464 | /* Remove zero to seven bits as needed to go to a byte boundary */ | ||
465 | #define BYTEBITS() \ | ||
466 | do { \ | ||
467 | hold >>= bits & 7; \ | ||
468 | bits -= bits & 7; \ | ||
469 | } while (0) | ||
470 | |||
471 | /* Reverse the bytes in a 32-bit value */ | ||
472 | #define REVERSE(q) \ | ||
473 | ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ | ||
474 | (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) | ||
475 | |||
476 | /* | ||
477 | inflate() uses a state machine to process as much input data and generate as | ||
478 | much output data as possible before returning. The state machine is | ||
479 | structured roughly as follows: | ||
480 | |||
481 | for (;;) switch (state) { | ||
482 | ... | ||
483 | case STATEn: | ||
484 | if (not enough input data or output space to make progress) | ||
485 | return; | ||
486 | ... make progress ... | ||
487 | state = STATEm; | ||
488 | break; | ||
489 | ... | ||
490 | } | ||
491 | |||
492 | so when inflate() is called again, the same case is attempted again, and | ||
493 | if the appropriate resources are provided, the machine proceeds to the | ||
494 | next state. The NEEDBITS() macro is usually the way the state evaluates | ||
495 | whether it can proceed or should return. NEEDBITS() does the return if | ||
496 | the requested bits are not available. The typical use of the BITS macros | ||
497 | is: | ||
498 | |||
499 | NEEDBITS(n); | ||
500 | ... do something with BITS(n) ... | ||
501 | DROPBITS(n); | ||
502 | |||
503 | where NEEDBITS(n) either returns from inflate() if there isn't enough | ||
504 | input left to load n bits into the accumulator, or it continues. BITS(n) | ||
505 | gives the low n bits in the accumulator. When done, DROPBITS(n) drops | ||
506 | the low n bits off the accumulator. INITBITS() clears the accumulator | ||
507 | and sets the number of available bits to zero. BYTEBITS() discards just | ||
508 | enough bits to put the accumulator on a byte boundary. After BYTEBITS() | ||
509 | and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. | ||
510 | |||
511 | NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return | ||
512 | if there is no input available. The decoding of variable length codes uses | ||
513 | PULLBYTE() directly in order to pull just enough bytes to decode the next | ||
514 | code, and no more. | ||
515 | |||
516 | Some states loop until they get enough input, making sure that enough | ||
517 | state information is maintained to continue the loop where it left off | ||
518 | if NEEDBITS() returns in the loop. For example, want, need, and keep | ||
519 | would all have to actually be part of the saved state in case NEEDBITS() | ||
520 | returns: | ||
521 | |||
522 | case STATEw: | ||
523 | while (want < need) { | ||
524 | NEEDBITS(n); | ||
525 | keep[want++] = BITS(n); | ||
526 | DROPBITS(n); | ||
527 | } | ||
528 | state = STATEx; | ||
529 | case STATEx: | ||
530 | |||
531 | As shown above, if the next state is also the next case, then the break | ||
532 | is omitted. | ||
533 | |||
534 | A state may also return if there is not enough output space available to | ||
535 | complete that state. Those states are copying stored data, writing a | ||
536 | literal byte, and copying a matching string. | ||
537 | |||
538 | When returning, a "goto inf_leave" is used to update the total counters, | ||
539 | update the check value, and determine whether any progress has been made | ||
540 | during that inflate() call in order to return the proper return code. | ||
541 | Progress is defined as a change in either strm->avail_in or strm->avail_out. | ||
542 | When there is a window, goto inf_leave will update the window with the last | ||
543 | output written. If a goto inf_leave occurs in the middle of decompression | ||
544 | and there is no window currently, goto inf_leave will create one and copy | ||
545 | output to the window for the next call of inflate(). | ||
546 | |||
547 | In this implementation, the flush parameter of inflate() only affects the | ||
548 | return code (per zlib.h). inflate() always writes as much as possible to | ||
549 | strm->next_out, given the space available and the provided input--the effect | ||
550 | documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers | ||
551 | the allocation of and copying into a sliding window until necessary, which | ||
552 | provides the effect documented in zlib.h for Z_FINISH when the entire input | ||
553 | stream available. So the only thing the flush parameter actually does is: | ||
554 | when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it | ||
555 | will return Z_BUF_ERROR if it has not reached the end of the stream. | ||
556 | */ | ||
557 | |||
558 | int ZEXPORT inflate(strm, flush) | ||
559 | z_streamp strm; | ||
560 | int flush; | ||
561 | { | ||
562 | //struct inflate_state FAR *state; | ||
563 | unsigned char FAR *next; /* next input */ | ||
564 | unsigned char FAR *put; /* next output */ | ||
565 | unsigned have, left; /* available input and output */ | ||
566 | unsigned long hold; /* bit buffer */ | ||
567 | unsigned bits; /* bits in bit buffer */ | ||
568 | unsigned in, out; /* save starting available input and output */ | ||
569 | unsigned copy; /* number of stored or match bytes to copy */ | ||
570 | unsigned char FAR *from; /* where to copy match bytes from */ | ||
571 | code this; /* current decoding table entry */ | ||
572 | code last; /* parent table entry */ | ||
573 | unsigned len; /* length to copy for repeats, bits to drop */ | ||
574 | int ret; /* return code */ | ||
575 | #ifdef GUNZIP | ||
576 | unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ | ||
577 | #endif | ||
578 | static const unsigned short order[19] = /* permutation of code lengths */ | ||
579 | {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; | ||
580 | |||
581 | if (strm == Z_NULL || strm->next_out == Z_NULL || | ||
582 | (strm->next_in == Z_NULL && strm->avail_in != 0)) | ||
583 | return Z_STREAM_ERROR; | ||
584 | |||
585 | uInt insize = strm->avail_in; | ||
586 | |||
587 | //state = (struct inflate_state FAR *)strm->state; | ||
588 | if (state.mode == TYPE) state.mode = TYPEDO; /* skip check */ | ||
589 | LOAD(); | ||
590 | in = have; | ||
591 | out = left; | ||
592 | ret = Z_OK; | ||
593 | for (;;) { | ||
594 | switch (state.mode) { | ||
595 | case HEAD: | ||
596 | if (state.wrap == 0) { | ||
597 | state.mode = TYPEDO; | ||
598 | break; | ||
599 | } | ||
600 | NEEDBITS(16); | ||
601 | #ifdef GUNZIP | ||
602 | if ((state.wrap & 2) && hold == 0x8b1f) { /* gzip header */ | ||
603 | state.check = crc32(0L, Z_NULL, 0); | ||
604 | CRC2(state.check, hold); | ||
605 | INITBITS(); | ||
606 | state.mode = FLAGS; | ||
607 | break; | ||
608 | } | ||
609 | state.flags = 0; /* expect zlib header */ | ||
610 | if (state.head != Z_NULL) | ||
611 | state.head->done = -1; | ||
612 | if (!(state.wrap & 1) || /* check if zlib header allowed */ | ||
613 | #else | ||
614 | if ( | ||
615 | #endif | ||
616 | ((BITS(8) << 8) + (hold >> 8)) % 31) { | ||
617 | strm->msg = (char *)"incorrect header check"; | ||
618 | state.mode = BAD; | ||
619 | break; | ||
620 | } | ||
621 | if (BITS(4) != Z_DEFLATED) { | ||
622 | strm->msg = (char *)"unknown compression method"; | ||
623 | state.mode = BAD; | ||
624 | break; | ||
625 | } | ||
626 | DROPBITS(4); | ||
627 | len = BITS(4) + 8; | ||
628 | if (len > state.wbits) { | ||
629 | strm->msg = (char *)"invalid window size"; | ||
630 | state.mode = BAD; | ||
631 | break; | ||
632 | } | ||
633 | state.dmax = 1U << len; | ||
634 | //DEBUGF("inflate: zlib header ok\n"); | ||
635 | strm->adler = state.check = adler32(0L, Z_NULL, 0); | ||
636 | state.mode = hold & 0x200 ? DICTID : TYPE; | ||
637 | INITBITS(); | ||
638 | break; | ||
639 | #ifdef GUNZIP | ||
640 | case FLAGS: | ||
641 | NEEDBITS(16); | ||
642 | state.flags = (int)(hold); | ||
643 | if ((state.flags & 0xff) != Z_DEFLATED) { | ||
644 | strm->msg = (char *)"unknown compression method"; | ||
645 | state.mode = BAD; | ||
646 | break; | ||
647 | } | ||
648 | if (state.flags & 0xe000) { | ||
649 | strm->msg = (char *)"unknown header flags set"; | ||
650 | state.mode = BAD; | ||
651 | break; | ||
652 | } | ||
653 | if (state.head != Z_NULL) | ||
654 | state.head->text = (int)((hold >> 8) & 1); | ||
655 | if (state.flags & 0x0200) CRC2(state.check, hold); | ||
656 | INITBITS(); | ||
657 | state.mode = TIME; | ||
658 | case TIME: | ||
659 | NEEDBITS(32); | ||
660 | if (state.head != Z_NULL) | ||
661 | state.head->time = hold; | ||
662 | if (state.flags & 0x0200) CRC4(state.check, hold); | ||
663 | INITBITS(); | ||
664 | state.mode = OS; | ||
665 | case OS: | ||
666 | NEEDBITS(16); | ||
667 | if (state.head != Z_NULL) { | ||
668 | state.head->xflags = (int)(hold & 0xff); | ||
669 | state.head->os = (int)(hold >> 8); | ||
670 | } | ||
671 | if (state.flags & 0x0200) CRC2(state.check, hold); | ||
672 | INITBITS(); | ||
673 | state.mode = EXLEN; | ||
674 | case EXLEN: | ||
675 | if (state.flags & 0x0400) { | ||
676 | NEEDBITS(16); | ||
677 | state.length = (unsigned)(hold); | ||
678 | if (state.head != Z_NULL) | ||
679 | state.head->extra_len = (unsigned)hold; | ||
680 | if (state.flags & 0x0200) CRC2(state.check, hold); | ||
681 | INITBITS(); | ||
682 | } | ||
683 | else if (state.head != Z_NULL) | ||
684 | state.head->extra = Z_NULL; | ||
685 | state.mode = EXTRA; | ||
686 | case EXTRA: | ||
687 | if (state.flags & 0x0400) { | ||
688 | copy = state.length; | ||
689 | if (copy > have) copy = have; | ||
690 | if (copy) { | ||
691 | if (state.head != Z_NULL && | ||
692 | state.head->extra != Z_NULL) { | ||
693 | len = state.head->extra_len - state.length; | ||
694 | zmemcpy(state.head->extra + len, next, | ||
695 | len + copy > state.head->extra_max ? | ||
696 | state.head->extra_max - len : copy); | ||
697 | } | ||
698 | if (state.flags & 0x0200) | ||
699 | state.check = crc32(state.check, next, copy); | ||
700 | have -= copy; | ||
701 | next += copy; | ||
702 | state.length -= copy; | ||
703 | } | ||
704 | if (state.length) goto inf_leave; | ||
705 | } | ||
706 | state.length = 0; | ||
707 | state.mode = NAME; | ||
708 | case NAME: | ||
709 | if (state.flags & 0x0800) { | ||
710 | if (have == 0) goto inf_leave; | ||
711 | copy = 0; | ||
712 | do { | ||
713 | len = (unsigned)(next[copy++]); | ||
714 | if (state.head != Z_NULL && | ||
715 | state.head->name != Z_NULL && | ||
716 | state.length < state.head->name_max) | ||
717 | state.head->name[state.length++] = len; | ||
718 | } while (len && copy < have); | ||
719 | if (state.flags & 0x0200) | ||
720 | state.check = crc32(state.check, next, copy); | ||
721 | have -= copy; | ||
722 | next += copy; | ||
723 | if (len) goto inf_leave; | ||
724 | } | ||
725 | else if (state.head != Z_NULL) | ||
726 | state.head->name = Z_NULL; | ||
727 | state.length = 0; | ||
728 | state.mode = COMMENT; | ||
729 | case COMMENT: | ||
730 | if (state.flags & 0x1000) { | ||
731 | if (have == 0) goto inf_leave; | ||
732 | copy = 0; | ||
733 | do { | ||
734 | len = (unsigned)(next[copy++]); | ||
735 | if (state.head != Z_NULL && | ||
736 | state.head->comment != Z_NULL && | ||
737 | state.length < state.head->comm_max) | ||
738 | state.head->comment[state.length++] = len; | ||
739 | } while (len && copy < have); | ||
740 | if (state.flags & 0x0200) | ||
741 | state.check = crc32(state.check, next, copy); | ||
742 | have -= copy; | ||
743 | next += copy; | ||
744 | if (len) goto inf_leave; | ||
745 | } | ||
746 | else if (state.head != Z_NULL) | ||
747 | state.head->comment = Z_NULL; | ||
748 | state.mode = HCRC; | ||
749 | case HCRC: | ||
750 | if (state.flags & 0x0200) { | ||
751 | NEEDBITS(16); | ||
752 | if (hold != (state.check & 0xffff)) { | ||
753 | strm->msg = (char *)"header crc mismatch"; | ||
754 | state.mode = BAD; | ||
755 | break; | ||
756 | } | ||
757 | INITBITS(); | ||
758 | } | ||
759 | if (state.head != Z_NULL) { | ||
760 | state.head->hcrc = (int)((state.flags >> 9) & 1); | ||
761 | state.head->done = 1; | ||
762 | } | ||
763 | strm->adler = state.check = crc32(0L, Z_NULL, 0); | ||
764 | state.mode = TYPE; | ||
765 | break; | ||
766 | #endif | ||
767 | case DICTID: | ||
768 | NEEDBITS(32); | ||
769 | strm->adler = state.check = REVERSE(hold); | ||
770 | INITBITS(); | ||
771 | state.mode = DICT; | ||
772 | case DICT: | ||
773 | if (state.havedict == 0) { | ||
774 | RESTORE(); | ||
775 | return Z_NEED_DICT; | ||
776 | } | ||
777 | strm->adler = state.check = adler32(0L, Z_NULL, 0); | ||
778 | state.mode = TYPE; | ||
779 | case TYPE: | ||
780 | if (flush == Z_BLOCK) goto inf_leave; | ||
781 | case TYPEDO: | ||
782 | if (state.last) { | ||
783 | BYTEBITS(); | ||
784 | state.mode = CHECK; | ||
785 | break; | ||
786 | } | ||
787 | NEEDBITS(3); | ||
788 | state.last = BITS(1); | ||
789 | DROPBITS(1); | ||
790 | switch (BITS(2)) { | ||
791 | case 0: /* stored block */ | ||
792 | //DEBUGF("inflate: stored block%s\n", | ||
793 | //state.last ? " (last)" : ""); | ||
794 | state.mode = STORED; | ||
795 | break; | ||
796 | case 1: /* fixed block */ | ||
797 | fixedtables(); | ||
798 | //DEBUGF("inflate: fixed codes block%s\n", | ||
799 | //state.last ? " (last)" : ""); | ||
800 | state.mode = LEN; /* decode codes */ | ||
801 | break; | ||
802 | case 2: /* dynamic block */ | ||
803 | //DEBUGF("inflate: dynamic codes block%s\n", | ||
804 | //state.last ? " (last)" : ""); | ||
805 | state.mode = TABLE; | ||
806 | break; | ||
807 | case 3: | ||
808 | strm->msg = (char *)"invalid block type"; | ||
809 | state.mode = BAD; | ||
810 | } | ||
811 | DROPBITS(2); | ||
812 | break; | ||
813 | case STORED: | ||
814 | BYTEBITS(); /* go to byte boundary */ | ||
815 | NEEDBITS(32); | ||
816 | if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { | ||
817 | strm->msg = (char *)"invalid stored block lengths"; | ||
818 | state.mode = BAD; | ||
819 | break; | ||
820 | } | ||
821 | state.length = (unsigned)hold & 0xffff; | ||
822 | //DEBUGF("inflate: stored length %u\n", | ||
823 | //state.length); | ||
824 | INITBITS(); | ||
825 | state.mode = COPY; | ||
826 | case COPY: | ||
827 | copy = state.length; | ||
828 | if (copy) { | ||
829 | if (copy > have) copy = have; | ||
830 | if (copy > left) copy = left; | ||
831 | if (copy == 0) goto inf_leave; | ||
832 | zmemcpy(put, next, copy); | ||
833 | have -= copy; | ||
834 | next += copy; | ||
835 | left -= copy; | ||
836 | put += copy; | ||
837 | state.length -= copy; | ||
838 | break; | ||
839 | } | ||
840 | //DEBUGF("inflate: stored end\n"); | ||
841 | state.mode = TYPE; | ||
842 | break; | ||
843 | case TABLE: | ||
844 | NEEDBITS(14); | ||
845 | state.nlen = BITS(5) + 257; | ||
846 | DROPBITS(5); | ||
847 | state.ndist = BITS(5) + 1; | ||
848 | DROPBITS(5); | ||
849 | state.ncode = BITS(4) + 4; | ||
850 | DROPBITS(4); | ||
851 | #ifndef PKZIP_BUG_WORKAROUND | ||
852 | if (state.nlen > 286 || state.ndist > 30) { | ||
853 | strm->msg = (char *)"too many length or distance symbols"; | ||
854 | state.mode = BAD; | ||
855 | break; | ||
856 | } | ||
857 | #endif | ||
858 | //DEBUGF("inflate: table sizes ok\n"); | ||
859 | state.have = 0; | ||
860 | state.mode = LENLENS; | ||
861 | case LENLENS: | ||
862 | while (state.have < state.ncode) { | ||
863 | NEEDBITS(3); | ||
864 | state.lens[order[state.have++]] = (unsigned short)BITS(3); | ||
865 | DROPBITS(3); | ||
866 | } | ||
867 | while (state.have < 19) | ||
868 | state.lens[order[state.have++]] = 0; | ||
869 | state.next = state.codes; | ||
870 | state.lencode = (code const FAR *)(state.next); | ||
871 | state.lenbits = 7; | ||
872 | ret = inflate_table(CODES, state.lens, 19, &(state.next), | ||
873 | &(state.lenbits), state.work); | ||
874 | if (ret) { | ||
875 | strm->msg = (char *)"invalid code lengths set"; | ||
876 | state.mode = BAD; | ||
877 | break; | ||
878 | } | ||
879 | //DEBUGF("inflate: code lengths ok\n"); | ||
880 | state.have = 0; | ||
881 | state.mode = CODELENS; | ||
882 | case CODELENS: | ||
883 | while (state.have < state.nlen + state.ndist) { | ||
884 | for (;;) { | ||
885 | this = state.lencode[BITS(state.lenbits)]; | ||
886 | if ((unsigned)(this.bits) <= bits) break; | ||
887 | PULLBYTE(); | ||
888 | } | ||
889 | if (this.val < 16) { | ||
890 | NEEDBITS(this.bits); | ||
891 | DROPBITS(this.bits); | ||
892 | state.lens[state.have++] = this.val; | ||
893 | } | ||
894 | else { | ||
895 | if (this.val == 16) { | ||
896 | NEEDBITS(this.bits + 2); | ||
897 | DROPBITS(this.bits); | ||
898 | if (state.have == 0) { | ||
899 | strm->msg = (char *)"invalid bit length repeat"; | ||
900 | state.mode = BAD; | ||
901 | break; | ||
902 | } | ||
903 | len = state.lens[state.have - 1]; | ||
904 | copy = 3 + BITS(2); | ||
905 | DROPBITS(2); | ||
906 | } | ||
907 | else if (this.val == 17) { | ||
908 | NEEDBITS(this.bits + 3); | ||
909 | DROPBITS(this.bits); | ||
910 | len = 0; | ||
911 | copy = 3 + BITS(3); | ||
912 | DROPBITS(3); | ||
913 | } | ||
914 | else { | ||
915 | NEEDBITS(this.bits + 7); | ||
916 | DROPBITS(this.bits); | ||
917 | len = 0; | ||
918 | copy = 11 + BITS(7); | ||
919 | DROPBITS(7); | ||
920 | } | ||
921 | if (state.have + copy > state.nlen + state.ndist) { | ||
922 | strm->msg = (char *)"invalid bit length repeat"; | ||
923 | state.mode = BAD; | ||
924 | break; | ||
925 | } | ||
926 | while (copy--) | ||
927 | state.lens[state.have++] = (unsigned short)len; | ||
928 | } | ||
929 | } | ||
930 | |||
931 | /* handle error breaks in while */ | ||
932 | if (state.mode == BAD) break; | ||
933 | |||
934 | /* build code tables */ | ||
935 | state.next = state.codes; | ||
936 | state.lencode = (code const FAR *)(state.next); | ||
937 | state.lenbits = 9; | ||
938 | ret = inflate_table(LENS, state.lens, state.nlen, &(state.next), | ||
939 | &(state.lenbits), state.work); | ||
940 | if (ret) { | ||
941 | strm->msg = (char *)"invalid literal/lengths set"; | ||
942 | state.mode = BAD; | ||
943 | break; | ||
944 | } | ||
945 | state.distcode = (code const FAR *)(state.next); | ||
946 | state.distbits = 6; | ||
947 | ret = inflate_table(DISTS, state.lens + state.nlen, state.ndist, | ||
948 | &(state.next), &(state.distbits), state.work); | ||
949 | if (ret) { | ||
950 | strm->msg = (char *)"invalid distances set"; | ||
951 | state.mode = BAD; | ||
952 | break; | ||
953 | } | ||
954 | //DEBUGF("inflate: codes ok\n"); | ||
955 | state.mode = LEN; | ||
956 | case LEN: | ||
957 | if (have >= 6 && left >= 258) { | ||
958 | RESTORE(); | ||
959 | inflate_fast(strm, out); | ||
960 | LOAD(); | ||
961 | break; | ||
962 | } | ||
963 | for (;;) { | ||
964 | this = state.lencode[BITS(state.lenbits)]; | ||
965 | if ((unsigned)(this.bits) <= bits) break; | ||
966 | PULLBYTE(); | ||
967 | } | ||
968 | if (this.op && (this.op & 0xf0) == 0) { | ||
969 | last = this; | ||
970 | for (;;) { | ||
971 | this = state.lencode[last.val + | ||
972 | (BITS(last.bits + last.op) >> last.bits)]; | ||
973 | if ((unsigned)(last.bits + this.bits) <= bits) break; | ||
974 | PULLBYTE(); | ||
975 | } | ||
976 | DROPBITS(last.bits); | ||
977 | } | ||
978 | DROPBITS(this.bits); | ||
979 | state.length = (unsigned)this.val; | ||
980 | if ((int)(this.op) == 0) { | ||
981 | //DEBUGF(this.val >= 0x20 && this.val < 0x7f ? | ||
982 | //"inflate: literal '%c'\n" : | ||
983 | //"inflate: literal 0x%02x\n", this.val); | ||
984 | state.mode = LIT; | ||
985 | break; | ||
986 | } | ||
987 | if (this.op & 32) { | ||
988 | //DEBUGF("inflate: end of block\n"); | ||
989 | state.mode = TYPE; | ||
990 | break; | ||
991 | } | ||
992 | if (this.op & 64) { | ||
993 | strm->msg = (char *)"invalid literal/length code"; | ||
994 | state.mode = BAD; | ||
995 | break; | ||
996 | } | ||
997 | state.extra = (unsigned)(this.op) & 15; | ||
998 | state.mode = LENEXT; | ||
999 | case LENEXT: | ||
1000 | if (state.extra) { | ||
1001 | NEEDBITS(state.extra); | ||
1002 | state.length += BITS(state.extra); | ||
1003 | DROPBITS(state.extra); | ||
1004 | } | ||
1005 | //DEBUGF("inflate: length %u\n", state.length); | ||
1006 | state.mode = DIST; | ||
1007 | case DIST: | ||
1008 | for (;;) { | ||
1009 | this = state.distcode[BITS(state.distbits)]; | ||
1010 | if ((unsigned)(this.bits) <= bits) break; | ||
1011 | PULLBYTE(); | ||
1012 | } | ||
1013 | if ((this.op & 0xf0) == 0) { | ||
1014 | last = this; | ||
1015 | for (;;) { | ||
1016 | this = state.distcode[last.val + | ||
1017 | (BITS(last.bits + last.op) >> last.bits)]; | ||
1018 | if ((unsigned)(last.bits + this.bits) <= bits) break; | ||
1019 | PULLBYTE(); | ||
1020 | } | ||
1021 | DROPBITS(last.bits); | ||
1022 | } | ||
1023 | DROPBITS(this.bits); | ||
1024 | if (this.op & 64) { | ||
1025 | strm->msg = (char *)"invalid distance code"; | ||
1026 | state.mode = BAD; | ||
1027 | break; | ||
1028 | } | ||
1029 | state.offset = (unsigned)this.val; | ||
1030 | state.extra = (unsigned)(this.op) & 15; | ||
1031 | state.mode = DISTEXT; | ||
1032 | case DISTEXT: | ||
1033 | if (state.extra) { | ||
1034 | NEEDBITS(state.extra); | ||
1035 | state.offset += BITS(state.extra); | ||
1036 | DROPBITS(state.extra); | ||
1037 | } | ||
1038 | #ifdef INFLATE_STRICT | ||
1039 | if (state.offset > state.dmax) { | ||
1040 | strm->msg = (char *)"invalid distance too far back"; | ||
1041 | state.mode = BAD; | ||
1042 | break; | ||
1043 | } | ||
1044 | #endif | ||
1045 | if (state.offset > state.whave + out - left) { | ||
1046 | strm->msg = (char *)"invalid distance too far back"; | ||
1047 | state.mode = BAD; | ||
1048 | break; | ||
1049 | } | ||
1050 | //DEBUGF("inflate: distance %u\n", state.offset); | ||
1051 | state.mode = MATCH; | ||
1052 | case MATCH: | ||
1053 | if (left == 0) goto inf_leave; | ||
1054 | copy = out - left; | ||
1055 | if (state.offset > copy) { /* copy from window */ | ||
1056 | copy = state.offset - copy; | ||
1057 | if (copy > state.write) { | ||
1058 | copy -= state.write; | ||
1059 | from = state.window + (state.wsize - copy); | ||
1060 | } | ||
1061 | else | ||
1062 | from = state.window + (state.write - copy); | ||
1063 | if (copy > state.length) copy = state.length; | ||
1064 | } | ||
1065 | else { /* copy from output */ | ||
1066 | from = put - state.offset; | ||
1067 | copy = state.length; | ||
1068 | } | ||
1069 | if (copy > left) copy = left; | ||
1070 | left -= copy; | ||
1071 | state.length -= copy; | ||
1072 | do { | ||
1073 | *put++ = *from++; | ||
1074 | } while (--copy); | ||
1075 | if (state.length == 0) state.mode = LEN; | ||
1076 | break; | ||
1077 | case LIT: | ||
1078 | if (left == 0) goto inf_leave; | ||
1079 | *put++ = (unsigned char)(state.length); | ||
1080 | left--; | ||
1081 | state.mode = LEN; | ||
1082 | break; | ||
1083 | case CHECK: | ||
1084 | if (state.wrap) { | ||
1085 | NEEDBITS(32); | ||
1086 | out -= left; | ||
1087 | strm->total_out += out; | ||
1088 | state.total += out; | ||
1089 | if (out) | ||
1090 | strm->adler = state.check = | ||
1091 | UPDATE(state.check, put - out, out); | ||
1092 | out = left; | ||
1093 | if (( | ||
1094 | #ifdef GUNZIP | ||
1095 | state.flags ? hold : | ||
1096 | #endif | ||
1097 | REVERSE(hold)) != state.check) { | ||
1098 | strm->msg = (char *)"incorrect data check"; | ||
1099 | state.mode = BAD; | ||
1100 | break; | ||
1101 | } | ||
1102 | INITBITS(); | ||
1103 | //DEBUGF("inflate: check matches trailer\n"); | ||
1104 | } | ||
1105 | #ifdef GUNZIP | ||
1106 | state.mode = LENGTH; | ||
1107 | case LENGTH: | ||
1108 | if (state.wrap && state.flags) { | ||
1109 | NEEDBITS(32); | ||
1110 | if (hold != (state.total & 0xffffffffUL)) { | ||
1111 | strm->msg = (char *)"incorrect length check"; | ||
1112 | state.mode = BAD; | ||
1113 | break; | ||
1114 | } | ||
1115 | INITBITS(); | ||
1116 | Tracev((stderr, "inflate: length matches trailer\n")); | ||
1117 | } | ||
1118 | #endif | ||
1119 | state.mode = DONE; | ||
1120 | case DONE: | ||
1121 | ret = Z_STREAM_END; | ||
1122 | goto inf_leave; | ||
1123 | case BAD: | ||
1124 | ret = Z_DATA_ERROR; | ||
1125 | goto inf_leave; | ||
1126 | case ZMEM: | ||
1127 | return Z_MEM_ERROR; | ||
1128 | case SYNC: | ||
1129 | default: | ||
1130 | return Z_STREAM_ERROR; | ||
1131 | } | ||
1132 | //DEBUGF("%d / %d\n", strm->total_in, strm->avail_in); | ||
1133 | if (rb->button_get(false) == IMGVIEW_MENU) | ||
1134 | return PLUGIN_ABORT; | ||
1135 | else cb_progress(insize - strm->avail_in, insize); | ||
1136 | } | ||
1137 | |||
1138 | /* | ||
1139 | Return from inflate(), updating the total counts and the check value. | ||
1140 | If there was no progress during the inflate() call, return a buffer | ||
1141 | error. Call updatewindow() to create and/or update the window state. | ||
1142 | Note: a memory error from inflate() is non-recoverable. | ||
1143 | */ | ||
1144 | inf_leave: | ||
1145 | RESTORE(); | ||
1146 | if (state.wsize || (state.mode < CHECK && out != strm->avail_out)) | ||
1147 | if (updatewindow(strm, out)) { | ||
1148 | state.mode = ZMEM; | ||
1149 | return Z_MEM_ERROR; | ||
1150 | } | ||
1151 | in -= strm->avail_in; | ||
1152 | out -= strm->avail_out; | ||
1153 | strm->total_in += in; | ||
1154 | strm->total_out += out; | ||
1155 | state.total += out; | ||
1156 | if (state.wrap && out) | ||
1157 | strm->adler = state.check = | ||
1158 | UPDATE(state.check, strm->next_out - out, out); | ||
1159 | strm->data_type = state.bits + (state.last ? 64 : 0) + | ||
1160 | (state.mode == TYPE ? 128 : 0); | ||
1161 | if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) | ||
1162 | ret = Z_BUF_ERROR; | ||
1163 | return ret; | ||
1164 | } | ||
1165 | |||
1166 | int ZEXPORT inflateEnd(strm) | ||
1167 | z_streamp strm; | ||
1168 | { | ||
1169 | //struct inflate_state FAR *state; | ||
1170 | if (strm == Z_NULL /*|| strm->zfree == (free_func)0*/) | ||
1171 | return Z_STREAM_ERROR; | ||
1172 | //state = (struct inflate_state FAR *)strm->state; | ||
1173 | //if (state.window != Z_NULL) ZFREE(strm, state.window); | ||
1174 | //ZFREE(strm, strm->state); | ||
1175 | //strm->state = Z_NULL; | ||
1176 | //DEBUGF("inflate: end\n"); | ||
1177 | return Z_OK; | ||
1178 | } | ||
1179 | |||
1180 | int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) | ||
1181 | z_streamp strm; | ||
1182 | const Bytef *dictionary; | ||
1183 | uInt dictLength; | ||
1184 | { | ||
1185 | //struct inflate_state FAR *state; | ||
1186 | unsigned long id; | ||
1187 | |||
1188 | /* check state */ | ||
1189 | if (strm == Z_NULL) return Z_STREAM_ERROR; | ||
1190 | //state = (struct inflate_state FAR *)strm->state; | ||
1191 | if (state.wrap != 0 && state.mode != DICT) | ||
1192 | return Z_STREAM_ERROR; | ||
1193 | |||
1194 | /* check for correct dictionary id */ | ||
1195 | if (state.mode == DICT) { | ||
1196 | id = adler32(0L, Z_NULL, 0); | ||
1197 | id = adler32(id, dictionary, dictLength); | ||
1198 | if (id != state.check) | ||
1199 | return Z_DATA_ERROR; | ||
1200 | } | ||
1201 | |||
1202 | /* copy dictionary to window */ | ||
1203 | if (updatewindow(strm, strm->avail_out)) { | ||
1204 | state.mode = ZMEM; | ||
1205 | return Z_MEM_ERROR; | ||
1206 | } | ||
1207 | if (dictLength > state.wsize) { | ||
1208 | zmemcpy(state.window, dictionary + dictLength - state.wsize, | ||
1209 | state.wsize); | ||
1210 | state.whave = state.wsize; | ||
1211 | } | ||
1212 | else { | ||
1213 | zmemcpy(state.window + state.wsize - dictLength, dictionary, | ||
1214 | dictLength); | ||
1215 | state.whave = dictLength; | ||
1216 | } | ||
1217 | state.havedict = 1; | ||
1218 | //DEBUGF("inflate: dictionary set\n"); | ||
1219 | return Z_OK; | ||
1220 | } | ||
1221 | |||
1222 | int ZEXPORT inflateGetHeader(strm, head) | ||
1223 | z_streamp strm; | ||
1224 | gz_headerp head; | ||
1225 | { | ||
1226 | //struct inflate_state FAR *state; | ||
1227 | |||
1228 | /* check state */ | ||
1229 | if (strm == Z_NULL) return Z_STREAM_ERROR; | ||
1230 | //state = (struct inflate_state FAR *)strm->state; | ||
1231 | if ((state.wrap & 2) == 0) return Z_STREAM_ERROR; | ||
1232 | |||
1233 | /* save header structure */ | ||
1234 | state.head = head; | ||
1235 | head->done = 0; | ||
1236 | return Z_OK; | ||
1237 | } | ||
1238 | |||
1239 | /* | ||
1240 | Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found | ||
1241 | or when out of input. When called, *have is the number of pattern bytes | ||
1242 | found in order so far, in 0..3. On return *have is updated to the new | ||
1243 | state. If on return *have equals four, then the pattern was found and the | ||
1244 | return value is how many bytes were read including the last byte of the | ||
1245 | pattern. If *have is less than four, then the pattern has not been found | ||
1246 | yet and the return value is len. In the latter case, syncsearch() can be | ||
1247 | called again with more data and the *have state. *have is initialized to | ||
1248 | zero for the first call. | ||
1249 | */ | ||
1250 | local unsigned syncsearch(have, buf, len) | ||
1251 | unsigned FAR *have; | ||
1252 | unsigned char FAR *buf; | ||
1253 | unsigned len; | ||
1254 | { | ||
1255 | unsigned got; | ||
1256 | unsigned next; | ||
1257 | |||
1258 | got = *have; | ||
1259 | next = 0; | ||
1260 | while (next < len && got < 4) { | ||
1261 | if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) | ||
1262 | got++; | ||
1263 | else if (buf[next]) | ||
1264 | got = 0; | ||
1265 | else | ||
1266 | got = 4 - got; | ||
1267 | next++; | ||
1268 | } | ||
1269 | *have = got; | ||
1270 | return next; | ||
1271 | } | ||
1272 | |||
1273 | int ZEXPORT inflateSync(strm) | ||
1274 | z_streamp strm; | ||
1275 | { | ||
1276 | unsigned len; /* number of bytes to look at or looked at */ | ||
1277 | unsigned long in, out; /* temporary to save total_in and total_out */ | ||
1278 | unsigned char buf[4]; /* to restore bit buffer to byte string */ | ||
1279 | //struct inflate_state FAR *state; | ||
1280 | |||
1281 | /* check parameters */ | ||
1282 | if (strm == Z_NULL) return Z_STREAM_ERROR; | ||
1283 | //state = (struct inflate_state FAR *)strm->state; | ||
1284 | if (strm->avail_in == 0 && state.bits < 8) return Z_BUF_ERROR; | ||
1285 | |||
1286 | /* if first time, start search in bit buffer */ | ||
1287 | if (state.mode != SYNC) { | ||
1288 | state.mode = SYNC; | ||
1289 | state.hold <<= state.bits & 7; | ||
1290 | state.bits -= state.bits & 7; | ||
1291 | len = 0; | ||
1292 | while (state.bits >= 8) { | ||
1293 | buf[len++] = (unsigned char)(state.hold); | ||
1294 | state.hold >>= 8; | ||
1295 | state.bits -= 8; | ||
1296 | } | ||
1297 | state.have = 0; | ||
1298 | syncsearch(&(state.have), buf, len); | ||
1299 | } | ||
1300 | |||
1301 | /* search available input */ | ||
1302 | len = syncsearch(&(state.have), strm->next_in, strm->avail_in); | ||
1303 | strm->avail_in -= len; | ||
1304 | strm->next_in += len; | ||
1305 | strm->total_in += len; | ||
1306 | |||
1307 | /* return no joy or set up to restart inflate() on a new block */ | ||
1308 | if (state.have != 4) return Z_DATA_ERROR; | ||
1309 | in = strm->total_in; out = strm->total_out; | ||
1310 | inflateReset(strm); | ||
1311 | strm->total_in = in; strm->total_out = out; | ||
1312 | state.mode = TYPE; | ||
1313 | return Z_OK; | ||
1314 | } | ||
1315 | |||
1316 | /* | ||
1317 | Returns true if inflate is currently at the end of a block generated by | ||
1318 | Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP | ||
1319 | implementation to provide an additional safety check. PPP uses | ||
1320 | Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored | ||
1321 | block. When decompressing, PPP checks that at the end of input packet, | ||
1322 | inflate is waiting for these length bytes. | ||
1323 | */ | ||
1324 | int ZEXPORT inflateSyncPoint(strm) | ||
1325 | z_streamp strm; | ||
1326 | { | ||
1327 | //struct inflate_state FAR *state; | ||
1328 | |||
1329 | if (strm == Z_NULL) return Z_STREAM_ERROR; | ||
1330 | //state = (struct inflate_state FAR *)strm->state; | ||
1331 | return state.mode == STORED && state.bits == 0; | ||
1332 | } | ||
diff --git a/apps/plugins/imageviewer/png/inflate.h b/apps/plugins/imageviewer/png/inflate.h deleted file mode 100644 index d35c1bc041..0000000000 --- a/apps/plugins/imageviewer/png/inflate.h +++ /dev/null | |||
@@ -1,116 +0,0 @@ | |||
1 | /* inflate.h -- internal inflate state definition | ||
2 | * Copyright (C) 1995-2004 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* WARNING: this file should *not* be used by applications. It is | ||
7 | part of the implementation of the compression library and is | ||
8 | subject to change. Applications should only use zlib.h. | ||
9 | */ | ||
10 | |||
11 | /* define NO_GZIP when compiling if you want to disable gzip header and | ||
12 | trailer decoding by inflate(). NO_GZIP would be used to avoid linking in | ||
13 | the crc code when it is not needed. For shared libraries, gzip decoding | ||
14 | should be left enabled. */ | ||
15 | |||
16 | #ifndef NO_GZIP | ||
17 | # define GUNZIP | ||
18 | #endif | ||
19 | |||
20 | /* Possible inflate modes between inflate() calls */ | ||
21 | typedef enum { | ||
22 | HEAD, /* i: waiting for magic header */ | ||
23 | FLAGS, /* i: waiting for method and flags (gzip) */ | ||
24 | TIME, /* i: waiting for modification time (gzip) */ | ||
25 | OS, /* i: waiting for extra flags and operating system (gzip) */ | ||
26 | EXLEN, /* i: waiting for extra length (gzip) */ | ||
27 | EXTRA, /* i: waiting for extra bytes (gzip) */ | ||
28 | NAME, /* i: waiting for end of file name (gzip) */ | ||
29 | COMMENT, /* i: waiting for end of comment (gzip) */ | ||
30 | HCRC, /* i: waiting for header crc (gzip) */ | ||
31 | DICTID, /* i: waiting for dictionary check value */ | ||
32 | DICT, /* waiting for inflateSetDictionary() call */ | ||
33 | TYPE, /* i: waiting for type bits, including last-flag bit */ | ||
34 | TYPEDO, /* i: same, but skip check to exit inflate on new block */ | ||
35 | STORED, /* i: waiting for stored size (length and complement) */ | ||
36 | COPY, /* i/o: waiting for input or output to copy stored block */ | ||
37 | TABLE, /* i: waiting for dynamic block table lengths */ | ||
38 | LENLENS, /* i: waiting for code length code lengths */ | ||
39 | CODELENS, /* i: waiting for length/lit and distance code lengths */ | ||
40 | LEN, /* i: waiting for length/lit code */ | ||
41 | LENEXT, /* i: waiting for length extra bits */ | ||
42 | DIST, /* i: waiting for distance code */ | ||
43 | DISTEXT, /* i: waiting for distance extra bits */ | ||
44 | MATCH, /* o: waiting for output space to copy string */ | ||
45 | LIT, /* o: waiting for output space to write literal */ | ||
46 | CHECK, /* i: waiting for 32-bit check value */ | ||
47 | LENGTH, /* i: waiting for 32-bit length (gzip) */ | ||
48 | DONE, /* finished check, done -- remain here until reset */ | ||
49 | BAD, /* got a data error -- remain here until reset */ | ||
50 | ZMEM, /* got an inflate() memory error -- remain here until reset */ | ||
51 | SYNC /* looking for synchronization bytes to restart inflate() */ | ||
52 | } inflate_mode; | ||
53 | |||
54 | /* | ||
55 | State transitions between above modes - | ||
56 | |||
57 | (most modes can go to the BAD or MEM mode -- not shown for clarity) | ||
58 | |||
59 | Process header: | ||
60 | HEAD -> (gzip) or (zlib) | ||
61 | (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME | ||
62 | NAME -> COMMENT -> HCRC -> TYPE | ||
63 | (zlib) -> DICTID or TYPE | ||
64 | DICTID -> DICT -> TYPE | ||
65 | Read deflate blocks: | ||
66 | TYPE -> STORED or TABLE or LEN or CHECK | ||
67 | STORED -> COPY -> TYPE | ||
68 | TABLE -> LENLENS -> CODELENS -> LEN | ||
69 | Read deflate codes: | ||
70 | LEN -> LENEXT or LIT or TYPE | ||
71 | LENEXT -> DIST -> DISTEXT -> MATCH -> LEN | ||
72 | LIT -> LEN | ||
73 | Process trailer: | ||
74 | CHECK -> LENGTH -> DONE | ||
75 | */ | ||
76 | |||
77 | /* state maintained between inflate() calls. Approximately 7K bytes. */ | ||
78 | struct inflate_state { | ||
79 | inflate_mode mode; /* current inflate mode */ | ||
80 | int last; /* true if processing last block */ | ||
81 | int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ | ||
82 | int havedict; /* true if dictionary provided */ | ||
83 | int flags; /* gzip header method and flags (0 if zlib) */ | ||
84 | unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ | ||
85 | unsigned long check; /* protected copy of check value */ | ||
86 | unsigned long total; /* protected copy of output count */ | ||
87 | gz_headerp head; /* where to save gzip header information */ | ||
88 | /* sliding window */ | ||
89 | unsigned wbits; /* log base 2 of requested window size */ | ||
90 | unsigned wsize; /* window size or zero if not using window */ | ||
91 | unsigned whave; /* valid bytes in the window */ | ||
92 | unsigned write; /* window write index */ | ||
93 | unsigned char FAR *window; /* allocated sliding window, if needed */ | ||
94 | /* bit accumulator */ | ||
95 | unsigned long hold; /* input bit accumulator */ | ||
96 | unsigned bits; /* number of bits in "in" */ | ||
97 | /* for string and stored block copying */ | ||
98 | unsigned length; /* literal or length of data to copy */ | ||
99 | unsigned offset; /* distance back to copy string from */ | ||
100 | /* for table and code decoding */ | ||
101 | unsigned extra; /* extra bits needed */ | ||
102 | /* fixed and dynamic code tables */ | ||
103 | code const FAR *lencode; /* starting table for length/literal codes */ | ||
104 | code const FAR *distcode; /* starting table for distance codes */ | ||
105 | unsigned lenbits; /* index bits for lencode */ | ||
106 | unsigned distbits; /* index bits for distcode */ | ||
107 | /* dynamic table building */ | ||
108 | unsigned ncode; /* number of code length code lengths */ | ||
109 | unsigned nlen; /* number of length code lengths */ | ||
110 | unsigned ndist; /* number of distance code lengths */ | ||
111 | unsigned have; /* number of code lengths in lens[] */ | ||
112 | code FAR *next; /* next available space in codes[] */ | ||
113 | unsigned short lens[320]; /* temporary storage for code lengths */ | ||
114 | unsigned short work[288]; /* work area for code table building */ | ||
115 | code codes[ENOUGH]; /* space for code tables */ | ||
116 | }; | ||
diff --git a/apps/plugins/imageviewer/png/inftrees.c b/apps/plugins/imageviewer/png/inftrees.c deleted file mode 100644 index 8a9c13ff03..0000000000 --- a/apps/plugins/imageviewer/png/inftrees.c +++ /dev/null | |||
@@ -1,329 +0,0 @@ | |||
1 | /* inftrees.c -- generate Huffman trees for efficient decoding | ||
2 | * Copyright (C) 1995-2005 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | #include "zutil.h" | ||
7 | #include "inftrees.h" | ||
8 | |||
9 | #define MAXBITS 15 | ||
10 | |||
11 | const char inflate_copyright[] = | ||
12 | " inflate 1.2.3 Copyright 1995-2005 Mark Adler "; | ||
13 | /* | ||
14 | If you use the zlib library in a product, an acknowledgment is welcome | ||
15 | in the documentation of your product. If for some reason you cannot | ||
16 | include such an acknowledgment, I would appreciate that you keep this | ||
17 | copyright string in the executable of your product. | ||
18 | */ | ||
19 | |||
20 | /* | ||
21 | Build a set of tables to decode the provided canonical Huffman code. | ||
22 | The code lengths are lens[0..codes-1]. The result starts at *table, | ||
23 | whose indices are 0..2^bits-1. work is a writable array of at least | ||
24 | lens shorts, which is used as a work area. type is the type of code | ||
25 | to be generated, CODES, LENS, or DISTS. On return, zero is success, | ||
26 | -1 is an invalid code, and +1 means that ENOUGH isn't enough. table | ||
27 | on return points to the next available entry's address. bits is the | ||
28 | requested root table index bits, and on return it is the actual root | ||
29 | table index bits. It will differ if the request is greater than the | ||
30 | longest code or if it is less than the shortest code. | ||
31 | */ | ||
32 | int inflate_table(type, lens, codes, table, bits, work) | ||
33 | codetype type; | ||
34 | unsigned short FAR *lens; | ||
35 | unsigned codes; | ||
36 | code FAR * FAR *table; | ||
37 | unsigned FAR *bits; | ||
38 | unsigned short FAR *work; | ||
39 | { | ||
40 | unsigned len; /* a code's length in bits */ | ||
41 | unsigned sym; /* index of code symbols */ | ||
42 | unsigned min, max; /* minimum and maximum code lengths */ | ||
43 | unsigned root; /* number of index bits for root table */ | ||
44 | unsigned curr; /* number of index bits for current table */ | ||
45 | unsigned drop; /* code bits to drop for sub-table */ | ||
46 | int left; /* number of prefix codes available */ | ||
47 | unsigned used; /* code entries in table used */ | ||
48 | unsigned huff; /* Huffman code */ | ||
49 | unsigned incr; /* for incrementing code, index */ | ||
50 | unsigned fill; /* index for replicating entries */ | ||
51 | unsigned low; /* low bits for current root entry */ | ||
52 | unsigned mask; /* mask for low root bits */ | ||
53 | code this; /* table entry for duplication */ | ||
54 | code FAR *next; /* next available space in table */ | ||
55 | const unsigned short FAR *base; /* base value table to use */ | ||
56 | const unsigned short FAR *extra; /* extra bits table to use */ | ||
57 | int end; /* use base and extra for symbol > end */ | ||
58 | unsigned short count[MAXBITS+1]; /* number of codes of each length */ | ||
59 | unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ | ||
60 | static const unsigned short lbase[31] = { /* Length codes 257..285 base */ | ||
61 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, | ||
62 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; | ||
63 | static const unsigned short lext[31] = { /* Length codes 257..285 extra */ | ||
64 | 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, | ||
65 | 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196}; | ||
66 | static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ | ||
67 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, | ||
68 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, | ||
69 | 8193, 12289, 16385, 24577, 0, 0}; | ||
70 | static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ | ||
71 | 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, | ||
72 | 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, | ||
73 | 28, 28, 29, 29, 64, 64}; | ||
74 | |||
75 | /* | ||
76 | Process a set of code lengths to create a canonical Huffman code. The | ||
77 | code lengths are lens[0..codes-1]. Each length corresponds to the | ||
78 | symbols 0..codes-1. The Huffman code is generated by first sorting the | ||
79 | symbols by length from short to long, and retaining the symbol order | ||
80 | for codes with equal lengths. Then the code starts with all zero bits | ||
81 | for the first code of the shortest length, and the codes are integer | ||
82 | increments for the same length, and zeros are appended as the length | ||
83 | increases. For the deflate format, these bits are stored backwards | ||
84 | from their more natural integer increment ordering, and so when the | ||
85 | decoding tables are built in the large loop below, the integer codes | ||
86 | are incremented backwards. | ||
87 | |||
88 | This routine assumes, but does not check, that all of the entries in | ||
89 | lens[] are in the range 0..MAXBITS. The caller must assure this. | ||
90 | 1..MAXBITS is interpreted as that code length. zero means that that | ||
91 | symbol does not occur in this code. | ||
92 | |||
93 | The codes are sorted by computing a count of codes for each length, | ||
94 | creating from that a table of starting indices for each length in the | ||
95 | sorted table, and then entering the symbols in order in the sorted | ||
96 | table. The sorted table is work[], with that space being provided by | ||
97 | the caller. | ||
98 | |||
99 | The length counts are used for other purposes as well, i.e. finding | ||
100 | the minimum and maximum length codes, determining if there are any | ||
101 | codes at all, checking for a valid set of lengths, and looking ahead | ||
102 | at length counts to determine sub-table sizes when building the | ||
103 | decoding tables. | ||
104 | */ | ||
105 | |||
106 | /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ | ||
107 | for (len = 0; len <= MAXBITS; len++) | ||
108 | count[len] = 0; | ||
109 | for (sym = 0; sym < codes; sym++) | ||
110 | count[lens[sym]]++; | ||
111 | |||
112 | /* bound code lengths, force root to be within code lengths */ | ||
113 | root = *bits; | ||
114 | for (max = MAXBITS; max >= 1; max--) | ||
115 | if (count[max] != 0) break; | ||
116 | if (root > max) root = max; | ||
117 | if (max == 0) { /* no symbols to code at all */ | ||
118 | this.op = (unsigned char)64; /* invalid code marker */ | ||
119 | this.bits = (unsigned char)1; | ||
120 | this.val = (unsigned short)0; | ||
121 | *(*table)++ = this; /* make a table to force an error */ | ||
122 | *(*table)++ = this; | ||
123 | *bits = 1; | ||
124 | return 0; /* no symbols, but wait for decoding to report error */ | ||
125 | } | ||
126 | for (min = 1; min <= MAXBITS; min++) | ||
127 | if (count[min] != 0) break; | ||
128 | if (root < min) root = min; | ||
129 | |||
130 | /* check for an over-subscribed or incomplete set of lengths */ | ||
131 | left = 1; | ||
132 | for (len = 1; len <= MAXBITS; len++) { | ||
133 | left <<= 1; | ||
134 | left -= count[len]; | ||
135 | if (left < 0) return -1; /* over-subscribed */ | ||
136 | } | ||
137 | if (left > 0 && (type == CODES || max != 1)) | ||
138 | return -1; /* incomplete set */ | ||
139 | |||
140 | /* generate offsets into symbol table for each length for sorting */ | ||
141 | offs[1] = 0; | ||
142 | for (len = 1; len < MAXBITS; len++) | ||
143 | offs[len + 1] = offs[len] + count[len]; | ||
144 | |||
145 | /* sort symbols by length, by symbol order within each length */ | ||
146 | for (sym = 0; sym < codes; sym++) | ||
147 | if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; | ||
148 | |||
149 | /* | ||
150 | Create and fill in decoding tables. In this loop, the table being | ||
151 | filled is at next and has curr index bits. The code being used is huff | ||
152 | with length len. That code is converted to an index by dropping drop | ||
153 | bits off of the bottom. For codes where len is less than drop + curr, | ||
154 | those top drop + curr - len bits are incremented through all values to | ||
155 | fill the table with replicated entries. | ||
156 | |||
157 | root is the number of index bits for the root table. When len exceeds | ||
158 | root, sub-tables are created pointed to by the root entry with an index | ||
159 | of the low root bits of huff. This is saved in low to check for when a | ||
160 | new sub-table should be started. drop is zero when the root table is | ||
161 | being filled, and drop is root when sub-tables are being filled. | ||
162 | |||
163 | When a new sub-table is needed, it is necessary to look ahead in the | ||
164 | code lengths to determine what size sub-table is needed. The length | ||
165 | counts are used for this, and so count[] is decremented as codes are | ||
166 | entered in the tables. | ||
167 | |||
168 | used keeps track of how many table entries have been allocated from the | ||
169 | provided *table space. It is checked when a LENS table is being made | ||
170 | against the space in *table, ENOUGH, minus the maximum space needed by | ||
171 | the worst case distance code, MAXD. This should never happen, but the | ||
172 | sufficiency of ENOUGH has not been proven exhaustively, hence the check. | ||
173 | This assumes that when type == LENS, bits == 9. | ||
174 | |||
175 | sym increments through all symbols, and the loop terminates when | ||
176 | all codes of length max, i.e. all codes, have been processed. This | ||
177 | routine permits incomplete codes, so another loop after this one fills | ||
178 | in the rest of the decoding tables with invalid code markers. | ||
179 | */ | ||
180 | |||
181 | /* set up for code type */ | ||
182 | switch (type) { | ||
183 | case CODES: | ||
184 | base = extra = work; /* dummy value--not used */ | ||
185 | end = 19; | ||
186 | break; | ||
187 | case LENS: | ||
188 | base = lbase; | ||
189 | base -= 257; | ||
190 | extra = lext; | ||
191 | extra -= 257; | ||
192 | end = 256; | ||
193 | break; | ||
194 | default: /* DISTS */ | ||
195 | base = dbase; | ||
196 | extra = dext; | ||
197 | end = -1; | ||
198 | } | ||
199 | |||
200 | /* initialize state for loop */ | ||
201 | huff = 0; /* starting code */ | ||
202 | sym = 0; /* starting code symbol */ | ||
203 | len = min; /* starting code length */ | ||
204 | next = *table; /* current table to fill in */ | ||
205 | curr = root; /* current table index bits */ | ||
206 | drop = 0; /* current bits to drop from code for index */ | ||
207 | low = (unsigned)(-1); /* trigger new sub-table when len > root */ | ||
208 | used = 1U << root; /* use root table entries */ | ||
209 | mask = used - 1; /* mask for comparing low */ | ||
210 | |||
211 | /* check available table space */ | ||
212 | if (type == LENS && used >= ENOUGH - MAXD) | ||
213 | return 1; | ||
214 | |||
215 | /* process all codes and make table entries */ | ||
216 | for (;;) { | ||
217 | /* create table entry */ | ||
218 | this.bits = (unsigned char)(len - drop); | ||
219 | if ((int)(work[sym]) < end) { | ||
220 | this.op = (unsigned char)0; | ||
221 | this.val = work[sym]; | ||
222 | } | ||
223 | else if ((int)(work[sym]) > end) { | ||
224 | this.op = (unsigned char)(extra[work[sym]]); | ||
225 | this.val = base[work[sym]]; | ||
226 | } | ||
227 | else { | ||
228 | this.op = (unsigned char)(32 + 64); /* end of block */ | ||
229 | this.val = 0; | ||
230 | } | ||
231 | |||
232 | /* replicate for those indices with low len bits equal to huff */ | ||
233 | incr = 1U << (len - drop); | ||
234 | fill = 1U << curr; | ||
235 | min = fill; /* save offset to next table */ | ||
236 | do { | ||
237 | fill -= incr; | ||
238 | next[(huff >> drop) + fill] = this; | ||
239 | } while (fill != 0); | ||
240 | |||
241 | /* backwards increment the len-bit code huff */ | ||
242 | incr = 1U << (len - 1); | ||
243 | while (huff & incr) | ||
244 | incr >>= 1; | ||
245 | if (incr != 0) { | ||
246 | huff &= incr - 1; | ||
247 | huff += incr; | ||
248 | } | ||
249 | else | ||
250 | huff = 0; | ||
251 | |||
252 | /* go to next symbol, update count, len */ | ||
253 | sym++; | ||
254 | if (--(count[len]) == 0) { | ||
255 | if (len == max) break; | ||
256 | len = lens[work[sym]]; | ||
257 | } | ||
258 | |||
259 | /* create new sub-table if needed */ | ||
260 | if (len > root && (huff & mask) != low) { | ||
261 | /* if first time, transition to sub-tables */ | ||
262 | if (drop == 0) | ||
263 | drop = root; | ||
264 | |||
265 | /* increment past last table */ | ||
266 | next += min; /* here min is 1 << curr */ | ||
267 | |||
268 | /* determine length of next table */ | ||
269 | curr = len - drop; | ||
270 | left = (int)(1 << curr); | ||
271 | while (curr + drop < max) { | ||
272 | left -= count[curr + drop]; | ||
273 | if (left <= 0) break; | ||
274 | curr++; | ||
275 | left <<= 1; | ||
276 | } | ||
277 | |||
278 | /* check for enough space */ | ||
279 | used += 1U << curr; | ||
280 | if (type == LENS && used >= ENOUGH - MAXD) | ||
281 | return 1; | ||
282 | |||
283 | /* point entry in root table to sub-table */ | ||
284 | low = huff & mask; | ||
285 | (*table)[low].op = (unsigned char)curr; | ||
286 | (*table)[low].bits = (unsigned char)root; | ||
287 | (*table)[low].val = (unsigned short)(next - *table); | ||
288 | } | ||
289 | } | ||
290 | |||
291 | /* | ||
292 | Fill in rest of table for incomplete codes. This loop is similar to the | ||
293 | loop above in incrementing huff for table indices. It is assumed that | ||
294 | len is equal to curr + drop, so there is no loop needed to increment | ||
295 | through high index bits. When the current sub-table is filled, the loop | ||
296 | drops back to the root table to fill in any remaining entries there. | ||
297 | */ | ||
298 | this.op = (unsigned char)64; /* invalid code marker */ | ||
299 | this.bits = (unsigned char)(len - drop); | ||
300 | this.val = (unsigned short)0; | ||
301 | while (huff != 0) { | ||
302 | /* when done with sub-table, drop back to root table */ | ||
303 | if (drop != 0 && (huff & mask) != low) { | ||
304 | drop = 0; | ||
305 | len = root; | ||
306 | next = *table; | ||
307 | this.bits = (unsigned char)len; | ||
308 | } | ||
309 | |||
310 | /* put invalid code marker in table */ | ||
311 | next[huff >> drop] = this; | ||
312 | |||
313 | /* backwards increment the len-bit code huff */ | ||
314 | incr = 1U << (len - 1); | ||
315 | while (huff & incr) | ||
316 | incr >>= 1; | ||
317 | if (incr != 0) { | ||
318 | huff &= incr - 1; | ||
319 | huff += incr; | ||
320 | } | ||
321 | else | ||
322 | huff = 0; | ||
323 | } | ||
324 | |||
325 | /* set return parameters */ | ||
326 | *table += used; | ||
327 | *bits = root; | ||
328 | return 0; | ||
329 | } | ||
diff --git a/apps/plugins/imageviewer/png/inftrees.h b/apps/plugins/imageviewer/png/inftrees.h deleted file mode 100644 index b1104c87e7..0000000000 --- a/apps/plugins/imageviewer/png/inftrees.h +++ /dev/null | |||
@@ -1,55 +0,0 @@ | |||
1 | /* inftrees.h -- header to use inftrees.c | ||
2 | * Copyright (C) 1995-2005 Mark Adler | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* WARNING: this file should *not* be used by applications. It is | ||
7 | part of the implementation of the compression library and is | ||
8 | subject to change. Applications should only use zlib.h. | ||
9 | */ | ||
10 | |||
11 | /* Structure for decoding tables. Each entry provides either the | ||
12 | information needed to do the operation requested by the code that | ||
13 | indexed that table entry, or it provides a pointer to another | ||
14 | table that indexes more bits of the code. op indicates whether | ||
15 | the entry is a pointer to another table, a literal, a length or | ||
16 | distance, an end-of-block, or an invalid code. For a table | ||
17 | pointer, the low four bits of op is the number of index bits of | ||
18 | that table. For a length or distance, the low four bits of op | ||
19 | is the number of extra bits to get after the code. bits is | ||
20 | the number of bits in this code or part of the code to drop off | ||
21 | of the bit buffer. val is the actual byte to output in the case | ||
22 | of a literal, the base length or distance, or the offset from | ||
23 | the current table to the next table. Each entry is four bytes. */ | ||
24 | typedef struct { | ||
25 | unsigned char op; /* operation, extra bits, table bits */ | ||
26 | unsigned char bits; /* bits in this part of the code */ | ||
27 | unsigned short val; /* offset in table or code value */ | ||
28 | } code; | ||
29 | |||
30 | /* op values as set by inflate_table(): | ||
31 | 00000000 - literal | ||
32 | 0000tttt - table link, tttt != 0 is the number of table index bits | ||
33 | 0001eeee - length or distance, eeee is the number of extra bits | ||
34 | 01100000 - end of block | ||
35 | 01000000 - invalid code | ||
36 | */ | ||
37 | |||
38 | /* Maximum size of dynamic tree. The maximum found in a long but non- | ||
39 | exhaustive search was 1444 code structures (852 for length/literals | ||
40 | and 592 for distances, the latter actually the result of an | ||
41 | exhaustive search). The true maximum is not known, but the value | ||
42 | below is more than safe. */ | ||
43 | #define ENOUGH 2048 | ||
44 | #define MAXD 592 | ||
45 | |||
46 | /* Type of code to build for inftable() */ | ||
47 | typedef enum { | ||
48 | CODES, | ||
49 | LENS, | ||
50 | DISTS | ||
51 | } codetype; | ||
52 | |||
53 | extern int inflate_table OF((codetype type, unsigned short FAR *lens, | ||
54 | unsigned codes, code FAR * FAR *table, | ||
55 | unsigned FAR *bits, unsigned short FAR *work)); | ||
diff --git a/apps/plugins/imageviewer/png/png.c b/apps/plugins/imageviewer/png/png.c index 46430607ea..4bd988d939 100644 --- a/apps/plugins/imageviewer/png/png.c +++ b/apps/plugins/imageviewer/png/png.c | |||
@@ -12,6 +12,11 @@ | |||
12 | * Based on lodepng, a lightweight png decoder/encoder | 12 | * Based on lodepng, a lightweight png decoder/encoder |
13 | * (c) 2005-2008 Lode Vandevenne | 13 | * (c) 2005-2008 Lode Vandevenne |
14 | * | 14 | * |
15 | * Copyright (c) 2010 Marcin Bukat | ||
16 | * - pixel format conversion & transparency handling | ||
17 | * - adaptation of tinf (tiny inflate library) | ||
18 | * - code refactoring & cleanups | ||
19 | * | ||
15 | * This program is free software; you can redistribute it and/or | 20 | * This program is free software; you can redistribute it and/or |
16 | * modify it under the terms of the GNU General Public License | 21 | * modify it under the terms of the GNU General Public License |
17 | * as published by the Free Software Foundation; either version 2 | 22 | * as published by the Free Software Foundation; either version 2 |
@@ -22,1279 +27,29 @@ | |||
22 | * | 27 | * |
23 | ****************************************************************************/ | 28 | ****************************************************************************/ |
24 | 29 | ||
25 | /* | ||
26 | LodePNG version 20080927 | ||
27 | |||
28 | Copyright (c) 2005-2008 Lode Vandevenne | ||
29 | |||
30 | This software is provided 'as-is', without any express or implied | ||
31 | warranty. In no event will the authors be held liable for any damages | ||
32 | arising from the use of this software. | ||
33 | |||
34 | Permission is granted to anyone to use this software for any purpose, | ||
35 | including commercial applications, and to alter it and redistribute it | ||
36 | freely, subject to the following restrictions: | ||
37 | |||
38 | 1. The origin of this software must not be misrepresented; you must not | ||
39 | claim that you wrote the original software. If you use this software | ||
40 | in a product, an acknowledgment in the product documentation would be | ||
41 | appreciated but is not required. | ||
42 | |||
43 | 2. Altered source versions must be plainly marked as such, and must not be | ||
44 | misrepresented as being the original software. | ||
45 | |||
46 | 3. This notice may not be removed or altered from any source | ||
47 | distribution. | ||
48 | */ | ||
49 | |||
50 | /* | ||
51 | The manual and changelog can be found in the header file "lodepng.h" | ||
52 | You are free to name this file lodepng.cpp or lodepng.c depending on your usage. | ||
53 | */ | ||
54 | |||
55 | #include "plugin.h" | 30 | #include "plugin.h" |
56 | #include "lcd.h" | 31 | #include "lcd.h" |
57 | #include <lib/pluginlib_bmp.h> | 32 | #include <lib/pluginlib_bmp.h> |
58 | #include "zlib.h" | 33 | #include "tinf.h" |
59 | #include "png.h" | 34 | #include "png.h" |
35 | #include "png_decoder.h" | ||
36 | #include "bmp.h" | ||
60 | 37 | ||
61 | /* ////////////////////////////////////////////////////////////////////////// */ | 38 | /* decoder context struct */ |
62 | /* LodeFlate & LodeZlib Setting structs */ | 39 | static LodePNG_Decoder decoder; |
63 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
64 | |||
65 | typedef struct LodePNG_InfoColor /*info about the color type of an image*/ | ||
66 | { | ||
67 | /*header (IHDR)*/ | ||
68 | unsigned colorType; /*color type*/ | ||
69 | unsigned bitDepth; /*bits per sample*/ | ||
70 | |||
71 | /*palette (PLTE)*/ | ||
72 | unsigned char palette[256 * 4]; /*palette in RGBARGBA... order*/ | ||
73 | size_t palettesize; /*palette size in number of colors (amount of bytes is 4 * palettesize)*/ | ||
74 | 40 | ||
75 | /*transparent color key (tRNS)*/ | 41 | /* my memory pool (from the mp3 buffer) */ |
76 | unsigned key_defined; /*is a transparent color key given?*/ | 42 | static char print[128]; /* use a common snprintf() buffer */ |
77 | unsigned key_r; /*red component of color key*/ | ||
78 | unsigned key_g; /*green component of color key*/ | ||
79 | unsigned key_b; /*blue component of color key*/ | ||
80 | } LodePNG_InfoColor; | ||
81 | |||
82 | typedef struct LodePNG_Time /*LodePNG's encoder does not generate the current time. To make it add a time chunk the correct time has to be provided*/ | ||
83 | { | ||
84 | unsigned year; /*2 bytes*/ | ||
85 | unsigned char month; /*1-12*/ | ||
86 | unsigned char day; /*1-31*/ | ||
87 | unsigned char hour; /*0-23*/ | ||
88 | unsigned char minute; /*0-59*/ | ||
89 | unsigned char second; /*0-60 (to allow for leap seconds)*/ | ||
90 | } LodePNG_Time; | ||
91 | |||
92 | typedef struct LodePNG_InfoPng /*information about the PNG image, except pixels and sometimes except width and height*/ | ||
93 | { | ||
94 | /*header (IHDR), palette (PLTE) and transparency (tRNS)*/ | ||
95 | unsigned width; /*width of the image in pixels (ignored by encoder, but filled in by decoder)*/ | ||
96 | unsigned height; /*height of the image in pixels (ignored by encoder, but filled in by decoder)*/ | ||
97 | unsigned compressionMethod; /*compression method of the original file*/ | ||
98 | unsigned filterMethod; /*filter method of the original file*/ | ||
99 | unsigned interlaceMethod; /*interlace method of the original file*/ | ||
100 | LodePNG_InfoColor color; /*color type and bits, palette, transparency*/ | ||
101 | |||
102 | /*suggested background color (bKGD)*/ | ||
103 | unsigned background_defined; /*is a suggested background color given?*/ | ||
104 | unsigned background_r; /*red component of suggested background color*/ | ||
105 | unsigned background_g; /*green component of suggested background color*/ | ||
106 | unsigned background_b; /*blue component of suggested background color*/ | ||
107 | |||
108 | /*time chunk (tIME)*/ | ||
109 | unsigned char time_defined; /*if 0, no tIME chunk was or will be generated in the PNG image*/ | ||
110 | LodePNG_Time time; | ||
111 | |||
112 | /*phys chunk (pHYs)*/ | ||
113 | unsigned phys_defined; /*is pHYs chunk defined?*/ | ||
114 | unsigned phys_x; | ||
115 | unsigned phys_y; | ||
116 | unsigned char phys_unit; /*may be 0 (unknown unit) or 1 (metre)*/ | ||
117 | |||
118 | } LodePNG_InfoPng; | ||
119 | |||
120 | typedef struct LodePNG_InfoRaw /*contains user-chosen information about the raw image data, which is independent of the PNG image*/ | ||
121 | { | ||
122 | LodePNG_InfoColor color; | ||
123 | } LodePNG_InfoRaw; | ||
124 | |||
125 | typedef struct LodePNG_DecodeSettings | ||
126 | { | ||
127 | unsigned color_convert; /*whether to convert the PNG to the color type you want. Default: yes*/ | ||
128 | } LodePNG_DecodeSettings; | ||
129 | |||
130 | typedef struct LodePNG_Decoder | ||
131 | { | ||
132 | LodePNG_DecodeSettings settings; | ||
133 | LodePNG_InfoRaw infoRaw; | ||
134 | LodePNG_InfoPng infoPng; /*info of the PNG image obtained after decoding*/ | ||
135 | long error; | ||
136 | char error_msg[128]; | ||
137 | } LodePNG_Decoder; | ||
138 | |||
139 | #define VERSION_STRING "20080927" | ||
140 | 43 | ||
141 | /* decompressed image in the possible sizes (1,2,4,8), wasting the other */ | 44 | /* decompressed image in the possible sizes (1,2,4,8), wasting the other */ |
142 | static fb_data *disp[9]; | 45 | static fb_data *disp[9]; |
143 | /* up to here currently used by image(s) */ | ||
144 | static fb_data *disp_buf; | 46 | static fb_data *disp_buf; |
145 | 47 | ||
146 | /* my memory pool (from the mp3 buffer) */ | 48 | #if defined(HAVE_LCD_COLOR) |
147 | static char print[32]; /* use a common snprintf() buffer */ | 49 | #define resize_bitmap smooth_resize_bitmap |
148 | 50 | #else | |
149 | unsigned char *memory, *memory_max; /* inffast.c needs memory_max */ | 51 | #define resize_bitmap grey_resize_bitmap |
150 | static size_t memory_size; | 52 | #endif |
151 | |||
152 | static unsigned char *image; /* where we put the content of the file */ | ||
153 | static size_t image_size; | ||
154 | |||
155 | static fb_data *converted_image; /* the (color) converted image */ | ||
156 | static size_t converted_image_size; | ||
157 | |||
158 | static unsigned char *decoded_image; /* the decoded image */ | ||
159 | static size_t decoded_image_size; | ||
160 | |||
161 | static LodePNG_Decoder _decoder; | ||
162 | |||
163 | /* | ||
164 | The two functions below (LodePNG_decompress and LodePNG_compress) directly call the | ||
165 | LodeZlib_decompress and LodeZlib_compress functions. The only purpose of the functions | ||
166 | below, is to provide the ability to let LodePNG use a different Zlib encoder by only | ||
167 | changing the two functions below, instead of changing it inside the vareous places | ||
168 | in the other LodePNG functions. | ||
169 | |||
170 | *out must be NULL and *outsize must be 0 initially, and after the function is done, | ||
171 | *out must point to the decompressed data, *outsize must be the size of it, and must | ||
172 | be the size of the useful data in bytes, not the alloc size. | ||
173 | */ | ||
174 | |||
175 | static unsigned LodePNG_decompress(unsigned char* out, size_t* outsize, const unsigned char* in, size_t insize, char *error_msg) | ||
176 | { | ||
177 | z_stream stream; | ||
178 | int err; | ||
179 | |||
180 | rb->strcpy(error_msg, ""); | ||
181 | |||
182 | stream.next_in = (Bytef*)in; | ||
183 | stream.avail_in = (uInt)insize; | ||
184 | |||
185 | stream.next_out = out; | ||
186 | stream.avail_out = (uInt)*outsize; | ||
187 | |||
188 | stream.zalloc = (alloc_func)0; | ||
189 | stream.zfree = (free_func)0; | ||
190 | |||
191 | err = inflateInit(&stream); | ||
192 | if (err != Z_OK) return err; | ||
193 | |||
194 | err = inflate(&stream, Z_FINISH); | ||
195 | if (err != Z_STREAM_END) { | ||
196 | inflateEnd(&stream); | ||
197 | if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) | ||
198 | return Z_DATA_ERROR; | ||
199 | return err; | ||
200 | } | ||
201 | *outsize = stream.total_out; | ||
202 | |||
203 | err = inflateEnd(&stream); | ||
204 | if (stream.msg != Z_NULL) | ||
205 | rb->strcpy(error_msg, stream.msg); | ||
206 | return err; | ||
207 | } | ||
208 | |||
209 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
210 | /* / Reading and writing single bits and bytes from/to stream for LodePNG / */ | ||
211 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
212 | |||
213 | static unsigned char readBitFromReversedStream(size_t* bitpointer, const unsigned char* bitstream) | ||
214 | { | ||
215 | unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> (7 - ((*bitpointer) & 0x7))) & 1); | ||
216 | (*bitpointer)++; | ||
217 | return result; | ||
218 | } | ||
219 | |||
220 | static unsigned readBitsFromReversedStream(size_t* bitpointer, const unsigned char* bitstream, size_t nbits) | ||
221 | { | ||
222 | unsigned result = 0; | ||
223 | size_t i; | ||
224 | for (i = nbits - 1; i < nbits; i--) result += (unsigned)readBitFromReversedStream(bitpointer, bitstream) << i; | ||
225 | return result; | ||
226 | } | ||
227 | |||
228 | static void setBitOfReversedStream0(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) | ||
229 | { | ||
230 | /*the current bit in bitstream must be 0 for this to work*/ | ||
231 | if (bit) bitstream[(*bitpointer) >> 3] |= (bit << (7 - ((*bitpointer) & 0x7))); /*earlier bit of huffman code is in a lesser significant bit of an earlier byte*/ | ||
232 | (*bitpointer)++; | ||
233 | } | ||
234 | |||
235 | static void setBitOfReversedStream(size_t* bitpointer, unsigned char* bitstream, unsigned char bit) | ||
236 | { | ||
237 | /*the current bit in bitstream may be 0 or 1 for this to work*/ | ||
238 | if (bit == 0) bitstream[(*bitpointer) >> 3] &= (unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7)))); | ||
239 | else bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7))); | ||
240 | (*bitpointer)++; | ||
241 | } | ||
242 | |||
243 | static unsigned LodePNG_read32bitInt(const unsigned char* buffer) | ||
244 | { | ||
245 | return (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]; | ||
246 | } | ||
247 | |||
248 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
249 | /* / PNG chunks / */ | ||
250 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
251 | |||
252 | unsigned LodePNG_chunk_length(const unsigned char* chunk) /*get the length of the data of the chunk. Total chunk length has 12 bytes more.*/ | ||
253 | { | ||
254 | return LodePNG_read32bitInt(&chunk[0]); | ||
255 | } | ||
256 | |||
257 | void LodePNG_chunk_type(char type[5], const unsigned char* chunk) /*puts the 4-byte type in null terminated string*/ | ||
258 | { | ||
259 | unsigned i; | ||
260 | for (i = 0; i < 4; i++) type[i] = chunk[4 + i]; | ||
261 | type[4] = 0; /*null termination char*/ | ||
262 | } | ||
263 | |||
264 | unsigned char LodePNG_chunk_type_equals(const unsigned char* chunk, const char* type) /*check if the type is the given type*/ | ||
265 | { | ||
266 | if (type[4] != 0) return 0; | ||
267 | return (chunk[4] == type[0] && chunk[5] == type[1] && chunk[6] == type[2] && chunk[7] == type[3]); | ||
268 | } | ||
269 | |||
270 | /*properties of PNG chunks gotten from capitalization of chunk type name, as defined by the standard*/ | ||
271 | unsigned char LodePNG_chunk_critical(const unsigned char* chunk) /*0: ancillary chunk, 1: it's one of the critical chunk types*/ | ||
272 | { | ||
273 | return((chunk[4] & 32) == 0); | ||
274 | } | ||
275 | |||
276 | unsigned char LodePNG_chunk_private(const unsigned char* chunk) /*0: public, 1: private*/ | ||
277 | { | ||
278 | return((chunk[6] & 32) != 0); | ||
279 | } | ||
280 | |||
281 | unsigned char LodePNG_chunk_safetocopy(const unsigned char* chunk) /*0: the chunk is unsafe to copy, 1: the chunk is safe to copy*/ | ||
282 | { | ||
283 | return((chunk[7] & 32) != 0); | ||
284 | } | ||
285 | |||
286 | unsigned char* LodePNG_chunk_data(unsigned char* chunk) /*get pointer to the data of the chunk*/ | ||
287 | { | ||
288 | return &chunk[8]; | ||
289 | } | ||
290 | |||
291 | const unsigned char* LodePNG_chunk_data_const(const unsigned char* chunk) /*get pointer to the data of the chunk*/ | ||
292 | { | ||
293 | return &chunk[8]; | ||
294 | } | ||
295 | |||
296 | unsigned LodePNG_chunk_check_crc(const unsigned char* chunk) /*returns 0 if the crc is correct, error code if it's incorrect*/ | ||
297 | { | ||
298 | unsigned length = LodePNG_chunk_length(chunk); | ||
299 | unsigned CRC = LodePNG_read32bitInt(&chunk[length + 8]); | ||
300 | unsigned checksum = crc32(0L, &chunk[4], length + 4); /*the CRC is taken of the data and the 4 chunk type letters, not the length*/ | ||
301 | if (CRC != checksum) return 1; | ||
302 | else return 0; | ||
303 | } | ||
304 | |||
305 | unsigned char* LodePNG_chunk_next(unsigned char* chunk) /*don't use on IEND chunk, as there is no next chunk then*/ | ||
306 | { | ||
307 | unsigned total_chunk_length = LodePNG_chunk_length(chunk) + 12; | ||
308 | return &chunk[total_chunk_length]; | ||
309 | } | ||
310 | |||
311 | const unsigned char* LodePNG_chunk_next_const(const unsigned char* chunk) /*don't use on IEND chunk, as there is no next chunk then*/ | ||
312 | { | ||
313 | unsigned total_chunk_length = LodePNG_chunk_length(chunk) + 12; | ||
314 | return &chunk[total_chunk_length]; | ||
315 | } | ||
316 | |||
317 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
318 | /* / Color types and such / */ | ||
319 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
320 | |||
321 | /*return type is a LodePNG error code*/ | ||
322 | static unsigned checkColorValidity(unsigned colorType, unsigned bd) /*bd = bitDepth*/ | ||
323 | { | ||
324 | switch (colorType) | ||
325 | { | ||
326 | case 0: | ||
327 | if (!(bd == 1 || bd == 2 || bd == 4 || bd == 8 || bd == 16)) return 37; break; /*grey*/ | ||
328 | case 2: | ||
329 | if (!( bd == 8 || bd == 16)) return 37; break; /*RGB*/ | ||
330 | case 3: | ||
331 | if (!(bd == 1 || bd == 2 || bd == 4 || bd == 8 )) return 37; break; /*palette*/ | ||
332 | case 4: | ||
333 | if (!( bd == 8 || bd == 16)) return 37; break; /*grey + alpha*/ | ||
334 | case 6: | ||
335 | if (!( bd == 8 || bd == 16)) return 37; break; /*RGBA*/ | ||
336 | default: | ||
337 | return 31; | ||
338 | } | ||
339 | return 0; /*allowed color type / bits combination*/ | ||
340 | } | ||
341 | |||
342 | static unsigned getNumColorChannels(unsigned colorType) | ||
343 | { | ||
344 | switch (colorType) | ||
345 | { | ||
346 | case 0: | ||
347 | return 1; /*grey*/ | ||
348 | case 2: | ||
349 | return 3; /*RGB*/ | ||
350 | case 3: | ||
351 | return 1; /*palette*/ | ||
352 | case 4: | ||
353 | return 2; /*grey + alpha*/ | ||
354 | case 6: | ||
355 | return 4; /*RGBA*/ | ||
356 | } | ||
357 | return 0; /*unexisting color type*/ | ||
358 | } | ||
359 | |||
360 | static unsigned getBpp(unsigned colorType, unsigned bitDepth) | ||
361 | { | ||
362 | return getNumColorChannels(colorType) * bitDepth; /*bits per pixel is amount of channels * bits per channel*/ | ||
363 | } | ||
364 | |||
365 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
366 | |||
367 | void LodePNG_InfoColor_init(LodePNG_InfoColor* info) | ||
368 | { | ||
369 | info->key_defined = 0; | ||
370 | info->key_r = info->key_g = info->key_b = 0; | ||
371 | info->colorType = 6; | ||
372 | info->bitDepth = 8; | ||
373 | memset(info->palette, 0, 256 * 4 * sizeof(unsigned char)); | ||
374 | info->palettesize = 0; | ||
375 | } | ||
376 | |||
377 | void LodePNG_InfoColor_cleanup(LodePNG_InfoColor* info) | ||
378 | { | ||
379 | info->palettesize = 0; | ||
380 | } | ||
381 | |||
382 | unsigned LodePNG_InfoColor_getBpp(const LodePNG_InfoColor* info) { return getBpp(info->colorType, info->bitDepth); } /*calculate bits per pixel out of colorType and bitDepth*/ | ||
383 | unsigned LodePNG_InfoColor_isGreyscaleType(const LodePNG_InfoColor* info) { return info->colorType == 0 || info->colorType == 4; } | ||
384 | |||
385 | unsigned LodePNG_InfoColor_equal(const LodePNG_InfoColor* info1, const LodePNG_InfoColor* info2) | ||
386 | { | ||
387 | return info1->colorType == info2->colorType | ||
388 | && info1->bitDepth == info2->bitDepth; /*palette and color key not compared*/ | ||
389 | } | ||
390 | |||
391 | void LodePNG_InfoPng_init(LodePNG_InfoPng* info) | ||
392 | { | ||
393 | info->width = info->height = 0; | ||
394 | LodePNG_InfoColor_init(&info->color); | ||
395 | info->interlaceMethod = 0; | ||
396 | info->compressionMethod = 0; | ||
397 | info->filterMethod = 0; | ||
398 | info->background_defined = 0; | ||
399 | info->background_r = info->background_g = info->background_b = 0; | ||
400 | |||
401 | info->time_defined = 0; | ||
402 | info->phys_defined = 0; | ||
403 | } | ||
404 | |||
405 | void LodePNG_InfoPng_cleanup(LodePNG_InfoPng* info) | ||
406 | { | ||
407 | LodePNG_InfoColor_cleanup(&info->color); | ||
408 | } | ||
409 | |||
410 | unsigned LodePNG_InfoColor_copy(LodePNG_InfoColor* dest, const LodePNG_InfoColor* source) | ||
411 | { | ||
412 | size_t i; | ||
413 | LodePNG_InfoColor_cleanup(dest); | ||
414 | *dest = *source; | ||
415 | for (i = 0; i < source->palettesize * 4; i++) dest->palette[i] = source->palette[i]; | ||
416 | return 0; | ||
417 | } | ||
418 | |||
419 | unsigned LodePNG_InfoPng_copy(LodePNG_InfoPng* dest, const LodePNG_InfoPng* source) | ||
420 | { | ||
421 | unsigned error = 0; | ||
422 | LodePNG_InfoPng_cleanup(dest); | ||
423 | *dest = *source; | ||
424 | LodePNG_InfoColor_init(&dest->color); | ||
425 | error = LodePNG_InfoColor_copy(&dest->color, &source->color); if (error) return error; | ||
426 | return error; | ||
427 | } | ||
428 | |||
429 | void LodePNG_InfoPng_swap(LodePNG_InfoPng* a, LodePNG_InfoPng* b) | ||
430 | { | ||
431 | LodePNG_InfoPng temp = *a; | ||
432 | *a = *b; | ||
433 | *b = temp; | ||
434 | } | ||
435 | |||
436 | void LodePNG_InfoRaw_init(LodePNG_InfoRaw* info) | ||
437 | { | ||
438 | LodePNG_InfoColor_init(&info->color); | ||
439 | } | ||
440 | |||
441 | void LodePNG_InfoRaw_cleanup(LodePNG_InfoRaw* info) | ||
442 | { | ||
443 | LodePNG_InfoColor_cleanup(&info->color); | ||
444 | } | ||
445 | |||
446 | unsigned LodePNG_InfoRaw_copy(LodePNG_InfoRaw* dest, const LodePNG_InfoRaw* source) | ||
447 | { | ||
448 | unsigned error = 0; | ||
449 | LodePNG_InfoRaw_cleanup(dest); | ||
450 | *dest = *source; | ||
451 | LodePNG_InfoColor_init(&dest->color); | ||
452 | error = LodePNG_InfoColor_copy(&dest->color, &source->color); if (error) return error; | ||
453 | return error; | ||
454 | } | ||
455 | |||
456 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
457 | |||
458 | /* | ||
459 | converts from any color type to 24-bit or 32-bit (later maybe more supported). return value = LodePNG error code | ||
460 | the out buffer must have (w * h * bpp + 7) / 8 bytes, where bpp is the bits per pixel of the output color type (LodePNG_InfoColor_getBpp) | ||
461 | for < 8 bpp images, there may _not_ be padding bits at the end of scanlines. | ||
462 | */ | ||
463 | unsigned LodePNG_convert(fb_data* out, const unsigned char* in, LodePNG_InfoColor* infoOut, LodePNG_InfoColor* infoIn, unsigned w, unsigned h) | ||
464 | { | ||
465 | size_t i, j, bp = 0; /*bitpointer, used by less-than-8-bit color types*/ | ||
466 | size_t x, y; | ||
467 | unsigned char c; | ||
468 | |||
469 | if (!running_slideshow) | ||
470 | { | ||
471 | rb->lcd_puts(0, 3, "color conversion in progress"); | ||
472 | rb->lcd_update(); | ||
473 | } | ||
474 | |||
475 | /*cases where in and out already have the same format*/ | ||
476 | if (LodePNG_InfoColor_equal(infoIn, infoOut)) | ||
477 | { | ||
478 | |||
479 | i = 0; | ||
480 | j = 0; | ||
481 | for (y = 0 ; y < h ; y++) { | ||
482 | for (x = 0 ; x < w ; x++) { | ||
483 | unsigned char r = in[i++]; | ||
484 | unsigned char g = in[i++]; | ||
485 | unsigned char b = in[i++]; | ||
486 | out[j++] = LCD_RGBPACK(r,g,b); | ||
487 | } | ||
488 | } | ||
489 | return 0; | ||
490 | } | ||
491 | |||
492 | if ((infoOut->colorType == 2 || infoOut->colorType == 6) && infoOut->bitDepth == 8) | ||
493 | { | ||
494 | if (infoIn->bitDepth == 8) | ||
495 | { | ||
496 | switch (infoIn->colorType) | ||
497 | { | ||
498 | case 0: /*greyscale color*/ | ||
499 | i = 0; | ||
500 | for (y = 0 ; y < h ; y++) { | ||
501 | for (x = 0 ; x < w ; x++) { | ||
502 | c=in[i]; | ||
503 | //unsigned char r = in[i]; | ||
504 | //unsigned char g = in[i]; | ||
505 | //unsigned char b = in[i]; | ||
506 | out[i++] = LCD_RGBPACK(c,c,c); | ||
507 | } | ||
508 | } | ||
509 | break; | ||
510 | case 2: /*RGB color*/ | ||
511 | i = 0; | ||
512 | for (y = 0 ; y < h ; y++) { | ||
513 | for (x = 0 ; x < w ; x++) { | ||
514 | j = 3 * i; | ||
515 | unsigned char r = in[j]; | ||
516 | unsigned char g = in[j + 1]; | ||
517 | unsigned char b = in[j + 2]; | ||
518 | out[i++] = LCD_RGBPACK(r,g,b); | ||
519 | } | ||
520 | } | ||
521 | break; | ||
522 | case 3: /*indexed color (palette)*/ | ||
523 | i = 0; | ||
524 | for (y = 0 ; y < h ; y++) { | ||
525 | for (x = 0 ; x < w ; x++) { | ||
526 | if (in[i] >= infoIn->palettesize) return 46; | ||
527 | j = in[i] << 2; | ||
528 | unsigned char r = infoIn->palette[j]; | ||
529 | unsigned char g = infoIn->palette[j + 1]; | ||
530 | unsigned char b = infoIn->palette[j + 2]; | ||
531 | out[i++] = LCD_RGBPACK(r,g,b); | ||
532 | } | ||
533 | } | ||
534 | break; | ||
535 | case 4: /*greyscale with alpha*/ | ||
536 | i = 0; | ||
537 | for (y = 0 ; y < h ; y++) { | ||
538 | for (x = 0 ; x < w ; x++) { | ||
539 | c = in[i << 1]; | ||
540 | //unsigned char r = in[i<<1]; | ||
541 | //unsigned char g = in[i<<1]; | ||
542 | //unsigned char b = in[i<<1]; | ||
543 | out[i++] = LCD_RGBPACK(c,c,c); | ||
544 | } | ||
545 | } | ||
546 | break; | ||
547 | case 6: /*RGB with alpha*/ | ||
548 | i = 0; | ||
549 | for (y = 0 ; y < h ; y++) { | ||
550 | for (x = 0 ; x < w ; x++) { | ||
551 | j = i << 2; | ||
552 | unsigned char r = in[j]; | ||
553 | unsigned char g = in[j + 1]; | ||
554 | unsigned char b = in[j + 2]; | ||
555 | out[i++] = LCD_RGBPACK(r,g,b); | ||
556 | } | ||
557 | } | ||
558 | break; | ||
559 | default: | ||
560 | break; | ||
561 | } | ||
562 | } | ||
563 | else if (infoIn->bitDepth == 16) | ||
564 | { | ||
565 | switch (infoIn->colorType) | ||
566 | { | ||
567 | case 0: /*greyscale color*/ | ||
568 | i = 0; | ||
569 | for (y = 0 ; y < h ; y++) { | ||
570 | for (x = 0 ; x < w ; x++) { | ||
571 | c = in[i << 1]; | ||
572 | //unsigned char r = in[2 * i]; | ||
573 | //unsigned char g = in[2 * i]; | ||
574 | //unsigned char b = in[2 * i]; | ||
575 | out[i++] = LCD_RGBPACK(c,c,c); | ||
576 | } | ||
577 | } | ||
578 | break; | ||
579 | case 2: /*RGB color*/ | ||
580 | i = 0; | ||
581 | for (y = 0 ; y < h ; y++) { | ||
582 | for (x = 0 ; x < w ; x++) { | ||
583 | j = 6 * i; | ||
584 | unsigned char r = in[j]; | ||
585 | unsigned char g = in[j + 2]; | ||
586 | unsigned char b = in[j + 4]; | ||
587 | out[i++] = LCD_RGBPACK(r,g,b); | ||
588 | } | ||
589 | } | ||
590 | break; | ||
591 | case 4: /*greyscale with alpha*/ | ||
592 | i = 0; | ||
593 | for (y = 0 ; y < h ; y++) { | ||
594 | for (x = 0 ; x < w ; x++) { | ||
595 | c = in[i << 2]; | ||
596 | //unsigned char r = in[4 * i]; | ||
597 | //unsigned char g = in[4 * i]; | ||
598 | //unsigned char b = in[4 * i]; | ||
599 | out[i++] = LCD_RGBPACK(c,c,c); | ||
600 | } | ||
601 | } | ||
602 | break; | ||
603 | case 6: /*RGB with alpha*/ | ||
604 | i = 0; | ||
605 | for (y = 0 ; y < h ; y++) { | ||
606 | for (x = 0 ; x < w ; x++) { | ||
607 | j = i << 3; | ||
608 | unsigned char r = in[j]; | ||
609 | unsigned char g = in[j + 2]; | ||
610 | unsigned char b = in[j + 4]; | ||
611 | out[i++] = LCD_RGBPACK(r,g,b); | ||
612 | } | ||
613 | } | ||
614 | break; | ||
615 | default: | ||
616 | break; | ||
617 | } | ||
618 | } | ||
619 | else /*infoIn->bitDepth is less than 8 bit per channel*/ | ||
620 | { | ||
621 | switch (infoIn->colorType) | ||
622 | { | ||
623 | case 0: /*greyscale color*/ | ||
624 | i = 0; | ||
625 | for (y = 0 ; y < h ; y++) { | ||
626 | for (x = 0 ; x < w ; x++) { | ||
627 | unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth); | ||
628 | value = (value * 255) / ((1 << infoIn->bitDepth) - 1); /*scale value from 0 to 255*/ | ||
629 | unsigned char r = (unsigned char)value; | ||
630 | unsigned char g = (unsigned char)value; | ||
631 | unsigned char b = (unsigned char)value; | ||
632 | out[i++] = LCD_RGBPACK(r,g,b); | ||
633 | } | ||
634 | } | ||
635 | break; | ||
636 | case 3: /*indexed color (palette)*/ | ||
637 | i = 0; | ||
638 | for (y = 0 ; y < h ; y++) { | ||
639 | for (x = 0 ; x < w ; x++) { | ||
640 | unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth); | ||
641 | if (value >= infoIn->palettesize) return 47; | ||
642 | j = value << 2; | ||
643 | unsigned char r = infoIn->palette[j]; | ||
644 | unsigned char g = infoIn->palette[j + 1]; | ||
645 | unsigned char b = infoIn->palette[j + 2]; | ||
646 | out[i++] = LCD_RGBPACK(r,g,b); | ||
647 | } | ||
648 | } | ||
649 | break; | ||
650 | default: | ||
651 | break; | ||
652 | } | ||
653 | } | ||
654 | } | ||
655 | else if (LodePNG_InfoColor_isGreyscaleType(infoOut) && infoOut->bitDepth == 8) /*conversion from greyscale to greyscale*/ | ||
656 | { | ||
657 | if (!LodePNG_InfoColor_isGreyscaleType(infoIn)) return 62; | ||
658 | if (infoIn->bitDepth == 8) | ||
659 | { | ||
660 | switch (infoIn->colorType) | ||
661 | { | ||
662 | case 0: /*greyscale color*/ | ||
663 | i = 0; | ||
664 | for (y = 0 ; y < h ; y++) { | ||
665 | for (x = 0 ; x < w ; x++) { | ||
666 | c = in[i]; | ||
667 | //unsigned char r = in[i]; | ||
668 | //unsigned char g = in[i]; | ||
669 | //unsigned char b = in[i]; | ||
670 | out[i++] = LCD_RGBPACK(c,c,c); | ||
671 | } | ||
672 | } | ||
673 | break; | ||
674 | case 4: /*greyscale with alpha*/ | ||
675 | i = 0; | ||
676 | for (y = 0 ; y < h ; y++) { | ||
677 | for (x = 0 ; x < w ; x++) { | ||
678 | c = in[(i << 1) + 1]; | ||
679 | //unsigned char r = in[2 * i + 1]; | ||
680 | //unsigned char g = in[2 * i + 1]; | ||
681 | //unsigned char b = in[2 * i + 1]; | ||
682 | out[i++] = LCD_RGBPACK(c,c,c); | ||
683 | } | ||
684 | } | ||
685 | break; | ||
686 | default: | ||
687 | return 31; | ||
688 | } | ||
689 | } | ||
690 | else if (infoIn->bitDepth == 16) | ||
691 | { | ||
692 | switch (infoIn->colorType) | ||
693 | { | ||
694 | case 0: /*greyscale color*/ | ||
695 | i = 0; | ||
696 | for (y = 0 ; y < h ; y++) { | ||
697 | for (x = 0 ; x < w ; x++) { | ||
698 | c = in[i << 1]; | ||
699 | //unsigned char r = in[2 * i]; | ||
700 | //unsigned char g = in[2 * i]; | ||
701 | //unsigned char b = in[2 * i]; | ||
702 | out[i++] = LCD_RGBPACK(c,c,c); | ||
703 | } | ||
704 | } | ||
705 | break; | ||
706 | case 4: /*greyscale with alpha*/ | ||
707 | i = 0; | ||
708 | for (y = 0 ; y < h ; y++) { | ||
709 | for (x = 0 ; x < w ; x++) { | ||
710 | c = in[i << 2]; | ||
711 | //unsigned char r = in[4 * i]; | ||
712 | //unsigned char g = in[4 * i]; | ||
713 | //unsigned char b = in[4 * i]; | ||
714 | out[i++] = LCD_RGBPACK(c,c,c); | ||
715 | } | ||
716 | } | ||
717 | break; | ||
718 | default: | ||
719 | return 31; | ||
720 | } | ||
721 | } | ||
722 | else /*infoIn->bitDepth is less than 8 bit per channel*/ | ||
723 | { | ||
724 | if (infoIn->colorType != 0) return 31; /*colorType 0 is the only greyscale type with < 8 bits per channel*/ | ||
725 | i = 0; | ||
726 | for (y = 0 ; y < h ; y++) { | ||
727 | for (x = 0 ; x < w ; x++) { | ||
728 | unsigned value = readBitsFromReversedStream(&bp, in, infoIn->bitDepth); | ||
729 | value = (value * 255) / ((1 << infoIn->bitDepth) - 1); /*scale value from 0 to 255*/ | ||
730 | unsigned char r = (unsigned char)value; | ||
731 | unsigned char g = (unsigned char)value; | ||
732 | unsigned char b = (unsigned char)value; | ||
733 | out[i++] = LCD_RGBPACK(r,g,b); | ||
734 | } | ||
735 | } | ||
736 | } | ||
737 | } | ||
738 | else return 59; | ||
739 | |||
740 | return 0; | ||
741 | } | ||
742 | |||
743 | /*Paeth predicter, used by PNG filter type 4*/ | ||
744 | static int paethPredictor(int a, int b, int c) | ||
745 | { | ||
746 | int p = a + b - c; | ||
747 | int pa = p > a ? p - a : a - p; | ||
748 | int pb = p > b ? p - b : b - p; | ||
749 | int pc = p > c ? p - c : c - p; | ||
750 | |||
751 | if (pa <= pb && pa <= pc) return a; | ||
752 | else if (pb <= pc) return b; | ||
753 | else return c; | ||
754 | } | ||
755 | |||
756 | /*shared values used by multiple Adam7 related functions*/ | ||
757 | |||
758 | static const unsigned ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/ | ||
759 | static const unsigned ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/ | ||
760 | static const unsigned ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/ | ||
761 | static const unsigned ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/ | ||
762 | |||
763 | static void Adam7_getpassvalues(unsigned passw[7], unsigned passh[7], size_t filter_passstart[8], size_t padded_passstart[8], size_t passstart[8], unsigned w, unsigned h, unsigned bpp) | ||
764 | { | ||
765 | /*the passstart values have 8 values: the 8th one actually indicates the byte after the end of the 7th (= last) pass*/ | ||
766 | unsigned i; | ||
767 | |||
768 | /*calculate width and height in pixels of each pass*/ | ||
769 | for (i = 0; i < 7; i++) | ||
770 | { | ||
771 | passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i]; | ||
772 | passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i]; | ||
773 | if (passw[i] == 0) passh[i] = 0; | ||
774 | if (passh[i] == 0) passw[i] = 0; | ||
775 | } | ||
776 | |||
777 | filter_passstart[0] = padded_passstart[0] = passstart[0] = 0; | ||
778 | for (i = 0; i < 7; i++) | ||
779 | { | ||
780 | filter_passstart[i + 1] = filter_passstart[i] + ((passw[i] && passh[i]) ? passh[i] * (1 + (passw[i] * bpp + 7) / 8) : 0); /*if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte)*/ | ||
781 | padded_passstart[i + 1] = padded_passstart[i] + passh[i] * ((passw[i] * bpp + 7) / 8); /*bits padded if needed to fill full byte at end of each scanline*/ | ||
782 | passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7) / 8; /*only padded at end of reduced image*/ | ||
783 | } | ||
784 | } | ||
785 | |||
786 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
787 | /* / PNG Decoder / */ | ||
788 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
789 | |||
790 | /*read the information from the header and store it in the LodePNG_Info. return value is error*/ | ||
791 | void LodePNG_inspect(LodePNG_Decoder* decoder, const unsigned char* in, size_t inlength) | ||
792 | { | ||
793 | if (inlength == 0 || in == 0) { decoder->error = 48; return; } /*the given data is empty*/ | ||
794 | if (inlength < 29) { decoder->error = 27; return; } /*error: the data length is smaller than the length of the header*/ | ||
795 | |||
796 | /*when decoding a new PNG image, make sure all parameters created after previous decoding are reset*/ | ||
797 | LodePNG_InfoPng_cleanup(&decoder->infoPng); | ||
798 | LodePNG_InfoPng_init(&decoder->infoPng); | ||
799 | decoder->error = 0; | ||
800 | |||
801 | if (in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71 || in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) { decoder->error = 28; return; } /*error: the first 8 bytes are not the correct PNG signature*/ | ||
802 | if (in[12] != 'I' || in[13] != 'H' || in[14] != 'D' || in[15] != 'R') { decoder->error = 29; return; } /*error: it doesn't start with a IHDR chunk!*/ | ||
803 | |||
804 | /*read the values given in the header*/ | ||
805 | decoder->infoPng.width = LodePNG_read32bitInt(&in[16]); | ||
806 | decoder->infoPng.height = LodePNG_read32bitInt(&in[20]); | ||
807 | decoder->infoPng.color.bitDepth = in[24]; | ||
808 | decoder->infoPng.color.colorType = in[25]; | ||
809 | decoder->infoPng.compressionMethod = in[26]; | ||
810 | decoder->infoPng.filterMethod = in[27]; | ||
811 | decoder->infoPng.interlaceMethod = in[28]; | ||
812 | |||
813 | unsigned CRC = LodePNG_read32bitInt(&in[29]); | ||
814 | unsigned checksum = crc32(0L, &in[12], 17); | ||
815 | if (CRC != checksum) { decoder->error = 57; return; } | ||
816 | |||
817 | if (decoder->infoPng.compressionMethod != 0) { decoder->error = 32; return; } /*error: only compression method 0 is allowed in the specification*/ | ||
818 | if (decoder->infoPng.filterMethod != 0) { decoder->error = 33; return; } /*error: only filter method 0 is allowed in the specification*/ | ||
819 | if (decoder->infoPng.interlaceMethod > 1) { decoder->error = 34; return; } /*error: only interlace methods 0 and 1 exist in the specification*/ | ||
820 | |||
821 | decoder->error = checkColorValidity(decoder->infoPng.color.colorType, decoder->infoPng.color.bitDepth); | ||
822 | } | ||
823 | |||
824 | static unsigned unfilterScanline(unsigned char* recon, const unsigned char* scanline, const unsigned char* precon, size_t bytewidth, unsigned char filterType, size_t length) | ||
825 | { | ||
826 | /* | ||
827 | For PNG filter method 0 | ||
828 | unfilter a PNG image scanline by scanline. when the pixels are smaller than 1 byte, the filter works byte per byte (bytewidth = 1) | ||
829 | precon is the previous unfiltered scanline, recon the result, scanline the current one | ||
830 | the incoming scanlines do NOT include the filtertype byte, that one is given in the parameter filterType instead | ||
831 | recon and scanline MAY be the same memory address! precon must be disjoint. | ||
832 | */ | ||
833 | |||
834 | size_t i; | ||
835 | switch (filterType) | ||
836 | { | ||
837 | case 0: | ||
838 | //for(i = 0; i < length; i++) recon[i] = scanline[i]; | ||
839 | memcpy(recon, scanline, length * sizeof(unsigned char)); | ||
840 | break; | ||
841 | case 1: | ||
842 | //for(i = 0; i < bytewidth; i++) recon[i] = scanline[i]; | ||
843 | memcpy(recon, scanline, bytewidth * sizeof(unsigned char)); | ||
844 | for (i = bytewidth; i < length; i++) recon[i] = scanline[i] + recon[i - bytewidth]; | ||
845 | break; | ||
846 | case 2: | ||
847 | if (precon) for (i = 0; i < length; i++) recon[i] = scanline[i] + precon[i]; | ||
848 | else //for(i = 0; i < length; i++) recon[i] = scanline[i]; | ||
849 | memcpy(recon, scanline, length * sizeof(unsigned char)); | ||
850 | break; | ||
851 | case 3: | ||
852 | if (precon) | ||
853 | { | ||
854 | for (i = 0; i < bytewidth; i++) recon[i] = scanline[i] + precon[i] / 2; | ||
855 | for (i = bytewidth; i < length; i++) recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2); | ||
856 | } | ||
857 | else | ||
858 | { | ||
859 | //for(i = 0; i < bytewidth; i++) recon[i] = scanline[i]; | ||
860 | memcpy(recon, scanline, bytewidth * sizeof(unsigned char)); | ||
861 | for (i = bytewidth; i < length; i++) recon[i] = scanline[i] + recon[i - bytewidth] / 2; | ||
862 | } | ||
863 | break; | ||
864 | case 4: | ||
865 | if (precon) | ||
866 | { | ||
867 | for (i = 0; i < bytewidth; i++) recon[i] = (unsigned char)(scanline[i] + paethPredictor(0, precon[i], 0)); | ||
868 | for (i = bytewidth; i < length; i++) recon[i] = (unsigned char)(scanline[i] + paethPredictor(recon[i - bytewidth], precon[i], precon[i - bytewidth])); | ||
869 | } | ||
870 | else | ||
871 | { | ||
872 | //for(i = 0; i < bytewidth; i++) recon[i] = scanline[i]; | ||
873 | memcpy(recon, scanline, bytewidth * sizeof(unsigned char)); | ||
874 | for (i = bytewidth; i < length; i++) recon[i] = (unsigned char)(scanline[i] + paethPredictor(recon[i - bytewidth], 0, 0)); | ||
875 | } | ||
876 | break; | ||
877 | default: | ||
878 | return 36; /*error: unexisting filter type given*/ | ||
879 | } | ||
880 | return 0; | ||
881 | } | ||
882 | |||
883 | static unsigned unfilter(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) | ||
884 | { | ||
885 | /* | ||
886 | For PNG filter method 0 | ||
887 | this function unfilters a single image (e.g. without interlacing this is called once, with Adam7 it's called 7 times) | ||
888 | out must have enough bytes allocated already, in must have the scanlines + 1 filtertype byte per scanline | ||
889 | w and h are image dimensions or dimensions of reduced image, bpp is bits per pixel | ||
890 | in and out are allowed to be the same memory address! | ||
891 | */ | ||
892 | |||
893 | unsigned y; | ||
894 | unsigned char* prevline = 0; | ||
895 | |||
896 | size_t bytewidth = (bpp + 7) / 8; /*bytewidth is used for filtering, is 1 when bpp < 8, number of bytes per pixel otherwise*/ | ||
897 | size_t linebytes = (w * bpp + 7) / 8; | ||
898 | |||
899 | for (y = 0; y < h; y++) | ||
900 | { | ||
901 | size_t outindex = linebytes * y; | ||
902 | size_t inindex = (1 + linebytes) * y; /*the extra filterbyte added to each row*/ | ||
903 | unsigned char filterType = in[inindex]; | ||
904 | |||
905 | unsigned error = unfilterScanline(&out[outindex], &in[inindex + 1], prevline, bytewidth, filterType, linebytes); | ||
906 | if (error) return error; | ||
907 | |||
908 | prevline = &out[outindex]; | ||
909 | } | ||
910 | |||
911 | return 0; | ||
912 | } | ||
913 | |||
914 | static void Adam7_deinterlace(unsigned char* out, const unsigned char* in, unsigned w, unsigned h, unsigned bpp) | ||
915 | { | ||
916 | /*Note: this function works on image buffers WITHOUT padding bits at end of scanlines with non-multiple-of-8 bit amounts, only between reduced images is padding | ||
917 | out must be big enough AND must be 0 everywhere if bpp < 8 in the current implementation (because that's likely a little bit faster)*/ | ||
918 | unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8]; | ||
919 | unsigned i; | ||
920 | |||
921 | Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); | ||
922 | |||
923 | if (bpp >= 8) | ||
924 | { | ||
925 | for (i = 0; i < 7; i++) | ||
926 | { | ||
927 | unsigned x, y, b; | ||
928 | size_t bytewidth = bpp / 8; | ||
929 | for (y = 0; y < passh[i]; y++) | ||
930 | for (x = 0; x < passw[i]; x++) | ||
931 | { | ||
932 | size_t pixelinstart = passstart[i] + (y * passw[i] + x) * bytewidth; | ||
933 | size_t pixeloutstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth; | ||
934 | for (b = 0; b < bytewidth; b++) | ||
935 | { | ||
936 | out[pixeloutstart + b] = in[pixelinstart + b]; | ||
937 | } | ||
938 | } | ||
939 | } | ||
940 | } | ||
941 | else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ | ||
942 | { | ||
943 | for (i = 0; i < 7; i++) | ||
944 | { | ||
945 | unsigned x, y, b; | ||
946 | unsigned ilinebits = bpp * passw[i]; | ||
947 | unsigned olinebits = bpp * w; | ||
948 | size_t obp, ibp; /*bit pointers (for out and in buffer)*/ | ||
949 | for (y = 0; y < passh[i]; y++) | ||
950 | for (x = 0; x < passw[i]; x++) | ||
951 | { | ||
952 | ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp); | ||
953 | obp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp; | ||
954 | for (b = 0; b < bpp; b++) | ||
955 | { | ||
956 | unsigned char bit = readBitFromReversedStream(&ibp, in); | ||
957 | setBitOfReversedStream0(&obp, out, bit); /*note that this function assumes the out buffer is completely 0, use setBitOfReversedStream otherwise*/ | ||
958 | } | ||
959 | } | ||
960 | } | ||
961 | } | ||
962 | } | ||
963 | |||
964 | static void removePaddingBits(unsigned char* out, const unsigned char* in, size_t olinebits, size_t ilinebits, unsigned h) | ||
965 | { | ||
966 | /* | ||
967 | After filtering there are still padding bits if scanlines have non multiple of 8 bit amounts. They need to be removed (except at last scanline of (Adam7-reduced) image) before working with pure image buffers for the Adam7 code, the color convert code and the output to the user. | ||
968 | in and out are allowed to be the same buffer, in may also be higher but still overlapping; in must have >= ilinebits*h bits, out must have >= olinebits*h bits, olinebits must be <= ilinebits | ||
969 | also used to move bits after earlier such operations happened, e.g. in a sequence of reduced images from Adam7 | ||
970 | only useful if (ilinebits - olinebits) is a value in the range 1..7 | ||
971 | */ | ||
972 | unsigned y; | ||
973 | size_t diff = ilinebits - olinebits; | ||
974 | size_t obp = 0, ibp = 0; /*bit pointers*/ | ||
975 | for (y = 0; y < h; y++) | ||
976 | { | ||
977 | size_t x; | ||
978 | for (x = 0; x < olinebits; x++) | ||
979 | { | ||
980 | unsigned char bit = readBitFromReversedStream(&ibp, in); | ||
981 | setBitOfReversedStream(&obp, out, bit); | ||
982 | } | ||
983 | ibp += diff; | ||
984 | } | ||
985 | } | ||
986 | |||
987 | /*out must be buffer big enough to contain full image, and in must contain the full decompressed data from the IDAT chunks*/ | ||
988 | static unsigned postProcessScanlines(unsigned char* out, unsigned char* in, const LodePNG_Decoder* decoder) /*return value is error*/ | ||
989 | { | ||
990 | /* | ||
991 | This function converts the filtered-padded-interlaced data into pure 2D image buffer with the PNG's colortype. Steps: | ||
992 | *) if no Adam7: 1) unfilter 2) remove padding bits (= posible extra bits per scanline if bpp < 8) | ||
993 | *) if adam7: 1) 7x unfilter 2) 7x remove padding bits 3) Adam7_deinterlace | ||
994 | NOTE: the in buffer will be overwritten with intermediate data! | ||
995 | */ | ||
996 | unsigned bpp = LodePNG_InfoColor_getBpp(&decoder->infoPng.color); | ||
997 | unsigned w = decoder->infoPng.width; | ||
998 | unsigned h = decoder->infoPng.height; | ||
999 | unsigned error = 0; | ||
1000 | if (bpp == 0) return 31; /*error: invalid colortype*/ | ||
1001 | |||
1002 | if (decoder->infoPng.interlaceMethod == 0) | ||
1003 | { | ||
1004 | if (bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) | ||
1005 | { | ||
1006 | error = unfilter(in, in, w, h, bpp); | ||
1007 | if (error) return error; | ||
1008 | removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h); | ||
1009 | } | ||
1010 | else error = unfilter(out, in, w, h, bpp); /*we can immediatly filter into the out buffer, no other steps needed*/ | ||
1011 | } | ||
1012 | else /*interlaceMethod is 1 (Adam7)*/ | ||
1013 | { | ||
1014 | unsigned passw[7], passh[7]; size_t filter_passstart[8], padded_passstart[8], passstart[8]; | ||
1015 | unsigned i; | ||
1016 | |||
1017 | Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, passstart, w, h, bpp); | ||
1018 | |||
1019 | for (i = 0; i < 7; i++) | ||
1020 | { | ||
1021 | error = unfilter(&in[padded_passstart[i]], &in[filter_passstart[i]], passw[i], passh[i], bpp); | ||
1022 | if (error) return error; | ||
1023 | if (bpp < 8) /*TODO: possible efficiency improvement: if in this reduced image the bits fit nicely in 1 scanline, move bytes instead of bits or move not at all*/ | ||
1024 | { | ||
1025 | /*remove padding bits in scanlines; after this there still may be padding bits between the different reduced images: each reduced image still starts nicely at a byte*/ | ||
1026 | removePaddingBits(&in[passstart[i]], &in[padded_passstart[i]], passw[i] * bpp, ((passw[i] * bpp + 7) / 8) * 8, passh[i]); | ||
1027 | } | ||
1028 | } | ||
1029 | |||
1030 | Adam7_deinterlace(out, in, w, h, bpp); | ||
1031 | } | ||
1032 | |||
1033 | return error; | ||
1034 | } | ||
1035 | |||
1036 | /*read a PNG, the result will be in the same color type as the PNG (hence "generic")*/ | ||
1037 | static void decodeGeneric(LodePNG_Decoder* decoder, unsigned char* in, size_t size, void (*pf_progress)(int current, int total)) | ||
1038 | { | ||
1039 | if (pf_progress != NULL) | ||
1040 | pf_progress(0, 100); | ||
1041 | unsigned char IEND = 0; | ||
1042 | const unsigned char* chunk; | ||
1043 | size_t i; | ||
1044 | unsigned char *idat = memory; | ||
1045 | size_t idat_size = 0; | ||
1046 | |||
1047 | /*for unknown chunk order*/ | ||
1048 | unsigned unknown = 0; | ||
1049 | unsigned critical_pos = 1; /*1 = after IHDR, 2 = after PLTE, 3 = after IDAT*/ | ||
1050 | |||
1051 | /*provide some proper output values if error will happen*/ | ||
1052 | decoded_image_size = 0; | ||
1053 | |||
1054 | if (size == 0 || in == 0) { decoder->error = 48; return; } /*the given data is empty*/ | ||
1055 | |||
1056 | LodePNG_inspect(decoder, in, size); /*reads header and resets other parameters in decoder->infoPng*/ | ||
1057 | if (decoder->error) return; | ||
1058 | |||
1059 | chunk = &in[33]; /*first byte of the first chunk after the header*/ | ||
1060 | |||
1061 | while (!IEND) /*loop through the chunks, ignoring unknown chunks and stopping at IEND chunk. IDAT data is put at the start of the in buffer*/ | ||
1062 | { | ||
1063 | unsigned chunkLength; | ||
1064 | const unsigned char* data; /*the data in the chunk*/ | ||
1065 | |||
1066 | if ((size_t)((chunk - in) + 12) > size || chunk < in) { decoder->error = 30; break; } /*error: size of the in buffer too small to contain next chunk*/ | ||
1067 | chunkLength = LodePNG_chunk_length(chunk); /*length of the data of the chunk, excluding the length bytes, chunk type and CRC bytes*/ | ||
1068 | if (chunkLength > 2147483647) { decoder->error = 63; break; } | ||
1069 | if ((size_t)((chunk - in) + chunkLength + 12) > size || (chunk + chunkLength + 12) < in) { decoder->error = 35; break; } /*error: size of the in buffer too small to contain next chunk*/ | ||
1070 | data = LodePNG_chunk_data_const(chunk); | ||
1071 | |||
1072 | /*IDAT chunk, containing compressed image data*/ | ||
1073 | if (LodePNG_chunk_type_equals(chunk, "IDAT")) | ||
1074 | { | ||
1075 | size_t oldsize = idat_size; | ||
1076 | idat_size += chunkLength; | ||
1077 | if (idat + idat_size >= image) { decoder->error = OUT_OF_MEMORY; break; } | ||
1078 | memcpy(idat+oldsize, data, chunkLength * sizeof(unsigned char)); | ||
1079 | critical_pos = 3; | ||
1080 | } | ||
1081 | /*IEND chunk*/ | ||
1082 | else if (LodePNG_chunk_type_equals(chunk, "IEND")) | ||
1083 | { | ||
1084 | IEND = 1; | ||
1085 | } | ||
1086 | /*palette chunk (PLTE)*/ | ||
1087 | else if (LodePNG_chunk_type_equals(chunk, "PLTE")) | ||
1088 | { | ||
1089 | unsigned pos = 0; | ||
1090 | decoder->infoPng.color.palettesize = chunkLength / 3; | ||
1091 | if (decoder->infoPng.color.palettesize > 256) { decoder->error = 38; break; } /*error: palette too big*/ | ||
1092 | for (i = 0; i < decoder->infoPng.color.palettesize; i++) | ||
1093 | { | ||
1094 | decoder->infoPng.color.palette[4 * i + 0] = data[pos++]; /*R*/ | ||
1095 | decoder->infoPng.color.palette[4 * i + 1] = data[pos++]; /*G*/ | ||
1096 | decoder->infoPng.color.palette[4 * i + 2] = data[pos++]; /*B*/ | ||
1097 | decoder->infoPng.color.palette[4 * i + 3] = 255; /*alpha*/ | ||
1098 | } | ||
1099 | critical_pos = 2; | ||
1100 | } | ||
1101 | /*palette transparency chunk (tRNS)*/ | ||
1102 | else if (LodePNG_chunk_type_equals(chunk, "tRNS")) | ||
1103 | { | ||
1104 | if (decoder->infoPng.color.colorType == 3) | ||
1105 | { | ||
1106 | if (chunkLength > decoder->infoPng.color.palettesize) { decoder->error = 39; break; } /*error: more alpha values given than there are palette entries*/ | ||
1107 | for (i = 0; i < chunkLength; i++) decoder->infoPng.color.palette[4 * i + 3] = data[i]; | ||
1108 | } | ||
1109 | else if (decoder->infoPng.color.colorType == 0) | ||
1110 | { | ||
1111 | if (chunkLength != 2) { decoder->error = 40; break; } /*error: this chunk must be 2 bytes for greyscale image*/ | ||
1112 | decoder->infoPng.color.key_defined = 1; | ||
1113 | decoder->infoPng.color.key_r = decoder->infoPng.color.key_g = decoder->infoPng.color.key_b = 256 * data[0] + data[1]; | ||
1114 | } | ||
1115 | else if (decoder->infoPng.color.colorType == 2) | ||
1116 | { | ||
1117 | if (chunkLength != 6) { decoder->error = 41; break; } /*error: this chunk must be 6 bytes for RGB image*/ | ||
1118 | decoder->infoPng.color.key_defined = 1; | ||
1119 | decoder->infoPng.color.key_r = 256 * data[0] + data[1]; | ||
1120 | decoder->infoPng.color.key_g = 256 * data[2] + data[3]; | ||
1121 | decoder->infoPng.color.key_b = 256 * data[4] + data[5]; | ||
1122 | } | ||
1123 | else { decoder->error = 42; break; } /*error: tRNS chunk not allowed for other color models*/ | ||
1124 | } | ||
1125 | /*background color chunk (bKGD)*/ | ||
1126 | else if (LodePNG_chunk_type_equals(chunk, "bKGD")) | ||
1127 | { | ||
1128 | if (decoder->infoPng.color.colorType == 3) | ||
1129 | { | ||
1130 | if (chunkLength != 1) { decoder->error = 43; break; } /*error: this chunk must be 1 byte for indexed color image*/ | ||
1131 | decoder->infoPng.background_defined = 1; | ||
1132 | decoder->infoPng.background_r = decoder->infoPng.color.palette[4 * data[0] + 0]; | ||
1133 | decoder->infoPng.background_g = decoder->infoPng.color.palette[4 * data[0] + 1]; | ||
1134 | decoder->infoPng.background_b = decoder->infoPng.color.palette[4 * data[0] + 2]; | ||
1135 | |||
1136 | } | ||
1137 | else if (decoder->infoPng.color.colorType == 0 || decoder->infoPng.color.colorType == 4) | ||
1138 | { | ||
1139 | if (chunkLength != 2) { decoder->error = 44; break; } /*error: this chunk must be 2 bytes for greyscale image*/ | ||
1140 | decoder->infoPng.background_defined = 1; | ||
1141 | decoder->infoPng.background_r = decoder->infoPng.background_g = decoder->infoPng.background_b = 256 * data[0] + data[1]; | ||
1142 | } | ||
1143 | else if (decoder->infoPng.color.colorType == 2 || decoder->infoPng.color.colorType == 6) | ||
1144 | { | ||
1145 | if (chunkLength != 6) { decoder->error = 45; break; } /*error: this chunk must be 6 bytes for greyscale image*/ | ||
1146 | decoder->infoPng.background_defined = 1; | ||
1147 | decoder->infoPng.background_r = 256 * data[0] + data[1]; | ||
1148 | decoder->infoPng.background_g = 256 * data[2] + data[3]; | ||
1149 | decoder->infoPng.background_b = 256 * data[4] + data[5]; | ||
1150 | } | ||
1151 | } | ||
1152 | else if (LodePNG_chunk_type_equals(chunk, "tIME")) | ||
1153 | { | ||
1154 | if (chunkLength != 7) { decoder->error = 73; break; } | ||
1155 | decoder->infoPng.time_defined = 1; | ||
1156 | decoder->infoPng.time.year = 256 * data[0] + data[+ 1]; | ||
1157 | decoder->infoPng.time.month = data[2]; | ||
1158 | decoder->infoPng.time.day = data[3]; | ||
1159 | decoder->infoPng.time.hour = data[4]; | ||
1160 | decoder->infoPng.time.minute = data[5]; | ||
1161 | decoder->infoPng.time.second = data[6]; | ||
1162 | } | ||
1163 | else if (LodePNG_chunk_type_equals(chunk, "pHYs")) | ||
1164 | { | ||
1165 | if (chunkLength != 9) { decoder->error = 74; break; } | ||
1166 | decoder->infoPng.phys_defined = 1; | ||
1167 | decoder->infoPng.phys_x = 16777216 * data[0] + 65536 * data[1] + 256 * data[2] + data[3]; | ||
1168 | decoder->infoPng.phys_y = 16777216 * data[4] + 65536 * data[5] + 256 * data[6] + data[7]; | ||
1169 | decoder->infoPng.phys_unit = data[8]; | ||
1170 | } | ||
1171 | else /*it's not an implemented chunk type, so ignore it: skip over the data*/ | ||
1172 | { | ||
1173 | if (LodePNG_chunk_critical(chunk)) { decoder->error = 69; break; } /*error: unknown critical chunk (5th bit of first byte of chunk type is 0)*/ | ||
1174 | unknown = 1; | ||
1175 | } | ||
1176 | |||
1177 | if (!unknown) /*check CRC if wanted, only on known chunk types*/ | ||
1178 | { | ||
1179 | long time = *rb->current_tick; | ||
1180 | if (LodePNG_chunk_check_crc(chunk)) { decoder->error = 57; break; } | ||
1181 | time = *rb->current_tick-time; | ||
1182 | } | ||
1183 | |||
1184 | if (!IEND) chunk = LodePNG_chunk_next_const(chunk); | ||
1185 | } | ||
1186 | |||
1187 | if (!decoder->error) | ||
1188 | { | ||
1189 | unsigned char *scanlines = idat + idat_size; | ||
1190 | size_t scanlines_size = (size_t)memory_max - idat_size; | ||
1191 | long time = *rb->current_tick; | ||
1192 | decoder->error = LodePNG_decompress(scanlines, &scanlines_size, idat, idat_size, decoder->error_msg); /*decompress with the Zlib decompressor*/ | ||
1193 | if (pf_progress) pf_progress(100, 100); | ||
1194 | time = *rb->current_tick-time; | ||
1195 | |||
1196 | if (!decoder->error) | ||
1197 | { | ||
1198 | decoded_image_size = (decoder->infoPng.height * decoder->infoPng.width * LodePNG_InfoColor_getBpp(&decoder->infoPng.color) + 7) / 8; | ||
1199 | if (decoded_image_size > memory_size) { decoder->error = OUT_OF_MEMORY; return; } | ||
1200 | decoded_image = memory_max - decoded_image_size; | ||
1201 | if (scanlines + scanlines_size >= decoded_image) { decoder->error = OUT_OF_MEMORY; return; } | ||
1202 | memset(decoded_image, 0, decoded_image_size * sizeof(unsigned char)); | ||
1203 | if (!running_slideshow) | ||
1204 | { | ||
1205 | rb->lcd_puts(0, 3, "unfiltering scanlines"); | ||
1206 | rb->lcd_update(); | ||
1207 | } | ||
1208 | decoder->error = postProcessScanlines(decoded_image, scanlines, decoder); | ||
1209 | } | ||
1210 | } | ||
1211 | } | ||
1212 | |||
1213 | void LodePNG_decode(LodePNG_Decoder* decoder, unsigned char* in, size_t insize, void (*pf_progress)(int current, int total)) | ||
1214 | { | ||
1215 | decodeGeneric(decoder, in, insize, pf_progress); | ||
1216 | if (decoder->error) return; | ||
1217 | |||
1218 | /*TODO: check if this works according to the statement in the documentation: "The converter can convert from greyscale input color type, to 8-bit greyscale or greyscale with alpha"*/ | ||
1219 | if (!(decoder->infoRaw.color.colorType == 2 || decoder->infoRaw.color.colorType == 6) && !(decoder->infoRaw.color.bitDepth == 8)) { decoder->error = 56; return; } | ||
1220 | converted_image = (fb_data *)memory; | ||
1221 | converted_image_size = decoder->infoPng.width*decoder->infoPng.height; | ||
1222 | if ((unsigned char *)(converted_image + converted_image_size) >= decoded_image) { decoder->error = OUT_OF_MEMORY; } | ||
1223 | if (!decoder->error) decoder->error = LodePNG_convert(converted_image, decoded_image, &decoder->infoRaw.color, &decoder->infoPng.color, decoder->infoPng.width, decoder->infoPng.height); | ||
1224 | } | ||
1225 | |||
1226 | void LodePNG_DecodeSettings_init(LodePNG_DecodeSettings* settings) | ||
1227 | { | ||
1228 | settings->color_convert = 1; | ||
1229 | } | ||
1230 | |||
1231 | void LodePNG_Decoder_init(LodePNG_Decoder* decoder) | ||
1232 | { | ||
1233 | LodePNG_DecodeSettings_init(&decoder->settings); | ||
1234 | LodePNG_InfoRaw_init(&decoder->infoRaw); | ||
1235 | LodePNG_InfoPng_init(&decoder->infoPng); | ||
1236 | decoder->error = 1; | ||
1237 | } | ||
1238 | |||
1239 | void LodePNG_Decoder_cleanup(LodePNG_Decoder* decoder) | ||
1240 | { | ||
1241 | LodePNG_InfoRaw_cleanup(&decoder->infoRaw); | ||
1242 | LodePNG_InfoPng_cleanup(&decoder->infoPng); | ||
1243 | } | ||
1244 | |||
1245 | #define PNG_ERROR_MIN 27 | ||
1246 | #define PNG_ERROR_MAX 74 | ||
1247 | static const unsigned char *png_error_messages[PNG_ERROR_MAX-PNG_ERROR_MIN+1] = | ||
1248 | { | ||
1249 | "png file smaller than a png header", /*27*/ | ||
1250 | "incorrect png signature", /*28*/ | ||
1251 | "first chunk is not IHDR", /*29*/ | ||
1252 | "chunk length too large", /*30*/ | ||
1253 | "illegal PNG color type or bpp", /*31*/ | ||
1254 | "illegal PNG compression method", /*32*/ | ||
1255 | "illegal PNG filter method", /*33*/ | ||
1256 | "illegal PNG interlace method", /*34*/ | ||
1257 | "chunk length of a chunk is too large or the chunk too small", /*35*/ | ||
1258 | "illegal PNG filter type encountered", /*36*/ | ||
1259 | "illegal bit depth for this color type given", /*37*/ | ||
1260 | "the palette is too big (more than 256 colors)", /*38*/ | ||
1261 | "more palette alpha values given in tRNS, than there are colors in the palette", /*39*/ | ||
1262 | "tRNS chunk has wrong size for greyscale image", /*40*/ | ||
1263 | "tRNS chunk has wrong size for RGB image", /*41*/ | ||
1264 | "tRNS chunk appeared while it was not allowed for this color type", /*42*/ | ||
1265 | "bKGD chunk has wrong size for palette image", /*43*/ | ||
1266 | "bKGD chunk has wrong size for greyscale image", /*44*/ | ||
1267 | "bKGD chunk has wrong size for RGB image", /*45*/ | ||
1268 | "value encountered in indexed image is larger than the palette size", /*46*/ | ||
1269 | "value encountered in indexed image is larger than the palette size", /*47*/ | ||
1270 | "input file is empty", /*48*/ | ||
1271 | NULL, /*49*/ | ||
1272 | NULL, /*50*/ | ||
1273 | NULL, /*51*/ | ||
1274 | NULL, /*52*/ | ||
1275 | NULL, /*53*/ | ||
1276 | NULL, /*54*/ | ||
1277 | NULL, /*55*/ | ||
1278 | NULL, /*56*/ | ||
1279 | "invalid CRC", /*57*/ | ||
1280 | NULL, /*58*/ | ||
1281 | "conversion to unexisting or unsupported color type or bit depth", /*59*/ | ||
1282 | NULL, /*60*/ | ||
1283 | NULL, /*61*/ | ||
1284 | NULL, /*62*/ | ||
1285 | "png chunk too long", /*63*/ | ||
1286 | NULL, /*64*/ | ||
1287 | NULL, /*65*/ | ||
1288 | NULL, /*66*/ | ||
1289 | NULL, /*67*/ | ||
1290 | NULL, /*68*/ | ||
1291 | "unknown critical chunk", /*69*/ | ||
1292 | NULL, /*70*/ | ||
1293 | NULL, /*71*/ | ||
1294 | NULL, /*72*/ | ||
1295 | "invalid tIME chunk size", /*73*/ | ||
1296 | "invalid pHYs chunk size", /*74*/ | ||
1297 | }; | ||
1298 | 53 | ||
1299 | bool img_ext(const char *ext) | 54 | bool img_ext(const char *ext) |
1300 | { | 55 | { |
@@ -1310,16 +65,33 @@ void draw_image_rect(struct image_info *info, | |||
1310 | int x, int y, int width, int height) | 65 | int x, int y, int width, int height) |
1311 | { | 66 | { |
1312 | fb_data **pdisp = (fb_data**)info->data; | 67 | fb_data **pdisp = (fb_data**)info->data; |
1313 | rb->lcd_bitmap_part(*pdisp, info->x + x, info->y + y, info->width, | 68 | |
69 | #ifdef HAVE_LCD_COLOR | ||
70 | rb->lcd_bitmap_part(*pdisp, info->x + x, info->y + y, | ||
71 | STRIDE(SCREEN_MAIN, info->width, info->height), | ||
1314 | x + MAX(0, (LCD_WIDTH-info->width)/2), | 72 | x + MAX(0, (LCD_WIDTH-info->width)/2), |
1315 | y + MAX(0, (LCD_HEIGHT-info->height)/2), | 73 | y + MAX(0, (LCD_HEIGHT-info->height)/2), |
1316 | width, height); | 74 | width, height); |
75 | #else | ||
76 | mylcd_ub_gray_bitmap_part((const unsigned char*)*pdisp, | ||
77 | info->x + x, info->y + y, info->width, | ||
78 | x + MAX(0, (LCD_WIDTH-info->width)/2), | ||
79 | y + MAX(0, (LCD_HEIGHT-info->height)/2), | ||
80 | width, height); | ||
81 | #endif | ||
1317 | } | 82 | } |
1318 | 83 | ||
1319 | int img_mem(int ds) | 84 | int img_mem(int ds) |
1320 | { | 85 | { |
1321 | LodePNG_Decoder *decoder = &_decoder; | 86 | LodePNG_Decoder *p_decoder = &decoder; |
1322 | return (decoder->infoPng.width/ds) * (decoder->infoPng.height/ds) * FB_DATA_SZ; | 87 | |
88 | #ifdef USEGSLIB | ||
89 | return (p_decoder->infoPng.width/ds) * (p_decoder->infoPng.height/ds); | ||
90 | #else | ||
91 | return (p_decoder->infoPng.width/ds) * | ||
92 | (p_decoder->infoPng.height/ds) * | ||
93 | FB_DATA_SZ; | ||
94 | #endif | ||
1323 | } | 95 | } |
1324 | 96 | ||
1325 | int load_image(char *filename, struct image_info *info, | 97 | int load_image(char *filename, struct image_info *info, |
@@ -1328,10 +100,14 @@ int load_image(char *filename, struct image_info *info, | |||
1328 | int fd; | 100 | int fd; |
1329 | long time = 0; /* measured ticks */ | 101 | long time = 0; /* measured ticks */ |
1330 | int w, h; /* used to center output */ | 102 | int w, h; /* used to center output */ |
1331 | LodePNG_Decoder *decoder = &_decoder; | 103 | LodePNG_Decoder *p_decoder = &decoder; |
104 | |||
105 | unsigned char *memory, *memory_max; | ||
106 | static size_t memory_size, file_size; | ||
107 | static unsigned char *image; | ||
1332 | 108 | ||
109 | /* cleanup */ | ||
1333 | memset(&disp, 0, sizeof(disp)); | 110 | memset(&disp, 0, sizeof(disp)); |
1334 | LodePNG_Decoder_init(decoder); | ||
1335 | 111 | ||
1336 | /* align buffer */ | 112 | /* align buffer */ |
1337 | memory = (unsigned char *)((intptr_t)(buf + 3) & ~3); | 113 | memory = (unsigned char *)((intptr_t)(buf + 3) & ~3); |
@@ -1344,7 +120,7 @@ int load_image(char *filename, struct image_info *info, | |||
1344 | rb->splashf(HZ, "err opening %s: %d", filename, fd); | 120 | rb->splashf(HZ, "err opening %s: %d", filename, fd); |
1345 | return PLUGIN_ERROR; | 121 | return PLUGIN_ERROR; |
1346 | } | 122 | } |
1347 | image_size = rb->filesize(fd); | 123 | file_size = rb->filesize(fd); |
1348 | 124 | ||
1349 | DEBUGF("reading file '%s'\n", filename); | 125 | DEBUGF("reading file '%s'\n", filename); |
1350 | 126 | ||
@@ -1353,18 +129,19 @@ int load_image(char *filename, struct image_info *info, | |||
1353 | rb->lcd_update(); | 129 | rb->lcd_update(); |
1354 | } | 130 | } |
1355 | 131 | ||
1356 | if (image_size > memory_size) { | 132 | if (file_size > memory_size) { |
1357 | decoder->error = FILE_TOO_LARGE; | 133 | p_decoder->error = FILE_TOO_LARGE; |
1358 | rb->close(fd); | 134 | rb->close(fd); |
1359 | 135 | ||
1360 | } else { | 136 | } else { |
1361 | if (!running_slideshow) { | 137 | if (!running_slideshow) { |
1362 | rb->lcd_putsf(0, 1, "loading %zu bytes", image_size); | 138 | rb->lcd_putsf(0, 1, "loading %zu bytes", file_size); |
1363 | rb->lcd_update(); | 139 | rb->lcd_update(); |
1364 | } | 140 | } |
1365 | 141 | ||
1366 | image = memory_max - image_size; | 142 | /* load file to the end of the buffer */ |
1367 | rb->read(fd, image, image_size); | 143 | image = memory_max - file_size; |
144 | rb->read(fd, image, file_size); | ||
1368 | rb->close(fd); | 145 | rb->close(fd); |
1369 | 146 | ||
1370 | if (!running_slideshow) { | 147 | if (!running_slideshow) { |
@@ -1378,19 +155,27 @@ int load_image(char *filename, struct image_info *info, | |||
1378 | } | 155 | } |
1379 | #endif | 156 | #endif |
1380 | 157 | ||
1381 | decoder->settings.color_convert = 1; | 158 | /* Initialize decoder context struct, set buffer decoder is free |
1382 | decoder->infoRaw.color.colorType = 2; | 159 | * to use. |
1383 | decoder->infoRaw.color.bitDepth = 8; | 160 | * Decoder assumes that raw image file is loaded at the very |
161 | * end of the allocated buffer | ||
162 | */ | ||
163 | LodePNG_Decoder_init(p_decoder, memory, memory_size); | ||
1384 | 164 | ||
1385 | LodePNG_inspect(decoder, image, image_size); | 165 | /* read file header; file is loaded at the end |
166 | * of the allocated buffer | ||
167 | */ | ||
168 | LodePNG_inspect(p_decoder, image, file_size); | ||
1386 | 169 | ||
1387 | if (!decoder->error) { | 170 | if (!p_decoder->error) { |
1388 | 171 | ||
1389 | if (!running_slideshow) { | 172 | if (!running_slideshow) { |
1390 | rb->lcd_putsf(0, 2, "image %dx%d", | 173 | rb->lcd_putsf(0, 2, "image %dx%d", |
1391 | decoder->infoPng.width, decoder->infoPng.height); | 174 | p_decoder->infoPng.width, |
175 | p_decoder->infoPng.height); | ||
1392 | rb->lcd_putsf(0, 3, "decoding %d*%d", | 176 | rb->lcd_putsf(0, 3, "decoding %d*%d", |
1393 | decoder->infoPng.width, decoder->infoPng.height); | 177 | p_decoder->infoPng.width, |
178 | p_decoder->infoPng.height); | ||
1394 | rb->lcd_update(); | 179 | rb->lcd_update(); |
1395 | } | 180 | } |
1396 | 181 | ||
@@ -1398,16 +183,16 @@ int load_image(char *filename, struct image_info *info, | |||
1398 | time = *rb->current_tick; | 183 | time = *rb->current_tick; |
1399 | #ifdef HAVE_ADJUSTABLE_CPU_FREQ | 184 | #ifdef HAVE_ADJUSTABLE_CPU_FREQ |
1400 | rb->cpu_boost(true); | 185 | rb->cpu_boost(true); |
1401 | LodePNG_decode(decoder, image, image_size, cb_progress); | 186 | LodePNG_decode(p_decoder, image, file_size, cb_progress); |
1402 | rb->cpu_boost(false); | 187 | rb->cpu_boost(false); |
1403 | #else | 188 | #else |
1404 | LodePNG_decode(decoder, image, image_size, cb_progress); | 189 | LodePNG_decode(p_decoder, image, file_size, cb_progress); |
1405 | #endif /*HAVE_ADJUSTABLE_CPU_FREQ*/ | 190 | #endif /*HAVE_ADJUSTABLE_CPU_FREQ*/ |
1406 | time = *rb->current_tick - time; | 191 | time = *rb->current_tick - time; |
1407 | } | 192 | } |
1408 | } | 193 | } |
1409 | 194 | ||
1410 | if (!running_slideshow && !decoder->error) | 195 | if (!running_slideshow && !p_decoder->error) |
1411 | { | 196 | { |
1412 | rb->snprintf(print, sizeof(print), " %ld.%02ld sec ", time/HZ, time%HZ); | 197 | rb->snprintf(print, sizeof(print), " %ld.%02ld sec ", time/HZ, time%HZ); |
1413 | rb->lcd_getstringsize(print, &w, &h); /* centered in progress bar */ | 198 | rb->lcd_getstringsize(print, &w, &h); /* centered in progress bar */ |
@@ -1415,55 +200,59 @@ int load_image(char *filename, struct image_info *info, | |||
1415 | rb->lcd_update(); | 200 | rb->lcd_update(); |
1416 | } | 201 | } |
1417 | 202 | ||
1418 | if (decoder->error) { | 203 | if (p_decoder->error) { |
1419 | #ifdef USE_PLUG_BUF | 204 | #ifdef USE_PLUG_BUF |
1420 | if (plug_buf && (decoder->error == FILE_TOO_LARGE | 205 | if (plug_buf && (p_decoder->error == FILE_TOO_LARGE || |
1421 | || decoder->error == OUT_OF_MEMORY || decoder->error == Z_MEM_ERROR)) | 206 | p_decoder->error == OUT_OF_MEMORY || |
207 | p_decoder->error == TINF_DATA_ERROR)) | ||
1422 | return PLUGIN_OUTOFMEM; | 208 | return PLUGIN_OUTOFMEM; |
1423 | #endif | 209 | #endif |
1424 | 210 | ||
1425 | if (decoder->error >= PNG_ERROR_MIN && decoder->error <= PNG_ERROR_MAX | 211 | if (p_decoder->error >= PNG_ERROR_MIN && |
1426 | && png_error_messages[decoder->error-PNG_ERROR_MIN] != NULL) | 212 | p_decoder->error <= PNG_ERROR_MAX && |
213 | LodePNG_perror(p_decoder) != NULL) | ||
1427 | { | 214 | { |
1428 | rb->splash(HZ, png_error_messages[decoder->error-PNG_ERROR_MIN]); | 215 | rb->splash(HZ, LodePNG_perror(p_decoder)); |
1429 | } | 216 | } |
1430 | else | 217 | else |
1431 | { | 218 | { |
1432 | switch (decoder->error) { | 219 | switch (p_decoder->error) { |
1433 | case PLUGIN_ABORT: | 220 | case PLUGIN_ABORT: |
1434 | break; | 221 | break; |
1435 | case OUT_OF_MEMORY: | 222 | case OUT_OF_MEMORY: |
1436 | case Z_MEM_ERROR: | ||
1437 | rb->splash(HZ, "Out of Memory");break; | 223 | rb->splash(HZ, "Out of Memory");break; |
1438 | case FILE_TOO_LARGE: | 224 | case FILE_TOO_LARGE: |
1439 | rb->splash(HZ, "File too large");break; | 225 | rb->splash(HZ, "File too large");break; |
1440 | case Z_DATA_ERROR: | 226 | case TINF_DATA_ERROR: |
1441 | rb->splash(HZ, decoder->error_msg);break; | 227 | rb->splash(HZ, "Zlib decompressor error");break; |
1442 | default: | 228 | default: |
1443 | rb->splashf(HZ, "other error : %ld", decoder->error);break; | 229 | rb->splashf(HZ, "other error : %ld", p_decoder->error);break; |
1444 | } | 230 | } |
1445 | } | 231 | } |
1446 | 232 | ||
1447 | if (decoder->error == PLUGIN_ABORT) | 233 | if (p_decoder->error == PLUGIN_ABORT) |
1448 | return PLUGIN_ABORT; | 234 | return PLUGIN_ABORT; |
1449 | else | 235 | else |
1450 | return PLUGIN_ERROR; | 236 | return PLUGIN_ERROR; |
1451 | } | 237 | } |
1452 | 238 | ||
1453 | disp_buf = converted_image + converted_image_size; | 239 | info->x_size = p_decoder->infoPng.width; |
1454 | info->x_size = decoder->infoPng.width; | 240 | info->y_size = p_decoder->infoPng.height; |
1455 | info->y_size = decoder->infoPng.height; | 241 | |
242 | disp_buf = (fb_data *)(p_decoder->buf + p_decoder->native_img_size); | ||
243 | disp_buf = (fb_data *)ALIGN_UP((uintptr_t)disp_buf,4); | ||
1456 | *buf_size = memory_max - (unsigned char*)disp_buf; | 244 | *buf_size = memory_max - (unsigned char*)disp_buf; |
245 | |||
1457 | return PLUGIN_OK; | 246 | return PLUGIN_OK; |
1458 | } | 247 | } |
1459 | 248 | ||
1460 | int get_image(struct image_info *info, int ds) | 249 | int get_image(struct image_info *info, int ds) |
1461 | { | 250 | { |
1462 | fb_data **p_disp = &disp[ds]; /* short cut */ | 251 | fb_data **p_disp = &disp[ds]; /* short cut */ |
1463 | LodePNG_Decoder *decoder = &_decoder; | 252 | LodePNG_Decoder *p_decoder = &decoder; |
1464 | 253 | ||
1465 | info->width = decoder->infoPng.width / ds; | 254 | info->width = p_decoder->infoPng.width / ds; |
1466 | info->height = decoder->infoPng.height / ds; | 255 | info->height = p_decoder->infoPng.height / ds; |
1467 | info->data = p_disp; | 256 | info->data = p_disp; |
1468 | 257 | ||
1469 | if (*p_disp != NULL) | 258 | if (*p_disp != NULL) |
@@ -1483,34 +272,40 @@ int get_image(struct image_info *info, int ds) | |||
1483 | 272 | ||
1484 | int size = info->width * info->height; | 273 | int size = info->width * info->height; |
1485 | 274 | ||
1486 | if ((unsigned char *)(disp_buf + size) >= memory_max) { | 275 | if ((unsigned char *)(disp_buf + size) >= p_decoder->buf + p_decoder->buf_size) { |
1487 | /* have to discard the current */ | 276 | /* have to discard the current */ |
1488 | int i; | 277 | int i; |
1489 | for (i=1; i<=8; i++) | 278 | for (i=1; i<=8; i++) |
1490 | disp[i] = NULL; /* invalidate all bitmaps */ | 279 | disp[i] = NULL; /* invalidate all bitmaps */ |
280 | |||
1491 | /* start again from the beginning of the buffer */ | 281 | /* start again from the beginning of the buffer */ |
1492 | disp_buf = converted_image + converted_image_size; | 282 | disp_buf = (fb_data *)(p_decoder->buf + p_decoder->native_img_size); |
283 | disp_buf = (fb_data *)ALIGN_UP((uintptr_t)disp_buf,4); | ||
1493 | } | 284 | } |
1494 | 285 | ||
1495 | *p_disp = disp_buf; | 286 | *p_disp = disp_buf; |
287 | #ifdef USEGSLIB | ||
288 | disp_buf = (fb_data *)((unsigned char *)disp_buf + size); | ||
289 | #else | ||
1496 | disp_buf += size; | 290 | disp_buf += size; |
291 | #endif | ||
1497 | 292 | ||
1498 | bmp_src.width = decoder->infoPng.width; | 293 | bmp_src.width = p_decoder->infoPng.width; |
1499 | bmp_src.height = decoder->infoPng.height; | 294 | bmp_src.height = p_decoder->infoPng.height; |
1500 | bmp_src.data = (unsigned char *)converted_image; | 295 | bmp_src.data = (unsigned char *)p_decoder->buf; |
1501 | 296 | ||
1502 | bmp_dst.width = info->width; | 297 | bmp_dst.width = info->width; |
1503 | bmp_dst.height = info->height; | 298 | bmp_dst.height = info->height; |
1504 | bmp_dst.data = (unsigned char *)*p_disp; | 299 | bmp_dst.data = (unsigned char *)*p_disp; |
1505 | #ifdef HAVE_ADJUSTABLE_CPU_FREQ | 300 | #ifdef HAVE_ADJUSTABLE_CPU_FREQ |
1506 | rb->cpu_boost(true); | 301 | rb->cpu_boost(true); |
1507 | smooth_resize_bitmap(&bmp_src, &bmp_dst); | 302 | resize_bitmap(&bmp_src, &bmp_dst); |
1508 | rb->cpu_boost(false); | 303 | rb->cpu_boost(false); |
1509 | #else | 304 | #else |
1510 | smooth_resize_bitmap(&bmp_src, &bmp_dst); | 305 | resize_bitmap(&bmp_src, &bmp_dst); |
1511 | #endif /*HAVE_ADJUSTABLE_CPU_FREQ*/ | 306 | #endif /*HAVE_ADJUSTABLE_CPU_FREQ*/ |
1512 | } else { | 307 | } else { |
1513 | *p_disp = converted_image; | 308 | *p_disp = (fb_data *)p_decoder->buf; |
1514 | } | 309 | } |
1515 | 310 | ||
1516 | return PLUGIN_OK; | 311 | return PLUGIN_OK; |
diff --git a/apps/plugins/imageviewer/png/png.h b/apps/plugins/imageviewer/png/png.h index e351d9a8aa..f5c76f1f44 100644 --- a/apps/plugins/imageviewer/png/png.h +++ b/apps/plugins/imageviewer/png/png.h | |||
@@ -23,7 +23,3 @@ | |||
23 | ****************************************************************************/ | 23 | ****************************************************************************/ |
24 | 24 | ||
25 | #include "../imageviewer.h" | 25 | #include "../imageviewer.h" |
26 | |||
27 | #define OUT_OF_MEMORY 9900 | ||
28 | #define FILE_TOO_LARGE 9910 | ||
29 | |||
diff --git a/apps/plugins/imageviewer/png/png.make b/apps/plugins/imageviewer/png/png.make index b2000faecf..0a7106d2a9 100644 --- a/apps/plugins/imageviewer/png/png.make +++ b/apps/plugins/imageviewer/png/png.make | |||
@@ -19,7 +19,7 @@ PNG_OBJ := $(call c2obj, $(PNG_SRC)) | |||
19 | OTHER_SRC += $(PNG_SRC) | 19 | OTHER_SRC += $(PNG_SRC) |
20 | 20 | ||
21 | # Use -O3 for png plugin : it gives a bigger file but very good performances | 21 | # Use -O3 for png plugin : it gives a bigger file but very good performances |
22 | PNGFLAGS = $(PLUGINFLAGS) -O3 -DNO_GZCOMPRESS -DNO_GZIP | 22 | PNGFLAGS = $(PLUGINFLAGS) -Os |
23 | 23 | ||
24 | $(PNGBUILDDIR)/png.rock: $(PNG_OBJ) | 24 | $(PNGBUILDDIR)/png.rock: $(PNG_OBJ) |
25 | 25 | ||
diff --git a/apps/plugins/imageviewer/png/png_decoder.c b/apps/plugins/imageviewer/png/png_decoder.c new file mode 100644 index 0000000000..b09d2e2ece --- /dev/null +++ b/apps/plugins/imageviewer/png/png_decoder.c | |||
@@ -0,0 +1,2195 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$id $ | ||
9 | * | ||
10 | * Copyright (C) 2009 by Christophe Gouiran <bechris13250 -at- gmail -dot- com> | ||
11 | * | ||
12 | * Based on lodepng, a lightweight png decoder/encoder | ||
13 | * (c) 2005-2008 Lode Vandevenne | ||
14 | * | ||
15 | * Copyright (c) 2010 Marcin Bukat | ||
16 | * - pixel format conversion & transparency handling | ||
17 | * - adaptation of tinf (tiny inflate library) | ||
18 | * - code refactoring & cleanups | ||
19 | * | ||
20 | * This program is free software; you can redistribute it and/or | ||
21 | * modify it under the terms of the GNU General Public License | ||
22 | * as published by the Free Software Foundation; either version 2 | ||
23 | * of the License, or (at your option) any later version. | ||
24 | * | ||
25 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
26 | * KIND, either express or implied. | ||
27 | * | ||
28 | ****************************************************************************/ | ||
29 | |||
30 | /* | ||
31 | LodePNG version 20080927 | ||
32 | |||
33 | Copyright (c) 2005-2008 Lode Vandevenne | ||
34 | |||
35 | This software is provided 'as-is', without any express or implied | ||
36 | warranty. In no event will the authors be held liable for any damages | ||
37 | arising from the use of this software. | ||
38 | |||
39 | Permission is granted to anyone to use this software for any purpose, | ||
40 | including commercial applications, and to alter it and redistribute it | ||
41 | freely, subject to the following restrictions: | ||
42 | |||
43 | 1. The origin of this software must not be misrepresented; you must not | ||
44 | claim that you wrote the original software. If you use this software | ||
45 | in a product, an acknowledgment in the product documentation would be | ||
46 | appreciated but is not required. | ||
47 | |||
48 | 2. Altered source versions must be plainly marked as such, and must not be | ||
49 | misrepresented as being the original software. | ||
50 | |||
51 | 3. This notice may not be removed or altered from any source | ||
52 | distribution. | ||
53 | */ | ||
54 | |||
55 | /* | ||
56 | The manual and changelog can be found in the header file "lodepng.h" | ||
57 | You are free to name this file lodepng.cpp or lodepng.c depending on your usage. | ||
58 | */ | ||
59 | |||
60 | /* supported chunk types: | ||
61 | * critical: | ||
62 | * IHDR | ||
63 | * PLTE | ||
64 | * IDAT | ||
65 | * IEND | ||
66 | * | ||
67 | * ancillary: | ||
68 | * tRNS | ||
69 | * bKGD | ||
70 | */ | ||
71 | |||
72 | #include "plugin.h" | ||
73 | #include "lcd.h" | ||
74 | #include <lib/pluginlib_bmp.h> | ||
75 | #include "tinf.h" | ||
76 | #include "bmp.h" | ||
77 | #include "png_decoder.h" | ||
78 | #if LCD_DEPTH < 8 | ||
79 | #include <lib/grey.h> | ||
80 | #endif | ||
81 | |||
82 | #ifndef resize_bitmap | ||
83 | #if defined(HAVE_LCD_COLOR) | ||
84 | #define resize_bitmap smooth_resize_bitmap | ||
85 | #else | ||
86 | #define resize_bitmap grey_resize_bitmap | ||
87 | #endif | ||
88 | #endif | ||
89 | |||
90 | static const char *png_error_messages[PNG_ERROR_MAX-PNG_ERROR_MIN+1] = | ||
91 | { | ||
92 | "png file smaller than a png header", /*27*/ | ||
93 | "incorrect png signature", /*28*/ | ||
94 | "first chunk is not IHDR", /*29*/ | ||
95 | "chunk length too large", /*30*/ | ||
96 | "illegal PNG color type or bpp", /*31*/ | ||
97 | "illegal PNG compression method", /*32*/ | ||
98 | "illegal PNG filter method", /*33*/ | ||
99 | "illegal PNG interlace method", /*34*/ | ||
100 | "chunk length of a chunk is too large or the chunk too small", /*35*/ | ||
101 | "illegal PNG filter type encountered", /*36*/ | ||
102 | "illegal bit depth for this color type given", /*37*/ | ||
103 | "the palette is too big (more than 256 colors)", /*38*/ | ||
104 | "more palette alpha values given in tRNS, than there are colors in the palette", /*39*/ | ||
105 | "tRNS chunk has wrong size for greyscale image", /*40*/ | ||
106 | "tRNS chunk has wrong size for RGB image", /*41*/ | ||
107 | "tRNS chunk appeared while it was not allowed for this color type", /*42*/ | ||
108 | "bKGD chunk has wrong size for palette image", /*43*/ | ||
109 | "bKGD chunk has wrong size for greyscale image", /*44*/ | ||
110 | "bKGD chunk has wrong size for RGB image", /*45*/ | ||
111 | "value encountered in indexed image is larger than the palette size", /*46*/ | ||
112 | "value encountered in indexed image is larger than the palette size", /*47*/ | ||
113 | "input file is empty", /*48*/ | ||
114 | NULL, /*49*/ | ||
115 | NULL, /*50*/ | ||
116 | NULL, /*51*/ | ||
117 | NULL, /*52*/ | ||
118 | NULL, /*53*/ | ||
119 | NULL, /*54*/ | ||
120 | NULL, /*55*/ | ||
121 | NULL, /*56*/ | ||
122 | "invalid CRC", /*57*/ | ||
123 | NULL, /*58*/ | ||
124 | "conversion to unexisting or unsupported color type or bit depth", /*59*/ | ||
125 | NULL, /*60*/ | ||
126 | NULL, /*61*/ | ||
127 | NULL, /*62*/ | ||
128 | "png chunk too long", /*63*/ | ||
129 | NULL, /*64*/ | ||
130 | NULL, /*65*/ | ||
131 | NULL, /*66*/ | ||
132 | NULL, /*67*/ | ||
133 | NULL, /*68*/ | ||
134 | "unknown critical chunk", /*69*/ | ||
135 | NULL, /*70*/ | ||
136 | NULL, /*71*/ | ||
137 | NULL, /*72*/ | ||
138 | "invalid tIME chunk size", /*73*/ | ||
139 | "invalid pHYs chunk size", /*74*/ | ||
140 | }; | ||
141 | |||
142 | /* | ||
143 | The two functions below (LodePNG_decompress and LodePNG_compress) directly call the | ||
144 | LodeZlib_decompress and LodeZlib_compress functions. The only purpose of the functions | ||
145 | below, is to provide the ability to let LodePNG use a different Zlib encoder by only | ||
146 | changing the two functions below, instead of changing it inside the vareous places | ||
147 | in the other LodePNG functions. | ||
148 | |||
149 | *out must be NULL and *outsize must be 0 initially, and after the function is done, | ||
150 | *out must point to the decompressed data, *outsize must be the size of it, and must | ||
151 | be the size of the useful data in bytes, not the alloc size. | ||
152 | */ | ||
153 | |||
154 | static unsigned LodePNG_decompress(unsigned char* out, | ||
155 | size_t* outsize, | ||
156 | const unsigned char* in, | ||
157 | size_t insize) | ||
158 | { | ||
159 | int err; | ||
160 | err = tinf_zlib_uncompress((void *)out, | ||
161 | (unsigned int*)outsize, | ||
162 | (const void*)in, | ||
163 | (unsigned int)insize); | ||
164 | return err; | ||
165 | } | ||
166 | |||
167 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
168 | /* / Reading and writing single bits and bytes from/to stream for LodePNG / */ | ||
169 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
170 | |||
171 | static unsigned char readBitFromReversedStream(size_t* bitpointer, | ||
172 | const unsigned char* bitstream) | ||
173 | { | ||
174 | unsigned char result = (unsigned char)((bitstream[(*bitpointer) >> 3] >> | ||
175 | (7 - ((*bitpointer) & 0x7))) & 1); | ||
176 | (*bitpointer)++; | ||
177 | return result; | ||
178 | } | ||
179 | |||
180 | static unsigned readBitsFromReversedStream(size_t* bitpointer, | ||
181 | const unsigned char* bitstream, | ||
182 | size_t nbits) | ||
183 | { | ||
184 | unsigned result = 0; | ||
185 | size_t i; | ||
186 | for (i = nbits - 1; i < nbits; i--) | ||
187 | result += (unsigned)readBitFromReversedStream(bitpointer, bitstream)<<i; | ||
188 | |||
189 | return result; | ||
190 | } | ||
191 | |||
192 | static void setBitOfReversedStream0(size_t* bitpointer, | ||
193 | unsigned char* bitstream, | ||
194 | unsigned char bit) | ||
195 | { | ||
196 | /* the current bit in bitstream must be 0 for this to work | ||
197 | * earlier bit of huffman code is in a lesser significant bit | ||
198 | * of an earlier byte | ||
199 | */ | ||
200 | if (bit) | ||
201 | bitstream[(*bitpointer) >> 3] |= (bit << (7 - ((*bitpointer) & 0x7))); | ||
202 | |||
203 | (*bitpointer)++; | ||
204 | } | ||
205 | |||
206 | static void setBitOfReversedStream(size_t* bitpointer, | ||
207 | unsigned char* bitstream, | ||
208 | unsigned char bit) | ||
209 | { | ||
210 | /* the current bit in bitstream may be 0 or 1 for this to work */ | ||
211 | if (bit == 0) | ||
212 | bitstream[(*bitpointer) >> 3] &= | ||
213 | (unsigned char)(~(1 << (7 - ((*bitpointer) & 0x7)))); | ||
214 | else | ||
215 | bitstream[(*bitpointer) >> 3] |= (1 << (7 - ((*bitpointer) & 0x7))); | ||
216 | |||
217 | (*bitpointer)++; | ||
218 | } | ||
219 | |||
220 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
221 | /* / PNG chunks / */ | ||
222 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
223 | |||
224 | /* get the length of the data of the chunk. | ||
225 | * Total chunk length has 12 bytes more. | ||
226 | */ | ||
227 | static unsigned LodePNG_chunk_length(const uint8_t* chunk) | ||
228 | { | ||
229 | return chunk[0]<<24|chunk[1]<<16|chunk[2]<<8|chunk[3]; | ||
230 | } | ||
231 | |||
232 | /* check if the type is the given type */ | ||
233 | static bool LodePNG_chunk_type_equals(const uint8_t* chunk, uint32_t type) | ||
234 | { | ||
235 | /* chunk type field: A 4-byte chunk type code. For convenience in | ||
236 | * description and in examining PNG files, type codes are restricted | ||
237 | * to consist of uppercase and lowercase ASCII letters | ||
238 | * (A-Z and a-z, or 65-90 and 97-122 decimal). However, encoders and | ||
239 | * decoders must treat the codes as fixed binary values, not character | ||
240 | * strings." | ||
241 | */ | ||
242 | return ((uint32_t)(chunk[4]<<24|chunk[5]<<16|chunk[6]<<8|chunk[7]) == (uint32_t)type); | ||
243 | } | ||
244 | |||
245 | /* properties of PNG chunks gotten from capitalization of chunk type name, | ||
246 | * as defined by the standard | ||
247 | * 0: ancillary chunk | ||
248 | * 1: critical chunk type | ||
249 | */ | ||
250 | static inline bool LodePNG_chunk_critical(const uint8_t* chunk) | ||
251 | { | ||
252 | return((chunk[4] & 32) == 0); | ||
253 | } | ||
254 | |||
255 | /* 0: public, 1: private */ | ||
256 | static inline bool LodePNG_chunk_private(const uint8_t* chunk) | ||
257 | { | ||
258 | return((chunk[6] & 32) != 0); | ||
259 | } | ||
260 | |||
261 | /* get pointer to the data of the chunk */ | ||
262 | static inline const uint8_t* LodePNG_chunk_data(const uint8_t* chunk) | ||
263 | { | ||
264 | return &chunk[8]; | ||
265 | } | ||
266 | |||
267 | /* returns 0 if the crc is correct, error code if it's incorrect */ | ||
268 | static bool LodePNG_chunk_check_crc(const uint8_t* chunk) | ||
269 | { | ||
270 | uint32_t length = LodePNG_chunk_length(chunk); | ||
271 | uint32_t crc = chunk[length + 8]<<24|chunk[length + 8 + 1]<<16| | ||
272 | chunk[length + 8 + 2]<<8|chunk[length + 8 + 3]; | ||
273 | |||
274 | /* the CRC is taken of the data and the 4 chunk type letters, | ||
275 | * not the length | ||
276 | */ | ||
277 | uint32_t checksum = tinf_crc32(chunk + 4, length + 4); | ||
278 | return (crc == checksum); | ||
279 | } | ||
280 | |||
281 | /* don't use on IEND chunk, as there is no next chunk then */ | ||
282 | static const uint8_t* LodePNG_chunk_next(const uint8_t* chunk) | ||
283 | { | ||
284 | uint32_t total_chunk_length = LodePNG_chunk_length(chunk) + 12; | ||
285 | return &chunk[total_chunk_length]; | ||
286 | } | ||
287 | |||
288 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
289 | /* / Color types and such / */ | ||
290 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
291 | |||
292 | /* return type is a LodePNG error code | ||
293 | * bd - bit depth | ||
294 | */ | ||
295 | static uint8_t checkColorValidity(uint8_t colorType, uint8_t bd) | ||
296 | { | ||
297 | switch (colorType) | ||
298 | { | ||
299 | case PNG_COLORTYPE_GREY: | ||
300 | if (!(bd == 1 || bd == 2 || | ||
301 | bd == 4 || bd == 8 || | ||
302 | bd == 16)) | ||
303 | return 37; | ||
304 | break; /*grey*/ | ||
305 | |||
306 | case PNG_COLORTYPE_RGB: | ||
307 | if (!(bd == 8 || bd == 16)) | ||
308 | return 37; | ||
309 | break; /*RGB*/ | ||
310 | |||
311 | case PNG_COLORTYPE_PALETTE: | ||
312 | if (!(bd == 1 || bd == 2 || | ||
313 | bd == 4 || bd == 8 )) | ||
314 | return 37; | ||
315 | break; /*palette*/ | ||
316 | |||
317 | case PNG_COLORTYPE_GREYA: | ||
318 | if (!( bd == 8 || bd == 16 )) | ||
319 | return 37; | ||
320 | break; /*grey + alpha*/ | ||
321 | |||
322 | case PNG_COLORTYPE_RGBA: | ||
323 | if (!( bd == 8 || bd == 16 )) | ||
324 | return 37; | ||
325 | break; /*RGBA*/ | ||
326 | |||
327 | default: | ||
328 | return 31; | ||
329 | } | ||
330 | return 0; /*allowed color type / bits combination*/ | ||
331 | } | ||
332 | |||
333 | static uint8_t getNumColorChannels(uint8_t colorType) | ||
334 | { | ||
335 | switch (colorType) | ||
336 | { | ||
337 | case PNG_COLORTYPE_GREY: | ||
338 | return 1; /*grey*/ | ||
339 | case PNG_COLORTYPE_RGB: | ||
340 | return 3; /*RGB*/ | ||
341 | case PNG_COLORTYPE_PALETTE: | ||
342 | return 1; /*palette*/ | ||
343 | case PNG_COLORTYPE_GREYA: | ||
344 | return 2; /*grey + alpha*/ | ||
345 | case PNG_COLORTYPE_RGBA: | ||
346 | return 4; /*RGBA*/ | ||
347 | } | ||
348 | return 0; /*unexisting color type*/ | ||
349 | } | ||
350 | |||
351 | static uint8_t getBpp(uint8_t colorType, uint8_t bitDepth) | ||
352 | { | ||
353 | /* bits per pixel is amount of channels * bits per channel */ | ||
354 | return getNumColorChannels(colorType) * bitDepth; | ||
355 | } | ||
356 | |||
357 | static void LodePNG_InfoColor_init(LodePNG_InfoColor* info) | ||
358 | { | ||
359 | info->key_defined = 0; | ||
360 | info->key_r = info->key_g = info->key_b = 0; | ||
361 | info->colorType = PNG_COLORTYPE_RGBA; | ||
362 | info->bitDepth = 8; | ||
363 | memset(info->palette, 0, 256 * 4 * sizeof(unsigned char)); | ||
364 | info->palettesize = 0; | ||
365 | } | ||
366 | |||
367 | static void LodePNG_InfoColor_cleanup(LodePNG_InfoColor* info) | ||
368 | { | ||
369 | info->palettesize = 0; | ||
370 | } | ||
371 | |||
372 | static void LodePNG_InfoPng_init(LodePNG_InfoPng* info) | ||
373 | { | ||
374 | info->width = info->height = 0; | ||
375 | LodePNG_InfoColor_init(&info->color); | ||
376 | info->interlaceMethod = 0; | ||
377 | info->compressionMethod = 0; | ||
378 | info->filterMethod = 0; | ||
379 | #ifdef HAVE_LCD_COLOR | ||
380 | info->background_r = info->background_g = info->background_b = 0; | ||
381 | #else | ||
382 | info->background_r = info->background_g = info->background_b = 255; | ||
383 | #endif | ||
384 | } | ||
385 | |||
386 | void LodePNG_InfoPng_cleanup(LodePNG_InfoPng* info) | ||
387 | { | ||
388 | LodePNG_InfoColor_cleanup(&info->color); | ||
389 | } | ||
390 | |||
391 | /* | ||
392 | * Convert from every colortype to rockbox native pixel format (color targets) or | ||
393 | * greylib pixel format (grey targets) | ||
394 | */ | ||
395 | static void LodePNG_convert(LodePNG_Decoder *decoder) | ||
396 | { | ||
397 | |||
398 | LodePNG_InfoPng *infoIn = &decoder->infoPng; | ||
399 | const uint8_t *in = decoder->decoded_img; | ||
400 | uint8_t *out = decoder->buf; | ||
401 | uint16_t w = infoIn->width; | ||
402 | uint16_t h = infoIn->height; | ||
403 | size_t i, j, bp = 0; /*bitpointer, used by less-than-8-bit color types*/ | ||
404 | size_t x, y; | ||
405 | uint16_t value, alpha, alpha_complement; | ||
406 | |||
407 | /* line buffer for pixel format transformation */ | ||
408 | #ifdef HAVE_LCD_COLOR | ||
409 | struct uint8_rgb *line_buf = (struct uint8_rgb *)(out + w * h * FB_DATA_SZ); | ||
410 | #else | ||
411 | uint8_t *line_buf = (unsigned char *)(out + w * h * FB_DATA_SZ); | ||
412 | #endif | ||
413 | |||
414 | struct bitmap bm = { | ||
415 | .width = w, | ||
416 | .height = h, | ||
417 | .data = (unsigned char*)out, | ||
418 | }; | ||
419 | |||
420 | struct scaler_context ctx = { | ||
421 | .bm = &bm, | ||
422 | .dither = 0, | ||
423 | }; | ||
424 | |||
425 | #if LCD_DEPTH < 8 | ||
426 | const struct custom_format *cformat = &format_grey; | ||
427 | #else | ||
428 | const struct custom_format *cformat = &format_native; | ||
429 | #endif | ||
430 | |||
431 | void (*output_row_8)(uint32_t, void*, struct scaler_context*) = cformat->output_row_8; | ||
432 | |||
433 | #ifdef HAVE_LCD_COLOR | ||
434 | struct uint8_rgb *pixel; | ||
435 | #else | ||
436 | unsigned char *pixel; | ||
437 | #endif | ||
438 | |||
439 | #ifdef HAVE_LCD_COLOR | ||
440 | if (infoIn->color.bitDepth == 8) | ||
441 | { | ||
442 | switch (infoIn->color.colorType) | ||
443 | { | ||
444 | case PNG_COLORTYPE_GREY: /*greyscale color*/ | ||
445 | i = 0; | ||
446 | for (y = 0 ; y < h ; y++) { | ||
447 | /* reset line buf */ | ||
448 | pixel = line_buf; | ||
449 | |||
450 | for (x = 0; x < w ; x++) { | ||
451 | value = in[i++]; | ||
452 | if (infoIn->color.key_defined) | ||
453 | if ( (uint8_t)value == (uint8_t)infoIn->color.key_r ) | ||
454 | value = infoIn->background_r; /* full transparent */ | ||
455 | |||
456 | pixel->red = (uint8_t)value; | ||
457 | pixel->green = (uint8_t)value; | ||
458 | pixel->blue = (uint8_t)value; | ||
459 | pixel++; | ||
460 | } | ||
461 | output_row_8(y,(void *)line_buf,&ctx); | ||
462 | } | ||
463 | break; | ||
464 | case PNG_COLORTYPE_RGB: /*RGB color*/ | ||
465 | i = 0; | ||
466 | for (y = 0 ; y < h ; y++) { | ||
467 | pixel = line_buf; | ||
468 | for (x = 0 ; x < w ; x++) { | ||
469 | j = 3*i++; | ||
470 | |||
471 | /* tRNs & bKGD */ | ||
472 | if (infoIn->color.key_defined && | ||
473 | in[j] == (uint8_t)infoIn->color.key_r && | ||
474 | in[j + 1] == (uint8_t)infoIn->color.key_g && | ||
475 | in[j + 2] == (uint8_t)infoIn->color.key_b) | ||
476 | { | ||
477 | pixel->red = (uint8_t)infoIn->background_r; | ||
478 | pixel->green = (uint8_t)infoIn->background_g; | ||
479 | pixel->blue = (uint8_t)infoIn->background_b; | ||
480 | } | ||
481 | else | ||
482 | { | ||
483 | pixel->red = in[j]; | ||
484 | pixel->green = in[j + 1]; | ||
485 | pixel->blue = in[j + 2]; | ||
486 | } | ||
487 | pixel++; | ||
488 | } | ||
489 | output_row_8(y,(void *)line_buf,&ctx); | ||
490 | } | ||
491 | break; | ||
492 | case PNG_COLORTYPE_PALETTE: /*indexed color (palette)*/ | ||
493 | i = 0; | ||
494 | for (y = 0 ; y < h ; y++) { | ||
495 | /* reset line buf */ | ||
496 | pixel = line_buf; | ||
497 | for (x = 0 ; x < w ; x++) { | ||
498 | if (in[i] >= infoIn->color.palettesize) | ||
499 | { | ||
500 | decoder->error = 46; | ||
501 | return; | ||
502 | } | ||
503 | |||
504 | j = in[i++]<<2; | ||
505 | alpha = infoIn->color.palette[j + 3]; | ||
506 | alpha_complement = (256 - alpha); | ||
507 | |||
508 | /* tRNS and bKGD */ | ||
509 | pixel->red = (infoIn->color.palette[j] * alpha + | ||
510 | alpha_complement*infoIn->background_r)>>8; | ||
511 | pixel->green = (infoIn->color.palette[j + 1] * alpha + | ||
512 | alpha_complement*infoIn->background_g)>>8; | ||
513 | pixel->blue = (infoIn->color.palette[j + 2] * alpha + | ||
514 | alpha_complement*infoIn->background_b)>>8; | ||
515 | pixel++; | ||
516 | } | ||
517 | output_row_8(y,(void *)(line_buf),&ctx); | ||
518 | } | ||
519 | break; | ||
520 | case PNG_COLORTYPE_GREYA: /*greyscale with alpha*/ | ||
521 | i = 0; | ||
522 | for (y = 0 ; y < h ; y++) { | ||
523 | pixel = line_buf; | ||
524 | for (x = 0 ; x < w ; x++) { | ||
525 | alpha = in[(i << 1) + 1]; | ||
526 | alpha_complement = (256 - alpha)*infoIn->background_r; | ||
527 | value = (alpha * in[i++ << 1] + alpha_complement)>>8; | ||
528 | pixel->red = (uint8_t)(value); | ||
529 | pixel->green = (uint8_t)value; | ||
530 | pixel->blue = (uint8_t)value; | ||
531 | pixel++; | ||
532 | } | ||
533 | output_row_8(y,(void *)line_buf,&ctx); | ||
534 | } | ||
535 | break; | ||
536 | case PNG_COLORTYPE_RGBA: /*RGB with alpha*/ | ||
537 | i = 0; | ||
538 | for (y = 0 ; y < h ; y++) { | ||
539 | pixel = line_buf; | ||
540 | for (x = 0 ; x < w ; x++) { | ||
541 | j = i++ << 2; | ||
542 | alpha = in[j + 3]; | ||
543 | alpha_complement = (256 - alpha); | ||
544 | pixel->red = (in[j] * alpha + | ||
545 | alpha_complement*infoIn->background_r)>>8; | ||
546 | pixel->green = (in[j + 1] * alpha + | ||
547 | alpha_complement*infoIn->background_g)>>8; | ||
548 | pixel->blue = (in[j + 2] * alpha + | ||
549 | alpha_complement*infoIn->background_b)>>8; | ||
550 | pixel++; | ||
551 | } | ||
552 | output_row_8(y,(void *)line_buf,&ctx); | ||
553 | } | ||
554 | break; | ||
555 | default: | ||
556 | break; | ||
557 | } | ||
558 | } | ||
559 | else if (infoIn->color.bitDepth == 16) | ||
560 | { | ||
561 | switch (infoIn->color.colorType) | ||
562 | { | ||
563 | case PNG_COLORTYPE_GREY: /*greyscale color*/ | ||
564 | i = 0; | ||
565 | for (y = 0 ; y < h ; y++) { | ||
566 | pixel = line_buf; | ||
567 | for (x = 0 ; x < w ; x++) { | ||
568 | value = (in[i<<1]<<8)|in[(i << 1) + 1]; | ||
569 | i++; | ||
570 | |||
571 | /* tRNS and bKGD */ | ||
572 | if (infoIn->color.key_defined && | ||
573 | value == infoIn->color.key_r) | ||
574 | value = infoIn->background_r<<8; | ||
575 | |||
576 | pixel->red = | ||
577 | pixel->green = | ||
578 | pixel->blue = (uint8_t)(value>>8); | ||
579 | pixel++; | ||
580 | } | ||
581 | output_row_8(y,(void *)line_buf,&ctx); | ||
582 | } | ||
583 | break; | ||
584 | case PNG_COLORTYPE_RGB: /*RGB color*/ | ||
585 | i = 0; | ||
586 | for (y = 0 ; y < h ; y++) { | ||
587 | pixel = line_buf; | ||
588 | for (x = 0 ; x < w ; x++) { | ||
589 | j = 6 * i++; | ||
590 | |||
591 | /* tRNS and bKGD */ | ||
592 | if (infoIn->color.key_defined && | ||
593 | ((uint16_t)(in[j]<<8|in[j + 1]) == | ||
594 | infoIn->color.key_r) && | ||
595 | ((uint16_t)(in[j + 2]<<8|in[j + 3]) == | ||
596 | infoIn->color.key_g) && | ||
597 | ((uint16_t)(in[j + 4]<<8|in[j + 5]) == | ||
598 | infoIn->color.key_b)) | ||
599 | { | ||
600 | pixel->red = (uint8_t)infoIn->background_r; | ||
601 | pixel->green = (uint8_t)infoIn->background_g; | ||
602 | pixel->blue = (uint8_t)infoIn->background_b; | ||
603 | } | ||
604 | else | ||
605 | { | ||
606 | pixel->red = in[j]; | ||
607 | pixel->green = in[j + 2]; | ||
608 | pixel->blue = in[j + 4]; | ||
609 | } | ||
610 | pixel++; | ||
611 | } | ||
612 | output_row_8(y,(void *)line_buf,&ctx); | ||
613 | } | ||
614 | break; | ||
615 | case PNG_COLORTYPE_GREYA: /*greyscale with alpha*/ | ||
616 | i = 0; | ||
617 | for (y = 0 ; y < h ; y++) { | ||
618 | pixel = line_buf; | ||
619 | for (x = 0 ; x < w ; x++) { | ||
620 | alpha = in[(i << 2) + 2]; | ||
621 | alpha_complement = (256-alpha)*infoIn->background_r; | ||
622 | value = (in[i++ << 2] * alpha + alpha_complement)>>8; | ||
623 | pixel->red = (uint8_t)value; | ||
624 | pixel->green = (uint8_t)value; | ||
625 | pixel->blue = (uint8_t)value; | ||
626 | pixel++; | ||
627 | } | ||
628 | output_row_8(y,(void *)line_buf,&ctx); | ||
629 | } | ||
630 | break; | ||
631 | case PNG_COLORTYPE_RGBA: /*RGB with alpha*/ | ||
632 | i = 0; | ||
633 | for (y = 0 ; y < h ; y++) { | ||
634 | pixel = line_buf; | ||
635 | for (x = 0 ; x < w ; x++) { | ||
636 | j = i++ << 3; | ||
637 | alpha = in[j + 6]; | ||
638 | alpha_complement = (256-alpha); | ||
639 | pixel->red = (in[j] * alpha + | ||
640 | alpha_complement*infoIn->background_r)>>8; | ||
641 | pixel->green = (in[j + 2] * alpha + | ||
642 | alpha_complement*infoIn->background_g)>>8; | ||
643 | pixel->blue = (in[j + 4] * alpha + | ||
644 | alpha_complement*infoIn->background_b)>>8; | ||
645 | pixel++; | ||
646 | } | ||
647 | output_row_8(y,(void *)line_buf,&ctx); | ||
648 | } | ||
649 | break; | ||
650 | default: | ||
651 | break; | ||
652 | } | ||
653 | } | ||
654 | else /*infoIn->bitDepth is less than 8 bit per channel*/ | ||
655 | { | ||
656 | switch (infoIn->color.colorType) | ||
657 | { | ||
658 | case PNG_COLORTYPE_GREY: /*greyscale color*/ | ||
659 | i = 0; | ||
660 | for (y = 0 ; y < h ; y++) { | ||
661 | pixel = line_buf; | ||
662 | for (x = 0 ; x < w ; x++) { | ||
663 | value = readBitsFromReversedStream(&bp, in, infoIn->color.bitDepth); | ||
664 | |||
665 | /* tRNS and bKGD */ | ||
666 | if (infoIn->color.key_defined) | ||
667 | if ( value == infoIn->color.key_r ) | ||
668 | value = infoIn->background_r; /* full transparent */ | ||
669 | |||
670 | /* scale value from 0 to 255 */ | ||
671 | value = (value * 255) / ((1 << infoIn->color.bitDepth) - 1); | ||
672 | |||
673 | pixel->red = (uint8_t)value; | ||
674 | pixel->green = (uint8_t)value; | ||
675 | pixel->blue = (uint8_t)value; | ||
676 | pixel++; | ||
677 | } | ||
678 | output_row_8(y,(void *)line_buf,&ctx); | ||
679 | } | ||
680 | break; | ||
681 | case PNG_COLORTYPE_PALETTE: /*indexed color (palette)*/ | ||
682 | i = 0; | ||
683 | for (y = 0 ; y < h ; y++) { | ||
684 | pixel = line_buf; | ||
685 | for (x = 0 ; x < w ; x++) { | ||
686 | value = readBitsFromReversedStream(&bp, in, infoIn->color.bitDepth); | ||
687 | if (value >= infoIn->color.palettesize) | ||
688 | { | ||
689 | decoder->error = 47; | ||
690 | return; | ||
691 | } | ||
692 | |||
693 | j = value << 2; | ||
694 | |||
695 | /* tRNS and bKGD */ | ||
696 | alpha = infoIn->color.palette[j + 3]; | ||
697 | alpha_complement = (256 - alpha); | ||
698 | pixel->red = (alpha * infoIn->color.palette[j] + | ||
699 | alpha_complement*infoIn->background_r)>>8; | ||
700 | pixel->green = (alpha * infoIn->color.palette[j + 1] + | ||
701 | alpha_complement*infoIn->background_g)>>8; | ||
702 | pixel->blue = (alpha * infoIn->color.palette[j + 2] + | ||
703 | alpha_complement*infoIn->background_b)>>8; | ||
704 | pixel++; | ||
705 | } | ||
706 | output_row_8(y,(void *)line_buf,&ctx); | ||
707 | } | ||
708 | break; | ||
709 | default: | ||
710 | break; | ||
711 | } | ||
712 | } | ||
713 | #else /* greyscale targets */ | ||
714 | struct uint8_rgb px_rgb; /* for rgb(a) -> greyscale conversion */ | ||
715 | uint8_t background_grey; /* for rgb background -> greyscale background */ | ||
716 | |||
717 | if (infoIn->color.bitDepth == 8) | ||
718 | { | ||
719 | switch (infoIn->color.colorType) | ||
720 | { | ||
721 | case PNG_COLORTYPE_GREY: /*greyscale color*/ | ||
722 | i = 0; | ||
723 | for (y = 0 ; y < h ; y++) { | ||
724 | pixel = line_buf; | ||
725 | for (x = 0 ; x < w ; x++ ) { | ||
726 | value = in[i++]; | ||
727 | |||
728 | /* transparent color defined in tRNS chunk */ | ||
729 | if (infoIn->color.key_defined) | ||
730 | if ( (uint8_t)value == (uint8_t)infoIn->color.key_r ) | ||
731 | value = infoIn->background_r; | ||
732 | |||
733 | *pixel++ = (uint8_t)value; | ||
734 | } | ||
735 | output_row_8(y,(void *)line_buf,&ctx); | ||
736 | } | ||
737 | break; | ||
738 | case PNG_COLORTYPE_RGB: /*RGB color*/ | ||
739 | /* convert background rgb color to greyscale */ | ||
740 | px_rgb.red = infoIn->background_r; | ||
741 | px_rgb.green = infoIn->background_g; | ||
742 | px_rgb.blue = infoIn->background_b; | ||
743 | background_grey = brightness(px_rgb); | ||
744 | |||
745 | i = 0; | ||
746 | for (y = 0 ; y < h ; y++) { | ||
747 | pixel = line_buf; | ||
748 | for (x = 0 ; x < w ; x++) { | ||
749 | j = 3*i++; | ||
750 | |||
751 | /* tRNs & bKGD */ | ||
752 | if (infoIn->color.key_defined && | ||
753 | in[j] == (uint8_t)infoIn->color.key_r && | ||
754 | in[j + 1] == (uint8_t)infoIn->color.key_g && | ||
755 | in[j + 2] == (uint8_t)infoIn->color.key_b) | ||
756 | { | ||
757 | *pixel = background_grey; | ||
758 | } | ||
759 | else | ||
760 | { | ||
761 | /* rgb -> greyscale */ | ||
762 | px_rgb.red = in[j]; | ||
763 | px_rgb.green = in[j + 1]; | ||
764 | px_rgb.blue = in[j + 2]; | ||
765 | *pixel = brightness(px_rgb); | ||
766 | } | ||
767 | pixel++; | ||
768 | |||
769 | } | ||
770 | output_row_8(y,(void *)line_buf,&ctx); | ||
771 | } | ||
772 | break; | ||
773 | case PNG_COLORTYPE_PALETTE: /*indexed color (palette)*/ | ||
774 | i = 0; | ||
775 | /* calculate grey value of rgb background */ | ||
776 | px_rgb.red = infoIn->background_r; | ||
777 | px_rgb.green = infoIn->background_g; | ||
778 | px_rgb.blue = infoIn->background_b; | ||
779 | background_grey = brightness(px_rgb); | ||
780 | |||
781 | for (y = 0 ; y < h ; y++) { | ||
782 | /* reset line buf */ | ||
783 | pixel = line_buf; | ||
784 | for (x = 0 ; x < w ; x++) { | ||
785 | if (in[i] >= infoIn->color.palettesize) | ||
786 | { | ||
787 | decoder->error = 46; | ||
788 | return; | ||
789 | } | ||
790 | |||
791 | j = in[i++] << 2; | ||
792 | alpha = infoIn->color.palette[j + 3]; | ||
793 | alpha_complement = (256 - alpha); | ||
794 | |||
795 | /* tRNS and bKGD */ | ||
796 | px_rgb.red = (alpha * infoIn->color.palette[j] + | ||
797 | alpha_complement*background_grey)>>8; | ||
798 | px_rgb.green = (alpha * infoIn->color.palette[j + 1] + | ||
799 | alpha_complement*background_grey)>>8; | ||
800 | px_rgb.blue = (alpha * infoIn->color.palette[j + 2] + | ||
801 | alpha_complement*background_grey)>>8; | ||
802 | |||
803 | *pixel++ = brightness(px_rgb); | ||
804 | } | ||
805 | output_row_8(y,(void *)(line_buf),&ctx); | ||
806 | } | ||
807 | break; | ||
808 | case PNG_COLORTYPE_GREYA: /*greyscale with alpha*/ | ||
809 | i = 0; | ||
810 | for (y = 0 ; y < h ; y++) { | ||
811 | pixel = line_buf; | ||
812 | for (x = 0 ; x < w ; x++) { | ||
813 | alpha = in[(i << 1) + 1]; | ||
814 | alpha_complement = ((256 - alpha)*infoIn->background_r); | ||
815 | *pixel++ = (alpha * in[i++ << 1] + alpha_complement)>>8; | ||
816 | } | ||
817 | output_row_8(y,(void *)line_buf,&ctx); | ||
818 | } | ||
819 | break; | ||
820 | case PNG_COLORTYPE_RGBA: /*RGB with alpha*/ | ||
821 | px_rgb.red = infoIn->background_r; | ||
822 | px_rgb.green = infoIn->background_g; | ||
823 | px_rgb.blue = infoIn->background_b; | ||
824 | background_grey = brightness(px_rgb); | ||
825 | |||
826 | i = 0; | ||
827 | for (y = 0 ; y < h ; y++) { | ||
828 | pixel = line_buf; | ||
829 | for (x = 0 ; x < w ; x++) { | ||
830 | j = i++ << 2; | ||
831 | alpha = in[j + 3]; | ||
832 | alpha_complement = ((256 - alpha)*background_grey); | ||
833 | |||
834 | px_rgb.red = in[j]; | ||
835 | px_rgb.green = in[j + 1]; | ||
836 | px_rgb.blue = in[j + 2]; | ||
837 | *pixel++ = (alpha * brightness(px_rgb) + | ||
838 | alpha_complement)>>8; | ||
839 | } | ||
840 | output_row_8(y,(void *)line_buf,&ctx); | ||
841 | } | ||
842 | break; | ||
843 | default: | ||
844 | break; | ||
845 | } | ||
846 | } | ||
847 | else if (infoIn->color.bitDepth == 16) | ||
848 | { | ||
849 | switch (infoIn->color.colorType) | ||
850 | { | ||
851 | case PNG_COLORTYPE_GREY: /*greyscale color*/ | ||
852 | i = 0; | ||
853 | for (y = 0 ; y < h ; y++) { | ||
854 | pixel = line_buf; | ||
855 | for (x = 0 ; x < w ; x++) { | ||
856 | /* specification states that we have to compare | ||
857 | * colors for simple transparency in 16bits | ||
858 | * even if we scale down to 8bits later | ||
859 | */ | ||
860 | value = in[i<<1]<<8|in[(i << 1) + 1]; | ||
861 | i++; | ||
862 | |||
863 | /* tRNS and bKGD */ | ||
864 | if (infoIn->color.key_defined && | ||
865 | value == infoIn->color.key_r) | ||
866 | value = infoIn->background_r<<8; | ||
867 | |||
868 | /* we take upper 8bits */ | ||
869 | *pixel++ = (uint8_t)(value>>8); | ||
870 | } | ||
871 | |||
872 | output_row_8(y,(void *)line_buf,&ctx); | ||
873 | } | ||
874 | break; | ||
875 | case PNG_COLORTYPE_RGB: /*RGB color*/ | ||
876 | i = 0; | ||
877 | px_rgb.red = infoIn->background_r; | ||
878 | px_rgb.green = infoIn->background_g; | ||
879 | px_rgb.blue = infoIn->background_b; | ||
880 | background_grey = brightness(px_rgb); | ||
881 | |||
882 | for (y = 0 ; y < h ; y++) { | ||
883 | pixel = line_buf; | ||
884 | for (x = 0 ; x < w ; x++) { | ||
885 | j = 6 * i++; | ||
886 | |||
887 | /* tRNS and bKGD */ | ||
888 | if (infoIn->color.key_defined && | ||
889 | (uint16_t)(in[j]<<8|in[j + 1]) == | ||
890 | infoIn->color.key_r && | ||
891 | (uint16_t)(in[j + 2]<<8|in[j + 3]) == | ||
892 | infoIn->color.key_g && | ||
893 | (uint16_t)(in[j + 4]<<8|in[j + 5]) == | ||
894 | infoIn->color.key_b) | ||
895 | { | ||
896 | *pixel = background_grey; | ||
897 | } | ||
898 | else | ||
899 | { | ||
900 | /* we take only upper byte of 16bit value */ | ||
901 | px_rgb.red = in[j]; | ||
902 | px_rgb.green = in[j + 2]; | ||
903 | px_rgb.blue = in[j + 4]; | ||
904 | *pixel = brightness(px_rgb); | ||
905 | } | ||
906 | pixel++; | ||
907 | } | ||
908 | output_row_8(y,(void *)line_buf,&ctx); | ||
909 | } | ||
910 | break; | ||
911 | case PNG_COLORTYPE_GREYA: /*greyscale with alpha*/ | ||
912 | i = 0; | ||
913 | for (y = 0 ; y < h ; y++) { | ||
914 | pixel = line_buf; | ||
915 | for (x = 0 ; x < w ; x++) { | ||
916 | alpha = in[(i << 2) + 2]; | ||
917 | alpha_complement = (256 - alpha)*infoIn->background_r; | ||
918 | *pixel++ = (alpha * in[i++ << 2] + alpha_complement)>>8; | ||
919 | } | ||
920 | output_row_8(y,(void *)line_buf,&ctx); | ||
921 | } | ||
922 | break; | ||
923 | case PNG_COLORTYPE_RGBA: /*RGB with alpha*/ | ||
924 | px_rgb.red = infoIn->background_r; | ||
925 | px_rgb.green = infoIn->background_g; | ||
926 | px_rgb.blue = infoIn->background_b; | ||
927 | background_grey = brightness(px_rgb); | ||
928 | |||
929 | i = 0; | ||
930 | for (y = 0 ; y < h ; y++) { | ||
931 | pixel = line_buf; | ||
932 | for (x = 0 ; x < w ; x++) { | ||
933 | j = i++ << 3; | ||
934 | alpha = in[j + 6]; | ||
935 | alpha_complement = (256 - alpha)*background_grey; | ||
936 | px_rgb.red = in[j]; | ||
937 | px_rgb.green = in[j + 2]; | ||
938 | px_rgb.blue = in[j + 4]; | ||
939 | *pixel++ = (alpha * brightness(px_rgb) + alpha_complement)>>8; | ||
940 | } | ||
941 | output_row_8(y,(void *)line_buf,&ctx); | ||
942 | } | ||
943 | break; | ||
944 | default: | ||
945 | break; | ||
946 | } | ||
947 | } | ||
948 | else /*infoIn->bitDepth is less than 8 bit per channel*/ | ||
949 | { | ||
950 | switch (infoIn->color.colorType) | ||
951 | { | ||
952 | case PNG_COLORTYPE_GREY: /*greyscale color*/ | ||
953 | i = 0; | ||
954 | for (y = 0 ; y < h ; y++) { | ||
955 | pixel = line_buf; | ||
956 | for (x = 0 ; x < w ; x++) { | ||
957 | value = readBitsFromReversedStream(&bp, in, infoIn->color.bitDepth); | ||
958 | |||
959 | /* tRNS and bKGD */ | ||
960 | if (infoIn->color.key_defined) | ||
961 | if ( value == infoIn->color.key_r ) | ||
962 | value = infoIn->background_r; /* full transparent */ | ||
963 | |||
964 | /*scale value from 0 to 255*/ | ||
965 | value = (value * 255) / ((1 << infoIn->color.bitDepth) - 1); | ||
966 | |||
967 | *pixel++ = (unsigned char)value; | ||
968 | } | ||
969 | output_row_8(y,(void *)line_buf,&ctx); | ||
970 | } | ||
971 | break; | ||
972 | case PNG_COLORTYPE_PALETTE: /*indexed color (palette)*/ | ||
973 | i = 0; | ||
974 | px_rgb.red = infoIn->background_r; | ||
975 | px_rgb.green = infoIn->background_g; | ||
976 | px_rgb.blue = infoIn->background_b; | ||
977 | uint8_t background_grey = brightness(px_rgb); | ||
978 | |||
979 | for (y = 0 ; y < h ; y++) { | ||
980 | pixel = line_buf; | ||
981 | for (x = 0 ; x < w ; x++) { | ||
982 | value = readBitsFromReversedStream(&bp, in, infoIn->color.bitDepth); | ||
983 | if (value >= infoIn->color.palettesize) | ||
984 | { | ||
985 | decoder->error = 47; | ||
986 | return; | ||
987 | } | ||
988 | |||
989 | j = value << 2; | ||
990 | |||
991 | /* tRNS and bKGD */ | ||
992 | alpha = infoIn->color.palette[j + 3]; | ||
993 | alpha_complement = (256 - alpha) * background_grey; | ||
994 | |||
995 | px_rgb.red = (alpha * infoIn->color.palette[j] + | ||
996 | alpha_complement)>>8; | ||
997 | px_rgb.green = (alpha * infoIn->color.palette[j + 1] + | ||
998 | alpha_complement)>>8; | ||
999 | px_rgb.blue = (alpha * infoIn->color.palette[j + 2] + | ||
1000 | alpha_complement)>>8; | ||
1001 | *pixel++ = brightness(px_rgb); | ||
1002 | } | ||
1003 | output_row_8(y,(void *)line_buf,&ctx); | ||
1004 | } | ||
1005 | break; | ||
1006 | default: | ||
1007 | break; | ||
1008 | } | ||
1009 | } | ||
1010 | #endif | ||
1011 | } | ||
1012 | |||
1013 | /*Paeth predicter, used by PNG filter type 4*/ | ||
1014 | static int paethPredictor(int a, int b, int c) | ||
1015 | { | ||
1016 | int p = a + b - c; | ||
1017 | int pa = p > a ? p - a : a - p; | ||
1018 | int pb = p > b ? p - b : b - p; | ||
1019 | int pc = p > c ? p - c : c - p; | ||
1020 | |||
1021 | if (pa <= pb && pa <= pc) return a; | ||
1022 | else if (pb <= pc) return b; | ||
1023 | else return c; | ||
1024 | } | ||
1025 | |||
1026 | /*shared values used by multiple Adam7 related functions*/ | ||
1027 | |||
1028 | static const uint8_t ADAM7_IX[7] = { 0, 4, 0, 2, 0, 1, 0 }; /*x start values*/ | ||
1029 | static const uint8_t ADAM7_IY[7] = { 0, 0, 4, 0, 2, 0, 1 }; /*y start values*/ | ||
1030 | static const uint8_t ADAM7_DX[7] = { 8, 8, 4, 4, 2, 2, 1 }; /*x delta values*/ | ||
1031 | static const uint8_t ADAM7_DY[7] = { 8, 8, 8, 4, 4, 2, 2 }; /*y delta values*/ | ||
1032 | |||
1033 | static void Adam7_getpassvalues(uint16_t passw[7], | ||
1034 | uint16_t passh[7], | ||
1035 | size_t filter_passstart[8], | ||
1036 | size_t padded_passstart[8], | ||
1037 | size_t passstart[8], | ||
1038 | uint16_t w, | ||
1039 | uint16_t h, | ||
1040 | uint8_t bpp) | ||
1041 | { | ||
1042 | /* the passstart values have 8 values: | ||
1043 | * the 8th one actually indicates the byte after the end | ||
1044 | * of the 7th (= last) pass | ||
1045 | */ | ||
1046 | uint8_t i; | ||
1047 | |||
1048 | /*calculate width and height in pixels of each pass*/ | ||
1049 | for (i = 0; i < 7; i++) | ||
1050 | { | ||
1051 | passw[i] = (w + ADAM7_DX[i] - ADAM7_IX[i] - 1) / ADAM7_DX[i]; | ||
1052 | passh[i] = (h + ADAM7_DY[i] - ADAM7_IY[i] - 1) / ADAM7_DY[i]; | ||
1053 | if (passw[i] == 0) passh[i] = 0; | ||
1054 | if (passh[i] == 0) passw[i] = 0; | ||
1055 | } | ||
1056 | |||
1057 | filter_passstart[0] = padded_passstart[0] = passstart[0] = 0; | ||
1058 | for (i = 0; i < 7; i++) | ||
1059 | { | ||
1060 | /* if passw[i] is 0, it's 0 bytes, not 1 (no filtertype-byte) */ | ||
1061 | filter_passstart[i + 1] = filter_passstart[i] + ((passw[i] && passh[i])? | ||
1062 | passh[i] * (1 + (passw[i] * bpp + 7) / 8):0); | ||
1063 | |||
1064 | /* bits padded if needed to fill full byte at end of each scanline */ | ||
1065 | padded_passstart[i + 1] = padded_passstart[i] + | ||
1066 | passh[i] * ((passw[i] * bpp + 7) / 8); | ||
1067 | |||
1068 | /* only padded at end of reduced image */ | ||
1069 | passstart[i + 1] = passstart[i] + (passh[i] * passw[i] * bpp + 7) / 8; | ||
1070 | } | ||
1071 | } | ||
1072 | |||
1073 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
1074 | /* / PNG Decoder / */ | ||
1075 | /* ////////////////////////////////////////////////////////////////////////// */ | ||
1076 | |||
1077 | |||
1078 | |||
1079 | static uint8_t unfilterScanline(uint8_t* recon, | ||
1080 | const uint8_t* scanline, | ||
1081 | const uint8_t* precon, | ||
1082 | size_t bytewidth, | ||
1083 | uint8_t filterType, | ||
1084 | size_t length) | ||
1085 | { | ||
1086 | /* For PNG filter method 0 | ||
1087 | * unfilter a PNG image scanline by scanline. when the pixels are smaller | ||
1088 | * than 1 byte, the filter works byte per byte (bytewidth = 1) | ||
1089 | * | ||
1090 | * precon is the previous unfiltered scanline, recon the result, | ||
1091 | * scanline the current one | ||
1092 | * | ||
1093 | * the incoming scanlines do NOT include the filtertype byte, that one is | ||
1094 | * given in the parameter filterType instead | ||
1095 | * | ||
1096 | * recon and scanline MAY be the same memory address! precon must be | ||
1097 | * disjoint. | ||
1098 | */ | ||
1099 | |||
1100 | /* storage space for cached portion of scanline */ | ||
1101 | unsigned char cache[512+16]; | ||
1102 | |||
1103 | /* ptr to second element of the cache */ | ||
1104 | unsigned char *cache_1 = cache + bytewidth; | ||
1105 | unsigned char *p_cache = cache + 256 + 8; /* half way */ | ||
1106 | unsigned char *p_cache_1 = p_cache + bytewidth; | ||
1107 | |||
1108 | size_t i; | ||
1109 | switch (filterType) | ||
1110 | { | ||
1111 | case PNG_FILTERTYPE_NONE: | ||
1112 | /* for(i = 0; i < length; i++) recon[i] = scanline[i]; */ | ||
1113 | memcpy(recon, scanline, length * sizeof(uint8_t)); | ||
1114 | break; | ||
1115 | case PNG_FILTERTYPE_SUB: | ||
1116 | /* | ||
1117 | for(i = 0; i < bytewidth; i++) recon[i] = scanline[i]; | ||
1118 | for (i = bytewidth; i < length; i++) | ||
1119 | recon[i] = scanline[i] + recon[i - bytewidth]; | ||
1120 | */ | ||
1121 | |||
1122 | /* first pixel */ | ||
1123 | memcpy(cache, scanline, bytewidth * sizeof(unsigned char)); | ||
1124 | scanline += bytewidth; | ||
1125 | |||
1126 | while ((length - bytewidth) >> 9) /* length >> 9 */ | ||
1127 | { | ||
1128 | /* cache part of scanline */ | ||
1129 | memcpy(cache_1, scanline, 512); | ||
1130 | |||
1131 | /* filtering */ | ||
1132 | for (i=bytewidth; i < 512 + bytewidth; i++) | ||
1133 | cache[i] += cache[i - bytewidth]; | ||
1134 | |||
1135 | /* copy part of filtered scanline */ | ||
1136 | memcpy(recon, cache, 512); | ||
1137 | |||
1138 | /* adjust pointers */ | ||
1139 | recon += 512; | ||
1140 | scanline += 512; | ||
1141 | length -= 512; | ||
1142 | |||
1143 | /* copy last pixel back to the begining of the cache */ | ||
1144 | memcpy(cache, cache + 512, bytewidth * sizeof(unsigned char)); | ||
1145 | } | ||
1146 | |||
1147 | /* less than our cache size */ | ||
1148 | if (length) | ||
1149 | { | ||
1150 | /* cache last part of the scanline */ | ||
1151 | memcpy(cache_1, scanline, length - bytewidth); | ||
1152 | |||
1153 | /* filtering */ | ||
1154 | for (i=bytewidth; i < length; i++) | ||
1155 | cache[i] += cache[i - bytewidth]; | ||
1156 | |||
1157 | /* copy remaining part of the filtered scanline */ | ||
1158 | memcpy(recon, cache, length * sizeof(unsigned char)); | ||
1159 | } | ||
1160 | break; | ||
1161 | case PNG_FILTERTYPE_UP: | ||
1162 | /* | ||
1163 | if (precon) for (i = 0; i < length; i++) | ||
1164 | recon[i] = scanline[i] + precon[i]; | ||
1165 | */ | ||
1166 | if (precon) | ||
1167 | { | ||
1168 | while (length >> 8) | ||
1169 | { | ||
1170 | memcpy(cache, scanline, 256); | ||
1171 | memcpy(p_cache, precon, 256); | ||
1172 | |||
1173 | for (i=0; i < 256; i++) | ||
1174 | cache[i] += p_cache[i]; | ||
1175 | |||
1176 | memcpy(recon, cache, 256); | ||
1177 | |||
1178 | scanline += 256; | ||
1179 | recon += 256; | ||
1180 | precon += 256; | ||
1181 | length -= 256; | ||
1182 | } | ||
1183 | |||
1184 | if (length) | ||
1185 | { | ||
1186 | memcpy(cache, scanline, length); | ||
1187 | memcpy(p_cache, precon, length); | ||
1188 | |||
1189 | for (i=0; i < length; i++) | ||
1190 | cache[i] += p_cache[i]; | ||
1191 | |||
1192 | memcpy(recon, cache, length); | ||
1193 | } | ||
1194 | } | ||
1195 | else | ||
1196 | /* for(i = 0; i < length; i++) recon[i] = scanline[i]; */ | ||
1197 | memcpy(recon, scanline, length * sizeof(uint8_t)); | ||
1198 | break; | ||
1199 | case PNG_FILTERTYPE_AVERAGE: | ||
1200 | if (precon) | ||
1201 | { | ||
1202 | /* | ||
1203 | for (i = 0; i < bytewidth; i++) | ||
1204 | recon[i] = scanline[i] + precon[i] / 2; | ||
1205 | for (i = bytewidth; i < length; i++) | ||
1206 | recon[i] = scanline[i] + ((recon[i - bytewidth] + precon[i]) / 2); | ||
1207 | */ | ||
1208 | memcpy(cache, scanline, bytewidth * sizeof(unsigned char)); | ||
1209 | memcpy(p_cache, precon, bytewidth * sizeof(unsigned char)); | ||
1210 | |||
1211 | for (i = 0; i < bytewidth; i++) | ||
1212 | cache[i] += p_cache[i]>>1; | ||
1213 | |||
1214 | scanline += bytewidth; | ||
1215 | precon += bytewidth; | ||
1216 | |||
1217 | while ((length - bytewidth)>> 8) /* length/256 */ | ||
1218 | { | ||
1219 | memcpy(cache_1, scanline, 256); | ||
1220 | memcpy(p_cache_1, precon, 256); | ||
1221 | |||
1222 | for (i=bytewidth; i < 256 + bytewidth; i++) | ||
1223 | cache[i] += (cache[i - bytewidth] + p_cache[i])>>1; | ||
1224 | |||
1225 | memcpy(recon, cache, 256); | ||
1226 | |||
1227 | recon += 256; | ||
1228 | scanline += 256; | ||
1229 | precon += 256; | ||
1230 | length -= 256; | ||
1231 | |||
1232 | memcpy(cache, cache + 256, bytewidth * sizeof(unsigned char)); | ||
1233 | memcpy(p_cache, p_cache + 256, bytewidth * sizeof(unsigned char)); | ||
1234 | } | ||
1235 | |||
1236 | /* less than our cache size */ | ||
1237 | if (length) | ||
1238 | { | ||
1239 | /* cache last part of the scanline */ | ||
1240 | memcpy(cache_1, scanline, length - bytewidth); | ||
1241 | memcpy(p_cache_1, precon, length - bytewidth); | ||
1242 | |||
1243 | /* filtering */ | ||
1244 | for (i=bytewidth; i < length; i++) | ||
1245 | cache[i] += (cache[i - bytewidth] + p_cache[i])>>1; | ||
1246 | |||
1247 | /* copy remaining part of the filtered scanline */ | ||
1248 | memcpy(recon, cache, length * sizeof(unsigned char)); | ||
1249 | } | ||
1250 | } | ||
1251 | else | ||
1252 | { | ||
1253 | /* | ||
1254 | for(i = 0; i < bytewidth; i++) recon[i] = scanline[i]; | ||
1255 | for (i = bytewidth; i < length; i++) | ||
1256 | recon[i] = scanline[i] + recon[i - bytewidth] / 2; | ||
1257 | */ | ||
1258 | |||
1259 | /* first pixel */ | ||
1260 | memcpy(cache, scanline, bytewidth * sizeof(unsigned char)); | ||
1261 | scanline += bytewidth; | ||
1262 | |||
1263 | while ((length - bytewidth) >> 9) /* length/512 */ | ||
1264 | { | ||
1265 | /* cache part of scanline */ | ||
1266 | memcpy(cache_1, scanline, 512); | ||
1267 | |||
1268 | /* filtering */ | ||
1269 | for (i=bytewidth; i < 512 + bytewidth; i++) | ||
1270 | cache[i] += (cache[i - bytewidth])>>1; | ||
1271 | |||
1272 | /* copy part of filtered scanline */ | ||
1273 | memcpy(recon, cache, 512); | ||
1274 | |||
1275 | /* adjust pointers */ | ||
1276 | recon += 512; | ||
1277 | scanline += 512; | ||
1278 | length -= 512; | ||
1279 | |||
1280 | /* copy last pixel back to the begining of the cache */ | ||
1281 | memcpy(cache, cache + 512, bytewidth * sizeof(unsigned char)); | ||
1282 | } | ||
1283 | |||
1284 | /* less than our cache size */ | ||
1285 | if (length) | ||
1286 | { | ||
1287 | /* cache last part of the scanline */ | ||
1288 | memcpy(cache_1, scanline, length - bytewidth); | ||
1289 | |||
1290 | /* filtering */ | ||
1291 | for (i=bytewidth; i < length; i++) | ||
1292 | cache[i] += (cache[i - bytewidth])>>1; | ||
1293 | |||
1294 | /* copy remaining part of the filtered scanline */ | ||
1295 | memcpy(recon, cache, length * sizeof(unsigned char)); | ||
1296 | } | ||
1297 | } | ||
1298 | break; | ||
1299 | case PNG_FILTERTYPE_PAETH: | ||
1300 | if (precon) | ||
1301 | { | ||
1302 | /* | ||
1303 | for (i = 0; i < bytewidth; i++) | ||
1304 | recon[i] = (uint8_t)(scanline[i] + | ||
1305 | paethPredictor(0, precon[i], 0)); | ||
1306 | for (i = bytewidth; i < length; i++) | ||
1307 | recon[i] = (uint8_t)(scanline[i] + | ||
1308 | paethPredictor(recon[i - bytewidth], | ||
1309 | precon[i], | ||
1310 | precon[i - bytewidth])); | ||
1311 | */ | ||
1312 | |||
1313 | memcpy(cache, scanline, bytewidth * sizeof(unsigned char)); | ||
1314 | memcpy(p_cache, precon, bytewidth * sizeof(unsigned char)); | ||
1315 | |||
1316 | for (i = 0; i < bytewidth; i++) | ||
1317 | cache[i] += paethPredictor(0, p_cache[i], 0); | ||
1318 | |||
1319 | scanline += bytewidth; | ||
1320 | precon += bytewidth; | ||
1321 | |||
1322 | while ((length - bytewidth)>> 8) /* length/256 */ | ||
1323 | { | ||
1324 | memcpy(cache_1, scanline, 256); | ||
1325 | memcpy(p_cache_1, precon, 256); | ||
1326 | |||
1327 | for (i=bytewidth; i < 256 + bytewidth; i++) | ||
1328 | cache[i] += paethPredictor(cache[i - bytewidth], | ||
1329 | p_cache[i], | ||
1330 | p_cache[i - bytewidth]); | ||
1331 | |||
1332 | memcpy(recon, cache, 256); | ||
1333 | |||
1334 | recon += 256; | ||
1335 | scanline += 256; | ||
1336 | precon += 256; | ||
1337 | length -= 256; | ||
1338 | |||
1339 | memcpy(cache, cache + 256, bytewidth * sizeof(unsigned char)); | ||
1340 | memcpy(p_cache, p_cache + 256, bytewidth * sizeof(unsigned char)); | ||
1341 | } | ||
1342 | |||
1343 | /* less than our cache size */ | ||
1344 | if (length) | ||
1345 | { | ||
1346 | /* cache last part of the scanline */ | ||
1347 | memcpy(cache_1, scanline, length - bytewidth); | ||
1348 | memcpy(p_cache_1, precon, length - bytewidth); | ||
1349 | |||
1350 | /* filtering */ | ||
1351 | for (i=bytewidth; i < length; i++) | ||
1352 | cache[i] += paethPredictor(cache[i - bytewidth], | ||
1353 | p_cache[i], | ||
1354 | p_cache[i - bytewidth]); | ||
1355 | |||
1356 | /* copy remaining part of the filtered scanline */ | ||
1357 | memcpy(recon, cache, length * sizeof(unsigned char)); | ||
1358 | } | ||
1359 | } | ||
1360 | else | ||
1361 | { | ||
1362 | /* | ||
1363 | for(i = 0; i < bytewidth; i++) recon[i] = scanline[i]; | ||
1364 | for (i = bytewidth; i < length; i++) | ||
1365 | recon[i] = (uint8_t)(scanline[i] + | ||
1366 | paethPredictor(recon[i - bytewidth], | ||
1367 | 0, 0)); | ||
1368 | */ | ||
1369 | |||
1370 | memcpy(cache, scanline, bytewidth * sizeof(unsigned char)); | ||
1371 | scanline += bytewidth; | ||
1372 | |||
1373 | while ((length - bytewidth) >> 9) /* length/512 */ | ||
1374 | { | ||
1375 | /* cache part of scanline */ | ||
1376 | memcpy(cache_1, scanline, 512); | ||
1377 | |||
1378 | /* filtering */ | ||
1379 | for (i=bytewidth; i < 512 + bytewidth; i++) | ||
1380 | cache[i] += paethPredictor(cache[i - bytewidth], 0, 0); | ||
1381 | |||
1382 | /* copy part of filtered scanline */ | ||
1383 | memcpy(recon, cache, 512); | ||
1384 | |||
1385 | /* adjust pointers */ | ||
1386 | recon += 512; | ||
1387 | scanline += 512; | ||
1388 | length -= 512; | ||
1389 | |||
1390 | /* copy last pixel back to the begining of the cache */ | ||
1391 | memcpy(cache, cache + 512, bytewidth * sizeof(unsigned char)); | ||
1392 | } | ||
1393 | |||
1394 | /* less than our cache size */ | ||
1395 | if (length) | ||
1396 | { | ||
1397 | /* cache last part of the scanline */ | ||
1398 | memcpy(cache_1, scanline, length - bytewidth); | ||
1399 | |||
1400 | /* filtering */ | ||
1401 | for (i=bytewidth; i < length; i++) | ||
1402 | cache[i] += paethPredictor(cache[i - bytewidth], 0, 0); | ||
1403 | |||
1404 | /* copy remaining part of the filtered scanline */ | ||
1405 | memcpy(recon, cache, length * sizeof(unsigned char)); | ||
1406 | } | ||
1407 | } | ||
1408 | break; | ||
1409 | default: | ||
1410 | return 36; /*error: unexisting filter type given*/ | ||
1411 | } | ||
1412 | return 0; | ||
1413 | } | ||
1414 | |||
1415 | static uint8_t unfilter(uint8_t* out, | ||
1416 | const uint8_t* in, | ||
1417 | uint16_t w, | ||
1418 | uint16_t h, | ||
1419 | uint8_t bpp) | ||
1420 | { | ||
1421 | /* For PNG filter method 0 | ||
1422 | * this function unfilters a single image (e.g. without interlacing this is | ||
1423 | * called once, with Adam7 it's called 7 times) | ||
1424 | * | ||
1425 | * out must have enough bytes allocated already, in must have the | ||
1426 | * scanlines + 1 filtertype byte per scanline | ||
1427 | * | ||
1428 | * w and h are image dimensions or dimensions of reduced image, | ||
1429 | * bpp is bits per pixel | ||
1430 | * | ||
1431 | * in and out are allowed to be the same memory address! | ||
1432 | */ | ||
1433 | |||
1434 | uint16_t y; | ||
1435 | uint8_t* prevline = 0; | ||
1436 | |||
1437 | /* bytewidth is used for filtering, is 1 when bpp < 8, | ||
1438 | * number of bytes per pixel otherwise | ||
1439 | */ | ||
1440 | size_t bytewidth = (bpp + 7) / 8; | ||
1441 | size_t linebytes = (w * bpp + 7) / 8; | ||
1442 | |||
1443 | for (y = 0; y < h; y++) | ||
1444 | { | ||
1445 | size_t outindex = linebytes * y; | ||
1446 | |||
1447 | /* the extra filterbyte added to each row */ | ||
1448 | size_t inindex = (1 + linebytes) * y; | ||
1449 | uint8_t filterType = in[inindex]; | ||
1450 | |||
1451 | uint8_t error = unfilterScanline(&out[outindex], &in[inindex + 1], | ||
1452 | prevline, bytewidth, filterType, | ||
1453 | linebytes); | ||
1454 | if (error) | ||
1455 | return error; | ||
1456 | |||
1457 | prevline = &out[outindex]; | ||
1458 | } | ||
1459 | |||
1460 | return 0; | ||
1461 | } | ||
1462 | |||
1463 | static void Adam7_deinterlace(uint8_t* out, | ||
1464 | const uint8_t* in, | ||
1465 | uint16_t w, | ||
1466 | uint16_t h, | ||
1467 | uint8_t bpp) | ||
1468 | { | ||
1469 | /* Note: this function works on image buffers WITHOUT padding bits at end | ||
1470 | * of scanlines with non-multiple-of-8 bit amounts, only between reduced | ||
1471 | * images is padding | ||
1472 | * out must be big enough AND must be 0 everywhere if bpp < 8 | ||
1473 | * in the current implementation (because that's likely a little bit faster) | ||
1474 | */ | ||
1475 | uint16_t passw[7], passh[7]; | ||
1476 | size_t filter_passstart[8], padded_passstart[8], passstart[8]; | ||
1477 | uint8_t i; | ||
1478 | |||
1479 | Adam7_getpassvalues(passw, passh, filter_passstart, padded_passstart, | ||
1480 | passstart, w, h, bpp); | ||
1481 | |||
1482 | if (bpp >= 8) | ||
1483 | { | ||
1484 | for (i = 0; i < 7; i++) | ||
1485 | { | ||
1486 | uint16_t x, y, b; | ||
1487 | size_t bytewidth = bpp >> 3; | ||
1488 | for (y = 0; y < passh[i]; y++) | ||
1489 | for (x = 0; x < passw[i]; x++) | ||
1490 | { | ||
1491 | size_t pixelinstart = passstart[i] + | ||
1492 | (y * passw[i] + x) * bytewidth; | ||
1493 | size_t pixeloutstart = ((ADAM7_IY[i] + y * ADAM7_DY[i]) * w + | ||
1494 | ADAM7_IX[i] + x * ADAM7_DX[i]) * bytewidth; | ||
1495 | for (b = 0; b < bytewidth; b++) | ||
1496 | { | ||
1497 | out[pixeloutstart + b] = in[pixelinstart + b]; | ||
1498 | } | ||
1499 | } | ||
1500 | } | ||
1501 | } | ||
1502 | else /*bpp < 8: Adam7 with pixels < 8 bit is a bit trickier: with bit pointers*/ | ||
1503 | { | ||
1504 | for (i = 0; i < 7; i++) | ||
1505 | { | ||
1506 | uint16_t x, y, b; | ||
1507 | uint32_t ilinebits = bpp * passw[i]; | ||
1508 | uint32_t olinebits = bpp * w; | ||
1509 | size_t obp, ibp; /*bit pointers (for out and in buffer)*/ | ||
1510 | for (y = 0; y < passh[i]; y++) | ||
1511 | for (x = 0; x < passw[i]; x++) | ||
1512 | { | ||
1513 | ibp = (8 * passstart[i]) + (y * ilinebits + x * bpp); | ||
1514 | obp = (ADAM7_IY[i] + y * ADAM7_DY[i]) * olinebits + | ||
1515 | (ADAM7_IX[i] + x * ADAM7_DX[i]) * bpp; | ||
1516 | for (b = 0; b < bpp; b++) | ||
1517 | { | ||
1518 | uint8_t bit = readBitFromReversedStream(&ibp, in); | ||
1519 | /* note that this function assumes the out buffer | ||
1520 | * is completely 0, use setBitOfReversedStream | ||
1521 | * otherwise*/ | ||
1522 | setBitOfReversedStream0(&obp, out, bit); | ||
1523 | } | ||
1524 | } | ||
1525 | } | ||
1526 | } | ||
1527 | } | ||
1528 | |||
1529 | static void removePaddingBits(uint8_t* out, | ||
1530 | const uint8_t* in, | ||
1531 | size_t olinebits, | ||
1532 | size_t ilinebits, | ||
1533 | uint16_t h) | ||
1534 | { | ||
1535 | /* After filtering there are still padding bits if scanlines have | ||
1536 | * non multiple of 8 bit amounts. They need to be removed | ||
1537 | * (except at last scanline of (Adam7-reduced) image) before working | ||
1538 | * with pure image buffers for the Adam7 code, the color convert code | ||
1539 | * and the output to the user. | ||
1540 | * | ||
1541 | * in and out are allowed to be the same buffer, in may also be higher | ||
1542 | * but still overlapping; in must have >= ilinebits*h bits, | ||
1543 | * out must have >= olinebits*h bits, olinebits must be <= ilinebits | ||
1544 | * also used to move bits after earlier such operations happened, e.g. | ||
1545 | * in a sequence of reduced images from Adam7 | ||
1546 | * only useful if (ilinebits - olinebits) is a value in the range 1..7 | ||
1547 | */ | ||
1548 | uint16_t y; | ||
1549 | size_t diff = ilinebits - olinebits; | ||
1550 | size_t obp = 0, ibp = 0; /*bit pointers*/ | ||
1551 | for (y = 0; y < h; y++) | ||
1552 | { | ||
1553 | size_t x; | ||
1554 | for (x = 0; x < olinebits; x++) | ||
1555 | { | ||
1556 | uint8_t bit = readBitFromReversedStream(&ibp, in); | ||
1557 | setBitOfReversedStream(&obp, out, bit); | ||
1558 | } | ||
1559 | ibp += diff; | ||
1560 | } | ||
1561 | } | ||
1562 | |||
1563 | /* out must be buffer big enough to contain full image, | ||
1564 | * and in must contain the full decompressed data from the IDAT chunks | ||
1565 | */ | ||
1566 | static uint8_t postProcessScanlines(uint8_t* out, | ||
1567 | uint8_t* in, | ||
1568 | const LodePNG_Decoder* decoder) | ||
1569 | { | ||
1570 | /*return value is error*/ | ||
1571 | |||
1572 | /* This function converts the filtered-padded-interlaced data into pure 2D | ||
1573 | * image buffer with the PNG's colortype. | ||
1574 | * Steps: | ||
1575 | * I) if no Adam7: | ||
1576 | * 1) unfilter | ||
1577 | * 2) remove padding bits (= posible extra bits per scanline if bpp < 8) | ||
1578 | * II) if adam7: | ||
1579 | * 1) 7x unfilter | ||
1580 | * 2) 7x remove padding bits | ||
1581 | * 3) Adam7_deinterlace | ||
1582 | * | ||
1583 | * NOTE: the in buffer will be overwritten with intermediate data! | ||
1584 | */ | ||
1585 | uint8_t bpp = getBpp(decoder->infoPng.color.colorType, | ||
1586 | decoder->infoPng.color.bitDepth); | ||
1587 | uint16_t w = decoder->infoPng.width; | ||
1588 | uint16_t h = decoder->infoPng.height; | ||
1589 | uint8_t error = 0; | ||
1590 | |||
1591 | if (bpp == 0) | ||
1592 | return 31; /*error: invalid colortype*/ | ||
1593 | |||
1594 | if (decoder->infoPng.interlaceMethod == 0) | ||
1595 | { | ||
1596 | if (bpp < 8 && w * bpp != ((w * bpp + 7) / 8) * 8) | ||
1597 | { | ||
1598 | error = unfilter(in, in, w, h, bpp); | ||
1599 | if (error) return error; | ||
1600 | removePaddingBits(out, in, w * bpp, ((w * bpp + 7) / 8) * 8, h); | ||
1601 | } | ||
1602 | else | ||
1603 | /* we can immediatly filter into the out buffer, | ||
1604 | * no other steps needed | ||
1605 | */ | ||
1606 | error = unfilter(out, in, w, h, bpp); | ||
1607 | } | ||
1608 | else /*interlaceMethod is 1 (Adam7)*/ | ||
1609 | { | ||
1610 | uint16_t passw[7], passh[7]; | ||
1611 | size_t filter_passstart[8], padded_passstart[8], passstart[8]; | ||
1612 | uint8_t i; | ||
1613 | |||
1614 | Adam7_getpassvalues(passw, | ||
1615 | passh, | ||
1616 | filter_passstart, | ||
1617 | padded_passstart, | ||
1618 | passstart, | ||
1619 | w, | ||
1620 | h, | ||
1621 | bpp); | ||
1622 | |||
1623 | for (i = 0; i < 7; i++) | ||
1624 | { | ||
1625 | error = unfilter(&in[padded_passstart[i]], | ||
1626 | &in[filter_passstart[i]], | ||
1627 | passw[i], | ||
1628 | passh[i], | ||
1629 | bpp); | ||
1630 | if (error) | ||
1631 | return error; | ||
1632 | if (bpp < 8) | ||
1633 | /* TODO: possible efficiency improvement: if in this reduced | ||
1634 | * image the bits fit nicely in 1 scanline, move bytes instead | ||
1635 | * of bits or move not at all | ||
1636 | */ | ||
1637 | { | ||
1638 | /* remove padding bits in scanlines; after this there still | ||
1639 | * may be padding bits between the different reduced images: | ||
1640 | * each reduced image still starts nicely at a byte | ||
1641 | */ | ||
1642 | removePaddingBits(&in[passstart[i]], | ||
1643 | &in[padded_passstart[i]], | ||
1644 | passw[i] * bpp, | ||
1645 | ((passw[i] * bpp + 7) / 8) * 8, | ||
1646 | passh[i]); | ||
1647 | } | ||
1648 | } | ||
1649 | |||
1650 | Adam7_deinterlace(out, in, w, h, bpp); | ||
1651 | } | ||
1652 | |||
1653 | return error; | ||
1654 | } | ||
1655 | |||
1656 | /* read a PNG, the result will be in the same color type as the PNG | ||
1657 | * (hence "generic") | ||
1658 | */ | ||
1659 | static void decodeGeneric(LodePNG_Decoder* decoder) | ||
1660 | { | ||
1661 | uint8_t *in = decoder->file; | ||
1662 | |||
1663 | uint8_t IEND = 0; | ||
1664 | const uint8_t* chunk; | ||
1665 | size_t i; | ||
1666 | |||
1667 | size_t chunkLength; /* chunk length */ | ||
1668 | const uint8_t* data; /*the data in the chunk*/ | ||
1669 | |||
1670 | uint8_t *idat = decoder->buf; /* allocated buffer */ | ||
1671 | |||
1672 | size_t idat_size = 0; | ||
1673 | |||
1674 | signed long free_mem = decoder->buf_size; | ||
1675 | |||
1676 | /* for unknown chunk order */ | ||
1677 | bool unknown = false; | ||
1678 | uint8_t critical_pos = 1; /*1 = after IHDR, 2 = after PLTE, 3 = after IDAT*/ | ||
1679 | |||
1680 | if (decoder->file_size == 0 || in == NULL) | ||
1681 | { | ||
1682 | /* the given data is empty */ | ||
1683 | decoder->error = 48; | ||
1684 | return; | ||
1685 | } | ||
1686 | |||
1687 | chunk = in + 33; /*first byte of the first chunk after the header*/ | ||
1688 | |||
1689 | /* loop through the chunks, ignoring unknown chunks and stopping at IEND | ||
1690 | * chunk. IDAT data is put at the start of the in buffer | ||
1691 | */ | ||
1692 | while (!IEND) { | ||
1693 | |||
1694 | /* minimal size of chunk is 12 bytes */ | ||
1695 | if ((size_t)((chunk - in) + 12) > decoder->file_size || chunk < in) | ||
1696 | { | ||
1697 | /* error: size of the in buffer too small to contain next chunk */ | ||
1698 | decoder->error = 30; | ||
1699 | break; | ||
1700 | } | ||
1701 | |||
1702 | /* length of the data of the chunk, excluding the length bytes, | ||
1703 | * chunk type and CRC bytes | ||
1704 | * | ||
1705 | * data field of the chunk is restricted to 2^31-1 bytes in size | ||
1706 | */ | ||
1707 | chunkLength = LodePNG_chunk_length(chunk); | ||
1708 | |||
1709 | if (chunkLength > 2147483647) | ||
1710 | { | ||
1711 | decoder->error = 63; | ||
1712 | break; | ||
1713 | } | ||
1714 | |||
1715 | /* check if chunk fits in buffer */ | ||
1716 | if ((size_t)((chunk - in) + chunkLength + 12) > decoder->file_size || | ||
1717 | (chunk + chunkLength + 12) < in) | ||
1718 | { | ||
1719 | /* error: size of the in buffer too small to contain next chunk */ | ||
1720 | decoder->error = 35; | ||
1721 | break; | ||
1722 | } | ||
1723 | data = LodePNG_chunk_data(chunk); | ||
1724 | |||
1725 | /* IDAT chunk, containing compressed image data | ||
1726 | * there may be more than 1 IDAT chunk, complete | ||
1727 | * compressed stream is concatenation of consecutive | ||
1728 | * chunks data | ||
1729 | */ | ||
1730 | if (LodePNG_chunk_type_equals(chunk, PNG_CHUNK_IDAT)) | ||
1731 | { | ||
1732 | free_mem -= chunkLength; | ||
1733 | |||
1734 | if (free_mem < 0) | ||
1735 | { | ||
1736 | decoder->error = OUT_OF_MEMORY; | ||
1737 | break; | ||
1738 | } | ||
1739 | /* copy compressed data */ | ||
1740 | memcpy(idat+idat_size, data, chunkLength * sizeof(uint8_t)); | ||
1741 | idat_size += chunkLength; | ||
1742 | critical_pos = 3; | ||
1743 | } | ||
1744 | /*IEND chunk*/ | ||
1745 | else if (LodePNG_chunk_type_equals(chunk, PNG_CHUNK_IEND)) | ||
1746 | { | ||
1747 | IEND = 1; | ||
1748 | } | ||
1749 | /*palette chunk (PLTE)*/ | ||
1750 | else if (LodePNG_chunk_type_equals(chunk, PNG_CHUNK_PLTE)) | ||
1751 | { | ||
1752 | uint32_t pos = 0; | ||
1753 | decoder->infoPng.color.palettesize = chunkLength / 3; | ||
1754 | if (decoder->infoPng.color.palettesize > 256) | ||
1755 | { | ||
1756 | /*error: palette too big*/ | ||
1757 | decoder->error = 38; | ||
1758 | break; | ||
1759 | } | ||
1760 | |||
1761 | for (i = 0; i < decoder->infoPng.color.palettesize; i++) | ||
1762 | { | ||
1763 | decoder->infoPng.color.palette[(i<<2)] = data[pos++]; /*R*/ | ||
1764 | decoder->infoPng.color.palette[(i<<2) | 1] = data[pos++]; /*G*/ | ||
1765 | decoder->infoPng.color.palette[(i<<2) | 2] = data[pos++]; /*B*/ | ||
1766 | decoder->infoPng.color.palette[(i<<2) | 3] = 255; /*alpha*/ | ||
1767 | } | ||
1768 | critical_pos = 2; | ||
1769 | } | ||
1770 | /*palette transparency chunk (tRNS)*/ | ||
1771 | else if (LodePNG_chunk_type_equals(chunk, PNG_CHUNK_tRNS)) | ||
1772 | { | ||
1773 | if (decoder->infoPng.color.colorType == PNG_COLORTYPE_PALETTE) | ||
1774 | { | ||
1775 | if (chunkLength > decoder->infoPng.color.palettesize) | ||
1776 | { | ||
1777 | /* error: more alpha values given than there are palette | ||
1778 | * entries | ||
1779 | */ | ||
1780 | decoder->error = 39; | ||
1781 | break; | ||
1782 | } | ||
1783 | for (i = 0; i < chunkLength; i++) | ||
1784 | /* copy alpha informations for palette colors */ | ||
1785 | decoder->infoPng.color.palette[(i<<2) | 3] = data[i]; | ||
1786 | } | ||
1787 | else if (decoder->infoPng.color.colorType == PNG_COLORTYPE_GREY) | ||
1788 | { | ||
1789 | if (chunkLength != 2) | ||
1790 | { | ||
1791 | /* error: this chunk must be 2 bytes for greyscale image */ | ||
1792 | decoder->error = 40; | ||
1793 | break; | ||
1794 | } | ||
1795 | /* transparent color definition */ | ||
1796 | decoder->infoPng.color.key_defined = 1; | ||
1797 | decoder->infoPng.color.key_r = | ||
1798 | decoder->infoPng.color.key_g = | ||
1799 | decoder->infoPng.color.key_b = data[0]<<8|data[1]; | ||
1800 | } | ||
1801 | else if (decoder->infoPng.color.colorType == PNG_COLORTYPE_RGB) | ||
1802 | { | ||
1803 | if (chunkLength != 6) | ||
1804 | { | ||
1805 | /* error: this chunk must be 6 bytes for RGB image */ | ||
1806 | decoder->error = 41; | ||
1807 | break; | ||
1808 | } | ||
1809 | /* transparent color definition */ | ||
1810 | decoder->infoPng.color.key_defined = 1; | ||
1811 | decoder->infoPng.color.key_r = data[0]<<8|data[1]; | ||
1812 | decoder->infoPng.color.key_g = data[2]<<8|data[3]; | ||
1813 | decoder->infoPng.color.key_b = data[4]<<8|data[5]; | ||
1814 | } | ||
1815 | else | ||
1816 | { | ||
1817 | /* error: tRNS chunk not allowed for other color models */ | ||
1818 | decoder->error = 42; | ||
1819 | break; | ||
1820 | } | ||
1821 | } | ||
1822 | /*background color chunk (bKGD)*/ | ||
1823 | else if (LodePNG_chunk_type_equals(chunk, PNG_CHUNK_bKGD)) | ||
1824 | { | ||
1825 | if (decoder->infoPng.color.colorType == PNG_COLORTYPE_PALETTE) | ||
1826 | { | ||
1827 | if (chunkLength != 1) | ||
1828 | { | ||
1829 | /* error: this chunk must be 1 byte for indexed color image */ | ||
1830 | decoder->error = 43; | ||
1831 | break; | ||
1832 | } | ||
1833 | decoder->infoPng.background_r = | ||
1834 | decoder->infoPng.color.palette[(data[0]<<2)]; | ||
1835 | |||
1836 | decoder->infoPng.background_g = | ||
1837 | decoder->infoPng.color.palette[(data[0]<<2) | 1]; | ||
1838 | |||
1839 | decoder->infoPng.background_b = | ||
1840 | decoder->infoPng.color.palette[(data[0]<<2) | 2]; | ||
1841 | |||
1842 | } | ||
1843 | else if (decoder->infoPng.color.colorType == PNG_COLORTYPE_GREY || | ||
1844 | decoder->infoPng.color.colorType == PNG_COLORTYPE_GREYA) | ||
1845 | { | ||
1846 | if (chunkLength != 2) | ||
1847 | { | ||
1848 | /* error: this chunk must be 2 bytes for greyscale image */ | ||
1849 | decoder->error = 44; | ||
1850 | break; | ||
1851 | } | ||
1852 | decoder->infoPng.background_r = | ||
1853 | decoder->infoPng.background_g = | ||
1854 | decoder->infoPng.background_b = data[0]; | ||
1855 | } | ||
1856 | else if (decoder->infoPng.color.colorType == PNG_COLORTYPE_RGB || | ||
1857 | decoder->infoPng.color.colorType == PNG_COLORTYPE_RGBA) | ||
1858 | { | ||
1859 | if (chunkLength != 6) | ||
1860 | { | ||
1861 | /* error: this chunk must be 6 bytes for greyscale image */ | ||
1862 | decoder->error = 45; | ||
1863 | break; | ||
1864 | } | ||
1865 | decoder->infoPng.background_r = data[0]; | ||
1866 | decoder->infoPng.background_g = data[2]; | ||
1867 | decoder->infoPng.background_b = data[4]; | ||
1868 | } | ||
1869 | } | ||
1870 | else | ||
1871 | { | ||
1872 | /* it's not an implemented chunk type, | ||
1873 | * so ignore it (unless it is critical) | ||
1874 | * skip over the data | ||
1875 | */ | ||
1876 | if (LodePNG_chunk_critical(chunk)) | ||
1877 | { | ||
1878 | /* error: unknown critical chunk | ||
1879 | * (5th bit of first byte of chunk type is 0) | ||
1880 | */ | ||
1881 | decoder->error = 69; | ||
1882 | break; | ||
1883 | } | ||
1884 | unknown = true; | ||
1885 | } | ||
1886 | |||
1887 | if (!unknown) /*check CRC if wanted, only on known chunk types*/ | ||
1888 | { | ||
1889 | if (!LodePNG_chunk_check_crc(chunk)) | ||
1890 | { | ||
1891 | decoder->error = 57; | ||
1892 | break; | ||
1893 | } | ||
1894 | } | ||
1895 | |||
1896 | if (!IEND) | ||
1897 | chunk = LodePNG_chunk_next(chunk); | ||
1898 | } | ||
1899 | |||
1900 | if (!decoder->error) | ||
1901 | { | ||
1902 | /* ptr to buffer just after concatenated IDATs */ | ||
1903 | uint8_t *scanlines = idat + idat_size; | ||
1904 | size_t scanline_size = free_mem; | ||
1905 | |||
1906 | /* decompress with the Zlib decompressor | ||
1907 | * decompressor updates scanlines_size to actual size | ||
1908 | * of decompressed data | ||
1909 | */ | ||
1910 | decoder->error = LodePNG_decompress(scanlines, | ||
1911 | &scanline_size, | ||
1912 | idat, | ||
1913 | idat_size); | ||
1914 | |||
1915 | free_mem -= scanline_size; | ||
1916 | /* possible memory saving (at cost of memcpy) | ||
1917 | * memcpy(decoder->buf - scanlines_size, | ||
1918 | * scanlines, | ||
1919 | * scanlines_size * sizeof(uint8_t)); | ||
1920 | * this will free compressed IDATs and | ||
1921 | * will trash raw PNG file (it is trashed anyway | ||
1922 | */ | ||
1923 | if (!decoder->error) | ||
1924 | { | ||
1925 | /* size of decoded image in bytes rounded up */ | ||
1926 | size_t decoded_img_size = (decoder->infoPng.height * | ||
1927 | decoder->infoPng.width * | ||
1928 | getBpp(decoder->infoPng.color.colorType, | ||
1929 | decoder->infoPng.color.bitDepth) + | ||
1930 | 7) / 8; | ||
1931 | |||
1932 | /* at this time buffer contains: | ||
1933 | * compressed IDATs | ||
1934 | * decompressed IDATs | ||
1935 | * png raw file at the end of the buffer (not needed any more ) | ||
1936 | */ | ||
1937 | free_mem -= decoded_img_size; | ||
1938 | |||
1939 | if (free_mem < 0) | ||
1940 | { | ||
1941 | decoder->error = OUT_OF_MEMORY; | ||
1942 | return; | ||
1943 | } | ||
1944 | |||
1945 | /* ptr to decoded png image | ||
1946 | * this will overwrite raw png file loaded into memory | ||
1947 | * decoded image is put in the end of allocated buffer | ||
1948 | */ | ||
1949 | decoder->decoded_img = decoder->buf + | ||
1950 | decoder->buf_size - | ||
1951 | decoded_img_size; | ||
1952 | |||
1953 | /* clear memory as filters assume 0'ed memory */ | ||
1954 | memset(decoder->decoded_img,0,decoded_img_size*sizeof(uint8_t)); | ||
1955 | |||
1956 | decoder->error = postProcessScanlines(decoder->decoded_img, | ||
1957 | scanlines, | ||
1958 | decoder); | ||
1959 | } | ||
1960 | } | ||
1961 | } | ||
1962 | |||
1963 | /* Public functions */ | ||
1964 | |||
1965 | /* read the information from the header and store it in the decoder | ||
1966 | * context struct | ||
1967 | * value is error | ||
1968 | */ | ||
1969 | void LodePNG_inspect(LodePNG_Decoder* decoder, uint8_t *in, size_t inlength) | ||
1970 | { | ||
1971 | uint32_t header_crc, checksum; | ||
1972 | if (inlength == 0 || in == NULL) | ||
1973 | { | ||
1974 | /* the given data is empty */ | ||
1975 | decoder->error = 48; | ||
1976 | return; | ||
1977 | } | ||
1978 | |||
1979 | if (inlength < 29) | ||
1980 | { | ||
1981 | /*error: the data length is smaller than the length of the header*/ | ||
1982 | decoder->error = 27; | ||
1983 | return; | ||
1984 | } | ||
1985 | |||
1986 | /* when decoding a new PNG image, make sure all parameters created after | ||
1987 | * previous decoding are reset | ||
1988 | */ | ||
1989 | LodePNG_InfoPng_cleanup(&decoder->infoPng); | ||
1990 | LodePNG_InfoPng_init(&decoder->infoPng); | ||
1991 | decoder->error = 0; | ||
1992 | |||
1993 | decoder->file = in; | ||
1994 | decoder->file_size = inlength; | ||
1995 | |||
1996 | if (in[0] != 137 || in[1] != 80 || in[2] != 78 || in[3] != 71 || | ||
1997 | in[4] != 13 || in[5] != 10 || in[6] != 26 || in[7] != 10) | ||
1998 | { | ||
1999 | /* error: the first 8 bytes are not the correct PNG signature */ | ||
2000 | decoder->error = 28; | ||
2001 | return; | ||
2002 | } | ||
2003 | |||
2004 | if (in[12] != 'I' || in[13] != 'H' || in[14] != 'D' || in[15] != 'R') | ||
2005 | { | ||
2006 | /* error: it doesn't start with a IHDR chunk! */ | ||
2007 | decoder->error = 29; | ||
2008 | return; | ||
2009 | } | ||
2010 | |||
2011 | /* read the values given in the header */ | ||
2012 | decoder->infoPng.width = in[16]<<24|in[17]<<16|in[18]<<8|in[19]; | ||
2013 | decoder->infoPng.height = in[20]<<24|in[21]<<16|in[22]<<8|in[23]; | ||
2014 | decoder->infoPng.color.bitDepth = in[24]; | ||
2015 | decoder->infoPng.color.colorType = in[25]; | ||
2016 | decoder->infoPng.compressionMethod = in[26]; | ||
2017 | decoder->infoPng.filterMethod = in[27]; | ||
2018 | decoder->infoPng.interlaceMethod = in[28]; | ||
2019 | |||
2020 | /* get the value from the chunk's crc field */ | ||
2021 | header_crc = in[29]<<24|in[30]<<16|in[31]<<8|in[32]; | ||
2022 | |||
2023 | /* calculate crc of the header chunk */ | ||
2024 | checksum = tinf_crc32(in + 12, 17); | ||
2025 | |||
2026 | if (header_crc != checksum) | ||
2027 | { | ||
2028 | decoder->error = 57; | ||
2029 | return; | ||
2030 | } | ||
2031 | |||
2032 | if (decoder->infoPng.compressionMethod != 0) | ||
2033 | { | ||
2034 | /* error: only compression method 0 is allowed in the specification */ | ||
2035 | decoder->error = 32; | ||
2036 | return; | ||
2037 | } | ||
2038 | |||
2039 | if (decoder->infoPng.filterMethod != 0) | ||
2040 | { | ||
2041 | /* error: only filter method 0 is allowed in the specification */ | ||
2042 | decoder->error = 33; | ||
2043 | return; | ||
2044 | } | ||
2045 | |||
2046 | if (decoder->infoPng.interlaceMethod > 1) | ||
2047 | { | ||
2048 | /* error: only interlace methods 0 and 1 exist in the specification */ | ||
2049 | decoder->error = 34; | ||
2050 | return; | ||
2051 | } | ||
2052 | |||
2053 | /* check validity of colortype and bitdepth combination */ | ||
2054 | decoder->error = checkColorValidity(decoder->infoPng.color.colorType, | ||
2055 | decoder->infoPng.color.bitDepth); | ||
2056 | } | ||
2057 | |||
2058 | void LodePNG_decode(LodePNG_Decoder* decoder, | ||
2059 | uint8_t* in, | ||
2060 | size_t insize, | ||
2061 | void (*pf_progress)(int current, int total)) | ||
2062 | { | ||
2063 | size_t line_buf_size; | ||
2064 | /* parse header */ | ||
2065 | LodePNG_inspect(decoder, in, insize); | ||
2066 | |||
2067 | |||
2068 | /* Check memory available against worst case where | ||
2069 | * we have to have decoded PNG image | ||
2070 | * and converted to the native pixel format image | ||
2071 | * in buffer at the same time (do we realy need that much?) | ||
2072 | */ | ||
2073 | |||
2074 | size_t decoded_img_size = (decoder->infoPng.height * | ||
2075 | decoder->infoPng.width * | ||
2076 | getBpp(decoder->infoPng.color.colorType, | ||
2077 | decoder->infoPng.color.bitDepth) + | ||
2078 | 7) / 8; | ||
2079 | |||
2080 | /* one line more as temp buffer for conversion */ | ||
2081 | #ifdef HAVE_LCD_COLOR | ||
2082 | decoder->native_img_size = decoder->infoPng.width * | ||
2083 | (decoder->infoPng.height)*FB_DATA_SZ; | ||
2084 | line_buf_size = decoder->infoPng.width * sizeof(struct uint8_rgb); | ||
2085 | #else | ||
2086 | decoder->native_img_size = decoder->infoPng.width * | ||
2087 | decoder->infoPng.height; | ||
2088 | line_buf_size = decoder->infoPng.width; | ||
2089 | #endif | ||
2090 | |||
2091 | if (decoded_img_size + decoder->native_img_size + line_buf_size | ||
2092 | > decoder->buf_size) | ||
2093 | { | ||
2094 | decoder->error = OUT_OF_MEMORY; | ||
2095 | return; | ||
2096 | } | ||
2097 | |||
2098 | if (pf_progress != NULL) | ||
2099 | pf_progress(0, 100); | ||
2100 | |||
2101 | long time = *rb->current_tick; | ||
2102 | /* put decoded png data (pure 2D array of pixels in format | ||
2103 | * defined by PNG header at the end of the allocated buffer | ||
2104 | */ | ||
2105 | decodeGeneric(decoder); | ||
2106 | if (decoder->error) return; | ||
2107 | |||
2108 | if (pf_progress != NULL) | ||
2109 | pf_progress(50, 100); | ||
2110 | |||
2111 | /* convert decoded png data into native rockbox | ||
2112 | * pixel format (native LCD data for color | ||
2113 | * or greylib pixel format for greyscale) | ||
2114 | * | ||
2115 | * converted image will be put at the begining | ||
2116 | * of the allocated buffer | ||
2117 | */ | ||
2118 | LodePNG_convert(decoder); | ||
2119 | |||
2120 | /* correct aspect ratio */ | ||
2121 | #if (LCD_PIXEL_ASPECT_HEIGHT != 1 || LCD_PIXEL_ASPECT_WIDTH != 1) | ||
2122 | struct bitmap img_src, img_dst; /* scaler vars */ | ||
2123 | struct dim dim_src, dim_dst; /* recalc_dimensions vars */ | ||
2124 | unsigned int c_native_img_size; /* size of the image after correction */ | ||
2125 | |||
2126 | dim_src.width = decoder->infoPng.width; | ||
2127 | dim_src.height = decoder->infoPng.height; | ||
2128 | |||
2129 | dim_dst.width = decoder->infoPng.width; | ||
2130 | dim_dst.height = decoder->infoPng.height; | ||
2131 | |||
2132 | /* defined in apps/recorder/resize.c */ | ||
2133 | if (!recalc_dimension(&dim_dst, &dim_src)) | ||
2134 | { | ||
2135 | /* calculate 'corrected' image size */ | ||
2136 | #ifdef HAVE_LCD_COLOR | ||
2137 | c_native_img_size = dim_dst.width * | ||
2138 | (dim_dst.height)*FB_DATA_SZ; | ||
2139 | #else | ||
2140 | c_native_img_size = dim_dst.width * | ||
2141 | dim_dst.height; | ||
2142 | #endif | ||
2143 | /* check memory constraints | ||
2144 | * do the correction only if there is enough | ||
2145 | * free memory | ||
2146 | */ | ||
2147 | if ( decoder->native_img_size + c_native_img_size <= | ||
2148 | decoder->buf_size ) | ||
2149 | { | ||
2150 | img_src.width = dim_src.width; | ||
2151 | img_src.height = dim_src.height; | ||
2152 | img_src.data = (unsigned char *)decoder->buf; | ||
2153 | |||
2154 | img_dst.width = dim_dst.width; | ||
2155 | img_dst.height = dim_dst.height; | ||
2156 | img_dst.data = (unsigned char *)decoder->buf + | ||
2157 | decoder->native_img_size; | ||
2158 | |||
2159 | /* scale the bitmap to correct physical | ||
2160 | * pixel dimentions | ||
2161 | */ | ||
2162 | resize_bitmap(&img_src, &img_dst); | ||
2163 | |||
2164 | /* update decoder struct */ | ||
2165 | decoder->infoPng.width = img_dst.width; | ||
2166 | decoder->infoPng.height = img_dst.height; | ||
2167 | decoder->native_img_size = c_native_img_size; | ||
2168 | |||
2169 | /* copy back corrected image to the begining of the buffer */ | ||
2170 | memcpy(img_src.data, img_dst.data, decoder->native_img_size); | ||
2171 | } | ||
2172 | } | ||
2173 | |||
2174 | #endif /* (LCD_PIXEL_ASPECT_HEIGHT != 1 || LCD_PIXEL_ASPECT_WIDTH != 1) */ | ||
2175 | time = *rb->current_tick - time; | ||
2176 | if (pf_progress) pf_progress(100, 100); | ||
2177 | } | ||
2178 | |||
2179 | void LodePNG_Decoder_init(LodePNG_Decoder* decoder, | ||
2180 | uint8_t *buf, | ||
2181 | size_t buf_size) | ||
2182 | { | ||
2183 | LodePNG_InfoPng_init(&decoder->infoPng); | ||
2184 | decoder->error = 0; | ||
2185 | decoder->buf = buf; | ||
2186 | decoder->buf_size = buf_size; | ||
2187 | decoder->decoded_img = NULL; | ||
2188 | decoder->file = NULL; | ||
2189 | decoder->file_size = 0; | ||
2190 | } | ||
2191 | |||
2192 | const char* LodePNG_perror(LodePNG_Decoder *decoder) | ||
2193 | { | ||
2194 | return png_error_messages[decoder->error-PNG_ERROR_MIN]; | ||
2195 | } | ||
diff --git a/apps/plugins/imageviewer/png/png_decoder.h b/apps/plugins/imageviewer/png/png_decoder.h new file mode 100644 index 0000000000..4ca2a51277 --- /dev/null +++ b/apps/plugins/imageviewer/png/png_decoder.h | |||
@@ -0,0 +1,141 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$id $ | ||
9 | * | ||
10 | * Copyright (C) 2009 by Christophe Gouiran <bechris13250 -at- gmail -dot- com> | ||
11 | * | ||
12 | * Based on lodepng, a lightweight png decoder/encoder | ||
13 | * (c) 2005-2008 Lode Vandevenne | ||
14 | * | ||
15 | * Copyright (c) 2010 Marcin Bukat | ||
16 | * - pixel format conversion & transparency handling | ||
17 | * - adaptation of tinf (tiny inflate library) | ||
18 | * - code refactoring & cleanups | ||
19 | * | ||
20 | * This program is free software; you can redistribute it and/or | ||
21 | * modify it under the terms of the GNU General Public License | ||
22 | * as published by the Free Software Foundation; either version 2 | ||
23 | * of the License, or (at your option) any later version. | ||
24 | * | ||
25 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
26 | * KIND, either express or implied. | ||
27 | * | ||
28 | ****************************************************************************/ | ||
29 | |||
30 | #define OUT_OF_MEMORY 9900 | ||
31 | #define FILE_TOO_LARGE 9910 | ||
32 | |||
33 | /* PNG chunk types signatures */ | ||
34 | /* critical chunks */ | ||
35 | #define PNG_CHUNK_IHDR 0x49484452 | ||
36 | #define PNG_CHUNK_PLTE 0x504c5445 | ||
37 | #define PNG_CHUNK_IDAT 0x49444154 | ||
38 | #define PNG_CHUNK_IEND 0x49454e44 | ||
39 | |||
40 | /* ancillary chunks */ | ||
41 | #define PNG_CHUNK_bKGD 0x624b4744 | ||
42 | #define PNG_CHUNK_cHRM 0x6348524d | ||
43 | #define PNG_CHUNK_gAMA 0x67414d41 | ||
44 | #define PNG_CHUNK_hIST 0x68495354 | ||
45 | #define PNG_CHUNK_iCCP 0x69434350 | ||
46 | #define PNG_CHUNK_pHYs 0x70485973 | ||
47 | #define PNG_CHUNK_sBIT 0x73424954 | ||
48 | #define PNG_CHUNK_sPLT 0x73504c54 | ||
49 | #define PNG_CHUNK_sRGB 0x73524742 | ||
50 | #define PNG_CHUNK_tEXt 0x74455874 | ||
51 | #define PNG_CHUNK_tIME 0x74494d45 | ||
52 | #define PNG_CHUNK_tRNS 0x74524e53 | ||
53 | #define PNG_CHUNK_zTXt 0x7a545874 | ||
54 | |||
55 | /* PNG color types */ | ||
56 | #define PNG_COLORTYPE_GREY 0 | ||
57 | #define PNG_COLORTYPE_RGB 2 | ||
58 | #define PNG_COLORTYPE_PALETTE 3 | ||
59 | #define PNG_COLORTYPE_GREYA 4 | ||
60 | #define PNG_COLORTYPE_RGBA 6 | ||
61 | |||
62 | /* PNG filter types */ | ||
63 | #define PNG_FILTERTYPE_NONE 0 | ||
64 | #define PNG_FILTERTYPE_SUB 1 | ||
65 | #define PNG_FILTERTYPE_UP 2 | ||
66 | #define PNG_FILTERTYPE_AVERAGE 3 | ||
67 | #define PNG_FILTERTYPE_PAETH 4 | ||
68 | |||
69 | #define PNG_ERROR_MIN 27 | ||
70 | #define PNG_ERROR_MAX 74 | ||
71 | |||
72 | /* Typedefs */ | ||
73 | typedef struct LodePNG_InfoColor /*info about the color type of an image*/ | ||
74 | { | ||
75 | /*header (IHDR)*/ | ||
76 | unsigned colorType; /*color type*/ | ||
77 | unsigned bitDepth; /*bits per sample*/ | ||
78 | |||
79 | /*palette (PLTE)*/ | ||
80 | unsigned char palette[256 * 4]; /*palette in RGBARGBA... order*/ | ||
81 | size_t palettesize; /* palette size in number of colors | ||
82 | * (amount of bytes is 4 * palettesize) | ||
83 | */ | ||
84 | |||
85 | /*transparent color key (tRNS)*/ | ||
86 | unsigned key_defined; /*is a transparent color key given?*/ | ||
87 | unsigned key_r; /*red component of color key*/ | ||
88 | unsigned key_g; /*green component of color key*/ | ||
89 | unsigned key_b; /*blue component of color key*/ | ||
90 | } LodePNG_InfoColor; | ||
91 | |||
92 | typedef struct LodePNG_InfoPng /*information about the PNG image, except pixels and sometimes except width and height*/ | ||
93 | { | ||
94 | /*header (IHDR), palette (PLTE) and transparency (tRNS)*/ | ||
95 | unsigned width; /*width of the image in pixels - filled in by decoder)*/ | ||
96 | unsigned height; /*height of the image in pixels - filled in by decoder)*/ | ||
97 | unsigned compressionMethod; /*compression method of the original file*/ | ||
98 | unsigned filterMethod; /*filter method of the original file*/ | ||
99 | unsigned interlaceMethod; /*interlace method of the original file*/ | ||
100 | LodePNG_InfoColor color; /*color type and bits, palette, transparency*/ | ||
101 | |||
102 | /*suggested background color (bKGD)*/ | ||
103 | unsigned background_r; /*red component of suggested background color*/ | ||
104 | unsigned background_g; /*green component of suggested background color*/ | ||
105 | unsigned background_b; /*blue component of suggested background color*/ | ||
106 | |||
107 | } LodePNG_InfoPng; | ||
108 | |||
109 | typedef struct LodePNG_Decoder | ||
110 | { | ||
111 | unsigned char *buf; /* pointer to the buffer allocated for decoder | ||
112 | * filled by LodePNG_Decoder_init() | ||
113 | */ | ||
114 | size_t buf_size; /* size of the buffer decoder is free to use | ||
115 | * filled by LodePNG_Decoder_init() | ||
116 | */ | ||
117 | unsigned char *file; /* ptr to raw png file loaded */ | ||
118 | size_t file_size; /* size of the raw png file in mem */ | ||
119 | unsigned char *decoded_img; /* ptr to decoded PNG image in PNG pixel | ||
120 | * format. set by decodeGeneric() | ||
121 | */ | ||
122 | unsigned int native_img_size; /* size of the image in native pixel | ||
123 | * format | ||
124 | */ | ||
125 | LodePNG_InfoPng infoPng; /*info of the PNG image obtained after decoding*/ | ||
126 | long error; | ||
127 | } LodePNG_Decoder; | ||
128 | |||
129 | /* Public functions prototypes */ | ||
130 | void LodePNG_Decoder_init(LodePNG_Decoder* decoder, | ||
131 | uint8_t *buf, | ||
132 | size_t buf_size); | ||
133 | |||
134 | void LodePNG_decode(LodePNG_Decoder* decoder, | ||
135 | uint8_t* in, | ||
136 | size_t insize, | ||
137 | void (*pf_progress)(int current, int total)); | ||
138 | |||
139 | void LodePNG_inspect(LodePNG_Decoder* decoder, uint8_t *in, size_t inlength); | ||
140 | |||
141 | const char* LodePNG_perror(LodePNG_Decoder *decoder); | ||
diff --git a/apps/plugins/imageviewer/png/tinf.h b/apps/plugins/imageviewer/png/tinf.h new file mode 100644 index 0000000000..645bdb7d5a --- /dev/null +++ b/apps/plugins/imageviewer/png/tinf.h | |||
@@ -0,0 +1,77 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Original source: | ||
11 | * Copyright (c) 2003 by Joergen Ibsen / Jibz | ||
12 | * | ||
13 | * Rockbox adaptation: | ||
14 | * Copyright (c) 2010 by Marcin Bukat | ||
15 | * | ||
16 | * This program is free software; you can redistribute it and/or | ||
17 | * modify it under the terms of the GNU General Public License | ||
18 | * as published by the Free Software Foundation; either version 2 | ||
19 | * of the License, or (at your option) any later version. | ||
20 | * | ||
21 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
22 | * KIND, either express or implied. | ||
23 | * | ||
24 | ****************************************************************************/ | ||
25 | |||
26 | /* | ||
27 | * tinf - tiny inflate library (inflate, gzip, zlib) | ||
28 | * | ||
29 | * version 1.00 | ||
30 | * | ||
31 | * Copyright (c) 2003 by Joergen Ibsen / Jibz | ||
32 | * All Rights Reserved | ||
33 | * | ||
34 | * http://www.ibsensoftware.com/ | ||
35 | */ | ||
36 | |||
37 | /* removed from original file: | ||
38 | * tinf_gzip_uncompress() prototype | ||
39 | * tinf_init() prototype | ||
40 | */ | ||
41 | |||
42 | #ifndef TINF_H_INCLUDED | ||
43 | #define TINF_H_INCLUDED | ||
44 | |||
45 | /* calling convention */ | ||
46 | #ifndef TINFCC | ||
47 | #ifdef __WATCOMC__ | ||
48 | #define TINFCC __cdecl | ||
49 | #else | ||
50 | #define TINFCC | ||
51 | #endif | ||
52 | #endif | ||
53 | |||
54 | #ifdef __cplusplus | ||
55 | extern "C" { | ||
56 | #endif | ||
57 | |||
58 | #define TINF_OK 0 | ||
59 | #define TINF_DATA_ERROR (-3) | ||
60 | |||
61 | /* function prototypes */ | ||
62 | |||
63 | int TINFCC tinf_uncompress(void *dest, unsigned int *destLen, | ||
64 | const void *source, unsigned int sourceLen); | ||
65 | |||
66 | int TINFCC tinf_zlib_uncompress(void *dest, unsigned int *destLen, | ||
67 | const void *source, unsigned int sourceLen); | ||
68 | |||
69 | unsigned int TINFCC tinf_adler32(const void *data, unsigned int length); | ||
70 | |||
71 | unsigned int TINFCC tinf_crc32(const void *data, unsigned int length); | ||
72 | |||
73 | #ifdef __cplusplus | ||
74 | } /* extern "C" */ | ||
75 | #endif | ||
76 | |||
77 | #endif /* TINF_H_INCLUDED */ | ||
diff --git a/apps/plugins/imageviewer/png/tinflate.c b/apps/plugins/imageviewer/png/tinflate.c new file mode 100644 index 0000000000..b142f7afe7 --- /dev/null +++ b/apps/plugins/imageviewer/png/tinflate.c | |||
@@ -0,0 +1,503 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Original source: | ||
11 | * Copyright (c) 2003 by Joergen Ibsen / Jibz | ||
12 | * | ||
13 | * Rockbox adaptation: | ||
14 | * Copyright (c) 2010 by Marcin Bukat | ||
15 | * | ||
16 | * This program is free software; you can redistribute it and/or | ||
17 | * modify it under the terms of the GNU General Public License | ||
18 | * as published by the Free Software Foundation; either version 2 | ||
19 | * of the License, or (at your option) any later version. | ||
20 | * | ||
21 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
22 | * KIND, either express or implied. | ||
23 | * | ||
24 | ****************************************************************************/ | ||
25 | |||
26 | /* | ||
27 | * tinflate - tiny inflate | ||
28 | * | ||
29 | * Copyright (c) 2003 by Joergen Ibsen / Jibz | ||
30 | * All Rights Reserved | ||
31 | * | ||
32 | * http://www.ibsensoftware.com/ | ||
33 | * | ||
34 | * This software is provided 'as-is', without any express | ||
35 | * or implied warranty. In no event will the authors be | ||
36 | * held liable for any damages arising from the use of | ||
37 | * this software. | ||
38 | * | ||
39 | * Permission is granted to anyone to use this software | ||
40 | * for any purpose, including commercial applications, | ||
41 | * and to alter it and redistribute it freely, subject to | ||
42 | * the following restrictions: | ||
43 | * | ||
44 | * 1. The origin of this software must not be | ||
45 | * misrepresented; you must not claim that you | ||
46 | * wrote the original software. If you use this | ||
47 | * software in a product, an acknowledgment in | ||
48 | * the product documentation would be appreciated | ||
49 | * but is not required. | ||
50 | * | ||
51 | * 2. Altered source versions must be plainly marked | ||
52 | * as such, and must not be misrepresented as | ||
53 | * being the original software. | ||
54 | * | ||
55 | * 3. This notice may not be removed or altered from | ||
56 | * any source distribution. | ||
57 | */ | ||
58 | |||
59 | #include "tinf.h" | ||
60 | #include <string.h> /* memcpy */ | ||
61 | |||
62 | /* ------------------------------ * | ||
63 | * -- internal data structures -- * | ||
64 | * ------------------------------ */ | ||
65 | |||
66 | typedef struct { | ||
67 | unsigned short table[16]; /* table of code length counts */ | ||
68 | unsigned short trans[288]; /* code -> symbol translation table */ | ||
69 | } TINF_TREE; | ||
70 | |||
71 | typedef struct { | ||
72 | unsigned short table[16]; | ||
73 | unsigned short trans[32]; | ||
74 | } TINF_SDTREE; | ||
75 | |||
76 | typedef struct { | ||
77 | TINF_TREE sltree; | ||
78 | TINF_TREE sdtree; | ||
79 | unsigned char length_bits[30]; | ||
80 | unsigned short length_base[30]; | ||
81 | unsigned char dist_bits[30]; | ||
82 | unsigned short dist_base[30]; | ||
83 | unsigned char clcidx[19]; | ||
84 | } TINF_TABLES; | ||
85 | |||
86 | typedef struct { | ||
87 | const unsigned char *source; | ||
88 | unsigned int tag; | ||
89 | unsigned int bitcount; | ||
90 | |||
91 | unsigned char *dest; | ||
92 | unsigned int *destLen; | ||
93 | |||
94 | TINF_TREE ltree; /* dynamic length/symbol tree */ | ||
95 | TINF_TREE dtree; /* dynamic distance tree */ | ||
96 | } TINF_DATA; | ||
97 | |||
98 | /* static tables */ | ||
99 | TINF_TABLES tbl = { | ||
100 | .sltree = { | ||
101 | .table = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0018, | ||
102 | 0x0098, 0x0070, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, | ||
103 | |||
104 | .trans = {0x0100, 0x0101, 0x0102, 0x0103, 0x0104, 0x0105, 0x0106, 0x0107, | ||
105 | 0x0108, 0x0109, 0x010a, 0x010b, 0x010c, 0x010d, 0x010e, 0x010f, | ||
106 | 0x0110, 0x0111, 0x0112, 0x0113, 0x0114, 0x0115, 0x0116, 0x0117, | ||
107 | 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, | ||
108 | 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, | ||
109 | 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, | ||
110 | 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f, | ||
111 | 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027, | ||
112 | 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f, | ||
113 | 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, | ||
114 | 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f, | ||
115 | 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, | ||
116 | 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, | ||
117 | 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, | ||
118 | 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, | ||
119 | 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, | ||
120 | 0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f, | ||
121 | 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, | ||
122 | 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f, | ||
123 | 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087, | ||
124 | 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f, | ||
125 | 0x0118, 0x0119, 0x011a, 0x011b, 0x011c, 0x011d, 0x011e, 0x011f, | ||
126 | 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, | ||
127 | 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009e, 0x009f, | ||
128 | 0x00a0, 0x00a1, 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, | ||
129 | 0x00a8, 0x00a9, 0x00aa, 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, | ||
130 | 0x00b0, 0x00b1, 0x00b2, 0x00b3, 0x00b4, 0x00b5, 0x00b6, 0x00b7, | ||
131 | 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc, 0x00bd, 0x00be, 0x00bf, | ||
132 | 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6, 0x00c7, | ||
133 | 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce, 0x00cf, | ||
134 | 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7, | ||
135 | 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, | ||
136 | 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, | ||
137 | 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, | ||
138 | 0x00f0, 0x00f1, 0x00f2, 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, | ||
139 | 0x00f8, 0x00f9, 0x00fa, 0x00fb, 0x00fc, 0x00fd, 0x00fe, 0x00ff}, | ||
140 | }, | ||
141 | |||
142 | .sdtree = { | ||
143 | .table = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0000, 0x0000, | ||
144 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}, | ||
145 | |||
146 | .trans = {0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, | ||
147 | 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, | ||
148 | 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, | ||
149 | 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f}, | ||
150 | }, | ||
151 | |||
152 | .length_bits = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
153 | 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, | ||
154 | 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, | ||
155 | 0x05, 0x05, 0x05, 0x05, 0x00, 0x06}, | ||
156 | |||
157 | .length_base = {0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, | ||
158 | 0x000b, 0x000d, 0x000f, 0x0011, 0x0013, 0x0017, 0x001b, 0x001f, | ||
159 | 0x0023, 0x002b, 0x0033, 0x003b, 0x0043, 0x0053, 0x0063, 0x0073, | ||
160 | 0x0083, 0x00a3, 0x00c3, 0x00e3, 0x0102, 0x0143}, | ||
161 | |||
162 | .dist_bits = {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x02, 0x02, | ||
163 | 0x03, 0x03, 0x04, 0x04, 0x05, 0x05, 0x06, 0x06, | ||
164 | 0x07, 0x07, 0x08, 0x08, 0x09, 0x09, 0x0a, 0x0a, | ||
165 | 0x0b, 0x0b, 0x0c, 0x0c, 0x0d, 0x0d}, | ||
166 | |||
167 | .dist_base = {0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0007, 0x0009, 0x000d, | ||
168 | 0x0011, 0x0019, 0x0021, 0x0031, 0x0041, 0x0061, 0x0081, 0x00c1, | ||
169 | 0x0101, 0x0181, 0x0201, 0x0301, 0x0401, 0x0601, 0x0801, 0x0c01, | ||
170 | 0x1001, 0x1801, 0x2001, 0x3001, 0x4001, 0x6001}, | ||
171 | |||
172 | .clcidx = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}, | ||
173 | }; | ||
174 | |||
175 | |||
176 | /* ----------------------- * | ||
177 | * -- utility functions -- * | ||
178 | * ----------------------- */ | ||
179 | |||
180 | /* given an array of code lengths, build a tree */ | ||
181 | static void tinf_build_tree(TINF_TREE *t, const unsigned char *lengths, unsigned int num) | ||
182 | { | ||
183 | unsigned short offs[16]; | ||
184 | unsigned int i, sum; | ||
185 | |||
186 | /* clear code length count table */ | ||
187 | /* for (i = 0; i < 16; ++i) t->table[i] = 0; */ | ||
188 | memset(t->table, 0, sizeof(short)*16); | ||
189 | |||
190 | /* scan symbol lengths, and sum code length counts */ | ||
191 | for (i = 0; i < num; ++i) t->table[lengths[i]]++; | ||
192 | |||
193 | t->table[0] = 0; | ||
194 | |||
195 | /* compute offset table for distribution sort */ | ||
196 | for (sum = 0, i = 0; i < 16; ++i) | ||
197 | { | ||
198 | offs[i] = sum; | ||
199 | sum += t->table[i]; | ||
200 | } | ||
201 | |||
202 | /* create code->symbol translation table (symbols sorted by code) */ | ||
203 | for (i = 0; i < num; ++i) | ||
204 | { | ||
205 | if (lengths[i]) t->trans[offs[lengths[i]]++] = i; | ||
206 | } | ||
207 | } | ||
208 | |||
209 | /* ---------------------- * | ||
210 | * -- decode functions -- * | ||
211 | * ---------------------- */ | ||
212 | |||
213 | /* get one bit from source stream */ | ||
214 | static int tinf_getbit(TINF_DATA *d) | ||
215 | { | ||
216 | unsigned int bit; | ||
217 | |||
218 | /* check if tag is empty */ | ||
219 | if (!d->bitcount--) | ||
220 | { | ||
221 | /* load next tag */ | ||
222 | d->tag = *d->source++; | ||
223 | d->bitcount = 7; | ||
224 | } | ||
225 | |||
226 | /* shift bit out of tag */ | ||
227 | bit = d->tag & 0x01; | ||
228 | d->tag >>= 1; | ||
229 | |||
230 | return bit; | ||
231 | } | ||
232 | |||
233 | /* read a num bit value from a stream and add base */ | ||
234 | static unsigned int tinf_read_bits(TINF_DATA *d, int num, int base) | ||
235 | { | ||
236 | unsigned int val = 0; | ||
237 | |||
238 | /* read num bits */ | ||
239 | if (num) | ||
240 | { | ||
241 | unsigned int limit = 1 << (num); | ||
242 | unsigned int mask; | ||
243 | |||
244 | for (mask = 1; mask < limit; mask <<= 1) | ||
245 | if (tinf_getbit(d)) val += mask; | ||
246 | } | ||
247 | |||
248 | return val + base; | ||
249 | } | ||
250 | |||
251 | /* given a data stream and a tree, decode a symbol */ | ||
252 | static int tinf_decode_symbol(TINF_DATA *d, TINF_TREE *t) | ||
253 | { | ||
254 | int sum = 0, cur = 0, len = 0; | ||
255 | |||
256 | /* get more bits while code value is above sum */ | ||
257 | do { | ||
258 | |||
259 | cur = (cur << 1) + tinf_getbit(d); | ||
260 | |||
261 | ++len; | ||
262 | |||
263 | sum += t->table[len]; | ||
264 | cur -= t->table[len]; | ||
265 | |||
266 | } while (cur >= 0); | ||
267 | |||
268 | return t->trans[sum + cur]; | ||
269 | } | ||
270 | |||
271 | /* given a data stream, decode dynamic trees from it */ | ||
272 | static void tinf_decode_trees(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt, TINF_TABLES *tbl) | ||
273 | { | ||
274 | TINF_TREE code_tree; | ||
275 | unsigned char lengths[288+32]; | ||
276 | unsigned int hlit, hdist, hclen; | ||
277 | unsigned int i, num, length; | ||
278 | |||
279 | /* get 5 bits HLIT (257-286) */ | ||
280 | hlit = tinf_read_bits(d, 5, 257); | ||
281 | |||
282 | /* get 5 bits HDIST (1-32) */ | ||
283 | hdist = tinf_read_bits(d, 5, 1); | ||
284 | |||
285 | /* get 4 bits HCLEN (4-19) */ | ||
286 | hclen = tinf_read_bits(d, 4, 4); | ||
287 | |||
288 | /* for (i = 0; i < 19; ++i) lengths[i] = 0; */ | ||
289 | memset(lengths, 0, sizeof(unsigned char)*19); | ||
290 | |||
291 | /* read code lengths for code length alphabet */ | ||
292 | for (i = 0; i < hclen; ++i) | ||
293 | { | ||
294 | /* get 3 bits code length (0-7) */ | ||
295 | unsigned int clen = tinf_read_bits(d, 3, 0); | ||
296 | |||
297 | lengths[tbl->clcidx[i]] = clen; | ||
298 | } | ||
299 | |||
300 | /* build code length tree */ | ||
301 | tinf_build_tree(&code_tree, lengths, 19); | ||
302 | |||
303 | /* decode code lengths for the dynamic trees */ | ||
304 | for (num = 0; num < hlit + hdist; ) | ||
305 | { | ||
306 | int sym = tinf_decode_symbol(d, &code_tree); | ||
307 | |||
308 | switch (sym) | ||
309 | { | ||
310 | case 16: | ||
311 | /* copy previous code length 3-6 times (read 2 bits) */ | ||
312 | { | ||
313 | unsigned char prev = lengths[num - 1]; | ||
314 | for (length = tinf_read_bits(d, 2, 3); length; --length) | ||
315 | { | ||
316 | lengths[num++] = prev; | ||
317 | } | ||
318 | } | ||
319 | break; | ||
320 | case 17: | ||
321 | /* repeat code length 0 for 3-10 times (read 3 bits) */ | ||
322 | for (length = tinf_read_bits(d, 3, 3); length; --length) | ||
323 | { | ||
324 | lengths[num++] = 0; | ||
325 | } | ||
326 | break; | ||
327 | case 18: | ||
328 | /* repeat code length 0 for 11-138 times (read 7 bits) */ | ||
329 | for (length = tinf_read_bits(d, 7, 11); length; --length) | ||
330 | { | ||
331 | lengths[num++] = 0; | ||
332 | } | ||
333 | break; | ||
334 | default: | ||
335 | /* values 0-15 represent the actual code lengths */ | ||
336 | lengths[num++] = sym; | ||
337 | break; | ||
338 | } | ||
339 | } | ||
340 | |||
341 | /* build dynamic trees */ | ||
342 | tinf_build_tree(lt, lengths, hlit); | ||
343 | tinf_build_tree(dt, lengths + hlit, hdist); | ||
344 | } | ||
345 | |||
346 | /* ----------------------------- * | ||
347 | * -- block inflate functions -- * | ||
348 | * ----------------------------- */ | ||
349 | |||
350 | /* given a stream and two trees, inflate a block of data */ | ||
351 | static int tinf_inflate_block_data(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt, TINF_TABLES *tbl) | ||
352 | { | ||
353 | /* remember current output position */ | ||
354 | unsigned char *start = d->dest; | ||
355 | |||
356 | while (1) | ||
357 | { | ||
358 | int sym = tinf_decode_symbol(d, lt); | ||
359 | |||
360 | /* check for end of block */ | ||
361 | if (sym == 256) | ||
362 | { | ||
363 | *d->destLen += d->dest - start; | ||
364 | return TINF_OK; | ||
365 | } | ||
366 | |||
367 | if (sym < 256) | ||
368 | { | ||
369 | *d->dest++ = sym; | ||
370 | |||
371 | } else { | ||
372 | |||
373 | int length, dist, offs; | ||
374 | int i; | ||
375 | |||
376 | sym -= 257; | ||
377 | |||
378 | /* possibly get more bits from length code */ | ||
379 | length = tinf_read_bits(d, tbl->length_bits[sym], tbl->length_base[sym]); | ||
380 | |||
381 | dist = tinf_decode_symbol(d, dt); | ||
382 | |||
383 | /* possibly get more bits from distance code */ | ||
384 | offs = tinf_read_bits(d, tbl->dist_bits[dist], tbl->dist_base[dist]); | ||
385 | |||
386 | /* copy match */ | ||
387 | for (i = 0; i < length; ++i) | ||
388 | { | ||
389 | d->dest[i] = d->dest[i - offs]; | ||
390 | } | ||
391 | |||
392 | d->dest += length; | ||
393 | } | ||
394 | } | ||
395 | } | ||
396 | |||
397 | /* inflate an uncompressed block of data */ | ||
398 | static int tinf_inflate_uncompressed_block(TINF_DATA *d) | ||
399 | { | ||
400 | unsigned int length, invlength; | ||
401 | /* unsigned int i; */ | ||
402 | |||
403 | /* get length */ | ||
404 | length = d->source[1]; | ||
405 | length = (length << 8) + d->source[0]; | ||
406 | |||
407 | /* get one's complement of length */ | ||
408 | invlength = d->source[3]; | ||
409 | invlength = (invlength << 8) + d->source[2]; | ||
410 | |||
411 | /* check length */ | ||
412 | if (length != (~invlength & 0x0000ffff)) return TINF_DATA_ERROR; | ||
413 | |||
414 | d->source += 4; | ||
415 | |||
416 | /* copy block */ | ||
417 | /* for (i = length; i; --i) *d->dest++ = *d->source++; */ | ||
418 | memcpy(d->dest, d->source, sizeof(unsigned char)*length); | ||
419 | d->dest += sizeof(unsigned char)*length; | ||
420 | d->source += sizeof(unsigned char)*length; | ||
421 | |||
422 | /* make sure we start next block on a byte boundary */ | ||
423 | d->bitcount = 0; | ||
424 | |||
425 | *d->destLen += length; | ||
426 | |||
427 | return TINF_OK; | ||
428 | } | ||
429 | |||
430 | /* inflate a block of data compressed with fixed huffman trees */ | ||
431 | static int tinf_inflate_fixed_block(TINF_DATA *d, TINF_TABLES *tbl) | ||
432 | { | ||
433 | /* decode block using fixed trees */ | ||
434 | return tinf_inflate_block_data(d, &tbl->sltree, &tbl->sdtree, tbl); | ||
435 | } | ||
436 | |||
437 | /* inflate a block of data compressed with dynamic huffman trees */ | ||
438 | static int tinf_inflate_dynamic_block(TINF_DATA *d, TINF_TABLES *tbl) | ||
439 | { | ||
440 | /* decode trees from stream */ | ||
441 | tinf_decode_trees(d, &d->ltree, &d->dtree, tbl); | ||
442 | |||
443 | /* decode block using decoded trees */ | ||
444 | return tinf_inflate_block_data(d, &d->ltree, &d->dtree, tbl); | ||
445 | } | ||
446 | |||
447 | /* ---------------------- * | ||
448 | * -- public functions -- * | ||
449 | * ---------------------- */ | ||
450 | |||
451 | /* inflate stream from source to dest */ | ||
452 | int tinf_uncompress(void *dest, unsigned int *destLen, | ||
453 | const void *source, unsigned int sourceLen) | ||
454 | { | ||
455 | TINF_DATA d; | ||
456 | int bfinal; | ||
457 | |||
458 | d.source = (const unsigned char *)source; | ||
459 | d.bitcount = 0; | ||
460 | |||
461 | d.dest = (unsigned char *)dest; | ||
462 | d.destLen = destLen; | ||
463 | |||
464 | *destLen = 0; | ||
465 | |||
466 | do { | ||
467 | |||
468 | unsigned int btype; | ||
469 | int res; | ||
470 | |||
471 | /* read final block flag */ | ||
472 | bfinal = tinf_getbit(&d); | ||
473 | |||
474 | /* read block type (2 bits) */ | ||
475 | btype = tinf_read_bits(&d, 2, 0); | ||
476 | |||
477 | /* decompress block */ | ||
478 | switch (btype) | ||
479 | { | ||
480 | case 0: | ||
481 | /* decompress uncompressed block */ | ||
482 | res = tinf_inflate_uncompressed_block(&d); | ||
483 | break; | ||
484 | case 1: | ||
485 | /* decompress block with fixed huffman trees */ | ||
486 | res = tinf_inflate_fixed_block(&d,&tbl); | ||
487 | break; | ||
488 | case 2: | ||
489 | /* decompress block with dynamic huffman trees */ | ||
490 | res = tinf_inflate_dynamic_block(&d,&tbl); | ||
491 | break; | ||
492 | default: | ||
493 | return TINF_DATA_ERROR; | ||
494 | } | ||
495 | |||
496 | if (res != TINF_OK) return TINF_DATA_ERROR; | ||
497 | |||
498 | if (d.source > (unsigned char *)source + sourceLen) | ||
499 | return TINF_DATA_ERROR; | ||
500 | } while (!bfinal); | ||
501 | |||
502 | return TINF_OK; | ||
503 | } | ||
diff --git a/apps/plugins/imageviewer/png/tinfzlib.c b/apps/plugins/imageviewer/png/tinfzlib.c new file mode 100644 index 0000000000..711b6cf2e9 --- /dev/null +++ b/apps/plugins/imageviewer/png/tinfzlib.c | |||
@@ -0,0 +1,100 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Original source: | ||
11 | * Copyright (c) 2003 by Joergen Ibsen / Jibz | ||
12 | * | ||
13 | * Rockbox adaptation: | ||
14 | * Copyright (c) 2010 by Marcin Bukat | ||
15 | * | ||
16 | * This program is free software; you can redistribute it and/or | ||
17 | * modify it under the terms of the GNU General Public License | ||
18 | * as published by the Free Software Foundation; either version 2 | ||
19 | * of the License, or (at your option) any later version. | ||
20 | * | ||
21 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
22 | * KIND, either express or implied. | ||
23 | * | ||
24 | ****************************************************************************/ | ||
25 | |||
26 | /* | ||
27 | * tinfzlib - tiny zlib decompressor | ||
28 | * | ||
29 | * Copyright (c) 2003 by Joergen Ibsen / Jibz | ||
30 | * All Rights Reserved | ||
31 | * | ||
32 | * http://www.ibsensoftware.com/ | ||
33 | * | ||
34 | * This software is provided 'as-is', without any express | ||
35 | * or implied warranty. In no event will the authors be | ||
36 | * held liable for any damages arising from the use of | ||
37 | * this software. | ||
38 | * | ||
39 | * Permission is granted to anyone to use this software | ||
40 | * for any purpose, including commercial applications, | ||
41 | * and to alter it and redistribute it freely, subject to | ||
42 | * the following restrictions: | ||
43 | * | ||
44 | * 1. The origin of this software must not be | ||
45 | * misrepresented; you must not claim that you | ||
46 | * wrote the original software. If you use this | ||
47 | * software in a product, an acknowledgment in | ||
48 | * the product documentation would be appreciated | ||
49 | * but is not required. | ||
50 | * | ||
51 | * 2. Altered source versions must be plainly marked | ||
52 | * as such, and must not be misrepresented as | ||
53 | * being the original software. | ||
54 | * | ||
55 | * 3. This notice may not be removed or altered from | ||
56 | * any source distribution. | ||
57 | */ | ||
58 | |||
59 | #include "tinf.h" | ||
60 | |||
61 | /* This routine do not check adler32 checksum | ||
62 | * as it is tailored to be used as PNG decompressor | ||
63 | * and PNG has crc32 check of the compressed block | ||
64 | * already | ||
65 | */ | ||
66 | int tinf_zlib_uncompress(void *dest, unsigned int *destLen, | ||
67 | const void *source, unsigned int sourceLen) | ||
68 | { | ||
69 | unsigned char *src = (unsigned char *)source; | ||
70 | unsigned char *dst = (unsigned char *)dest; | ||
71 | int res; | ||
72 | unsigned char cmf, flg; | ||
73 | |||
74 | /* -- get header bytes -- */ | ||
75 | |||
76 | cmf = src[0]; | ||
77 | flg = src[1]; | ||
78 | |||
79 | /* -- check format -- */ | ||
80 | |||
81 | /* check checksum */ | ||
82 | if (((cmf << 8) + flg) % 31) return TINF_DATA_ERROR; | ||
83 | |||
84 | /* check method is deflate */ | ||
85 | if ((cmf & 0x0f) != 8) return TINF_DATA_ERROR; | ||
86 | |||
87 | /* check window size is valid */ | ||
88 | if ((cmf >> 4) > 7) return TINF_DATA_ERROR; | ||
89 | |||
90 | /* check there is no preset dictionary */ | ||
91 | if (flg & 0x20) return TINF_DATA_ERROR; | ||
92 | |||
93 | /* -- inflate -- */ | ||
94 | |||
95 | res = tinf_uncompress(dst, destLen, src + 2, sourceLen - 6); | ||
96 | |||
97 | if (res != TINF_OK) return TINF_DATA_ERROR; | ||
98 | |||
99 | return TINF_OK; | ||
100 | } | ||
diff --git a/apps/plugins/imageviewer/png/zconf.h b/apps/plugins/imageviewer/png/zconf.h deleted file mode 100644 index 03a9431c8b..0000000000 --- a/apps/plugins/imageviewer/png/zconf.h +++ /dev/null | |||
@@ -1,332 +0,0 @@ | |||
1 | /* zconf.h -- configuration of the zlib compression library | ||
2 | * Copyright (C) 1995-2005 Jean-loup Gailly. | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* @(#) $Id$ */ | ||
7 | |||
8 | #ifndef ZCONF_H | ||
9 | #define ZCONF_H | ||
10 | |||
11 | /* | ||
12 | * If you *really* need a unique prefix for all types and library functions, | ||
13 | * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. | ||
14 | */ | ||
15 | #ifdef Z_PREFIX | ||
16 | # define deflateInit_ z_deflateInit_ | ||
17 | # define deflate z_deflate | ||
18 | # define deflateEnd z_deflateEnd | ||
19 | # define inflateInit_ z_inflateInit_ | ||
20 | # define inflate z_inflate | ||
21 | # define inflateEnd z_inflateEnd | ||
22 | # define deflateInit2_ z_deflateInit2_ | ||
23 | # define deflateSetDictionary z_deflateSetDictionary | ||
24 | # define deflateCopy z_deflateCopy | ||
25 | # define deflateReset z_deflateReset | ||
26 | # define deflateParams z_deflateParams | ||
27 | # define deflateBound z_deflateBound | ||
28 | # define deflatePrime z_deflatePrime | ||
29 | # define inflateInit2_ z_inflateInit2_ | ||
30 | # define inflateSetDictionary z_inflateSetDictionary | ||
31 | # define inflateSync z_inflateSync | ||
32 | # define inflateSyncPoint z_inflateSyncPoint | ||
33 | # define inflateCopy z_inflateCopy | ||
34 | # define inflateReset z_inflateReset | ||
35 | # define inflateBack z_inflateBack | ||
36 | # define inflateBackEnd z_inflateBackEnd | ||
37 | # define compress z_compress | ||
38 | # define compress2 z_compress2 | ||
39 | # define compressBound z_compressBound | ||
40 | # define uncompress z_uncompress | ||
41 | # define adler32 z_adler32 | ||
42 | # define crc32 z_crc32 | ||
43 | # define get_crc_table z_get_crc_table | ||
44 | # define zError z_zError | ||
45 | |||
46 | # define alloc_func z_alloc_func | ||
47 | # define free_func z_free_func | ||
48 | # define in_func z_in_func | ||
49 | # define out_func z_out_func | ||
50 | # define Byte z_Byte | ||
51 | # define uInt z_uInt | ||
52 | # define uLong z_uLong | ||
53 | # define Bytef z_Bytef | ||
54 | # define charf z_charf | ||
55 | # define intf z_intf | ||
56 | # define uIntf z_uIntf | ||
57 | # define uLongf z_uLongf | ||
58 | # define voidpf z_voidpf | ||
59 | # define voidp z_voidp | ||
60 | #endif | ||
61 | |||
62 | #if defined(__MSDOS__) && !defined(MSDOS) | ||
63 | # define MSDOS | ||
64 | #endif | ||
65 | #if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) | ||
66 | # define OS2 | ||
67 | #endif | ||
68 | #if defined(_WINDOWS) && !defined(WINDOWS) | ||
69 | # define WINDOWS | ||
70 | #endif | ||
71 | #if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) | ||
72 | # ifndef WIN32 | ||
73 | # define WIN32 | ||
74 | # endif | ||
75 | #endif | ||
76 | #if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) | ||
77 | # if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) | ||
78 | # ifndef SYS16BIT | ||
79 | # define SYS16BIT | ||
80 | # endif | ||
81 | # endif | ||
82 | #endif | ||
83 | |||
84 | /* | ||
85 | * Compile with -DMAXSEG_64K if the alloc function cannot allocate more | ||
86 | * than 64k bytes at a time (needed on systems with 16-bit int). | ||
87 | */ | ||
88 | #ifdef SYS16BIT | ||
89 | # define MAXSEG_64K | ||
90 | #endif | ||
91 | #ifdef MSDOS | ||
92 | # define UNALIGNED_OK | ||
93 | #endif | ||
94 | |||
95 | #ifdef __STDC_VERSION__ | ||
96 | # ifndef STDC | ||
97 | # define STDC | ||
98 | # endif | ||
99 | # if __STDC_VERSION__ >= 199901L | ||
100 | # ifndef STDC99 | ||
101 | # define STDC99 | ||
102 | # endif | ||
103 | # endif | ||
104 | #endif | ||
105 | #if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) | ||
106 | # define STDC | ||
107 | #endif | ||
108 | #if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) | ||
109 | # define STDC | ||
110 | #endif | ||
111 | #if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) | ||
112 | # define STDC | ||
113 | #endif | ||
114 | #if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) | ||
115 | # define STDC | ||
116 | #endif | ||
117 | |||
118 | #if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ | ||
119 | # define STDC | ||
120 | #endif | ||
121 | |||
122 | #ifndef STDC | ||
123 | # ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ | ||
124 | # define const /* note: need a more gentle solution here */ | ||
125 | # endif | ||
126 | #endif | ||
127 | |||
128 | /* Some Mac compilers merge all .h files incorrectly: */ | ||
129 | #if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__) | ||
130 | # define NO_DUMMY_DECL | ||
131 | #endif | ||
132 | |||
133 | /* Maximum value for memLevel in deflateInit2 */ | ||
134 | #ifndef MAX_MEM_LEVEL | ||
135 | # ifdef MAXSEG_64K | ||
136 | # define MAX_MEM_LEVEL 8 | ||
137 | # else | ||
138 | # define MAX_MEM_LEVEL 9 | ||
139 | # endif | ||
140 | #endif | ||
141 | |||
142 | /* Maximum value for windowBits in deflateInit2 and inflateInit2. | ||
143 | * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files | ||
144 | * created by gzip. (Files created by minigzip can still be extracted by | ||
145 | * gzip.) | ||
146 | */ | ||
147 | #ifndef MAX_WBITS | ||
148 | # define MAX_WBITS 15 /* 32K LZ77 window */ | ||
149 | #endif | ||
150 | |||
151 | /* The memory requirements for deflate are (in bytes): | ||
152 | (1 << (windowBits+2)) + (1 << (memLevel+9)) | ||
153 | that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) | ||
154 | plus a few kilobytes for small objects. For example, if you want to reduce | ||
155 | the default memory requirements from 256K to 128K, compile with | ||
156 | make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" | ||
157 | Of course this will generally degrade compression (there's no free lunch). | ||
158 | |||
159 | The memory requirements for inflate are (in bytes) 1 << windowBits | ||
160 | that is, 32K for windowBits=15 (default value) plus a few kilobytes | ||
161 | for small objects. | ||
162 | */ | ||
163 | |||
164 | /* Type declarations */ | ||
165 | |||
166 | #ifndef OF /* function prototypes */ | ||
167 | # ifdef STDC | ||
168 | # define OF(args) args | ||
169 | # else | ||
170 | # define OF(args) () | ||
171 | # endif | ||
172 | #endif | ||
173 | |||
174 | /* The following definitions for FAR are needed only for MSDOS mixed | ||
175 | * model programming (small or medium model with some far allocations). | ||
176 | * This was tested only with MSC; for other MSDOS compilers you may have | ||
177 | * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, | ||
178 | * just define FAR to be empty. | ||
179 | */ | ||
180 | #ifdef SYS16BIT | ||
181 | # if defined(M_I86SM) || defined(M_I86MM) | ||
182 | /* MSC small or medium model */ | ||
183 | # define SMALL_MEDIUM | ||
184 | # ifdef _MSC_VER | ||
185 | # define FAR _far | ||
186 | # else | ||
187 | # define FAR far | ||
188 | # endif | ||
189 | # endif | ||
190 | # if (defined(__SMALL__) || defined(__MEDIUM__)) | ||
191 | /* Turbo C small or medium model */ | ||
192 | # define SMALL_MEDIUM | ||
193 | # ifdef __BORLANDC__ | ||
194 | # define FAR _far | ||
195 | # else | ||
196 | # define FAR far | ||
197 | # endif | ||
198 | # endif | ||
199 | #endif | ||
200 | |||
201 | #if defined(WINDOWS) || defined(WIN32) | ||
202 | /* If building or using zlib as a DLL, define ZLIB_DLL. | ||
203 | * This is not mandatory, but it offers a little performance increase. | ||
204 | */ | ||
205 | # ifdef ZLIB_DLL | ||
206 | # if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) | ||
207 | # ifdef ZLIB_INTERNAL | ||
208 | # define ZEXTERN extern __declspec(dllexport) | ||
209 | # else | ||
210 | # define ZEXTERN extern __declspec(dllimport) | ||
211 | # endif | ||
212 | # endif | ||
213 | # endif /* ZLIB_DLL */ | ||
214 | /* If building or using zlib with the WINAPI/WINAPIV calling convention, | ||
215 | * define ZLIB_WINAPI. | ||
216 | * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. | ||
217 | */ | ||
218 | # ifdef ZLIB_WINAPI | ||
219 | # ifdef FAR | ||
220 | # undef FAR | ||
221 | # endif | ||
222 | # include <windows.h> | ||
223 | /* No need for _export, use ZLIB.DEF instead. */ | ||
224 | /* For complete Windows compatibility, use WINAPI, not __stdcall. */ | ||
225 | # define ZEXPORT WINAPI | ||
226 | # ifdef WIN32 | ||
227 | # define ZEXPORTVA WINAPIV | ||
228 | # else | ||
229 | # define ZEXPORTVA FAR CDECL | ||
230 | # endif | ||
231 | # endif | ||
232 | #endif | ||
233 | |||
234 | #if defined (__BEOS__) | ||
235 | # ifdef ZLIB_DLL | ||
236 | # ifdef ZLIB_INTERNAL | ||
237 | # define ZEXPORT __declspec(dllexport) | ||
238 | # define ZEXPORTVA __declspec(dllexport) | ||
239 | # else | ||
240 | # define ZEXPORT __declspec(dllimport) | ||
241 | # define ZEXPORTVA __declspec(dllimport) | ||
242 | # endif | ||
243 | # endif | ||
244 | #endif | ||
245 | |||
246 | #ifndef ZEXTERN | ||
247 | # define ZEXTERN extern | ||
248 | #endif | ||
249 | #ifndef ZEXPORT | ||
250 | # define ZEXPORT | ||
251 | #endif | ||
252 | #ifndef ZEXPORTVA | ||
253 | # define ZEXPORTVA | ||
254 | #endif | ||
255 | |||
256 | #ifndef FAR | ||
257 | # define FAR | ||
258 | #endif | ||
259 | |||
260 | #if !defined(__MACTYPES__) | ||
261 | typedef unsigned char Byte; /* 8 bits */ | ||
262 | #endif | ||
263 | typedef unsigned int uInt; /* 16 bits or more */ | ||
264 | typedef unsigned long uLong; /* 32 bits or more */ | ||
265 | |||
266 | #ifdef SMALL_MEDIUM | ||
267 | /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ | ||
268 | # define Bytef Byte FAR | ||
269 | #else | ||
270 | typedef Byte FAR Bytef; | ||
271 | #endif | ||
272 | typedef char FAR charf; | ||
273 | typedef int FAR intf; | ||
274 | typedef uInt FAR uIntf; | ||
275 | typedef uLong FAR uLongf; | ||
276 | |||
277 | #ifdef STDC | ||
278 | typedef void const *voidpc; | ||
279 | typedef void FAR *voidpf; | ||
280 | typedef void *voidp; | ||
281 | #else | ||
282 | typedef Byte const *voidpc; | ||
283 | typedef Byte FAR *voidpf; | ||
284 | typedef Byte *voidp; | ||
285 | #endif | ||
286 | |||
287 | #if 0 /* HAVE_UNISTD_H -- this line is updated by ./configure */ | ||
288 | # include <sys/types.h> /* for off_t */ | ||
289 | # include <unistd.h> /* for SEEK_* and off_t */ | ||
290 | # ifdef VMS | ||
291 | # include <unixio.h> /* for off_t */ | ||
292 | # endif | ||
293 | # define z_off_t off_t | ||
294 | #endif | ||
295 | #ifndef SEEK_SET | ||
296 | # define SEEK_SET 0 /* Seek from beginning of file. */ | ||
297 | # define SEEK_CUR 1 /* Seek from current position. */ | ||
298 | # define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ | ||
299 | #endif | ||
300 | #ifndef z_off_t | ||
301 | # define z_off_t long | ||
302 | #endif | ||
303 | |||
304 | #if defined(__OS400__) | ||
305 | # define NO_vsnprintf | ||
306 | #endif | ||
307 | |||
308 | #if defined(__MVS__) | ||
309 | # define NO_vsnprintf | ||
310 | # ifdef FAR | ||
311 | # undef FAR | ||
312 | # endif | ||
313 | #endif | ||
314 | |||
315 | /* MVS linker does not support external names larger than 8 bytes */ | ||
316 | #if defined(__MVS__) | ||
317 | # pragma map(deflateInit_,"DEIN") | ||
318 | # pragma map(deflateInit2_,"DEIN2") | ||
319 | # pragma map(deflateEnd,"DEEND") | ||
320 | # pragma map(deflateBound,"DEBND") | ||
321 | # pragma map(inflateInit_,"ININ") | ||
322 | # pragma map(inflateInit2_,"ININ2") | ||
323 | # pragma map(inflateEnd,"INEND") | ||
324 | # pragma map(inflateSync,"INSY") | ||
325 | # pragma map(inflateSetDictionary,"INSEDI") | ||
326 | # pragma map(compressBound,"CMBND") | ||
327 | # pragma map(inflate_table,"INTABL") | ||
328 | # pragma map(inflate_fast,"INFA") | ||
329 | # pragma map(inflate_copyright,"INCOPY") | ||
330 | #endif | ||
331 | |||
332 | #endif /* ZCONF_H */ | ||
diff --git a/apps/plugins/imageviewer/png/zlib.h b/apps/plugins/imageviewer/png/zlib.h deleted file mode 100644 index 23e6dcd8f5..0000000000 --- a/apps/plugins/imageviewer/png/zlib.h +++ /dev/null | |||
@@ -1,1357 +0,0 @@ | |||
1 | /* zlib.h -- interface of the 'zlib' general purpose compression library | ||
2 | version 1.2.3, July 18th, 2005 | ||
3 | |||
4 | Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler | ||
5 | |||
6 | This software is provided 'as-is', without any express or implied | ||
7 | warranty. In no event will the authors be held liable for any damages | ||
8 | arising from the use of this software. | ||
9 | |||
10 | Permission is granted to anyone to use this software for any purpose, | ||
11 | including commercial applications, and to alter it and redistribute it | ||
12 | freely, subject to the following restrictions: | ||
13 | |||
14 | 1. The origin of this software must not be misrepresented; you must not | ||
15 | claim that you wrote the original software. If you use this software | ||
16 | in a product, an acknowledgment in the product documentation would be | ||
17 | appreciated but is not required. | ||
18 | 2. Altered source versions must be plainly marked as such, and must not be | ||
19 | misrepresented as being the original software. | ||
20 | 3. This notice may not be removed or altered from any source distribution. | ||
21 | |||
22 | Jean-loup Gailly Mark Adler | ||
23 | jloup@gzip.org madler@alumni.caltech.edu | ||
24 | |||
25 | |||
26 | The data format used by the zlib library is described by RFCs (Request for | ||
27 | Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt | ||
28 | (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). | ||
29 | */ | ||
30 | |||
31 | #ifndef ZLIB_H | ||
32 | #define ZLIB_H | ||
33 | |||
34 | #include "zconf.h" | ||
35 | |||
36 | #ifdef __cplusplus | ||
37 | extern "C" { | ||
38 | #endif | ||
39 | |||
40 | #define ZLIB_VERSION "1.2.3" | ||
41 | #define ZLIB_VERNUM 0x1230 | ||
42 | |||
43 | /* | ||
44 | The 'zlib' compression library provides in-memory compression and | ||
45 | decompression functions, including integrity checks of the uncompressed | ||
46 | data. This version of the library supports only one compression method | ||
47 | (deflation) but other algorithms will be added later and will have the same | ||
48 | stream interface. | ||
49 | |||
50 | Compression can be done in a single step if the buffers are large | ||
51 | enough (for example if an input file is mmap'ed), or can be done by | ||
52 | repeated calls of the compression function. In the latter case, the | ||
53 | application must provide more input and/or consume the output | ||
54 | (providing more output space) before each call. | ||
55 | |||
56 | The compressed data format used by default by the in-memory functions is | ||
57 | the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped | ||
58 | around a deflate stream, which is itself documented in RFC 1951. | ||
59 | |||
60 | The library also supports reading and writing files in gzip (.gz) format | ||
61 | with an interface similar to that of stdio using the functions that start | ||
62 | with "gz". The gzip format is different from the zlib format. gzip is a | ||
63 | gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. | ||
64 | |||
65 | This library can optionally read and write gzip streams in memory as well. | ||
66 | |||
67 | The zlib format was designed to be compact and fast for use in memory | ||
68 | and on communications channels. The gzip format was designed for single- | ||
69 | file compression on file systems, has a larger header than zlib to maintain | ||
70 | directory information, and uses a different, slower check method than zlib. | ||
71 | |||
72 | The library does not install any signal handler. The decoder checks | ||
73 | the consistency of the compressed data, so the library should never | ||
74 | crash even in case of corrupted input. | ||
75 | */ | ||
76 | |||
77 | typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); | ||
78 | typedef void (*free_func) OF((voidpf opaque, voidpf address)); | ||
79 | |||
80 | struct internal_state; | ||
81 | |||
82 | typedef struct z_stream_s { | ||
83 | Bytef *next_in; /* next input byte */ | ||
84 | uInt avail_in; /* number of bytes available at next_in */ | ||
85 | uLong total_in; /* total nb of input bytes read so far */ | ||
86 | |||
87 | Bytef *next_out; /* next output byte should be put there */ | ||
88 | uInt avail_out; /* remaining free space at next_out */ | ||
89 | uLong total_out; /* total nb of bytes output so far */ | ||
90 | |||
91 | char *msg; /* last error message, NULL if no error */ | ||
92 | //struct internal_state state; /* not visible by applications */ | ||
93 | |||
94 | alloc_func zalloc; /* used to allocate the internal state */ | ||
95 | free_func zfree; /* used to free the internal state */ | ||
96 | voidpf opaque; /* private data object passed to zalloc and zfree */ | ||
97 | |||
98 | int data_type; /* best guess about the data type: binary or text */ | ||
99 | uLong adler; /* adler32 value of the uncompressed data */ | ||
100 | uLong reserved; /* reserved for future use */ | ||
101 | } z_stream; | ||
102 | |||
103 | typedef z_stream FAR *z_streamp; | ||
104 | |||
105 | /* | ||
106 | gzip header information passed to and from zlib routines. See RFC 1952 | ||
107 | for more details on the meanings of these fields. | ||
108 | */ | ||
109 | typedef struct gz_header_s { | ||
110 | int text; /* true if compressed data believed to be text */ | ||
111 | uLong time; /* modification time */ | ||
112 | int xflags; /* extra flags (not used when writing a gzip file) */ | ||
113 | int os; /* operating system */ | ||
114 | Bytef *extra; /* pointer to extra field or Z_NULL if none */ | ||
115 | uInt extra_len; /* extra field length (valid if extra != Z_NULL) */ | ||
116 | uInt extra_max; /* space at extra (only when reading header) */ | ||
117 | Bytef *name; /* pointer to zero-terminated file name or Z_NULL */ | ||
118 | uInt name_max; /* space at name (only when reading header) */ | ||
119 | Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */ | ||
120 | uInt comm_max; /* space at comment (only when reading header) */ | ||
121 | int hcrc; /* true if there was or will be a header crc */ | ||
122 | int done; /* true when done reading gzip header (not used | ||
123 | when writing a gzip file) */ | ||
124 | } gz_header; | ||
125 | |||
126 | typedef gz_header FAR *gz_headerp; | ||
127 | |||
128 | /* | ||
129 | The application must update next_in and avail_in when avail_in has | ||
130 | dropped to zero. It must update next_out and avail_out when avail_out | ||
131 | has dropped to zero. The application must initialize zalloc, zfree and | ||
132 | opaque before calling the init function. All other fields are set by the | ||
133 | compression library and must not be updated by the application. | ||
134 | |||
135 | The opaque value provided by the application will be passed as the first | ||
136 | parameter for calls of zalloc and zfree. This can be useful for custom | ||
137 | memory management. The compression library attaches no meaning to the | ||
138 | opaque value. | ||
139 | |||
140 | zalloc must return Z_NULL if there is not enough memory for the object. | ||
141 | If zlib is used in a multi-threaded application, zalloc and zfree must be | ||
142 | thread safe. | ||
143 | |||
144 | On 16-bit systems, the functions zalloc and zfree must be able to allocate | ||
145 | exactly 65536 bytes, but will not be required to allocate more than this | ||
146 | if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, | ||
147 | pointers returned by zalloc for objects of exactly 65536 bytes *must* | ||
148 | have their offset normalized to zero. The default allocation function | ||
149 | provided by this library ensures this (see zutil.c). To reduce memory | ||
150 | requirements and avoid any allocation of 64K objects, at the expense of | ||
151 | compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h). | ||
152 | |||
153 | The fields total_in and total_out can be used for statistics or | ||
154 | progress reports. After compression, total_in holds the total size of | ||
155 | the uncompressed data and may be saved for use in the decompressor | ||
156 | (particularly if the decompressor wants to decompress everything in | ||
157 | a single step). | ||
158 | */ | ||
159 | |||
160 | /* constants */ | ||
161 | |||
162 | #define Z_NO_FLUSH 0 | ||
163 | #define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */ | ||
164 | #define Z_SYNC_FLUSH 2 | ||
165 | #define Z_FULL_FLUSH 3 | ||
166 | #define Z_FINISH 4 | ||
167 | #define Z_BLOCK 5 | ||
168 | /* Allowed flush values; see deflate() and inflate() below for details */ | ||
169 | |||
170 | #define Z_OK 0 | ||
171 | #define Z_STREAM_END 1 | ||
172 | #define Z_NEED_DICT 2 | ||
173 | #define Z_ERRNO (-1) | ||
174 | #define Z_STREAM_ERROR (-2) | ||
175 | #define Z_DATA_ERROR (-3) | ||
176 | #define Z_MEM_ERROR (-4) | ||
177 | #define Z_BUF_ERROR (-5) | ||
178 | #define Z_VERSION_ERROR (-6) | ||
179 | /* Return codes for the compression/decompression functions. Negative | ||
180 | * values are errors, positive values are used for special but normal events. | ||
181 | */ | ||
182 | |||
183 | #define Z_NO_COMPRESSION 0 | ||
184 | #define Z_BEST_SPEED 1 | ||
185 | #define Z_BEST_COMPRESSION 9 | ||
186 | #define Z_DEFAULT_COMPRESSION (-1) | ||
187 | /* compression levels */ | ||
188 | |||
189 | #define Z_FILTERED 1 | ||
190 | #define Z_HUFFMAN_ONLY 2 | ||
191 | #define Z_RLE 3 | ||
192 | #define Z_FIXED 4 | ||
193 | #define Z_DEFAULT_STRATEGY 0 | ||
194 | /* compression strategy; see deflateInit2() below for details */ | ||
195 | |||
196 | #define Z_BINARY 0 | ||
197 | #define Z_TEXT 1 | ||
198 | #define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ | ||
199 | #define Z_UNKNOWN 2 | ||
200 | /* Possible values of the data_type field (though see inflate()) */ | ||
201 | |||
202 | #define Z_DEFLATED 8 | ||
203 | /* The deflate compression method (the only one supported in this version) */ | ||
204 | |||
205 | #define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ | ||
206 | |||
207 | #define zlib_version zlibVersion() | ||
208 | /* for compatibility with versions < 1.0.2 */ | ||
209 | |||
210 | /* basic functions */ | ||
211 | |||
212 | ZEXTERN const char * ZEXPORT zlibVersion OF((void)); | ||
213 | /* The application can compare zlibVersion and ZLIB_VERSION for consistency. | ||
214 | If the first character differs, the library code actually used is | ||
215 | not compatible with the zlib.h header file used by the application. | ||
216 | This check is automatically made by deflateInit and inflateInit. | ||
217 | */ | ||
218 | |||
219 | /* | ||
220 | ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); | ||
221 | |||
222 | Initializes the internal stream state for compression. The fields | ||
223 | zalloc, zfree and opaque must be initialized before by the caller. | ||
224 | If zalloc and zfree are set to Z_NULL, deflateInit updates them to | ||
225 | use default allocation functions. | ||
226 | |||
227 | The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: | ||
228 | 1 gives best speed, 9 gives best compression, 0 gives no compression at | ||
229 | all (the input data is simply copied a block at a time). | ||
230 | Z_DEFAULT_COMPRESSION requests a default compromise between speed and | ||
231 | compression (currently equivalent to level 6). | ||
232 | |||
233 | deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not | ||
234 | enough memory, Z_STREAM_ERROR if level is not a valid compression level, | ||
235 | Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible | ||
236 | with the version assumed by the caller (ZLIB_VERSION). | ||
237 | msg is set to null if there is no error message. deflateInit does not | ||
238 | perform any compression: this will be done by deflate(). | ||
239 | */ | ||
240 | |||
241 | |||
242 | ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); | ||
243 | /* | ||
244 | deflate compresses as much data as possible, and stops when the input | ||
245 | buffer becomes empty or the output buffer becomes full. It may introduce some | ||
246 | output latency (reading input without producing any output) except when | ||
247 | forced to flush. | ||
248 | |||
249 | The detailed semantics are as follows. deflate performs one or both of the | ||
250 | following actions: | ||
251 | |||
252 | - Compress more input starting at next_in and update next_in and avail_in | ||
253 | accordingly. If not all input can be processed (because there is not | ||
254 | enough room in the output buffer), next_in and avail_in are updated and | ||
255 | processing will resume at this point for the next call of deflate(). | ||
256 | |||
257 | - Provide more output starting at next_out and update next_out and avail_out | ||
258 | accordingly. This action is forced if the parameter flush is non zero. | ||
259 | Forcing flush frequently degrades the compression ratio, so this parameter | ||
260 | should be set only when necessary (in interactive applications). | ||
261 | Some output may be provided even if flush is not set. | ||
262 | |||
263 | Before the call of deflate(), the application should ensure that at least | ||
264 | one of the actions is possible, by providing more input and/or consuming | ||
265 | more output, and updating avail_in or avail_out accordingly; avail_out | ||
266 | should never be zero before the call. The application can consume the | ||
267 | compressed output when it wants, for example when the output buffer is full | ||
268 | (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK | ||
269 | and with zero avail_out, it must be called again after making room in the | ||
270 | output buffer because there might be more output pending. | ||
271 | |||
272 | Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to | ||
273 | decide how much data to accumualte before producing output, in order to | ||
274 | maximize compression. | ||
275 | |||
276 | If the parameter flush is set to Z_SYNC_FLUSH, all pending output is | ||
277 | flushed to the output buffer and the output is aligned on a byte boundary, so | ||
278 | that the decompressor can get all input data available so far. (In particular | ||
279 | avail_in is zero after the call if enough output space has been provided | ||
280 | before the call.) Flushing may degrade compression for some compression | ||
281 | algorithms and so it should be used only when necessary. | ||
282 | |||
283 | If flush is set to Z_FULL_FLUSH, all output is flushed as with | ||
284 | Z_SYNC_FLUSH, and the compression state is reset so that decompression can | ||
285 | restart from this point if previous compressed data has been damaged or if | ||
286 | random access is desired. Using Z_FULL_FLUSH too often can seriously degrade | ||
287 | compression. | ||
288 | |||
289 | If deflate returns with avail_out == 0, this function must be called again | ||
290 | with the same value of the flush parameter and more output space (updated | ||
291 | avail_out), until the flush is complete (deflate returns with non-zero | ||
292 | avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that | ||
293 | avail_out is greater than six to avoid repeated flush markers due to | ||
294 | avail_out == 0 on return. | ||
295 | |||
296 | If the parameter flush is set to Z_FINISH, pending input is processed, | ||
297 | pending output is flushed and deflate returns with Z_STREAM_END if there | ||
298 | was enough output space; if deflate returns with Z_OK, this function must be | ||
299 | called again with Z_FINISH and more output space (updated avail_out) but no | ||
300 | more input data, until it returns with Z_STREAM_END or an error. After | ||
301 | deflate has returned Z_STREAM_END, the only possible operations on the | ||
302 | stream are deflateReset or deflateEnd. | ||
303 | |||
304 | Z_FINISH can be used immediately after deflateInit if all the compression | ||
305 | is to be done in a single step. In this case, avail_out must be at least | ||
306 | the value returned by deflateBound (see below). If deflate does not return | ||
307 | Z_STREAM_END, then it must be called again as described above. | ||
308 | |||
309 | deflate() sets strm->adler to the adler32 checksum of all input read | ||
310 | so far (that is, total_in bytes). | ||
311 | |||
312 | deflate() may update strm->data_type if it can make a good guess about | ||
313 | the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered | ||
314 | binary. This field is only for information purposes and does not affect | ||
315 | the compression algorithm in any manner. | ||
316 | |||
317 | deflate() returns Z_OK if some progress has been made (more input | ||
318 | processed or more output produced), Z_STREAM_END if all input has been | ||
319 | consumed and all output has been produced (only when flush is set to | ||
320 | Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example | ||
321 | if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible | ||
322 | (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not | ||
323 | fatal, and deflate() can be called again with more input and more output | ||
324 | space to continue compressing. | ||
325 | */ | ||
326 | |||
327 | |||
328 | ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); | ||
329 | /* | ||
330 | All dynamically allocated data structures for this stream are freed. | ||
331 | This function discards any unprocessed input and does not flush any | ||
332 | pending output. | ||
333 | |||
334 | deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the | ||
335 | stream state was inconsistent, Z_DATA_ERROR if the stream was freed | ||
336 | prematurely (some input or output was discarded). In the error case, | ||
337 | msg may be set but then points to a static string (which must not be | ||
338 | deallocated). | ||
339 | */ | ||
340 | |||
341 | |||
342 | /* | ||
343 | ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); | ||
344 | |||
345 | Initializes the internal stream state for decompression. The fields | ||
346 | next_in, avail_in, zalloc, zfree and opaque must be initialized before by | ||
347 | the caller. If next_in is not Z_NULL and avail_in is large enough (the exact | ||
348 | value depends on the compression method), inflateInit determines the | ||
349 | compression method from the zlib header and allocates all data structures | ||
350 | accordingly; otherwise the allocation will be deferred to the first call of | ||
351 | inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to | ||
352 | use default allocation functions. | ||
353 | |||
354 | inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough | ||
355 | memory, Z_VERSION_ERROR if the zlib library version is incompatible with the | ||
356 | version assumed by the caller. msg is set to null if there is no error | ||
357 | message. inflateInit does not perform any decompression apart from reading | ||
358 | the zlib header if present: this will be done by inflate(). (So next_in and | ||
359 | avail_in may be modified, but next_out and avail_out are unchanged.) | ||
360 | */ | ||
361 | |||
362 | |||
363 | ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); | ||
364 | /* | ||
365 | inflate decompresses as much data as possible, and stops when the input | ||
366 | buffer becomes empty or the output buffer becomes full. It may introduce | ||
367 | some output latency (reading input without producing any output) except when | ||
368 | forced to flush. | ||
369 | |||
370 | The detailed semantics are as follows. inflate performs one or both of the | ||
371 | following actions: | ||
372 | |||
373 | - Decompress more input starting at next_in and update next_in and avail_in | ||
374 | accordingly. If not all input can be processed (because there is not | ||
375 | enough room in the output buffer), next_in is updated and processing | ||
376 | will resume at this point for the next call of inflate(). | ||
377 | |||
378 | - Provide more output starting at next_out and update next_out and avail_out | ||
379 | accordingly. inflate() provides as much output as possible, until there | ||
380 | is no more input data or no more space in the output buffer (see below | ||
381 | about the flush parameter). | ||
382 | |||
383 | Before the call of inflate(), the application should ensure that at least | ||
384 | one of the actions is possible, by providing more input and/or consuming | ||
385 | more output, and updating the next_* and avail_* values accordingly. | ||
386 | The application can consume the uncompressed output when it wants, for | ||
387 | example when the output buffer is full (avail_out == 0), or after each | ||
388 | call of inflate(). If inflate returns Z_OK and with zero avail_out, it | ||
389 | must be called again after making room in the output buffer because there | ||
390 | might be more output pending. | ||
391 | |||
392 | The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, | ||
393 | Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much | ||
394 | output as possible to the output buffer. Z_BLOCK requests that inflate() stop | ||
395 | if and when it gets to the next deflate block boundary. When decoding the | ||
396 | zlib or gzip format, this will cause inflate() to return immediately after | ||
397 | the header and before the first block. When doing a raw inflate, inflate() | ||
398 | will go ahead and process the first block, and will return when it gets to | ||
399 | the end of that block, or when it runs out of data. | ||
400 | |||
401 | The Z_BLOCK option assists in appending to or combining deflate streams. | ||
402 | Also to assist in this, on return inflate() will set strm->data_type to the | ||
403 | number of unused bits in the last byte taken from strm->next_in, plus 64 | ||
404 | if inflate() is currently decoding the last block in the deflate stream, | ||
405 | plus 128 if inflate() returned immediately after decoding an end-of-block | ||
406 | code or decoding the complete header up to just before the first byte of the | ||
407 | deflate stream. The end-of-block will not be indicated until all of the | ||
408 | uncompressed data from that block has been written to strm->next_out. The | ||
409 | number of unused bits may in general be greater than seven, except when | ||
410 | bit 7 of data_type is set, in which case the number of unused bits will be | ||
411 | less than eight. | ||
412 | |||
413 | inflate() should normally be called until it returns Z_STREAM_END or an | ||
414 | error. However if all decompression is to be performed in a single step | ||
415 | (a single call of inflate), the parameter flush should be set to | ||
416 | Z_FINISH. In this case all pending input is processed and all pending | ||
417 | output is flushed; avail_out must be large enough to hold all the | ||
418 | uncompressed data. (The size of the uncompressed data may have been saved | ||
419 | by the compressor for this purpose.) The next operation on this stream must | ||
420 | be inflateEnd to deallocate the decompression state. The use of Z_FINISH | ||
421 | is never required, but can be used to inform inflate that a faster approach | ||
422 | may be used for the single inflate() call. | ||
423 | |||
424 | In this implementation, inflate() always flushes as much output as | ||
425 | possible to the output buffer, and always uses the faster approach on the | ||
426 | first call. So the only effect of the flush parameter in this implementation | ||
427 | is on the return value of inflate(), as noted below, or when it returns early | ||
428 | because Z_BLOCK is used. | ||
429 | |||
430 | If a preset dictionary is needed after this call (see inflateSetDictionary | ||
431 | below), inflate sets strm->adler to the adler32 checksum of the dictionary | ||
432 | chosen by the compressor and returns Z_NEED_DICT; otherwise it sets | ||
433 | strm->adler to the adler32 checksum of all output produced so far (that is, | ||
434 | total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described | ||
435 | below. At the end of the stream, inflate() checks that its computed adler32 | ||
436 | checksum is equal to that saved by the compressor and returns Z_STREAM_END | ||
437 | only if the checksum is correct. | ||
438 | |||
439 | inflate() will decompress and check either zlib-wrapped or gzip-wrapped | ||
440 | deflate data. The header type is detected automatically. Any information | ||
441 | contained in the gzip header is not retained, so applications that need that | ||
442 | information should instead use raw inflate, see inflateInit2() below, or | ||
443 | inflateBack() and perform their own processing of the gzip header and | ||
444 | trailer. | ||
445 | |||
446 | inflate() returns Z_OK if some progress has been made (more input processed | ||
447 | or more output produced), Z_STREAM_END if the end of the compressed data has | ||
448 | been reached and all uncompressed output has been produced, Z_NEED_DICT if a | ||
449 | preset dictionary is needed at this point, Z_DATA_ERROR if the input data was | ||
450 | corrupted (input stream not conforming to the zlib format or incorrect check | ||
451 | value), Z_STREAM_ERROR if the stream structure was inconsistent (for example | ||
452 | if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory, | ||
453 | Z_BUF_ERROR if no progress is possible or if there was not enough room in the | ||
454 | output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and | ||
455 | inflate() can be called again with more input and more output space to | ||
456 | continue decompressing. If Z_DATA_ERROR is returned, the application may then | ||
457 | call inflateSync() to look for a good compression block if a partial recovery | ||
458 | of the data is desired. | ||
459 | */ | ||
460 | |||
461 | |||
462 | ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); | ||
463 | /* | ||
464 | All dynamically allocated data structures for this stream are freed. | ||
465 | This function discards any unprocessed input and does not flush any | ||
466 | pending output. | ||
467 | |||
468 | inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state | ||
469 | was inconsistent. In the error case, msg may be set but then points to a | ||
470 | static string (which must not be deallocated). | ||
471 | */ | ||
472 | |||
473 | /* Advanced functions */ | ||
474 | |||
475 | /* | ||
476 | The following functions are needed only in some special applications. | ||
477 | */ | ||
478 | |||
479 | /* | ||
480 | ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, | ||
481 | int level, | ||
482 | int method, | ||
483 | int windowBits, | ||
484 | int memLevel, | ||
485 | int strategy)); | ||
486 | |||
487 | This is another version of deflateInit with more compression options. The | ||
488 | fields next_in, zalloc, zfree and opaque must be initialized before by | ||
489 | the caller. | ||
490 | |||
491 | The method parameter is the compression method. It must be Z_DEFLATED in | ||
492 | this version of the library. | ||
493 | |||
494 | The windowBits parameter is the base two logarithm of the window size | ||
495 | (the size of the history buffer). It should be in the range 8..15 for this | ||
496 | version of the library. Larger values of this parameter result in better | ||
497 | compression at the expense of memory usage. The default value is 15 if | ||
498 | deflateInit is used instead. | ||
499 | |||
500 | windowBits can also be -8..-15 for raw deflate. In this case, -windowBits | ||
501 | determines the window size. deflate() will then generate raw deflate data | ||
502 | with no zlib header or trailer, and will not compute an adler32 check value. | ||
503 | |||
504 | windowBits can also be greater than 15 for optional gzip encoding. Add | ||
505 | 16 to windowBits to write a simple gzip header and trailer around the | ||
506 | compressed data instead of a zlib wrapper. The gzip header will have no | ||
507 | file name, no extra data, no comment, no modification time (set to zero), | ||
508 | no header crc, and the operating system will be set to 255 (unknown). If a | ||
509 | gzip stream is being written, strm->adler is a crc32 instead of an adler32. | ||
510 | |||
511 | The memLevel parameter specifies how much memory should be allocated | ||
512 | for the internal compression state. memLevel=1 uses minimum memory but | ||
513 | is slow and reduces compression ratio; memLevel=9 uses maximum memory | ||
514 | for optimal speed. The default value is 8. See zconf.h for total memory | ||
515 | usage as a function of windowBits and memLevel. | ||
516 | |||
517 | The strategy parameter is used to tune the compression algorithm. Use the | ||
518 | value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a | ||
519 | filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no | ||
520 | string match), or Z_RLE to limit match distances to one (run-length | ||
521 | encoding). Filtered data consists mostly of small values with a somewhat | ||
522 | random distribution. In this case, the compression algorithm is tuned to | ||
523 | compress them better. The effect of Z_FILTERED is to force more Huffman | ||
524 | coding and less string matching; it is somewhat intermediate between | ||
525 | Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as | ||
526 | Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy | ||
527 | parameter only affects the compression ratio but not the correctness of the | ||
528 | compressed output even if it is not set appropriately. Z_FIXED prevents the | ||
529 | use of dynamic Huffman codes, allowing for a simpler decoder for special | ||
530 | applications. | ||
531 | |||
532 | deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | ||
533 | memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid | ||
534 | method). msg is set to null if there is no error message. deflateInit2 does | ||
535 | not perform any compression: this will be done by deflate(). | ||
536 | */ | ||
537 | |||
538 | ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, | ||
539 | const Bytef *dictionary, | ||
540 | uInt dictLength)); | ||
541 | /* | ||
542 | Initializes the compression dictionary from the given byte sequence | ||
543 | without producing any compressed output. This function must be called | ||
544 | immediately after deflateInit, deflateInit2 or deflateReset, before any | ||
545 | call of deflate. The compressor and decompressor must use exactly the same | ||
546 | dictionary (see inflateSetDictionary). | ||
547 | |||
548 | The dictionary should consist of strings (byte sequences) that are likely | ||
549 | to be encountered later in the data to be compressed, with the most commonly | ||
550 | used strings preferably put towards the end of the dictionary. Using a | ||
551 | dictionary is most useful when the data to be compressed is short and can be | ||
552 | predicted with good accuracy; the data can then be compressed better than | ||
553 | with the default empty dictionary. | ||
554 | |||
555 | Depending on the size of the compression data structures selected by | ||
556 | deflateInit or deflateInit2, a part of the dictionary may in effect be | ||
557 | discarded, for example if the dictionary is larger than the window size in | ||
558 | deflate or deflate2. Thus the strings most likely to be useful should be | ||
559 | put at the end of the dictionary, not at the front. In addition, the | ||
560 | current implementation of deflate will use at most the window size minus | ||
561 | 262 bytes of the provided dictionary. | ||
562 | |||
563 | Upon return of this function, strm->adler is set to the adler32 value | ||
564 | of the dictionary; the decompressor may later use this value to determine | ||
565 | which dictionary has been used by the compressor. (The adler32 value | ||
566 | applies to the whole dictionary even if only a subset of the dictionary is | ||
567 | actually used by the compressor.) If a raw deflate was requested, then the | ||
568 | adler32 value is not computed and strm->adler is not set. | ||
569 | |||
570 | deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a | ||
571 | parameter is invalid (such as NULL dictionary) or the stream state is | ||
572 | inconsistent (for example if deflate has already been called for this stream | ||
573 | or if the compression method is bsort). deflateSetDictionary does not | ||
574 | perform any compression: this will be done by deflate(). | ||
575 | */ | ||
576 | |||
577 | ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, | ||
578 | z_streamp source)); | ||
579 | /* | ||
580 | Sets the destination stream as a complete copy of the source stream. | ||
581 | |||
582 | This function can be useful when several compression strategies will be | ||
583 | tried, for example when there are several ways of pre-processing the input | ||
584 | data with a filter. The streams that will be discarded should then be freed | ||
585 | by calling deflateEnd. Note that deflateCopy duplicates the internal | ||
586 | compression state which can be quite large, so this strategy is slow and | ||
587 | can consume lots of memory. | ||
588 | |||
589 | deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not | ||
590 | enough memory, Z_STREAM_ERROR if the source stream state was inconsistent | ||
591 | (such as zalloc being NULL). msg is left unchanged in both source and | ||
592 | destination. | ||
593 | */ | ||
594 | |||
595 | ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); | ||
596 | /* | ||
597 | This function is equivalent to deflateEnd followed by deflateInit, | ||
598 | but does not free and reallocate all the internal compression state. | ||
599 | The stream will keep the same compression level and any other attributes | ||
600 | that may have been set by deflateInit2. | ||
601 | |||
602 | deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source | ||
603 | stream state was inconsistent (such as zalloc or state being NULL). | ||
604 | */ | ||
605 | |||
606 | ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, | ||
607 | int level, | ||
608 | int strategy)); | ||
609 | /* | ||
610 | Dynamically update the compression level and compression strategy. The | ||
611 | interpretation of level and strategy is as in deflateInit2. This can be | ||
612 | used to switch between compression and straight copy of the input data, or | ||
613 | to switch to a different kind of input data requiring a different | ||
614 | strategy. If the compression level is changed, the input available so far | ||
615 | is compressed with the old level (and may be flushed); the new level will | ||
616 | take effect only at the next call of deflate(). | ||
617 | |||
618 | Before the call of deflateParams, the stream state must be set as for | ||
619 | a call of deflate(), since the currently available input may have to | ||
620 | be compressed and flushed. In particular, strm->avail_out must be non-zero. | ||
621 | |||
622 | deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source | ||
623 | stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR | ||
624 | if strm->avail_out was zero. | ||
625 | */ | ||
626 | |||
627 | ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, | ||
628 | int good_length, | ||
629 | int max_lazy, | ||
630 | int nice_length, | ||
631 | int max_chain)); | ||
632 | /* | ||
633 | Fine tune deflate's internal compression parameters. This should only be | ||
634 | used by someone who understands the algorithm used by zlib's deflate for | ||
635 | searching for the best matching string, and even then only by the most | ||
636 | fanatic optimizer trying to squeeze out the last compressed bit for their | ||
637 | specific input data. Read the deflate.c source code for the meaning of the | ||
638 | max_lazy, good_length, nice_length, and max_chain parameters. | ||
639 | |||
640 | deflateTune() can be called after deflateInit() or deflateInit2(), and | ||
641 | returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. | ||
642 | */ | ||
643 | |||
644 | ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, | ||
645 | uLong sourceLen)); | ||
646 | /* | ||
647 | deflateBound() returns an upper bound on the compressed size after | ||
648 | deflation of sourceLen bytes. It must be called after deflateInit() | ||
649 | or deflateInit2(). This would be used to allocate an output buffer | ||
650 | for deflation in a single pass, and so would be called before deflate(). | ||
651 | */ | ||
652 | |||
653 | ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, | ||
654 | int bits, | ||
655 | int value)); | ||
656 | /* | ||
657 | deflatePrime() inserts bits in the deflate output stream. The intent | ||
658 | is that this function is used to start off the deflate output with the | ||
659 | bits leftover from a previous deflate stream when appending to it. As such, | ||
660 | this function can only be used for raw deflate, and must be used before the | ||
661 | first deflate() call after a deflateInit2() or deflateReset(). bits must be | ||
662 | less than or equal to 16, and that many of the least significant bits of | ||
663 | value will be inserted in the output. | ||
664 | |||
665 | deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source | ||
666 | stream state was inconsistent. | ||
667 | */ | ||
668 | |||
669 | ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, | ||
670 | gz_headerp head)); | ||
671 | /* | ||
672 | deflateSetHeader() provides gzip header information for when a gzip | ||
673 | stream is requested by deflateInit2(). deflateSetHeader() may be called | ||
674 | after deflateInit2() or deflateReset() and before the first call of | ||
675 | deflate(). The text, time, os, extra field, name, and comment information | ||
676 | in the provided gz_header structure are written to the gzip header (xflag is | ||
677 | ignored -- the extra flags are set according to the compression level). The | ||
678 | caller must assure that, if not Z_NULL, name and comment are terminated with | ||
679 | a zero byte, and that if extra is not Z_NULL, that extra_len bytes are | ||
680 | available there. If hcrc is true, a gzip header crc is included. Note that | ||
681 | the current versions of the command-line version of gzip (up through version | ||
682 | 1.3.x) do not support header crc's, and will report that it is a "multi-part | ||
683 | gzip file" and give up. | ||
684 | |||
685 | If deflateSetHeader is not used, the default gzip header has text false, | ||
686 | the time set to zero, and os set to 255, with no extra, name, or comment | ||
687 | fields. The gzip header is returned to the default state by deflateReset(). | ||
688 | |||
689 | deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source | ||
690 | stream state was inconsistent. | ||
691 | */ | ||
692 | |||
693 | /* | ||
694 | ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, | ||
695 | int windowBits)); | ||
696 | |||
697 | This is another version of inflateInit with an extra parameter. The | ||
698 | fields next_in, avail_in, zalloc, zfree and opaque must be initialized | ||
699 | before by the caller. | ||
700 | |||
701 | The windowBits parameter is the base two logarithm of the maximum window | ||
702 | size (the size of the history buffer). It should be in the range 8..15 for | ||
703 | this version of the library. The default value is 15 if inflateInit is used | ||
704 | instead. windowBits must be greater than or equal to the windowBits value | ||
705 | provided to deflateInit2() while compressing, or it must be equal to 15 if | ||
706 | deflateInit2() was not used. If a compressed stream with a larger window | ||
707 | size is given as input, inflate() will return with the error code | ||
708 | Z_DATA_ERROR instead of trying to allocate a larger window. | ||
709 | |||
710 | windowBits can also be -8..-15 for raw inflate. In this case, -windowBits | ||
711 | determines the window size. inflate() will then process raw deflate data, | ||
712 | not looking for a zlib or gzip header, not generating a check value, and not | ||
713 | looking for any check values for comparison at the end of the stream. This | ||
714 | is for use with other formats that use the deflate compressed data format | ||
715 | such as zip. Those formats provide their own check values. If a custom | ||
716 | format is developed using the raw deflate format for compressed data, it is | ||
717 | recommended that a check value such as an adler32 or a crc32 be applied to | ||
718 | the uncompressed data as is done in the zlib, gzip, and zip formats. For | ||
719 | most applications, the zlib format should be used as is. Note that comments | ||
720 | above on the use in deflateInit2() applies to the magnitude of windowBits. | ||
721 | |||
722 | windowBits can also be greater than 15 for optional gzip decoding. Add | ||
723 | 32 to windowBits to enable zlib and gzip decoding with automatic header | ||
724 | detection, or add 16 to decode only the gzip format (the zlib format will | ||
725 | return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is | ||
726 | a crc32 instead of an adler32. | ||
727 | |||
728 | inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | ||
729 | memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg | ||
730 | is set to null if there is no error message. inflateInit2 does not perform | ||
731 | any decompression apart from reading the zlib header if present: this will | ||
732 | be done by inflate(). (So next_in and avail_in may be modified, but next_out | ||
733 | and avail_out are unchanged.) | ||
734 | */ | ||
735 | |||
736 | ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, | ||
737 | const Bytef *dictionary, | ||
738 | uInt dictLength)); | ||
739 | /* | ||
740 | Initializes the decompression dictionary from the given uncompressed byte | ||
741 | sequence. This function must be called immediately after a call of inflate, | ||
742 | if that call returned Z_NEED_DICT. The dictionary chosen by the compressor | ||
743 | can be determined from the adler32 value returned by that call of inflate. | ||
744 | The compressor and decompressor must use exactly the same dictionary (see | ||
745 | deflateSetDictionary). For raw inflate, this function can be called | ||
746 | immediately after inflateInit2() or inflateReset() and before any call of | ||
747 | inflate() to set the dictionary. The application must insure that the | ||
748 | dictionary that was used for compression is provided. | ||
749 | |||
750 | inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a | ||
751 | parameter is invalid (such as NULL dictionary) or the stream state is | ||
752 | inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the | ||
753 | expected one (incorrect adler32 value). inflateSetDictionary does not | ||
754 | perform any decompression: this will be done by subsequent calls of | ||
755 | inflate(). | ||
756 | */ | ||
757 | |||
758 | ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); | ||
759 | /* | ||
760 | Skips invalid compressed data until a full flush point (see above the | ||
761 | description of deflate with Z_FULL_FLUSH) can be found, or until all | ||
762 | available input is skipped. No output is provided. | ||
763 | |||
764 | inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR | ||
765 | if no more input was provided, Z_DATA_ERROR if no flush point has been found, | ||
766 | or Z_STREAM_ERROR if the stream structure was inconsistent. In the success | ||
767 | case, the application may save the current current value of total_in which | ||
768 | indicates where valid compressed data was found. In the error case, the | ||
769 | application may repeatedly call inflateSync, providing more input each time, | ||
770 | until success or end of the input data. | ||
771 | */ | ||
772 | |||
773 | ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, | ||
774 | z_streamp source)); | ||
775 | /* | ||
776 | Sets the destination stream as a complete copy of the source stream. | ||
777 | |||
778 | This function can be useful when randomly accessing a large stream. The | ||
779 | first pass through the stream can periodically record the inflate state, | ||
780 | allowing restarting inflate at those points when randomly accessing the | ||
781 | stream. | ||
782 | |||
783 | inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not | ||
784 | enough memory, Z_STREAM_ERROR if the source stream state was inconsistent | ||
785 | (such as zalloc being NULL). msg is left unchanged in both source and | ||
786 | destination. | ||
787 | */ | ||
788 | |||
789 | ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); | ||
790 | /* | ||
791 | This function is equivalent to inflateEnd followed by inflateInit, | ||
792 | but does not free and reallocate all the internal decompression state. | ||
793 | The stream will keep attributes that may have been set by inflateInit2. | ||
794 | |||
795 | inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source | ||
796 | stream state was inconsistent (such as zalloc or state being NULL). | ||
797 | */ | ||
798 | |||
799 | ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, | ||
800 | int bits, | ||
801 | int value)); | ||
802 | /* | ||
803 | This function inserts bits in the inflate input stream. The intent is | ||
804 | that this function is used to start inflating at a bit position in the | ||
805 | middle of a byte. The provided bits will be used before any bytes are used | ||
806 | from next_in. This function should only be used with raw inflate, and | ||
807 | should be used before the first inflate() call after inflateInit2() or | ||
808 | inflateReset(). bits must be less than or equal to 16, and that many of the | ||
809 | least significant bits of value will be inserted in the input. | ||
810 | |||
811 | inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source | ||
812 | stream state was inconsistent. | ||
813 | */ | ||
814 | |||
815 | ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, | ||
816 | gz_headerp head)); | ||
817 | /* | ||
818 | inflateGetHeader() requests that gzip header information be stored in the | ||
819 | provided gz_header structure. inflateGetHeader() may be called after | ||
820 | inflateInit2() or inflateReset(), and before the first call of inflate(). | ||
821 | As inflate() processes the gzip stream, head->done is zero until the header | ||
822 | is completed, at which time head->done is set to one. If a zlib stream is | ||
823 | being decoded, then head->done is set to -1 to indicate that there will be | ||
824 | no gzip header information forthcoming. Note that Z_BLOCK can be used to | ||
825 | force inflate() to return immediately after header processing is complete | ||
826 | and before any actual data is decompressed. | ||
827 | |||
828 | The text, time, xflags, and os fields are filled in with the gzip header | ||
829 | contents. hcrc is set to true if there is a header CRC. (The header CRC | ||
830 | was valid if done is set to one.) If extra is not Z_NULL, then extra_max | ||
831 | contains the maximum number of bytes to write to extra. Once done is true, | ||
832 | extra_len contains the actual extra field length, and extra contains the | ||
833 | extra field, or that field truncated if extra_max is less than extra_len. | ||
834 | If name is not Z_NULL, then up to name_max characters are written there, | ||
835 | terminated with a zero unless the length is greater than name_max. If | ||
836 | comment is not Z_NULL, then up to comm_max characters are written there, | ||
837 | terminated with a zero unless the length is greater than comm_max. When | ||
838 | any of extra, name, or comment are not Z_NULL and the respective field is | ||
839 | not present in the header, then that field is set to Z_NULL to signal its | ||
840 | absence. This allows the use of deflateSetHeader() with the returned | ||
841 | structure to duplicate the header. However if those fields are set to | ||
842 | allocated memory, then the application will need to save those pointers | ||
843 | elsewhere so that they can be eventually freed. | ||
844 | |||
845 | If inflateGetHeader is not used, then the header information is simply | ||
846 | discarded. The header is always checked for validity, including the header | ||
847 | CRC if present. inflateReset() will reset the process to discard the header | ||
848 | information. The application would need to call inflateGetHeader() again to | ||
849 | retrieve the header from the next gzip stream. | ||
850 | |||
851 | inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source | ||
852 | stream state was inconsistent. | ||
853 | */ | ||
854 | |||
855 | /* | ||
856 | ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, | ||
857 | unsigned char FAR *window)); | ||
858 | |||
859 | Initialize the internal stream state for decompression using inflateBack() | ||
860 | calls. The fields zalloc, zfree and opaque in strm must be initialized | ||
861 | before the call. If zalloc and zfree are Z_NULL, then the default library- | ||
862 | derived memory allocation routines are used. windowBits is the base two | ||
863 | logarithm of the window size, in the range 8..15. window is a caller | ||
864 | supplied buffer of that size. Except for special applications where it is | ||
865 | assured that deflate was used with small window sizes, windowBits must be 15 | ||
866 | and a 32K byte window must be supplied to be able to decompress general | ||
867 | deflate streams. | ||
868 | |||
869 | See inflateBack() for the usage of these routines. | ||
870 | |||
871 | inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of | ||
872 | the paramaters are invalid, Z_MEM_ERROR if the internal state could not | ||
873 | be allocated, or Z_VERSION_ERROR if the version of the library does not | ||
874 | match the version of the header file. | ||
875 | */ | ||
876 | |||
877 | typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *)); | ||
878 | typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); | ||
879 | |||
880 | ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, | ||
881 | in_func in, void FAR *in_desc, | ||
882 | out_func out, void FAR *out_desc)); | ||
883 | /* | ||
884 | inflateBack() does a raw inflate with a single call using a call-back | ||
885 | interface for input and output. This is more efficient than inflate() for | ||
886 | file i/o applications in that it avoids copying between the output and the | ||
887 | sliding window by simply making the window itself the output buffer. This | ||
888 | function trusts the application to not change the output buffer passed by | ||
889 | the output function, at least until inflateBack() returns. | ||
890 | |||
891 | inflateBackInit() must be called first to allocate the internal state | ||
892 | and to initialize the state with the user-provided window buffer. | ||
893 | inflateBack() may then be used multiple times to inflate a complete, raw | ||
894 | deflate stream with each call. inflateBackEnd() is then called to free | ||
895 | the allocated state. | ||
896 | |||
897 | A raw deflate stream is one with no zlib or gzip header or trailer. | ||
898 | This routine would normally be used in a utility that reads zip or gzip | ||
899 | files and writes out uncompressed files. The utility would decode the | ||
900 | header and process the trailer on its own, hence this routine expects | ||
901 | only the raw deflate stream to decompress. This is different from the | ||
902 | normal behavior of inflate(), which expects either a zlib or gzip header and | ||
903 | trailer around the deflate stream. | ||
904 | |||
905 | inflateBack() uses two subroutines supplied by the caller that are then | ||
906 | called by inflateBack() for input and output. inflateBack() calls those | ||
907 | routines until it reads a complete deflate stream and writes out all of the | ||
908 | uncompressed data, or until it encounters an error. The function's | ||
909 | parameters and return types are defined above in the in_func and out_func | ||
910 | typedefs. inflateBack() will call in(in_desc, &buf) which should return the | ||
911 | number of bytes of provided input, and a pointer to that input in buf. If | ||
912 | there is no input available, in() must return zero--buf is ignored in that | ||
913 | case--and inflateBack() will return a buffer error. inflateBack() will call | ||
914 | out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out() | ||
915 | should return zero on success, or non-zero on failure. If out() returns | ||
916 | non-zero, inflateBack() will return with an error. Neither in() nor out() | ||
917 | are permitted to change the contents of the window provided to | ||
918 | inflateBackInit(), which is also the buffer that out() uses to write from. | ||
919 | The length written by out() will be at most the window size. Any non-zero | ||
920 | amount of input may be provided by in(). | ||
921 | |||
922 | For convenience, inflateBack() can be provided input on the first call by | ||
923 | setting strm->next_in and strm->avail_in. If that input is exhausted, then | ||
924 | in() will be called. Therefore strm->next_in must be initialized before | ||
925 | calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called | ||
926 | immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in | ||
927 | must also be initialized, and then if strm->avail_in is not zero, input will | ||
928 | initially be taken from strm->next_in[0 .. strm->avail_in - 1]. | ||
929 | |||
930 | The in_desc and out_desc parameters of inflateBack() is passed as the | ||
931 | first parameter of in() and out() respectively when they are called. These | ||
932 | descriptors can be optionally used to pass any information that the caller- | ||
933 | supplied in() and out() functions need to do their job. | ||
934 | |||
935 | On return, inflateBack() will set strm->next_in and strm->avail_in to | ||
936 | pass back any unused input that was provided by the last in() call. The | ||
937 | return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR | ||
938 | if in() or out() returned an error, Z_DATA_ERROR if there was a format | ||
939 | error in the deflate stream (in which case strm->msg is set to indicate the | ||
940 | nature of the error), or Z_STREAM_ERROR if the stream was not properly | ||
941 | initialized. In the case of Z_BUF_ERROR, an input or output error can be | ||
942 | distinguished using strm->next_in which will be Z_NULL only if in() returned | ||
943 | an error. If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to | ||
944 | out() returning non-zero. (in() will always be called before out(), so | ||
945 | strm->next_in is assured to be defined if out() returns non-zero.) Note | ||
946 | that inflateBack() cannot return Z_OK. | ||
947 | */ | ||
948 | |||
949 | ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); | ||
950 | /* | ||
951 | All memory allocated by inflateBackInit() is freed. | ||
952 | |||
953 | inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream | ||
954 | state was inconsistent. | ||
955 | */ | ||
956 | |||
957 | ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); | ||
958 | /* Return flags indicating compile-time options. | ||
959 | |||
960 | Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: | ||
961 | 1.0: size of uInt | ||
962 | 3.2: size of uLong | ||
963 | 5.4: size of voidpf (pointer) | ||
964 | 7.6: size of z_off_t | ||
965 | |||
966 | Compiler, assembler, and debug options: | ||
967 | 8: DEBUG | ||
968 | 9: ASMV or ASMINF -- use ASM code | ||
969 | 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention | ||
970 | 11: 0 (reserved) | ||
971 | |||
972 | One-time table building (smaller code, but not thread-safe if true): | ||
973 | 12: BUILDFIXED -- build static block decoding tables when needed | ||
974 | 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed | ||
975 | 14,15: 0 (reserved) | ||
976 | |||
977 | Library content (indicates missing functionality): | ||
978 | 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking | ||
979 | deflate code when not needed) | ||
980 | 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect | ||
981 | and decode gzip streams (to avoid linking crc code) | ||
982 | 18-19: 0 (reserved) | ||
983 | |||
984 | Operation variations (changes in library functionality): | ||
985 | 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate | ||
986 | 21: FASTEST -- deflate algorithm with only one, lowest compression level | ||
987 | 22,23: 0 (reserved) | ||
988 | |||
989 | The sprintf variant used by gzprintf (zero is best): | ||
990 | 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format | ||
991 | 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! | ||
992 | 26: 0 = returns value, 1 = void -- 1 means inferred string length returned | ||
993 | |||
994 | Remainder: | ||
995 | 27-31: 0 (reserved) | ||
996 | */ | ||
997 | |||
998 | |||
999 | /* utility functions */ | ||
1000 | |||
1001 | /* | ||
1002 | The following utility functions are implemented on top of the | ||
1003 | basic stream-oriented functions. To simplify the interface, some | ||
1004 | default options are assumed (compression level and memory usage, | ||
1005 | standard memory allocation functions). The source code of these | ||
1006 | utility functions can easily be modified if you need special options. | ||
1007 | */ | ||
1008 | |||
1009 | ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, | ||
1010 | const Bytef *source, uLong sourceLen)); | ||
1011 | /* | ||
1012 | Compresses the source buffer into the destination buffer. sourceLen is | ||
1013 | the byte length of the source buffer. Upon entry, destLen is the total | ||
1014 | size of the destination buffer, which must be at least the value returned | ||
1015 | by compressBound(sourceLen). Upon exit, destLen is the actual size of the | ||
1016 | compressed buffer. | ||
1017 | This function can be used to compress a whole file at once if the | ||
1018 | input file is mmap'ed. | ||
1019 | compress returns Z_OK if success, Z_MEM_ERROR if there was not | ||
1020 | enough memory, Z_BUF_ERROR if there was not enough room in the output | ||
1021 | buffer. | ||
1022 | */ | ||
1023 | |||
1024 | ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, | ||
1025 | const Bytef *source, uLong sourceLen, | ||
1026 | int level)); | ||
1027 | /* | ||
1028 | Compresses the source buffer into the destination buffer. The level | ||
1029 | parameter has the same meaning as in deflateInit. sourceLen is the byte | ||
1030 | length of the source buffer. Upon entry, destLen is the total size of the | ||
1031 | destination buffer, which must be at least the value returned by | ||
1032 | compressBound(sourceLen). Upon exit, destLen is the actual size of the | ||
1033 | compressed buffer. | ||
1034 | |||
1035 | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough | ||
1036 | memory, Z_BUF_ERROR if there was not enough room in the output buffer, | ||
1037 | Z_STREAM_ERROR if the level parameter is invalid. | ||
1038 | */ | ||
1039 | |||
1040 | ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); | ||
1041 | /* | ||
1042 | compressBound() returns an upper bound on the compressed size after | ||
1043 | compress() or compress2() on sourceLen bytes. It would be used before | ||
1044 | a compress() or compress2() call to allocate the destination buffer. | ||
1045 | */ | ||
1046 | |||
1047 | ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, | ||
1048 | const Bytef *source, uLong sourceLen)); | ||
1049 | /* | ||
1050 | Decompresses the source buffer into the destination buffer. sourceLen is | ||
1051 | the byte length of the source buffer. Upon entry, destLen is the total | ||
1052 | size of the destination buffer, which must be large enough to hold the | ||
1053 | entire uncompressed data. (The size of the uncompressed data must have | ||
1054 | been saved previously by the compressor and transmitted to the decompressor | ||
1055 | by some mechanism outside the scope of this compression library.) | ||
1056 | Upon exit, destLen is the actual size of the compressed buffer. | ||
1057 | This function can be used to decompress a whole file at once if the | ||
1058 | input file is mmap'ed. | ||
1059 | |||
1060 | uncompress returns Z_OK if success, Z_MEM_ERROR if there was not | ||
1061 | enough memory, Z_BUF_ERROR if there was not enough room in the output | ||
1062 | buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. | ||
1063 | */ | ||
1064 | |||
1065 | |||
1066 | typedef voidp gzFile; | ||
1067 | |||
1068 | ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); | ||
1069 | /* | ||
1070 | Opens a gzip (.gz) file for reading or writing. The mode parameter | ||
1071 | is as in fopen ("rb" or "wb") but can also include a compression level | ||
1072 | ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for | ||
1073 | Huffman only compression as in "wb1h", or 'R' for run-length encoding | ||
1074 | as in "wb1R". (See the description of deflateInit2 for more information | ||
1075 | about the strategy parameter.) | ||
1076 | |||
1077 | gzopen can be used to read a file which is not in gzip format; in this | ||
1078 | case gzread will directly read from the file without decompression. | ||
1079 | |||
1080 | gzopen returns NULL if the file could not be opened or if there was | ||
1081 | insufficient memory to allocate the (de)compression state; errno | ||
1082 | can be checked to distinguish the two cases (if errno is zero, the | ||
1083 | zlib error is Z_MEM_ERROR). */ | ||
1084 | |||
1085 | ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); | ||
1086 | /* | ||
1087 | gzdopen() associates a gzFile with the file descriptor fd. File | ||
1088 | descriptors are obtained from calls like open, dup, creat, pipe or | ||
1089 | fileno (in the file has been previously opened with fopen). | ||
1090 | The mode parameter is as in gzopen. | ||
1091 | The next call of gzclose on the returned gzFile will also close the | ||
1092 | file descriptor fd, just like fclose(fdopen(fd), mode) closes the file | ||
1093 | descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode). | ||
1094 | gzdopen returns NULL if there was insufficient memory to allocate | ||
1095 | the (de)compression state. | ||
1096 | */ | ||
1097 | |||
1098 | ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); | ||
1099 | /* | ||
1100 | Dynamically update the compression level or strategy. See the description | ||
1101 | of deflateInit2 for the meaning of these parameters. | ||
1102 | gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not | ||
1103 | opened for writing. | ||
1104 | */ | ||
1105 | |||
1106 | ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); | ||
1107 | /* | ||
1108 | Reads the given number of uncompressed bytes from the compressed file. | ||
1109 | If the input file was not in gzip format, gzread copies the given number | ||
1110 | of bytes into the buffer. | ||
1111 | gzread returns the number of uncompressed bytes actually read (0 for | ||
1112 | end of file, -1 for error). */ | ||
1113 | |||
1114 | ZEXTERN int ZEXPORT gzwrite OF((gzFile file, | ||
1115 | voidpc buf, unsigned len)); | ||
1116 | /* | ||
1117 | Writes the given number of uncompressed bytes into the compressed file. | ||
1118 | gzwrite returns the number of uncompressed bytes actually written | ||
1119 | (0 in case of error). | ||
1120 | */ | ||
1121 | |||
1122 | ZEXTERN int ZEXPORTVA gzprintf OF((gzFile file, const char *format, ...)); | ||
1123 | /* | ||
1124 | Converts, formats, and writes the args to the compressed file under | ||
1125 | control of the format string, as in fprintf. gzprintf returns the number of | ||
1126 | uncompressed bytes actually written (0 in case of error). The number of | ||
1127 | uncompressed bytes written is limited to 4095. The caller should assure that | ||
1128 | this limit is not exceeded. If it is exceeded, then gzprintf() will return | ||
1129 | return an error (0) with nothing written. In this case, there may also be a | ||
1130 | buffer overflow with unpredictable consequences, which is possible only if | ||
1131 | zlib was compiled with the insecure functions sprintf() or vsprintf() | ||
1132 | because the secure snprintf() or vsnprintf() functions were not available. | ||
1133 | */ | ||
1134 | |||
1135 | ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); | ||
1136 | /* | ||
1137 | Writes the given null-terminated string to the compressed file, excluding | ||
1138 | the terminating null character. | ||
1139 | gzputs returns the number of characters written, or -1 in case of error. | ||
1140 | */ | ||
1141 | |||
1142 | ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); | ||
1143 | /* | ||
1144 | Reads bytes from the compressed file until len-1 characters are read, or | ||
1145 | a newline character is read and transferred to buf, or an end-of-file | ||
1146 | condition is encountered. The string is then terminated with a null | ||
1147 | character. | ||
1148 | gzgets returns buf, or Z_NULL in case of error. | ||
1149 | */ | ||
1150 | |||
1151 | ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); | ||
1152 | /* | ||
1153 | Writes c, converted to an unsigned char, into the compressed file. | ||
1154 | gzputc returns the value that was written, or -1 in case of error. | ||
1155 | */ | ||
1156 | |||
1157 | ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); | ||
1158 | /* | ||
1159 | Reads one byte from the compressed file. gzgetc returns this byte | ||
1160 | or -1 in case of end of file or error. | ||
1161 | */ | ||
1162 | |||
1163 | ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); | ||
1164 | /* | ||
1165 | Push one character back onto the stream to be read again later. | ||
1166 | Only one character of push-back is allowed. gzungetc() returns the | ||
1167 | character pushed, or -1 on failure. gzungetc() will fail if a | ||
1168 | character has been pushed but not read yet, or if c is -1. The pushed | ||
1169 | character will be discarded if the stream is repositioned with gzseek() | ||
1170 | or gzrewind(). | ||
1171 | */ | ||
1172 | |||
1173 | ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); | ||
1174 | /* | ||
1175 | Flushes all pending output into the compressed file. The parameter | ||
1176 | flush is as in the deflate() function. The return value is the zlib | ||
1177 | error number (see function gzerror below). gzflush returns Z_OK if | ||
1178 | the flush parameter is Z_FINISH and all output could be flushed. | ||
1179 | gzflush should be called only when strictly necessary because it can | ||
1180 | degrade compression. | ||
1181 | */ | ||
1182 | |||
1183 | ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, | ||
1184 | z_off_t offset, int whence)); | ||
1185 | /* | ||
1186 | Sets the starting position for the next gzread or gzwrite on the | ||
1187 | given compressed file. The offset represents a number of bytes in the | ||
1188 | uncompressed data stream. The whence parameter is defined as in lseek(2); | ||
1189 | the value SEEK_END is not supported. | ||
1190 | If the file is opened for reading, this function is emulated but can be | ||
1191 | extremely slow. If the file is opened for writing, only forward seeks are | ||
1192 | supported; gzseek then compresses a sequence of zeroes up to the new | ||
1193 | starting position. | ||
1194 | |||
1195 | gzseek returns the resulting offset location as measured in bytes from | ||
1196 | the beginning of the uncompressed stream, or -1 in case of error, in | ||
1197 | particular if the file is opened for writing and the new starting position | ||
1198 | would be before the current position. | ||
1199 | */ | ||
1200 | |||
1201 | ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); | ||
1202 | /* | ||
1203 | Rewinds the given file. This function is supported only for reading. | ||
1204 | |||
1205 | gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) | ||
1206 | */ | ||
1207 | |||
1208 | ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); | ||
1209 | /* | ||
1210 | Returns the starting position for the next gzread or gzwrite on the | ||
1211 | given compressed file. This position represents a number of bytes in the | ||
1212 | uncompressed data stream. | ||
1213 | |||
1214 | gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) | ||
1215 | */ | ||
1216 | |||
1217 | ZEXTERN int ZEXPORT gzeof OF((gzFile file)); | ||
1218 | /* | ||
1219 | Returns 1 when EOF has previously been detected reading the given | ||
1220 | input stream, otherwise zero. | ||
1221 | */ | ||
1222 | |||
1223 | ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); | ||
1224 | /* | ||
1225 | Returns 1 if file is being read directly without decompression, otherwise | ||
1226 | zero. | ||
1227 | */ | ||
1228 | |||
1229 | ZEXTERN int ZEXPORT gzclose OF((gzFile file)); | ||
1230 | /* | ||
1231 | Flushes all pending output if necessary, closes the compressed file | ||
1232 | and deallocates all the (de)compression state. The return value is the zlib | ||
1233 | error number (see function gzerror below). | ||
1234 | */ | ||
1235 | |||
1236 | ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); | ||
1237 | /* | ||
1238 | Returns the error message for the last error which occurred on the | ||
1239 | given compressed file. errnum is set to zlib error number. If an | ||
1240 | error occurred in the file system and not in the compression library, | ||
1241 | errnum is set to Z_ERRNO and the application may consult errno | ||
1242 | to get the exact error code. | ||
1243 | */ | ||
1244 | |||
1245 | ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); | ||
1246 | /* | ||
1247 | Clears the error and end-of-file flags for file. This is analogous to the | ||
1248 | clearerr() function in stdio. This is useful for continuing to read a gzip | ||
1249 | file that is being written concurrently. | ||
1250 | */ | ||
1251 | |||
1252 | /* checksum functions */ | ||
1253 | |||
1254 | /* | ||
1255 | These functions are not related to compression but are exported | ||
1256 | anyway because they might be useful in applications using the | ||
1257 | compression library. | ||
1258 | */ | ||
1259 | |||
1260 | ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); | ||
1261 | /* | ||
1262 | Update a running Adler-32 checksum with the bytes buf[0..len-1] and | ||
1263 | return the updated checksum. If buf is NULL, this function returns | ||
1264 | the required initial value for the checksum. | ||
1265 | An Adler-32 checksum is almost as reliable as a CRC32 but can be computed | ||
1266 | much faster. Usage example: | ||
1267 | |||
1268 | uLong adler = adler32(0L, Z_NULL, 0); | ||
1269 | |||
1270 | while (read_buffer(buffer, length) != EOF) { | ||
1271 | adler = adler32(adler, buffer, length); | ||
1272 | } | ||
1273 | if (adler != original_adler) error(); | ||
1274 | */ | ||
1275 | |||
1276 | ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, | ||
1277 | z_off_t len2)); | ||
1278 | /* | ||
1279 | Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 | ||
1280 | and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for | ||
1281 | each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of | ||
1282 | seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. | ||
1283 | */ | ||
1284 | |||
1285 | ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); | ||
1286 | /* | ||
1287 | Update a running CRC-32 with the bytes buf[0..len-1] and return the | ||
1288 | updated CRC-32. If buf is NULL, this function returns the required initial | ||
1289 | value for the for the crc. Pre- and post-conditioning (one's complement) is | ||
1290 | performed within this function so it shouldn't be done by the application. | ||
1291 | Usage example: | ||
1292 | |||
1293 | uLong crc = crc32(0L, Z_NULL, 0); | ||
1294 | |||
1295 | while (read_buffer(buffer, length) != EOF) { | ||
1296 | crc = crc32(crc, buffer, length); | ||
1297 | } | ||
1298 | if (crc != original_crc) error(); | ||
1299 | */ | ||
1300 | |||
1301 | ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); | ||
1302 | |||
1303 | /* | ||
1304 | Combine two CRC-32 check values into one. For two sequences of bytes, | ||
1305 | seq1 and seq2 with lengths len1 and len2, CRC-32 check values were | ||
1306 | calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 | ||
1307 | check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and | ||
1308 | len2. | ||
1309 | */ | ||
1310 | |||
1311 | |||
1312 | /* various hacks, don't look :) */ | ||
1313 | |||
1314 | /* deflateInit and inflateInit are macros to allow checking the zlib version | ||
1315 | * and the compiler's view of z_stream: | ||
1316 | */ | ||
1317 | ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, | ||
1318 | const char *version, int stream_size)); | ||
1319 | ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, | ||
1320 | const char *version, int stream_size)); | ||
1321 | ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, | ||
1322 | int windowBits, int memLevel, | ||
1323 | int strategy, const char *version, | ||
1324 | int stream_size)); | ||
1325 | ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, | ||
1326 | const char *version, int stream_size)); | ||
1327 | ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, | ||
1328 | unsigned char FAR *window, | ||
1329 | const char *version, | ||
1330 | int stream_size)); | ||
1331 | #define deflateInit(strm, level) \ | ||
1332 | deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream)) | ||
1333 | #define inflateInit(strm) \ | ||
1334 | inflateInit_((strm), ZLIB_VERSION, sizeof(z_stream)) | ||
1335 | #define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ | ||
1336 | deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ | ||
1337 | (strategy), ZLIB_VERSION, sizeof(z_stream)) | ||
1338 | #define inflateInit2(strm, windowBits) \ | ||
1339 | inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream)) | ||
1340 | #define inflateBackInit(strm, windowBits, window) \ | ||
1341 | inflateBackInit_((strm), (windowBits), (window), \ | ||
1342 | ZLIB_VERSION, sizeof(z_stream)) | ||
1343 | |||
1344 | |||
1345 | #if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL) | ||
1346 | struct internal_state {int dummy;}; /* hack for buggy compilers */ | ||
1347 | #endif | ||
1348 | |||
1349 | ZEXTERN const char * ZEXPORT zError OF((int)); | ||
1350 | ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp z)); | ||
1351 | ZEXTERN const uLongf * ZEXPORT get_crc_table OF((void)); | ||
1352 | |||
1353 | #ifdef __cplusplus | ||
1354 | } | ||
1355 | #endif | ||
1356 | |||
1357 | #endif /* ZLIB_H */ | ||
diff --git a/apps/plugins/imageviewer/png/zutil.h b/apps/plugins/imageviewer/png/zutil.h deleted file mode 100644 index b7d5eff81b..0000000000 --- a/apps/plugins/imageviewer/png/zutil.h +++ /dev/null | |||
@@ -1,269 +0,0 @@ | |||
1 | /* zutil.h -- internal interface and configuration of the compression library | ||
2 | * Copyright (C) 1995-2005 Jean-loup Gailly. | ||
3 | * For conditions of distribution and use, see copyright notice in zlib.h | ||
4 | */ | ||
5 | |||
6 | /* WARNING: this file should *not* be used by applications. It is | ||
7 | part of the implementation of the compression library and is | ||
8 | subject to change. Applications should only use zlib.h. | ||
9 | */ | ||
10 | |||
11 | /* @(#) $Id$ */ | ||
12 | |||
13 | #ifndef ZUTIL_H | ||
14 | #define ZUTIL_H | ||
15 | |||
16 | #define ZLIB_INTERNAL | ||
17 | #include "zlib.h" | ||
18 | |||
19 | #ifdef STDC | ||
20 | # ifndef _WIN32_WCE | ||
21 | # include <stddef.h> | ||
22 | # endif | ||
23 | # include <string.h> | ||
24 | # include <stdlib.h> | ||
25 | #endif | ||
26 | #ifdef NO_ERRNO_H | ||
27 | # ifdef _WIN32_WCE | ||
28 | /* The Microsoft C Run-Time Library for Windows CE doesn't have | ||
29 | * errno. We define it as a global variable to simplify porting. | ||
30 | * Its value is always 0 and should not be used. We rename it to | ||
31 | * avoid conflict with other libraries that use the same workaround. | ||
32 | */ | ||
33 | # define errno z_errno | ||
34 | # endif | ||
35 | extern int errno; | ||
36 | #else | ||
37 | # ifndef _WIN32_WCE | ||
38 | # include <errno.h> | ||
39 | # endif | ||
40 | #endif | ||
41 | |||
42 | #ifndef local | ||
43 | # define local static | ||
44 | #endif | ||
45 | /* compile with -Dlocal if your debugger can't find static symbols */ | ||
46 | |||
47 | typedef unsigned char uch; | ||
48 | typedef uch FAR uchf; | ||
49 | typedef unsigned short ush; | ||
50 | typedef ush FAR ushf; | ||
51 | typedef unsigned long ulg; | ||
52 | |||
53 | extern const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ | ||
54 | /* (size given to avoid silly warnings with Visual C++) */ | ||
55 | |||
56 | #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] | ||
57 | |||
58 | #define ERR_RETURN(strm,err) \ | ||
59 | return (strm->msg = (char*)ERR_MSG(err), (err)) | ||
60 | /* To be used only when the state is known to be valid */ | ||
61 | |||
62 | /* common constants */ | ||
63 | |||
64 | #ifndef DEF_WBITS | ||
65 | # define DEF_WBITS MAX_WBITS | ||
66 | #endif | ||
67 | /* default windowBits for decompression. MAX_WBITS is for compression only */ | ||
68 | |||
69 | #if MAX_MEM_LEVEL >= 8 | ||
70 | # define DEF_MEM_LEVEL 8 | ||
71 | #else | ||
72 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL | ||
73 | #endif | ||
74 | /* default memLevel */ | ||
75 | |||
76 | #define STORED_BLOCK 0 | ||
77 | #define STATIC_TREES 1 | ||
78 | #define DYN_TREES 2 | ||
79 | /* The three kinds of block type */ | ||
80 | |||
81 | #define MIN_MATCH 3 | ||
82 | #define MAX_MATCH 258 | ||
83 | /* The minimum and maximum match lengths */ | ||
84 | |||
85 | #define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ | ||
86 | |||
87 | /* target dependencies */ | ||
88 | |||
89 | #if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) | ||
90 | # define OS_CODE 0x00 | ||
91 | # if defined(__TURBOC__) || defined(__BORLANDC__) | ||
92 | # if(__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) | ||
93 | /* Allow compilation with ANSI keywords only enabled */ | ||
94 | void _Cdecl farfree( void *block ); | ||
95 | void *_Cdecl farmalloc( unsigned long nbytes ); | ||
96 | # else | ||
97 | # include <alloc.h> | ||
98 | # endif | ||
99 | # else /* MSC or DJGPP */ | ||
100 | # include <malloc.h> | ||
101 | # endif | ||
102 | #endif | ||
103 | |||
104 | #ifdef AMIGA | ||
105 | # define OS_CODE 0x01 | ||
106 | #endif | ||
107 | |||
108 | #if defined(VAXC) || defined(VMS) | ||
109 | # define OS_CODE 0x02 | ||
110 | # define F_OPEN(name, mode) \ | ||
111 | fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") | ||
112 | #endif | ||
113 | |||
114 | #if defined(ATARI) || defined(atarist) | ||
115 | # define OS_CODE 0x05 | ||
116 | #endif | ||
117 | |||
118 | #ifdef OS2 | ||
119 | # define OS_CODE 0x06 | ||
120 | # ifdef M_I86 | ||
121 | #include <malloc.h> | ||
122 | # endif | ||
123 | #endif | ||
124 | |||
125 | #if defined(MACOS) || defined(TARGET_OS_MAC) | ||
126 | # define OS_CODE 0x07 | ||
127 | # if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os | ||
128 | # include <unix.h> /* for fdopen */ | ||
129 | # else | ||
130 | # ifndef fdopen | ||
131 | # define fdopen(fd,mode) NULL /* No fdopen() */ | ||
132 | # endif | ||
133 | # endif | ||
134 | #endif | ||
135 | |||
136 | #ifdef TOPS20 | ||
137 | # define OS_CODE 0x0a | ||
138 | #endif | ||
139 | |||
140 | #ifdef WIN32 | ||
141 | # ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */ | ||
142 | # define OS_CODE 0x0b | ||
143 | # endif | ||
144 | #endif | ||
145 | |||
146 | #ifdef __50SERIES /* Prime/PRIMOS */ | ||
147 | # define OS_CODE 0x0f | ||
148 | #endif | ||
149 | |||
150 | #if defined(_BEOS_) || defined(RISCOS) | ||
151 | # define fdopen(fd,mode) NULL /* No fdopen() */ | ||
152 | #endif | ||
153 | |||
154 | #if (defined(_MSC_VER) && (_MSC_VER > 600)) | ||
155 | # if defined(_WIN32_WCE) | ||
156 | # define fdopen(fd,mode) NULL /* No fdopen() */ | ||
157 | # ifndef _PTRDIFF_T_DEFINED | ||
158 | typedef int ptrdiff_t; | ||
159 | # define _PTRDIFF_T_DEFINED | ||
160 | # endif | ||
161 | # else | ||
162 | # define fdopen(fd,type) _fdopen(fd,type) | ||
163 | # endif | ||
164 | #endif | ||
165 | |||
166 | /* common defaults */ | ||
167 | |||
168 | #ifndef OS_CODE | ||
169 | # define OS_CODE 0x03 /* assume Unix */ | ||
170 | #endif | ||
171 | |||
172 | #ifndef F_OPEN | ||
173 | # define F_OPEN(name, mode) fopen((name), (mode)) | ||
174 | #endif | ||
175 | |||
176 | /* functions */ | ||
177 | |||
178 | #if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) | ||
179 | # ifndef HAVE_VSNPRINTF | ||
180 | # define HAVE_VSNPRINTF | ||
181 | # endif | ||
182 | #endif | ||
183 | #if defined(__CYGWIN__) | ||
184 | # ifndef HAVE_VSNPRINTF | ||
185 | # define HAVE_VSNPRINTF | ||
186 | # endif | ||
187 | #endif | ||
188 | #ifndef HAVE_VSNPRINTF | ||
189 | # ifdef MSDOS | ||
190 | /* vsnprintf may exist on some MS-DOS compilers (DJGPP?), | ||
191 | but for now we just assume it doesn't. */ | ||
192 | # define NO_vsnprintf | ||
193 | # endif | ||
194 | # ifdef __TURBOC__ | ||
195 | # define NO_vsnprintf | ||
196 | # endif | ||
197 | # ifdef WIN32 | ||
198 | /* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ | ||
199 | # if !defined(vsnprintf) && !defined(NO_vsnprintf) | ||
200 | # define vsnprintf _vsnprintf | ||
201 | # endif | ||
202 | # endif | ||
203 | # ifdef __SASC | ||
204 | # define NO_vsnprintf | ||
205 | # endif | ||
206 | #endif | ||
207 | #ifdef VMS | ||
208 | # define NO_vsnprintf | ||
209 | #endif | ||
210 | |||
211 | #if defined(pyr) | ||
212 | # define NO_MEMCPY | ||
213 | #endif | ||
214 | #if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) | ||
215 | /* Use our own functions for small and medium model with MSC <= 5.0. | ||
216 | * You may have to use the same strategy for Borland C (untested). | ||
217 | * The __SC__ check is for Symantec. | ||
218 | */ | ||
219 | # define NO_MEMCPY | ||
220 | #endif | ||
221 | #if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) | ||
222 | # define HAVE_MEMCPY | ||
223 | #endif | ||
224 | #ifdef HAVE_MEMCPY | ||
225 | # ifdef SMALL_MEDIUM /* MSDOS small or medium model */ | ||
226 | # define zmemcpy _fmemcpy | ||
227 | # define zmemcmp _fmemcmp | ||
228 | # define zmemzero(dest, len) _fmemset(dest, 0, len) | ||
229 | # else | ||
230 | # define zmemcpy memcpy | ||
231 | # define zmemcmp memcmp | ||
232 | # define zmemzero(dest, len) memset(dest, 0, len) | ||
233 | # endif | ||
234 | #else | ||
235 | extern void zmemcpy OF((Bytef* dest, const Bytef* source, uInt len)); | ||
236 | extern int zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); | ||
237 | extern void zmemzero OF((Bytef* dest, uInt len)); | ||
238 | #endif | ||
239 | |||
240 | /* Diagnostic functions */ | ||
241 | #ifdef DEBUG | ||
242 | # include <stdio.h> | ||
243 | extern int z_verbose; | ||
244 | extern void z_error OF((char *m)); | ||
245 | # define Assert(cond,msg) {if(!(cond)) z_error(msg);} | ||
246 | # define Trace(x) {if (z_verbose>=0) fprintf x ;} | ||
247 | # define Tracev(x) {if (z_verbose>0) fprintf x ;} | ||
248 | # define Tracevv(x) {if (z_verbose>1) fprintf x ;} | ||
249 | # define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} | ||
250 | # define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} | ||
251 | #else | ||
252 | # define Assert(cond,msg) | ||
253 | # define Trace(x) | ||
254 | # define Tracev(x) | ||
255 | # define Tracevv(x) | ||
256 | # define Tracec(c,x) | ||
257 | # define Tracecv(c,x) | ||
258 | #endif | ||
259 | |||
260 | |||
261 | voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size)); | ||
262 | void zcfree OF((voidpf opaque, voidpf ptr)); | ||
263 | |||
264 | #define ZALLOC(strm, items, size) \ | ||
265 | (*((strm)->zalloc))((strm)->opaque, (items), (size)) | ||
266 | #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) | ||
267 | #define TRY_FREE(s, p) {if (p) ZFREE(s, p);} | ||
268 | |||
269 | #endif /* ZUTIL_H */ | ||