2011-12-14 00:23:17 +03:00
|
|
|
/*
|
|
|
|
* vcmi_endian.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2017-07-13 10:26:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-12-29 12:06:45 +02:00
|
|
|
//FIXME:library file depends on SDL - may cause troubles
|
2017-07-13 10:26:03 +02:00
|
|
|
#include <SDL_endian.h>
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
/* Reading values from memory.
|
|
|
|
*
|
|
|
|
* read_le_u16, read_le_u32 : read a little endian value from
|
|
|
|
* memory. On big endian machines, the value will be byteswapped.
|
|
|
|
*/
|
|
|
|
|
2022-12-29 12:06:34 +02:00
|
|
|
#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#define PACKED_STRUCT_BEGIN __pragma( pack(push, 1) )
|
|
|
|
#define PACKED_STRUCT_END __pragma( pack(pop) )
|
|
|
|
#else
|
|
|
|
#define PACKED_STRUCT_BEGIN
|
|
|
|
#define PACKED_STRUCT_END __attribute__(( packed ))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PACKED_STRUCT_BEGIN struct unaligned_Uint16 { ui16 val; } PACKED_STRUCT_END;
|
|
|
|
PACKED_STRUCT_BEGIN struct unaligned_Uint32 { ui32 val; } PACKED_STRUCT_END;
|
2011-12-14 00:23:17 +03:00
|
|
|
|
|
|
|
static inline ui16 read_unaligned_u16(const void *p)
|
|
|
|
{
|
|
|
|
const struct unaligned_Uint16 *v = reinterpret_cast<const struct unaligned_Uint16 *>(p);
|
|
|
|
return v->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ui32 read_unaligned_u32(const void *p)
|
|
|
|
{
|
|
|
|
const struct unaligned_Uint32 *v = reinterpret_cast<const struct unaligned_Uint32 *>(p);
|
|
|
|
return v->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define read_le_u16(p) (SDL_SwapLE16(read_unaligned_u16(p)))
|
|
|
|
#define read_le_u32(p) (SDL_SwapLE32(read_unaligned_u32(p)))
|
|
|
|
|
|
|
|
#else
|
2013-02-05 22:56:28 +03:00
|
|
|
|
2022-12-29 12:06:34 +02:00
|
|
|
#warning UB: unaligned memory access
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
#define read_le_u16(p) (SDL_SwapLE16(* reinterpret_cast<const ui16 *>(p)))
|
|
|
|
#define read_le_u32(p) (SDL_SwapLE32(* reinterpret_cast<const ui32 *>(p)))
|
2013-02-05 22:56:28 +03:00
|
|
|
|
2022-12-29 12:06:34 +02:00
|
|
|
#define PACKED_STRUCT_BEGIN
|
|
|
|
#define PACKED_STRUCT_END
|
2013-02-05 22:56:28 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
#endif
|
2012-08-02 14:03:26 +03:00
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
static inline char readChar(const ui8 * buffer, int & i)
|
2012-08-02 14:03:26 +03:00
|
|
|
{
|
2012-11-03 16:30:47 +03:00
|
|
|
return buffer[i++];
|
2012-08-02 14:03:26 +03:00
|
|
|
}
|
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
static inline std::string readString(const ui8 * buffer, int & i)
|
2012-08-02 14:03:26 +03:00
|
|
|
{
|
2012-11-03 16:30:47 +03:00
|
|
|
int len = read_le_u32(buffer + i);
|
|
|
|
i += 4;
|
2012-08-02 14:03:26 +03:00
|
|
|
assert(len >= 0 && len <= 500000); //not too long
|
2012-11-03 16:30:47 +03:00
|
|
|
std::string ret;
|
|
|
|
ret.reserve(len);
|
|
|
|
for(int gg = 0; gg < len; ++gg)
|
2012-08-02 14:03:26 +03:00
|
|
|
{
|
2012-11-03 16:30:47 +03:00
|
|
|
ret += buffer[i++];
|
2012-08-02 14:03:26 +03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|