about summary refs log tree commit diff
path: root/converter/other/cameratopam/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/other/cameratopam/util.c')
-rw-r--r--converter/other/cameratopam/util.c34
1 files changed, 24 insertions, 10 deletions
diff --git a/converter/other/cameratopam/util.c b/converter/other/cameratopam/util.c
index b3ccf7e9..ede5ef69 100644
--- a/converter/other/cameratopam/util.c
+++ b/converter/other/cameratopam/util.c
@@ -1,10 +1,12 @@
 #define _XOPEN_SOURCE   /* Make sure unistd.h contains swab() */
 #include <unistd.h>
-#include <stdio.h>
 
-#include "pm.h"
+#include "netpbm/pm.h"
+#include "netpbm/mallocvar.h"
+
 #include "global_variables.h"
 #include "util.h"
+#include "stdio_nofail.h"
 
 #ifndef LONG_BITS
 #define LONG_BITS (8 * sizeof(long))
@@ -18,7 +20,7 @@ get2(FILE * const ifp)
 {
     unsigned char a, b;
 
-    a = fgetc(ifp);  b = fgetc(ifp);
+    a = fgetc_nofail(ifp);  b = fgetc_nofail(ifp);
 
     if (order == 0x4949)      /* "II" means little-endian */
         return a | b << 8;
@@ -34,8 +36,8 @@ get4(FILE * const ifp)
 {
     unsigned char a, b, c, d;
 
-    a = fgetc(ifp);  b = fgetc(ifp);
-    c = fgetc(ifp);  d = fgetc(ifp);
+    a = fgetc_nofail(ifp);  b = fgetc_nofail(ifp);
+    c = fgetc_nofail(ifp);  d = fgetc_nofail(ifp);
 
     if (order == 0x4949)
         return a | b << 8 | c << 16 | d << 24;
@@ -49,9 +51,21 @@ get4(FILE * const ifp)
 void
 read_shorts (FILE * const ifp, unsigned short *pixel, int count)
 {
-    fread (pixel, 2, count, ifp);
-    if ((order == 0x4949) == (BYTE_ORDER == BIG_ENDIAN))
-        swab (pixel, pixel, count*2);
+    unsigned short * buffer;
+
+    MALLOCARRAY(buffer, count);
+
+    if (!buffer)
+        pm_error("Failed to allocate a buffer for reading %u short "
+                 "integers from file", count);
+    else {
+        fread_nofail(buffer, 2, count, ifp);
+
+        if ((order == 0x4949) == (BYTE_ORDER == BIG_ENDIAN))
+            swab(buffer, pixel, count*2);
+
+        free(buffer);
+    }
 }
 
 /*
@@ -73,10 +87,10 @@ getbits (FILE * const ifp, int nbits)
         vbits -= nbits;
     }
     while (vbits < LONG_BITS - 7) {
-        c = fgetc(ifp);
+        c = fgetc_nofail(ifp);
         bitbuf = (bitbuf << 8) + c;
         if (c == 0xff && zero_after_ff)
-            fgetc(ifp);
+            fgetc_nofail(ifp);
         vbits += 8;
     }
     return ret;