From 369cd803e1f18dddc0f27b226ed95aa6ff12f833 Mon Sep 17 00:00:00 2001
From: Paul Wolneykien <manowar@altlinux.ru>
Date: Wed, 2 Nov 2011 05:48:14 +0400
Subject: [PATCH] Fix the reading of the PS part size and length from an EPS file

Fix the getDouble() function and its calls. It seems that the
problem is related to both QByteArray::insert() and the use of
QString type as the intermediate representation of the data.
First, the insert() method introduces spaces into the target
array at the getDouble() function (scribus/util_math.cpp), so
the number being parsed is corrupted. Second, an attempt to
copy bytes to the target array from a string using QChar::cell()
also results in a data corruption. So I decided not to use
QString at all.
---
 scribus/scribus/pslib.cpp              |    8 ++++----
 scribus/scribus/scimgdataloader_ps.cpp |    2 +-
 scribus/scribus/util_math.cpp          |   18 +++++++++---------
 scribus/scribus/util_math.h            |    2 +-
 4 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/scribus/scribus/pslib.cpp b/scribus/scribus/pslib.cpp
index e7ce466..6d3f8f8 100644
--- a/scribus/scribus/pslib.cpp
+++ b/scribus/scribus/pslib.cpp
@@ -1443,11 +1443,11 @@ bool PSLib::PS_ImageData(PageItem *c, QString fn, QString Name, QString Prof, bo
 		{
 			PutStream("currentfile 1 (%ENDEPSDATA) /SubFileDecode filter /ReusableStreamDecode filter\n");
 			PutStream("%%BeginDocument: " + fi.fileName() + "\n");
-			if (getDouble(QString(tmp.mid(0, 4)), true) == 0xC5D0D3C6)
+			if (getDouble(tmp.mid(0, 4), true) == 0xC5D0D3C6)
 			{
 				char* data = tmp.data();
-				uint startPos = getDouble(QString(tmp.mid(4, 4)), false);
-				uint length = getDouble(QString(tmp.mid(8, 4)), false);
+				uint startPos = getDouble(tmp.mid(4, 4), false);
+				uint length = getDouble(tmp.mid(8, 4), false);
 				PutStream(data+startPos, length, false);
 			}
 			else
@@ -1528,7 +1528,7 @@ bool PSLib::PS_image(PageItem *c, double x, double y, QString fn, double scalex,
 			else
 			{
       				PutStream("%%BeginDocument: " + fi.fileName() + "\n");
-					if (getDouble(QString(tmp.mid(0, 4)), true) == 0xC5D0D3C6)
+					if (getDouble(tmp.mid(0, 4), true) == 0xC5D0D3C6)
 					{
 						char* data = tmp.data();
 						uint startPos = getDouble(tmp.mid(4, 4), false);
diff --git a/scribus/scribus/scimgdataloader_ps.cpp b/scribus/scribus/scimgdataloader_ps.cpp
index 3c03b9f..4e0c8f3 100644
--- a/scribus/scribus/scimgdataloader_ps.cpp
+++ b/scribus/scribus/scimgdataloader_ps.cpp
@@ -147,7 +147,7 @@ bool ScImgDataLoader_PS::parseData(QString fn)
 	{
 		QByteArray tempBuf(9, ' ');
 		f.read(tempBuf.data(), 8);
-		if (getDouble(QString(tempBuf.mid(0, 4)), true) == 0xC5D0D3C6)
+		if (getDouble(tempBuf.mid(0, 4), true) == 0xC5D0D3C6)
 		{
 			startPos = getDouble(tempBuf.mid(4, 4), false);
 			if (doThumbnail)
diff --git a/scribus/scribus/util_math.cpp b/scribus/scribus/util_math.cpp
index e398616..8736b36 100644
--- a/scribus/scribus/util_math.cpp
+++ b/scribus/scribus/util_math.cpp
@@ -30,7 +30,7 @@ using namespace std;
 
 
 
-uint getDouble(QString in, bool raw)
+uint getDouble(QByteArray in, bool raw)
 {
 	QByteArray bb(4, ' ');
 	if (raw)
@@ -40,10 +40,10 @@ uint getDouble(QString in, bool raw)
 		bb[2] = static_cast<uchar>(QChar(in.at(1)));
 		bb[1] = static_cast<uchar>(QChar(in.at(2)));
 		bb[0] = static_cast<uchar>(QChar(in.at(3)));*/
-		bb = bb.insert(3, in.at(0));
-		bb = bb.insert(2, in.at(1));
-		bb = bb.insert(1, in.at(2));
-		bb = bb.insert(0, in.at(3));
+		bb[3] = in[0];
+		bb[2] = in[1];
+		bb[1] = in[2];
+		bb[0] = in[3];
 	}
 	else
 	{
@@ -52,10 +52,10 @@ uint getDouble(QString in, bool raw)
 // 		bb[1] = static_cast<uchar>(QChar(in.at(1)));
 // 		bb[2] = static_cast<uchar>(QChar(in.at(2)));
 // 		bb[3] = static_cast<uchar>(QChar(in.at(3)));
-		bb = bb.insert(0, in.at(0));
-		bb = bb.insert(1, in.at(1));
-		bb = bb.insert(2, in.at(2));
-		bb = bb.insert(3, in.at(3));
+		bb[0] = in[0];
+		bb[1] = in[1];
+		bb[2] = in[2];
+		bb[3] = in[3];
 	}
 	uint ret;
 	ret = bb[0] & 0xff;
diff --git a/scribus/scribus/util_math.h b/scribus/scribus/util_math.h
index 603495c..688444d 100644
--- a/scribus/scribus/util_math.h
+++ b/scribus/scribus/util_math.h
@@ -35,7 +35,7 @@ FPoint SCRIBUS_API projectPointOnLine(FPoint p, QPointF lineStart, QPointF lineE
 QPolygon SCRIBUS_API FlattenPath(const FPointArray& ina, QList<uint> &Segs);
 QList<QPainterPath> SCRIBUS_API decomposePath(QPainterPath &path);
 QPainterPath SCRIBUS_API RegularPolygon(double w, double h, uint c, bool star, double factor, double rota, double factor2 = 0.0);
-uint SCRIBUS_API getDouble(QString in, bool raw);
+uint SCRIBUS_API getDouble(QByteArray in, bool raw);
 inline double SCRIBUS_API sind(double);
 inline double SCRIBUS_API cosd(double);
 inline double SCRIBUS_API square(double);
-- 
1.7.3.3

