[master] Fixed atomicity of chunk header serialization (Cosmin)

and added test for io_state in pngtest.c (Cosmin)
diff --git a/ANNOUNCE b/ANNOUNCE
index 0fd3105..7580737 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -49,6 +49,10 @@
 version 1.4.5rc02 [November 20, 2010]
   Removed some extraneous parentheses that appeared in pngrutil.c of
     libpng-1.4.3bet01
+  Revised png_get_uint_32, png_get_int_32, png_get_uint_16 (Cosmin)
+  Moved reading of file signature into png_read_sig (Cosmin)
+  Fixed atomicity of chunk header serialization (Cosmin)
+  Added test for io_state in pngtest.c (Cosmin)
 
 Send comments/corrections/commendations to glennrp at users.sourceforge.net
 or to png-mng-implement at lists.sf.net (subscription required; visit
diff --git a/CHANGES b/CHANGES
index 4691289..525068c 100644
--- a/CHANGES
+++ b/CHANGES
@@ -2702,9 +2702,11 @@
 
 version 1.4.5rc02 [November 20, 2010]
   Removed some extraneous parentheses that appeared in pngrutil.c of
-    libpng-1.4.3bet01 (Cosmin)
+    libpng-1.4.3bet01
   Revised png_get_uint_32, png_get_int_32, png_get_uint_16 (Cosmin)
   Moved reading of file signature into png_read_sig (Cosmin)
+  Fixed atomicity of chunk header serialization (Cosmin)
+  Added test for io_state in pngtest.c (Cosmin)
 
 */ }
 #endif
diff --git a/png.h b/png.h
index 973b87d..cea2f5f 100644
--- a/png.h
+++ b/png.h
@@ -2657,6 +2657,7 @@
       ? -((png_int_32)((png_get_uint_32(buf) ^ 0xffffffffL) + 1)) \
       : (png_int_32)png_get_uint_32(buf)))
 #else
+PNG_EXPORT(png_uint_32,png_get_uint_32) PNGARG((png_bytep buf));
 PNG_EXPORT(png_uint_16,png_get_uint_16) PNGARG((png_bytep buf));
 #ifdef PNG_GET_INT_32_SUPPORTED
 PNG_EXPORT(png_int_32,png_get_int_32) PNGARG((png_bytep buf));
diff --git a/pngtest.c b/pngtest.c
index d6691bf..4f01d90 100644
--- a/pngtest.c
+++ b/pngtest.c
@@ -1,7 +1,7 @@
 
 /* pngtest.c - a simple test program to test libpng
  *
- * Last changed in libpng 1.4.1 [February 25, 2010]
+ * Last changed in libpng 1.4.5 [$RDATE]
  * Copyright (c) 1998-2010 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -263,6 +263,48 @@
  * than changing the library.
  */
 
+#ifdef PNG_IO_STATE_SUPPORTED
+void
+pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
+   png_uint_32 io_op);
+void
+pngtest_check_io_state(png_structp png_ptr, png_size_t data_length,
+   png_uint_32 io_op)
+{
+   png_uint_32 io_state = png_get_io_state(png_ptr);
+   int err = 0;
+
+   /* Check if the current operation (reading / writing) is as expected. */
+   if ((io_state & PNG_IO_MASK_OP) != io_op)
+      png_error(png_ptr, "Incorrect operation in I/O state");
+
+   /* Check if the buffer size specific to the current location
+    * (file signature / header / data / crc) is as expected.
+    */
+   switch (io_state & PNG_IO_MASK_LOC)
+   {
+   case PNG_IO_SIGNATURE:
+      if (data_length > 8)
+         err = 1;
+      break;
+   case PNG_IO_CHUNK_HDR:
+      if (data_length != 8)
+         err = 1;
+      break;
+   case PNG_IO_CHUNK_DATA:
+      break;  /* no restrictions here */
+   case PNG_IO_CHUNK_CRC:
+      if (data_length != 4)
+         err = 1;
+      break;
+   default:
+      err = 1;  /* uninitialized */
+   }
+   if (err)
+      png_error(png_ptr, "Bad I/O state or buffer size");
+}
+#endif
+
 #ifndef USE_FAR_KEYWORD
 static void
 pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
@@ -281,8 +323,12 @@
 
    if (check != length)
    {
-      png_error(png_ptr, "Read Error!");
+      png_error(png_ptr, "Read Error");
    }
+
+#ifdef PNG_IO_STATE_SUPPORTED
+   pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
+#endif
 }
 #else
 /* This is the model-independent version. Since the standard I/O library
@@ -328,7 +374,11 @@
       while (remaining != 0);
    }
    if (check != length)
-      png_error(png_ptr, "read Error");
+      png_error(png_ptr, "Read Error");
+
+#ifdef PNG_IO_STATE_SUPPORTED
+   pngtest_check_io_state(png_ptr, length, PNG_IO_READING);
+#endif
 }
 #endif /* USE_FAR_KEYWORD */
 
@@ -359,6 +409,10 @@
    {
       png_error(png_ptr, "Write Error");
    }
+
+#ifdef PNG_IO_STATE_SUPPORTED
+   pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
+#endif
 }
 #else
 /* This is the model-independent version. Since the standard I/O library
@@ -407,6 +461,10 @@
    {
       png_error(png_ptr, "Write Error");
    }
+
+#ifdef PNG_IO_STATE_SUPPORTED
+   pngtest_check_io_state(png_ptr, length, PNG_IO_WRITING);
+#endif
 }
 #endif /* USE_FAR_KEYWORD */
 
@@ -1629,4 +1687,4 @@
 }
 
 /* Generate a compiler error if there is an old png.h in the search path. */
-typedef version_1_4_5rc01 your_png_h_is_not_version_1_4_5rc01;
+typedef version_1_4_5rc02 your_png_h_is_not_version_1_4_5rc02;