[Date Prev][Date Next][Thread Prev][Thread Next][Thread Index]

Re: Bug#382658: [XaraXtreme-dev] [Fwd: Bug#382658: xaralx: Unusable on powerpc (two errors : "Error from Xara Xtreme" and "Load failed")]



Please try the attached patch (committed in r1695, but
not tested on bigendian as I haven't got a platform).
This might fix the document loading issue. I am by no
means guaranteeing it fixes everything.

Alex
Index: Kernel/cxfile.cpp
===================================================================
--- Kernel/cxfile.cpp	(revision 1694)
+++ Kernel/cxfile.cpp	(revision 1695)
@@ -889,15 +889,17 @@
 
 BOOL CXaraFile::Read(FLOAT* pf)
 {
-	BOOL ok = (Read((BYTE*)pf,sizeof(FLOAT)));
-	*pf = LEtoNative(*pf);
+	FloatUnion f;
+	BOOL ok = (Read((BYTE*)&(f.u_INT32),sizeof(f.u_INT32)));
+	*pf = LEtoNative(f);
 	return ok;
 }
 
 BOOL CXaraFile::Read(double* pd)
 {
-	BOOL ok = (Read((BYTE*)pd,sizeof(double)));
-	*pd = LEtoNative(*pd);
+	DoubleUnion f;
+	BOOL ok = (Read((BYTE*)&(f.u_INT64),sizeof(f.u_INT64)));
+	*pd = LEtoNative(f);
 	return ok;
 }
 
@@ -1088,12 +1090,14 @@
 
 BOOL CXaraFile::Write(FLOAT f)
 {
-	return (Write((BYTE*)&f,sizeof(FLOAT)));
+	FloatUnion u=NativetoLEU(f);
+	return (Write((BYTE*)&(u.u_INT32),sizeof(u.u_INT32)));
 }
 
 BOOL CXaraFile::Write(double d)
 {
-	return (Write((BYTE*)&d,sizeof(double)));
+	DoubleUnion u=NativetoLEU(d);
+	return (Write((BYTE*)&(u.u_INT64),sizeof(u.u_INT64)));
 }
 
 BOOL CXaraFile::WriteWCHAR(WCHAR w)
Index: Kernel/cxfrec.cpp
===================================================================
--- Kernel/cxfrec.cpp	(revision 1694)
+++ Kernel/cxfrec.cpp	(revision 1695)
@@ -624,7 +624,8 @@
 BOOL CXaraFileRecord::WriteFLOAT(FLOAT f)
 {
 	ENTERWRITEFUNCTION(FTT_FLOAT);
-	BOOL ok = WriteBuffer((BYTE*)&f,sizeof(FLOAT));
+	FloatUnion u=NativetoLEU(f);
+	BOOL ok = WriteBuffer((BYTE*)&(u.u_INT32),sizeof(u.u_INT32));
 	LEAVEWRITEFUNCTION;
 	return (ok);
 }	
@@ -668,7 +669,8 @@
 BOOL CXaraFileRecord::WriteDOUBLE(double d)
 {
 	ENTERWRITEFUNCTION(FTT_DOUBLE);
-	BOOL ok = WriteBuffer((BYTE*)&d,sizeof(double));
+	DoubleUnion u=NativetoLEU(d);
+	BOOL ok = WriteBuffer((BYTE*)&(u.u_INT64),sizeof(u.u_INT64));
 	LEAVEWRITEFUNCTION;
 	return (ok);
 }	
@@ -1323,22 +1325,25 @@
 
 BOOL CXaraFileRecord::ReadFLOAT(FLOAT* pf)
 {
-	BOOL ok = ReadBuffer((BYTE*)pf,sizeof(FLOAT));
-	*pf = LEtoNative(*pf);
+	FloatUnion f;
+	BOOL ok = ReadBuffer((BYTE*)&(f.u_INT32),sizeof(f.u_INT32));
+	*pf = LEtoNative(f);
 	return ok;
 }
 
 BOOL CXaraFileRecord::ReadDOUBLE(double* pd)
 {
-	BOOL ok = ReadBuffer((BYTE*)pd,sizeof(double));
-	*pd = LEtoNative(*pd);
+	DoubleUnion f;
+	BOOL ok = ReadBuffer((BYTE*)&(f.u_INT64),sizeof(f.u_INT64));
+	*pd = LEtoNative(f);
 	return ok;
 }
 
 BOOL CXaraFileRecord::ReadDOUBLEnoError(double* pd)
 {
-	BOOL ok = ReadBuffernoError((BYTE*)pd,sizeof(double));
-	*pd = LEtoNative(*pd);
+	DoubleUnion f;
+	BOOL ok = ReadBuffernoError((BYTE*)&(f.u_INT64),sizeof(f.u_INT64));
+	*pd = LEtoNative(f);
 	return ok;
 }
 
Index: wxOil/hardwaremanager.h
===================================================================
--- wxOil/hardwaremanager.h	(revision 1694)
+++ wxOil/hardwaremanager.h	(revision 1695)
@@ -115,6 +115,19 @@
 
 namespace oilHardwareManager
 {
+	// Avoid aliasing problems
+	union DoubleUnion
+	{
+		double u_double;
+		INT64 u_INT64;
+	};
+
+	union FloatUnion
+	{
+		float u_float;
+		INT32 u_INT32;
+	};
+
 // Byte ordering functions
 #if defined(WORDS_BIGENDIAN)
 	// __BIG_ENDIAN__
@@ -158,16 +171,28 @@
 	static inline FIXED16 NativetoBE(FIXED16 n) {return n;}
 	static inline FIXED16 NativetoLE(FIXED16 n) {return FIXED16::FromRawLong(wxINT32_SWAP_ALWAYS(n.GetRawLong()));}
 
-	static inline float BEtoNative(float n) {return n;}
-	static inline float LEtoNative(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
-	static inline float NativetoBE(float n) {return n;}
-	static inline float NativetoLE(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
+	// AMB is not sure these are are a great idea as they rely on floats/doubles being set up with invalid data
+//	static inline float BEtoNative(float n) {return n;}
+//	static inline float LEtoNative(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
+//	static inline float NativetoBE(float n) {return n;}
+//	static inline float NativetoLE(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
 
-	static inline double BEtoNative(double n) {return n;}
-	static inline double LEtoNative(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
-	static inline double NativetoBE(double n) {return n;}
-	static inline double NativetoLE(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
+	// AMB is not sure these are are a great idea as they rely on floats/doubles being set up with invalid data
+//	static inline double BEtoNative(double n) {return n;}
+//	static inline double LEtoNative(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
+//	static inline double NativetoBE(double n) {return n;}
+//	static inline double NativetoLE(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
 
+	static inline float BEtoNative(FloatUnion n) {return n.u_float;}
+	static inline float LEtoNative(FloatUnion n) {n.u_INT32 = wxINT32_SWAP_ALWAYS(n.u_INT32); return n.u_float;}
+	static inline FloatUnion NativetoBEU(float n) {FloatUnion f; f.u_float=n; return f;}
+	static inline FloatUnion NativetoLEU(float n) {FloatUnion f; f.u_float=n; f.u_INT32 = wxINT32_SWAP_ALWAYS(f.u_INT32); return f;}
+
+	static inline double BEtoNative(DoubleUnion n) {return n.u_double;}
+	static inline double LEtoNative(DoubleUnion n) {n.u_INT64 = wxINT64_SWAP_ALWAYS(n.u_INT64); return n.u_double;}
+	static inline DoubleUnion NativetoBEU(double n) {DoubleUnion f; f.u_double=n; return f;}
+	static inline DoubleUnion NativetoLEU(double n) {DoubleUnion f; f.u_double=n; f.u_INT64 = wxINT64_SWAP_ALWAYS(f.u_INT64); return f;}
+
 #else
 	// __LITTLE_ENDIAN__
 	// Little-Endian, signed
@@ -210,16 +235,27 @@
 	static inline FIXED16 NativetoBE(FIXED16 n) {return FIXED16::FromRawLong(wxINT32_SWAP_ALWAYS(n.GetRawLong()));}
 	static inline FIXED16 NativetoLE(FIXED16 n) {return n;}
 
-	static inline float BEtoNative(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
-	static inline float LEtoNative(float n) {return n;}
-	static inline float NativetoBE(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
-	static inline float NativetoLE(float n) {return n;}
+	// AMB is not sure these are are a great idea as they rely on floats/doubles being set up with invalid data
+//	static inline float BEtoNative(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
+//	static inline float LEtoNative(float n) {return n;}
+//	static inline float NativetoBE(float n) {return n;} //{return reinterpret_cast<float>(wxINT32_SWAP_ALWAYS(reinterpret_cast<INT32>(n)));}
+//	static inline float NativetoLE(float n) {return n;}
 
-	static inline double BEtoNative(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
-	static inline double LEtoNative(double n) {return n;}
-	static inline double NativetoBE(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
-	static inline double NativetoLE(double n) {return n;}
+	// AMB is not sure these are are a great idea as they rely on floats/doubles being set up with invalid data
+//	static inline double BEtoNative(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
+//	static inline double LEtoNative(double n) {return n;}
+//	static inline double NativetoBE(double n) {return n;} //{return reinterpret_cast<double>(wxINT64_SWAP_ALWAYS(reinterpret_cast<INT64>(n)));}
+//	static inline double NativetoLE(double n) {return n;}
 
+	static inline float BEtoNative(FloatUnion n) {n.u_INT32 = wxINT32_SWAP_ALWAYS(n.u_INT32); return n.u_float;}
+	static inline float LEtoNative(FloatUnion n) {return n.u_float;}
+	static inline FloatUnion NativetoBEU(float n) {FloatUnion f; f.u_float=n; f.u_INT32 = wxINT32_SWAP_ALWAYS(f.u_INT32); return f;}
+	static inline FloatUnion NativetoLEU(float n) {FloatUnion f; f.u_float=n; return f;}
+
+	static inline double BEtoNative(DoubleUnion n) {n.u_INT64 = wxINT64_SWAP_ALWAYS(n.u_INT64); return n.u_double;}
+	static inline double LEtoNative(DoubleUnion n) {return n.u_double;}
+	static inline DoubleUnion NativetoBEU(double n) {DoubleUnion f; f.u_double=n; f.u_INT64 = wxINT64_SWAP_ALWAYS(f.u_INT64); return f;}
+	static inline DoubleUnion NativetoLEU(double n) {DoubleUnion f; f.u_double=n; return f;}
 #endif
 
 // -------------------------------------------------------------------------------