12
12
#define SPNG_DISABLE_OPT
13
13
#include "tests/framac_stubs.h"
14
14
#else
15
- #include <zlib.h>
15
+ #ifdef SPNG_USE_MINIZ
16
+ #include <miniz.h>
17
+ #else
18
+ #include <zlib.h>
19
+ #endif
16
20
#endif
17
21
18
22
#ifdef SPNG_MULTITHREADING
@@ -317,7 +321,11 @@ static inline void spng__free(spng_ctx *ctx, void *ptr)
317
321
ctx -> alloc .free_fn (ptr );
318
322
}
319
323
324
+ #if defined(SPNG_USE_MINIZ )
325
+ static void * spng__zalloc (void * opaque , long unsigned items , long unsigned size )
326
+ #else
320
327
static void * spng__zalloc (void * opaque , unsigned items , unsigned size )
328
+ #endif
321
329
{
322
330
spng_ctx * ctx = opaque ;
323
331
@@ -344,10 +352,10 @@ static int spng__inflate_init(spng_ctx *ctx)
344
352
345
353
if (inflateInit (& ctx -> zstream ) != Z_OK ) return SPNG_EZLIB ;
346
354
347
- #if ZLIB_VERNUM >= 0x1290
355
+ #if ZLIB_VERNUM >= 0x1290 && !defined( SPNG_USE_MINIZ )
348
356
if (inflateValidate (& ctx -> zstream , ctx -> flags & SPNG_CTX_IGNORE_ADLER32 )) return SPNG_EZLIB ;
349
- #else
350
- #warning "zlib >= 1.2.11 is required for SPNG_CTX_IGNORE_ADLER32 "
357
+ #else /* This requires zlib >= 1.2.11 */
358
+ #warning "inflateValidate() not available, SPNG_CTX_IGNORE_ADLER32 will be ignored "
351
359
#endif
352
360
353
361
return 0 ;
@@ -595,7 +603,7 @@ static inline int read_header(spng_ctx *ctx, int *discard)
595
603
static int read_chunk_bytes (spng_ctx * ctx , uint32_t bytes )
596
604
{
597
605
if (ctx == NULL ) return 1 ;
598
- if (!bytes ) return 0 ;
606
+ if (!ctx -> cur_chunk_bytes_left || ! bytes ) return 1 ;
599
607
if (bytes > ctx -> cur_chunk_bytes_left ) return 1 ; /* XXX: more specific error? */
600
608
601
609
int ret ;
@@ -619,7 +627,7 @@ static int read_chunk_bytes(spng_ctx *ctx, uint32_t bytes)
619
627
static int read_chunk_bytes2 (spng_ctx * ctx , void * out , uint32_t bytes )
620
628
{
621
629
if (ctx == NULL ) return 1 ;
622
- if (!bytes ) return 0 ;
630
+ if (!ctx -> cur_chunk_bytes_left || ! bytes ) return 1 ;
623
631
if (bytes > ctx -> cur_chunk_bytes_left ) return 1 ; /* XXX: more specific error? */
624
632
625
633
int ret ;
@@ -726,49 +734,54 @@ static int spng__inflate_stream(spng_ctx *ctx, char **out, size_t *len, int extr
726
734
stream -> avail_out = size ;
727
735
stream -> next_out = buf ;
728
736
729
- do
737
+ while ( ret != Z_STREAM_END )
730
738
{
731
- if (ret != Z_OK )
732
- {
733
- spng__free (ctx , buf );
734
- return SPNG_EZLIB ;
735
- }
739
+ ret = inflate (stream , 0 );
736
740
737
- if (!stream -> avail_out )
738
- {
739
- /* overflow or reached chunk/cache limit */
740
- if ( (2 > SIZE_MAX / size ) || (size > max / 2 ) ) goto mem ;
741
+ if (ret == Z_OK ) continue ;
741
742
742
- size *= 2 ;
743
+ if ( ret == Z_STREAM_END ) break ;
743
744
744
- t = spng__realloc (ctx , buf , size );
745
- if (t == NULL ) goto mem ;
745
+ if (ret == Z_BUF_ERROR )
746
+ {
747
+ if (!stream -> avail_out ) /* Resize buffer */
748
+ {
749
+ /* overflow or reached chunk/cache limit */
750
+ if ( (2 > SIZE_MAX / size ) || (size > max / 2 ) ) goto mem ;
746
751
747
- buf = t ;
752
+ size *= 2 ;
748
753
749
- stream -> avail_out = size / 2 ;
750
- stream -> next_out = (unsigned char * )buf + size / 2 ;
751
- }
754
+ t = spng__realloc (ctx , buf , size );
755
+ if (t == NULL ) goto mem ;
752
756
753
- if (!stream -> avail_in ) /* Read more chunk bytes */
754
- {
755
- read_size = ctx -> cur_chunk_bytes_left ;
756
- if (ctx -> streaming && read_size > SPNG_READ_SIZE ) read_size = SPNG_READ_SIZE ;
757
+ buf = t ;
757
758
758
- ret = read_chunk_bytes (ctx , read_size );
759
- if (ret )
760
- {
761
- spng__free (ctx , buf );
762
- return ret ;
759
+ stream -> avail_out = size / 2 ;
760
+ stream -> next_out = (unsigned char * )buf + size / 2 ;
763
761
}
764
762
765
- stream -> avail_in = read_size ;
766
- stream -> next_in = ctx -> data ;
767
- }
763
+ if (!stream -> avail_in ) /* Read more chunk bytes */
764
+ {
765
+ read_size = ctx -> cur_chunk_bytes_left ;
766
+ if (ctx -> streaming && read_size > SPNG_READ_SIZE ) read_size = SPNG_READ_SIZE ;
768
767
769
- ret = inflate (stream , Z_SYNC_FLUSH );
768
+ ret = read_chunk_bytes (ctx , read_size );
769
+ if (ret )
770
+ {
771
+ spng__free (ctx , buf );
772
+ return ret ;
773
+ }
770
774
771
- }while (ret != Z_STREAM_END );
775
+ stream -> avail_in = read_size ;
776
+ stream -> next_in = ctx -> data ;
777
+ }
778
+ }
779
+ else
780
+ {
781
+ spng__free (ctx , buf );
782
+ return SPNG_EZLIB ;
783
+ }
784
+ }
772
785
773
786
size = stream -> total_out ;
774
787
@@ -833,33 +846,33 @@ static int read_scanline_bytes(spng_ctx *ctx, unsigned char *dest, size_t len)
833
846
{
834
847
if (ctx == NULL || dest == NULL ) return 1 ;
835
848
836
- int ret ;
849
+ int ret = Z_OK ;
837
850
uint32_t bytes_read ;
838
851
839
- ctx -> zstream .avail_out = len ;
840
- ctx -> zstream .next_out = dest ;
852
+ z_stream * zstream = & ctx -> zstream ;
841
853
842
- while (ctx -> zstream .avail_out != 0 )
854
+ zstream -> avail_out = len ;
855
+ zstream -> next_out = dest ;
856
+
857
+ while (zstream -> avail_out != 0 )
843
858
{
844
- if (ctx -> zstream .avail_in == 0 ) /* Need more IDAT bytes */
859
+ ret = inflate (& ctx -> zstream , 0 );
860
+
861
+ if (ret == Z_OK ) continue ;
862
+
863
+ if (ret == Z_STREAM_END ) /* Reached an end-marker */
864
+ {
865
+ if (zstream -> avail_out != 0 ) return SPNG_EIDAT_TOO_SHORT ;
866
+ }
867
+ else if (ret == Z_BUF_ERROR ) /* Read more IDAT bytes */
845
868
{
846
869
ret = read_idat_bytes (ctx , & bytes_read );
847
870
if (ret ) return ret ;
848
871
849
- ctx -> zstream .avail_in = bytes_read ;
850
- ctx -> zstream .next_in = ctx -> data ;
851
- }
852
-
853
- ret = inflate (& ctx -> zstream , Z_SYNC_FLUSH );
854
-
855
- if (ret != Z_OK )
856
- {
857
- if (ret == Z_STREAM_END ) /* zlib reached an end-marker */
858
- {
859
- if (ctx -> zstream .avail_out != 0 ) return SPNG_EIDAT_TOO_SHORT ;
860
- }
861
- else if (ret != Z_BUF_ERROR ) return SPNG_EIDAT_STREAM ;
872
+ zstream -> avail_in = bytes_read ;
873
+ zstream -> next_in = ctx -> data ;
862
874
}
875
+ else return SPNG_EIDAT_STREAM ;
863
876
}
864
877
865
878
return 0 ;
0 commit comments