Skip to content

Commit

Permalink
byte order: move declarations into namespace mediascanner
Browse files Browse the repository at this point in the history
  • Loading branch information
janbar committed Feb 14, 2024
1 parent 30ca400 commit 363187b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
3 changes: 2 additions & 1 deletion backend/NosonMediaScanner/byteorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "byteorder.h"

static int test_endianess() {
unsigned int test = 1;
return (*((char*)(&test))) ? LITTLE_ENDIAN : BIG_ENDIAN;
}

int __endianess__ = test_endianess();
int mediascanner::__endianess__ = test_endianess();
51 changes: 32 additions & 19 deletions backend/NosonMediaScanner/byteorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#ifndef BYTEORDER_H
#define BYTEORDER_H

#include <cinttypes>
#include <cstring>

namespace mediascanner
{

#ifndef BYTE_ORDER
#define LITTLE_ENDIAN 1234
#define BIG_ENDIAN 4321
Expand All @@ -30,7 +34,6 @@ extern int __endianess__;
#define is_big_endian (BYTE_ORDER == BIG_ENDIAN)
#define is_little_endian (BYTE_ORDER == LITTLE_ENDIAN)


template <typename T> inline T fromUnaligned(const void * data)
{
T val;
Expand All @@ -43,24 +46,24 @@ template <typename T> inline void toUnaligned(void * data, T val)
memcpy(data, &val, sizeof(T));
}

static inline int8_t read8(const void * data)
inline int8_t read8(const void * data)
{
return fromUnaligned<int8_t>(data);
}

static inline void write8(void * data, int8_t val)
inline void write8(void * data, int8_t val)
{
toUnaligned(data, val);
}

static inline int16_t swap16(int16_t val)
inline int16_t swap16(int16_t val)
{
return (int16_t)(0 |
(((uint32_t)val & 0x00ff) << 8) |
(((uint32_t)val & 0xff00) >> 8));
}

static inline int32_t swap32(int32_t val)
inline int32_t swap32(int32_t val)
{
return (int32_t)(0 |
(((uint32_t)val & 0x000000ff) << 24) |
Expand All @@ -70,83 +73,93 @@ static inline int32_t swap32(int32_t val)
);
}

static inline int16_t read16le(const void * data)
inline int16_t read16le(const void * data)
{
int16_t val = fromUnaligned<int16_t>(data);
if (is_little_endian)
return val;
return swap16(val);
}

static inline void write16le(void * data, int16_t val)
inline void write16le(void * data, int16_t val)
{
if (is_little_endian)
toUnaligned(data, val);
else
toUnaligned(data, swap16(val));
}

static inline int16_t read16be(const void * data)
inline int16_t read16be(const void * data)
{
int16_t val = fromUnaligned<int16_t>(data);
if (is_big_endian)
return val;
return swap16(val);
}

static inline void write16be(void * data, int16_t val)
inline void write16be(void * data, int16_t val)
{
if (is_big_endian)
toUnaligned(data, val);
else
toUnaligned(data, swap16(val));
}

static inline int32_t read24le(const void * data)
inline int32_t read24le(const void * data)
{
const char * p = (const char*)data;
uint8_t val = fromUnaligned<uint8_t>(&p[0]);
return ((int32_t)read16le(&p[1]) * 256) | (val & 0xff);
int32_t val = fromUnaligned<uint8_t>(&p[2]) << 16;
val |= fromUnaligned<uint8_t>(&p[1]) << 8;
val |= fromUnaligned<uint8_t>(&p[0]);
if (val & 0x800000)
val |= ~0xffffff;
return val;
}

static inline int32_t read24be(const void * data)
inline int32_t read24be(const void * data)
{
const char * p = (const char*)data;
uint8_t val = fromUnaligned<uint8_t>(&p[2]);
return ((int32_t)read16be(&p[1]) * 256) | (val & 0xff);
int32_t val = fromUnaligned<uint8_t>(&p[0]) << 16;
val |= fromUnaligned<uint8_t>(&p[1]) << 8;
val |= fromUnaligned<uint8_t>(&p[2]);
if (val & 0x800000)
val |= ~0xffffff;
return val;
}

static inline int32_t read32le(const void * data)
inline int32_t read32le(const void * data)
{
int32_t val = fromUnaligned<int32_t>(data);
if (is_little_endian)
return val;
return swap32(val);
}

static inline void write32le(void * data, int32_t val)
inline void write32le(void * data, int32_t val)
{
if (is_little_endian)
toUnaligned(data, val);
else
toUnaligned(data, swap32(val));
}

static inline int32_t read32be(const void * data)
inline int32_t read32be(const void * data)
{
int32_t val = fromUnaligned<int32_t>(data);
if (is_big_endian)
return val;
return swap32(val);
}

static inline void write32be(void * data, int32_t val)
inline void write32be(void * data, int32_t val)
{
if (is_big_endian)
toUnaligned(data, val);
else
toUnaligned(data, swap32(val));
}

}

#endif /* BYTEORDER_H */

0 comments on commit 363187b

Please sign in to comment.