test changes for big-endian etc.

This commit is contained in:
Syping 2025-10-18 14:41:26 +02:00
parent 72f094958d
commit a3d89d2c5b
2 changed files with 36 additions and 54 deletions

View file

@ -159,21 +159,12 @@ static inline bool writeDataChar(const char *input, char **output)
return true; return true;
} }
static inline uint32_t charToUInt32LE(char *x) static inline uint32_t swapUInt32(uint32_t x)
{ {
return ((unsigned char)(x[3]) << 24 | return (x >> 24 & 0x000000FF) |
(unsigned char)(x[2]) << 16 | (x >> 8 & 0x0000FF00) |
(unsigned char)(x[1]) << 8 | (x << 8 & 0x00FF0000) |
(unsigned char)(x[0])); (x << 24 & 0xFF000000);
}
static inline uint32_t swapToUInt32LE(uint32_t x)
{
unsigned char *y = (unsigned char*)&x;
return (y[3] << 24 |
y[2] << 16 |
y[1] << 8 |
y[0]);
} }
static inline void uInt32ToCharLE(uint32_t x, char *y) static inline void uInt32ToCharLE(uint32_t x, char *y)
@ -245,7 +236,7 @@ void ragephoto_addparser(ragephoto_t instance_t, RagePhotoFormatParser *rp_parse
return; return;
size_t length; size_t length;
for (length = 0; memcmp(&n_parser, &instance->parser[length], sizeof(RagePhotoFormatParser)); length++); for (length = 0; memcmp(&n_parser, &instance->parser[length], sizeof(RagePhotoFormatParser)); length++);
RagePhotoFormatParser *t_parser = (RagePhotoFormatParser*)realloc(instance->parser, (length + 2 * sizeof(RagePhotoFormatParser))); RagePhotoFormatParser *t_parser = (RagePhotoFormatParser*)realloc(instance->parser, (length + 2) * sizeof(RagePhotoFormatParser));
if (!t_parser) if (!t_parser)
return; return;
instance->parser = t_parser; instance->parser = t_parser;
@ -296,7 +287,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size_t pos = 0; size_t pos = 0;
size_t size = readBuffer(data, &rp_data->photoFormat, &pos, 4, length); size_t size = readBuffer(data, &rp_data->photoFormat, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->photoFormat = swapToUInt32LE(rp_data->photoFormat); rp_data->photoFormat = swapUInt32(rp_data->photoFormat);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_NOFORMATIDENTIFIER; // 1 rp_data->error = RAGEPHOTO_ERROR_NOFORMATIDENTIFIER; // 1
@ -351,7 +342,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size = readBuffer(data, &rp_data->headerSum, &pos, 4, length); size = readBuffer(data, &rp_data->headerSum, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->headerSum = swapToUInt32LE(rp_data->headerSum); rp_data->headerSum = swapUInt32(rp_data->headerSum);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETECHECKSUM; // 7 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETECHECKSUM; // 7
@ -374,7 +365,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size = readBuffer(data, &rp_data->headerSum2, &pos, 4, length); size = readBuffer(data, &rp_data->headerSum2, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->headerSum2 = swapToUInt32LE(rp_data->headerSum2); rp_data->headerSum2 = swapUInt32(rp_data->headerSum2);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETECHECKSUM; // 7 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETECHECKSUM; // 7
@ -384,7 +375,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size = readBuffer(data, &rp_data->endOfFile, &pos, 4, length); size = readBuffer(data, &rp_data->endOfFile, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->endOfFile = swapToUInt32LE(rp_data->endOfFile); rp_data->endOfFile = swapUInt32(rp_data->endOfFile);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEEOF; // 8 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEEOF; // 8
@ -393,7 +384,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size = readBuffer(data, &rp_data->jsonOffset, &pos, 4, length); size = readBuffer(data, &rp_data->jsonOffset, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->jsonOffset = swapToUInt32LE(rp_data->jsonOffset); rp_data->jsonOffset = swapUInt32(rp_data->jsonOffset);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEJSONOFFSET; // 9 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEJSONOFFSET; // 9
@ -401,7 +392,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
} }
size = readBuffer(data, &rp_data->titlOffset, &pos, 4, length); size = readBuffer(data, &rp_data->titlOffset, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->titlOffset = swapToUInt32LE(rp_data->titlOffset); rp_data->titlOffset = swapUInt32(rp_data->titlOffset);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETETITLEOFFSET; // 10 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETETITLEOFFSET; // 10
@ -410,7 +401,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size = readBuffer(data, &rp_data->descOffset, &pos, 4, length); size = readBuffer(data, &rp_data->descOffset, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->descOffset = swapToUInt32LE(rp_data->descOffset); rp_data->descOffset = swapUInt32(rp_data->descOffset);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEDESCOFFSET; // 11 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEDESCOFFSET; // 11
@ -430,7 +421,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size = readBuffer(data, &rp_data->jpegBuffer, &pos, 4, length); size = readBuffer(data, &rp_data->jpegBuffer, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->jpegBuffer = swapToUInt32LE(rp_data->jpegBuffer); rp_data->jpegBuffer = swapUInt32(rp_data->jpegBuffer);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEPHOTOBUFFER; // 14 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEPHOTOBUFFER; // 14
@ -439,7 +430,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size = readBuffer(data, &rp_data->jpegSize, &pos, 4, length); size = readBuffer(data, &rp_data->jpegSize, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->jpegSize = swapToUInt32LE(rp_data->jpegSize); rp_data->jpegSize = swapUInt32(rp_data->jpegSize);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEPHOTOSIZE; // 15 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEPHOTOSIZE; // 15
@ -472,7 +463,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size = readBuffer(data, &rp_data->jsonBuffer, &pos, 4, length); size = readBuffer(data, &rp_data->jsonBuffer, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->jsonBuffer = swapToUInt32LE(rp_data->jsonBuffer); rp_data->jsonBuffer = swapUInt32(rp_data->jsonBuffer);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEJSONBUFFER; // 20 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEJSONBUFFER; // 20
@ -504,7 +495,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size = readBuffer(data, &rp_data->titlBuffer, &pos, 4, length); size = readBuffer(data, &rp_data->titlBuffer, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->titlBuffer = swapToUInt32LE(rp_data->titlBuffer); rp_data->titlBuffer = swapUInt32(rp_data->titlBuffer);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETETITLEBUFFER; // 25 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETETITLEBUFFER; // 25
@ -536,7 +527,7 @@ bool ragephotodata_load(RagePhotoData *rp_data, RagePhotoFormatParser *rp_parser
size = readBuffer(data, &rp_data->descBuffer, &pos, 4, length); size = readBuffer(data, &rp_data->descBuffer, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->descBuffer = swapToUInt32LE(rp_data->descBuffer); rp_data->descBuffer = swapUInt32(rp_data->descBuffer);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEDESCBUFFER; // 30 rp_data->error = RAGEPHOTO_ERROR_INCOMPLETEDESCBUFFER; // 30

View file

@ -153,21 +153,12 @@ inline bool writeDataChar(const char *input, char **output)
return true; return true;
} }
inline uint32_t charToUInt32LE(char *x) inline uint32_t swapUInt32(uint32_t x)
{ {
return (static_cast<unsigned char>(x[3]) << 24 | return (x >> 24 & 0x000000FF) |
static_cast<unsigned char>(x[2]) << 16 | (x >> 8 & 0x0000FF00) |
static_cast<unsigned char>(x[1]) << 8 | (x << 8 & 0x00FF0000) |
static_cast<unsigned char>(x[0])); (x << 24 & 0xFF000000);
}
inline uint32_t swapToUInt32LE(uint32_t x)
{
unsigned char *y = reinterpret_cast<unsigned char*>(&x);
return (y[3] << 24 |
y[2] << 16 |
y[1] << 8 |
y[0]);
} }
inline void uInt32ToCharLE(uint32_t x, char *y) inline void uInt32ToCharLE(uint32_t x, char *y)
@ -227,7 +218,7 @@ void RagePhoto::addParser(RagePhotoFormatParser *rp_parser)
return; return;
size_t length; size_t length;
for (length = 0; memcmp(&n_parser[0], &m_parser[length], sizeof(RagePhotoFormatParser)); length++); for (length = 0; memcmp(&n_parser[0], &m_parser[length], sizeof(RagePhotoFormatParser)); length++);
RagePhotoFormatParser *t_parser = static_cast<RagePhotoFormatParser*>(realloc(m_parser, (length + 2 * sizeof(RagePhotoFormatParser)))); RagePhotoFormatParser *t_parser = static_cast<RagePhotoFormatParser*>(realloc(m_parser, (length + 2) * sizeof(RagePhotoFormatParser)));
if (!t_parser) if (!t_parser)
throw std::runtime_error("RagePhotoFormatParser array can't be expanded"); throw std::runtime_error("RagePhotoFormatParser array can't be expanded");
m_parser = t_parser; m_parser = t_parser;
@ -269,7 +260,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size_t pos = 0; size_t pos = 0;
size_t size = readBuffer(data, &rp_data->photoFormat, &pos, 4, length); size_t size = readBuffer(data, &rp_data->photoFormat, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->photoFormat = swapToUInt32LE(rp_data->photoFormat); rp_data->photoFormat = swapUInt32(rp_data->photoFormat);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::NoFormatIdentifier; // 1 rp_data->error = Error::NoFormatIdentifier; // 1
@ -338,7 +329,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size = readBuffer(data, &rp_data->headerSum, &pos, 4, length); size = readBuffer(data, &rp_data->headerSum, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->headerSum = swapToUInt32LE(rp_data->headerSum); rp_data->headerSum = swapUInt32(rp_data->headerSum);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompleteChecksum; // 7 rp_data->error = Error::IncompleteChecksum; // 7
@ -360,7 +351,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size = readBuffer(data, &rp_data->headerSum2, &pos, 4, length); size = readBuffer(data, &rp_data->headerSum2, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->headerSum2 = swapToUInt32LE(rp_data->headerSum2); rp_data->headerSum2 = swapUInt32(rp_data->headerSum2);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompleteChecksum; // 7 rp_data->error = Error::IncompleteChecksum; // 7
@ -370,7 +361,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size = readBuffer(data, &rp_data->endOfFile, &pos, 4, length); size = readBuffer(data, &rp_data->endOfFile, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->endOfFile = swapToUInt32LE(rp_data->endOfFile); rp_data->endOfFile = swapUInt32(rp_data->endOfFile);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompleteEOF; // 8 rp_data->error = Error::IncompleteEOF; // 8
@ -379,7 +370,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size = readBuffer(data, &rp_data->jsonOffset, &pos, 4, length); size = readBuffer(data, &rp_data->jsonOffset, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->jsonOffset = swapToUInt32LE(rp_data->jsonOffset); rp_data->jsonOffset = swapUInt32(rp_data->jsonOffset);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompleteJsonOffset; // 9 rp_data->error = Error::IncompleteJsonOffset; // 9
@ -387,7 +378,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
} }
size = readBuffer(data, &rp_data->titlOffset, &pos, 4, length); size = readBuffer(data, &rp_data->titlOffset, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->titlOffset = swapToUInt32LE(rp_data->titlOffset); rp_data->titlOffset = swapUInt32(rp_data->titlOffset);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompleteTitleOffset; // 10 rp_data->error = Error::IncompleteTitleOffset; // 10
@ -396,7 +387,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size = readBuffer(data, &rp_data->descOffset, &pos, 4, length); size = readBuffer(data, &rp_data->descOffset, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->descOffset = swapToUInt32LE(rp_data->descOffset); rp_data->descOffset = swapUInt32(rp_data->descOffset);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompleteDescOffset; // 11 rp_data->error = Error::IncompleteDescOffset; // 11
@ -416,7 +407,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size = readBuffer(data, &rp_data->jpegBuffer, &pos, 4, length); size = readBuffer(data, &rp_data->jpegBuffer, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->jpegBuffer = swapToUInt32LE(rp_data->jpegBuffer); rp_data->jpegBuffer = swapUInt32(rp_data->jpegBuffer);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompletePhotoBuffer; // 14 rp_data->error = Error::IncompletePhotoBuffer; // 14
@ -425,7 +416,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size = readBuffer(data, &rp_data->jpegSize, &pos, 4, length); size = readBuffer(data, &rp_data->jpegSize, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->jpegSize = swapToUInt32LE(rp_data->jpegSize); rp_data->jpegSize = swapUInt32(rp_data->jpegSize);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompletePhotoSize; // 15 rp_data->error = Error::IncompletePhotoSize; // 15
@ -458,7 +449,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size = readBuffer(data, &rp_data->jsonBuffer, &pos, 4, length); size = readBuffer(data, &rp_data->jsonBuffer, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->jsonBuffer = swapToUInt32LE(rp_data->jsonBuffer); rp_data->jsonBuffer = swapUInt32(rp_data->jsonBuffer);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompleteJsonBuffer; // 20 rp_data->error = Error::IncompleteJsonBuffer; // 20
@ -490,7 +481,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size = readBuffer(data, &rp_data->titlBuffer, &pos, 4, length); size = readBuffer(data, &rp_data->titlBuffer, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->titlBuffer = swapToUInt32LE(rp_data->titlBuffer); rp_data->titlBuffer = swapUInt32(rp_data->titlBuffer);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompleteTitleBuffer; // 25 rp_data->error = Error::IncompleteTitleBuffer; // 25
@ -522,7 +513,7 @@ bool RagePhoto::load(const char *data, size_t length, RagePhotoData *rp_data, Ra
size = readBuffer(data, &rp_data->descBuffer, &pos, 4, length); size = readBuffer(data, &rp_data->descBuffer, &pos, 4, length);
#ifndef LIBRAGEPHOTO_LITTLE_ENDIAN #ifndef LIBRAGEPHOTO_LITTLE_ENDIAN
rp_data->descBuffer = swapToUInt32LE(rp_data->descBuffer); rp_data->descBuffer = swapUInt32(rp_data->descBuffer);
#endif #endif
if (size != 4) { if (size != 4) {
rp_data->error = Error::IncompleteDescBuffer; // 30 rp_data->error = Error::IncompleteDescBuffer; // 30