check for destination overrun
authorClaus Gittinger <cg@exept.de>
Mon, 17 Jun 1996 10:13:50 +0200
changeset 1472 abaa73dc0e9a
parent 1471 a85f3257ae88
child 1473 63918b669150
check for destination overrun
ByteArray.st
--- a/ByteArray.st	Sat Jun 15 15:25:22 1996 +0200
+++ b/ByteArray.st	Mon Jun 17 10:13:50 1996 +0200
@@ -1449,7 +1449,7 @@
 !ByteArray methodsFor:'image manipulation support'!
 
 compressPixels:nBitsPerPixel width:width height:height into:aByteArray
-			 mapping:aMapByteArray
+                         mapping:aMapByteArray
 
     "given the receiver with 8-bit pixels, compress them into aByteArray
      with nBitsPerPixel-depth pixels. The width/height-arguments are needed
@@ -1468,6 +1468,7 @@
     REGISTER unsigned char *src, *dst;
     REGISTER int wrun;
     unsigned char *dstNext;
+    unsigned char *dstEnd;
     int bytesPerRow, mask, shift0, shift;
     int w, h, hrun;
     int srcBytes, dstBytes;
@@ -1480,78 +1481,82 @@
      && (__qClass(aByteArray) == @global(ByteArray))
      && __isSmallInteger(nBitsPerPixel)
      && __bothSmallInteger(height, width)) {
-	if ((aMapByteArray != nil)
-	 && (__Class(aMapByteArray) == @global(ByteArray))) {
-	    map = __ByteArrayInstPtr(aMapByteArray)->ba_element;
-	} else {
-	    map = (unsigned char *)0;
-	}
+        if ((aMapByteArray != nil)
+         && (__Class(aMapByteArray) == @global(ByteArray))) {
+            map = __ByteArrayInstPtr(aMapByteArray)->ba_element;
+        } else {
+            map = (unsigned char *)0;
+        }
+
+        bitsPerPixel = __intVal(nBitsPerPixel);
+        w = __intVal(width);
+        h = __intVal(height);
+        src = __ByteArrayInstPtr(self)->ba_element;
+        dst = __ByteArrayInstPtr(aByteArray)->ba_element;
+        dstEnd = dst + __byteArraySize(aByteArray);
+        switch (bitsPerPixel) {
+            case 1:
+                mask = 0x01;
+                break;
+            case 2:
+                mask = 0x03;
+                break;
+            case 4:
+                mask = 0x0F;
+                break;
+            case 8:
+                mask = 0xFF;
+                break;
+            default:
+                goto fail;
+        }
+        if (map) {
+            /*
+             * if a map is present, it must have entries for
+             * all possible byte-values (i.e. its size must be >= 256)
+             */
+            if ((__qSize(aMapByteArray) - OHDR_SIZE) < 256)
+                goto fail;
+        }
 
-	bitsPerPixel = __intVal(nBitsPerPixel);
-	w = __intVal(width);
-	h = __intVal(height);
-	src = __ByteArrayInstPtr(self)->ba_element;
-	dst = __ByteArrayInstPtr(aByteArray)->ba_element;
-	switch (bitsPerPixel) {
-	    case 1:
-		mask = 0x01;
-		break;
-	    case 2:
-		mask = 0x03;
-		break;
-	    case 4:
-		mask = 0x0F;
-		break;
-	    case 8:
-		mask = 0xFF;
-		break;
-	    default:
-		goto fail;
-	}
-	if (map) {
-	    /*
-	     * if a map is present, it must have entries for
-	     * all possible byte-values (i.e. its size must be >= 256)
-	     */
-	    if ((__qSize(aMapByteArray) - OHDR_SIZE) < 256)
-		goto fail;
-	}
+        bytesPerRow = (w * bitsPerPixel + 7) / 8;
+        dstBytes = bytesPerRow * h;
+        srcBytes = w * h;
 
-	bytesPerRow = (w * bitsPerPixel + 7) / 8;
-	dstBytes = bytesPerRow * h;
-	srcBytes = w * h;
-
-	if ((__byteArraySize(self) >= srcBytes)
-	 && (__byteArraySize(aByteArray) >= dstBytes)) {
-	    for (hrun=h; hrun; hrun--) {
-		dstNext = dst + bytesPerRow;
-		bits = 0; shift = 8;
-		if (map) {
-		    for (wrun=w; wrun; wrun--) {
-			bits = (bits << bitsPerPixel) | (map[*src++] & mask);
-			shift -= bitsPerPixel;
-			if (shift == 0) {
-			    *dst++ = bits;
-			    bits = 0; shift = 8;
-			}
-		    }
-		} else {
-		    for (wrun=w; wrun; wrun--) {
-			bits = (bits << bitsPerPixel) | (*src++ & mask);
-			shift -= bitsPerPixel;
-			if (shift == 0) {
-			    *dst++ = bits;
-			    bits = 0; shift = 8;
-			}
-		    }
-		}
-		if (shift != 8) {
-		    *dst = bits;
-		}
-		dst = dstNext;
-	    }
-	    RETURN ( self );
-	}
+        if ((__byteArraySize(self) >= srcBytes)
+         && (__byteArraySize(aByteArray) >= dstBytes)) {
+            for (hrun=h; hrun; hrun--) {
+                dstNext = dst + bytesPerRow;
+                bits = 0; shift = 8;
+                if (map) {
+                    for (wrun=w; wrun; wrun--) {
+                        bits = (bits << bitsPerPixel) | (map[*src++] & mask);
+                        shift -= bitsPerPixel;
+                        if (shift == 0) {
+                            if (dst == dstEnd) goto fail;
+                            *dst++ = bits;
+                            bits = 0; shift = 8;
+                        }
+                    }
+                } else {
+                    for (wrun=w; wrun; wrun--) {
+                        bits = (bits << bitsPerPixel) | (*src++ & mask);
+                        shift -= bitsPerPixel;
+                        if (shift == 0) {
+                            if (dst == dstEnd) goto fail;
+                            *dst++ = bits;
+                            bits = 0; shift = 8;
+                        }
+                    }
+                }
+                if (shift != 8) {
+                    if (dst == dstEnd) goto fail;
+                    *dst = bits;
+                }
+                dst = dstNext;
+            }
+            RETURN ( self );
+        }
     }
 fail: ;
 %}
@@ -1565,12 +1570,12 @@
      |inBits outBits|
 
      inBits := #[0 0 0 0 1 1 1 1
-		 0 0 1 1 0 0 1 1
-		 0 1 0 1 0 1 0 1
-		 1 1 1 1 0 0 0 0].
+                 0 0 1 1 0 0 1 1
+                 0 1 0 1 0 1 0 1
+                 1 1 1 1 0 0 0 0].
      outBits := ByteArray new:4.
      inBits compressPixels:1 width:8 height:4
-		    into:outBits mapping:nil.
+                    into:outBits mapping:nil.
      outBits inspect
     "
 
@@ -1587,7 +1592,7 @@
 
      outBits := ByteArray new:2.
      inBits compressPixels:1 width:16 height:1 
-		    into:outBits mapping:map.
+                    into:outBits mapping:map.
      outBits inspect
     "
 
@@ -1604,7 +1609,7 @@
 
      outBits := ByteArray new:2.
      inBits compressPixels:1 width:16 height:1 
-		    into:outBits mapping:map.
+                    into:outBits mapping:map.
      outBits inspect
     "
 !
@@ -2185,5 +2190,5 @@
 !ByteArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.65 1996-06-10 18:27:05 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.66 1996-06-17 08:13:50 cg Exp $'
 ! !