added libimago
[eradicate] / libs / imago / libpng / pngrio.c
diff --git a/libs/imago/libpng/pngrio.c b/libs/imago/libpng/pngrio.c
new file mode 100644 (file)
index 0000000..dffae55
--- /dev/null
@@ -0,0 +1,166 @@
+\r
+/* pngrio.c - functions for data input\r
+ *\r
+ * Last changed in libpng 1.2.30 [August 15, 2008]\r
+ * For conditions of distribution and use, see copyright notice in png.h\r
+ * Copyright (c) 1998-2008 Glenn Randers-Pehrson\r
+ * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)\r
+ * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)\r
+ *\r
+ * This file provides a location for all input.  Users who need\r
+ * special handling are expected to write a function that has the same\r
+ * arguments as this and performs a similar function, but that possibly\r
+ * has a different input method.  Note that you shouldn't change this\r
+ * function, but rather write a replacement function and then make\r
+ * libpng use it at run time with png_set_read_fn(...).\r
+ */\r
+\r
+#define PNG_INTERNAL\r
+#include "png.h"\r
+#if defined(PNG_READ_SUPPORTED)\r
+\r
+/* Read the data from whatever input you are using.  The default routine\r
+   reads from a file pointer.  Note that this routine sometimes gets called\r
+   with very small lengths, so you should implement some kind of simple\r
+   buffering if you are using unbuffered reads.  This should never be asked\r
+   to read more then 64K on a 16 bit machine. */\r
+void /* PRIVATE */\r
+png_read_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_debug1(4, "reading %d bytes\n", (int)length);\r
+   if (png_ptr->read_data_fn != NULL)\r
+      (*(png_ptr->read_data_fn))(png_ptr, data, length);\r
+   else\r
+      png_error(png_ptr, "Call to NULL read function");\r
+}\r
+\r
+#if !defined(PNG_NO_STDIO)\r
+/* This is the function that does the actual reading of data.  If you are\r
+   not reading from a standard C stream, you should create a replacement\r
+   read_data function and use it at run time with png_set_read_fn(), rather\r
+   than changing the library. */\r
+#ifndef USE_FAR_KEYWORD\r
+void PNGAPI\r
+png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   png_size_t check;\r
+\r
+   if (png_ptr == NULL) return;\r
+   /* fread() returns 0 on error, so it is OK to store this in a png_size_t\r
+    * instead of an int, which is what fread() actually returns.\r
+    */\r
+#if defined(_WIN32_WCE)\r
+   if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )\r
+      check = 0;\r
+#else\r
+   check = (png_size_t)fread(data, (png_size_t)1, length,\r
+      (png_FILE_p)png_ptr->io_ptr);\r
+#endif\r
+\r
+   if (check != length)\r
+      png_error(png_ptr, "Read Error");\r
+}\r
+#else\r
+/* this is the model-independent version. Since the standard I/O library\r
+   can't handle far buffers in the medium and small models, we have to copy\r
+   the data.\r
+*/\r
+\r
+#define NEAR_BUF_SIZE 1024\r
+#define MIN(a,b) (a <= b ? a : b)\r
+\r
+static void PNGAPI\r
+png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)\r
+{\r
+   int check;\r
+   png_byte *n_data;\r
+   png_FILE_p io_ptr;\r
+\r
+   if (png_ptr == NULL) return;\r
+   /* Check if data really is near. If so, use usual code. */\r
+   n_data = (png_byte *)CVT_PTR_NOCHECK(data);\r
+   io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr);\r
+   if ((png_bytep)n_data == data)\r
+   {\r
+#if defined(_WIN32_WCE)\r
+      if ( !ReadFile((HANDLE)(png_ptr->io_ptr), data, length, &check, NULL) )\r
+         check = 0;\r
+#else\r
+      check = fread(n_data, 1, length, io_ptr);\r
+#endif\r
+   }\r
+   else\r
+   {\r
+      png_byte buf[NEAR_BUF_SIZE];\r
+      png_size_t read, remaining, err;\r
+      check = 0;\r
+      remaining = length;\r
+      do\r
+      {\r
+         read = MIN(NEAR_BUF_SIZE, remaining);\r
+#if defined(_WIN32_WCE)\r
+         if ( !ReadFile((HANDLE)(io_ptr), buf, read, &err, NULL) )\r
+            err = 0;\r
+#else\r
+         err = fread(buf, (png_size_t)1, read, io_ptr);\r
+#endif\r
+         png_memcpy(data, buf, read); /* copy far buffer to near buffer */\r
+         if (err != read)\r
+            break;\r
+         else\r
+            check += err;\r
+         data += read;\r
+         remaining -= read;\r
+      }\r
+      while (remaining != 0);\r
+   }\r
+   if ((png_uint_32)check != (png_uint_32)length)\r
+      png_error(png_ptr, "read Error");\r
+}\r
+#endif\r
+#endif\r
+\r
+/* This function allows the application to supply a new input function\r
+   for libpng if standard C streams aren't being used.\r
+\r
+   This function takes as its arguments:\r
+   png_ptr      - pointer to a png input data structure\r
+   io_ptr       - pointer to user supplied structure containing info about\r
+                  the input functions.  May be NULL.\r
+   read_data_fn - pointer to a new input function that takes as its\r
+                  arguments a pointer to a png_struct, a pointer to\r
+                  a location where input data can be stored, and a 32-bit\r
+                  unsigned int that is the number of bytes to be read.\r
+                  To exit and output any fatal error messages the new write\r
+                  function should call png_error(png_ptr, "Error msg"). */\r
+void PNGAPI\r
+png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,\r
+   png_rw_ptr read_data_fn)\r
+{\r
+   if (png_ptr == NULL) return;\r
+   png_ptr->io_ptr = io_ptr;\r
+\r
+#if !defined(PNG_NO_STDIO)\r
+   if (read_data_fn != NULL)\r
+      png_ptr->read_data_fn = read_data_fn;\r
+   else\r
+      png_ptr->read_data_fn = png_default_read_data;\r
+#else\r
+   png_ptr->read_data_fn = read_data_fn;\r
+#endif\r
+\r
+   /* It is an error to write to a read device */\r
+   if (png_ptr->write_data_fn != NULL)\r
+   {\r
+      png_ptr->write_data_fn = NULL;\r
+      png_warning(png_ptr,\r
+         "It's an error to set both read_data_fn and write_data_fn in the ");\r
+      png_warning(png_ptr,\r
+         "same structure.  Resetting write_data_fn to NULL.");\r
+   }\r
+\r
+#if defined(PNG_WRITE_FLUSH_SUPPORTED)\r
+   png_ptr->output_flush_fn = NULL;\r
+#endif\r
+}\r
+#endif /* PNG_READ_SUPPORTED */\r