From 02ad0efbc858a8f02c7878fe7a152358d2f08478 Mon Sep 17 00:00:00 2001 From: Andreas Dilger Date: Fri, 17 Jan 1997 01:34:35 -0600 Subject: [PATCH] Imported from libpng-0.90.tar --- pngchang.txt => CHANGES | 25 +- README | 168 +++++++++ pngtodo.txt => TODO | 12 +- example.c | 61 ++-- libpng.txt | 73 ++-- makefile.aco | 112 ++---- makefile.elf | 35 +- png.c | 146 ++++++-- png.h | 467 ++++++++++++++----------- pngconf.h | 170 +++++---- pngerror.c | 4 +- pngmem.c | 107 +----- pngpread.c | 682 ++++++++++++------------------------ pngrcb.c | 54 +-- pngread.c | 566 ++++++++++++------------------ pngrio.c | 24 +- pngrtran.c | 306 +++++++++------- pngrutil.c | 750 +++++++++++++++++++++++++++++++--------- pngtest.c | 36 +- pngtrans.c | 17 +- pngwio.c | 57 ++- pngwrite.c | 208 +++++------ pngwtran.c | 6 +- pngwutil.c | 333 +++++++++++------- readme.txt | 106 ------ 25 files changed, 2448 insertions(+), 2077 deletions(-) rename pngchang.txt => CHANGES (75%) create mode 100644 README rename pngtodo.txt => TODO (61%) delete mode 100644 readme.txt diff --git a/pngchang.txt b/CHANGES similarity index 75% rename from pngchang.txt rename to CHANGES index 8fc0956f..76e4d5f7 100644 --- a/pngchang.txt +++ b/CHANGES @@ -93,11 +93,30 @@ version 0.89 fixed gamma and background correction for paletted images, so png_correct_palette is not needed unless you are correcting an external palette (you will need to #define PNG_CORRECT_PALETTE_SUPPORTED - in pngconf.h) + in pngconf.h) - if nobody uses this, it may disappear in the future. fixed bug with Borland 64K memory allocation (Alexander Lehmann) - fixed bug in interlace handling (Smaraderagd, I think) + fixed bug in interlace handling (Smarasderagd, I think) added more error checking for writing and image to reduce invalid files separated read and write functions so that they won't both be linked into a binary when only reading or writing functionality is used new pngtest image also has interlacing and zTXt - updated dcumentation to reflect new API + updated documentation to reflect new API +version 0.90 + made CRC errors/warnings on critical and ancillary chunks configurable + libpng will use the zlib CRC routines by (compile-time) default + changed DOS small/medium model memory support - needs zlib 1.04 (Tim Wegner) + added external C++ wrapper statements to png.h (Gilles Dauphin) + allow PNG file to be read when some or all of file signature has already + been read from the beginning of the stream (this affects the size of + info_struct and invalidates all programs that use a shared libpng) + fixed png_filler() declarations + fixed? background color conversions + fixed order of error function pointers to match documentation + current chunk name is now available in png_struct to reduce the number + of nearly identical error messages (will simplify multi-lingual + support when available) + try to get ready for unknown-chunk callback functions: + - previously read critical chunks are flagged, so the chunk handling + routines can determine if the chunk is in the right place + - all chunk handling routines have the same prototypes, so we will + be able to handle all chunks via a callback mechanism diff --git a/README b/README new file mode 100644 index 00000000..000e5726 --- /dev/null +++ b/README @@ -0,0 +1,168 @@ +readme.txt - for libpng 0.90 + +This is the fourth beta version of libpng 1.0. The changes from +libpng-0.89 include bug fixes, a C++ wrapper for png.h, some +additions to the API, as well as internal changes to the library. + +** +Note that some of the changes to the png_info structure render +this version of the library binary incompatible with libpng-0.89 +if you are using a shared library. Re-compiling the application +should be enough to remove this problem. +** + +The additions to 0.89 include the ability to read from a PNG stream +which has had some (or all) of the signature bytes read by the +calling application. This also allows the reading of embedded PNG +streams that do not have the PNG file signature. As well, it is +now possible to set the library action on the detection of chunk +CRC errors. It is possible to set different actions based on +whether the error occurred in a critical or an ancillary chunk. + +The callback functions for the error/warning messages have changed +since the 0.88 release because their implementation was broken, +and it was thought best to change the API itself (which was only +introduced in libpng-0.88 itself) to alert the user to the change, +rather than mislead the user into thinking their application was +OK after re-compiling. This means that calls to png_set_message_fn() +no longer exist, because the previously suggested method of calling +them before png_read_init() or png_write_init() is now ineffective. + +The preferred method of setting the error and warning callbacks +has been incorporated into the allocation of the png_struct and +info_struct itself, which allow them to be safely used during the +initialization of the structure, as well as to keep the size of +the png_struct internal to the library, rather than at compile time +of the application. This will hopefully remove any problems with +dynamically linked libraries, and should be considered the preferred +method of creating these structures, although the previous +initialization API is still available for compatibility. See libpng.txt +for more information on the new API. + +The changes made to the library, and bugs fixed are based on discussions +on the PNG implementation mailing list +and not on material submitted to Guy. + +For a detailed description on using libpng, read libpng.txt. For +usage information and restrictions (what little they are) on libpng, +see png.h. For a description on using zlib (the compression library +used by libpng) and zlib's restrictions, see zlib.h + +I have included a general makefile, as well as several machine and compiler +specific ones, but you may have to modify one for your own needs. + +You should use zlib 1.0.4 or later to run this, but it MAY work with +versions as old as zlib 0.95. Even so, there are bugs in older zlib +versions which can cause the output of invalid compression streams for +some images. You will definitely need zlib 1.0.4 or later if you are +taking advantage of the MS-DOS "far" structure allocation for the small +and medium memory models. You should also note that zlib is a +compression library that is useful for more things than just PNG files. +You can use zlib as a drop-in replacement for fread() and fwrite() if +you are so inclined. + +zlib should be available at the same place that libpng is. +If not, it should be at ftp.uu.net in /graphics/png +Eventually, it will be at ftp.uu.net in /pub/archiving/zip/zlib + +You may also want a copy of the PNG specification. It is available +as an Internet Draft, and RFC, and a W3C Recommendation. Failing +these resources you can try ftp.uu.net in the /graphics/png directory. + +This code is currently being archived at ftp.uu.net in the +/graphics/png directory, and on CompuServe, Lib 20 (PNG SUPPORT) +at GO GRAPHSUP. If you can't find it in any of those places, +e-mail me, and I'll help you find it. + +If you have any code changes, requests, problems, etc., please e-mail +them to me. Also, I'd appreciate any make files or project files, +and any modifications you needed to make to get libpng to compile, +along with a #define variable to tell what compiler/system you are on. +If you needed to add transformations to libpng, or wish libpng would +provide the image in a different way, drop me a note (and code, if +possible), so I can consider supporting the transformation. +Finally, if you get any warning messages when compiling libpng +(note: not zlib), and they are easy to fix, I'd appreciate the +fix. Please mention "libpng" somewhere in the subject line. Thanks. + +This release was created and will be supported by myself (of course +based in a large way on Guy's earlier work), and the PNG group. + +adilger@enel.ucalgary.ca +png-implement@dworkin.wustl.edu + +You can reach Guy, the original libpng author, at (internet preferred): + +internet: schalnat@group42.com +CompuServe: 75501,1625 + +Please do not send general questions about PNG. Send them to +the address in the specification. At the same time, please do +not send libpng questions to that address, send them to me. I'll +get them in the end anyway. If you have a question about something +in the PNG specification that is related to using libpng, send it +to me. Send me any questions that start with "I was using libpng, +and ...". If in doubt, send questions to me. I'll bounce them +to others, if necessary. + +Please do not send suggestions on how to change PNG. We have +been discussing PNG for a couple years now, and it is official and +finished. If you have suggestions for libpng, however, I'll +gladly listen. Even if your suggestion is not used for version +1.0, it may be used later. + +Files in this distribution: + + CHANGES.txt => Description of changes between libpng versions + README => This file + TODO => Things not implemented in the current library + ansi2knr.c => Converts files to K&R style function declarations + build.bat => MS-DOS batch file for Borland compiler + descrip.mms => VMS project file + example.c => Example code for using libpng functions + libpng.txt => Description of libpng and its functions + makefile => Defualt makefile + makefile.aco => ACORN makefile + makefile.ama => Amiga makefile + makefile.atr => Atari makefile + makefile.bor => Borland makefile + makefile.dj2 => DJGPP 2 makefile + makefile.elf => Unix ELF makefile + makefile.knr => Makefile which calls ansi2knr to convert files + makefile.mip => MIPS makefile + makefile.msc => Microsoft C makefile + makefile.std => Standard Unix makefile + makefile.tc => Turbo C makefile + makevms.com => VMS make program + png.c => Basic interface functions common to library + png.h => Library function and interface declarations + pngconf.h => System specific library configuration + pngerror.c => Error/warning message I/O functions + pngmem.c => Memory handling functions + pngpread.c => Progressive reading functions + pngrcb.c => Read callback (data handling) low-level functions + pngread.c => Read data/helper high-level functions + pngrio.c => Lowest-level data read I/O functions + pngrtran.c => Read data transformation functions + pngrutil.c => Read data utility functions + pngtest.c => Library test program + pngtest.png => Library test sample image + pngtrans.c => Common data transformation functions + pngwio.c => Lowest-level write I/O functions + pngwrite.c => High-level write functions + pngwtran.c => Write data transformations + pngwutil.c => Write utility functions + +Good luck, and happy coding. + +-Andreas Eric Dilger + University of Calgary + Internet: adilger@enel.ucalgary.ca + Web: www-mddsp.enel.ucalgary.ca/People/adilger/ + +-Guy Eric Schalnat + Group 42, Inc. + Internet: schalnat@group42.com + CompuServe: 75501,1625 + Web: www.group42.com + diff --git a/pngtodo.txt b/TODO similarity index 61% rename from pngtodo.txt rename to TODO index 8b8089b7..fedeb0fc 100644 --- a/pngtodo.txt +++ b/TODO @@ -5,21 +5,19 @@ for 0.9 final bug fixes cHRM transformation better documentation + multi-lingual message support after 1.0 overlaying one image on top of another optional palette creation histogram creation text conversion between different code types - support for other chunks being defined (sCAl, the gIF series, - and others that people come up with). + support for application-defined chunk handlers + support for embedded PNG usage (MNG) pull writer better dithering keep up with public chunks - other compression libraries - more exotic interlace handling - better filtering (counting huffman bits? filter type inertia?) + better filter selection (counting huffman bits? filter type inertia?) C++ wrapper other languages (pascal, for one) - comments of > 64K - + comments of > 64K for DOS diff --git a/example.c b/example.c index 65e615d7..0903933a 100644 --- a/example.c +++ b/example.c @@ -14,45 +14,61 @@ #include -/* check to see if a file is a png file using png_check_sig() */ -int check_png(char * file_name) +/* Check to see if a file is a png file using png_check_sig(). + + If this call is successful, and you are going to keep the file + open, you should call png_set_sig_bytes_read(png_ptr, 8); + once you have created the png_ptr, so that libpng knows it + doesn't have to read the signature again. Make sure you don't + call png_set_sig_bytes_read() with more than 8 bytes read or + give it an incorrect number of bytes read, or you will either + have read too many bytes (your fault), or you are telling libpng + to read the wrong number of magic bytes (also your fault). */ +int check_png(char *file_name, FILE **fp) { - FILE *fp; char buf[8]; int ret; - fp = fopen(file_name, "rb"); + *fp = fopen(file_name, "rb"); if (!fp) return 0; - ret = fread(buf, 1, 8, fp); - fclose(fp); + ret = fread(buf, 1, 8, *fp); if (ret != 8) return 0; - ret = png_check_sig(buf, 8); + /* Check the signature starting at byte 0, and check all 8 bytes */ + ret = png_check_sig(buf, 0, 8); return (ret); } /* read a png file. You may want to return an error code if the read - fails (depending upon the failure). */ -void read_png(char *file_name) + fails (depending upon the failure). There are two "prototypes" given + here - one where we are given the filename, and we need to open the + file, and the other where we are given an open file (possibly with + some or all of the magic bytes read - see above) and an opened file + for reading. */ +------- prototype 1 ---------- +void read_png(char *file_name) /* We need to open the file */ { - FILE *fp; png_structp png_ptr; png_infop info_ptr; + FILE *fp; - /* open the file */ - fp = fopen(file_name, "rb"); - if (!fp) + if ((fp = fopen(file_name, "rb")) == NULL) return; +------- prototype 2 ---------- +void read_png(FILE *fp, unsigned int sig_read) /* file is already open */ +{ + png_structp png_ptr; + png_infop info_ptr; +------- only use one! -------- /* Create and initialize the png_struct with the desired error handler functions. If you want to use the default stderr and longjump method, you can supply NULL for the last three parameters. We also check that - the header file is compatible with the library version. - */ + the header file is compatible with the library version. */ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (void *)user_error_ptr, user_error_fn, user_warning_fn); @@ -88,7 +104,11 @@ void read_png(char *file_name) png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn); /* where user_io_ptr is a structure you want available to the callbacks */ - /* read the file information */ + /* if we have already read some of the signature from the beginning call */ + png_set_sig_bytes_read(png_ptr, sig_read); + + /* The call to png_read_info() gives us all of the information + from the PNG file before the first IDAT (image data chunk). */ png_read_info(png_ptr, info_ptr); /* set up the transformations you want. Note that these are @@ -120,7 +140,10 @@ void read_png(char *file_name) png_set_background(png_ptr, &my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); - /* tell libpng to handle the gamma conversion for you */ + /* tell libpng to handle the gamma conversion for you. We only + need the second call if the screen_gamma isn't the usual 2.2 + or if it is controllable by the user. It may also be a good + idea to allow the user to set the file gamma if it is unknown. */ if (info_ptr->valid & PNG_INFO_gAMA) png_set_gamma(png_ptr, screen_gamma, info_ptr->gamma); else @@ -398,10 +421,6 @@ void write_png(char *file_name, ... other image information ...) /* set up the output control if you are using standard C streams */ png_init_io(png_ptr, fp); - /* if you are using replacement message functions, here you would call */ - png_set_message_fn(png_ptr, (void *)msg_ptr, user_error_fn, user_warning_fn); - /* where msg_ptr is a structure you want available to the callbacks */ - /* set the file information here */ info_ptr->width = ; info_ptr->height = ; diff --git a/libpng.txt b/libpng.txt index 0abf51c0..5ff4d056 100644 --- a/libpng.txt +++ b/libpng.txt @@ -56,14 +56,14 @@ II. Structures There are two main structures that are important to libpng, png_struct and png_info. The first, png_struct, is an internal structure that will not, for the most part, be used by a user except as the first -variable passed to every png function call. +variable passed to every libpng function call. The png_info structure is designed to provide information about the png file. All of its fields are intended to be examined or modified by the user. See png.h for a good description of the png_info fields. png.h is also an invaluable reference for programming with libpng. -And while I'm on the topic, make sure you include the png header file: +And while I'm on the topic, make sure you include the libpng header file: #include @@ -86,11 +86,13 @@ file. Libpng provides a simple check to see if a file is a PNG file. To use it, pass in the first 1 to 8 bytes of the file, and it will return true or false (1 or 0) depending on whether the bytes could be part of a PNG file. Of course, the more bytes you pass in, the -greater the accuracy of the prediction. If you pass in more then -eight bytes, libpng will only look at the first eight bytes. However, -because libpng automatically checks the file header, this is not often -necessary, and you should pass a newly opened file pointer to libpng -when reading a file. +greater the accuracy of the prediction. + +If you are intending to keep the file pointer open for use in libpng, +you must ensure you don't read more than 8 bytes from the beginning +of the file, and you also have to make a call to png_set_sig_bytes_read() +with the number of bytes you read from the beginning. Libpng will +then only check the bytes (if any) that your program didn't read. (*): If you are not using the standard I/O functions, you will need to replace them with custom functions. See the discussion under @@ -102,12 +104,11 @@ Customizing libpng. return; } fread(header, 1, number, fp); - is_png = png_check_sig(header, number); + is_png = png_check_sig(header, 0, number); if (!is_png) { return; } - fclose(fp); Next, png_struct and png_info need to be allocated and initialized. In order to ensure that the size of these structures is correct even @@ -134,20 +135,20 @@ Changes to Libpng below regarding the old initialization functions. return; } -The error handling routines passed to png_create_read_struct() -are only necessary if you are not using the libpng supplied -error handling functions. When libpng encounters an error, -it expects to longjmp back to your routine. Therefore, you -will need to call setjmp and pass the jmpbuf field of your -png_struct. If you read the file from different routines, you -will need to update the jmpbuf field every time you enter a new -routine that will call a png_ function. See your documentation -of setjmp/longjmp for your compiler for more information on -setjmp/longjmp. See the discussion on libpng error handling -in the Customizing Libpng section below for more information on -the libpng error handling. If an error occurs, and libpng -longjmp's back to your setjmp, you will want to call -png_destroy_read_struct() to free any memory. +The error handling routines passed to png_create_read_struct() are only +necessary if you are not using the libpng supplied error handling +functions. When libpng encounters an error, it expects to longjmp back +to your routine. Therefore, you will need to call setjmp and pass the +jmpbuf field of your png_struct. If you read the file from different +routines, you will need to update the jmpbuf field every time you enter +a new routine that will call a png_ function. + +See your documentation of setjmp/longjmp for your compiler for more +information on setjmp/longjmp. See the discussion on libpng error +handling in the Customizing Libpng section below for more information on +the libpng error handling. If an error occurs, and libpng longjmp's +back to your setjmp, you will want to call png_destroy_read_struct() to +free any memory. if (setjmp(png_ptr->jmpbuf)) { @@ -165,10 +166,16 @@ Libpng section below. png_init_io(png_ptr, fp); +If you had previously opened the file and read any of the signature from +the beginning in order to see if this was a PNG file, you need to let +libpng know that there are some bytes missing from the start of the file. + + png_set_sig_bytes(png_ptr, number); + You are now ready to read all the file information up to the actual image data. You do this with a call to png_read_info(). - png_read_info(png_ptr, info_ptr); + png_read_info(png_ptr, info_ptr); The png_info structure is now filled in with all the data necessary to read the file. Some of the more important parts of the info_ptr are: @@ -183,8 +190,13 @@ to read the file. Some of the more important parts of the info_ptr are: bit_depth times the channels rowbytes - number of bytes needed to hold a row interlace_type - currently 0 for none, 1 for interlaced + signature - holds the signature read from the file (if any). The + data is kept in the same offset it would be if the + whole signature were read (ie if you had already read + in 4 bytes of signature, the remaining 4 bytes would + be in signature[4] through signature[7]). valid - this details which optional chunks were found in the - file to see if a chunk was present, AND '&' valid with + file. To see if a chunk was present, AND '&' valid with the appropriate PNG_INFO_ define. These are also important, but their validity depends on whether a @@ -217,7 +229,7 @@ keyword/text pairs, one pair per chunk. While there are suggested keywords, there is no requirement to restrict the use to these strings. There is a requirement to have at least one character for a keyword. It is strongly suggested that keywords be sensible to humans -(that's the point), so don't use abbreviations. See the png +(that's the point), so don't use abbreviations. See the PNG specification for more details. There is also no requirement to have text after the keyword. @@ -243,6 +255,13 @@ checks to see if it has data that it can do somthing with, you should make sure to only enable a transformation if it will be valid for the data. For example, don't swap red and blue on grayscale data. +The colors used for the background and transparency values should be +supplied in the same format/depth as the current image data. They +are stored in the same format/depth as the image data in a bKGD or tRNS +chunk, so this is what libpng expects for this data. The colors are +transformed to keep in sync with the image data when an application +calls the png_update_info() routine (see below). + Data will be decoded into the supplied row buffers packed into bytes unless the library has been told to transform it into another format. For example, 4 bit/pixel paletted or grayscale data will be returned @@ -747,7 +766,7 @@ Libpng section below. You now have the option of modifying how the compression library will run. The following functions are mainly for testing, but may be useful -in some cases, like if you need to write png files extremely fast and +in some cases, like if you need to write PNG files extremely fast and are willing to give up some compression, or if you want to get the maximum possible compression at the expense of slower writing. If you have no special needs in this area, let the library do what it wants by diff --git a/makefile.aco b/makefile.aco index 6ae1b8e4..27915f32 100644 --- a/makefile.aco +++ b/makefile.aco @@ -1,4 +1,3 @@ -# makefile for libpng on Acorn RISCOS # Project: libpng @@ -8,108 +7,43 @@ C++flags = -c -depend !Depend -IC: -throwback Linkflags = -aif -c++ -o $@ ObjAsmflags = -throwback -NoCache -depend !Depend CMHGflags = -LibFileflags = -c -o $@ +LibFileflags = -c -l -o $@ Squeezeflags = -o $@ # Final targets: @.libpng-lib: @.o.png @.o.pngerror @.o.pngrio @.o.pngwio @.o.pngmem \ - @.o.pngpread @.o.pngrcb @.o.pngread @.o.pngrtran @.o.pngrutil \ - @.o.pngtrans @.o.pngwrite @.o.pngwtran @.o.pngwutil + @.o.pngpread @.o.pngrcb @.o.pngread @.o.pngrtran @.o.pngrutil @.o.pngtrans @.o.pngwrite \ + @.o.pngwtran @.o.pngwutil LibFile $(LibFileflags) @.o.png @.o.pngerror @.o.pngrio @.o.pngwio \ - @.o.pngmem @.o.pngpread @.o.pngrcb @.o.pngread @.o.pngrtran \ - @.o.pngrutil @.o.pngtrans @.o.pngwrite @.o.pngwtran @.o.pngwutil -@.test: @.tests.pngtest - echo Please run "Test" in directory tests -@.tests.pngtest: @.o.pngtest @.libpng-lib C:o.Stubs Zlib:zlib_lib - Link $(Linkflags) @.o.pngtest @.libpng-lib C:o.Stubs Zlib:zlib_lib + @.o.pngmem @.o.pngpread @.o.pngrcb @.o.pngread @.o.pngrtran @.o.pngrutil @.o.pngtrans \ + @.o.pngwrite @.o.pngwtran @.o.pngwutil +@.mm-libpng-lib: @.mm.png @.mm.pngerror @.mm.pngrio @.mm.pngwio @.mm.pngmem \ + @.mm.pngpread @.mm.pngrcb @.mm.pngread @.mm.pngrtran @.mm.pngrutil @.mm.pngtrans \ + @.mm.pngwrite @.mm.pngwtran @.mm.pngwutil + LibFile $(LibFileflags) @.mm.png @.mm.pngerror @.mm.pngrio @.mm.pngwio \ + @.mm.pngmem @.mm.pngpread @.mm.pngrcb @.mm.pngread @.mm.pngrtran @.mm.pngrutil \ + @.mm.pngtrans @.mm.pngwrite @.mm.pngwtran @.mm.pngwutil # User-editable dependencies: +Test: @.pngtest + .PngTest + @remove .pngout_png + +#It would be nice if you could stop "make" listing from here on! +@.pngtest: @.o.pngtest @.libpng-lib C:o.Stubs Zlib:zlib_lib + Link $(Linkflags) @.o.pngtest @.libpng-lib C:o.Stubs Zlib:zlib_lib + +.SUFFIXES: .o .mm .c + +.c.mm: + MemCheck.CC cc $(ccflags) -o $@ LibPng:$< .c.o: cc $(ccflags) -o $@ $< # Static dependencies: -@.o.example: @.tests.c.example - cc $(ccflags) -o @.o.example @.tests.c.example -@.o.pngtest: @.tests.c.pngtest - cc $(ccflags) -o @.o.pngtest @.tests.c.pngtest # Dynamic dependencies: -o.png: c.png -o.png: h.png -o.png: Zlib:h.zlib -o.png: Zlib:h.zconf -o.png: h.pngconf -o.pngerror: c.pngerror -o.pngerror: h.png -o.pngerror: Zlib:h.zlib -o.pngerror: Zlib:h.zconf -o.pngerror: h.pngconf -o.pngrio: c.pngrio -o.pngrio: h.png -o.pngrio: Zlib:h.zlib -o.pngrio: Zlib:h.zconf -o.pngrio: h.pngconf -o.pngwio: c.pngwio -o.pngwio: h.png -o.pngwio: Zlib:h.zlib -o.pngwio: Zlib:h.zconf -o.pngwio: h.pngconf -o.pngmem: c.pngmem -o.pngmem: h.png -o.pngmem: Zlib:h.zlib -o.pngmem: Zlib:h.zconf -o.pngmem: h.pngconf -o.pngpread: c.pngpread -o.pngpread: h.png -o.pngpread: Zlib:h.zlib -o.pngpread: Zlib:h.zconf -o.pngpread: h.pngconf -o.pngrcb: c.pngrcb -o.pngrcb: h.png -o.pngrcb: Zlib:h.zlib -o.pngrcb: Zlib:h.zconf -o.pngrcb: h.pngconf -o.pngread: c.pngread -o.pngread: h.png -o.pngread: Zlib:h.zlib -o.pngread: Zlib:h.zconf -o.pngread: h.pngconf -o.pngrtran: c.pngrtran -o.pngrtran: h.png -o.pngrtran: Zlib:h.zlib -o.pngrtran: Zlib:h.zconf -o.pngrtran: h.pngconf -o.pngrutil: c.pngrutil -o.pngrutil: h.png -o.pngrutil: Zlib:h.zlib -o.pngrutil: Zlib:h.zconf -o.pngrutil: h.pngconf -o.pngtrans: c.pngtrans -o.pngtrans: h.png -o.pngtrans: Zlib:h.zlib -o.pngtrans: Zlib:h.zconf -o.pngtrans: h.pngconf -o.pngwrite: c.pngwrite -o.pngwrite: h.png -o.pngwrite: Zlib:h.zlib -o.pngwrite: Zlib:h.zconf -o.pngwrite: h.pngconf -o.pngwtran: c.pngwtran -o.pngwtran: h.png -o.pngwtran: Zlib:h.zlib -o.pngwtran: Zlib:h.zconf -o.pngwtran: h.pngconf -o.pngwutil: c.pngwutil -o.pngwutil: h.png -o.pngwutil: Zlib:h.zlib -o.pngwutil: Zlib:h.zconf -o.pngwutil: h.pngconf -o.pngtest: tests.c.pngtest -o.pngtest: h.png -o.pngtest: Zlib:h.zlib -o.pngtest: Zlib:h.zconf -o.pngtest: h.pngconf diff --git a/makefile.elf b/makefile.elf index f61d30de..608172c7 100644 --- a/makefile.elf +++ b/makefile.elf @@ -1,15 +1,18 @@ # makefile for libpng on (linux) ELF -# Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc. +# Copyright (C) 1996, 1997 Andreas Dilger # For conditions of distribution and use, see copyright notice in png.h CC=gcc -CFLAGS=-I../zlib -O2 -Wall -fPIC +CFLAGS=-I../zlib -Wall -Wwrite-strings -Wpointer-arith \ + -Wstrict-prototypes -Wmissing-prototypes -O2 -fPIC LDFLAGS=-L. -Wl,-rpath,. -L../zlib/ -Wl,-rpath,../zlib/ -lpng -lz -lm RANLIB=ranlib #RANLIB=echo -PNGVER = 0.89 +PNGMAJ = 0 +PNGMIN = 90 +PNGVER = $(PNGMAJ).$(PNGMIN) # where make install puts libpng.a, libpng.so*, and png.h prefix=/usr/local @@ -24,14 +27,14 @@ libpng.a: $(OBJS) ar rc $@ $(OBJS) $(RANLIB) $@ -libpng.so: libpng.so.1 - ln -sf libpng.so.1 libpng.so +libpng.so: libpng.so.$(PNGMAJ) + ln -sf libpng.so.$(PNGMAJ) libpng.so -libpng.so.1: libpng.so.1.$(PNGVER) - ln -sf libpng.so.1.$(PNGVER) libpng.so.1 +libpng.so.$(PNGMAJ): libpng.so.$(PNGVER) + ln -sf libpng.so.$(PNGVER) libpng.so.$(PNGMAJ) -libpng.so.1.$(PNGVER): $(OBJS) - gcc -shared -Wl,-soname,libpng.so.1 -o libpng.so.1.$(PNGVER) $(OBJS) +libpng.so.$(PNGVER): $(OBJS) + gcc -shared -Wl,-soname,libpng.so.$(PNGMAJ) -o libpng.so.$(PNGVER) $(OBJS) pngtest: pngtest.o libpng.so $(CC) -o pngtest $(CCFLAGS) pngtest.o $(LDFLAGS) @@ -39,21 +42,21 @@ pngtest: pngtest.o libpng.so test: pngtest ./pngtest -install: libpng.so.1.$(PNGVER) +install: libpng.so.$(PNGVER) -@mkdir $(prefix)/include -@mkdir $(prefix)/lib cp png.h $(prefix)/include cp pngconf.h $(prefix)/include chmod 644 $(prefix)/include/png.h chmod 644 $(prefix)/include/pngconf.h - cp libpng.so.1.$(PNGVER) $(prefix)/lib - chmod 755 $(prefix)/lib/libpng.so.1.$(PNGVER) - -@/bin/rm $(prefix)/lib/libpng.so.1 $(prefix)/lib/libpng.so - (cd $(prefix)/lib; ln -sf libpng.so.1.$(PNGVER) libpng.so.1; \ - ln -sf libpng.so.1 libpng.so) + cp libpng.so.$(PNGVER) $(prefix)/lib + chmod 755 $(prefix)/lib/libpng.so.$(PNGVER) + -@/bin/rm $(prefix)/lib/libpng.so.$(PNGMAJ) $(prefix)/lib/libpng.so + (cd $(prefix)/lib; ln -sf libpng.so.$(PNGVER) libpng.so.$(PNGMAJ); \ + ln -sf libpng.so.$(PNGMAJ) libpng.so) clean: - rm -f *.o libpng.so.1.$(PNGVER) libpng.so.1 libpng.so pngtest pngout.png + rm -f *.o libpng.a libpng.so* pngtest pngout.png # DO NOT DELETE THIS LINE -- make depend depends on it. diff --git a/png.c b/png.c index d582f423..eb69411b 100644 --- a/png.c +++ b/png.c @@ -1,10 +1,10 @@ /* png.c - location for general purpose png functions - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 */ #define PNG_INTERNAL @@ -13,7 +13,7 @@ /* version information for c files. This better match the version string defined in png.h */ -char png_libpng_ver[] = "0.89"; +char png_libpng_ver[] = "0.90"; /* place to hold the signiture string for a png file. */ png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10}; @@ -70,15 +70,52 @@ int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}; int FARDATA png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff}; +/* Tells libpng that we have already handled the first "num_bytes" bytes + * of the PNG file signature. If the PNG data is embedded into another + * stream we can set num_bytes = 8 so that libpng will not attempt to read + * or write any of the magic bytes before it starts on the IHDR. + */ +void +png_set_sig_bytes(png_structp png_ptr, int num_bytes) +{ + if (num_bytes > 8) + png_error(png_ptr, "Too many bytes for PNG signature."); + + png_ptr->sig_bytes = num_bytes < 0 ? 0 : num_bytes; +} + +/* Checks whether the supplied bytes match the PNG signature. We allow + * checking less than the full 8-byte signature so that those apps that + * already read the first few bytes of a file to determine the file type + * can simply check the remaining bytes for extra assurance. Returns + * an integer less than, equal to, or greater than zero if sig is found, + * respectively, to be less than, to match, or be greater than the correct + * PNG signature (this is the same behaviour as strcmp, memcmp, etc). + */ +int +png_sig_cmp(png_bytep sig, int start, int num_to_check) +{ + if (num_to_check > 8) + num_to_check = 8; + else if (num_to_check < 1) + return 0; + + if (start > 7 || start < 0) + return 0; + + if (start + num_to_check > 8) + num_to_check = 8 - start; + + return (png_memcmp(sig, &png_sig[start], (unsigned int)num_to_check)); +} + +/* (Obsolete) function to check signature bytes. It does not allow one + to check a partial signature. This function will be removed in the + future - use png_sig_cmp(). */ int png_check_sig(png_bytep sig, int num) { - if (num > 8) - num = 8; - if (num < 1) - return 0; - - return (!png_memcmp(sig, png_sig, num)); + return !png_sig_cmp(sig, 0, num); } /* Function to allocate memory for zlib. */ @@ -88,7 +125,7 @@ png_zalloc(voidpf png_ptr, uInt items, uInt size) png_voidp ptr; png_uint_32 num_bytes; - ptr = png_large_malloc((png_structp)png_ptr, + ptr = png_malloc((png_structp)png_ptr, (png_uint_32)items * (png_uint_32)size); num_bytes = (png_uint_32)items * (png_uint_32)size; if (num_bytes > (png_uint_32)0x7fff) @@ -108,26 +145,27 @@ png_zalloc(voidpf png_ptr, uInt items, uInt size) void png_zfree(voidpf png_ptr, voidpf ptr) { - png_large_free((png_structp)png_ptr, (png_voidp)ptr); + png_free((png_structp)png_ptr, (png_voidp)ptr); } -/* reset the crc variable to 32 bits of 1's. Care must be taken - in case crc is > 32 bits to leave the top bits 0 */ +/* Reset the CRC variable to 32 bits of 1's. Care must be taken + in case CRC is > 32 bits to leave the top bits 0. */ void png_reset_crc(png_structp png_ptr) { /* set crc to all 1's */ +#ifdef PNG_USE_OWN_CRC png_ptr->crc = 0xffffffffL; +#else + png_ptr->crc = crc32(0, Z_NULL, 0); +#endif } -/* Note: the crc code below was copied from the sample code in the - PNG spec, with appropriate modifications made to ensure the - variables are large enough */ - -/* table of crc's of all 8-bit messages. If you wish to png_malloc this - table, turn this into a pointer, and png_malloc it in make_crc_table(). +#ifdef PNG_USE_OWN_CRC +/* Table of CRC's of all 8-bit messages. If you wish to png_malloc this + table, turn this into a pointer, and png_malloc() it in make_crc_table(). You may then want to hook it into png_struct and free it with the - destroy functions. */ + destroy functions. Another alternative is to pre-fill the table. */ static png_uint_32 crc_table[256]; /* Flag: has the table been computed? Initially false. */ @@ -150,9 +188,9 @@ make_crc_table(void) crc_table_computed = 1; } -/* update a running crc with the bytes buf[0..len-1]--the crc should be +/* Update a running CRC with the bytes buf[0..len-1]--the crc should be initialized to all 1's, and the transmitted value is the 1's complement - of the final running crc. */ + of the final running CRC. */ static png_uint_32 update_crc(png_uint_32 crc, png_bytep buf, png_uint_32 len) { @@ -176,8 +214,9 @@ update_crc(png_uint_32 crc, png_bytep buf, png_uint_32 len) return c; } +#endif /* PNG_USE_OWN_CRC */ -/* calculate the crc over a section of data. Note that while we +/* Calculate the crc over a section of data. Note that while we are passing in a 32 bit value for length, on 16 bit machines, you would need to use huge pointers to access all that data. If you need this, put huge here and above. */ @@ -185,9 +224,18 @@ void png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_uint_32 length) { +#ifdef PNG_USE_OWN_CRC png_ptr->crc = update_crc(png_ptr->crc, ptr, length); +#else + png_ptr->crc = crc32(png_ptr->crc, ptr, length); +#endif } +/* Allocate the memory for an info_struct for the application. We don't + really need the png_ptr, but it could potentially be useful in the + future. This should be used in favour of malloc(sizeof(png_info)) + and png_info_init() so that applications that want to use a shared + libpng don't have to be recompiled if png_info changes size. */ png_infop png_create_info_struct(png_structp png_ptr) { @@ -195,23 +243,65 @@ png_create_info_struct(png_structp png_ptr) if ((info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO)) != NULL) { - png_memset(info_ptr, 0, sizeof(png_info)); - png_ptr->do_free |= PNG_FREE_INFO; + png_info_init(info_ptr); } return info_ptr; } +/* This function frees the memory associated with a single info struct. + Normally, one would use either png_destroy_read_struct() or + png_destroy_write_struct() to free an info struct, but this may be + useful for some applications. */ void -png_info_init(png_infop info) +png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr) +{ + png_infop info_ptr = NULL; + + if (info_ptr_ptr) + info_ptr = *info_ptr_ptr; + + if (info_ptr) + { + png_info_destroy(png_ptr, info_ptr); + + png_destroy_struct((png_voidp)info_ptr); + *info_ptr_ptr = (png_infop)NULL; + } +} + +/* Initialize the info structure. This is now an internal function (0.89) + and applications using it are urged to use png_create_info_struct() + instead. */ +void +png_info_init(png_infop info_ptr) { /* set everything to 0 */ - png_memset(info, 0, sizeof (png_info)); + png_memset(info_ptr, 0, sizeof (png_info)); +} + +/* This is an internal routine to free any memory that the info struct is + pointing to before re-using it or freeing the struct itself. */ +void +png_info_destroy(png_structp png_ptr, png_infop info_ptr) +{ +#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) + int i; + + for (i = 0; i < info_ptr->num_text; i++) + { + png_free(png_ptr, info_ptr->text[i].key); + } + + png_free(png_ptr, info_ptr->text); +#endif + + png_info_init(info_ptr); } /* This function returns a pointer to the io_ptr associated with the user functions. The application should free any memory associated with this - pointer before png_write_destroy and png_read_destroy are called. */ + pointer before png_write_destroy() or png_read_destroy() are called. */ png_voidp png_get_io_ptr(png_structp png_ptr) { diff --git a/png.h b/png.h index 53aea082..ce841bd0 100644 --- a/png.h +++ b/png.h @@ -1,8 +1,8 @@ /* png.h - header file for png reference library - libpng 1.0 beta 3 - version 0.89 - May 25, 1996 + libpng 1.0 beta 4 - version 0.90 + January 10, 1997 Note: This is a beta version. It reads and writes valid files on the platforms I have, and has had a wide testing program. @@ -10,14 +10,15 @@ your system, and you may have to supply the correct compiler flags in the makefile, if you can't find a makefile suitable for your operating system/compiler combination. Read the libpng.txt - for more information, and how to contact me if you have any + for more information, and how to contact the authors if you have any problems, or if you want your compiler/platform to be supported in the next official libpng release. - See libpng.txt for more information + See libpng.txt for more information. - Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. + Copyright (c) 1995, 1996, 1997 Guy Eric Schalnat, Group 42, Inc. Contributing Authors: + Sam Bushnell Andreas Dilger Dave Martindale Guy Eric Schalnat @@ -57,6 +58,10 @@ #ifndef _PNG_H #define _PNG_H +#ifdef __cplusplus +extern "C" { +#endif + /* This is not the place to learn how to use libpng. The file libpng.txt describes how to use libpng, and the file example.c summarizes it with some code to build around. This file is useful for looking @@ -71,15 +76,14 @@ /* This file is arranged in several sections. The first section details the functions most users will use. The third section describes the stub files that users will most likely need to change. The last - section contains functions used internally by the code. - */ + section contains functions used internally by the code. */ /* version information for png.h - this should match the version number in png.c */ -#define PNG_LIBPNG_VER_STRING "0.89" -/* careful here. I wanted to use 089, but that would be octal. Version +#define PNG_LIBPNG_VER_STRING "0.90" +/* careful here. I wanted to use 090, but that would be octal. Version 1.0 will be 100 here, etc. */ -#define PNG_LIBPNG_VER 89 +#define PNG_LIBPNG_VER 90 /* variables defined in png.c - only it needs to define PNG_NO_EXTERN */ #ifndef PNG_NO_EXTERN @@ -154,9 +158,8 @@ typedef png_time FAR * FAR * png_timepp; If you are reading the file, This structure will tell you what is in the png file. If you are writing the file, fill in the information you want to put into the png file, then call png_write_info(). - The names chosen should be very close to the PNG - specification, so consult that document for information - about the meaning of each field. */ + The names chosen should be very close to the PNG specification, so + consult that document for information about the meaning of each field. */ typedef struct png_info_struct { /* the following are necessary for every png file */ @@ -177,6 +180,7 @@ typedef struct png_info_struct png_byte channels; /* number of channels of data per pixel */ png_byte pixel_depth; /* number of bits per pixel */ png_byte spare_byte; /* To align the data, and for future use */ + png_byte signature[8]; /* Signature read from start of file */ /* the rest are optional. If you are reading, check the valid field to see if the information in these are valid. If you @@ -318,12 +322,11 @@ struct png_struct_def png_rw_ptr read_data_fn; /* Function for reading input data */ png_voidp io_ptr; /* Pointer to user supplied struct for I/O functions */ - png_byte mode; /* used to determine where we are in the png file */ - png_uint_32 do_free; /* flags indicating if libpng should free memory */ + png_uint_32 mode; /* used to determine where we are in the png file */ png_uint_32 flags; /* flags indicating various things to libpng */ png_uint_32 transformations; /* which transformations to perform */ - z_stream * zstream; /* pointer to decompression structure (below) */ + z_stream zstream; /* pointer to decompression structure (below) */ png_bytep zbuf; /* buffer for zlib */ png_uint_32 zbuf_size; /* size of zbuf */ int zlib_level; /* holds zlib compression level */ @@ -340,12 +343,12 @@ struct png_struct_def png_uint_32 iwidth; /* interlaced width */ png_uint_32 irowbytes; /* interlaced rowbytes */ png_uint_32 row_number; /* current row in pass */ - png_bytep row_buf; /* row buffer */ - png_bytep prev_row; /* previous row */ - png_bytep sub_row; /* place to save row when filtering */ - png_bytep up_row; /* place to save row when filtering */ - png_bytep avg_row; /* place to save row when filtering */ - png_bytep paeth_row; /* place to save row when filtering */ + png_bytep prev_row; /* place to save previous (unfiltered) row */ + png_bytep row_buf; /* place to save current (unfiltered) row */ + png_bytep sub_row; /* place to save "sub" row when filtering */ + png_bytep up_row; /* place to save "up" row when filtering */ + png_bytep avg_row; /* place to save "avg" row when filtering */ + png_bytep paeth_row; /* place to save "Paeth" row when filtering */ png_row_info row_info; /* used for transformation routines */ png_uint_32 idat_size; /* current idat size for read */ @@ -353,17 +356,20 @@ struct png_struct_def png_colorp palette; /* files palette */ png_uint_16 num_palette; /* number of entries in palette */ png_uint_16 num_trans; /* number of transparency values */ - png_byte interlaced; /* interlace type of file */ - png_byte pass; /* current pass (0 - 6) */ - png_byte compression; /* compression type of file */ - png_byte filter; /* filter type */ - png_byte do_filter; /* non-zero if row filtering, zero if not */ + png_byte chunk_name[5]; /* name of current chunk being processed + '\0' */ + png_byte compression; /* file compression type (currently only '0' used) */ + png_byte filter; /* file filter type (currently only '0' used) */ + png_byte interlaced; /* file interlace type (currently only '0' and '1') */ + png_byte pass; /* current interlace pass (0 - 6) */ + png_byte do_filter; /* zero if not row filtering, non-zero if filtering */ png_byte color_type; /* color type of file */ png_byte bit_depth; /* bit depth of file */ png_byte usr_bit_depth; /* bit depth of users row */ png_byte pixel_depth; /* number of bits per pixel */ png_byte channels; /* number of channels in file */ png_byte usr_channels; /* channels at start of write */ + png_byte sig_bytes; /* signature bytes read/written from start of file */ + #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) png_byte filler; /* filler byte to be used for 32-bit frame buffers */ @@ -420,7 +426,6 @@ struct png_struct_def png_uint_32 current_buffer_size; int process_mode; int cur_palette; - png_byte push_chunk_name[4]; #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) png_uint_32 current_text_size; png_uint_32 current_text_left; @@ -445,44 +450,37 @@ struct png_struct_def typedef png_struct FAR * FAR * png_structpp; -/* flags for png_set_filter() to say which filters to use. The flags - are chosen so that they don't conflict with real filter types, in case they - are supplied instead of the #defined constants. - */ -#define PNG_NO_FILTERS 0x00 -#define PNG_FILTER_NONE 0x08 -#define PNG_FILTER_SUB 0x10 -#define PNG_FILTER_UP 0x20 -#define PNG_FILTER_AVG 0x40 -#define PNG_FILTER_PAETH 0x80 -#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \ - PNG_FILTER_AVG | PNG_FILTER_PAETH) - /* Here are the function definitions most commonly used. This is not the place to find out how to use libpng. See libpng.txt for the full explanation, see example.c for the summary. This just provides a simple one line of the use of each function. */ -/* check the first 1 - 8 bytes to see if it is a png file */ +/* Tell lib we have already handled the first magic bytes. + Handling more than 8 bytes from the beginning of the file is an error. */ +extern void png_set_sig_bytes PNGARG((png_structp png_ptr, int num_bytes)); + +/* Check sig[start] through sig[start + num_to_check] to see if it's a PNG + file. Returns zero if the supplied bytes match the 8-byte PNG signature, + and non-zero otherwise. Having num_to_check == 0 or start > 7 will + always fail (ie return non-zero). */ +extern int png_sig_cmp PNGARG((png_bytep sig, int start, int num_to_check)); + +/* (Obsolete) signature checking function. This is the same as calling + png_check_sig(sig, n) := !png_sig_cmp(sig, 0, n). To be removed in a + future version. */ extern int png_check_sig PNGARG((png_bytep sig, int num)); -/* allocate and initialize png structure for reading, and any other memory */ +/* Allocate and initialize png structure for reading, and any other memory. */ extern png_structp png_create_read_struct PNGARG((png_const_charp user_png_ver, - voidp error_ptr, png_error_ptr warn_fn, png_error_ptr error_fn)); + voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn)); -/* reset the png_struct to read a new image */ -extern void png_reset_read_struct PNGARG((png_structpp png_ptr)); - /* initialize png structure for reading, and allocate any other memory (old) */ extern void png_read_init PNGARG((png_structp png_ptr)); /* allocate and initialize png structure for reading, and any other memory */ extern png_structp png_create_write_struct PNGARG((png_const_charp user_png_ver, voidp error_ptr, - png_error_ptr warn_fn, png_error_ptr error_fn)); - -/* reset the png_struct to read a new image */ -extern void png_reset_write_struct PNGARG((png_structpp png_ptr)); + png_error_ptr error_fn, png_error_ptr warn_fn)); /* initialize png structure for writing, and allocate any other memory (old) */ extern void png_write_init PNGARG((png_structp png_ptr)); @@ -491,13 +489,13 @@ extern void png_write_init PNGARG((png_structp png_ptr)); extern png_infop png_create_info_struct PNGARG((png_structp png_ptr)); /* initialize the info structure (old interface) */ -extern void png_info_init PNGARG((png_infop info)); +extern void png_info_init PNGARG((png_infop info_ptr)); /* Writes all the png information before the image. */ -extern void png_write_info PNGARG((png_structp png_ptr, png_infop info)); +extern void png_write_info PNGARG((png_structp png_ptr, png_infop info_ptr)); /* read the information before the actual image data. */ -extern void png_read_info PNGARG((png_structp png_ptr, png_infop info)); +extern void png_read_info PNGARG((png_structp png_ptr, png_infop info_ptr)); #if defined(PNG_WRITE_tIME_SUPPORTED) /* convert from a struct tm to png_time */ @@ -524,15 +522,23 @@ extern void png_set_bgr PNGARG((png_structp png_ptr)); extern void png_set_gray_to_rgb PNGARG((png_structp png_ptr)); #endif +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) +/* Reduce RGB to grayscale. (Not yet implemented) */ +extern void png_set_rgb_to_gray PNGARG((png_structp png_ptr)); +#endif + +extern void png_build_grayscale_palette PNGARG((int bit_depth, + png_colorp palette)); + #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) #define PNG_FILLER_BEFORE 0 #define PNG_FILLER_AFTER 1 /* Add a filler byte to rgb images. */ -extern void png_set_filler PNGARG((png_structp png_ptr, int filler, +extern void png_set_filler PNGARG((png_structp png_ptr, png_byte filler, int flags)); -/* old ways of doing this, still supported through 1.x for backwards - compatability, but not suggested */ +/* Old way of doing this, still supported through 1.x for backwards + compatability, but should not be used in new code. */ /* Add a filler byte to rgb images after the colors. */ extern void png_set_rgbx PNGARG((png_structp png_ptr)); @@ -584,11 +590,6 @@ extern void png_set_background PNGARG((png_structp png_ptr, extern void png_set_strip_16 PNGARG((png_structp png_ptr)); #endif -#if defined(PNG_GRAY_TO_RGB_SUPPORTED) -/* convert a grayscale file into rgb. */ -extern void png_set_gray_to_rgb PNGARG((png_structp png_ptr)); -#endif - #if defined(PNG_READ_DITHER_SUPPORTED) /* Turn on dithering, and reduce the palette to the number of colors available. */ extern void png_set_dither PNGARG((png_structp png_ptr, png_colorp palette, @@ -644,40 +645,74 @@ extern void png_write_rows PNGARG((png_structp png_ptr, extern void png_write_image PNGARG((png_structp png_ptr, png_bytepp image)); /* writes the end of the png file. */ -extern void png_write_end PNGARG((png_structp png_ptr, png_infop info)); +extern void png_write_end PNGARG((png_structp png_ptr, png_infop info_ptr)); /* read the end of the png file. */ -extern void png_read_end PNGARG((png_structp png_ptr, png_infop info)); +extern void png_read_end PNGARG((png_structp png_ptr, png_infop info_ptr)); -/* free the info structure */ +/* free any memory associated with the info_struct */ extern void png_destroy_info_struct PNGARG((png_structp png_ptr, - png_infopp info_ptr)); + png_infopp info_ptr_ptr)); /* free any memory associated with the png_struct and the info_structs */ -extern void png_destroy_read_struct PNGARG((png_structpp png_ptr, - png_infopp info, png_infopp end_info)); +extern void png_destroy_read_struct PNGARG((png_structpp png_ptr_ptr, + png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr)); /* free all memory used by the read (old method) */ -extern void png_read_destroy PNGARG((png_structp png_ptr, png_infop info, - png_infop end_info)); +extern void png_read_destroy PNGARG((png_structp png_ptr, png_infop info_ptr, + png_infop end_info_ptr)); /* free any memory associated with the png_struct and the info_structs */ -extern void png_destroy_write_struct PNGARG((png_structpp png_ptr, - png_infopp info)); +extern void png_destroy_write_struct PNGARG((png_structpp png_ptr_ptr, + png_infopp info_ptr_ptr)); /* free any memory used in png struct */ extern void png_write_destroy PNGARG((png_structp png_ptr)); -/* These functions give the user control over the filtering and - compression libraries used by zlib. These functions are mainly - useful for testing, as the defaults should work with most users. - Those users who are tight on memory, or are wanting faster - performance at the expense of compression can modify them. - See the compression library header file for an explination - of these functions */ +/* set the libpng method of handling chunk CRC errors */ +extern void png_set_crc_action PNGARG((png_structp png_ptr, int crit_action, + int ancil_action)); + +/* Values for png_set_crc_action() to say how to handle CRC errors in + ancillary and critical chunks, and whether to use the data contained + therein. Note that it is impossible to "discard" data in a critical + chunk. For versions prior to 0.90, the action was always error/quit, + whereas in version 0.90, the action for CRC errors in ancillary + chunks is warn/discard. + + value action:critical action:ancillary + */ +#define PNG_CRC_DEFAULT 0 /* error/quit warn/discard data */ +#define PNG_CRC_ERROR_QUIT 1 /* error/quit error/quit */ +#define PNG_CRC_WARN_DISCARD 2 /* (INVALID) warn/discard data */ +#define PNG_CRC_WARN_USE 3 /* warn/use data warn/use data */ +#define PNG_CRC_QUIET_USE 4 /* quiet/use data quiet/use data */ +#define PNG_CRC_NO_CHANGE 5 /* use current value use current value */ + +/* These functions give the user control over the scan-line filtering + in libpng and the compression methods used by zlib. These functions are + mainly useful for testing, as the defaults should work with most users. + Those users who are tight on memory, or are wanting faster performance + at the expense of compression can modify them. See the compression + library header file for an explination of the compression functions */ + +/* set the filtering method(s) used by libpng */ extern void png_set_filter PNGARG((png_structp png_ptr, int method, int filters)); +/* Flags for png_set_filter() to say which filters to use. The flags + are chosen so that they don't conflict with real filter types, in case they + are supplied instead of the #defined constants. + */ +#define PNG_NO_FILTERS 0x00 +#define PNG_FILTER_NONE 0x08 +#define PNG_FILTER_SUB 0x10 +#define PNG_FILTER_UP 0x20 +#define PNG_FILTER_AVG 0x40 +#define PNG_FILTER_PAETH 0x80 +#define PNG_ALL_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP | \ + PNG_FILTER_AVG | PNG_FILTER_PAETH) + extern void png_set_compression_level PNGARG((png_structp png_ptr, int level)); @@ -712,12 +747,12 @@ extern void png_read_data PNGARG((png_structp png_ptr, png_bytep data, /* Initialize the input/output for the png file to the default functions. */ extern void png_init_io PNGARG((png_structp png_ptr, FILE *fp)); -/* Replace the error message and abort, and warning functions with user - supplied functions. If no messages are to be printed then you must - supply replacement message functions. The replacement error_fn should +/* Replace the (error and abort), and warning functions with user + supplied functions. If no messages are to be printed you must still + write and use replacement functions. The replacement error_fn should still do a longjmp to the last setjmp location if you are using this method of error handling. If error_fn or warning_fn is NULL, the - default functions will be used. */ + default function will be used. */ extern void png_set_error_fn PNGARG((png_structp png_ptr, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn)); @@ -748,27 +783,11 @@ extern void png_set_push_fn PNGARG((png_structp png_ptr, png_voidp push_ptr, extern png_voidp png_get_progressive_ptr PNGARG((png_structp png_ptr)); #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ -extern png_voidp png_large_malloc PNGARG((png_structp png_ptr, +extern png_voidp png_malloc PNGARG((png_structp png_ptr, png_uint_32 size)); -/* free's a pointer allocated by png_large_malloc() */ -extern void png_large_free PNGARG((png_structp png_ptr, png_voidp ptr)); - -/* Allocate memory. */ -extern void * png_malloc PNGARG((png_structp png_ptr, png_uint_32 size)); - -/* Reallocate memory. */ -extern void * png_realloc PNGARG((png_structp png_ptr, void * ptr, - png_uint_32 size, png_uint_32 old_size)); - /* free's a pointer allocated by png_malloc() */ -extern void png_free PNGARG((png_structp png_ptr, void * ptr)); - -/* allocate memory for an internal libpng struct */ -extern png_voidp png_create_struct PNGARG((uInt type)); - -/* free memory from internal libpng struct */ -extern void png_destroy_struct PNGARG((voidp struct_ptr)); +extern void png_free PNGARG((png_structp png_ptr, png_voidp ptr)); /* Fatal error in libpng - can't continue */ extern void png_error PNGARG((png_structp png_ptr, png_const_charp error)); @@ -786,33 +805,27 @@ extern void png_warning PNGARG((png_structp png_ptr, png_const_charp message)); #if defined(PNG_INTERNAL) -/* various modes of operation. Note that after an init, mode is set to - zero automatically */ -#define PNG_BEFORE_IHDR 0x00 -#define PNG_HAVE_IHDR 0x01 -#define PNG_HAVE_PLTE 0x02 -#define PNG_HAVE_IDAT 0x04 -#define PNG_AT_LAST_IDAT 0x08 -#define PNG_AFTER_IDAT 0x10 -#define PNG_AFTER_IEND 0x20 +/* Various modes of operation. Note that after an init, mode is set to + zero automatically when the structure is created. */ +#define PNG_BEFORE_IHDR 0x00 +#define PNG_HAVE_IHDR 0x01 +#define PNG_HAVE_PLTE 0x02 +#define PNG_HAVE_IDAT 0x04 +#define PNG_AFTER_IDAT 0x08 +#define PNG_HAVE_IEND 0x10 /* push model modes */ #define PNG_READ_SIG_MODE 0 #define PNG_READ_CHUNK_MODE 1 #define PNG_READ_IDAT_MODE 2 -#define PNG_READ_PLTE_MODE 3 -#define PNG_READ_END_MODE 4 -#define PNG_SKIP_MODE 5 -#define PNG_READ_tEXt_MODE 6 -#define PNG_READ_zTXt_MODE 7 -#define PNG_READ_DONE_MODE 8 -#define PNG_ERROR_MODE 9 +#define PNG_READ_END_MODE 3 +#define PNG_SKIP_MODE 4 +#define PNG_READ_tEXt_MODE 5 +#define PNG_READ_zTXt_MODE 6 +#define PNG_READ_DONE_MODE 7 +#define PNG_ERROR_MODE 8 -/* read modes */ -#define PNG_READ_PULL_MODE 0 -#define PNG_READ_PUSH_MODE 1 - -/* defines for the transformations the png library does on the image data */ +/* defines for the transformations the PNG library does on the image data */ #define PNG_BGR 0x0001 #define PNG_INTERLACE 0x0002 #define PNG_PACK 0x0004 @@ -822,7 +835,7 @@ extern void png_warning PNGARG((png_structp png_ptr, png_const_charp message)); #define PNG_DITHER 0x0040 #define PNG_BACKGROUND 0x0080 #define PNG_BACKGROUND_EXPAND 0x0100 -#define PNG_XRGB 0x0200 +#define PNG_RGB_TO_GRAY 0x0200 #define PNG_16_TO_8 0x0400 #define PNG_RGBA 0x0800 #define PNG_EXPAND 0x1000 @@ -830,28 +843,37 @@ extern void png_warning PNGARG((png_structp png_ptr, png_const_charp message)); #define PNG_GRAY_TO_RGB 0x4000 #define PNG_FILLER 0x8000 -/* flags for png_ptr->do_free to say if memory in png_info needs to be freed */ -#define PNG_FREE_PALETTE 0x0001 -#define PNG_FREE_HIST 0x0002 -#define PNG_FREE_TRANS 0x0004 -#define PNG_FREE_STRUCT 0x0008 -#define PNG_FREE_INFO 0x0010 - /* flags for png_create_struct */ #define PNG_STRUCT_PNG 0x0001 #define PNG_STRUCT_INFO 0x0002 /* flags for the png_ptr->flags rather than declaring a bye for each one */ -#define PNG_FLAG_WROTE_tIME 0x0001 -#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY 0x0002 -#define PNG_FLAG_ZLIB_CUSTOM_LEVEL 0x0004 -#define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL 0x0008 -#define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS 0x0010 -#define PNG_FLAG_ZLIB_CUSTOM_METHOD 0x0020 -#define PNG_FLAG_ZLIB_FINISHED 0x0040 -#define PNG_FLAG_ROW_INIT 0x0080 -#define PNG_FLAG_FILLER_AFTER 0x0100 -#define PNG_FLAG_HAVE_CHUNK_HEADER 0x0200 +#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY 0x0001 +#define PNG_FLAG_ZLIB_CUSTOM_LEVEL 0x0002 +#define PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL 0x0004 +#define PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS 0x0008 +#define PNG_FLAG_ZLIB_CUSTOM_METHOD 0x0010 +#define PNG_FLAG_ZLIB_FINISHED 0x0020 +#define PNG_FLAG_ROW_INIT 0x0040 +#define PNG_FLAG_FILLER_AFTER 0x0080 +#define PNG_FLAG_CRC_ANCILLARY_USE 0x0100 +#define PNG_FLAG_CRC_ANCILLARY_NOWARN 0x0200 +#define PNG_FLAG_CRC_CRITICAL_USE 0x0400 +#define PNG_FLAG_CRC_CRITICAL_IGNORE 0x0800 +#define PNG_FLAG_FREE_PALETTE 0x1000 +#define PNG_FLAG_FREE_TRANS 0x2000 +#define PNG_FLAG_FREE_HIST 0x4000 +#define PNG_FLAG_HAVE_CHUNK_HEADER 0x8000 +#define PNG_FLAG_WROTE_tIME 0x10000 + +#define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \ + PNG_FLAG_CRC_ANCILLARY_NOWARN) + +#define PNG_FLAG_CRC_CRITICAL_MASK (PNG_FLAG_CRC_CRITICAL_USE | \ + PNG_FLAG_CRC_CRITICAL_IGNORE) + +#define PNG_FLAG_CRC_MASK (PNG_FLAG_CRC_ANCILLARY_MASK | \ + PNG_FLAG_CRC_CRITICAL_MASK) /* save typing and make code easier to understand */ #define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \ @@ -861,7 +883,7 @@ extern void png_warning PNGARG((png_structp png_ptr, png_const_charp message)); /* variables defined in png.c - only it needs to define PNG_NO_EXTERN */ #ifndef PNG_NO_EXTERN /* place to hold the signiture string for a png file. */ -extern png_byte png_sig[]; +extern png_byte FARDATA png_sig[]; /* version information for c files, stored in png.c. */ extern char png_libpng_ver[]; @@ -899,15 +921,35 @@ extern int FARDATA png_pass_dsp_mask[]; #endif /* PNG_NO_EXTERN */ +/* allocate memory for an internal libpng struct */ +extern png_voidp png_create_struct PNGARG((int type)); + +/* free memory from internal libpng struct */ +extern void png_destroy_struct PNGARG((png_voidp struct_ptr)); + +/* free any memory that info_ptr points to and reset struct. */ +extern void png_info_destroy PNGARG((png_structp png_ptr, + png_infop info_ptr)); + /* Function to allocate memory for zlib. */ extern voidpf png_zalloc PNGARG((voidpf png_ptr, uInt items, uInt size)); /* function to free memory for zlib */ extern void png_zfree PNGARG((voidpf png_ptr, voidpf ptr)); -/* reset the crc variable */ +/* reset the CRC variable */ extern void png_reset_crc PNGARG((png_structp png_ptr)); +/* read bytes into buf, and update png_ptr->crc */ +extern void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf, + png_uint_32 length)); + +/* read "skip" bytes, read the file crc, and (optionally) verify png_ptr->crc */ +extern int png_crc_finish PNGARG((png_structp png_ptr, png_uint_32 skip)); + +/* read the CRC from the file and compare it to the libpng calculated CRC */ +extern int png_crc_error PNGARG((png_structp png_ptr)); + /* calculate the crc over a section of data. Note that while we are passing in a 32 bit value for length, on 16 bit machines, you would need to use huge pointers to access all that data. See the @@ -934,12 +976,12 @@ extern void png_write_uint_32 PNGARG((png_structp png_ptr, png_uint_32 i)); extern void png_write_uint_16 PNGARG((png_structp png_ptr, png_uint_16 i)); /* Write a png chunk. */ -extern void png_write_chunk PNGARG((png_structp png_ptr, png_bytep type, +extern void png_write_chunk PNGARG((png_structp png_ptr, png_bytep chunk_name, png_bytep data, png_uint_32 length)); /* Write the start of a png chunk. */ -extern void png_write_chunk_start PNGARG((png_structp png_ptr, png_bytep type, - png_uint_32 total_length)); +extern void png_write_chunk_start PNGARG((png_structp png_ptr, png_bytep chunk_name, + png_uint_32 length)); /* write the data of a png chunk started with png_write_chunk_start(). */ extern void png_write_chunk_data PNGARG((png_structp png_ptr, png_bytep data, @@ -961,7 +1003,7 @@ extern void png_write_IHDR PNGARG((png_structp png_ptr, png_uint_32 width, int interlace_type)); extern void png_write_PLTE PNGARG((png_structp png_ptr, png_colorp palette, - int number)); + png_uint_32 number)); extern void png_write_IDAT PNGARG((png_structp png_ptr, png_bytep data, png_uint_32 length)); @@ -999,6 +1041,10 @@ extern void png_write_hIST PNGARG((png_structp png_ptr, png_uint_16p hist, int number)); #endif +#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) +extern int png_check_keyword PNGARG((png_structp png_ptr, png_charpp key)); +#endif + #if defined(PNG_WRITE_tEXt_SUPPORTED) extern void png_write_tEXt PNGARG((png_structp png_ptr, png_charp key, png_charp text, png_uint_32 text_len)); @@ -1034,67 +1080,67 @@ extern void png_write_finish_row PNGARG((png_structp png_ptr)); extern void png_write_start_row PNGARG((png_structp png_ptr)); /* callbacks for png chunks */ -extern void png_read_IHDR PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_IHDR PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int compression_type, int filter_type, int interlace_type)); -extern void png_read_PLTE PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_PLTE PNGARG((png_structp png_ptr, png_infop info_ptr, png_colorp palette, int num)); #if defined(PNG_READ_gAMA_SUPPORTED) -extern void png_read_gAMA PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_gAMA PNGARG((png_structp png_ptr, png_infop info_ptr, double gamma)); #endif #if defined(PNG_READ_sBIT_SUPPORTED) -extern void png_read_sBIT PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_sBIT PNGARG((png_structp png_ptr, png_infop info_ptr, png_color_8p sig_bit)); #endif #if defined(PNG_READ_cHRM_SUPPORTED) -extern void png_read_cHRM PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_cHRM PNGARG((png_structp png_ptr, png_infop info_ptr, double white_x, double white_y, double red_x, double red_y, double green_x, double green_y, double blue_x, double blue_y)); #endif #if defined(PNG_READ_tRNS_SUPPORTED) -extern void png_read_tRNS PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr, png_bytep trans, int num_trans, png_color_16p trans_values)); #endif #if defined(PNG_READ_bKGD_SUPPORTED) -extern void png_read_bKGD PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_bKGD PNGARG((png_structp png_ptr, png_infop info_ptr, png_color_16p background)); #endif #if defined(PNG_READ_hIST_SUPPORTED) -extern void png_read_hIST PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_hIST PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_16p hist)); #endif #if defined(PNG_READ_pHYs_SUPPORTED) -extern void png_read_pHYs PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_pHYs PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type)); #endif #if defined(PNG_READ_oFFs_SUPPORTED) -extern void png_read_oFFs PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_oFFs PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 offset_x, png_uint_32 offset_y, int unit_type)); #endif #if defined(PNG_READ_tIME_SUPPORTED) -extern void png_read_tIME PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_tIME PNGARG((png_structp png_ptr, png_infop info_ptr, png_timep mod_time)); #endif #if defined(PNG_READ_tEXt_SUPPORTED) -extern void png_read_tEXt PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_charp key, png_charp text, png_uint_32 text_len)); #endif #if defined(PNG_READ_zTXt_SUPPORTED) -extern void png_read_zTXt PNGARG((png_structp png_ptr, png_infop info, +extern void png_read_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_charp key, png_charp text, png_uint_32 text_len, int compression)); #endif @@ -1139,18 +1185,26 @@ extern void png_read_transform_info PNGARG((png_structp png_ptr, /* these are the functions that do the transformations */ #if defined(PNG_READ_FILLER_SUPPORTED) extern void png_do_read_filler PNGARG((png_row_infop row_info, - png_bytep row, png_byte filler, png_byte filler_loc)); + png_bytep row, png_byte filler, png_uint_32 filler_loc)); #endif #if defined(PNG_WRITE_FILLER_SUPPORTED) extern void png_do_write_filler PNGARG((png_row_infop row_info, - png_bytep row, png_byte filler_loc)); + png_bytep row, png_uint_32 filler_loc)); #endif #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) extern void png_do_swap PNGARG((png_row_infop row_info, png_bytep row)); #endif +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) +extern void png_do_rgb_to_gray PNGARG((png_row_infop row_info, png_bytep row)); +#endif + +#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) +extern void png_do_gray_to_rgb PNGARG((png_row_infop row_info, png_bytep row)); +#endif + #if defined(PNG_READ_PACK_SUPPORTED) extern void png_do_unpack PNGARG((png_row_infop row_info, png_bytep row)); #endif @@ -1164,14 +1218,6 @@ extern void png_do_unshift PNGARG((png_row_infop row_info, png_bytep row, extern void png_do_invert PNGARG((png_row_infop row_info, png_bytep row)); #endif -extern void png_build_grayscale_palette PNGARG((int bit_depth, - png_colorp palette)); - -#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) -extern void png_do_gray_to_rgb PNGARG((png_row_infop row_info, - png_bytep row)); -#endif - #if defined(PNG_READ_16_TO_8_SUPPORTED) extern void png_do_chop PNGARG((png_row_infop row_info, png_bytep row)); #endif @@ -1179,11 +1225,10 @@ extern void png_do_chop PNGARG((png_row_infop row_info, png_bytep row)); #if defined(PNG_READ_DITHER_SUPPORTED) extern void png_do_dither PNGARG((png_row_infop row_info, png_bytep row, png_bytep palette_lookup, png_bytep dither_lookup)); -#endif - -#if defined(PNG_CORRECT_PALETTE_SUPPORTED) +# if defined(PNG_CORRECT_PALETTE_SUPPORTED) extern void png_correct_palette PNGARG((png_structp png_ptr, png_colorp palette, int num_palette)); +# endif #endif #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) @@ -1226,75 +1271,77 @@ extern void png_do_expand PNGARG((png_row_infop row_info, extern png_uint_32 png_get_uint_32 PNGARG((png_bytep buf)); extern png_uint_16 png_get_uint_16 PNGARG((png_bytep buf)); -/* read bytes into buf, and update png_ptr->crc */ -extern void png_crc_read PNGARG((png_structp png_ptr, png_bytep buf, - png_uint_32 length)); -/* skip length bytes, and update png_ptr->crc */ -extern void png_crc_skip PNGARG((png_structp png_ptr, png_uint_32 length)); - /* the following decodes the appropriate chunks, and does error correction, then calls the appropriate callback for the chunk if it is valid */ /* decode the IHDR chunk */ -extern void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); -extern void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +extern void png_handle_IEND PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #if defined(PNG_READ_gAMA_SUPPORTED) -extern void png_handle_gAMA PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_gAMA PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif #if defined(PNG_READ_sBIT_SUPPORTED) -extern void png_handle_sBIT PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_sBIT PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif #if defined(PNG_READ_cHRM_SUPPORTED) -extern void png_handle_cHRM PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_cHRM PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif #if defined(PNG_READ_tRNS_SUPPORTED) -extern void png_handle_tRNS PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif #if defined(PNG_READ_bKGD_SUPPORTED) -extern void png_handle_bKGD PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_bKGD PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif #if defined(PNG_READ_hIST_SUPPORTED) -extern void png_handle_hIST PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_hIST PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif #if defined(PNG_READ_pHYs_SUPPORTED) -extern void png_handle_pHYs PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_pHYs PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif #if defined(PNG_READ_oFFs_SUPPORTED) -extern void png_handle_oFFs PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_oFFs PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif #if defined(PNG_READ_tIME_SUPPORTED) -extern void png_handle_tIME PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_tIME PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif #if defined(PNG_READ_tEXt_SUPPORTED) -extern void png_handle_tEXt PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif #if defined(PNG_READ_zTXt_SUPPORTED) -extern void png_handle_zTXt PNGARG((png_structp png_ptr, png_infop info, +extern void png_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); #endif +extern void png_handle_unknown PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); + +extern void png_check_chunk_name PNGARG((png_structp png_ptr, + png_bytep chunk_name)); + /* handle the transformations for reading and writing */ extern void png_do_read_transformations PNGARG((png_structp png_ptr)); extern void png_do_write_transformations PNGARG((png_structp png_ptr)); @@ -1302,8 +1349,8 @@ extern void png_do_write_transformations PNGARG((png_structp png_ptr)); extern void png_init_read_transformations PNGARG((png_structp png_ptr)); #ifdef PNG_PROGRESSIVE_READ_SUPPORTED -extern void png_push_read_chunk PNGARG((png_structp png_ptr, png_infop info)); -extern void png_push_read_sig PNGARG((png_structp png_ptr)); +extern void png_push_read_chunk PNGARG((png_structp png_ptr, png_infop info_ptr)); +extern void png_push_read_sig PNGARG((png_structp png_ptr, png_infop info_ptr)); extern void png_push_check_crc PNGARG((png_structp png_ptr)); extern void png_push_crc_skip PNGARG((png_structp png_ptr, png_uint_32 length)); extern void png_push_skip PNGARG((png_structp png_ptr)); @@ -1318,27 +1365,28 @@ extern void png_process_IDAT_data PNGARG((png_structp png_ptr, extern void png_push_process_row PNGARG((png_structp png_ptr)); extern void png_push_handle_PLTE PNGARG((png_structp png_ptr, png_uint_32 length)); -extern void png_push_read_PLTE PNGARG((png_structp png_ptr, png_infop info)); -extern void png_push_handle_tRNS PNGARG((png_structp png_ptr, png_infop info, +extern void png_push_handle_tRNS PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); -extern void png_push_handle_hIST PNGARG((png_structp png_ptr, png_infop info, +extern void png_push_handle_hIST PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); -extern void png_push_have_info PNGARG((png_structp png_ptr, png_infop info)); -extern void png_push_have_end PNGARG((png_structp png_ptr, png_infop info)); +extern void png_push_handle_unknown PNGARG((png_structp png_ptr, png_infop info_ptr, + png_uint_32 length)); +extern void png_push_have_info PNGARG((png_structp png_ptr, png_infop info_ptr)); +extern void png_push_have_end PNGARG((png_structp png_ptr, png_infop info_ptr)); extern void png_push_have_row PNGARG((png_structp png_ptr, png_bytep row)); -extern void png_push_read_end PNGARG((png_structp png_ptr, png_infop info)); +extern void png_push_read_end PNGARG((png_structp png_ptr, png_infop info_ptr)); extern void png_process_some_data PNGARG((png_structp png_ptr, - png_infop info)); + png_infop info_ptr)); extern void png_read_push_finish_row PNGARG((png_structp png_ptr)); #if defined(PNG_READ_tEXt_SUPPORTED) -extern void png_push_handle_tEXt PNGARG((png_structp png_ptr, +extern void png_push_handle_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); -extern void png_push_read_tEXt PNGARG((png_structp png_ptr, png_infop info)); +extern void png_push_read_tEXt PNGARG((png_structp png_ptr, png_infop info_ptr)); #endif #if defined(PNG_READ_zTXt_SUPPORTED) -extern void png_push_handle_zTXt PNGARG((png_structp png_ptr, +extern void png_push_handle_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr, png_uint_32 length)); -extern void png_push_read_zTXt PNGARG((png_structp png_ptr, png_infop info)); +extern void png_push_read_zTXt PNGARG((png_structp png_ptr, png_infop info_ptr)); #endif #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ @@ -1346,7 +1394,7 @@ extern void png_push_read_zTXt PNGARG((png_structp png_ptr, png_infop info)); #endif /* PNG_INTERNAL */ #ifdef PNG_PROGRESSIVE_READ_SUPPORTED -extern void png_process_data PNGARG((png_structp png_ptr, png_infop info, +extern void png_process_data PNGARG((png_structp png_ptr, png_infop info_ptr, png_bytep buffer, png_uint_32 buffer_size)); extern void png_set_progressive_read_fn PNGARG((png_structp png_ptr, png_voidp progressive_ptr, @@ -1356,6 +1404,13 @@ extern void png_progressive_combine_row PNGARG((png_structp png_ptr, png_bytep old_row, png_bytep new_row)); #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ +#if defined(USE_FAR_KEYWORD) /* memory model independent fns */ +extern void *far_to_near PNGARG((png_structp png_ptr,png_voidp ptr,int check)); +#endif /* defined(USE_FAR_KEYWORD) */ + +#ifdef __cplusplus +} +#endif + /* do not put anything past this line */ #endif /* _PNG_H */ - diff --git a/pngconf.h b/pngconf.h index 35b5b967..03df3259 100644 --- a/pngconf.h +++ b/pngconf.h @@ -1,10 +1,10 @@ /* pngconf.c - machine configurable file for libpng - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + December 3, 1996 */ /* Any machine specific code is near the front of this file, so if you @@ -15,7 +15,7 @@ #ifndef PNGCONF_H #define PNGCONF_H -/* this is the size of the compression buffer, and thus the size of +/* This is the size of the compression buffer, and thus the size of an IDAT chunk. Make this whatever size you feel is best for your machine. One of these will be allocated per png_struct. When this is full, it writes the data to the disk, and does some other @@ -29,14 +29,7 @@ #define PNG_ZBUF_SIZE 8192 -/* While libpng currently uses zlib for it's compression, it has been designed - to stand on it's own. Towards this end, there are two defines that are - used to help portability between machines. To make it simpler to - setup libpng on a machine, this currently uses zlib's definitions, so - any changes should be made in zlib. Libpng will check zlib's settings - and adjust it's own accordingly. */ - -/* if you are running on a machine where you cannot allocate more then +/* If you are running on a machine where you cannot allocate more then 64K of memory, uncomment this. While libpng will not normally need that much memory in a chunk (unless you load up a very large file), zlib needs to know how big of a chunk it can use, and libpng thus @@ -48,7 +41,7 @@ #define PNG_MAX_ALLOC_64K #endif -/* this protects us against compilers which run on a windowing system +/* This protects us against compilers which run on a windowing system and thus don't have or would rather us not use the stdio types: stdin, stdout, and stderr. The only one currently used is stderr in png_error() and png_warning(). #defining PNG_NO_STDIO will @@ -56,14 +49,19 @@ /* #define PNG_NO_STDIO */ -/* this macro protects us against machines that don't have function - prototypes. If your compiler does not handle function prototypes, - define this macro. I've always been able to use _NO_PROTO as the - indicator, but you may need to drag the empty declaration out in - front of here, or change the ifdef to suit your own needs. */ +/* for FILE. If you are not using standard io, you don't need this */ +#ifndef PNG_NO_STDIO +#include +#endif + +/* This macro protects us against machines that don't have function + prototypes (ie K&R style headers). If your compiler does not handle + function prototypes, define this macro. I've always been able to use + _NO_PROTO as the indicator, but you may need to drag the empty declaration + out in front of here, or change the ifdef to suit your own needs. */ #ifndef PNGARG -#ifdef OF +#ifdef OF /* Zlib prototype munger */ #define PNGARG(arglist) OF(arglist) #else @@ -77,39 +75,65 @@ #endif /* PNGARG */ +/* Try to determine if we are compiling on a Mac */ +#if defined(__MWERKS__) ||defined(applec) ||defined(THINK_C) ||defined(__SC__) +#define MACOS +#endif + /* enough people need this for various reasons to include it here */ #if !defined(MACOS) && !defined(RISCOS) #include #endif + /* need the time information for reading tIME chunks */ #include -/* for FILE. If you are not using standard io, you don't need this */ -#include +/* This is an attempt to force a single setjmp behaviour on Linux */ +#ifdef linux +#ifdef _BSD_SOURCE +#define _PNG_SAVE_BSD_SOURCE +#undef _BSD_SOURCE +#endif +#ifdef _SETJMP_H +error: png.h already includes setjmp.h +#endif +#endif /* linux */ /* include setjmp.h for error handling */ #include +#ifdef linux +#ifdef _PNG_SAVE_BSD_SOURCE +#define _BSD_SOURCE +#undef _PNG_SAVE_BSD_SOURCE +#endif +#endif /* linux */ + #ifdef BSD #include #else #include #endif -/* other defines for things like memory and the like can go here. These +/* Other defines for things like memory and the like can go here. These are the only files included in libpng, so if you need to change them, change them here. They are only included if PNG_INTERNAL is defined. */ #ifdef PNG_INTERNAL #include #include -#include -/* other defines specific to compilers can go here. Try to keep +/* Other defines specific to compilers can go here. Try to keep them inside an appropriate ifdef/endif pair for portability */ -/* for some reason, Borland C++ defines memcmp, etc. in mem.h, not +#ifdef MACOS +#include +#else +#include +#endif + +/* For some reason, Borland C++ defines memcmp, etc. in mem.h, not stdlib.h like it should (I think). Or perhaps this is a C++ - feature */ + feature? */ #ifdef __TURBOC__ #include #include "alloc.h" @@ -119,17 +143,17 @@ #include #endif -/* this controls how fine the dithering gets. As this allocates +/* This controls how fine the dithering gets. As this allocates a largish chunk of memory (32K), those who are not as concerned - with dithering quality can decrease some or all of these */ + with dithering quality can decrease some or all of these. */ #define PNG_DITHER_RED_BITS 5 #define PNG_DITHER_GREEN_BITS 5 #define PNG_DITHER_BLUE_BITS 5 -/* this controls how fine the gamma correction becomes when you +/* This controls how fine the gamma correction becomes when you are only interested in 8 bits anyway. Increasing this value results in more memory being used, and more pow() functions - being called to fill in the gamma tables. Don't get this + being called to fill in the gamma tables. Don't set this value less then 8, and even that may not work (I haven't tested it). */ @@ -137,32 +161,33 @@ #endif /* PNG_INTERNAL */ -/* the following uses const char * instead of char * for error +/* The following uses const char * instead of char * for error and warning message functions, so some compilers won't complain. If you want to use const, define PNG_USE_CONST here. It is not normally defined to make configuration easier, as it is not a critical part of the code. */ +#define PNG_USE_CONST + #ifdef PNG_USE_CONST # define PNG_CONST const #else # define PNG_CONST #endif -/* The following defines give you the ability to remove code - from the library that you will not be using. I wish I - could figure out how to automate this, but I can't do - that without making it seriously hard on the users. So - if you are not using an ability, change the #define to - and #undef, and that part of the library will not be - compiled. If your linker can't find a function, you - may want to make sure the ability is defined here. - Some of these depend upon some others being defined. - I haven't figured out all the interactions here, so - you may have to experiment awhile to get everything - to compile. - */ +/* The following defines give you the ability to remove code from the + library that you will not be using. I wish I could figure out how to + automate this, but I can't do that without making it seriously hard + on the users. So if you are not using an ability, change the #define + to and #undef, and that part of the library will not be compiled. If + your linker can't find a function, you may want to make sure the + ability is defined here. Some of these depend upon some others being + defined. I haven't figured out all the interactions here, so you may + have to experiment awhile to get everything to compile. If you are + creating or using a shared library, you probably shouldn't touch this, + as it will affect the size of the structures, and this will cause bad + things to happen if the library and/or application ever change. */ /* Any transformations you will not be using can be undef'ed here */ #define PNG_PROGRESSIVE_READ_SUPPORTED @@ -179,7 +204,6 @@ #define PNG_READ_FILLER_SUPPORTED #define PNG_READ_GAMMA_SUPPORTED #define PNG_READ_GRAY_TO_RGB_SUPPORTED -#undef PNG_CORRECT_PALETTE_SUPPORTED #define PNG_WRITE_INTERLACING_SUPPORTED #define PNG_WRITE_SHIFT_SUPPORTED @@ -190,6 +214,10 @@ #define PNG_WRITE_FILLER_SUPPORTED #define PNG_WRITE_FLUSH_SUPPORTED +/* These functions are turned off by default, as they will be phased out. */ +#undef PNG_USE_OWN_CRC +#undef PNG_CORRECT_PALETTE_SUPPORTED + /* any chunks you are not interested in, you can undef here. The ones that allocate memory may be expecially important (hIST, tEXt, zTXt, tRNS) Others will just save time and make png_info @@ -221,10 +249,10 @@ #define PNG_WRITE_tEXt_SUPPORTED #define PNG_WRITE_zTXt_SUPPORTED -/* some typedefs to get us started. These should be safe on most of the - common platforms. The typedefs should be at least as large - as the numbers suggest (a png_uint_32 must be at least 32 bits long), - but they don't have to be exactly that size. */ +/* Some typedefs to get us started. These should be safe on most of the + common platforms. The typedefs should be at least as large as the + numbers suggest (a png_uint_32 must be at least 32 bits long), but they + don't have to be exactly that size. */ typedef unsigned long png_uint_32; typedef long png_int_32; @@ -232,7 +260,7 @@ typedef unsigned short png_uint_16; typedef short png_int_16; typedef unsigned char png_byte; -/* this is usually size_t. it is typedef'ed just in case you need it to +/* This is usually size_t. It is typedef'ed just in case you need it to change (I'm not sure if you will or not, so I thought I'd be safe) */ typedef size_t png_size_t; @@ -243,10 +271,10 @@ typedef size_t png_size_t; model independent data. As of version Zlib .95, the necessary changes have been made in Zlib. The USE_FAR_KEYWORD define triggers other changes that are needed. Most of the far keyword changes are hidden - inside typedefs with suffix "f". Tim Wegner */ + inside typedefs with suffix "f". (Tim Wegner) */ -/* SJT: Separate compiler dependencies */ -/* SJT: problem here is that zlib.h always defines FAR */ +/* Separate compiler dependencies (problem here is that zlib.h always + defines FAR. (SJT) */ #ifdef __BORLANDC__ #if defined(__LARGE__) || defined(__HUGE__) || defined(__COMPACT__) #define LDATA 1 @@ -263,9 +291,9 @@ typedef size_t png_size_t; #define USE_FAR_KEYWORD #endif /* LDATA != 1 */ -/* SJT: Possibly useful for moving data out of default segment. - Uncomment it if you want. Could also define FARDATA as const - if your compiler supports it. +/* Possibly useful for moving data out of default segment. + Uncomment it if you want. Could also define FARDATA as + const if your compiler supports it. (SJT) # define FARDATA FAR */ #endif /* __WIN32__, __FLAT__ */ @@ -273,16 +301,16 @@ typedef size_t png_size_t; #endif /* __BORLANDC__ */ -/* SJT: Suggest testing for specific compiler first before - testing for FAR. The Watcom compiler defines both __MEDIUM__ - and M_I86MM, making reliance oncertain keywords suspect -*/ +/* Suggest testing for specific compiler first before testing for + FAR. The Watcom compiler defines both __MEDIUM__ and M_I86MM, + making reliance oncertain keywords suspect. (SJT) */ /* MSC Medium model */ #if defined(FAR) # if defined(M_I86MM) # define USE_FAR_KEYWORD -# define FARDATA FAR /* SJT: added */ +# define FARDATA FAR +# include # endif #endif @@ -293,21 +321,16 @@ typedef size_t png_size_t; /* SJT: At this point FAR is always defined */ -/* not used anymore, but kept for compatability */ -typedef unsigned char FAR png_bytef; - /* SJT: */ #ifndef FARDATA #define FARDATA #endif -/* End medium model changes to be in zconf.h */ - -/* SJT: More typedefs */ -typedef void FAR * png_voidp; - +/* Not used anymore (as of 0.88), but kept for compatability (for now). */ +typedef unsigned char FAR png_bytef; /* SJT: Add typedefs for pointers */ +typedef void FAR * png_voidp; typedef png_byte FAR * png_bytep; typedef png_uint_32 FAR * png_uint_32p; typedef png_int_32 FAR * png_int_32p; @@ -332,12 +355,17 @@ typedef char FAR * FAR * png_charpp; */ typedef charf * png_zcharp; typedef charf * FAR * png_zcharpp; -typedef z_stream * png_zstreamp; /* zlib won't accept far z_stream */ - +typedef z_stream FAR * png_zstreamp; /* User may want to use these so not in PNG_INTERNAL. Any library functions that are passed far data must be model independent. */ + #if defined(USE_FAR_KEYWORD) /* memory model independent fns */ +/* use this to make far-to-near assignments */ +# define CHECK 1 +# define NOCHECK 0 +# define CVT_PTR(ptr) (far_to_near(png_ptr,ptr,CHECK)) +# define CVT_PTR_NOCHECK(ptr) (far_to_near(png_ptr,ptr,NOCHECK)) # define png_strcpy _fstrcpy # define png_strcat _fstrcat # define png_strlen _fstrlen @@ -346,6 +374,8 @@ typedef z_stream * png_zstreamp; /* zlib won't accept far z_stream */ # define png_memcpy _fmemcpy # define png_memset _fmemset #else /* use the usual functions */ +# define CVT_PTR(ptr) (ptr) +# define CVT_PTR_NOCHECK(ptr) (ptr) # define png_strcpy strcpy # define png_strcat strcat # define png_strlen strlen diff --git a/pngerror.c b/pngerror.c index 0e286ae4..8ce44c3f 100644 --- a/pngerror.c +++ b/pngerror.c @@ -1,10 +1,10 @@ /* pngerror.c - stub functions for i/o and memory allocation - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 This file provides a location for all error handling. Users which need special error handling are expected to write replacement functions diff --git a/pngmem.c b/pngmem.c index 60645f0a..4c951170 100644 --- a/pngmem.c +++ b/pngmem.c @@ -1,10 +1,10 @@ /* pngmem.c - stub functions for memory allocation - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 This file provides a location for all memory allocation. Users which need special memory handling are expected to modify the code in this file @@ -18,12 +18,11 @@ /* if you change this, be sure to change the one in png.h also */ /* Allocate memory for a png_struct. The malloc and memset can be replaced - * by a single call to calloc() if this is thought to improve performance. - */ + by a single call to calloc() if this is thought to improve performance. */ png_voidp -png_create_struct(uInt type) +png_create_struct(int type) { - png_size_t type; + png_size_t size; png_voidp struct_ptr; if (type == PNG_STRUCT_INFO) @@ -66,7 +65,7 @@ png_destroy_struct(png_voidp struct_ptr) */ png_voidp -png_large_malloc(png_structp png_ptr, png_uint_32 size) +png_malloc(png_structp png_ptr, png_uint_32 size) { png_voidp ret; if (!png_ptr || !size) @@ -160,11 +159,11 @@ png_large_malloc(png_structp png_ptr, png_uint_32 size) return ret; } -/* free a pointer allocated by png_large_malloc(). In the default - configuration, png_ptr is not used, but is passed in case it - is needed. If ptr is NULL, return without taking any action. */ +/* free a pointer allocated by png_malloc(). In the default + configuration, png_ptr is not used, but is passed in case it + is needed. If ptr is NULL, return without taking any action. */ void -png_large_free(png_structp png_ptr, png_voidp ptr) +png_free(png_structp png_ptr, png_voidp ptr) { if (!png_ptr) return; @@ -201,11 +200,10 @@ png_large_free(png_structp png_ptr, png_voidp ptr) #else /* Not the Borland DOS special memory handler */ /* Allocate memory for a png_struct or a png_info. The malloc and - * memset can be replaced by a single call to calloc() if this is thought - * to improve performance noticably. - */ + memset can be replaced by a single call to calloc() if this is thought + to improve performance noticably.*/ png_voidp -png_create_struct(uInt type) +png_create_struct(int type) { size_t size; png_voidp struct_ptr; @@ -257,8 +255,9 @@ png_destroy_struct(png_voidp struct_ptr) need to allocate exactly 64K, so whatever you call here must have the ability to do that. */ +#ifndef FORTIFY png_voidp -png_large_malloc(png_structp png_ptr, png_uint_32 size) +png_malloc(png_structp png_ptr, png_uint_32 size) { png_voidp ret; if (!png_ptr || !size) @@ -287,11 +286,11 @@ png_large_malloc(png_structp png_ptr, png_uint_32 size) return ret; } -/* free a pointer allocated by png_large_malloc(). In the default +/* free a pointer allocated by png_malloc(). In the default configuration, png_ptr is not used, but is passed in case it is needed. If ptr is NULL, return without taking any action. */ void -png_large_free(png_structp png_ptr, png_voidp ptr) +png_free(png_structp png_ptr, png_voidp ptr) { if (!png_ptr) return; @@ -310,76 +309,6 @@ png_large_free(png_structp png_ptr, png_voidp ptr) } } +#endif /* FORTIFY */ #endif /* Not Borland DOS special memory handler */ -/* Allocate memory. This is called for smallish blocks only It - should not get anywhere near 64K. On segmented machines, this - must come from the local heap (for zlib). Currently, zlib is - the only one that uses this, so you should only get one call - to this, and that a small block. */ -void * -png_malloc(png_structp png_ptr, png_uint_32 size) -{ - void *ret; - - if (!png_ptr || !size) - { - return ((void *)0); - } - -#ifdef PNG_MAX_MALLOC_64K - if (size > (png_uint_32)65536L) - png_error(png_ptr, "Cannot Allocate > 64K"); -#endif - - - ret = malloc((png_size_t)size); - - if (!ret) - { - png_error(png_ptr, "Out of Memory"); - } - - return ret; -} - -/* Reallocate memory. This will not get near 64K on a - even marginally reasonable file. This is not used in - the current version of the library. */ -void * -png_realloc(png_structp png_ptr, void * ptr, png_uint_32 size, - png_uint_32 old_size) -{ - void *ret; - - if (!png_ptr || !old_size || !ptr || !size) - return ((void *)0); - -#ifdef PNG_MAX_MALLOC_64K - if (size > (png_uint_32)65536L) - png_error(png_ptr, "Cannot Allocate > 64K"); -#endif - - ret = realloc(ptr, (png_size_t)size); - - if (!ret) - { - png_error(png_ptr, "Out of Memory 7"); - } - - return ret; -} - -/* free a pointer allocated by png_malloc(). In the default - configuration, png_ptr is not used, but is passed incase it - is needed. If ptr is NULL, return without taking any action. */ -void -png_free(png_structp png_ptr, void * ptr) -{ - if (!png_ptr) - return; - - if (ptr != (void *)0) - free(ptr); -} - diff --git a/pngpread.c b/pngpread.c index bcac0eaf..284b749a 100644 --- a/pngpread.c +++ b/pngpread.c @@ -1,10 +1,10 @@ /* pngpread.c - read a png file in push mode - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 */ #define PNG_INTERNAL @@ -13,30 +13,33 @@ #ifdef PNG_PROGRESSIVE_READ_SUPPORTED void -png_process_data(png_structp png_ptr, png_infop info, +png_process_data(png_structp png_ptr, png_infop info_ptr, png_bytep buffer, png_uint_32 buffer_size) { png_push_restore_buffer(png_ptr, buffer, buffer_size); while (png_ptr->buffer_size) { - png_process_some_data(png_ptr, info); + png_process_some_data(png_ptr, info_ptr); } } +/* What we do with the incoming data depends on what we were previously + * doing before we ran out of data... + */ void -png_process_some_data(png_structp png_ptr, png_infop info) +png_process_some_data(png_structp png_ptr, png_infop info_ptr) { switch (png_ptr->process_mode) { case PNG_READ_SIG_MODE: { - png_push_read_sig(png_ptr); + png_push_read_sig(png_ptr, info_ptr); break; } case PNG_READ_CHUNK_MODE: { - png_push_read_chunk(png_ptr, info); + png_push_read_chunk(png_ptr, info_ptr); break; } case PNG_READ_IDAT_MODE: @@ -44,28 +47,23 @@ png_process_some_data(png_structp png_ptr, png_infop info) png_push_read_IDAT(png_ptr); break; } - case PNG_READ_PLTE_MODE: - { - png_push_read_PLTE(png_ptr, info); - break; - } #if defined(PNG_READ_tEXt_SUPPORTED) case PNG_READ_tEXt_MODE: { - png_push_read_tEXt(png_ptr, info); + png_push_read_tEXt(png_ptr, info_ptr); break; } #endif #if defined(PNG_READ_zTXt_SUPPORTED) case PNG_READ_zTXt_MODE: { - png_push_read_zTXt(png_ptr, info); + png_push_read_zTXt(png_ptr, info_ptr); break; } #endif case PNG_READ_END_MODE: { - png_push_read_end(png_ptr, info); + png_push_read_chunk(png_ptr, info_ptr); break; } case PNG_SKIP_MODE: @@ -81,35 +79,54 @@ png_process_some_data(png_structp png_ptr, png_infop info) } } +/* Read any remaining signature bytes from the stream and compare them with + * the correct PNG signature. It is possible that this routine is called + * with bytes already read from the signature, whether because they have been + * checked by the calling application, or from multiple calls to this routine. + */ void -png_push_read_sig(png_structp png_ptr) +png_push_read_sig(png_structp png_ptr, png_infop info_ptr) { - png_byte sig[8]; + int num_checked = png_ptr->sig_bytes, + num_to_check = 8 - num_checked; - if (png_ptr->buffer_size < 8) + if (png_ptr->buffer_size < num_to_check) { - png_push_save_buffer(png_ptr); - return; + num_to_check = png_ptr->buffer_size; } - png_push_fill_buffer(png_ptr, sig, 8); + png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]), + (png_uint_32)num_to_check); + png_ptr->sig_bytes += num_to_check; - if (png_check_sig(sig, 8)) + if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) { - png_ptr->process_mode = PNG_READ_CHUNK_MODE; + if (num_checked < 4 && + png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) + png_error(png_ptr, "Not a PNG file"); + else + png_error(png_ptr, "PNG file corrupted by ASCII conversion"); } else { - png_error(png_ptr, "Not a PNG file"); + if (png_ptr->sig_bytes >= 8) + { + png_ptr->process_mode = PNG_READ_CHUNK_MODE; + } } } void -png_push_read_chunk(png_structp png_ptr, png_infop info) +png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) { + /* First we make sure we have enough data for the 4 byte chunk name + and the 4 byte chunk length before proceeding with decoding the + chunk data. To fully decode each of these chunks, we also make + sure we have enough data in the buffer for the 4 byte CRC at the + end of every chunk (except IDAT, which is handled separately). */ if (!(png_ptr->flags & PNG_FLAG_HAVE_CHUNK_HEADER)) { - png_byte chunk_start[8]; + png_byte chunk_length[4]; if (png_ptr->buffer_size < 8) { @@ -117,61 +134,57 @@ png_push_read_chunk(png_structp png_ptr, png_infop info) return; } - png_push_fill_buffer(png_ptr, chunk_start, 8); - png_ptr->push_length = png_get_uint_32(chunk_start); - png_memcpy(png_ptr->push_chunk_name, (png_voidp)(chunk_start + 4), 4); - png_ptr->flags |= PNG_FLAG_HAVE_CHUNK_HEADER; + png_push_fill_buffer(png_ptr, chunk_length, 4); + png_ptr->push_length = png_get_uint_32(chunk_length); png_reset_crc(png_ptr); - png_calculate_crc(png_ptr, chunk_start + 4, 4); + png_push_fill_buffer(png_ptr, png_ptr->chunk_name, 4); + png_calculate_crc(png_ptr, png_ptr->chunk_name, 4); + png_ptr->flags |= PNG_FLAG_HAVE_CHUNK_HEADER; } - if (!png_memcmp(png_ptr->push_chunk_name, png_IHDR, 4)) + if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) { - if (png_ptr->mode != PNG_BEFORE_IHDR) - png_error(png_ptr, "Out of place IHDR"); - if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } - png_handle_IHDR(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); - png_ptr->mode = PNG_HAVE_IHDR; + png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length); } - else if (!png_memcmp(png_ptr->push_chunk_name, png_PLTE, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) { - if (png_ptr->mode != PNG_HAVE_IHDR) - png_error(png_ptr, "Missing IHDR"); - -#if !defined(PNG_READ_OPT_PLTE_SUPPORTED) - if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) - png_push_crc_skip(png_ptr, length); - else -#else + if (png_ptr->push_length + 4 > png_ptr->buffer_size) { - png_push_handle_PLTE(png_ptr, png_ptr->push_length); + png_push_save_buffer(png_ptr); + return; } -#endif - png_ptr->mode = PNG_HAVE_PLTE; + + png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length); } - else if (!png_memcmp(png_ptr->push_chunk_name, png_IDAT, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) { + /* If we reach an IDAT chunk, this means we have read all of the + header chunks, and we can start reading the image (or if this + is called after the image has been read - we have an error). */ + if (png_ptr->mode & PNG_HAVE_IDAT) + { + if (png_ptr->push_length == 0) + return; + + if (png_ptr->mode & PNG_AFTER_IDAT) + png_error(png_ptr, "Too many IDAT's found"); + } + png_ptr->idat_size = png_ptr->push_length; - png_ptr->mode = PNG_HAVE_IDAT; + png_ptr->mode |= PNG_HAVE_IDAT; png_ptr->process_mode = PNG_READ_IDAT_MODE; - png_push_have_info(png_ptr, info); - png_ptr->zstream->avail_out = (uInt)png_ptr->irowbytes; - png_ptr->zstream->next_out = png_ptr->row_buf; + png_push_have_info(png_ptr, info_ptr); + png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; + png_ptr->zstream.next_out = png_ptr->row_buf; return; } - else if (!png_memcmp(png_ptr->push_chunk_name, png_IEND, 4)) - { - png_error(png_ptr, "No image in file"); - } -#if defined(PNG_READ_gAMA_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_gAMA, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { @@ -179,15 +192,24 @@ png_push_read_chunk(png_structp png_ptr, png_infop info) return; } - if (png_ptr->mode != PNG_HAVE_IHDR) - png_error(png_ptr, "Out of place gAMA"); + png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length); + png_ptr->process_mode = PNG_READ_DONE_MODE; + png_push_have_end(png_ptr, info_ptr); + } +#if defined(PNG_READ_gAMA_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) + { + if (png_ptr->push_length + 4 > png_ptr->buffer_size) + { + png_push_save_buffer(png_ptr); + return; + } - png_handle_gAMA(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); + png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length); } #endif #if defined(PNG_READ_sBIT_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_sBIT, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { @@ -195,15 +217,11 @@ png_push_read_chunk(png_structp png_ptr, png_infop info) return; } - if (png_ptr->mode != PNG_HAVE_IHDR) - png_error(png_ptr, "Out of place sBIT"); - - png_handle_sBIT(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); + png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length); } #endif #if defined(PNG_READ_cHRM_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_cHRM, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { @@ -211,31 +229,23 @@ png_push_read_chunk(png_structp png_ptr, png_infop info) return; } - if (png_ptr->mode != PNG_HAVE_IHDR) - png_error(png_ptr, "Out of place cHRM"); - - png_handle_cHRM(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); + png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length); } #endif #if defined(PNG_READ_tRNS_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_tRNS, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { png_push_save_buffer(png_ptr); return; } - if (png_ptr->mode != PNG_HAVE_IHDR && - png_ptr->mode != PNG_HAVE_PLTE) - png_error(png_ptr, "Out of place tRNS"); - png_handle_tRNS(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); + png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length); } #endif #if defined(PNG_READ_bKGD_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_bKGD, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { @@ -243,16 +253,11 @@ png_push_read_chunk(png_structp png_ptr, png_infop info) return; } - if (png_ptr->mode != PNG_HAVE_IHDR && - png_ptr->mode != PNG_HAVE_PLTE) - png_error(png_ptr, "Out of place bKGD"); - - png_handle_bKGD(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); + png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length); } #endif #if defined(PNG_READ_hIST_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_hIST, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { @@ -260,15 +265,11 @@ png_push_read_chunk(png_structp png_ptr, png_infop info) return; } - if (png_ptr->mode != PNG_HAVE_PLTE) - png_error(png_ptr, "Out of place hIST"); - - png_handle_hIST(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); + png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length); } #endif #if defined(PNG_READ_pHYs_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_pHYs, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { @@ -276,16 +277,11 @@ png_push_read_chunk(png_structp png_ptr, png_infop info) return; } - if (png_ptr->mode != PNG_HAVE_IHDR && - png_ptr->mode != PNG_HAVE_PLTE) - png_error(png_ptr, "Out of place pHYs"); - - png_handle_pHYs(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); + png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length); } #endif #if defined(PNG_READ_oFFs_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_oFFs, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { @@ -293,16 +289,11 @@ png_push_read_chunk(png_structp png_ptr, png_infop info) return; } - if (png_ptr->mode != PNG_HAVE_IHDR && - png_ptr->mode != PNG_HAVE_PLTE) - png_error(png_ptr, "Out of place oFFs"); - - png_handle_oFFs(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); + png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length); } #endif #if defined(PNG_READ_tIME_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_tIME, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) { if (png_ptr->push_length + 4 > png_ptr->buffer_size) { @@ -310,81 +301,29 @@ png_push_read_chunk(png_structp png_ptr, png_infop info) return; } - if (png_ptr->mode == PNG_BEFORE_IHDR || - png_ptr->mode == PNG_AFTER_IEND) - png_error(png_ptr, "Out of place tIME"); - - png_handle_tIME(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); + png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length); } #endif #if defined(PNG_READ_tEXt_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_tEXt, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) { - if (png_ptr->mode == PNG_BEFORE_IHDR || - png_ptr->mode == PNG_AFTER_IEND) - png_error(png_ptr, "Out of place tEXt"); - - png_push_handle_tEXt(png_ptr, png_ptr->push_length); + png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length); } #endif #if defined(PNG_READ_zTXt_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_zTXt, 4)) + else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) { - if (png_ptr->mode == PNG_BEFORE_IHDR || - png_ptr->mode == PNG_AFTER_IEND) - png_error(png_ptr, "Out of place zTXt"); - - png_push_handle_zTXt(png_ptr, png_ptr->push_length); + png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length); } #endif else { - if (png_ptr->push_chunk_name[0] <41 || png_ptr->push_chunk_name[0]> 122 || - (png_ptr->push_chunk_name[0]>90 && png_ptr->push_chunk_name[0]< 97) || - png_ptr->push_chunk_name[1] <41 || png_ptr->push_chunk_name[1]> 122 || - (png_ptr->push_chunk_name[1]>90 && png_ptr->push_chunk_name[1]< 97) || - png_ptr->push_chunk_name[2] <41 || png_ptr->push_chunk_name[2]> 122 || - (png_ptr->push_chunk_name[2]>90 && png_ptr->push_chunk_name[2]< 97) || - png_ptr->push_chunk_name[3] <41 || png_ptr->push_chunk_name[3]> 122 || - (png_ptr->push_chunk_name[3]>90 && png_ptr->push_chunk_name[3]< 97)) - { - char msg[200]; - - sprintf(msg, "Invalid chunk type 0x%02X 0x%02X 0x%02X 0x%02X", - png_ptr->push_chunk_name[0], png_ptr->push_chunk_name[1], - png_ptr->push_chunk_name[2], png_ptr->push_chunk_name[3]); - png_error(png_ptr, msg); - } - - if ((png_ptr->push_chunk_name[0] & 0x20) == 0) - { - char msg[200]; - - sprintf(msg, "Unknown critical chunk %c%c%c%c", - png_ptr->push_chunk_name[0], png_ptr->push_chunk_name[1], - png_ptr->push_chunk_name[2], png_ptr->push_chunk_name[3]); - png_error(png_ptr, msg); - } - - png_push_crc_skip(png_ptr, png_ptr->push_length); + png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length); } + png_ptr->flags &= ~PNG_FLAG_HAVE_CHUNK_HEADER; } -void -png_push_check_crc(png_structp png_ptr) -{ - png_byte crc_buf[4]; - png_uint_32 crc; - - png_push_fill_buffer(png_ptr, crc_buf, 4); - crc = png_get_uint_32(crc_buf); - if (((crc ^ 0xffffffffL) & 0xffffffffL) != - (png_ptr->crc & 0xffffffffL)) - png_error(png_ptr, "Bad CRC value"); -} - void png_push_crc_skip(png_structp png_ptr, png_uint_32 length) { @@ -434,8 +373,13 @@ png_push_skip(png_structp png_ptr) png_push_save_buffer(png_ptr); return; } - png_push_check_crc(png_ptr); - png_ptr->process_mode = PNG_READ_CHUNK_MODE; + + png_crc_finish(png_ptr, 0); + + if (png_ptr->mode & PNG_AFTER_IDAT) + png_ptr->process_mode = PNG_READ_END_MODE; + else + png_ptr->process_mode = PNG_READ_CHUNK_MODE; } } @@ -500,17 +444,17 @@ png_push_save_buffer(png_structp png_ptr) if (png_ptr->save_buffer_size + png_ptr->current_buffer_size > png_ptr->save_buffer_max) { - int new_max; + png_uint_32 new_max; png_bytep old_buffer; new_max = (int)(png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256); old_buffer = png_ptr->save_buffer; png_ptr->save_buffer = (png_bytep) - png_large_malloc(png_ptr, new_max); + png_malloc(png_ptr, new_max); png_memcpy(png_ptr->save_buffer, old_buffer, (png_size_t)png_ptr->save_buffer_size); - png_large_free(png_ptr, old_buffer); + png_free(png_ptr, old_buffer); png_ptr->save_buffer_max = new_max; } if (png_ptr->current_buffer_size) @@ -541,7 +485,7 @@ png_push_read_IDAT(png_structp png_ptr) { if (!(png_ptr->flags & PNG_FLAG_HAVE_CHUNK_HEADER)) { - png_byte chunk_start[8]; + png_byte chunk_length[4]; if (png_ptr->buffer_size < 8) { @@ -549,14 +493,15 @@ png_push_read_IDAT(png_structp png_ptr) return; } - png_push_fill_buffer(png_ptr, chunk_start, 8); - png_ptr->push_length = png_get_uint_32(chunk_start); - png_memcpy(png_ptr->push_chunk_name, - (png_voidp)(chunk_start + 4), 4); - png_ptr->flags |= PNG_FLAG_HAVE_CHUNK_HEADER; + png_push_fill_buffer(png_ptr, chunk_length, 4); + png_ptr->push_length = png_get_uint_32(chunk_length); + png_reset_crc(png_ptr); - png_calculate_crc(png_ptr, chunk_start + 4, 4); - if (png_memcmp(png_ptr->push_chunk_name, png_IDAT, 4)) + png_push_fill_buffer(png_ptr, png_ptr->chunk_name, 4); + png_calculate_crc(png_ptr, chunk_length, 4); + png_ptr->flags |= PNG_FLAG_HAVE_CHUNK_HEADER; + + if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) { png_ptr->process_mode = PNG_READ_END_MODE; if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) @@ -608,7 +553,7 @@ png_push_read_IDAT(png_structp png_ptr) return; } - png_push_check_crc(png_ptr); + png_crc_finish(png_ptr, 0); png_ptr->flags &= ~PNG_FLAG_HAVE_CHUNK_HEADER; } } @@ -622,33 +567,33 @@ png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length) png_error(png_ptr, "Extra compression data"); - png_ptr->zstream->next_in = buffer; - png_ptr->zstream->avail_in = (uInt)buffer_length; + png_ptr->zstream.next_in = buffer; + png_ptr->zstream.avail_in = (uInt)buffer_length; do { - ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH); + ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); if (ret == Z_STREAM_END) { - if (png_ptr->zstream->avail_in) + if (png_ptr->zstream.avail_in) png_error(png_ptr, "Extra compressed data"); - if (!png_ptr->zstream->avail_out) + if (!png_ptr->zstream.avail_out) { png_push_process_row(png_ptr); } - png_ptr->mode = PNG_AT_LAST_IDAT; + png_ptr->mode = PNG_AFTER_IDAT; png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; break; } if (ret != Z_OK) png_error(png_ptr, "Decompression Error"); - if (!(png_ptr->zstream->avail_out)) + if (!(png_ptr->zstream.avail_out)) { png_push_process_row(png_ptr); - png_ptr->zstream->avail_out = (uInt)png_ptr->irowbytes; - png_ptr->zstream->next_out = png_ptr->row_buf; + png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; + png_ptr->zstream.next_out = png_ptr->row_buf; } - } while (png_ptr->zstream->avail_in); + } while (png_ptr->zstream.avail_in); } void @@ -829,54 +774,14 @@ png_read_push_finish_row(png_structp png_ptr) } } - -void -png_push_handle_PLTE(png_structp png_ptr, png_uint_32 length) -{ - if (length % 3) - png_error(png_ptr, "Invalid palette chunk"); - - png_ptr->num_palette = (png_uint_16)(length / 3); - png_ptr->cur_palette = 0; - png_ptr->palette = (png_colorp)png_large_malloc(png_ptr, - png_ptr->num_palette * sizeof (png_color)); - png_ptr->process_mode = PNG_READ_PLTE_MODE; -} - -void -png_push_read_PLTE(png_structp png_ptr, png_infop info) -{ - while (png_ptr->cur_palette < png_ptr->num_palette && - png_ptr->buffer_size >= 3) - { - png_byte buf[3]; - png_push_fill_buffer(png_ptr, buf, 3); - png_calculate_crc(png_ptr, buf, 3); - - /* don't depend upon png_color being any order */ - png_ptr->palette[png_ptr->cur_palette].red = buf[0]; - png_ptr->palette[png_ptr->cur_palette].green = buf[1]; - png_ptr->palette[png_ptr->cur_palette].blue = buf[2]; - png_ptr->cur_palette++; - } - if (png_ptr->cur_palette == png_ptr->num_palette && - png_ptr->buffer_size >= 4) - { - png_push_check_crc(png_ptr); - png_read_PLTE(png_ptr, info, png_ptr->palette, png_ptr->num_palette); - png_ptr->process_mode = PNG_READ_CHUNK_MODE; - } - else - { - png_push_save_buffer(png_ptr); - } -} - #if defined(PNG_READ_tEXt_SUPPORTED) void -png_push_handle_tEXt(png_structp png_ptr, png_uint_32 length) +png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { - png_ptr->current_text = (png_charp)png_large_malloc(png_ptr, length + 1); + if (png_ptr->mode == PNG_BEFORE_IHDR || png_ptr->mode & PNG_HAVE_IEND) + png_error(png_ptr, "Out of place tEXt"); + + png_ptr->current_text = (png_charp)png_malloc(png_ptr, length + 1); png_ptr->current_text[(png_size_t)length] = '\0'; png_ptr->current_text_ptr = png_ptr->current_text; png_ptr->current_text_size = length; @@ -885,7 +790,7 @@ png_push_handle_tEXt(png_structp png_ptr, png_uint_32 length) } void -png_push_read_tEXt(png_structp png_ptr, png_infop info) +png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr) { if (png_ptr->buffer_size && png_ptr->current_text_left) { @@ -913,7 +818,7 @@ png_push_read_tEXt(png_structp png_ptr, png_infop info) return; } - png_push_check_crc(png_ptr); + png_crc_finish(png_ptr, 0); key = png_ptr->current_text; png_ptr->current_text = 0; @@ -924,11 +829,10 @@ png_push_read_tEXt(png_structp png_ptr, png_infop info) if (text != key + (png_size_t)png_ptr->current_text_size) text++; - png_read_tEXt(png_ptr, info, key, text, + png_read_tEXt(png_ptr, info_ptr, key, text, png_ptr->current_text_size - (text - key)); - if (png_ptr->mode == PNG_AT_LAST_IDAT || - png_ptr->mode == PNG_AFTER_IDAT) + if (png_ptr->mode == PNG_AFTER_IDAT) { png_ptr->process_mode = PNG_READ_END_MODE; } @@ -942,10 +846,12 @@ png_push_read_tEXt(png_structp png_ptr, png_infop info) #if defined(PNG_READ_zTXt_SUPPORTED) void -png_push_handle_zTXt(png_structp png_ptr, - png_uint_32 length) +png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { - png_ptr->current_text = (png_charp)png_large_malloc(png_ptr, length + 1); + if (png_ptr->mode == PNG_BEFORE_IHDR || png_ptr->mode & PNG_HAVE_IEND) + png_error(png_ptr, "Out of place zTXt"); + + png_ptr->current_text = (png_charp)png_malloc(png_ptr, length + 1); png_ptr->current_text[(png_size_t)length] = '\0'; png_ptr->current_text_ptr = png_ptr->current_text; png_ptr->current_text_size = length; @@ -954,7 +860,7 @@ png_push_handle_zTXt(png_structp png_ptr, } void -png_push_read_zTXt(png_structp png_ptr, png_infop info) +png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr) { if (png_ptr->buffer_size && png_ptr->current_text_left) { @@ -984,7 +890,7 @@ png_push_read_zTXt(png_structp png_ptr, png_infop info) return; } - png_push_check_crc(png_ptr); + png_crc_finish(png_ptr, 0); key = png_ptr->current_text; png_ptr->current_text = 0; @@ -995,7 +901,7 @@ png_push_read_zTXt(png_structp png_ptr, png_infop info) /* zTXt can't have zero text */ if (text == key + (png_size_t)png_ptr->current_text_size) { - png_large_free(png_ptr, key); + png_free(png_ptr, key); return; } @@ -1003,46 +909,46 @@ png_push_read_zTXt(png_structp png_ptr, png_infop info) if (*text) /* check compression byte */ { - png_large_free(png_ptr, key); + png_free(png_ptr, key); return; } text++; - png_ptr->zstream->next_in = (png_bytep )text; - png_ptr->zstream->avail_in = (uInt)(png_ptr->current_text_size - + png_ptr->zstream.next_in = (png_bytep )text; + png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size - (text - key)); - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (png_size_t)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (png_size_t)png_ptr->zbuf_size; key_size = text - key; text_size = 0; text = NULL; ret = Z_STREAM_END; - while (png_ptr->zstream->avail_in) + while (png_ptr->zstream.avail_in) { - ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH); + ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); if (ret != Z_OK && ret != Z_STREAM_END) { - inflateReset(png_ptr->zstream); - png_ptr->zstream->avail_in = 0; - png_large_free(png_ptr, key); - png_large_free(png_ptr, text); + inflateReset(&png_ptr->zstream); + png_ptr->zstream.avail_in = 0; + png_free(png_ptr, key); + png_free(png_ptr, text); return; } - if (!png_ptr->zstream->avail_out || ret == Z_STREAM_END) + if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END) { if (!text) { - text = (png_charp)png_large_malloc(png_ptr, - png_ptr->zbuf_size - png_ptr->zstream->avail_out + + text = (png_charp)png_malloc(png_ptr, + png_ptr->zbuf_size - png_ptr->zstream.avail_out + key_size + 1); png_memcpy(text + (png_size_t)key_size, png_ptr->zbuf, - (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream->avail_out)); + (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out)); png_memcpy(text, key, (png_size_t)key_size); text_size = key_size + (png_size_t)png_ptr->zbuf_size - - png_ptr->zstream->avail_out; + png_ptr->zstream.avail_out; *(text + (png_size_t)text_size) = '\0'; } else @@ -1050,19 +956,19 @@ png_push_read_zTXt(png_structp png_ptr, png_infop info) png_charp tmp; tmp = text; - text = png_large_malloc(png_ptr, text_size + - png_ptr->zbuf_size - png_ptr->zstream->avail_out + 1); + text = png_malloc(png_ptr, text_size + + png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1); png_memcpy(text, tmp, (png_size_t)text_size); - png_large_free(png_ptr, tmp); + png_free(png_ptr, tmp); png_memcpy(text + (png_size_t)text_size, png_ptr->zbuf, - (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream->avail_out)); - text_size += png_ptr->zbuf_size - png_ptr->zstream->avail_out; + (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out)); + text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; *(text + (png_size_t)text_size) = '\0'; } if (ret != Z_STREAM_END) { - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; } } else @@ -1074,24 +980,23 @@ png_push_read_zTXt(png_structp png_ptr, png_infop info) break; } - inflateReset(png_ptr->zstream); - png_ptr->zstream->avail_in = 0; + inflateReset(&png_ptr->zstream); + png_ptr->zstream.avail_in = 0; if (ret != Z_STREAM_END) { - png_large_free(png_ptr, key); - png_large_free(png_ptr, text); + png_free(png_ptr, key); + png_free(png_ptr, text); return; } - png_large_free(png_ptr, key); + png_free(png_ptr, key); key = text; text += (png_size_t)key_size; text_size -= key_size; - png_read_zTXt(png_ptr, info, key, text, text_size, 0); - if (png_ptr->mode == PNG_AT_LAST_IDAT || - png_ptr->mode == PNG_AFTER_IDAT) + png_read_zTXt(png_ptr, info_ptr, key, text, text_size, 0); + if (png_ptr->mode == PNG_AFTER_IDAT) { png_ptr->process_mode = PNG_READ_END_MODE; } @@ -1103,18 +1008,40 @@ png_push_read_zTXt(png_structp png_ptr, png_infop info) } #endif +/* This function is called when we haven't found a handler for this + chunk. In the future we will have code here which can handle + user-defined callback functions for unknown chunks before they are + ignored or cause an error. If there isn't a problem with the + chunk itself (ie a bad chunk name or a critical chunk), the chunk + is (currently) silently ignored. */ void -png_push_have_info(png_structp png_ptr, png_infop info) +png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { - if (png_ptr->info_fn) - (*(png_ptr->info_fn))(png_ptr, info); + png_check_chunk_name(png_ptr, png_ptr->chunk_name); + + if (!(png_ptr->chunk_name[0] & 0x20)) + { + char msg[40]; + + sprintf(msg, "Unknown critical chunk %s", png_ptr->chunk_name); + png_error(png_ptr, msg); + } + + png_push_crc_skip(png_ptr, length); } void -png_push_have_end(png_structp png_ptr, png_infop info) +png_push_have_info(png_structp png_ptr, png_infop info_ptr) +{ + if (png_ptr->info_fn) + (*(png_ptr->info_fn))(png_ptr, info_ptr); +} + +void +png_push_have_end(png_structp png_ptr, png_infop info_ptr) { if (png_ptr->end_fn) - (*(png_ptr->end_fn))(png_ptr, info); + (*(png_ptr->end_fn))(png_ptr, info_ptr); } void @@ -1125,179 +1052,12 @@ png_push_have_row(png_structp png_ptr, png_bytep row) (int)png_ptr->pass); } -png_voidp -png_get_progressive_ptr(png_structp png_ptr) -{ - return png_ptr->io_ptr; -} - void -png_push_read_end(png_structp png_ptr, png_infop info) +png_progressive_combine_row (png_structp png_ptr, + png_bytep old_row, png_bytep new_row) { - if (!(png_ptr->flags & PNG_FLAG_HAVE_CHUNK_HEADER)) - { - png_byte chunk_start[8]; - - if (png_ptr->buffer_size < 8) - { - png_push_save_buffer(png_ptr); - return; - } - - png_push_fill_buffer(png_ptr, chunk_start, 8); - png_ptr->push_length = png_get_uint_32(chunk_start); - png_memcpy(png_ptr->push_chunk_name, (png_voidp)(chunk_start + 4), 4); - png_ptr->flags |= PNG_FLAG_HAVE_CHUNK_HEADER; - png_reset_crc(png_ptr); - png_calculate_crc(png_ptr, chunk_start + 4, 4); - } - - if (!png_memcmp(png_ptr->push_chunk_name, png_IHDR, 4)) - { - png_error(png_ptr, "Invalid IHDR after IDAT"); - } - else if (!png_memcmp(png_ptr->push_chunk_name, png_PLTE, 4)) - { - png_error(png_ptr, "Invalid PLTE after IDAT"); - } - else if (!png_memcmp(png_ptr->push_chunk_name, png_IDAT, 4)) - { - if (png_ptr->push_length > 0 || png_ptr->mode != PNG_AT_LAST_IDAT) - png_error(png_ptr, "Too many IDAT's found"); - } - else if (!png_memcmp(png_ptr->push_chunk_name, png_IEND, 4)) - { - if (png_ptr->push_length) - png_error(png_ptr, "Invalid IEND chunk"); - if (png_ptr->buffer_size < 4) - { - png_push_save_buffer(png_ptr); - return; - } - png_push_check_crc(png_ptr); - png_ptr->mode = PNG_AFTER_IEND; - png_ptr->process_mode = PNG_READ_DONE_MODE; - png_push_have_end(png_ptr, info); - } -#if defined(PNG_READ_gAMA_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_gAMA, 4)) - { - png_error(png_ptr, "Invalid gAMA after IDAT"); - } -#endif -#if defined(PNG_READ_sBIT_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_sBIT, 4)) - { - png_error(png_ptr, "Invalid sBIT after IDAT"); - } -#endif -#if defined(PNG_READ_cHRM_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_cHRM, 4)) - { - png_error(png_ptr, "Invalid cHRM after IDAT"); - } -#endif -#if defined(PNG_READ_tRNS_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_tRNS, 4)) - { - png_error(png_ptr, "Invalid tRNS after IDAT"); - } -#endif -#if defined(PNG_READ_bKGD_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_bKGD, 4)) - { - png_error(png_ptr, "Invalid bKGD after IDAT"); - } -#endif -#if defined(PNG_READ_hIST_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_hIST, 4)) - { - png_error(png_ptr, "Invalid hIST after IDAT"); - } -#endif -#if defined(PNG_READ_pHYs_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_pHYs, 4)) - { - png_error(png_ptr, "Invalid pHYs after IDAT"); - } -#endif -#if defined(PNG_READ_oFFs_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_oFFs, 4)) - { - png_error(png_ptr, "Invalid oFFs after IDAT"); - } -#endif -#if defined(PNG_READ_tIME_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_tIME, 4)) - { - if (png_ptr->push_length + 4 > png_ptr->buffer_size) - { - png_push_save_buffer(png_ptr); - return; - } - - if (png_ptr->mode == PNG_BEFORE_IHDR || - png_ptr->mode == PNG_AFTER_IEND) - png_error(png_ptr, "Out of place tIME"); - - png_handle_tIME(png_ptr, info, png_ptr->push_length); - png_push_check_crc(png_ptr); - } -#endif -#if defined(PNG_READ_tEXt_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_tEXt, 4)) - { - if (png_ptr->mode == PNG_BEFORE_IHDR || - png_ptr->mode == PNG_AFTER_IEND) - png_error(png_ptr, "Out of place tEXt"); - - png_push_handle_tEXt(png_ptr, png_ptr->push_length); - } -#endif -#if defined(PNG_READ_zTXt_SUPPORTED) - else if (!png_memcmp(png_ptr->push_chunk_name, png_zTXt, 4)) - { - if (png_ptr->mode == PNG_BEFORE_IHDR || - png_ptr->mode == PNG_AFTER_IEND) - png_error(png_ptr, "Out of place zTXt"); - - png_push_handle_zTXt(png_ptr, png_ptr->push_length); - } -#endif - else - { - if (png_ptr->push_chunk_name[0] <41 || png_ptr->push_chunk_name[0]> 122 || - (png_ptr->push_chunk_name[0]>90 && png_ptr->push_chunk_name[0]< 97) || - png_ptr->push_chunk_name[1] <41 || png_ptr->push_chunk_name[1]> 122 || - (png_ptr->push_chunk_name[1]>90 && png_ptr->push_chunk_name[1]< 97) || - png_ptr->push_chunk_name[2] <41 || png_ptr->push_chunk_name[2]> 122 || - (png_ptr->push_chunk_name[2]>90 && png_ptr->push_chunk_name[2]< 97) || - png_ptr->push_chunk_name[3] <41 || png_ptr->push_chunk_name[3]> 122 || - (png_ptr->push_chunk_name[3]>90 && png_ptr->push_chunk_name[3]< 97)) - { - char msg[45]; - - sprintf(msg, "Invalid chunk type 0x%02X 0x%02X 0x%02X 0x%02X", - png_ptr->push_chunk_name[0], png_ptr->push_chunk_name[1], - png_ptr->push_chunk_name[2], png_ptr->push_chunk_name[3]); - png_error(png_ptr, msg); - } - - if ((png_ptr->push_chunk_name[0] & 0x20) == 0) - { - char msg[40]; - - sprintf(msg, "Unknown critical chunk %c%c%c%c", - png_ptr->push_chunk_name[0], png_ptr->push_chunk_name[1], - png_ptr->push_chunk_name[2], png_ptr->push_chunk_name[3]); - png_error(png_ptr, msg); - } - - png_push_crc_skip(png_ptr, png_ptr->push_length); - } - if (png_ptr->mode == PNG_AT_LAST_IDAT) - png_ptr->mode = PNG_AFTER_IDAT; - png_ptr->flags &= ~PNG_FLAG_HAVE_CHUNK_HEADER; + if (new_row) + png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]); } void @@ -1312,12 +1072,10 @@ png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr, png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer); } -void -png_progressive_combine_row (png_structp png_ptr, - png_bytep old_row, png_bytep new_row) +png_voidp +png_get_progressive_ptr(png_structp png_ptr) { - if (new_row) - png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]); + return png_ptr->io_ptr; } #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ diff --git a/pngrcb.c b/pngrcb.c index 914a6a1a..1df35503 100644 --- a/pngrcb.c +++ b/pngrcb.c @@ -1,9 +1,9 @@ /* pngrcb.c - callbacks while reading a png file - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 */ #define PNG_INTERNAL @@ -15,7 +15,7 @@ png_read_IHDR(png_structp png_ptr, png_infop info, int color_type, int compression_type, int filter_type, int interlace_type) { - if (!png_ptr || !info) + if (!info) return; info->width = width; @@ -41,7 +41,7 @@ void png_read_PLTE(png_structp png_ptr, png_infop info, png_colorp palette, int num) { - if (!png_ptr || !info) + if (!info) return; info->palette = palette; @@ -53,10 +53,10 @@ png_read_PLTE(png_structp png_ptr, png_infop info, void png_read_gAMA(png_structp png_ptr, png_infop info, double gamma) { - if (!png_ptr || !info) + if (!info) return; - info->gamma = gamma; + info->gamma = (float)gamma; info->valid |= PNG_INFO_gAMA; } #endif @@ -66,7 +66,7 @@ void png_read_sBIT(png_structp png_ptr, png_infop info, png_color_8p sig_bit) { - if (!png_ptr || !info) + if (!info) return; png_memcpy(&(info->sig_bit), sig_bit, sizeof (png_color_8)); @@ -80,17 +80,17 @@ png_read_cHRM(png_structp png_ptr, png_infop info, double white_x, double white_y, double red_x, double red_y, double green_x, double green_y, double blue_x, double blue_y) { - if (!png_ptr || !info) + if (!info) return; - info->x_white = white_x; - info->y_white = white_y; - info->x_red = red_x; - info->y_red = red_y; - info->x_green = green_x; - info->y_green = green_y; - info->x_blue = blue_x; - info->y_blue = blue_y; + info->x_white = (float)white_x; + info->y_white = (float)white_y; + info->x_red = (float)red_x; + info->y_red = (float)red_y; + info->x_green = (float)green_x; + info->y_green = (float)green_y; + info->x_blue = (float)blue_x; + info->y_blue = (float)blue_y; info->valid |= PNG_INFO_cHRM; } #endif @@ -100,7 +100,7 @@ void png_read_tRNS(png_structp png_ptr, png_infop info, png_bytep trans, int num_trans, png_color_16p trans_values) { - if (!png_ptr || !info) + if (!info) return; if (trans) @@ -122,7 +122,7 @@ void png_read_bKGD(png_structp png_ptr, png_infop info, png_color_16p background) { - if (!png_ptr || !info) + if (!info) return; png_memcpy(&(info->background), background, sizeof(png_color_16)); @@ -134,7 +134,7 @@ png_read_bKGD(png_structp png_ptr, png_infop info, void png_read_hIST(png_structp png_ptr, png_infop info, png_uint_16p hist) { - if (!png_ptr || !info) + if (!info) return; info->hist = hist; @@ -147,7 +147,7 @@ void png_read_pHYs(png_structp png_ptr, png_infop info, png_uint_32 res_x, png_uint_32 res_y, int unit_type) { - if (!png_ptr || !info) + if (!info) return; info->x_pixels_per_unit = res_x; @@ -162,7 +162,7 @@ void png_read_oFFs(png_structp png_ptr, png_infop info, png_uint_32 offset_x, png_uint_32 offset_y, int unit_type) { - if (!png_ptr || !info) + if (!info) return; info->x_offset = offset_x; @@ -177,7 +177,7 @@ void png_read_tIME(png_structp png_ptr, png_infop info, png_timep mod_time) { - if (!png_ptr || !info) + if (!info) return; png_memcpy(&(info->mod_time), mod_time, sizeof (png_time)); @@ -190,7 +190,7 @@ void png_read_zTXt(png_structp png_ptr, png_infop info, png_charp key, png_charp text, png_uint_32 text_len, int compression) { - if (!png_ptr || !info) + if (!info) return; if (info->max_text <= info->num_text) @@ -205,18 +205,18 @@ png_read_zTXt(png_structp png_ptr, png_infop info, png_textp old_text; old_text = info->text; - info->text = (png_textp)png_large_malloc(png_ptr, + info->text = (png_textp)png_malloc(png_ptr, info->max_text * sizeof (png_text)); png_memcpy(info->text, old_text, (png_size_t)(old_max * sizeof (png_text))); - png_large_free(png_ptr, old_text); + png_free(png_ptr, old_text); } } else { info->max_text = 16; info->num_text = 0; - info->text = (png_textp)png_large_malloc(png_ptr, + info->text = (png_textp)png_malloc(png_ptr, info->max_text * sizeof (png_text)); } } @@ -234,7 +234,7 @@ void png_read_tEXt(png_structp png_ptr, png_infop info, png_charp key, png_charp text, png_uint_32 text_len) { - if (!png_ptr || !info) + if (!info) return; png_read_zTXt(png_ptr, info, key, text, text_len, -1); diff --git a/pngread.c b/pngread.c index 29afd6e2..4ebf805b 100644 --- a/pngread.c +++ b/pngread.c @@ -1,10 +1,10 @@ /* pngread.c - read a png file - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 */ #define PNG_INTERNAL @@ -12,48 +12,53 @@ /* Create a png structure for reading, and allocate any memory needed. */ png_structp -png_create_read_struct(png_const_charp user_png_ver, voidp error_ptr, - png_error_ptr warn_fn, png_error_ptr error_fn) +png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, + png_error_ptr error_fn, png_error_ptr warn_fn) { png_structp png_ptr; - +#ifdef USE_FAR_KEYWORD + jmp_buf jmpbuf; +#endif if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL) { return (png_structp)NULL; } - +#ifdef USE_FAR_KEYWORD + if (setjmp(jmpbuf)) +#else if (setjmp(png_ptr->jmpbuf)) +#endif { - png_large_free(png_ptr, png_ptr->zbuf); - png_free(png_ptr, png_ptr->zstream); + png_free(png_ptr, png_ptr->zbuf); png_destroy_struct(png_ptr); return (png_structp)NULL; } +#ifdef USE_FAR_KEYWORD + png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); +#endif + png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); - png_set_error_fn(png_ptr, error_ptr, warn_fn, error_fn); - - if (user_png_ver == NULL || strcmp(user_png_ver, png_libpng_ver)) + /* This is what will be used for the final version. For the + (ongoing) development library, we know that ALL of the + older library versions are out of date because of the + change in the info_struct size. */ + /* + if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0]) + */ + if (user_png_ver == NULL || atof(user_png_ver) < atof(png_libpng_ver)) { - if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0]) - { - png_error(png_ptr, "Incompatible libpng versions"); - } - else - { - png_warning(png_ptr, "Different libpng versions"); - } + png_error(png_ptr, + "Incompatible libpng version in application and library"); } /* initialize zbuf - compression buffer */ png_ptr->zbuf_size = PNG_ZBUF_SIZE; - png_ptr->zbuf = png_large_malloc(png_ptr, png_ptr->zbuf_size); + png_ptr->zbuf = png_malloc(png_ptr, png_ptr->zbuf_size); + png_ptr->zstream.zalloc = png_zalloc; + png_ptr->zstream.zfree = png_zfree; + png_ptr->zstream.opaque = (voidpf)png_ptr; - png_ptr->zstream = (z_stream *)png_malloc(png_ptr, sizeof (z_stream)); - png_ptr->zstream->zalloc = png_zalloc; - png_ptr->zstream->zfree = png_zfree; - png_ptr->zstream->opaque = (voidpf)png_ptr; - - switch (inflateInit(png_ptr->zstream)) + switch (inflateInit(&png_ptr->zstream)) { case Z_OK: /* Do nothing */ break; case Z_MEM_ERROR: @@ -62,19 +67,18 @@ png_create_read_struct(png_const_charp user_png_ver, voidp error_ptr, default: png_error(png_ptr, "Unknown zlib error"); } - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_set_read_fn(png_ptr, NULL, NULL); - png_ptr->do_free |= PNG_FREE_STRUCT; - return (png_ptr); } -/* initialize png structure for reading, and allocate any memory needed */ -/* This interface is depreciated in favour of the png_create_read_struct() */ +/* Initialize png structure for reading, and allocate any memory needed. + This interface is depreciated in favour of the png_create_read_struct(), + and it will eventually disappear. */ void png_read_init(png_structp png_ptr) { @@ -91,14 +95,12 @@ png_read_init(png_structp png_ptr) /* initialize zbuf - compression buffer */ png_ptr->zbuf_size = PNG_ZBUF_SIZE; - png_ptr->zbuf = png_large_malloc(png_ptr, png_ptr->zbuf_size); + png_ptr->zbuf = png_malloc(png_ptr, png_ptr->zbuf_size); + png_ptr->zstream.zalloc = png_zalloc; + png_ptr->zstream.zfree = png_zfree; + png_ptr->zstream.opaque = (voidpf)png_ptr; - png_ptr->zstream = (z_stream *)png_malloc(png_ptr, sizeof (z_stream)); - png_ptr->zstream->zalloc = png_zalloc; - png_ptr->zstream->zfree = png_zfree; - png_ptr->zstream->opaque = (voidpf)png_ptr; - - switch (inflateInit(png_ptr->zstream)) + switch (inflateInit(&png_ptr->zstream)) { case Z_OK: /* Do nothing */ break; case Z_MEM_ERROR: @@ -107,41 +109,105 @@ png_read_init(png_structp png_ptr) default: png_error(png_ptr, "Unknown zlib error"); } - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_set_read_fn(png_ptr, NULL, NULL); } -/* read the information before the actual image data. */ +/* Read the information before the actual image data. This has been + * changed in v0.90 to allow reading a file which already has the magic + * bytes read from the stream. You can tell libpng how many bytes have + * been read from the beginning of the stream (up to the maxumum of 8) + * via png_set_sig_bytes(), and we will only check the remaining bytes + * here. The application can then have access to the signature bytes we + * read if it is determined that this isn't a valid PNG file. + */ void -png_read_info(png_structp png_ptr, png_infop info) +png_read_info(png_structp png_ptr, png_infop info_ptr) { - png_byte chunk_start[8]; - png_uint_32 length; - - png_read_data(png_ptr, chunk_start, 8); - if (!png_check_sig(chunk_start, 8)) + /* If we haven't checked all of the PNG signature bytes, do so now. */ + if (png_ptr->sig_bytes < 8) { - if (!png_check_sig(chunk_start, 4)) - png_error(png_ptr, "Not a PNG file"); - else - png_error(png_ptr, "PNG file corrupted by ASCII conversion"); + int num_checked = png_ptr->sig_bytes, + num_to_check = 8 - num_checked; + + png_read_data(png_ptr, &(info_ptr->signature[num_checked]), + (png_uint_32)num_to_check); + png_ptr->sig_bytes = 8; + + if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) + { + if (num_checked < 4 && + png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) + png_error(png_ptr, "Not a PNG file"); + else + png_error(png_ptr, "PNG file corrupted by ASCII conversion"); + } } while (1) { - png_uint_32 crc; + png_byte chunk_length[4]; + png_uint_32 length; + + png_read_data(png_ptr, chunk_length, 4); + length = png_get_uint_32(chunk_length); - png_read_data(png_ptr, chunk_start, 8); - length = png_get_uint_32(chunk_start); png_reset_crc(png_ptr); - png_calculate_crc(png_ptr, chunk_start + 4, 4); - if (!png_memcmp(chunk_start + 4, png_IHDR, 4)) - png_handle_IHDR(png_ptr, info, length); - else if (!png_memcmp(chunk_start + 4, png_PLTE, 4)) - png_handle_PLTE(png_ptr, info, length); - else if (!png_memcmp(chunk_start + 4, png_IDAT, 4)) + png_crc_read(png_ptr, png_ptr->chunk_name, 4); + + if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) + png_handle_IHDR(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) + png_handle_PLTE(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) + png_handle_IEND(png_ptr, info_ptr, length); +#if defined(PNG_READ_gAMA_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) + png_handle_gAMA(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_sBIT_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) + png_handle_sBIT(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_cHRM_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) + png_handle_cHRM(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_tRNS_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) + png_handle_tRNS(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_bKGD_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) + png_handle_bKGD(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_hIST_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) + png_handle_hIST(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_pHYs_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) + png_handle_pHYs(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_oFFs_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) + png_handle_oFFs(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_tIME_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) + png_handle_tIME(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_tEXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) + png_handle_tEXt(png_ptr, info_ptr, length); +#endif +#if defined(PNG_READ_zTXt_SUPPORTED) + else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) + png_handle_zTXt(png_ptr, info_ptr, length); +#endif + else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) { if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before IDAT"); @@ -153,95 +219,18 @@ png_read_info(png_structp png_ptr, png_infop info) png_ptr->mode |= PNG_HAVE_IDAT; break; } - else if (!png_memcmp(chunk_start + 4, png_IEND, 4)) - png_error(png_ptr, "No image in file"); -#if defined(PNG_READ_gAMA_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_gAMA, 4)) - png_handle_gAMA(png_ptr, info, length); -#endif -#if defined(PNG_READ_sBIT_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_sBIT, 4)) - png_handle_sBIT(png_ptr, info, length); -#endif -#if defined(PNG_READ_cHRM_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_cHRM, 4)) - png_handle_cHRM(png_ptr, info, length); -#endif -#if defined(PNG_READ_tRNS_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_tRNS, 4)) - png_handle_tRNS(png_ptr, info, length); -#endif -#if defined(PNG_READ_bKGD_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_bKGD, 4)) - png_handle_bKGD(png_ptr, info, length); -#endif -#if defined(PNG_READ_hIST_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_hIST, 4)) - png_handle_hIST(png_ptr, info, length); -#endif -#if defined(PNG_READ_pHYs_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_pHYs, 4)) - png_handle_pHYs(png_ptr, info, length); -#endif -#if defined(PNG_READ_oFFs_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_oFFs, 4)) - png_handle_oFFs(png_ptr, info, length); -#endif -#if defined(PNG_READ_tIME_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_tIME, 4)) - png_handle_tIME(png_ptr, info, length); -#endif -#if defined(PNG_READ_tEXt_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_tEXt, 4)) - png_handle_tEXt(png_ptr, info, length); -#endif -#if defined(PNG_READ_zTXt_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_zTXt, 4)) - png_handle_zTXt(png_ptr, info, length); -#endif else - { - if (chunk_start[4] < 41 || chunk_start[4] > 122 || - (chunk_start[4] > 90 && chunk_start[4] < 97) || - chunk_start[5] < 41 || chunk_start[5] > 122 || - (chunk_start[5] > 90 && chunk_start[5] < 97) || - chunk_start[6] < 41 || chunk_start[6] > 122 || - (chunk_start[6] > 90 && chunk_start[6] < 97) || - chunk_start[7] < 41 || chunk_start[7] > 122 || - (chunk_start[7] > 90 && chunk_start[7] < 97)) - { - char msg[45]; - - sprintf(msg, "Invalid chunk type 0x%02X 0x%02X 0x%02X 0x%02X", - chunk_start[4], chunk_start[5], chunk_start[6], chunk_start[7]); - png_error(png_ptr, msg); - } - - if ((chunk_start[4] & 0x20) == 0) - { - char msg[40]; - - sprintf(msg, "Unknown critical chunk %c%c%c%c", - chunk_start[4], chunk_start[5], chunk_start[6], chunk_start[7]); - png_error(png_ptr, msg); - } - - png_crc_skip(png_ptr, length); - } - png_read_data(png_ptr, chunk_start, 4); - crc = png_get_uint_32(chunk_start); - if (((crc ^ 0xffffffffL) & 0xffffffffL) != (png_ptr->crc & 0xffffffffL)) - png_error(png_ptr, "Bad CRC value"); + png_handle_unknown(png_ptr, info_ptr, length); } } -/* optional call to update the users info structure */ +/* optional call to update the users info_ptr structure */ void -png_read_update_info(png_structp png_ptr, png_infop info) +png_read_update_info(png_structp png_ptr, png_infop info_ptr) { if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) png_read_start_row(png_ptr); - png_read_transform_info(png_ptr, info); + png_read_transform_info(png_ptr, info_ptr); } /* initialize palette, background, etc, after transformations @@ -342,52 +331,48 @@ png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row) if (!(png_ptr->mode & PNG_HAVE_IDAT)) png_error(png_ptr, "Invalid attempt to read row data"); - png_ptr->zstream->next_out = png_ptr->row_buf; - png_ptr->zstream->avail_out = (uInt)png_ptr->irowbytes; + png_ptr->zstream.next_out = png_ptr->row_buf; + png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; do { - if (!(png_ptr->zstream->avail_in)) + if (!(png_ptr->zstream.avail_in)) { while (!png_ptr->idat_size) { - png_byte buf[4]; - png_uint_32 crc; + png_byte chunk_length[4]; - png_read_data(png_ptr, buf, 4); - crc = png_get_uint_32(buf); - if (((crc ^ 0xffffffffL) & 0xffffffffL) != - (png_ptr->crc & 0xffffffffL)) - png_error(png_ptr, "Bad CRC value"); + png_crc_finish(png_ptr, 0); + + png_read_data(png_ptr, chunk_length, 4); + png_ptr->idat_size = png_get_uint_32(chunk_length); - png_read_data(png_ptr, buf, 4); - png_ptr->idat_size = png_get_uint_32(buf); png_reset_crc(png_ptr); - - png_crc_read(png_ptr, buf, 4); - if (png_memcmp(buf, png_IDAT, 4)) - png_error(png_ptr, "Not enough IDATs for image"); + png_crc_read(png_ptr, png_ptr->chunk_name, 4); + if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) + png_error(png_ptr, "Not enough image data"); } - png_ptr->zstream->avail_in = (uInt)png_ptr->zbuf_size; - png_ptr->zstream->next_in = png_ptr->zbuf; + png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_in = png_ptr->zbuf; if (png_ptr->zbuf_size > png_ptr->idat_size) - png_ptr->zstream->avail_in = (uInt)png_ptr->idat_size; - png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream->avail_in); - png_ptr->idat_size -= png_ptr->zstream->avail_in; + png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; + png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in); + png_ptr->idat_size -= png_ptr->zstream.avail_in; } - ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH); + ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); if (ret == Z_STREAM_END) { - if (png_ptr->zstream->avail_out || png_ptr->zstream->avail_in || + if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in || png_ptr->idat_size) png_error(png_ptr, "Extra compressed data"); - png_ptr->mode |= PNG_AT_LAST_IDAT; + png_ptr->mode |= PNG_AFTER_IDAT; png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; break; } if (ret != Z_OK) - png_error(png_ptr, png_ptr->zstream->msg ? png_ptr->zstream->msg : + png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : "Decompression error"); - } while (png_ptr->zstream->avail_out); + + } while (png_ptr->zstream.avail_out); png_ptr->row_info.color_type = png_ptr->color_type; png_ptr->row_info.width = png_ptr->iwidth; @@ -518,142 +503,72 @@ png_read_image(png_structp png_ptr, png_bytepp image) file, will verify the end is accurate, and will read any comments or time information at the end of the file, if info is not NULL. */ void -png_read_end(png_structp png_ptr, png_infop info) +png_read_end(png_structp png_ptr, png_infop info_ptr) { - png_byte chunk_start[8]; + png_byte chunk_length[4]; png_uint_32 length; - png_uint_32 crc; - png_read_data(png_ptr, chunk_start, 4); - crc = png_get_uint_32(chunk_start); - if (((crc ^ 0xffffffffL) & 0xffffffffL) != - (png_ptr->crc & 0xffffffffL)) - png_error(png_ptr, "Bad CRC value"); + png_crc_finish(png_ptr, 0); do { - png_read_data(png_ptr, chunk_start, 8); - length = png_get_uint_32(chunk_start); - png_reset_crc(png_ptr); - png_calculate_crc(png_ptr, chunk_start + 4, 4); + png_read_data(png_ptr, chunk_length, 4); + length = png_get_uint_32(chunk_length); - if (!png_memcmp(chunk_start + 4, png_IHDR, 4)) - { - png_error(png_ptr, "Invalid IHDR after IDAT"); - } - else if (!png_memcmp(chunk_start + 4, png_PLTE, 4)) - { - png_error(png_ptr, "Invalid PLTE after IDAT"); - } - else if (!png_memcmp(chunk_start + 4, png_gAMA, 4)) - { - png_error(png_ptr, "Invalid gAMA after IDAT"); - } - else if (!png_memcmp(chunk_start + 4, png_sBIT, 4)) - { - png_error(png_ptr, "Invalid sBIT after IDAT"); - } - else if (!png_memcmp(chunk_start + 4, png_cHRM, 4)) - { - png_error(png_ptr, "Invalid cHRM after IDAT"); - } - else if (!png_memcmp(chunk_start + 4, png_tRNS, 4)) - { - png_error(png_ptr, "Invalid tRNS after IDAT"); - } - else if (!png_memcmp(chunk_start + 4, png_bKGD, 4)) - { - png_error(png_ptr, "Invalid bKGD after IDAT"); - } - else if (!png_memcmp(chunk_start + 4, png_hIST, 4)) - { - png_error(png_ptr, "Invalid hIST after IDAT"); - } - else if (!png_memcmp(chunk_start + 4, png_IDAT, 4)) + png_reset_crc(png_ptr); + png_crc_read(png_ptr, png_ptr->chunk_name, 4); + + if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) + png_handle_IHDR(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) + png_handle_PLTE(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) + png_handle_gAMA(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) + png_handle_sBIT(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) + png_handle_cHRM(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) + png_handle_tRNS(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) + png_handle_bKGD(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) + png_handle_hIST(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) + png_handle_pHYs(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) + png_handle_oFFs(png_ptr, info_ptr, length); + else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) { if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT) png_error(png_ptr, "Too many IDAT's found"); } - else if (!png_memcmp(chunk_start + 4, png_pHYs, 4)) - { - png_error(png_ptr, "Invalid pHYs after IDAT"); - } - else if (!png_memcmp(chunk_start + 4, png_oFFs, 4)) - { - png_error(png_ptr, "Invalid oFFs after IDAT"); - } #if defined(PNG_READ_tIME_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_tIME, 4)) - { - png_ptr->mode |= PNG_AFTER_IDAT; - - if (info) - png_handle_tIME(png_ptr, info, length); - else - png_crc_skip(png_ptr, length); - } + else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) + png_handle_tIME(png_ptr, info_ptr, length); #endif #if defined(PNG_READ_tEXt_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_tEXt, 4)) - { - png_ptr->mode |= PNG_AFTER_IDAT; - - if (info) - png_handle_tEXt(png_ptr, info, length); - else - png_crc_skip(png_ptr, length); - } + else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) + png_handle_tEXt(png_ptr, info_ptr, length); #endif #if defined(PNG_READ_zTXt_SUPPORTED) - else if (!png_memcmp(chunk_start + 4, png_zTXt, 4)) - { - png_ptr->mode |= PNG_AFTER_IDAT; - - if (info) - png_handle_zTXt(png_ptr, info, length); - else - png_crc_skip(png_ptr, length); - } + else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) + png_handle_zTXt(png_ptr, info_ptr, length); #endif - else if (!png_memcmp(chunk_start + 4, png_IEND, 4)) - { - png_ptr->mode |= PNG_AFTER_IDAT; - png_ptr->mode |= PNG_AFTER_IEND; - } + else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) + png_handle_IEND(png_ptr, info_ptr, length); else - { - if (chunk_start[4] < 41 || chunk_start[4] > 122 || - (chunk_start[4] > 90 && chunk_start[4] < 97) || - chunk_start[5] < 41 || chunk_start[5] > 122 || - (chunk_start[5] > 90 && chunk_start[5] < 97) || - chunk_start[6] < 41 || chunk_start[6] > 122 || - (chunk_start[6] > 90 && chunk_start[6] < 97) || - chunk_start[7] < 41 || chunk_start[7] > 122 || - (chunk_start[7] > 90 && chunk_start[7] < 97)) - { - png_error(png_ptr, "Invalid chunk type"); - } - - if ((chunk_start[4] & 0x20) == 0) - png_error(png_ptr, "Unknown critical chunk"); - - png_ptr->mode |= PNG_AFTER_IDAT; - png_crc_skip(png_ptr, length); - } - png_read_data(png_ptr, chunk_start, 4); - crc = png_get_uint_32(chunk_start); - if (((crc ^ 0xffffffffL) & 0xffffffffL) != (png_ptr->crc & 0xffffffffL)) - png_error(png_ptr, "Bad CRC value"); - } while (!(png_ptr->mode & PNG_AFTER_IEND)); + png_handle_unknown(png_ptr, info_ptr, length); + } while (!(png_ptr->mode & PNG_HAVE_IEND)); } /* free all memory used by the read */ void png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, - png_infopp end_info_ptr) + png_infopp end_info_ptr_ptr) { png_structp png_ptr = NULL; - png_infop info_ptr = NULL, end_info = NULL; + png_infop info_ptr = NULL, end_info_ptr = NULL; if (png_ptr_ptr) png_ptr = *png_ptr_ptr; @@ -661,33 +576,33 @@ png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, if (info_ptr_ptr) info_ptr = *info_ptr_ptr; - if (end_info_ptr) - end_info = *end_info_ptr; + if (end_info_ptr_ptr) + end_info_ptr = *end_info_ptr_ptr; - png_read_destroy(png_ptr, info_ptr, end_info); + png_read_destroy(png_ptr, info_ptr, end_info_ptr); if (info_ptr) { - png_destroy_struct((voidp)info_ptr); + png_destroy_struct((png_voidp)info_ptr); *info_ptr_ptr = (png_infop)NULL; } - if (end_info) + if (end_info_ptr) { - png_destroy_struct((voidp)end_info); - *end_info_ptr = (png_infop)NULL; + png_destroy_struct((png_voidp)end_info_ptr); + *end_info_ptr_ptr = (png_infop)NULL; } if (png_ptr) { - png_destroy_struct((voidp)png_ptr); + png_destroy_struct((png_voidp)png_ptr); *png_ptr_ptr = (png_structp)NULL; } } -/* free all memory used by the read (old) */ +/* free all memory used by the read (old method) */ void -png_read_destroy(png_structp png_ptr, png_infop info, png_infop end_info) +png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr) { int i; jmp_buf tmp_jmp; @@ -695,89 +610,68 @@ png_read_destroy(png_structp png_ptr, png_infop info, png_infop end_info) png_error_ptr warning_fn; png_voidp error_ptr; - if (info) - { -#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) - for (i = 0; i < info->num_text; i++) - { - png_large_free(png_ptr, info->text[i].key); - } + if (info_ptr) + png_info_destroy(png_ptr, info_ptr); - png_large_free(png_ptr, info->text); -#endif - png_memset(info, 0, sizeof(png_info)); - } + if (end_info_ptr) + png_info_destroy(png_ptr, end_info_ptr); - if (end_info) - { -#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) - for (i = 0; i < end_info->num_text; i++) - { - png_large_free(png_ptr, end_info->text[i].key); - } - - png_large_free(png_ptr, end_info->text); -#endif - png_memset(end_info, 0, sizeof(png_info)); - } - - png_large_free(png_ptr, png_ptr->zbuf); - png_large_free(png_ptr, png_ptr->row_buf); - png_large_free(png_ptr, png_ptr->prev_row); + png_free(png_ptr, png_ptr->zbuf); + png_free(png_ptr, png_ptr->row_buf); + png_free(png_ptr, png_ptr->prev_row); #if defined(PNG_READ_DITHER_SUPPORTED) - png_large_free(png_ptr, png_ptr->palette_lookup); - png_large_free(png_ptr, png_ptr->dither_index); + png_free(png_ptr, png_ptr->palette_lookup); + png_free(png_ptr, png_ptr->dither_index); #endif #if defined(PNG_READ_GAMMA_SUPPORTED) - png_large_free(png_ptr, png_ptr->gamma_table); + png_free(png_ptr, png_ptr->gamma_table); #endif #if defined(PNG_READ_BACKGROUND_SUPPORTED) - png_large_free(png_ptr, png_ptr->gamma_from_1); - png_large_free(png_ptr, png_ptr->gamma_to_1); + png_free(png_ptr, png_ptr->gamma_from_1); + png_free(png_ptr, png_ptr->gamma_to_1); #endif - if (png_ptr->do_free & PNG_FREE_PALETTE) - png_large_free(png_ptr, png_ptr->palette); + if (png_ptr->flags & PNG_FLAG_FREE_PALETTE) + png_free(png_ptr, png_ptr->palette); #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_bKGD_SUPPORTED) - if (png_ptr->do_free & PNG_FREE_TRANS) - png_large_free(png_ptr, png_ptr->trans); + if (png_ptr->flags & PNG_FLAG_FREE_TRANS) + png_free(png_ptr, png_ptr->trans); #endif #if defined(PNG_READ_hIST_SUPPORTED) - if (png_ptr->do_free & PNG_FREE_HIST) - png_large_free(png_ptr, png_ptr->hist); + if (png_ptr->flags & PNG_FLAG_FREE_HIST) + png_free(png_ptr, png_ptr->hist); #endif #if defined(PNG_READ_GAMMA_SUPPORTED) if (png_ptr->gamma_16_table) { for (i = 0; i < (1 << (8 - png_ptr->gamma_shift)); i++) { - png_large_free(png_ptr, png_ptr->gamma_16_table[i]); + png_free(png_ptr, png_ptr->gamma_16_table[i]); } } #endif #if defined(PNG_READ_BACKGROUND_SUPPORTED) - png_large_free(png_ptr, png_ptr->gamma_16_table); + png_free(png_ptr, png_ptr->gamma_16_table); if (png_ptr->gamma_16_from_1) { for (i = 0; i < (1 << (8 - png_ptr->gamma_shift)); i++) { - png_large_free(png_ptr, png_ptr->gamma_16_from_1[i]); + png_free(png_ptr, png_ptr->gamma_16_from_1[i]); } } - png_large_free(png_ptr, png_ptr->gamma_16_from_1); + png_free(png_ptr, png_ptr->gamma_16_from_1); if (png_ptr->gamma_16_to_1) { for (i = 0; i < (1 << (8 - png_ptr->gamma_shift)); i++) { - png_large_free(png_ptr, png_ptr->gamma_16_to_1[i]); + png_free(png_ptr, png_ptr->gamma_16_to_1[i]); } } - png_large_free(png_ptr, png_ptr->gamma_16_to_1); + png_free(png_ptr, png_ptr->gamma_16_to_1); #endif - inflateEnd(png_ptr->zstream); - png_free(png_ptr, png_ptr->zstream); + inflateEnd(&png_ptr->zstream); #ifdef PNG_PROGRESSIVE_READ_SUPPORTED - png_large_free(png_ptr, png_ptr->save_buffer); + png_free(png_ptr, png_ptr->save_buffer); #endif /* Save the important info out of the png_struct, in case it is diff --git a/pngrio.c b/pngrio.c index facd4310..96d82903 100644 --- a/pngrio.c +++ b/pngrio.c @@ -1,10 +1,10 @@ /* pngrio.c - functions for data input - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 This file provides a location for all input. Users which need special handling are expected to write a function which has the same @@ -56,29 +56,19 @@ png_default_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length) #define NEAR_BUF_SIZE 1024 #define MIN(a,b) (a <= b ? a : b) -#ifdef _MSC_VER -/* for FP_OFF */ -#include -#endif - static void png_default_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length) { png_uint_32 check; png_byte *n_data; + FILE *io_ptr; /* Check if data really is near. If so, use usual code. */ -#ifdef _MSC_VER - /* do it this way just to quiet warning */ - FP_OFF(n_data) = FP_OFF(data); - if (FP_SEG(n_data) == FP_SEG(data)) -#else - /* this works in MSC also but with lost segment warning */ - n_data = (png_byte *)data; + n_data = (png_byte *)CVT_PTR_NOCHECK(data); + io_ptr = (FILE *)CVT_PTR(png_ptr->io_ptr); if ((png_bytep)n_data == data) -#endif { - check = fread(n_data, 1, (size_t)length, (FILE *)png_ptr->io_ptr); + check = fread(n_data, 1, (size_t)length, io_ptr); } else { @@ -89,7 +79,7 @@ png_default_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length) do { read = MIN(NEAR_BUF_SIZE, remaining); - err = fread(buf, 1, read, (FILE *)png_ptr->io_ptr); + err = fread(buf, 1, read, io_ptr); png_memcpy(data, buf, read); /* copy far buffer to near buffer */ if(err != read) break; diff --git a/pngrtran.c b/pngrtran.c index 0adf311b..fa9fe95b 100644 --- a/pngrtran.c +++ b/pngrtran.c @@ -1,10 +1,10 @@ /* pngrtran.c - transforms the data in a row for png readers - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 */ #define PNG_INTERNAL @@ -17,6 +17,12 @@ png_set_background(png_structp png_ptr, png_color_16p background_color, int background_gamma_code, int need_expand, double background_gamma) { + if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) + { + png_warning(png_ptr, "Application must supply a known background gamma"); + return; + } + png_ptr->transformations |= PNG_BACKGROUND; png_memcpy(&(png_ptr->background), background_color, sizeof(png_color_16)); @@ -63,7 +69,7 @@ png_set_dither(png_structp png_ptr, png_colorp palette, { int i; - png_ptr->dither_index = (png_bytep)png_large_malloc(png_ptr, + png_ptr->dither_index = (png_bytep)png_malloc(png_ptr, num_palette * sizeof (png_byte)); for (i = 0; i < num_palette; i++) png_ptr->dither_index[i] = (png_byte)i; @@ -80,7 +86,7 @@ png_set_dither(png_structp png_ptr, png_colorp palette, png_bytep sort; /* initialize an array to sort colors */ - sort = (png_bytep)png_large_malloc(png_ptr, num_palette * sizeof (png_byte)); + sort = (png_bytep)png_malloc(png_ptr, num_palette * sizeof (png_byte)); /* initialize the sort array */ for (i = 0; i < num_palette; i++) @@ -190,7 +196,7 @@ png_set_dither(png_structp png_ptr, png_colorp palette, } } } - png_large_free(png_ptr, sort); + png_free(png_ptr, sort); } else { @@ -210,9 +216,9 @@ png_set_dither(png_structp png_ptr, png_colorp palette, /* which original index points to this palette color */ /* initialize palette index arrays */ - index_to_palette = (png_bytep)png_large_malloc(png_ptr, + index_to_palette = (png_bytep)png_malloc(png_ptr, num_palette * sizeof (png_byte)); - palette_to_index = (png_bytep)png_large_malloc(png_ptr, + palette_to_index = (png_bytep)png_malloc(png_ptr, num_palette * sizeof (png_byte)); /* initialize the sort array */ @@ -222,7 +228,7 @@ png_set_dither(png_structp png_ptr, png_colorp palette, palette_to_index[i] = (png_byte)i; } - hash = (png_dsortpp)png_large_malloc(png_ptr, 769 * sizeof (png_dsortp)); + hash = (png_dsortpp)png_malloc(png_ptr, 769 * sizeof (png_dsortp)); for (i = 0; i < 769; i++) hash[i] = (png_dsortp)0; /* png_memset(hash, 0, 769 * sizeof (png_dsortp)); */ @@ -255,7 +261,7 @@ png_set_dither(png_structp png_ptr, png_colorp palette, { png_dsortp t; - t = png_large_malloc(png_ptr, sizeof (png_dsort)); + t = png_malloc(png_ptr, sizeof (png_dsort)); t->next = hash[d]; t->left = (png_byte)i; t->right = (png_byte)j; @@ -336,7 +342,7 @@ png_set_dither(png_structp png_ptr, png_colorp palette, png_dsortp t; t = p->next; - png_large_free(png_ptr, p); + png_free(png_ptr, p); p = t; } } @@ -344,9 +350,9 @@ png_set_dither(png_structp png_ptr, png_colorp palette, } max_d += 96; } - png_large_free(png_ptr, hash); - png_large_free(png_ptr, palette_to_index); - png_large_free(png_ptr, index_to_palette); + png_free(png_ptr, hash); + png_free(png_ptr, palette_to_index); + png_free(png_ptr, index_to_palette); } num_palette = maximum_colors; } @@ -371,12 +377,12 @@ png_set_dither(png_structp png_ptr, png_colorp palette, num_blue = (1 << PNG_DITHER_BLUE_BITS); num_entries = ((png_uint_32)1 << total_bits); - png_ptr->palette_lookup = (png_bytep )png_large_malloc(png_ptr, + png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr, (png_size_t)num_entries * sizeof (png_byte)); png_memset(png_ptr->palette_lookup, 0, (png_size_t)num_entries * sizeof (png_byte)); - distance = (png_bytep )png_large_malloc(png_ptr, + distance = (png_bytep )png_malloc(png_ptr, (png_size_t)num_entries * sizeof (png_byte)); png_memset(distance, 0xff, (png_size_t)num_entries * sizeof (png_byte)); @@ -422,27 +428,30 @@ png_set_dither(png_structp png_ptr, png_colorp palette, } } - png_large_free(png_ptr, distance); + png_free(png_ptr, distance); } } #endif #if defined(PNG_READ_GAMMA_SUPPORTED) -/* transform the image from the file_gamma to the screen_gamma */ +/* Transform the image from the file_gamma to the screen_gamma. We + only do transformations on images where the file_gamma and screen_gamma + are not close reciprocals, otherwise it slows things down slightly, and + also introduces small errors. */ void -png_set_gamma(png_structp png_ptr, double screen_gamma, - double file_gamma) +png_set_gamma(png_structp png_ptr, double screen_gamma, double file_gamma) { - png_ptr->transformations |= PNG_GAMMA; + if (fabs(screen_gamma * file_gamma - 1.0) > 0.05) + png_ptr->transformations |= PNG_GAMMA; png_ptr->gamma = (float)file_gamma; png_ptr->display_gamma = (float)screen_gamma; } #endif #if defined(PNG_READ_EXPAND_SUPPORTED) -/* expand paletted images to rgb, expand grayscale images of +/* Expand paletted images to rgb, expand grayscale images of less then 8 bit depth to 8 bit depth, and expand tRNS chunks - to alpha channels */ + to alpha channels. */ void png_set_expand(png_structp png_ptr) { @@ -458,6 +467,18 @@ png_set_gray_to_rgb(png_structp png_ptr) } #endif +#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) +/* Convert a RGB image to a grayscale of the given width. This would + allow us, for example, to convert a 24 bpp RGB image into an 8 or + 16 bpp grayscale image. (Not yet implemented.) */ +void +png_set_rgb_to_gray(png_structp png_ptr, int gray_bits) +{ + png_ptr->transformations |= PNG_RGB_TO_GRAY; + /* Need to do something with gray_bits here. */ +} +#endif + /* initialize everything needed for the read. This includes modifying the palette */ void @@ -804,6 +825,7 @@ png_read_transform_info(png_structp png_ptr, png_infop info_ptr) void png_do_read_transformations(png_structp png_ptr) { + #if defined(PNG_READ_EXPAND_SUPPORTED) if ((png_ptr->transformations & PNG_EXPAND) && png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE) @@ -835,12 +857,18 @@ png_do_read_transformations(png_structp png_ptr) #if defined(PNG_READ_GAMMA_SUPPORTED) if ((png_ptr->transformations & PNG_GAMMA) && - !(png_ptr->transformations & PNG_BACKGROUND)) + !(png_ptr->transformations & PNG_BACKGROUND) && + (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1, png_ptr->gamma_table, png_ptr->gamma_16_table, png_ptr->gamma_shift); #endif +#if defined(PNG_RGB_TO_GRAY_SUPPORTED) + if (png_ptr->transformations & PNG_RGB_TO_GRAY) + png_do_rgb_to_gray(&(png_ptr->row_info), png_ptr->row_buf + 1); +#endif + #if defined(PNG_READ_16_TO_8_SUPPORTED) if (png_ptr->transformations & PNG_16_TO_8) png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1); @@ -1097,6 +1125,7 @@ png_do_chop(png_row_infop row_info, png_bytep row) { png_bytep sp, dp; png_uint_32 i; + if (row && row_info && row_info->bit_depth == 16) { sp = row; @@ -1123,7 +1152,7 @@ png_do_chop(png_row_infop row_info, png_bytep row) /* add filler byte */ void png_do_read_filler(png_row_infop row_info, png_bytep row, - png_byte filler, png_byte flags) + png_byte filler, png_uint_32 flags) { png_bytep sp, dp; png_uint_32 i; @@ -1304,8 +1333,8 @@ png_build_grayscale_palette(int bit_depth, png_colorp palette) } } -/* This function is currently unused? */ -#if defined(PNG_READ_DITHER_SUPPORTED) || defined(PNG_CORRECT_PALETTE_SUPPORTED) +/* This function is currently unused. Do we really need it? */ +#if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED) void png_correct_palette(png_structp png_ptr, png_colorp palette, int num_palette) @@ -2137,6 +2166,7 @@ png_do_background(png_row_infop row_info, png_bytep row, break; } } + if (row_info->color_type & PNG_COLOR_MASK_ALPHA) { row_info->color_type &= ~PNG_COLOR_MASK_ALPHA; @@ -2463,123 +2493,131 @@ png_do_expand(png_row_infop row_info, png_bytep row, if (row && row_info) { - if (row_info->color_type == PNG_COLOR_TYPE_GRAY && - row_info->bit_depth < 8) + if (row_info->color_type == PNG_COLOR_TYPE_GRAY ) { - switch (row_info->bit_depth) + png_uint_16 gray = trans_value ? trans_value->gray : 0; + + if (row_info->bit_depth < 8) { - case 1: + switch (row_info->bit_depth) { - sp = row + (png_size_t)((row_info->width - 1) >> 3); - dp = row + (png_size_t)row_info->width - 1; - shift = 7 - (int)((row_info->width + 7) & 7); + case 1: + { + gray *= 0xff; + sp = row + (png_size_t)((row_info->width - 1) >> 3); + dp = row + (png_size_t)row_info->width - 1; + shift = 7 - (int)((row_info->width + 7) & 7); + for (i = 0; i < row_info->width; i++) + { + if ((*sp >> shift) & 0x1) + *dp = 0xff; + else + *dp = 0; + if (shift == 7) + { + shift = 0; + sp--; + } + else + shift++; + + dp--; + } + break; + } + case 2: + { + gray *= 0x55; + sp = row + (png_size_t)((row_info->width - 1) >> 2); + dp = row + (png_size_t)row_info->width - 1; + shift = (int)((3 - ((row_info->width + 3) & 3)) << 1); + for (i = 0; i < row_info->width; i++) + { + value = (*sp >> shift) & 0x3; + *dp = (png_byte)(value | (value << 2) | (value << 4) | + (value << 6)); + if (shift == 6) + { + shift = 0; + sp--; + } + else + shift += 2; + + dp--; + } + break; + } + case 4: + { + gray *= 0x11; + sp = row + (png_size_t)((row_info->width - 1) >> 1); + dp = row + (png_size_t)row_info->width - 1; + shift = (int)((1 - ((row_info->width + 1) & 1)) << 2); + for (i = 0; i < row_info->width; i++) + { + value = (*sp >> shift) & 0xf; + *dp = (png_byte)(value | (value << 4)); + if (shift == 4) + { + shift = 0; + sp--; + } + else + shift = 4; + + dp--; + } + break; + } + } + row_info->bit_depth = 8; + row_info->pixel_depth = 8; + row_info->rowbytes = row_info->width; + } + + if (trans_value) + { + if (row_info->bit_depth == 8) + { + sp = row + (png_size_t)row_info->width - 1; + dp = row + (png_size_t)(row_info->width << 1) - 1; for (i = 0; i < row_info->width; i++) { - if ((*sp >> shift) & 0x1) - *dp = 0xff; + if (*sp == gray) + *dp-- = 0; else - *dp = 0; - if (shift == 7) - { - shift = 0; - sp--; - } - else - shift++; - - dp--; + *dp-- = 0xff; + *dp-- = *sp--; } - break; } - case 2: + else if (row_info->bit_depth == 16) { - sp = row + (png_size_t)((row_info->width - 1) >> 2); - dp = row + (png_size_t)row_info->width - 1; - shift = (int)((3 - ((row_info->width + 3) & 3)) << 1); + sp = row + (png_size_t)row_info->rowbytes - 1; + dp = row + (png_size_t)(row_info->rowbytes << 1) - 1; for (i = 0; i < row_info->width; i++) { - value = (*sp >> shift) & 0x3; - *dp = (png_byte)(value | (value << 2) | (value << 4) | - (value << 6)); - if (shift == 6) + if (((png_uint_16)*(sp) | + ((png_uint_16)*(sp - 1) << 8)) == gray) { - shift = 0; - sp--; + *dp-- = 0; + *dp-- = 0; } else - shift += 2; - - dp--; - } - break; - } - case 4: - { - sp = row + (png_size_t)((row_info->width - 1) >> 1); - dp = row + (png_size_t)row_info->width - 1; - shift = (int)((1 - ((row_info->width + 1) & 1)) << 2); - for (i = 0; i < row_info->width; i++) - { - value = (*sp >> shift) & 0xf; - *dp = (png_byte)(value | (value << 4)); - if (shift == 4) { - shift = 0; - sp--; + *dp-- = 0xff; + *dp-- = 0xff; } - else - shift = 4; - - dp--; + *dp-- = *sp--; + *dp-- = *sp--; } - break; } + row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; + row_info->channels = 2; + row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); + row_info->rowbytes = + ((row_info->width * row_info->pixel_depth) >> 3); } - row_info->bit_depth = 8; - row_info->pixel_depth = 8; - row_info->rowbytes = row_info->width; - } - if (row_info->color_type == PNG_COLOR_TYPE_GRAY && trans_value) - { - if (row_info->bit_depth == 8) - { - sp = row + (png_size_t)row_info->width - 1; - dp = row + (png_size_t)(row_info->width << 1) - 1; - for (i = 0; i < row_info->width; i++) - { - if (*sp == trans_value->gray) - *dp-- = 0; - else - *dp-- = 0xff; - *dp-- = *sp--; - } - } - else if (row_info->bit_depth == 16) - { - sp = row + (png_size_t)row_info->rowbytes - 1; - dp = row + (png_size_t)(row_info->rowbytes << 1) - 1; - for (i = 0; i < row_info->width; i++) - { - if (((png_uint_16)*(sp) | - ((png_uint_16)*(sp - 1) << 8)) == trans_value->gray) - { - *dp-- = 0; - *dp-- = 0; - } - else - { - *dp-- = 0xff; - *dp-- = 0xff; - } - *dp-- = *sp--; - *dp-- = *sp--; - } - } - row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; - row_info->channels = 2; - row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); - row_info->rowbytes = - ((row_info->width * row_info->pixel_depth) >> 3); } else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value) { @@ -2742,7 +2780,7 @@ png_build_gamma_table(png_structp png_ptr) g = 1.0 / (png_ptr->gamma * png_ptr->display_gamma); - png_ptr->gamma_table = (png_bytep)png_large_malloc(png_ptr, + png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr, (png_uint_32)256); for (i = 0; i < 256; i++) @@ -2755,7 +2793,7 @@ png_build_gamma_table(png_structp png_ptr) { g = 1.0 / (png_ptr->gamma); - png_ptr->gamma_to_1 = (png_bytep)png_large_malloc(png_ptr, + png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr, (png_uint_32)256); for (i = 0; i < 256; i++) @@ -2766,7 +2804,7 @@ png_build_gamma_table(png_structp png_ptr) g = 1.0 / (png_ptr->display_gamma); - png_ptr->gamma_from_1 = (png_bytep)png_large_malloc(png_ptr, + png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr, (png_uint_32)256); for (i = 0; i < 256; i++) @@ -2818,7 +2856,7 @@ png_build_gamma_table(png_structp png_ptr) g = 1.0 / (png_ptr->gamma * png_ptr->display_gamma); - png_ptr->gamma_16_table = (png_uint_16pp)png_large_malloc(png_ptr, + png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr, num * sizeof (png_uint_16p )); if ((png_ptr->transformations & PNG_16_TO_8) && @@ -2829,7 +2867,7 @@ png_build_gamma_table(png_structp png_ptr) for (i = 0; i < num; i++) { - png_ptr->gamma_16_table[i] = (png_uint_16p)png_large_malloc(png_ptr, + png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, 256 * sizeof (png_uint_16)); } @@ -2860,7 +2898,7 @@ png_build_gamma_table(png_structp png_ptr) { for (i = 0; i < num; i++) { - png_ptr->gamma_16_table[i] = (png_uint_16p)png_large_malloc(png_ptr, + png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, 256 * sizeof (png_uint_16)); ig = (((png_uint_32)i * @@ -2878,12 +2916,12 @@ png_build_gamma_table(png_structp png_ptr) { g = 1.0 / (png_ptr->gamma); - png_ptr->gamma_16_to_1 = (png_uint_16pp)png_large_malloc(png_ptr, + png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr, num * sizeof (png_uint_16p )); for (i = 0; i < num; i++) { - png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_large_malloc(png_ptr, + png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr, 256 * sizeof (png_uint_16)); ig = (((png_uint_32)i * @@ -2897,12 +2935,12 @@ png_build_gamma_table(png_structp png_ptr) } g = 1.0 / (png_ptr->display_gamma); - png_ptr->gamma_16_from_1 = (png_uint_16pp)png_large_malloc(png_ptr, + png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr, num * sizeof (png_uint_16p)); for (i = 0; i < num; i++) { - png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_large_malloc(png_ptr, + png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr, 256 * sizeof (png_uint_16)); ig = (((png_uint_32)i * diff --git a/pngrutil.c b/pngrutil.c index 0c9e9dfb..08ca9d7e 100644 --- a/pngrutil.c +++ b/pngrutil.c @@ -1,10 +1,10 @@ /* pngrutil.c - utilities to read a png file - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 */ #define PNG_INTERNAL @@ -36,35 +36,165 @@ png_get_uint_16(png_bytep buf) return i; } -/* read data, and run it through the crc */ +/* Set the action on getting a CRC error for an ancillary or critical chunk. */ +void +png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action) +{ + /* Tell libpng how we react to CRC errors in critical chunks */ + switch (crit_action) + { + case PNG_CRC_NO_CHANGE: /* leave setting as is */ + break; + case PNG_CRC_WARN_USE: /* warn/use data */ + png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; + png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; + break; + case PNG_CRC_QUIET_USE: /* quiet/use data */ + png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; + png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | + PNG_FLAG_CRC_CRITICAL_IGNORE; + break; + case PNG_CRC_WARN_DISCARD: /* not a valid action for critical data */ + png_warning(png_ptr, "Can't discard critical data on CRC error."); + case PNG_CRC_ERROR_QUIT: /* error/quit */ + case PNG_CRC_DEFAULT: + default: + png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; + break; + } + + switch (ancil_action) + { + case PNG_CRC_NO_CHANGE: /* leave setting as is */ + break; + case PNG_CRC_WARN_USE: /* warn/use data */ + png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; + png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; + break; + case PNG_CRC_QUIET_USE: /* quiet/use data */ + png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; + png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | + PNG_FLAG_CRC_ANCILLARY_NOWARN; + break; + case PNG_CRC_ERROR_QUIT: /* error/quit */ + png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; + png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; + break; + case PNG_CRC_WARN_DISCARD: /* warn/discard data */ + case PNG_CRC_DEFAULT: + default: + png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; + break; + } +} + +/* Read data, and (optionally) run it through the CRC. */ void png_crc_read(png_structp png_ptr, png_bytep buf, png_uint_32 length) { + int need_crc = 1; + + if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ + { + if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == + (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) + need_crc = 0; + } + else /* critical */ + { + if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) + need_crc = 0; + } + png_read_data(png_ptr, buf, length); - png_calculate_crc(png_ptr, buf, length); + + if (need_crc) + png_calculate_crc(png_ptr, buf, length); } -/* skip data, but calcuate the crc anyway */ -void -png_crc_skip(png_structp png_ptr, png_uint_32 length) +/* Optionally skip data and then check the CRC. Depending on whether we + are reading a ancillary or critical chunk, and how the program has set + things up, we may calculate the CRC on the data and print a message. */ +int +png_crc_finish(png_structp png_ptr, png_uint_32 skip) { + int need_crc = 1; + int crc_error; png_uint_32 i; - for (i = length; i > png_ptr->zbuf_size; i -= png_ptr->zbuf_size) + if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ + { + if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == + (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) + need_crc = 0; + } + else /* critical */ + { + if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) + need_crc = 0; + } + + for (i = skip; i > png_ptr->zbuf_size; i -= png_ptr->zbuf_size) { png_read_data(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); - png_calculate_crc(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); + if (need_crc) + png_calculate_crc(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); } if (i) { png_read_data(png_ptr, png_ptr->zbuf, i); - png_calculate_crc(png_ptr, png_ptr->zbuf, i); + if (need_crc) + png_calculate_crc(png_ptr, png_ptr->zbuf, i); } + + crc_error = png_crc_error(png_ptr); + + if (need_crc && crc_error) + { + char msg[80]; + + sprintf(msg,"CRC error in %s", png_ptr->chunk_name); + + if ((png_ptr->chunk_name[0] & 0x20 && /* Ancillary */ + !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) || + (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */ + png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)) + { + png_warning(png_ptr, msg); + } + else + { + png_error(png_ptr, msg); + } + return 1; + } + + return 0; } +/* Compare the CRC stored in the PNG file with that calulated by libpng from + the data it has read thus far. */ +int +png_crc_error(png_structp png_ptr) +{ + png_byte crc_bytes[4]; + png_uint_32 crc; + + png_read_data(png_ptr, crc_bytes, 4); + + crc = png_get_uint_32(crc_bytes); + +#ifdef PNG_USE_OWN_CRC + return (((crc^0xffffffffL) & 0xffffffffL) != (png_ptr->crc & 0xffffffffL)); +#else + return (crc != png_ptr->crc); +#endif +} + + /* read and check the IDHR chunk */ void -png_handle_IHDR(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte buf[13]; png_uint_32 width, height; @@ -78,7 +208,10 @@ png_handle_IHDR(png_structp png_ptr, png_infop info, png_uint_32 length) if (length != 13) png_error(png_ptr, "Invalid IHDR chunk"); + png_ptr->mode |= PNG_HAVE_IHDR; + png_crc_read(png_ptr, buf, 13); + png_crc_finish(png_ptr, 0); width = png_get_uint_32(buf); height = png_get_uint_32(buf + 4); @@ -89,7 +222,7 @@ png_handle_IHDR(png_structp png_ptr, png_infop info, png_uint_32 length) interlace_type = buf[12]; /* check for width and height valid values */ - if (width == 0 || height == 0) + if (width == 0 || width > 2147483647 || height == 0 || height > 2147483647) png_error(png_ptr, "Invalid image size in IHDR"); /* check other values */ @@ -102,24 +235,20 @@ png_handle_IHDR(png_structp png_ptr, png_infop info, png_uint_32 length) color_type == 5 || color_type > 6) png_error(png_ptr, "Invalid color type in IHDR"); - if (color_type == PNG_COLOR_TYPE_PALETTE && - bit_depth == 16) - png_error(png_ptr, "Invalid color type and bit depth combination in IHDR"); - - if ((color_type == PNG_COLOR_TYPE_RGB || - color_type == PNG_COLOR_TYPE_GRAY_ALPHA || - color_type == PNG_COLOR_TYPE_RGB_ALPHA) && - bit_depth < 8) - png_error(png_ptr, "Invalid color type and bit depth in IHDR"); + if ((color_type == PNG_COLOR_TYPE_PALETTE && bit_depth) > 8 || + ((color_type == PNG_COLOR_TYPE_RGB || + color_type == PNG_COLOR_TYPE_GRAY_ALPHA || + color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8)) + png_error(png_ptr, "Invalid color type/bit depth combination in IHDR"); if (interlace_type > 1) - png_error(png_ptr, "Invalid interlace method in IHDR"); + png_error(png_ptr, "Unknown interlace method in IHDR"); if (compression_type > 0) - png_error(png_ptr, "Invalid compression method in IHDR"); + png_error(png_ptr, "Unknown compression method in IHDR"); if (filter_type > 0) - png_error(png_ptr, "Invalid filter method in IHDR"); + png_error(png_ptr, "Unknown filter method in IHDR"); /* set internal variables */ png_ptr->width = width; @@ -145,34 +274,40 @@ png_handle_IHDR(png_structp png_ptr, png_infop info, png_uint_32 length) png_ptr->channels = 4; break; } + /* set up other useful info */ png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels); png_ptr->rowbytes = ((png_ptr->width * (png_uint_32)png_ptr->pixel_depth + 7) >> 3); - /* call the IHDR callback (which should just set up info) */ - png_read_IHDR(png_ptr, info, width, height, bit_depth, + png_read_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, compression_type, filter_type, interlace_type); - - png_ptr->mode |= PNG_HAVE_IHDR; } /* read and check the palette */ void -png_handle_PLTE(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { - int num, i; png_colorp palette; + int num, i; if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before PLTE"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid PLTE after IDAT"); + png_crc_finish(png_ptr, length); + return; + } else if (png_ptr->mode & PNG_HAVE_PLTE) - png_error(png_ptr, "Multiple PLTE"); + png_error(png_ptr, "Duplicate PLTE chunk"); + + png_ptr->mode |= PNG_HAVE_PLTE; #if !defined(PNG_READ_OPT_PLTE_SUPPORTED) if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) { - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } #endif @@ -182,7 +317,7 @@ png_handle_PLTE(png_structp png_ptr, png_infop info, png_uint_32 length) if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) { png_warning(png_ptr, "Invalid palette chunk"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } else @@ -192,8 +327,8 @@ png_handle_PLTE(png_structp png_ptr, png_infop info, png_uint_32 length) } num = (int)length / 3; - palette = (png_colorp)png_large_malloc(png_ptr, num * sizeof (png_color)); - png_ptr->do_free |= PNG_FREE_PALETTE; + palette = (png_colorp)png_malloc(png_ptr, num * sizeof (png_color)); + png_ptr->flags |= PNG_FLAG_FREE_PALETTE; for (i = 0; i < num; i++) { png_byte buf[3]; @@ -204,16 +339,69 @@ png_handle_PLTE(png_structp png_ptr, png_infop info, png_uint_32 length) palette[i].green = buf[1]; palette[i].blue = buf[2]; } + + /* If we actually NEED the PLTE chunk (ie for a paletted image), we do + whatever the normal CRC configuration tells us. However, if we + have an RGB image, the PLTE can be considered ancillary, so + we will act as though it is. */ + if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) + { + if (png_crc_finish(png_ptr, 0)) + return; + } + else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */ + { + char msg[80]; + + sprintf(msg,"CRC error in %s", png_ptr->chunk_name); + + /* If we don't want to use the data from an ancillary chunk, + we have two options: an error abort, or a warning and we + ignore the data in this chunk (which should be OK, since + it's considered ancillary for a RGB or RGBA image). */ + if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE)) + { + if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) + { + png_error(png_ptr, msg); + } + else + { + png_warning(png_ptr, msg); + return; + } + } + else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) + { + png_warning(png_ptr, msg); + } + } + png_ptr->palette = palette; png_ptr->num_palette = (png_uint_16)num; - png_read_PLTE(png_ptr, info, palette, num); + png_read_PLTE(png_ptr, info_ptr, palette, num); +} - png_ptr->mode |= PNG_HAVE_PLTE; +void +png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT)) + { + png_error(png_ptr, "No image in file"); + } + + png_ptr->mode |= PNG_AFTER_IDAT | PNG_HAVE_IEND; + + if (length != 0) + { + png_warning(png_ptr, "Incorrect IEND chunk length"); + png_crc_finish(png_ptr, length); + } } #if defined(PNG_READ_gAMA_SUPPORTED) void -png_handle_gAMA(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_uint_32 igamma; float gamma; @@ -221,57 +409,87 @@ png_handle_gAMA(png_structp png_ptr, png_infop info, png_uint_32 length) if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before gAMA"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid gAMA after IDAT"); + png_crc_finish(png_ptr, length); + return; + } else if (png_ptr->mode & PNG_HAVE_PLTE) /* Should be an error, but we can cope with it */ png_warning(png_ptr, "Out of place gAMA chunk"); + else if (info_ptr && info_ptr->valid & PNG_INFO_gAMA) + { + png_warning(png_ptr, "Duplicate gAMA chunk"); + png_crc_finish(png_ptr, length); + return; + } if (length != 4) { png_warning(png_ptr, "Incorrect gAMA chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 4); + if (png_crc_finish(png_ptr, 0)) + return; + igamma = png_get_uint_32(buf); /* check for zero gamma */ if (!igamma) return; gamma = (float)igamma / (float)100000.0; - png_read_gAMA(png_ptr, info, gamma); png_ptr->gamma = gamma; + png_read_gAMA(png_ptr, info_ptr, gamma); } #endif #if defined(PNG_READ_sBIT_SUPPORTED) void -png_handle_sBIT(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { - int slen; + png_uint_32 truelen; png_byte buf[4]; buf[0] = buf[1] = buf[2] = buf[3] = 0; if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before sBIT"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid sBIT after IDAT"); + png_crc_finish(png_ptr, length); + return; + } else if (png_ptr->mode & PNG_HAVE_PLTE) /* Should be an error, but we can cope with it */ png_warning(png_ptr, "Out of place sBIT chunk"); + else if (info_ptr && info_ptr->valid & PNG_INFO_sBIT) + { + png_warning(png_ptr, "Duplicate sBIT chunk"); + png_crc_finish(png_ptr, length); + return; + } if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) - slen = 3; + truelen = 3; else - slen = png_ptr->channels; + truelen = png_ptr->channels; - if (length != (png_uint_32)slen) + if (length != truelen) { png_warning(png_ptr, "Incorrect sBIT chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, length); + if (png_crc_finish(png_ptr, 0)) + return; + if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) { png_ptr->sig_bit.red = buf[0]; @@ -284,74 +502,133 @@ png_handle_sBIT(png_structp png_ptr, png_infop info, png_uint_32 length) png_ptr->sig_bit.gray = buf[0]; png_ptr->sig_bit.alpha = buf[1]; } - png_read_sBIT(png_ptr, info, &(png_ptr->sig_bit)); + png_read_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); } #endif #if defined(PNG_READ_cHRM_SUPPORTED) void -png_handle_cHRM(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte buf[4]; - png_uint_32 v; + png_uint_32 val; float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y; if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before sBIT"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid cHRM after IDAT"); + png_crc_finish(png_ptr, length); + return; + } else if (png_ptr->mode & PNG_HAVE_PLTE) /* Should be an error, but we can cope with it */ png_warning(png_ptr, "Missing PLTE before cHRM"); + else if (info_ptr && info_ptr->valid & PNG_INFO_cHRM) + { + png_warning(png_ptr, "Duplicate cHRM chunk"); + png_crc_finish(png_ptr, length); + return; + } if (length != 32) { png_warning(png_ptr, "Incorrect cHRM chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 4); - v = png_get_uint_32(buf); - white_x = (float)v / (float)100000.0; + val = png_get_uint_32(buf); + white_x = (float)val / (float)100000.0; png_crc_read(png_ptr, buf, 4); - v = png_get_uint_32(buf); - white_y = (float)v / (float)100000.0; + val = png_get_uint_32(buf); + white_y = (float)val / (float)100000.0; + + if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 || + white_x + white_y > 1.0) + { + png_warning(png_ptr, "Invalid cHRM white point"); + png_crc_finish(png_ptr, 24); + return; + } png_crc_read(png_ptr, buf, 4); - v = png_get_uint_32(buf); - red_x = (float)v / (float)100000.0; + val = png_get_uint_32(buf); + red_x = (float)val / (float)100000.0; png_crc_read(png_ptr, buf, 4); - v = png_get_uint_32(buf); - red_y = (float)v / (float)100000.0; + val = png_get_uint_32(buf); + red_y = (float)val / (float)100000.0; + + if (red_x < 0 || red_x > 0.8 || red_y < 0 || red_y > 0.8 || + red_x + red_y > 1.0) + { + png_warning(png_ptr, "Invalid cHRM red point"); + png_crc_finish(png_ptr, 16); + return; + } png_crc_read(png_ptr, buf, 4); - v = png_get_uint_32(buf); - green_x = (float)v / (float)100000.0; + val = png_get_uint_32(buf); + green_x = (float)val / (float)100000.0; png_crc_read(png_ptr, buf, 4); - v = png_get_uint_32(buf); - green_y = (float)v / (float)100000.0; + val = png_get_uint_32(buf); + green_y = (float)val / (float)100000.0; + + if (green_x < 0 || green_x > 0.8 || green_y < 0 || green_y > 0.8 || + green_x + green_y > 1.0) + { + png_warning(png_ptr, "Invalid cHRM green point"); + png_crc_finish(png_ptr, 8); + return; + } png_crc_read(png_ptr, buf, 4); - v = png_get_uint_32(buf); - blue_x = (float)v / (float)100000.0; + val = png_get_uint_32(buf); + blue_x = (float)val / (float)100000.0; png_crc_read(png_ptr, buf, 4); - v = png_get_uint_32(buf); - blue_y = (float)v / (float)100000.0; + val = png_get_uint_32(buf); + blue_y = (float)val / (float)100000.0; - png_read_cHRM(png_ptr, info, + if (blue_x < 0 || blue_x > 0.8 || blue_y < 0 || blue_y > 0.8 || + blue_x + blue_y > 1.0) + { + png_warning(png_ptr, "Invalid cHRM blue point"); + png_crc_finish(png_ptr, 0); + return; + } + + if (png_crc_finish(png_ptr, 0)) + return; + + png_read_cHRM(png_ptr, info_ptr, white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y); } #endif #if defined(PNG_READ_tRNS_SUPPORTED) void -png_handle_tRNS(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before tRNS"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid tRNS after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr && info_ptr->valid & PNG_INFO_tRNS) + { + png_warning(png_ptr, "Duplcate tRNS chunk"); + png_crc_finish(png_ptr, length); + return; + } if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { @@ -363,12 +640,12 @@ png_handle_tRNS(png_structp png_ptr, png_infop info, png_uint_32 length) else if (length > png_ptr->num_palette) { png_warning(png_ptr, "Incorrect tRNS chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } - png_ptr->trans = (png_bytep)png_large_malloc(png_ptr, length); - png_ptr->do_free |= PNG_FREE_TRANS; + png_ptr->trans = (png_bytep)png_malloc(png_ptr, length); + png_ptr->flags |= PNG_FLAG_FREE_TRANS; png_crc_read(png_ptr, png_ptr->trans, length); png_ptr->num_trans = (png_uint_16)length; } @@ -379,7 +656,7 @@ png_handle_tRNS(png_structp png_ptr, png_infop info, png_uint_32 length) if (length != 6) { png_warning(png_ptr, "Incorrect tRNS chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } @@ -396,7 +673,7 @@ png_handle_tRNS(png_structp png_ptr, png_infop info, png_uint_32 length) if (length != 2) { png_warning(png_ptr, "Incorrect tRNS chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } @@ -407,29 +684,44 @@ png_handle_tRNS(png_structp png_ptr, png_infop info, png_uint_32 length) else { png_warning(png_ptr, "tRNS chunk not allowed with alpha channel"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } - png_read_tRNS(png_ptr, info, png_ptr->trans, png_ptr->num_trans, + if (png_crc_finish(png_ptr, 0)) + return; + + png_read_tRNS(png_ptr, info_ptr, png_ptr->trans, png_ptr->num_trans, &(png_ptr->trans_values)); } #endif #if defined(PNG_READ_bKGD_SUPPORTED) void -png_handle_bKGD(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { - int truelen; + png_uint_32 truelen; png_byte buf[6]; if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before bKGD"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid bKGD after IDAT"); + png_crc_finish(png_ptr, length); + return; + } else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && !(png_ptr->mode & PNG_HAVE_PLTE)) { png_warning(png_ptr, "Missing PLTE before bKGD"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr && info_ptr->valid & PNG_INFO_bKGD) + { + png_warning(png_ptr, "Duplicate bKGD chunk"); + png_crc_finish(png_ptr, length); return; } @@ -440,18 +732,21 @@ png_handle_bKGD(png_structp png_ptr, png_infop info, png_uint_32 length) else truelen = 2; - if (length != (png_uint_32)truelen) + if (length != truelen) { png_warning(png_ptr, "Incorrect bKGD chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, length); + if (png_crc_finish(png_ptr, 0)) + return; + /* We convert the index value into RGB components so that we can allow * arbitrary RGB values for background when we have transparency, and * so it is easy to determine the RGB values of the background color - * from the info_ptr. */ + * from the info_ptr struct. */ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { png_ptr->background.index = buf[0]; @@ -473,36 +768,48 @@ png_handle_bKGD(png_structp png_ptr, png_infop info, png_uint_32 length) png_ptr->background.blue = png_get_uint_16(buf + 4); } - png_read_bKGD(png_ptr, info, &(png_ptr->background)); + png_read_bKGD(png_ptr, info_ptr, &(png_ptr->background)); } #endif #if defined(PNG_READ_hIST_SUPPORTED) void -png_handle_hIST(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { int num, i; if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before hIST"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid hIST after IDAT"); + png_crc_finish(png_ptr, length); + return; + } else if (!(png_ptr->mode & PNG_HAVE_PLTE)) { png_warning(png_ptr, "Missing PLTE before hIST"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr && info_ptr->valid & PNG_INFO_hIST) + { + png_warning(png_ptr, "Duplicate hIST chunk"); + png_crc_finish(png_ptr, length); return; } if (length != 2 * png_ptr->num_palette) { png_warning(png_ptr, "Incorrect hIST chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } num = (int)length / 2; - png_ptr->hist = (png_uint_16p)png_large_malloc(png_ptr, + png_ptr->hist = (png_uint_16p)png_malloc(png_ptr, num * sizeof (png_uint_16)); - png_ptr->do_free |= PNG_FREE_HIST; + png_ptr->flags |= PNG_FLAG_FREE_HIST; for (i = 0; i < num; i++) { png_byte buf[2]; @@ -510,13 +817,17 @@ png_handle_hIST(png_structp png_ptr, png_infop info, png_uint_32 length) png_crc_read(png_ptr, buf, 2); png_ptr->hist[i] = png_get_uint_16(buf); } - png_read_hIST(png_ptr, info, png_ptr->hist); + + if (png_crc_finish(png_ptr, 0)) + return; + + png_read_hIST(png_ptr, info_ptr, png_ptr->hist); } #endif #if defined(PNG_READ_pHYs_SUPPORTED) void -png_handle_pHYs(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte buf[9]; png_uint_32 res_x, res_y; @@ -524,26 +835,40 @@ png_handle_pHYs(png_structp png_ptr, png_infop info, png_uint_32 length) if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before pHYS"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid pHYS after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr && info_ptr->valid & PNG_INFO_pHYs) + { + png_warning(png_ptr, "Duplicate pHYS chunk"); + png_crc_finish(png_ptr, length); + return; + } if (length != 9) { png_warning(png_ptr, "Incorrect pHYs chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 9); + if (png_crc_finish(png_ptr, 0)) + return; res_x = png_get_uint_32(buf); res_y = png_get_uint_32(buf + 4); unit_type = buf[8]; - png_read_pHYs(png_ptr, info, res_x, res_y, unit_type); + png_read_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); } #endif #if defined(PNG_READ_oFFs_SUPPORTED) void -png_handle_oFFs(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte buf[9]; png_uint_32 offset_x, offset_y; @@ -551,41 +876,66 @@ png_handle_oFFs(png_structp png_ptr, png_infop info, png_uint_32 length) if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before oFFs"); + else if (png_ptr->mode & PNG_HAVE_IDAT) + { + png_warning(png_ptr, "Invalid oFFs after IDAT"); + png_crc_finish(png_ptr, length); + return; + } + else if (info_ptr && info_ptr->valid & PNG_INFO_oFFs) + { + png_warning(png_ptr, "Duplicate oFFs chunk"); + png_crc_finish(png_ptr, length); + return; + } if (length != 9) { png_warning(png_ptr, "Incorrect oFFs chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 9); + if (png_crc_finish(png_ptr, 0)) + return; offset_x = png_get_uint_32(buf); offset_y = png_get_uint_32(buf + 4); unit_type = buf[8]; - png_read_oFFs(png_ptr, info, offset_x, offset_y, unit_type); + png_read_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); } #endif #if defined(PNG_READ_tIME_SUPPORTED) void -png_handle_tIME(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_byte buf[7]; png_time mod_time; if (!(png_ptr->mode & PNG_HAVE_IHDR)) - png_error(png_ptr, "Missing IHDR before tIME"); + png_error(png_ptr, "Out of place tIME chunk"); + else if (info_ptr && info_ptr->valid & PNG_INFO_tIME) + { + png_warning(png_ptr, "Duplicate tIME chunk"); + png_crc_finish(png_ptr, length); + return; + } + + if (png_ptr->mode & PNG_HAVE_IDAT) + png_ptr->mode |= PNG_AFTER_IDAT; if (length != 7) { png_warning(png_ptr, "Incorrect tIME chunk length"); - png_crc_skip(png_ptr, length); + png_crc_finish(png_ptr, length); return; } png_crc_read(png_ptr, buf, 7); + if (png_crc_finish(png_ptr, 0)) + return; mod_time.second = buf[6]; mod_time.minute = buf[5]; @@ -594,14 +944,14 @@ png_handle_tIME(png_structp png_ptr, png_infop info, png_uint_32 length) mod_time.month = buf[2]; mod_time.year = png_get_uint_16(buf); - png_read_tIME(png_ptr, info, &mod_time); + png_read_tIME(png_ptr, info_ptr, &mod_time); } #endif #if defined(PNG_READ_tEXt_SUPPORTED) /* note: this does not correctly handle chunks that are > 64K */ void -png_handle_tEXt(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { png_charp key; png_charp text; @@ -609,8 +959,17 @@ png_handle_tEXt(png_structp png_ptr, png_infop info, png_uint_32 length) if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before tEXt"); - key = (png_charp )png_large_malloc(png_ptr, length + 1); + if (png_ptr->mode & PNG_HAVE_IDAT) + png_ptr->mode |= PNG_AFTER_IDAT; + + key = (png_charp )png_malloc(png_ptr, length + 1); png_crc_read(png_ptr, (png_bytep )key, length); + if (png_crc_finish(png_ptr, 0)) + { + png_free(png_ptr, key); + return; + } + key[(png_size_t)length] = '\0'; for (text = key; *text; text++) @@ -619,7 +978,7 @@ png_handle_tEXt(png_structp png_ptr, png_infop info, png_uint_32 length) if (text != key + (png_size_t)length) text++; - png_read_tEXt(png_ptr, info, key, text, length - (text - key)); + png_read_tEXt(png_ptr, info_ptr, key, text, length - (text - key)); } #endif @@ -627,7 +986,7 @@ png_handle_tEXt(png_structp png_ptr, png_infop info, png_uint_32 length) /* note: this does not correctly handle chunks that are > 64K compressed on those systems that can't malloc more than 64KB at a time. */ void -png_handle_zTXt(png_structp png_ptr, png_infop info, png_uint_32 length) +png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) { static char msg[] = "Error decoding zTXt chunk"; png_charp key; @@ -637,8 +996,17 @@ png_handle_zTXt(png_structp png_ptr, png_infop info, png_uint_32 length) if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before zTXt"); - key = png_large_malloc(png_ptr, length + 1); + if (png_ptr->mode & PNG_HAVE_IDAT) + png_ptr->mode |= PNG_AFTER_IDAT; + + key = png_malloc(png_ptr, length + 1); png_crc_read(png_ptr, (png_bytep )key, length); + if (png_crc_finish(png_ptr, 0)) + { + png_free(png_ptr, key); + return; + } + key[(png_size_t)length] = '\0'; for (text = key; *text; text++) @@ -663,33 +1031,33 @@ png_handle_zTXt(png_structp png_ptr, png_infop info, png_uint_32 length) { text++; - png_ptr->zstream->next_in = (png_bytep )text; - png_ptr->zstream->avail_in = (uInt)(length - (text - key)); - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (png_size_t)png_ptr->zbuf_size; + png_ptr->zstream.next_in = (png_bytep )text; + png_ptr->zstream.avail_in = (uInt)(length - (text - key)); + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (png_size_t)png_ptr->zbuf_size; key_size = text - key; text_size = 0; text = NULL; - while (png_ptr->zstream->avail_in) + while (png_ptr->zstream.avail_in) { int ret; - ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH); + ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); if (ret != Z_OK && ret != Z_STREAM_END) { - if (png_ptr->zstream->msg) - png_warning(png_ptr, png_ptr->zstream->msg); + if (png_ptr->zstream.msg) + png_warning(png_ptr, png_ptr->zstream.msg); else png_warning(png_ptr, "zTXt decompression error"); - inflateReset(png_ptr->zstream); - png_ptr->zstream->avail_in = 0; + inflateReset(&png_ptr->zstream); + png_ptr->zstream.avail_in = 0; if (!text) { text_size = key_size + sizeof(msg) + 1; - text = (png_charp)png_large_malloc(png_ptr, text_size); + text = (png_charp)png_malloc(png_ptr, text_size); png_memcpy(text, key, (png_size_t)key_size); } @@ -701,18 +1069,18 @@ png_handle_zTXt(png_structp png_ptr, png_infop info, png_uint_32 length) png_memcpy(text + key_size, msg, (png_size_t)(text_size + 1)); break; } - if (!png_ptr->zstream->avail_out || ret == Z_STREAM_END) + if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END) { if (!text) { - text = (png_charp)png_large_malloc(png_ptr, - png_ptr->zbuf_size - png_ptr->zstream->avail_out + + text = (png_charp)png_malloc(png_ptr, + png_ptr->zbuf_size - png_ptr->zstream.avail_out + key_size + 1); png_memcpy(text + (png_size_t)key_size, png_ptr->zbuf, - (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream->avail_out)); + (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out)); png_memcpy(text, key, (png_size_t)key_size); text_size = key_size + (png_size_t)png_ptr->zbuf_size - - png_ptr->zstream->avail_out; + png_ptr->zstream.avail_out; *(text + (png_size_t)text_size) = '\0'; } else @@ -720,19 +1088,19 @@ png_handle_zTXt(png_structp png_ptr, png_infop info, png_uint_32 length) png_charp tmp; tmp = text; - text = png_large_malloc(png_ptr, text_size + - png_ptr->zbuf_size - png_ptr->zstream->avail_out + 1); + text = png_malloc(png_ptr, text_size + + png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1); png_memcpy(text, tmp, (png_size_t)text_size); - png_large_free(png_ptr, tmp); + png_free(png_ptr, tmp); png_memcpy(text + (png_size_t)text_size, png_ptr->zbuf, - (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream->avail_out)); - text_size += png_ptr->zbuf_size - png_ptr->zstream->avail_out; + (png_size_t)(png_ptr->zbuf_size - png_ptr->zstream.avail_out)); + text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; *(text + (png_size_t)text_size) = '\0'; } if (ret != Z_STREAM_END) { - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; } else { @@ -741,19 +1109,70 @@ png_handle_zTXt(png_structp png_ptr, png_infop info, png_uint_32 length) } } - inflateReset(png_ptr->zstream); - png_ptr->zstream->avail_in = 0; + inflateReset(&png_ptr->zstream); + png_ptr->zstream.avail_in = 0; - png_large_free(png_ptr, key); + png_free(png_ptr, key); key = text; text += (png_size_t)key_size; text_size -= key_size; } - png_read_zTXt(png_ptr, info, key, text, text_size, 0); + png_read_zTXt(png_ptr, info_ptr, key, text, text_size, 0); } #endif +/* This function is called when we haven't found a handler for a + chunk. If there isn't a problem with the chunk itself (ie bad + chunk name or a critical chunk), the chunk is silently ignored. */ +void +png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) +{ + /* In the future we can have code here that calls user-supplied + * callback functions for unknown chunks before they are ignored or + * cause an error. + */ + png_check_chunk_name(png_ptr, png_ptr->chunk_name); + + if (!(png_ptr->chunk_name[0] & 0x20)) + { + char msg[40]; + + sprintf(msg, "Unknown critical chunk %s", png_ptr->chunk_name); + png_error(png_ptr, msg); + } + + if (png_ptr->mode & PNG_HAVE_IDAT) + png_ptr->mode |= PNG_AFTER_IDAT; + + png_crc_finish(png_ptr, length); +} + +/* This function is called to verify that a chunk name is valid. + This function can't have the "critical chunk check" incorporated + into it, as in the future, we will need to be able to call user + functions to handle unknown critical chunks after we check that + the chunk name itself is valid. */ +void +png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name) +{ + if (chunk_name[0] < 41 || chunk_name[0] > 122 || + (chunk_name[0] > 90 && chunk_name[0] < 97) || + chunk_name[1] < 41 || chunk_name[1] > 122 || + (chunk_name[1] > 90 && chunk_name[1] < 97) || + chunk_name[2] < 41 || chunk_name[2] > 122 || + (chunk_name[2] > 90 && chunk_name[2] < 97) || + chunk_name[3] < 41 || chunk_name[3] > 122 || + (chunk_name[3] > 90 && chunk_name[3] < 97)) + { + char msg[45]; + + sprintf(msg, "Invalid chunk type 0x%02X 0x%02X 0x%02X 0x%02X", + chunk_name[0], chunk_name[1], chunk_name[2], chunk_name[3]); + png_error(png_ptr, msg); + } +} + /* Combines the row recently read in with the previous row. This routine takes care of alpha and transparency if requested. This routine also handles the two methods of progressive display @@ -763,8 +1182,7 @@ png_handle_zTXt(png_structp png_ptr, png_infop info, png_uint_32 length) bits are needed. A one indicates the pixels is to be combined, a zero indicates the pixel is to be skipped. This is in addition to any alpha or transparency value associated with the pixel. If - you want all pixels to be combined, pass 0xff (255) in mask. -*/ + you want all pixels to be combined, pass 0xff (255) in mask. */ void png_combine_row(png_structp png_ptr, png_bytep row, int mask) @@ -895,8 +1313,8 @@ png_combine_row(png_structp png_ptr, png_bytep row, { png_bytep sp; png_bytep dp; - png_uint_32 i; - int pixel_bytes, m; + png_uint_32 i, pixel_bytes; + png_byte m; pixel_bytes = (png_ptr->row_info.pixel_depth >> 3); @@ -1050,9 +1468,8 @@ png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass) { png_bytep sp, dp; png_byte v[8]; - png_uint_32 i; + png_uint_32 i, pixel_bytes; int j; - int pixel_bytes; pixel_bytes = (row_info->pixel_depth >> 3); @@ -1228,65 +1645,61 @@ png_read_finish_row(png_structp png_ptr) char extra; int ret; - png_ptr->zstream->next_out = (Byte *)&extra; - png_ptr->zstream->avail_out = (uInt)1; + png_ptr->zstream.next_out = (Byte *)&extra; + png_ptr->zstream.avail_out = (uInt)1; do { - if (!(png_ptr->zstream->avail_in)) + if (!(png_ptr->zstream.avail_in)) { while (!png_ptr->idat_size) { - png_byte buf[4]; - png_uint_32 crc; + png_byte chunk_length[4]; - png_read_data(png_ptr, buf, 4); - crc = png_get_uint_32(buf); - if (((crc ^ 0xffffffffL) & 0xffffffffL) != - (png_ptr->crc & 0xffffffffL)) - png_warning(png_ptr, "Bad CRC value"); + png_crc_finish(png_ptr, 0); + + png_read_data(png_ptr, chunk_length, 4); + png_ptr->idat_size = png_get_uint_32(chunk_length); - png_read_data(png_ptr, buf, 4); - png_ptr->idat_size = png_get_uint_32(buf); png_reset_crc(png_ptr); - - png_crc_read(png_ptr, buf, 4); - if (png_memcmp(buf, png_IDAT, 4)) + png_crc_read(png_ptr, png_ptr->chunk_name, 4); + if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) png_error(png_ptr, "Not enough image data"); } - png_ptr->zstream->avail_in = (uInt)png_ptr->zbuf_size; - png_ptr->zstream->next_in = png_ptr->zbuf; + png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_in = png_ptr->zbuf; if (png_ptr->zbuf_size > png_ptr->idat_size) - png_ptr->zstream->avail_in = (uInt)png_ptr->idat_size; - png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream->avail_in); - png_ptr->idat_size -= png_ptr->zstream->avail_in; + png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size; + png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in); + png_ptr->idat_size -= png_ptr->zstream.avail_in; } - ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH); + ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); if (ret == Z_STREAM_END) { - if (!(png_ptr->zstream->avail_out) || png_ptr->zstream->avail_in || + if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in || png_ptr->idat_size) png_error(png_ptr, "Extra compressed data"); - png_ptr->mode |= PNG_AT_LAST_IDAT; + png_ptr->mode |= PNG_AFTER_IDAT; + png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; break; } if (ret != Z_OK) - png_error(png_ptr, png_ptr->zstream->msg ? png_ptr->zstream->msg : + png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : "Decompression Error"); - if (!(png_ptr->zstream->avail_out)) + if (!(png_ptr->zstream.avail_out)) png_error(png_ptr, "Extra compressed data"); } while (1); - png_ptr->zstream->avail_out = 0; + png_ptr->zstream.avail_out = 0; } - if (png_ptr->idat_size || png_ptr->zstream->avail_in) + if (png_ptr->idat_size || png_ptr->zstream.avail_in) png_error(png_ptr, "Extra compression data"); - inflateReset(png_ptr->zstream); + inflateReset(&png_ptr->zstream); - png_ptr->mode |= PNG_AT_LAST_IDAT; + png_ptr->mode |= PNG_AFTER_IDAT; } void @@ -1295,7 +1708,7 @@ png_read_start_row(png_structp png_ptr) int max_pixel_depth; png_uint_32 rowbytes; - png_ptr->zstream->avail_in = 0; + png_ptr->zstream.avail_in = 0; png_init_read_transformations(png_ptr); if (png_ptr->interlaced) { @@ -1394,16 +1807,15 @@ png_read_start_row(png_structp png_ptr) if (rowbytes > 65536L) png_error(png_ptr, "This image requires a row greater than 64KB"); #endif - png_ptr->row_buf = (png_bytep )png_large_malloc(png_ptr, rowbytes); + png_ptr->row_buf = (png_bytep )png_malloc(png_ptr, rowbytes); #ifdef PNG_MAX_MALLOC_64K if (png_ptr->rowbytes + 1 > 65536L) png_error(png_ptr, "This image requires a row greater than 64KB"); #endif - png_ptr->prev_row = png_large_malloc(png_ptr, png_ptr->rowbytes + 1); + png_ptr->prev_row = png_malloc(png_ptr, png_ptr->rowbytes + 1); png_memset(png_ptr->prev_row, 0, (png_size_t)png_ptr->rowbytes + 1); png_ptr->flags |= PNG_FLAG_ROW_INIT; } - diff --git a/pngtest.c b/pngtest.c index 18fc07ab..76804e65 100644 --- a/pngtest.c +++ b/pngtest.c @@ -1,9 +1,10 @@ + /* pngtest.c - a simple test program to test libpng - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 */ #include @@ -20,7 +21,7 @@ /* input and output filenames */ #ifdef RISCOS -char *inname = "pngtest_pn"; +char *inname = "pngtest_png"; char *outname = "pngout_png"; #else char *inname = "pngtest.png"; @@ -41,7 +42,9 @@ int main(int argc, char *argv[]) png_uint_32 rowbytes; png_uint_32 y; int channels, num_pass, pass; - +#ifdef USE_FAR_KEYWORD + jmp_buf jmpbuf; +#endif row_buf = (png_bytep)NULL; near_row_buf = (png_byte *)NULL; @@ -82,14 +85,18 @@ int main(int argc, char *argv[]) return 1; } - read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (void *)NULL, + read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, (png_error_ptr)NULL, (png_error_ptr)NULL); - write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (void *)NULL, + write_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, (png_error_ptr)NULL, (png_error_ptr)NULL); info_ptr = png_create_info_struct(read_ptr); end_info = png_create_info_struct(read_ptr); +#ifdef USE_FAR_KEYWORD + if (setjmp(jmpbuf)) +#else if (setjmp(read_ptr->jmpbuf)) +#endif { fprintf(STDERR, "libpng read error\n"); png_destroy_read_struct(&read_ptr, &info_ptr, &end_info); @@ -98,8 +105,12 @@ int main(int argc, char *argv[]) fclose(fpout); return 1; } - +#ifdef USE_FAR_KEYWORD + png_memcpy(read_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); + if (setjmp(jmpbuf)) +#else if (setjmp(write_ptr->jmpbuf)) +#endif { fprintf(STDERR, "libpng write error\n"); png_destroy_read_struct(&read_ptr, &info_ptr, &end_info); @@ -108,7 +119,9 @@ int main(int argc, char *argv[]) fclose(fpout); return 1; } - +#ifdef USE_FAR_KEYWORD + png_memcpy(write_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); +#endif png_init_io(read_ptr, fpin); png_init_io(write_ptr, fpout); @@ -166,7 +179,7 @@ int main(int argc, char *argv[]) fclose(fpin); fclose(fpout); - free((void *)near_row_buf); + free((png_byte *)near_row_buf); fpin = fopen(inname, "rb"); @@ -183,13 +196,12 @@ int main(int argc, char *argv[]) fclose(fpin); return 1; } - while (1) { int num_in, num_out; - num_in = fread(inbuf, 1, 256, fpin); - num_out = fread(outbuf, 1, 256, fpout); + num_in = fread(inbuf, 1, 1, fpin); + num_out = fread(outbuf, 1, 1, fpout); if (num_in != num_out) { diff --git a/pngtrans.c b/pngtrans.c index 759ccb58..608c2ea0 100644 --- a/pngtrans.c +++ b/pngtrans.c @@ -2,10 +2,10 @@ /* pngtrans.c - transforms the data in a row routines used by both readers and writers - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 */ #define PNG_INTERNAL @@ -68,10 +68,10 @@ png_set_interlace_handling(png_structp png_ptr) #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) void -png_set_filler(png_structp png_ptr, int filler, int filler_loc) +png_set_filler(png_structp png_ptr, png_byte filler, int filler_loc) { png_ptr->transformations |= PNG_FILLER; - png_ptr->filler = (png_byte)filler; + png_ptr->filler = filler; if (filler_loc == PNG_FILLER_AFTER) png_ptr->flags |= PNG_FLAG_FILLER_AFTER; else @@ -82,17 +82,20 @@ png_set_filler(png_structp png_ptr, int filler, int filler_loc) png_ptr->usr_channels = 4; } -/* old functions kept around for compatability purposes */ +/* Old functions kept around for compatability purposes. They will be + * removed at some time in the future, so don't use them. You should + * use png_set_filler() above instead. We set filler bytes to 0xff in + * case they are mistakenly used as PNG alpha (0xff is fully opaque). */ void png_set_rgbx(png_structp png_ptr) { - png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); + png_set_filler(png_ptr, (png_byte)0xff, PNG_FILLER_AFTER); } void png_set_xrgb(png_structp png_ptr) { - png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE); + png_set_filler(png_ptr, (png_byte)0xff, PNG_FILLER_BEFORE); } #endif diff --git a/pngwio.c b/pngwio.c index 904c3196..9d6b4133 100644 --- a/pngwio.c +++ b/pngwio.c @@ -1,10 +1,10 @@ /* pngwio.c - functions for data output - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 This file provides a location for all output. Users which need special handling are expected to write functions which have the same @@ -57,29 +57,19 @@ png_default_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length) #define NEAR_BUF_SIZE 1024 #define MIN(a,b) (a <= b ? a : b) -#ifdef _MSC_VER -/* for FP_OFF */ -#include -#endif - static void png_default_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length) { png_uint_32 check; png_byte *n_data; + FILE *io_ptr; /* Check if data really is near. If so, use usual code. */ -#ifdef _MSC_VER - /* do it this way just to quiet warning */ - FP_OFF(n_data) = FP_OFF(data); - if (FP_SEG(n_data) == FP_SEG(data)) -#else - /* this works in MSC also but with lost segment warning */ - n_data = (png_byte *)data; + n_data = (png_byte *)CVT_PTR_NOCHECK(data); + io_ptr = (FILE *)CVT_PTR(png_ptr->io_ptr); if ((png_bytep)n_data == data) -#endif { - check = fwrite(n_data, 1, (png_size_t)length, (FILE *)(png_ptr->io_ptr)); + check = fwrite(n_data, 1, (png_size_t)length, io_ptr); } else { @@ -91,7 +81,7 @@ png_default_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length) { written = MIN(NEAR_BUF_SIZE, remaining); png_memcpy(buf, data, written); /* copy far buffer to near buffer */ - err = fwrite(buf, 1, written, (FILE *)(png_ptr->io_ptr)); + err = fwrite(buf, 1, written, io_ptr); if (err != written) break; else @@ -123,8 +113,10 @@ png_flush(png_structp png_ptr) static void png_default_flush(png_structp png_ptr) { - if ((FILE *)(png_ptr->io_ptr)) - fflush((FILE *)(png_ptr->io_ptr)); + FILE *io_ptr; + io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr)); + if (io_ptr) + fflush(io_ptr); } #endif @@ -172,3 +164,30 @@ png_set_write_fn(png_structp png_ptr, png_voidp io_ptr, png_ptr->read_data_fn = NULL; } +#if defined(USE_FAR_KEYWORD) +#if defined(_MSC_VER) +void *far_to_near(png_structp png_ptr,png_voidp ptr, int check) +{ + void *near_ptr; + void FAR *far_ptr; + FP_OFF(near_ptr) = FP_OFF(ptr); + far_ptr = (void FAR *)near_ptr; + if(check != 0) + if(FP_SEG(ptr) != FP_SEG(far_ptr)) + png_error(png_ptr,"segment lost in conversion"); + return(near_ptr); +} +# else +void *far_to_near(png_structp png_ptr,png_voidp ptr, int check) +{ + void *near_ptr; + void FAR *far_ptr; + near_ptr = (void FAR *)ptr; + far_ptr = (void FAR *)near_ptr; + if(check != 0) + if(far_ptr != ptr) + png_error(png_ptr,"segment lost in conversion"); + return(near_ptr); +} +# endif +# endif diff --git a/pngwrite.c b/pngwrite.c index 338c369c..e4553680 100644 --- a/pngwrite.c +++ b/pngwrite.c @@ -1,10 +1,10 @@ /* pngwrite.c - general routines to write a png file - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 */ /* get internal access to png.h */ @@ -16,87 +16,87 @@ write it, and put it in the correct location here. If you want the chunk written after the image data, put it in png_write_end(). I strongly encurage you to supply a PNG_INFO_ flag, and check - info->valid before writing the chunk, as that will keep the code + info_ptr->valid before writing the chunk, as that will keep the code from breaking if you want to just write a plain png file. If you have long comments, I suggest writing them in png_write_end(), and compressing them. */ void -png_write_info(png_structp png_ptr, png_infop info) +png_write_info(png_structp png_ptr, png_infop info_ptr) { png_write_sig(png_ptr); /* write PNG signature */ /* write IHDR information. */ - png_write_IHDR(png_ptr, info->width, info->height, info->bit_depth, - info->color_type, info->compression_type, info->filter_type, - info->interlace_type); + png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height, + info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type, + info_ptr->filter_type, info_ptr->interlace_type); /* the rest of these check to see if the valid field has the appropriate flag set, and if it does, writes the chunk. */ #if defined(PNG_WRITE_gAMA_SUPPORTED) - if (info->valid & PNG_INFO_gAMA) - png_write_gAMA(png_ptr, info->gamma); + if (info_ptr->valid & PNG_INFO_gAMA) + png_write_gAMA(png_ptr, info_ptr->gamma); #endif #if defined(PNG_WRITE_sBIT_SUPPORTED) - if (info->valid & PNG_INFO_sBIT) - png_write_sBIT(png_ptr, &(info->sig_bit), info->color_type); + if (info_ptr->valid & PNG_INFO_sBIT) + png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type); #endif #if defined(PNG_WRITE_cHRM_SUPPORTED) - if (info->valid & PNG_INFO_cHRM) + if (info_ptr->valid & PNG_INFO_cHRM) png_write_cHRM(png_ptr, - info->x_white, info->y_white, - info->x_red, info->y_red, - info->x_green, info->y_green, - info->x_blue, info->y_blue); + info_ptr->x_white, info_ptr->y_white, + info_ptr->x_red, info_ptr->y_red, + info_ptr->x_green, info_ptr->y_green, + info_ptr->x_blue, info_ptr->y_blue); #endif - if (info->valid & PNG_INFO_PLTE) - png_write_PLTE(png_ptr, info->palette, info->num_palette); - else if (info->color_type == PNG_COLOR_TYPE_PALETTE) + if (info_ptr->valid & PNG_INFO_PLTE) + png_write_PLTE(png_ptr, info_ptr->palette, info_ptr->num_palette); + else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) png_error(png_ptr, "Valid palette required for paletted images\n"); #if defined(PNG_WRITE_tRNS_SUPPORTED) - if (info->valid & PNG_INFO_tRNS) - png_write_tRNS(png_ptr, info->trans, &(info->trans_values), - info->num_trans, info->color_type); + if (info_ptr->valid & PNG_INFO_tRNS) + png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values), + info_ptr->num_trans, info_ptr->color_type); #endif #if defined(PNG_WRITE_bKGD_SUPPORTED) - if (info->valid & PNG_INFO_bKGD) - png_write_bKGD(png_ptr, &(info->background), info->color_type); + if (info_ptr->valid & PNG_INFO_bKGD) + png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); #endif #if defined(PNG_WRITE_hIST_SUPPORTED) - if (info->valid & PNG_INFO_hIST) - png_write_hIST(png_ptr, info->hist, info->num_palette); + if (info_ptr->valid & PNG_INFO_hIST) + png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); #endif #if defined(PNG_WRITE_pHYs_SUPPORTED) - if (info->valid & PNG_INFO_pHYs) - png_write_pHYs(png_ptr, info->x_pixels_per_unit, - info->y_pixels_per_unit, info->phys_unit_type); + if (info_ptr->valid & PNG_INFO_pHYs) + png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit, + info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type); #endif #if defined(PNG_WRITE_oFFs_SUPPORTED) - if (info->valid & PNG_INFO_oFFs) - png_write_oFFs(png_ptr, info->x_offset, info->y_offset, - info->offset_unit_type); + if (info_ptr->valid & PNG_INFO_oFFs) + png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset, + info_ptr->offset_unit_type); #endif #if defined(PNG_WRITE_tIME_SUPPORTED) - if (info->valid & PNG_INFO_tIME) + if (info_ptr->valid & PNG_INFO_tIME) { - png_write_tIME(png_ptr, &(info->mod_time)); + png_write_tIME(png_ptr, &(info_ptr->mod_time)); png_ptr->flags |= PNG_FLAG_WROTE_tIME; } #endif #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) /* Check to see if we need to write text chunks */ - if (info->num_text) + if (info_ptr->num_text) { int i; /* local counter */ /* loop through the text chunks */ - for (i = 0; i < info->num_text; i++) + for (i = 0; i < info_ptr->num_text; i++) { /* if chunk is compressed */ - if (info->text[i].compression >= 0) + if (info_ptr->text[i].compression >= 0) { #if defined(PNG_WRITE_zTXt_SUPPORTED) /* write compressed chunk */ - png_write_zTXt(png_ptr, info->text[i].key, - info->text[i].text, info->text[i].text_length, - info->text[i].compression); + png_write_zTXt(png_ptr, info_ptr->text[i].key, + info_ptr->text[i].text, info_ptr->text[i].text_length, + info_ptr->text[i].compression); #else png_warning(png_ptr, "Unable to write compressed text\n"); #endif @@ -105,8 +105,8 @@ png_write_info(png_structp png_ptr, png_infop info) { #if defined(PNG_WRITE_tEXt_SUPPORTED) /* write uncompressed chunk */ - png_write_tEXt(png_ptr, info->text[i].key, - info->text[i].text, info->text[i].text_length); + png_write_tEXt(png_ptr, info_ptr->text[i].key, + info_ptr->text[i].text, info_ptr->text[i].text_length); #else png_warning(png_ptr, "Unable to write uncompressed text\n"); #endif @@ -121,37 +121,37 @@ png_write_info(png_structp png_ptr, png_infop info) in png_write_info(), do not write them again here. If you have long comments, I suggest writing them here, and compressing them. */ void -png_write_end(png_structp png_ptr, png_infop info) +png_write_end(png_structp png_ptr, png_infop info_ptr) { if (!(png_ptr->mode & PNG_HAVE_IDAT)) png_error(png_ptr, "No IDATs written into file"); /* see if user wants us to write information chunks */ - if (info) + if (info_ptr) { #if defined(PNG_WRITE_tIME_SUPPORTED) /* check to see if user has supplied a time chunk */ - if (info->valid & PNG_INFO_tIME && + if (info_ptr->valid & PNG_INFO_tIME && !(png_ptr->flags & PNG_FLAG_WROTE_tIME)) - png_write_tIME(png_ptr, &(info->mod_time)); + png_write_tIME(png_ptr, &(info_ptr->mod_time)); #endif #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) /* check to see if we need to write comment chunks */ - if (info->num_text) + if (info_ptr->num_text) { int i; /* local index variable */ /* loop through comment chunks */ - for (i = 0; i < info->num_text; i++) + for (i = 0; i < info_ptr->num_text; i++) { #if defined(PNG_WRITE_zTXt_SUPPORTED) /* check to see if comment is to be compressed */ - if (info->text[i].compression >= 0) + if (info_ptr->text[i].compression >= 0) { /* write compressed chunk */ - png_write_zTXt(png_ptr, info->text[i].key, - info->text[i].text, info->text[i].text_length, - info->text[i].compression); + png_write_zTXt(png_ptr, info_ptr->text[i].key, + info_ptr->text[i].text, info_ptr->text[i].text_length, + info_ptr->text[i].compression); } #if defined(PNG_WRITE_tEXt_SUPPORTED) else @@ -160,8 +160,8 @@ png_write_end(png_structp png_ptr, png_infop info) #if defined(PNG_WRITE_tEXt_SUPPORTED) { /* write uncompressed chunk */ - png_write_tEXt(png_ptr, info->text[i].key, - info->text[i].text, info->text[i].text_length); + png_write_tEXt(png_ptr, info_ptr->text[i].key, + info_ptr->text[i].text, info_ptr->text[i].text_length); } #endif } @@ -200,26 +200,32 @@ png_convert_from_time_t(png_timep ptime, time_t ttime) /* initialize png structure, and allocate any memory needed */ png_structp png_create_write_struct(png_const_charp user_png_ver, voidp error_ptr, - png_error_ptr warn_fn, png_error_ptr error_fn) + png_error_ptr error_fn, png_error_ptr warn_fn) { png_structp png_ptr; - +#ifdef USE_FAR_KEYWORD + jmp_buf jmpbuf; +#endif if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL) { return (png_structp)NULL; } - +#ifdef USE_FAR_KEYWORD + if (setjmp(jmpbuf)) +#else if (setjmp(png_ptr->jmpbuf)) +#endif { - png_large_free(png_ptr, png_ptr->zbuf); - png_free(png_ptr, png_ptr->zstream); + png_free(png_ptr, png_ptr->zbuf); png_destroy_struct(png_ptr); return (png_structp)NULL; } +#ifdef USE_FAR_KEYWORD + png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf)); +#endif + png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); - png_set_error_fn(png_ptr, error_ptr, warn_fn, error_fn); - - if (user_png_ver == NULL || strcmp(user_png_ver, png_libpng_ver)) + if (user_png_ver == NULL || png_strcmp(user_png_ver, png_libpng_ver)) { if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0]) { @@ -233,12 +239,10 @@ png_create_write_struct(png_const_charp user_png_ver, voidp error_ptr, /* initialize zbuf - compression buffer */ png_ptr->zbuf_size = PNG_ZBUF_SIZE; - png_ptr->zbuf = png_large_malloc(png_ptr, png_ptr->zbuf_size); + png_ptr->zbuf = png_malloc(png_ptr, png_ptr->zbuf_size); png_set_write_fn(png_ptr, NULL, NULL, NULL); - png_ptr->do_free |= PNG_FREE_STRUCT; - return (png_ptr); } @@ -260,7 +264,7 @@ png_write_init(png_structp png_ptr) /* initialize zbuf - compression buffer */ png_ptr->zbuf_size = PNG_ZBUF_SIZE; - png_ptr->zbuf = png_large_malloc(png_ptr, png_ptr->zbuf_size); + png_ptr->zbuf = png_malloc(png_ptr, png_ptr->zbuf_size); png_set_write_fn(png_ptr, NULL, NULL, NULL); } @@ -434,37 +438,37 @@ png_write_flush(png_structp png_ptr) int ret; /* compress the data */ - ret = deflate(png_ptr->zstream, Z_SYNC_FLUSH); + ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH); wrote_IDAT = 0; /* check for compression errors */ if (ret != Z_OK) { - if (png_ptr->zstream->msg) - png_error(png_ptr, png_ptr->zstream->msg); + if (png_ptr->zstream.msg) + png_error(png_ptr, png_ptr->zstream.msg); else png_error(png_ptr, "zlib error"); } - if (!png_ptr->zstream->avail_out) + if (!png_ptr->zstream.avail_out) { /* write the IDAT and reset the zlib output buffer */ png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; wrote_IDAT = 1; } } while(wrote_IDAT == 1); /* If there is any data left to be output, write it into a new IDAT */ - if (png_ptr->zbuf_size != png_ptr->zstream->avail_out) + if (png_ptr->zbuf_size != png_ptr->zstream.avail_out) { /* write the IDAT and reset the zlib output buffer */ png_write_IDAT(png_ptr, png_ptr->zbuf, - png_ptr->zbuf_size - png_ptr->zstream->avail_out); - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zbuf_size - png_ptr->zstream.avail_out); + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; } png_ptr->flush_rows = 0; png_flush(png_ptr); @@ -473,24 +477,33 @@ png_write_flush(png_structp png_ptr) /* free all memory used by the write */ void -png_destroy_write_struct(png_structpp png_ptr, png_infopp info_ptr) +png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) { - if (info_ptr && *info_ptr) + png_structp png_ptr = NULL; + png_infop info_ptr = NULL; + + if (png_ptr_ptr) + png_ptr = *png_ptr_ptr; + + if (info_ptr_ptr) + info_ptr = *info_ptr_ptr; + + if (info_ptr) { - png_destroy_struct((voidp)*info_ptr); - *info_ptr = (png_infop)NULL; + png_destroy_struct((png_voidp)info_ptr); + *info_ptr_ptr = (png_infop)NULL; } - if (png_ptr && *png_ptr) + if (png_ptr) { - png_write_destroy(*png_ptr); - png_destroy_struct((voidp)*png_ptr); - *png_ptr = (png_structp)NULL; + png_write_destroy(png_ptr); + png_destroy_struct((png_voidp)png_ptr); + *png_ptr_ptr = (png_structp)NULL; } } -/* free any memory used in png struct (old) */ +/* free any memory used in png struct (old method) */ void png_write_destroy(png_structp png_ptr) { @@ -500,17 +513,16 @@ png_write_destroy(png_structp png_ptr) png_voidp error_ptr; /* free any memory zlib uses */ - deflateEnd(png_ptr->zstream); - png_free(png_ptr, png_ptr->zstream); + deflateEnd(&png_ptr->zstream); /* free our memory. png_free checks NULL for us. */ - png_large_free(png_ptr, png_ptr->zbuf); - png_large_free(png_ptr, png_ptr->row_buf); - png_large_free(png_ptr, png_ptr->prev_row); - png_large_free(png_ptr, png_ptr->sub_row); - png_large_free(png_ptr, png_ptr->up_row); - png_large_free(png_ptr, png_ptr->avg_row); - png_large_free(png_ptr, png_ptr->paeth_row); + png_free(png_ptr, png_ptr->zbuf); + png_free(png_ptr, png_ptr->row_buf); + png_free(png_ptr, png_ptr->prev_row); + png_free(png_ptr, png_ptr->sub_row); + png_free(png_ptr, png_ptr->up_row); + png_free(png_ptr, png_ptr->avg_row); + png_free(png_ptr, png_ptr->paeth_row); /* reset structure */ png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf)); @@ -555,7 +567,7 @@ png_set_filter(png_structp png_ptr, int method, int filters) { if (png_ptr->do_filter & PNG_FILTER_SUB && !(png_ptr->sub_row)) { - png_ptr->sub_row = (png_bytep )png_large_malloc(png_ptr, + png_ptr->sub_row = (png_bytep )png_malloc(png_ptr, png_ptr->rowbytes + 1); png_ptr->sub_row[0] = 1; /* Set the row filter type */ } @@ -569,7 +581,7 @@ png_set_filter(png_structp png_ptr, int method, int filters) } else { - png_ptr->up_row = (png_bytep )png_large_malloc(png_ptr, + png_ptr->up_row = (png_bytep )png_malloc(png_ptr, png_ptr->rowbytes + 1); png_ptr->up_row[0] = 2; /* Set the row filter type */ } @@ -584,7 +596,7 @@ png_set_filter(png_structp png_ptr, int method, int filters) } else { - png_ptr->up_row = (png_bytep )png_large_malloc(png_ptr, + png_ptr->up_row = (png_bytep )png_malloc(png_ptr, png_ptr->rowbytes + 1); png_ptr->up_row[0] = 3; /* Set the row filter type */ } @@ -599,7 +611,7 @@ png_set_filter(png_structp png_ptr, int method, int filters) } else { - png_ptr->paeth_row = (png_bytep )png_large_malloc(png_ptr, + png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr, png_ptr->rowbytes + 1); png_ptr->paeth_row[0] = 4; /* Set the row filter type */ } diff --git a/pngwtran.c b/pngwtran.c index cea52a29..b31d24cf 100644 --- a/pngwtran.c +++ b/pngwtran.c @@ -1,10 +1,10 @@ /* pngwtran.c - transforms the data in a row for png writers - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + January 10, 1997 */ #define PNG_INTERNAL @@ -299,7 +299,7 @@ png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth) /* remove filler byte */ void png_do_write_filler(png_row_infop row_info, png_bytep row, - png_byte flags) + png_uint_32 flags) { if (row && row_info && row_info->color_type == PNG_COLOR_TYPE_RGB && row_info->bit_depth == 8) diff --git a/pngwutil.c b/pngwutil.c index c9821bc8..4adc1c9b 100644 --- a/pngwutil.c +++ b/pngwutil.c @@ -1,10 +1,10 @@ /* pngwutil.c - utilities to write a png file - libpng 1.0 beta 3 - version 0.89 + libpng 1.0 beta 4 - version 0.90 For conditions of distribution and use, see copyright notice in png.h Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. - May 25, 1996 + December 3, 1996 */ #define PNG_INTERNAL #include "png.h" @@ -62,40 +62,28 @@ png_write_uint_16(png_structp png_ptr, png_uint_16 i) png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() functions instead. */ void -png_write_chunk(png_structp png_ptr, png_bytep type, +png_write_chunk(png_structp png_ptr, png_bytep chunk_name, png_bytep data, png_uint_32 length) { - /* write length */ - png_write_uint_32(png_ptr, length); - /* write chunk name */ - png_write_data(png_ptr, type, (png_uint_32)4); - /* reset the crc and run the chunk name over it */ - png_reset_crc(png_ptr); - png_calculate_crc(png_ptr, type, (png_uint_32)4); - /* write the data and update the crc */ - if (length) - { - png_calculate_crc(png_ptr, data, length); - png_write_data(png_ptr, data, length); - } - /* write the crc */ - png_write_uint_32(png_ptr, ~png_ptr->crc); + png_write_chunk_start(png_ptr, chunk_name, length); + png_write_chunk_data(png_ptr, data, length); + png_write_chunk_end(png_ptr); } /* Write the start of a png chunk. The type is the chunk type. The total_length is the sum of the lengths of all the data you will be passing in png_write_chunk_data() */ void -png_write_chunk_start(png_structp png_ptr, png_bytep type, - png_uint_32 total_length) +png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name, + png_uint_32 length) { /* write the length */ - png_write_uint_32(png_ptr, total_length); + png_write_uint_32(png_ptr, length); /* write the chunk name */ - png_write_data(png_ptr, type, (png_uint_32)4); + png_write_data(png_ptr, chunk_name, (png_uint_32)4); /* reset the crc and run it over the chunk name */ png_reset_crc(png_ptr); - png_calculate_crc(png_ptr, type, (png_uint_32)4); + png_calculate_crc(png_ptr, chunk_name, (png_uint_32)4); } /* write the data of a png chunk started with png_write_chunk_start(). @@ -118,15 +106,24 @@ void png_write_chunk_end(png_structp png_ptr) { /* write the crc */ +#ifdef PNG_USE_OWN_CRC png_write_uint_32(png_ptr, ~png_ptr->crc); +#else + png_write_uint_32(png_ptr, png_ptr->crc); +#endif } -/* simple function to write the signature */ +/* Simple function to write the signature. If we have already written + * the magic bytes of the signature, or more likely, the PNG stream is + * being embedded into another stream and doesn't need its own signature, + * we should call png_set_sig_bytes() to tell libpng how many of the + * bytes have already been written. */ void png_write_sig(png_structp png_ptr) { - /* write the 8 byte signature */ - png_write_data(png_ptr, png_sig, (png_uint_32)8); + /* write the rest of the 8 byte signature */ + png_write_data(png_ptr, &png_sig[png_ptr->sig_bytes], + (png_uint_32)8 - png_ptr->sig_bytes); } /* Write the IHDR chunk, and update the png_struct with the necessary @@ -227,10 +224,9 @@ png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, png_write_chunk(png_ptr, png_IHDR, buf, (png_uint_32)13); /* initialize zlib with png info */ - png_ptr->zstream = (z_stream *)png_malloc(png_ptr, sizeof (z_stream)); - png_ptr->zstream->zalloc = png_zalloc; - png_ptr->zstream->zfree = png_zfree; - png_ptr->zstream->opaque = (voidpf)png_ptr; + png_ptr->zstream.zalloc = png_zalloc; + png_ptr->zstream.zfree = png_zfree; + png_ptr->zstream.opaque = (voidpf)png_ptr; if (!(png_ptr->do_filter)) { if (png_ptr->color_type == 3 || png_ptr->bit_depth < 8) @@ -253,13 +249,13 @@ png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, png_ptr->zlib_window_bits = 15; if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD)) png_ptr->zlib_method = 8; - deflateInit2(png_ptr->zstream, png_ptr->zlib_level, + deflateInit2(&png_ptr->zstream, png_ptr->zlib_level, png_ptr->zlib_method, png_ptr->zlib_window_bits, png_ptr->zlib_mem_level, png_ptr->zlib_strategy); - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_ptr->mode = PNG_HAVE_IHDR; } @@ -268,7 +264,7 @@ png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height, correct order for PNG, so people can redefine it to any convient structure. */ void -png_write_PLTE(png_structp png_ptr, png_colorp palette, int number) +png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 number) { int i; png_colorp pal_ptr; @@ -290,9 +286,7 @@ png_write_PLTE(png_structp png_ptr, png_colorp palette, int number) png_ptr->num_palette = number; png_write_chunk_start(png_ptr, png_PLTE, number * 3); - for (i = 0, pal_ptr = palette; - i < number; - i++, pal_ptr++) + for (i = 0, pal_ptr = palette; i < number; i++, pal_ptr++) { buf[0] = pal_ptr->red; buf[1] = pal_ptr->green; @@ -316,7 +310,7 @@ void png_write_IEND(png_structp png_ptr) { png_write_chunk(png_ptr, png_IEND, NULL, (png_uint_32)0); - png_ptr->mode |= PNG_AFTER_IEND; + png_ptr->mode |= PNG_HAVE_IEND; } #if defined(PNG_WRITE_gAMA_SUPPORTED) @@ -539,6 +533,102 @@ png_write_hIST(png_structp png_ptr, png_uint_16p hist, int number) } #endif +#if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED) +/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, + * and if invalid, correct the keyword rather than discarding the entire + * chunk. The PNG 1.0 specification requires keywords 1-79 characters in + * length, forbids leading or trailing whitespace, multiple internal spaces, + * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. + */ +#undef BROKEN_CHECK +int +png_check_keyword(png_structp png_ptr, png_charpp key) +{ + png_charp kp; + int key_len; +#ifdef BROKEN_CHECK + png_charp np; + int kflag; + char newkey[80]; +#endif + + if (*key == NULL || (key_len = png_strlen(*key)) == 0) + { + char msg[40]; + + sprintf(msg, "Zero length %s keyword", png_ptr->chunk_name); + png_warning(png_ptr, msg); + return 0; + } + else if (key_len > 79) + { + char msg[40]; + + sprintf(msg, "%s keyword length must be 1 - 79 characters", + png_ptr->chunk_name); + png_warning(png_ptr, msg); + (*key)[79] = '\0'; + key_len = 79; + } + + /* Replace non-printing characters with a blank and print a warning */ + for (kp = *key; *kp != '\0'; kp++) + { + if (*kp < 0x20 || ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1)) + { + char msg[40]; + + sprintf(msg, "Invalid %s keyword character 0x%02X", + png_ptr->chunk_name, *kp); + png_warning(png_ptr, msg); + *kp = ' '; + } + } + + /* Remove any leading white space. */ + while (**key == ' ') + { + (*key)++; + key_len--; + } + + /* Remove any trailing white space. */ + for (kp = *key + key_len - 1; *kp == ' '; key_len--) + *kp-- = '\0'; + +#ifdef BROKEN_CHECK + /* Remove multiple internal spaces. This is currently broken for + * an unknown reason, and I didn't want to hold up the release of + * libpng-0.90 even longer to fix it. */ + kflag = 0; + for (kp = *key, np = newkey; *kp; kp++) + { + if (*kp != ' ') + { + *np++ = *kp; + kflag = 0; + } + else if (kflag) + { + key_len--; + } + else + { + *np++ = *kp; + kflag = 1; + } + } + *np = *kp; + png_strcpy(kp, np); +#endif + + if (key_len == 0) + png_warning(png_ptr, "Zero length tEXt keyword"); + + return key_len; +} +#endif + #if defined(PNG_WRITE_tEXt_SUPPORTED) /* write a tEXt chunk */ void @@ -547,27 +637,19 @@ png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text, { int key_len; - key_len = png_strlen(key); - - if (key_len == 0) - { - png_warning(png_ptr, "Invalid text keyword length"); + if (key == NULL || (key_len = png_check_keyword(png_ptr, &key)) == 0) return; - } - else if (key_len > 80) - { - png_warning(png_ptr, "Text keyword length restricted to 80 characters\n"); - key[80] = '\0'; - key_len = 80; - } - /* make sure we count the 0 after the key */ + if (text == NULL) + text_len = 0; + + /* make sure we include the 0 after the key */ png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)(key_len + text_len + 1)); - /* key has an 0 at the end. How nice */ png_write_chunk_data(png_ptr, (png_bytep )key, (png_uint_32)(key_len + 1)); - if (text && text_len) + if (text_len) png_write_chunk_data(png_ptr, (png_bytep )text, (png_uint_32)text_len); + png_write_chunk_end(png_ptr); } #endif @@ -585,19 +667,11 @@ png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text, int num_output_ptr = 0; /* number of output pointers used */ int max_output_ptr = 0; /* size of output_ptr */ - key_len = png_strlen(key); - - if (key_len == 0) - { - png_warning(png_ptr, "Invalid text keyword length"); + if (key == NULL || (key_len = png_check_keyword(png_ptr, &key)) == 0) return; - } - else if (key_len > 80) - { - png_warning(png_ptr, "Text keyword length restricted to 80 characters\n"); - key[80] = '\0'; - key_len = 80; - } + + if (text == NULL) + text_len = 0; if (compression != 0) { @@ -612,26 +686,26 @@ png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text, malloced pointers to be sure. */ /* set up the compression buffers */ - png_ptr->zstream->avail_in = (uInt)text_len; - png_ptr->zstream->next_in = (Bytef *)text; - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; - png_ptr->zstream->next_out = (Bytef *)png_ptr->zbuf; + png_ptr->zstream.avail_in = (uInt)text_len; + png_ptr->zstream.next_in = (Bytef *)text; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf; /* this is the same compression loop as in png_write_row() */ do { /* compress the data */ - ret = deflate(png_ptr->zstream, Z_NO_FLUSH); + ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); if (ret != Z_OK) { /* error */ - if (png_ptr->zstream->msg) - png_error(png_ptr, png_ptr->zstream->msg); + if (png_ptr->zstream.msg) + png_error(png_ptr, png_ptr->zstream.msg); else png_error(png_ptr, "zlib error"); } /* check to see if we need more room */ - if (!png_ptr->zstream->avail_out && png_ptr->zstream->avail_in) + if (!png_ptr->zstream.avail_out && png_ptr->zstream.avail_in) { /* make sure the output array has room */ if (num_output_ptr >= max_output_ptr) @@ -645,47 +719,47 @@ png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text, png_charpp old_ptr; old_ptr = output_ptr; - output_ptr = (png_charpp)png_large_malloc(png_ptr, + output_ptr = (png_charpp)png_malloc(png_ptr, max_output_ptr * sizeof (png_charpp)); png_memcpy(output_ptr, old_ptr, (png_size_t)(old_max * sizeof (png_charp))); - png_large_free(png_ptr, old_ptr); + png_free(png_ptr, old_ptr); } else - output_ptr = (png_charpp)png_large_malloc(png_ptr, + output_ptr = (png_charpp)png_malloc(png_ptr, max_output_ptr * sizeof (png_charp)); } /* save the data */ - output_ptr[num_output_ptr] = png_large_malloc(png_ptr, + output_ptr[num_output_ptr] = png_malloc(png_ptr, png_ptr->zbuf_size); png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf, (png_size_t)png_ptr->zbuf_size); num_output_ptr++; /* and reset the buffer */ - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; - png_ptr->zstream->next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; } /* continue until we don't have anymore to compress */ - } while (png_ptr->zstream->avail_in); + } while (png_ptr->zstream.avail_in); /* finish the compression */ do { /* tell zlib we are finished */ - ret = deflate(png_ptr->zstream, Z_FINISH); + ret = deflate(&png_ptr->zstream, Z_FINISH); if (ret != Z_OK && ret != Z_STREAM_END) { /* we got an error */ - if (png_ptr->zstream->msg) - png_error(png_ptr, png_ptr->zstream->msg); + if (png_ptr->zstream.msg) + png_error(png_ptr, png_ptr->zstream.msg); else png_error(png_ptr, "zlib error"); } /* check to see if we need more room */ - if (!png_ptr->zstream->avail_out && ret == Z_OK) + if (!png_ptr->zstream.avail_out && ret == Z_OK) { /* check to make sure our output array has room */ if (num_output_ptr >= max_output_ptr) @@ -699,35 +773,35 @@ png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text, png_charpp old_ptr; old_ptr = output_ptr; - output_ptr = (png_charpp)png_large_malloc(png_ptr, + output_ptr = (png_charpp)png_malloc(png_ptr, max_output_ptr * sizeof (png_charpp)); png_memcpy(output_ptr, old_ptr, (png_size_t)(old_max * sizeof (png_charp))); - png_large_free(png_ptr, old_ptr); + png_free(png_ptr, old_ptr); } else - output_ptr = (png_charpp)png_large_malloc(png_ptr, + output_ptr = (png_charpp)png_malloc(png_ptr, max_output_ptr * sizeof (png_charp)); } /* save off the data */ - output_ptr[num_output_ptr] = png_large_malloc(png_ptr, + output_ptr[num_output_ptr] = png_malloc(png_ptr, png_ptr->zbuf_size); png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf, (png_size_t)png_ptr->zbuf_size); num_output_ptr++; /* and reset the buffer pointers */ - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; - png_ptr->zstream->next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; } } while (ret != Z_STREAM_END); /* text length is number of buffers plus last buffer */ text_len = png_ptr->zbuf_size * num_output_ptr; - if (png_ptr->zstream->avail_out < png_ptr->zbuf_size) + if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) text_len += (png_uint_32)(png_ptr->zbuf_size - - png_ptr->zstream->avail_out); + png_ptr->zstream.avail_out); /* write start of chunk */ png_write_chunk_start(png_ptr, png_zTXt, @@ -742,19 +816,19 @@ png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text, for (i = 0; i < num_output_ptr; i++) { png_write_chunk_data(png_ptr, (png_bytep )output_ptr[i], png_ptr->zbuf_size); - png_large_free(png_ptr, output_ptr[i]); + png_free(png_ptr, output_ptr[i]); } if (max_output_ptr) - png_large_free(png_ptr, output_ptr); + png_free(png_ptr, output_ptr); /* write anything left in zbuf */ - if (png_ptr->zstream->avail_out < png_ptr->zbuf_size) + if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) png_write_chunk_data(png_ptr, png_ptr->zbuf, - png_ptr->zbuf_size - png_ptr->zstream->avail_out); + png_ptr->zbuf_size - png_ptr->zstream.avail_out); /* close the chunk */ png_write_chunk_end(png_ptr); /* reset zlib for another zTXt or the image data */ - deflateReset(png_ptr->zstream); + deflateReset(&png_ptr->zstream); } #endif @@ -830,7 +904,7 @@ void png_write_start_row(png_structp png_ptr) { /* set up row buffer */ - png_ptr->row_buf = (png_bytep )png_large_malloc(png_ptr, + png_ptr->row_buf = (png_bytep )png_malloc(png_ptr, (((png_uint_32)png_ptr->usr_channels * (png_uint_32)png_ptr->usr_bit_depth * png_ptr->width + 7) >> 3) + 1); @@ -839,7 +913,7 @@ png_write_start_row(png_structp png_ptr) /* set up filtering buffer, if using this filter */ if (png_ptr->do_filter & PNG_FILTER_SUB) { - png_ptr->sub_row = (png_bytep )png_large_malloc(png_ptr, + png_ptr->sub_row = (png_bytep )png_malloc(png_ptr, png_ptr->rowbytes + 1); png_ptr->sub_row[0] = 1; /* Set the row filter type */ } @@ -848,30 +922,32 @@ png_write_start_row(png_structp png_ptr) if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) { /* set up previous row buffer */ - png_ptr->prev_row = (png_bytep )png_large_malloc(png_ptr, + png_ptr->prev_row = (png_bytep )png_malloc(png_ptr, (((png_uint_32)png_ptr->usr_channels * (png_uint_32)png_ptr->usr_bit_depth * png_ptr->width + 7) >> 3) + 1); - png_memset(png_ptr->prev_row, 0, (((png_uint_32)png_ptr->usr_channels * - (png_uint_32)png_ptr->usr_bit_depth * png_ptr->width + 7) >> 3) + 1); + png_memset(png_ptr->prev_row, 0, + (png_size_t)(((png_uint_32)png_ptr->usr_channels * + (png_uint_32)png_ptr->usr_bit_depth * + png_ptr->width + 7) >> 3) + 1); if (png_ptr->do_filter & PNG_FILTER_UP) { - png_ptr->up_row = (png_bytep )png_large_malloc(png_ptr, + png_ptr->up_row = (png_bytep )png_malloc(png_ptr, png_ptr->rowbytes + 1); png_ptr->up_row[0] = 2; /* Set the row filter type */ } if (png_ptr->do_filter & PNG_FILTER_AVG) { - png_ptr->avg_row = (png_bytep )png_large_malloc(png_ptr, + png_ptr->avg_row = (png_bytep )png_malloc(png_ptr, png_ptr->rowbytes + 1); png_ptr->avg_row[0] = 3; /* Set the row filter type */ } if (png_ptr->do_filter & PNG_FILTER_PAETH) { - png_ptr->paeth_row = (png_bytep )png_large_malloc(png_ptr, + png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr, png_ptr->rowbytes + 1); png_ptr->paeth_row[0] = 4; /* Set the row filter type */ } @@ -900,8 +976,8 @@ png_write_start_row(png_structp png_ptr) png_ptr->num_rows = png_ptr->height; png_ptr->usr_width = png_ptr->width; } - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; - png_ptr->zstream->next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; } /* Internal use only. Called when finished processing a row of data */ @@ -951,8 +1027,8 @@ png_write_finish_row(png_structp png_ptr) if (png_ptr->pass < 7) { if (png_ptr->prev_row) - png_memset(png_ptr->prev_row, 0, - (((png_uint_32)png_ptr->usr_channels * + png_memset(png_ptr->prev_row, 0, + (png_size_t) (((png_uint_32)png_ptr->usr_channels * (png_uint_32)png_ptr->usr_bit_depth * png_ptr->width + 7) >> 3) + 1); return; @@ -964,32 +1040,32 @@ png_write_finish_row(png_structp png_ptr) do { /* tell the compressor we are done */ - ret = deflate(png_ptr->zstream, Z_FINISH); + ret = deflate(&png_ptr->zstream, Z_FINISH); /* check for an error */ if (ret != Z_OK && ret != Z_STREAM_END) { - if (png_ptr->zstream->msg) - png_error(png_ptr, png_ptr->zstream->msg); + if (png_ptr->zstream.msg) + png_error(png_ptr, png_ptr->zstream.msg); else png_error(png_ptr, "zlib error"); } /* check to see if we need more room */ - if (!png_ptr->zstream->avail_out && ret == Z_OK) + if (!png_ptr->zstream.avail_out && ret == Z_OK) { png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; } } while (ret != Z_STREAM_END); /* write any extra space */ - if (png_ptr->zstream->avail_out < png_ptr->zbuf_size) + if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) { png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size - - png_ptr->zstream->avail_out); + png_ptr->zstream.avail_out); } - deflateReset(png_ptr->zstream); + deflateReset(&png_ptr->zstream); } #if defined(PNG_WRITE_INTERLACING_SUPPORTED) @@ -1114,8 +1190,7 @@ png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) { png_bytep sp; png_bytep dp; - png_uint_32 i; - int pixel_bytes; + png_uint_32 i, pixel_bytes; /* start at the beginning */ dp = row; @@ -1319,34 +1394,34 @@ void png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row) { /* set up the zlib input buffer */ - png_ptr->zstream->next_in = filtered_row; - png_ptr->zstream->avail_in = (uInt)png_ptr->row_info.rowbytes + 1; + png_ptr->zstream.next_in = filtered_row; + png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1; /* repeat until we have compressed all the data */ do { int ret; /* return of zlib */ /* compress the data */ - ret = deflate(png_ptr->zstream, Z_NO_FLUSH); + ret = deflate(&png_ptr->zstream, Z_NO_FLUSH); /* check for compression errors */ if (ret != Z_OK) { - if (png_ptr->zstream->msg) - png_error(png_ptr, png_ptr->zstream->msg); + if (png_ptr->zstream.msg) + png_error(png_ptr, png_ptr->zstream.msg); else png_error(png_ptr, "zlib error"); } /* see if it is time to write another IDAT */ - if (!png_ptr->zstream->avail_out) + if (!png_ptr->zstream.avail_out) { /* write the IDAT and reset the zlib output buffer */ png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size); - png_ptr->zstream->next_out = png_ptr->zbuf; - png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size; + png_ptr->zstream.next_out = png_ptr->zbuf; + png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; } /* repeat until all data has been compressed */ - } while (png_ptr->zstream->avail_in); + } while (png_ptr->zstream.avail_in); /* swap the current and previous rows */ if (png_ptr->prev_row) diff --git a/readme.txt b/readme.txt deleted file mode 100644 index 9da76ce7..00000000 --- a/readme.txt +++ /dev/null @@ -1,106 +0,0 @@ -readme.txt - for libpng 0.89 - -This is a bug fix for the third beta version of libpng 1.0. The -changes from libpng-0.88 are bug fixes and some changes to the -API itself to increase robustness with shared libraries. This -release is based on libpng-0.88, but has been modified from that -version by Andreas Dilger because the -original author, Guy Schalnat, has not been able to keep up with -the time demands of maintaining this library. - -The callback functions for the error/warning messages have changed -since the last release because their implementation was broken, -and it was thought best to change the API itself (which was only -introduced in libpng-0.88 itself) to alert the user to the change, -rather than mislead the user into thinking their application was -OK after re-compiling. This means that calls to png_set_message_fn() -no longer exist, because the previously suggested method of calling -them before png_read_init() or png_write_init() is now ineffective. - -The preferred method of setting the error and warning callbacks -has been incorporated into the allocation of the png_struct and -info_struct itself, which allow them to be safely used during the -initialization of the structure, as well as to keep the size of -the png_struct internal to the library, rather than at compile time -of the application. This will hopefully remove any problems with -dynamically linked libraries, and should be considered the preferred -method of creating these structures, although the previous -initialization API is still available for compatibility. See libpng.txt -for more information on the new API. - -The changes made to the library, and bugs fixed are based on discussions -on the PNG implementation mailing list -and not on material submitted to Guy. - -For a detailed description on using libpng, read libpng.txt. For -usage information and restrictions (what little they are) on libpng, -see png.h. For a description on using zlib (the compression library -used by libpng) and zlib's restrictions, see zlib.h - -I have included a general makefile, as well as several machine and compiler -specific ones, but you may have to modify one for your own needs. - -You will need zlib 0.95 or later to run this. zlib is a compression -library that is useful for more things then just png files. If -you need a compression library, check out zlib.h. There was a bug in -zlib <= 0.99 which caused it to generate invalid compression streams -on some occasions. Later versions of zlib do not have this problem. - -zlib should be available at the same place that libpng is. -If not, it should be at ftp.uu.net in /graphics/png -Eventually, it will be at ftp.uu.net in /pub/archiving/zip/zlib - -You may also want a copy of the PNG specification. It should -be available at the same place you picked up libpng. If it is -not there, try ftp.uu.net in the /graphics/png directory. - -This code is currently being archived at ftp.uu.net in the -/graphics/png directory, and on CompuServe, Lib 20 (PNG SUPPORT) -at GO GRAPHSUP. If you can't find it in any of those places, -e-mail me, and I'll help you find it. - -If you have any code changes, requests, problems, etc., please e-mail -them to me. Also, I'd appreciate any make files or project files, -and any modifications you needed to make to get libpng to compile, -along with a #define variable to tell what compiler/system you are on. -If you needed to add transformations to libpng, or wish libpng would -provide the image in a different way, drop me a note (and code, if -possible), so I can consider supporting the transformation. -Finally, if you get any warning messages when compiling libpng -(note: not zlib), and they are easy to fix, I'd appreciate the -fix. Please mention "libpng" somewhere in the subject line. Thanks. - -This release was created and will be supported by myself, and the -PNG group. - -adilger@enel.ucalgary.ca -png-implement@dworkin.wustl.edu - -You can reach Guy, the original libpng author, at (internet preferred): - -internet: schalnat@group42.com -CompuServe: 75501,1625 - -Please do not send general questions about PNG. Send them to -the address in the specification. At the same time, please do -not send libpng questions to that address, send them to me. I'll -get them in the end anyway. If you have a question about something -in the PNG specification that is related to using libpng, send it -to me. Send me any questions that start with "I was using libpng, -and ...". If in doubt, send questions to me. I'll bounce them -to others, if necessary. - -Please do not send suggestions on how to change PNG. We have -been discussing PNG for over a year now, and it is official and -finished. If you have suggestions for libpng, however, I'll -gladly listen. Even if your suggestion is not used for version -1.0, it may be used later. - -Good luck, and happy coding. - --Guy Eric Schalnat - Group 42, Inc. - Internet: schalnat@group42.com - CompuServe: 75501,1625 - Web: www.group42.com -