[poppler] 2 commits - poppler/Annot.cc poppler/SplashOutputDev.cc poppler/Stream.cc poppler/Stream.h

Albert Astals Cid aacid at kemper.freedesktop.org
Fri Apr 6 10:22:17 UTC 2018


 poppler/Annot.cc           |   16 ++----
 poppler/SplashOutputDev.cc |    3 -
 poppler/Stream.cc          |   83 --------------------------------
 poppler/Stream.h           |  116 ++++++++++++++++++++++++++++++++++++++-------
 4 files changed, 105 insertions(+), 113 deletions(-)

New commits:
commit f3551055dab57eb0bc544070b15c6a7435585003
Author: Albert Astals Cid <aacid at kde.org>
Date:   Fri Apr 6 12:19:54 2018 +0200

    Introduce AutoFreeMemStream
    
    This is done by turning the old MemStream implementation to a new template implementation in BaseMemStream
    and inheriting from it MemStream for const char * and AutoFreeMemStream for char *.
    
    This way we make clear one frees the data and the other does not, while also removing the ugly setNeedsFree in MemStream

diff --git a/poppler/Annot.cc b/poppler/Annot.cc
index c66096e7..2752bc7e 100644
--- a/poppler/Annot.cc
+++ b/poppler/Annot.cc
@@ -1747,10 +1747,9 @@ Object Annot::createForm(const GooString *appearBuf, double *bbox, GBool transpa
   if (resDict)
     appearDict->set("Resources", Object(resDict));
 
-  MemStream *mStream = new MemStream(copyString(appearBuf->getCString()), 0,
+  Stream *mStream = new AutoFreeMemStream(copyString(appearBuf->getCString()), 0,
 				     appearBuf->getLength(), Object(appearDict));
-  mStream->setNeedFree(gTrue);
-  return Object(static_cast<Stream*>(mStream));
+  return Object(mStream);
 }
 
 Dict *Annot::createResourcesDict(const char *formName, Object &&formStream,
@@ -4898,11 +4897,9 @@ void AnnotWidget::generateFieldAppearance(bool *addedDingbatsResource) {
   }
 
   // build the appearance stream
-  MemStream *appearStream = new MemStream(copyString(appearBuf->getCString()), 0,
+  Stream *appearStream = new AutoFreeMemStream(copyString(appearBuf->getCString()), 0,
       appearBuf->getLength(), Object(appearDict));
-  appearance = Object(static_cast<Stream*>(appearStream));
-
-  appearStream->setNeedFree(gTrue);
+  appearance = Object(appearStream);
 }
 
 void AnnotWidget::updateAppearanceStream()
@@ -5085,13 +5082,12 @@ void AnnotMovie::draw(Gfx *gfx, GBool printing) {
       formDict->set("Matrix", Object(matrix));
       formDict->set("Resources", Object(resDict));
 
-      MemStream *mStream = new MemStream(copyString(appearBuf->getCString()), 0,
+      Stream *mStream = new AutoFreeMemStream(copyString(appearBuf->getCString()), 0,
 			      appearBuf->getLength(), Object(formDict));
-      mStream->setNeedFree(gTrue);
       delete appearBuf;
 
       Dict *dict = new Dict(gfx->getXRef());
-      dict->set("FRM", Object(static_cast<Stream*>(mStream)));
+      dict->set("FRM", Object(mStream));
 
       Dict *resDict2 = new Dict(gfx->getXRef());
       resDict2->set("XObject", Object(dict));
diff --git a/poppler/SplashOutputDev.cc b/poppler/SplashOutputDev.cc
index 253df7f0..73963302 100644
--- a/poppler/SplashOutputDev.cc
+++ b/poppler/SplashOutputDev.cc
@@ -3979,8 +3979,7 @@ void SplashOutputDev::drawSoftMaskedImage(GfxState *state, Object *ref,
     maskStr->reset();
     maskStr->doGetChars(maskWidth * maskHeight, data);
     maskStr->close();
-    maskStr = new MemStream((char *)data, 0, maskWidth * maskHeight, maskStr->getDictObject()->copy());
-    ((MemStream *) maskStr)->setNeedFree(gTrue);
+    maskStr = new AutoFreeMemStream((char *)data, 0, maskWidth * maskHeight, maskStr->getDictObject()->copy());
   }
   imgMaskData.imgStr = new ImageStream(maskStr, maskWidth,
 				       maskColorMap->getNumPixelComps(),
diff --git a/poppler/Stream.h b/poppler/Stream.h
index 8d98ef75..3eca7452 100644
--- a/poppler/Stream.h
+++ b/poppler/Stream.h
@@ -567,30 +567,23 @@ private:
 // MemStream
 //------------------------------------------------------------------------
 
-class MemStream: public BaseStream {
+template<typename T>
+class BaseMemStream: public BaseStream {
 public:
 
-  MemStream(char *bufA, Goffset startA, Goffset lengthA, Object &&dictA) : BaseStream(std::move(dictA), lengthA) {
+  BaseMemStream(T *bufA, Goffset startA, Goffset lengthA, Object &&dictA) : BaseStream(std::move(dictA), lengthA) {
     buf = bufA;
     start = startA;
     length = lengthA;
     bufEnd = buf + start + length;
     bufPtr = buf + start;
-    needFree = gFalse;
-  }
-
-  ~MemStream() {
-    if (needFree) {
-      gfree(buf);
-    }
   }
 
   BaseStream *copy() override {
-    return new MemStream(buf, start, length, dict.copy());
+    return new BaseMemStream(buf, start, length, dict.copy());
   }
 
   Stream *makeSubStream(Goffset startA, GBool limited, Goffset lengthA, Object &&dictA) override {
-    MemStream *subStr;
     Goffset newLength;
 
     if (!limited || startA + lengthA > start + length) {
@@ -598,8 +591,7 @@ public:
     } else {
       newLength = lengthA;
     }
-    subStr = new MemStream(buf, startA, newLength, std::move(dictA));
-    return subStr;
+    return new BaseMemStream(buf, startA, newLength, std::move(dictA));
   }
 
   StreamKind getKind() override { return strWeird; }
@@ -642,14 +634,13 @@ public:
     bufPtr = buf + start;
   }
 
-  //if needFree = true, the stream will delete buf when it is destroyed
-  //otherwise it will not touch it. Default value is false
-  virtual void setNeedFree (GBool val) { needFree = val; }
-
   int getUnfilteredChar () override { return getChar(); }
 
   void unfilteredReset () override { reset (); } 
 
+protected:
+  T *buf;
+
 private:
 
   GBool hasGetChars() override { return true; }
@@ -670,13 +661,31 @@ private:
     return n;
   }
 
-  char *buf;
   Goffset start;
-  char *bufEnd;
-  char *bufPtr;
-  GBool needFree;
+  T *bufEnd;
+  T *bufPtr;
 };
 
+class MemStream : public BaseMemStream<const char>
+{
+public:
+  MemStream(const char *bufA, Goffset startA, Goffset lengthA, Object &&dictA)
+   : BaseMemStream(bufA, startA, lengthA, std::move(dictA))
+   { }
+};
+
+class AutoFreeMemStream : public BaseMemStream<char>
+{
+public:
+  AutoFreeMemStream(char *bufA, Goffset startA, Goffset lengthA, Object &&dictA)
+   : BaseMemStream(bufA, startA, lengthA, std::move(dictA))
+   { }
+
+  ~AutoFreeMemStream()
+    { gfree(buf); }
+};
+
+
 //------------------------------------------------------------------------
 // EmbedStream
 //
commit 4cc89c79f58cb416aae8396190a788e1398113ff
Author: Albert Astals Cid <aacid at kde.org>
Date:   Fri Apr 6 12:11:55 2018 +0200

    Move MemStream implementation to header
    
    Will be useful in next step where we turn it into a template

diff --git a/poppler/Stream.cc b/poppler/Stream.cc
index 0602c7bb..dd8bfc1a 100644
--- a/poppler/Stream.cc
+++ b/poppler/Stream.cc
@@ -951,89 +951,6 @@ void CachedFileStream::moveStart(Goffset delta)
 }
 
 //------------------------------------------------------------------------
-// MemStream
-//------------------------------------------------------------------------
-
-MemStream::MemStream(char *bufA, Goffset startA, Goffset lengthA, Object &&dictA):
-    BaseStream(std::move(dictA), lengthA) {
-  buf = bufA;
-  start = startA;
-  length = lengthA;
-  bufEnd = buf + start + length;
-  bufPtr = buf + start;
-  needFree = gFalse;
-}
-
-MemStream::~MemStream() {
-  if (needFree) {
-    gfree(buf);
-  }
-}
-
-BaseStream *MemStream::copy() {
-  return new MemStream(buf, start, length, dict.copy());
-}
-
-Stream *MemStream::makeSubStream(Goffset startA, GBool limited,
-				 Goffset lengthA, Object &&dictA) {
-  MemStream *subStr;
-  Goffset newLength;
-
-  if (!limited || startA + lengthA > start + length) {
-    newLength = start + length - startA;
-  } else {
-    newLength = lengthA;
-  }
-  subStr = new MemStream(buf, startA, newLength, std::move(dictA));
-  return subStr;
-}
-
-void MemStream::reset() {
-  bufPtr = buf + start;
-}
-
-void MemStream::close() {
-}
-
-int MemStream::getChars(int nChars, Guchar *buffer) {
-  int n;
-
-  if (nChars <= 0) {
-    return 0;
-  }
-  if (bufEnd - bufPtr < nChars) {
-    n = (int)(bufEnd - bufPtr);
-  } else {
-    n = nChars;
-  }
-  memcpy(buffer, bufPtr, n);
-  bufPtr += n;
-  return n;
-}
-
-void MemStream::setPos(Goffset pos, int dir) {
-  Guint i;
-
-  if (dir >= 0) {
-    i = pos;
-  } else {
-    i = start + length - pos;
-  }
-  if (i < start) {
-    i = start;
-  } else if (i > start + length) {
-    i = start + length;
-  }
-  bufPtr = buf + i;
-}
-
-void MemStream::moveStart(Goffset delta) {
-  start += delta;
-  length -= delta;
-  bufPtr = buf + start;
-}
-
-//------------------------------------------------------------------------
 // EmbedStream
 //------------------------------------------------------------------------
 
diff --git a/poppler/Stream.h b/poppler/Stream.h
index b58da825..8d98ef75 100644
--- a/poppler/Stream.h
+++ b/poppler/Stream.h
@@ -570,34 +570,105 @@ private:
 class MemStream: public BaseStream {
 public:
 
-  MemStream(char *bufA, Goffset startA, Goffset lengthA, Object &&dictA);
-  ~MemStream();
-  BaseStream *copy() override;
-  Stream *makeSubStream(Goffset start, GBool limited,
-				Goffset lengthA, Object &&dictA) override;
+  MemStream(char *bufA, Goffset startA, Goffset lengthA, Object &&dictA) : BaseStream(std::move(dictA), lengthA) {
+    buf = bufA;
+    start = startA;
+    length = lengthA;
+    bufEnd = buf + start + length;
+    bufPtr = buf + start;
+    needFree = gFalse;
+  }
+
+  ~MemStream() {
+    if (needFree) {
+      gfree(buf);
+    }
+  }
+
+  BaseStream *copy() override {
+    return new MemStream(buf, start, length, dict.copy());
+  }
+
+  Stream *makeSubStream(Goffset startA, GBool limited, Goffset lengthA, Object &&dictA) override {
+    MemStream *subStr;
+    Goffset newLength;
+
+    if (!limited || startA + lengthA > start + length) {
+      newLength = start + length - startA;
+    } else {
+      newLength = lengthA;
+    }
+    subStr = new MemStream(buf, startA, newLength, std::move(dictA));
+    return subStr;
+  }
+
   StreamKind getKind() override { return strWeird; }
-  void reset() override;
-  void close() override;
+
+  void reset() override {
+    bufPtr = buf + start;
+  }
+
+  void close() override { }
+
   int getChar() override
     { return (bufPtr < bufEnd) ? (*bufPtr++ & 0xff) : EOF; }
+
   int lookChar() override
     { return (bufPtr < bufEnd) ? (*bufPtr & 0xff) : EOF; }
+
   Goffset getPos() override { return (int)(bufPtr - buf); }
-  void setPos(Goffset pos, int dir = 0) override;
+
+  void setPos(Goffset pos, int dir = 0) override {
+    Guint i;
+
+    if (dir >= 0) {
+      i = pos;
+    } else {
+      i = start + length - pos;
+    }
+    if (i < start) {
+      i = start;
+    } else if (i > start + length) {
+      i = start + length;
+    }
+    bufPtr = buf + i;
+  }
+
   Goffset getStart() override { return start; }
-  void moveStart(Goffset delta) override;
+
+  void moveStart(Goffset delta) override {
+    start += delta;
+    length -= delta;
+    bufPtr = buf + start;
+  }
 
   //if needFree = true, the stream will delete buf when it is destroyed
   //otherwise it will not touch it. Default value is false
   virtual void setNeedFree (GBool val) { needFree = val; }
 
   int getUnfilteredChar () override { return getChar(); }
+
   void unfilteredReset () override { reset (); } 
 
 private:
 
   GBool hasGetChars() override { return true; }
-  int getChars(int nChars, Guchar *buffer) override;
+
+  int getChars(int nChars, Guchar *buffer) override {
+    int n;
+
+    if (nChars <= 0) {
+      return 0;
+    }
+    if (bufEnd - bufPtr < nChars) {
+      n = (int)(bufEnd - bufPtr);
+    } else {
+      n = nChars;
+    }
+    memcpy(buffer, bufPtr, n);
+    bufPtr += n;
+    return n;
+  }
 
   char *buf;
   Goffset start;


More information about the poppler mailing list