Skip to content

Commit

Permalink
making old endian utils 64-bit safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Supnik committed Jul 9, 2018
1 parent d494741 commit e1a67e3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
29 changes: 15 additions & 14 deletions src/Utils/BitmapUtils.cpp
Expand Up @@ -36,6 +36,7 @@
#include <jasper/jasper.h>
#endif
#include "AssertUtils.h"
#include <stdint.h>

#if IBM
#include "GUI_Unicode.h"
Expand All @@ -61,23 +62,23 @@
struct BMPHeader {
char signature1;
char signature2;
long fileSize;
long reserved;
long dataOffset;
int32_t fileSize;
int32_t reserved;
int32_t dataOffset;
};

struct BMPImageDesc {
long structSize;
long imageWidth;
long imageHeight;
short planes;
short bitCount;
long compressionType;
long imageSize;
long xPixelsPerM; //130B0000? B013 = 45075?
long yPixelsPerM;
long colorsUsed;
long colorsImportant;
int32_t structSize;
int32_t imageWidth;
int32_t imageHeight;
int16_t planes;
int16_t bitCount;
int32_t compressionType;
int32_t imageSize;
int32_t xPixelsPerM; //130B0000? B013 = 45075?
int32_t yPixelsPerM;
int32_t colorsUsed;
int32_t colorsImportant;
};

#if APL
Expand Down
50 changes: 25 additions & 25 deletions src/Utils/EndianUtils.c
Expand Up @@ -30,28 +30,28 @@

#ifndef Endian16_Swap
#define Endian16_Swap(value) \
(((((unsigned short)value)<<8) & 0xFF00) | \
((((unsigned short)value)>>8) & 0x00FF))
(((((uint16_t)value)<<8) & 0xFF00) | \
((((uint16_t)value)>>8) & 0x00FF))
#endif

#ifndef Endian32_Swap
#define Endian32_Swap(value) \
(((((unsigned long)value)<<24) & 0xFF000000) | \
((((unsigned long)value)<< 8) & 0x00FF0000) | \
((((unsigned long)value)>> 8) & 0x0000FF00) | \
((((unsigned long)value)>>24) & 0x000000FF))
(((((uint32_t)value)<<24) & 0xFF000000) | \
((((uint32_t)value)<< 8) & 0x00FF0000) | \
((((uint32_t)value)>> 8) & 0x0000FF00) | \
((((uint32_t)value)>>24) & 0x000000FF))
#endif

#ifndef Endian64_Swap
#define Endian64_Swap(value) \
(((((unsigned long long)value)<<56) & 0xFF00000000000000ULL) | \
((((unsigned long long)value)<<40) & 0x00FF000000000000ULL) | \
((((unsigned long long)value)<<24) & 0x0000FF0000000000ULL) | \
((((unsigned long long)value)<< 8) & 0x000000FF00000000ULL) | \
((((unsigned long long)value)>> 8) & 0x00000000FF000000ULL) | \
((((unsigned long long)value)>>24) & 0x0000000000FF0000ULL) | \
((((unsigned long long)value)>>40) & 0x000000000000FF00ULL) | \
((((unsigned long long)value)>>56) & 0x00000000000000FFULL))
(((((uint64_t)value)<<56) & 0xFF00000000000000ULL) | \
((((uint64_t)value)<<40) & 0x00FF000000000000ULL) | \
((((uint64_t)value)<<24) & 0x0000FF0000000000ULL) | \
((((uint64_t)value)<< 8) & 0x000000FF00000000ULL) | \
((((uint64_t)value)>> 8) & 0x00000000FF000000ULL) | \
((((uint64_t)value)>>24) & 0x0000000000FF0000ULL) | \
((((uint64_t)value)>>40) & 0x000000000000FF00ULL) | \
((((uint64_t)value)>>56) & 0x00000000000000FFULL))
#endif


Expand Down Expand Up @@ -86,16 +86,16 @@ void EndianSwapBuffer(
{
switch(*inFormat) {
case 2:
*((short *) buf) =
Endian16_Swap(*((short *) buf));
*((int16_t *) buf) =
Endian16_Swap(*((int16_t *) buf));
break;
case 4:
*((long *) buf) =
Endian32_Swap(*((long *) buf));
*((int32_t *) buf) =
Endian32_Swap(*((int32_t *) buf));
break;
case 8:
*((long long *) buf) =
Endian64_Swap(*((long long *) buf));
*((int64_t *) buf) =
Endian64_Swap(*((int64_t *) buf));
/* default:
DebugAssert(!"Bad format for EndianSwapBuffer!");
*/
Expand Down Expand Up @@ -130,21 +130,21 @@ void EndianSwapArray(
case 2:
while (inCount--)
{
*((short *) buf) = Endian16_Swap(*((short *) buf));
*((int16_t *) buf) = Endian16_Swap(*((int16_t *) buf));
buf += 2;
}
break;
case 4:
while (inCount--)
{
*((long *) buf) = Endian32_Swap(*((long *) buf));
*((int32_t *) buf) = Endian32_Swap(*((int32_t *) buf));
buf += 4;
}
break;
case 8:
while (inCount--)
{
*((long long *) buf) = Endian64_Swap(*((long long *) buf));
*((int64_t *) buf) = Endian64_Swap(*((int64_t *) buf));
buf += 8;
}
break;
Expand All @@ -154,14 +154,14 @@ void EndianSwapArray(

/* Endian routines for the Mac use Apple's Endian macros. */

void EndianFlipShort(short * ioShort)
void EndianFlipShort(int16_t * ioShort)
{
#if BIG
*ioShort = Endian16_Swap(*ioShort);
#endif
}

void EndianFlipLong(long * ioLong)
void EndianFlipLong(int32_t * ioLong)
{
#if BIG
*ioLong = Endian32_Swap(*ioLong);
Expand Down
6 changes: 4 additions & 2 deletions src/Utils/EndianUtils.h
Expand Up @@ -23,6 +23,8 @@
#ifndef _EndianUtils_h_
#define _EndianUtils_h_

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -72,8 +74,8 @@ PlatformType GetNativePlatformType(void);
*
*/

void EndianFlipShort(short * ioShort);
void EndianFlipLong(long * ioLong);
void EndianFlipShort(int16_t * ioShort);
void EndianFlipLong(int32_t * ioLong);



Expand Down

0 comments on commit e1a67e3

Please sign in to comment.