Merge pull request #1164 from sebras/master
[openjpeg.git] / src / bin / jpwl / convert.c
index 245ae28e9218df75837aa8ebbe05c24454fc44bc..4f636c175fce408fa30b85cd32181195734e6a1c 100644 (file)
@@ -41,6 +41,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <limits.h>
 
 #ifdef OPJ_HAVE_LIBTIFF
 #include <tiffio.h>
@@ -129,6 +130,7 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel,
     if (fread(tga, TGA_HEADER_SIZE, 1, fp) != 1) {
         fprintf(stderr,
                 "\nError: fread return a number of element different from the expected.\n");
+        free(tga);
         return 0 ;
     }
     id_len = (unsigned char)tga[0];
@@ -443,7 +445,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
 {
     int width, height, bpp, x, y;
     opj_bool write_alpha;
-    int i, adjustR, adjustG, adjustB;
+    int i, adjustR, adjustG = 0, adjustB = 0;
     unsigned int alpha_channel;
     float r, g, b, a;
     unsigned char value;
@@ -484,8 +486,10 @@ int imagetotga(opj_image_t * image, const char *outfile)
     scale = 255.0f / (float)((1 << image->comps[0].prec) - 1);
 
     adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
-    adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
-    adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
+    if (image->numcomps >= 3) {
+        adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
+        adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
+    }
 
     for (y = 0; y < height; y++) {
         unsigned int index = y * width;
@@ -561,7 +565,7 @@ typedef struct {
 typedef struct {
     DWORD biSize;         /* Size of the structure in bytes */
     DWORD biWidth;        /* Width of the image in pixels */
-    DWORD biHeight;       /* Heigth of the image in pixels */
+    DWORD biHeight;       /* Height of the image in pixels */
     WORD biPlanes;        /* 1 */
     WORD biBitCount;      /* Number of color bits by pixels */
     DWORD biCompression;      /* Type of encoding 0: none 1: RLE8 2: RLE4 */
@@ -736,6 +740,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
 
         if (fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H,
                   IN) != (3 * W + PAD) * H) {
+            fclose(IN);
             free(RGB);
             opj_image_destroy(image);
             fprintf(stderr,
@@ -1347,10 +1352,11 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
     }
 
     fseek(f, 0, SEEK_SET);
-    if (fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d", temp, &endian1,
+    if (fscanf(f, "PG%31[ \t]%c%c%31[ \t+-]%d%31[ \t]%d%31[ \t]%d", temp, &endian1,
                &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) {
         fprintf(stderr,
                 "ERROR: Failed to read the right number of element from the fscanf() function!\n");
+        fclose(f);
         return NULL;
     }
 
@@ -1370,6 +1376,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
         bigendian = 0;
     } else {
         fprintf(stderr, "Bad pgx header, please check input file\n");
+        fclose(f);
         return NULL;
     }
 
@@ -1523,6 +1530,7 @@ int imagetopgx(opj_image_t * image, const char *outfile)
                 res = fwrite(&byte, 1, 1, fdest);
                 if (res < 1) {
                     fprintf(stderr, "failed to write 1 byte for %s\n", name);
+                    fclose(fdest);
                     return 1;
                 }
             }
@@ -1857,6 +1865,15 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
         return NULL;
     }
 
+    /* This limitation could be removed by making sure to use size_t below */
+    if (header_info.height != 0 &&
+            header_info.width > INT_MAX / header_info.height) {
+        fprintf(stderr, "pnmtoimage:Image %dx%d too big!\n",
+                header_info.width, header_info.height);
+        fclose(fp);
+        return NULL;
+    }
+
     format = header_info.format;
 
     switch (format) {