about summary refs log tree commit diff
path: root/converter/pbm
diff options
context:
space:
mode:
Diffstat (limited to 'converter/pbm')
-rw-r--r--converter/pbm/pbmtoatk.c8
-rw-r--r--converter/pbm/pbmtog3.c2
-rw-r--r--converter/pbm/pbmtomacp.c19
-rw-r--r--converter/pbm/pbmtomgr.c13
-rw-r--r--converter/pbm/pbmtopi3.c31
-rw-r--r--converter/pbm/pbmtopk.c12
-rw-r--r--converter/pbm/pbmtoxbm.c19
-rw-r--r--converter/pbm/pktopbm.c5
8 files changed, 67 insertions, 42 deletions
diff --git a/converter/pbm/pbmtoatk.c b/converter/pbm/pbmtoatk.c
index dd829776..9399f602 100644
--- a/converter/pbm/pbmtoatk.c
+++ b/converter/pbm/pbmtoatk.c
@@ -125,7 +125,6 @@ main(int argc, char *argv[]) {
     register bit *bP;
     int rows, cols, format, row;
     int col;
-    char name[100], *cp;
     unsigned char curbyte, newbyte;
     int curcount, gather;
 
@@ -136,15 +135,8 @@ main(int argc, char *argv[]) {
 
     else if (argc-1 == 1) {
         ifd = pm_openr( argv[1] );
-        strcpy(name, argv[1]);
-        if (streq( name, "-"))
-            strcpy(name, "noname");
-        
-        if ((cp = strchr(name, '.')) != 0)
-            *cp = '\0';
     } else {
         ifd = stdin;
-        strcpy( name, "noname" );
     }
 
     pbm_readpbminit(ifd, &cols, &rows, &format);
diff --git a/converter/pbm/pbmtog3.c b/converter/pbm/pbmtog3.c
index f53bfaa0..c0dd8c64 100644
--- a/converter/pbm/pbmtog3.c
+++ b/converter/pbm/pbmtog3.c
@@ -440,7 +440,7 @@ main(int    argc,
 
     MALLOCARRAY_NOFAIL(bitrow, pbm_packed_bytes(cols) + sizeof(wordint));
 
-    MALLOCARRAY_NOFAIL(milepost, readcols + 1);
+    MALLOCARRAY_NOFAIL(milepost, readcols + 2);
 
     initOutStream(cmdline.reversebits);
     puteol();
diff --git a/converter/pbm/pbmtomacp.c b/converter/pbm/pbmtomacp.c
index ad0b22b1..9dc700e6 100644
--- a/converter/pbm/pbmtomacp.c
+++ b/converter/pbm/pbmtomacp.c
@@ -20,6 +20,8 @@
 #define EQUAL		1
 #define UNEQUAL		0
 
+#define MIN3(a,b,c)     (MIN((MIN((a),(b))),(c)))
+
 static void fillbits ARGS(( bit **bits, bit **bitsr, int top, int left, int bottom, int right ));
 static void writemacp ARGS(( bit **bits ));
 static int packit ARGS(( bit *pb, bit *bits ));
@@ -102,23 +104,18 @@ char *argv[];
     left = 0;
 
   if( rflg )
-  { if( right - left >= MAX_COLS )
-      right = left + MAX_COLS - 1;
-  }
+    right = MIN3( right, cols - 1, left + MAX_COLS - 1 );
   else
-    right = ( left + MAX_COLS > cols ) ? ( cols - 1 ) : ( left + MAX_COLS - 1 );
+    right = MIN( cols - 1,  left + MAX_COLS - 1 );
 
   if( !tflg )
     top = 0;
 
   if( bflg )
-  { if( bottom - top >= MAX_LINES )
-      bottom = top + MAX_LINES - 1;
-  }
+    bottom = MIN3( bottom, rows - 1, top + MAX_LINES - 1);
   else
-    bottom = ( top + MAX_LINES > rows ) ?
-		   ( rows - 1 ) : ( top + MAX_LINES - 1 );
-  
+    bottom = MIN( rows - 1, top + MAX_LINES - 1 );
+
     if( right <= left || left < 0 || right - left + 1 > MAX_COLS )
       pm_error("error in right (= %d) and/or left (=%d)",right,left );
     if( bottom <= top || top < 0 || bottom - top + 1 > MAX_LINES )
@@ -220,7 +217,7 @@ packit( pb, bits )
       save = *srcb++;
       charcount--;
       newcount = 1;
-      while( (*srcb == save) && charcount ) { 
+      while( charcount && (*srcb == save) ) { 
           srcb++;
           newcount++;
           charcount--;
diff --git a/converter/pbm/pbmtomgr.c b/converter/pbm/pbmtomgr.c
index 2ca7c7d0..d12e6635 100644
--- a/converter/pbm/pbmtomgr.c
+++ b/converter/pbm/pbmtomgr.c
@@ -6,9 +6,12 @@
    ftp://sunsite.unc.edu/pub/Linux/apps/MGR/!INDEX.html
 */
 
+#include <assert.h>
 #include "pbm.h"
 #include "mgr.h"
 
+
+
 static void
 putinit(unsigned int const rows,
         unsigned int const cols) {
@@ -16,6 +19,10 @@ putinit(unsigned int const rows,
     struct b_header head;
     size_t writtenCount;
 
+    /* Because of argument restrictions: maximum dimensions: */
+    assert((rows & 0xfff) == rows);
+    assert((cols & 0xfff) == cols);
+
     head.magic[0] = 'y';
     head.magic[1] = 'z';
     head.h_wide = ((cols >> 6) & 0x3f) + ' ';
@@ -48,6 +55,8 @@ main(int argc,
            a multiple of 8, i.e. an integral number of packed bytes.
         */
     const char * inputFileName;
+    unsigned int const maxDimension = 4095;
+        /* Dimensions are 2 characters of the header -- 12 bits */
 
     pbm_init(&argc, argv);
 
@@ -62,6 +71,10 @@ main(int argc,
     ifP = pm_openr(inputFileName);
 
     pbm_readpbminit(ifP, &cols, &rows, &format);
+    if (cols > maxDimension)
+        pm_error("Image width too large: %u (max: %u)", cols, maxDimension);
+    if (rows > maxDimension)
+        pm_error("Image height too large: %u (max: %u)", rows, maxDimension);
     
     bitrow = pbm_allocrow_packed(cols);
     bytesPerRow = pbm_packed_bytes(cols);
diff --git a/converter/pbm/pbmtopi3.c b/converter/pbm/pbmtopi3.c
index 6a60af62..1dbf1a71 100644
--- a/converter/pbm/pbmtopi3.c
+++ b/converter/pbm/pbmtopi3.c
@@ -14,6 +14,7 @@
 
 #include <stdio.h>
 #include "pbm.h"
+#include "pm_c_util.h"
 
 static void putinit ARGS(( void ));
 static void putbit ARGS(( bit b ));
@@ -28,8 +29,9 @@ main( argc, argv )
     FILE* ifp;
     bit* bitrow;
     register bit* bP;
-    int rows, cols, format, padright, row, col;
-
+    int inrows, incols, format, padright, row, col;
+    int const outcols = 640;
+    int const outrows = 400;
 
     pbm_init( &argc, argv );
 
@@ -41,27 +43,26 @@ main( argc, argv )
     else
 	ifp = stdin;
 
-    pbm_readpbminit( ifp, &cols, &rows, &format );
-    if (cols > 640)
-	cols = 640;
-    if (rows > 400)
-	rows = 400;
-    bitrow = pbm_allocrow( cols );
-    
+    pbm_readpbminit( ifp, &incols, &inrows, &format );
+    bitrow = pbm_allocrow( MAX(incols, outcols) );
+
     /* Compute padding to round cols up to 640 */
-    padright = 640 - cols;
+    if(incols < outcols)
+        padright = outcols - incols;
+    else 
+        padright = 0;
 
     putinit( );
-    for ( row = 0; row < rows; ++row )
+    for ( row = 0; row < MIN(inrows, outrows); ++row )
 	{
-	pbm_readpbmrow( ifp, bitrow, cols, format );
-        for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
+	pbm_readpbmrow( ifp, bitrow, incols, format );
+        for ( col = 0, bP = bitrow; col < MIN(incols, outcols); ++col, ++bP )
 	    putbit( *bP );
 	for ( col = 0; col < padright; ++col )
 	    putbit( 0 );
         }
-    while (row++ < 400)
-	for ( col = 0; col < 640; ++col)
+    while (row++ < outrows)
+	for ( col = 0; col < outcols; ++col)
 	    putbit( 0 );
 
     pm_close( ifp );
diff --git a/converter/pbm/pbmtopk.c b/converter/pbm/pbmtopk.c
index a9683190..fc94f855 100644
--- a/converter/pbm/pbmtopk.c
+++ b/converter/pbm/pbmtopk.c
@@ -854,11 +854,17 @@ main(int argc, char *argv[]) {
     initialize_pk() ;
    
     if (--argc < 1) pm_usage(usage) ;
-    strcpy(pkname, *++argv) ;
+    ++argv;
+    if(strlen(*argv) + 4 > NAMELENGTH)
+        pm_error("pkname is too long");
+    strcpy(pkname, *argv) ;
     pbmtopk_add_suffix(pkname, ".pk") ;
    
-    if (--argc < 1) pm_usage(usage) ;
-    strcpy(tfmname, *++argv) ;
+    if (--argc < 1) pm_usage(usage);
+    ++argv;
+    if(strlen(*argv) + 4 > NAMELENGTH)
+        pm_error("tfmname is too long");
+    strcpy(tfmname, *argv) ;
     pbmtopk_add_suffix(tfmname, ".tfm") ;
    
     if (--argc < 1) pm_usage(usage) ;
diff --git a/converter/pbm/pbmtoxbm.c b/converter/pbm/pbmtoxbm.c
index 937e56c5..340642ce 100644
--- a/converter/pbm/pbmtoxbm.c
+++ b/converter/pbm/pbmtoxbm.c
@@ -249,7 +249,7 @@ puttermX10(void) {
                     (i == 0) ? " " : "",
                     itemBuff[i+1],
                     itemBuff[i], 
-                    (i == itemCnt - 2) ? "};\n" : ",");
+                    (i == itemCnt - 2) ? "" : ",");
         if (rc < 0)        
             pm_error("Error writing end of X10 bitmap raster.  "
                      "printf() failed with errno %d (%s)",
@@ -270,7 +270,7 @@ puttermX11(void) {
         rc = printf("%s0x%02x%s",
                     (i == 0)  ? " " : "",
                     itemBuff[i],
-                    (i == itemCnt - 1) ? "};\n" : ",");
+                    (i == itemCnt - 1) ? "" : ",");
 
         if (rc < 0)        
             pm_error("Error writing end of X11 bitmap raster.  "
@@ -297,6 +297,17 @@ putterm(void) {
     case X10: puttermX10(); break;
     case X11: puttermX11(); break;
     }
+
+    {
+        int rc;
+
+        rc = printf("};\n");
+
+        if (rc < 0)        
+            pm_error("Error writing end of X11 bitmap raster.  "
+                     "printf() failed with errno %d (%s)",
+                     errno, strerror(errno));
+    }
 }
 
 
@@ -339,7 +350,6 @@ convertRaster(FILE *          const ifP,
     putinit(xbmVersion);
 
     bitrow = pbm_allocrow_packed(cols + padright);
-    bitrow[bitrowBytes-1] = 0;
     
     for (row = 0; row < rows; ++row) {
         int const bitrowInBytes = pbm_packed_bytes(cols);
@@ -354,6 +364,9 @@ convertRaster(FILE *          const ifP,
             bitrow[bitrowInBytes - 1] <<= padrightIn;
         }
 
+        if (padright >= 8)
+            bitrow[bitrowBytes-1] = 0x00;
+
         for (i = 0; i < bitrowBytes; ++i)
             putitem(bitrow[i]);
     }
diff --git a/converter/pbm/pktopbm.c b/converter/pbm/pktopbm.c
index a3584ee5..712f339f 100644
--- a/converter/pbm/pktopbm.c
+++ b/converter/pbm/pktopbm.c
@@ -214,7 +214,10 @@ main(int argc, char *argv[]) {
 
     if (--argc < 1) pm_usage(usage) ;
 
-    strcpy(pkname, *++argv) ;
+    ++argv;
+    if(strlen(*argv) + 4 > NAMELENGTH)
+        pm_error("pkname is too long");
+    strcpy(pkname, *argv) ;
     pktopbm_add_suffix(pkname, ".pk") ;
 
     car = 0 ;