aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorNorbert Lange2009-08-24 13:08:21 +0000
committerNorbert Lange2009-08-24 13:08:21 +0000
commit54ef7a892ba9460c21fdb7bb5cc72fedc9d4b8cd (patch)
tree885efa86a34742fc63c2d0489c32a973e7c46b34 /common
parent184aae3c8c863541771fbeb09ce5f1a72ecabb95 (diff)
downloadscummvm-rg350-54ef7a892ba9460c21fdb7bb5cc72fedc9d4b8cd.tar.gz
scummvm-rg350-54ef7a892ba9460c21fdb7bb5cc72fedc9d4b8cd.tar.bz2
scummvm-rg350-54ef7a892ba9460c21fdb7bb5cc72fedc9d4b8cd.zip
reverting changes from patch 43696 that shouldnt have been committed
svn-id: r43697
Diffstat (limited to 'common')
-rw-r--r--common/endian.h302
-rw-r--r--common/stream.h101
2 files changed, 135 insertions, 268 deletions
diff --git a/common/endian.h b/common/endian.h
index 2e52af139b..c889371a2f 100644
--- a/common/endian.h
+++ b/common/endian.h
@@ -32,47 +32,23 @@
// Endian conversion functions, macros etc., follow from here!
//
-// Sanity check
-#if !defined(SCUMM_LITTLE_ENDIAN) && !defined(SCUMM_BIG_ENDIAN)
-# error No endianness defined
-#endif
-
/**
* Swap the bytes in a 32 bit word in order to convert LE encoded data to BE
* and vice versa.
- * compilerspecific variants come first, fallback last
*/
-
-// Test for GCC >= 4.3.0 as this version added the bswap builtin
-#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
-
- FORCEINLINE uint32 SWAP_BYTES_32(uint32 a) {
- return __builtin_bswap32(a);
- }
-
-// test for MSVC 7 or newer
-#elif defined(_MSC_VER) && _MSC_VER >= 1300
-
- FORCEINLINE uint32 SWAP_BYTES_32(uint32 a) {
- return _byteswap_ulong(a);
- }
-
-// generic fallback
-#else
-
- inline uint32 SWAP_BYTES_32(uint32 a) {
- const uint16 low = (uint16)a, high = (uint16)(a >> 16);
- return ((uint32)(uint16)((low >> 8) | (low << 8)) << 16)
- | (uint16)((high >> 8) | (high << 8));
- }
-#endif
+FORCEINLINE uint32 SWAP_BYTES_32(uint32 a) {
+ return ((a >> 24) & 0x000000FF) |
+ ((a >> 8) & 0x0000FF00) |
+ ((a << 8) & 0x00FF0000) |
+ ((a << 24) & 0xFF000000);
+}
/**
* Swap the bytes in a 16 bit word in order to convert LE encoded data to BE
* and vice versa.
*/
-FORCEINLINE uint16 SWAP_BYTES_16(const uint16 a) {
- return (a >> 8) | (a << 8);
+FORCEINLINE uint16 SWAP_BYTES_16(uint16 a) {
+ return ((a >> 8) & 0x00FF) + ((a << 8) & 0xFF00);
}
@@ -94,123 +70,25 @@ FORCEINLINE uint16 SWAP_BYTES_16(const uint16 a) {
* For the latter systems we provide the INVERSE_MKID override.
*/
#if defined(INVERSE_MKID)
-#define MKID_BE(a) ((uint32)( \
- (((a) >> 24) & 0x00FF) | \
- (((a) >> 8) & 0xFF00) | \
- (((a) & 0xFF00) << 8) | \
- (((a) & 0x00FF) << 24) ))
+#define MKID_BE(a) ((uint32) \
+ (((a) >> 24) & 0x000000FF) | \
+ (((a) >> 8) & 0x0000FF00) | \
+ (((a) << 8) & 0x00FF0000) | \
+ (((a) << 24) & 0xFF000000))
#else
# define MKID_BE(a) ((uint32)(a))
#endif
-// Functions for reading/writing native Integers,
-// this transparently handles the need for alignment
-
-#if !defined(SCUMM_NEED_ALIGNMENT)
-
- FORCEINLINE uint16 READ_UINT16(const void *ptr) {
- return *(const uint16 *)(ptr);
- }
-
- FORCEINLINE uint32 READ_UINT32(const void *ptr) {
- return *(const uint32 *)(ptr);
- }
-
- FORCEINLINE void WRITE_UINT16(void *ptr, uint16 value) {
- *(uint16 *)(ptr) = value;
- }
-
- FORCEINLINE void WRITE_UINT32(void *ptr, uint32 value) {
- *(uint32 *)(ptr) = value;
- }
-
-// test for GCC >= 4.0. these implementations will automatically use CPU-specific
-// instructions for unaligned data when they are available (eg. MIPS)
-#elif defined(__GNUC__) && (__GNUC__ >= 4)
-
- FORCEINLINE uint16 READ_UINT16(const void *ptr) {
- struct Unaligned16 { uint16 val; } __attribute__ ((__packed__));
- return ((const Unaligned16 *)ptr)->val;
- }
-
- FORCEINLINE uint32 READ_UINT32(const void *ptr) {
- struct Unaligned32 { uint32 val; } __attribute__ ((__packed__));
- return ((const Unaligned32 *)ptr)->val;
- }
-
- FORCEINLINE void WRITE_UINT16(void *ptr, uint16 value) {
- struct Unaligned16 { uint16 val; } __attribute__ ((__packed__));
- ((Unaligned16 *)ptr)->val = value;
- }
-
- FORCEINLINE void WRITE_UINT32(void *ptr, uint32 value) {
- struct Unaligned32 { uint32 val; } __attribute__ ((__packed__));
- ((Unaligned32 *)ptr)->val = value;
- }
-
-// use software fallback by loading each byte explicitely
-#else
-
-# if defined(SCUMM_LITTLE_ENDIAN)
-
- FORCEINLINE uint16 READ_UINT16(const void *ptr) {
- const uint8 *b = (const uint8 *)ptr;
- return (b[1] << 8) | b[0];
- }
- inline uint32 READ_UINT32(const void *ptr) {
- const uint8 *b = (const uint8 *)ptr;
- return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);
- }
- FORCEINLINE void WRITE_UINT16(void *ptr, uint16 value) {
- uint8 *b = (uint8 *)ptr;
- b[0] = (uint8)(value >> 0);
- b[1] = (uint8)(value >> 8);
- }
- inline void WRITE_UINT32(void *ptr, uint32 value) {
- uint8 *b = (uint8 *)ptr;
- b[0] = (uint8)(value >> 0);
- b[1] = (uint8)(value >> 8);
- b[2] = (uint8)(value >> 16);
- b[3] = (uint8)(value >> 24);
- }
-
-# elif defined(SCUMM_BIG_ENDIAN)
-
- FORCEINLINE uint16 READ_UINT16(const void *ptr) {
- const uint8 *b = (const uint8 *)ptr;
- return (b[0] << 8) | b[1];
- }
- inline uint32 READ_UINT32(const void *ptr) {
- const uint8 *b = (const uint8 *)ptr;
- return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
- }
- FORCEINLINE void WRITE_UINT16(void *ptr, uint16 value) {
- uint8 *b = (uint8 *)ptr;
- b[0] = (uint8)(value >> 8);
- b[1] = (uint8)(value >> 0);
- }
- inline void WRITE_UINT32(void *ptr, uint32 value) {
- uint8 *b = (uint8 *)ptr;
- b[0] = (uint8)(value >> 24);
- b[1] = (uint8)(value >> 16);
- b[2] = (uint8)(value >> 8);
- b[3] = (uint8)(value >> 0);
- }
-
-# endif
-#endif
-
-// Map Funtions for reading/writing BE/LE integers depending on native endianess
#if defined(SCUMM_LITTLE_ENDIAN)
- #define READ_LE_UINT16(a) READ_UINT16(a)
- #define READ_LE_UINT32(a) READ_UINT32(a)
+ #define READ_UINT16(a) READ_LE_UINT16(a)
+ #define READ_UINT32(a) READ_LE_UINT32(a)
- #define WRITE_LE_UINT16(a, v) WRITE_UINT16(a, v)
- #define WRITE_LE_UINT32(a, v) WRITE_UINT32(a, v)
+ #define WRITE_UINT16(a, v) WRITE_LE_UINT16(a, v)
+ #define WRITE_UINT32(a, v) WRITE_LE_UINT32(a, v)
#define FROM_LE_32(a) ((uint32)(a))
#define FROM_LE_16(a) ((uint16)(a))
@@ -224,57 +102,16 @@ FORCEINLINE uint16 SWAP_BYTES_16(const uint16 a) {
#define TO_BE_32(a) SWAP_BYTES_32(a)
#define TO_BE_16(a) SWAP_BYTES_16(a)
-# if defined(SCUMM_NEED_ALIGNMENT)
-
- FORCEINLINE uint16 READ_BE_UINT16(const void *ptr) {
- const uint8 *b = (const uint8 *)ptr;
- return (b[0] << 8) | b[1];
- }
- inline uint32 READ_BE_UINT32(const void *ptr) {
- const uint8 *b = (const uint8 *)ptr;
- return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | (b[3]);
- }
- FORCEINLINE void WRITE_BE_UINT16(void *ptr, uint16 value) {
- uint8 *b = (uint8 *)ptr;
- b[0] = (uint8)(value >> 8);
- b[1] = (uint8)(value >> 0);
- }
- inline void WRITE_BE_UINT32(void *ptr, uint32 value) {
- uint8 *b = (uint8 *)ptr;
- b[0] = (uint8)(value >> 24);
- b[1] = (uint8)(value >> 16);
- b[2] = (uint8)(value >> 8);
- b[3] = (uint8)(value >> 0);
- }
-# else
-
- FORCEINLINE uint16 READ_BE_UINT16(const void *ptr) {
- return SWAP_BYTES_16(*(const uint16 *)ptr);
- }
- FORCEINLINE uint32 READ_BE_UINT32(const void *ptr) {
- return SWAP_BYTES_32(*(const uint32 *)ptr);
- }
- FORCEINLINE void WRITE_BE_UINT16(void *ptr, uint16 value) {
- *(uint16 *)ptr = SWAP_BYTES_16(value);
- }
- FORCEINLINE void WRITE_BE_UINT32(void *ptr, uint32 value) {
- *(uint32 *)ptr = SWAP_BYTES_32(value);
- }
-
-# endif // if defined(SCUMM_NEED_ALIGNMENT)
-
#elif defined(SCUMM_BIG_ENDIAN)
- // I thought this would be compiler-specific and not dependent
- // on endianess after the comments above?
#define MKID(a) ((uint32)(a))
#define MKID_BE(a) ((uint32)(a))
- #define READ_BE_UINT16(a) READ_UINT16(a)
- #define READ_BE_UINT32(a) READ_UINT32(a)
+ #define READ_UINT16(a) READ_BE_UINT16(a)
+ #define READ_UINT32(a) READ_BE_UINT32(a)
- #define WRITE_BE_UINT16(a, v) WRITE_UINT16(a, v)
- #define WRITE_BE_UINT32(a, v) WRITE_UINT32(a, v)
+ #define WRITE_UINT16(a, v) WRITE_BE_UINT16(a, v)
+ #define WRITE_UINT32(a, v) WRITE_BE_UINT32(a, v)
#define FROM_LE_32(a) SWAP_BYTES_32(a)
#define FROM_LE_16(a) SWAP_BYTES_16(a)
@@ -288,55 +125,96 @@ FORCEINLINE uint16 SWAP_BYTES_16(const uint16 a) {
#define TO_BE_32(a) ((uint32)(a))
#define TO_BE_16(a) ((uint16)(a))
-# if defined(SCUMM_NEED_ALIGNMENT)
+#else
+
+ #error No endianness defined
+
+
+#endif
+
+#if defined(SCUMM_NEED_ALIGNMENT) || !defined(SCUMM_LITTLE_ENDIAN)
FORCEINLINE uint16 READ_LE_UINT16(const void *ptr) {
- const uint8 *b = (const uint8 *)ptr;
- return (b[1] << 8) | b[0];
+ const byte *b = (const byte *)ptr;
+ return (b[1] << 8) + b[0];
}
- inline uint32 READ_LE_UINT32(const void *ptr) {
- const uint8 *b = (const uint8 *)ptr;
- return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | (b[0]);
+ FORCEINLINE uint32 READ_LE_UINT32(const void *ptr) {
+ const byte *b = (const byte *)ptr;
+ return (b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0]);
}
FORCEINLINE void WRITE_LE_UINT16(void *ptr, uint16 value) {
- uint8 *b = (uint8 *)ptr;
- b[0] = (uint8)(value >> 0);
- b[1] = (uint8)(value >> 8);
+ byte *b = (byte *)ptr;
+ b[0] = (byte)(value >> 0);
+ b[1] = (byte)(value >> 8);
}
- inline void WRITE_LE_UINT32(void *ptr, uint32 value) {
- uint8 *b = (uint8 *)ptr;
- b[0] = (uint8)(value >> 0);
- b[1] = (uint8)(value >> 8);
- b[2] = (uint8)(value >> 16);
- b[3] = (uint8)(value >> 24);
+ FORCEINLINE void WRITE_LE_UINT32(void *ptr, uint32 value) {
+ byte *b = (byte *)ptr;
+ b[0] = (byte)(value >> 0);
+ b[1] = (byte)(value >> 8);
+ b[2] = (byte)(value >> 16);
+ b[3] = (byte)(value >> 24);
}
-# else
-
+#else
FORCEINLINE uint16 READ_LE_UINT16(const void *ptr) {
- return SWAP_BYTES_16(*(const uint16 *)ptr);
+ return *(const uint16 *)(ptr);
}
FORCEINLINE uint32 READ_LE_UINT32(const void *ptr) {
- return SWAP_BYTES_32(*(const uint32 *)ptr);
+ return *(const uint32 *)(ptr);
}
FORCEINLINE void WRITE_LE_UINT16(void *ptr, uint16 value) {
- *(uint16 *)ptr = SWAP_BYTES_16(value);
+ *(uint16 *)(ptr) = value;
}
FORCEINLINE void WRITE_LE_UINT32(void *ptr, uint32 value) {
- *(uint32 *)ptr = SWAP_BYTES_32(value);
+ *(uint32 *)(ptr) = value;
}
-
-# endif // if defined(SCUMM_NEED_ALIGNMENT)
+#endif
+
-#endif // if defined(SCUMM_LITTLE_ENDIAN)
+#if defined(SCUMM_NEED_ALIGNMENT) || !defined(SCUMM_BIG_ENDIAN)
+ FORCEINLINE uint16 READ_BE_UINT16(const void *ptr) {
+ const byte *b = (const byte *)ptr;
+ return (b[0] << 8) + b[1];
+ }
+ FORCEINLINE uint32 READ_BE_UINT32(const void *ptr) {
+ const byte *b = (const byte*)ptr;
+ return (b[0] << 24) + (b[1] << 16) + (b[2] << 8) + (b[3]);
+ }
+ FORCEINLINE void WRITE_BE_UINT16(void *ptr, uint16 value) {
+ byte *b = (byte *)ptr;
+ b[0] = (byte)(value >> 8);
+ b[1] = (byte)(value >> 0);
+ }
+ FORCEINLINE void WRITE_BE_UINT32(void *ptr, uint32 value) {
+ byte *b = (byte *)ptr;
+ b[0] = (byte)(value >> 24);
+ b[1] = (byte)(value >> 16);
+ b[2] = (byte)(value >> 8);
+ b[3] = (byte)(value >> 0);
+ }
+#else
+ FORCEINLINE uint16 READ_BE_UINT16(const void *ptr) {
+ return *(const uint16 *)(ptr);
+ }
+ FORCEINLINE uint32 READ_BE_UINT32(const void *ptr) {
+ return *(const uint32 *)(ptr);
+ }
+ FORCEINLINE void WRITE_BE_UINT16(void *ptr, uint16 value) {
+ *(uint16 *)(ptr) = value;
+ }
+ FORCEINLINE void WRITE_BE_UINT32(void *ptr, uint32 value) {
+ *(uint32 *)(ptr) = value;
+ }
+#endif
FORCEINLINE uint32 READ_LE_UINT24(const void *ptr) {
- const uint8 *b = (const uint8 *)ptr;
- return (b[2] << 16) | (b[1] << 8) | (b[0]);
+ const byte *b = (const byte *)ptr;
+ return (b[2] << 16) + (b[1] << 8) + (b[0]);
}
FORCEINLINE uint32 READ_BE_UINT24(const void *ptr) {
- const uint8 *b = (const uint8 *)ptr;
- return (b[0] << 16) | (b[1] << 8) | (b[2]);
+ const byte *b = (const byte*)ptr;
+ return (b[0] << 16) + (b[1] << 8) + (b[2]);
}
+
#endif
diff --git a/common/stream.h b/common/stream.h
index 9475f6fa2d..86e8e71134 100644
--- a/common/stream.h
+++ b/common/stream.h
@@ -27,7 +27,6 @@
#define COMMON_STREAM_H
#include "common/scummsys.h"
-#include "common/endian.h"
namespace Common {
@@ -107,38 +106,38 @@ public:
}
void writeUint16LE(uint16 value) {
- value = TO_LE_16(value);
- write(&value, 2);
+ writeByte((byte)(value & 0xff));
+ writeByte((byte)(value >> 8));
}
void writeUint32LE(uint32 value) {
- value = TO_LE_32(value);
- write(&value, 4);
+ writeUint16LE((uint16)(value & 0xffff));
+ writeUint16LE((uint16)(value >> 16));
}
void writeUint16BE(uint16 value) {
- value = TO_BE_16(value);
- write(&value, 2);
+ writeByte((byte)(value >> 8));
+ writeByte((byte)(value & 0xff));
}
void writeUint32BE(uint32 value) {
- value = TO_BE_32(value);
- write(&value, 4);
+ writeUint16BE((uint16)(value >> 16));
+ writeUint16BE((uint16)(value & 0xffff));
}
- FORCEINLINE void writeSint16LE(int16 value) {
+ void writeSint16LE(int16 value) {
writeUint16LE((uint16)value);
}
- FORCEINLINE void writeSint32LE(int32 value) {
+ void writeSint32LE(int32 value) {
writeUint32LE((uint32)value);
}
- FORCEINLINE void writeSint16BE(int16 value) {
+ void writeSint16BE(int16 value) {
writeUint16BE((uint16)value);
}
- FORCEINLINE void writeSint32BE(int32 value) {
+ void writeSint32BE(int32 value) {
writeUint32BE((uint32)value);
}
@@ -189,7 +188,7 @@ public:
* calling err() and eos() ).
*/
byte readByte() {
- byte b = 0; // FIXME: remove initialisation
+ byte b = 0;
read(&b, 1);
return b;
}
@@ -201,7 +200,7 @@ public:
* calling err() and eos() ).
*/
int8 readSByte() {
- int8 b = 0; // FIXME: remove initialisation
+ int8 b = 0;
read(&b, 1);
return b;
}
@@ -214,9 +213,9 @@ public:
* calling err() and eos() ).
*/
uint16 readUint16LE() {
- uint16 val;
- read(&val, 2);
- return FROM_LE_16(val);
+ uint16 a = readByte();
+ uint16 b = readByte();
+ return a | (b << 8);
}
/**
@@ -227,9 +226,9 @@ public:
* calling err() and eos() ).
*/
uint32 readUint32LE() {
- uint32 val;
- read(&val, 4);
- return FROM_LE_32(val);
+ uint32 a = readUint16LE();
+ uint32 b = readUint16LE();
+ return (b << 16) | a;
}
/**
@@ -240,9 +239,9 @@ public:
* calling err() and eos() ).
*/
uint16 readUint16BE() {
- uint16 val;
- read(&val, 2);
- return FROM_BE_16(val);
+ uint16 b = readByte();
+ uint16 a = readByte();
+ return a | (b << 8);
}
/**
@@ -253,9 +252,9 @@ public:
* calling err() and eos() ).
*/
uint32 readUint32BE() {
- uint32 val;
- read(&val, 4);
- return FROM_BE_32(val);
+ uint32 b = readUint16BE();
+ uint32 a = readUint16BE();
+ return (b << 16) | a;
}
/**
@@ -265,7 +264,7 @@ public:
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
- FORCEINLINE int16 readSint16LE() {
+ int16 readSint16LE() {
return (int16)readUint16LE();
}
@@ -276,7 +275,7 @@ public:
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
- FORCEINLINE int32 readSint32LE() {
+ int32 readSint32LE() {
return (int32)readUint32LE();
}
@@ -287,7 +286,7 @@ public:
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
- FORCEINLINE int16 readSint16BE() {
+ int16 readSint16BE() {
return (int16)readUint16BE();
}
@@ -298,7 +297,7 @@ public:
* if a read error occurred (for which client code can check by
* calling err() and eos() ).
*/
- FORCEINLINE int32 readSint32BE() {
+ int32 readSint32BE() {
return (int32)readUint32BE();
}
@@ -461,31 +460,26 @@ public:
* @see SubReadStream
*/
class SeekableSubReadStreamEndian : public SeekableSubReadStream {
-private:
- const bool _bigEndian;
-
public:
+ bool _bigEndian;
+
SeekableSubReadStreamEndian(SeekableReadStream *parentStream, uint32 begin, uint32 end, bool bigEndian = false, bool disposeParentStream = false)
: SeekableSubReadStream(parentStream, begin, end, disposeParentStream), _bigEndian(bigEndian) {
}
- uint16 readUint16() {
- uint16 val;
- read(&val, 2);
- return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val);
+ inline uint16 readUint16() {
+ return (_bigEndian) ? readUint16BE() : readUint16LE();
}
- uint32 readUint32() {
- uint32 val;
- read(&val, 4);
- return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val);
+ inline uint32 readUint32() {
+ return (_bigEndian) ? readUint32BE() : readUint32LE();
}
- FORCEINLINE int16 readSint16() {
+ inline int16 readSint16() {
return (int16)readUint16();
}
- FORCEINLINE int32 readSint32() {
+ inline int32 readSint32() {
return (int32)readUint32();
}
};
@@ -588,28 +582,23 @@ public:
*/
class MemoryReadStreamEndian : public Common::MemoryReadStream {
private:
- const bool _bigEndian;
-
public:
+ bool _bigEndian;
MemoryReadStreamEndian(const byte *buf, uint32 len, bool bigEndian = false) : MemoryReadStream(buf, len), _bigEndian(bigEndian) {}
- uint16 readUint16() {
- uint16 val;
- read(&val, 2);
- return (_bigEndian) ? TO_BE_16(val) : TO_LE_16(val);
+ inline uint16 readUint16() {
+ return (_bigEndian) ? readUint16BE() : readUint16LE();
}
- uint32 readUint32() {
- uint32 val;
- read(&val, 4);
- return (_bigEndian) ? TO_BE_32(val) : TO_LE_32(val);
+ inline uint32 readUint32() {
+ return (_bigEndian) ? readUint32BE() : readUint32LE();
}
- FORCEINLINE int16 readSint16() {
+ inline int16 readSint16() {
return (int16)readUint16();
}
- FORCEINLINE int32 readSint32() {
+ inline int32 readSint32() {
return (int32)readUint32();
}
};