Author: post
Date: 2012-12-09 15:08:06 +0100 (Sun, 09 Dec 2012)
New Revision: 503

Modified:
   RawSpeed/BitPumpJPEG.cpp
   RawSpeed/BitPumpJPEG.h
   RawSpeed/BitPumpMSB.cpp
   RawSpeed/BitPumpMSB.h
Log:
Make the bitpumps not read outside the area needed for decoding.

Modified: RawSpeed/BitPumpJPEG.cpp
===================================================================
--- RawSpeed/BitPumpJPEG.cpp    2012-12-09 13:58:03 UTC (rev 502)
+++ RawSpeed/BitPumpJPEG.cpp    2012-12-09 14:08:06 UTC (rev 503)
@@ -52,6 +52,33 @@
     return;
   // Fill in 96 bits
   int* b = (int*)current_buffer;
+  if ((off + 12) >= size) {
+    while(mLeft <= 64 && off < size) {
+      for (int i = (mLeft>>3); i >= 0; i--)
+        current_buffer[i+1] = current_buffer[i];
+      uchar8 val = buffer[off++];
+      if (val == 0xff) {
+        if (buffer[off] == 0)
+          off++;
+        else {
+          // We hit another marker - don't forward bitpump anymore
+          val = 0;
+          off--;
+          stuffed++;
+        }
+      }
+      current_buffer[0] = val;
+      mLeft+=8;
+    }
+    while (mLeft < 64) {
+      b[2] = b[1];
+      b[1] = b[0];
+      b[0] = 0;
+      mLeft +=32;
+      stuffed +=4;  //We are adding to mLeft without incrementing offset
+    }
+    return;
+  }
   b[3] = b[0];
   for (int i = 0; i < 12; i++) {
     uchar8 val = buffer[off++];

Modified: RawSpeed/BitPumpJPEG.h
===================================================================
--- RawSpeed/BitPumpJPEG.h      2012-12-09 13:58:03 UTC (rev 502)
+++ RawSpeed/BitPumpJPEG.h      2012-12-09 14:08:06 UTC (rev 503)
@@ -41,7 +41,7 @@
        uchar8 getByteSafe();
        void setAbsoluteOffset(uint32 offset);     // Set offset in bytes
   __inline uint32 getOffset() { return off-(mLeft>>3)+stuffed;}
-  __inline void checkPos()  { if (off>size+12) throw IOException("Out of 
buffer read");};        // Check if we have a valid position
+  __inline void checkPos()  { if (off>=size || stuffed > (mLeft>>3)) 
ThrowIOE("Out of buffer read");};        // Check if we have a valid position
 
   // Fill the buffer with at least 24 bits
  void fill();

Modified: RawSpeed/BitPumpMSB.cpp
===================================================================
--- RawSpeed/BitPumpMSB.cpp     2012-12-09 13:58:03 UTC (rev 502)
+++ RawSpeed/BitPumpMSB.cpp     2012-12-09 14:08:06 UTC (rev 503)
@@ -39,6 +39,7 @@
 }
 
 __inline void BitPumpMSB::init() {
+  mStuffed = 0;
   current_buffer = (uchar8*)_aligned_malloc(16, 16);
   if (!current_buffer)
     ThrowRDE("BitPumpMSB::init(): Unable to allocate memory");
@@ -52,6 +53,23 @@
     return;
   // Fill in 96 bits
   int* b = (int*)current_buffer;
+  if ((off + 12) > size) {
+    while(mLeft <= 64 && off < size) {
+      for (int i = (mLeft>>3); i >= 0; i--)
+        current_buffer[i+1] = current_buffer[i];
+      current_buffer[0] = buffer[off++];
+      mLeft+=8;
+    }
+    while (mLeft <= 64) {
+      b[3] = b[2];
+      b[2] = b[1];
+      b[1] = b[0];
+      b[0] = 0;
+      mLeft +=32;
+      mStuffed += 4;
+    }
+    return;
+  }
   b[3] = b[0];
 #if defined(LE_PLATFORM_HAS_BSWAP)
   b[2] = PLATFORM_BSWAP32(*(int*)&buffer[off]);
@@ -79,7 +97,7 @@
 
 uint32 BitPumpMSB::getBitsSafe(unsigned int nbits) {
   if (nbits > MIN_GET_BITS)
-    throw IOException("Too many bits requested");
+    ThrowIOE("Too many bits requested");
 
   fill();
   checkPos();
@@ -95,9 +113,10 @@
 
 void BitPumpMSB::setAbsoluteOffset(unsigned int offset) {
   if (offset >= size)
-    throw IOException("Offset set out of buffer");
+    ThrowIOE("Offset set out of buffer");
 
   mLeft = 0;
+  mStuffed = 0;
   off = offset;
   fill();
 }

Modified: RawSpeed/BitPumpMSB.h
===================================================================
--- RawSpeed/BitPumpMSB.h       2012-12-09 13:58:03 UTC (rev 502)
+++ RawSpeed/BitPumpMSB.h       2012-12-09 14:08:06 UTC (rev 503)
@@ -41,7 +41,7 @@
        uchar8 getByteSafe();
        void setAbsoluteOffset(uint32 offset);     // Set offset in bytes
   __inline uint32 getOffset() { return off-(mLeft>>3);}
-  __inline void checkPos()  { if (off>size+12) throw IOException("Out of 
buffer read");};        // Check if we have a valid position
+  __inline void checkPos()  { if (mStuffed > 8) ThrowIOE("Out of buffer 
read");};        // Check if we have a valid position
 
   // Fill the buffer with at least 24 bits
  void fill();
@@ -133,6 +133,7 @@
   const uint32 size;            // This if the end of buffer.
   uint32 mLeft;
   uint32 off;                  // Offset in bytes
+  uint32 mStuffed;
 private:
 };
 


_______________________________________________
Rawstudio-commit mailing list
[email protected]
http://rawstudio.org/cgi-bin/mailman/listinfo/rawstudio-commit

Reply via email to