Skip to content

Commit

Permalink
openbsd build: renaming byte order routines
Browse files Browse the repository at this point in the history
  • Loading branch information
janbar committed Feb 15, 2024
1 parent 9bcf5ba commit 681583a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion backend/NosonMediaScanner/byteorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ static int test_endianess() {
return (*((char*)(&test))) ? LITTLE_ENDIAN : BIG_ENDIAN;
}

int __endianess__ = test_endianess();
int machine_bom = test_endianess();
52 changes: 26 additions & 26 deletions backend/NosonMediaScanner/byteorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#ifndef BYTE_ORDER
#define LITTLE_ENDIAN 1234
#define BIG_ENDIAN 4321
extern int __endianess__;
#define BYTE_ORDER __endianess__
extern int machine_bom;
#define BYTE_ORDER machine_bom
#endif
#define is_big_endian (BYTE_ORDER == BIG_ENDIAN)
#define is_little_endian (BYTE_ORDER == LITTLE_ENDIAN)
Expand All @@ -43,24 +43,24 @@ template <typename T> inline void toUnaligned(void * data, T val)
memcpy(data, &val, sizeof(T));
}

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

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

static inline int16_t swap16(int16_t val)
static inline int16_t swap_b16(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)
static inline int32_t swap_b32(int32_t val)
{
return (int32_t)(0 |
(((uint32_t)val & 0x000000ff) << 24) |
Expand All @@ -70,82 +70,82 @@ static inline int32_t swap32(int32_t val)
);
}

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

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

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

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

static inline int32_t read24le(const void * data)
static inline int32_t read_b24le(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);
return ((int32_t)read_b16le(&p[1]) * 256) | (val & 0xff);
}

static inline int32_t read24be(const void * data)
static inline int32_t read_b24be(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);
return ((int32_t)read_b16be(&p[1]) * 256) | (val & 0xff);
}

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

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

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

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

#endif /* BYTEORDER_H */
Expand Down
12 changes: 6 additions & 6 deletions backend/NosonMediaScanner/flacparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bool FLACParser::parse(MediaFile * file, MediaInfo * info, bool debug)
isLast = ((*buf & 0x80) != 0);
unsigned block = *buf & 0x7f;
// get the current block size
unsigned offset = (read32be(buf) & 0xffffff);
unsigned offset = (read_b32be(buf) & 0xffffff);
if (debug)
qDebug("%s: on block type %02x size %u", __FUNCTION__, block, offset);

Expand All @@ -85,11 +85,11 @@ bool FLACParser::parse(MediaFile * file, MediaInfo * info, bool debug)
if (fread(buf, 1, FLAC_BLOCK_SIZE, fp) != FLAC_BLOCK_SIZE)
break;
offset -= FLAC_BLOCK_SIZE;
unsigned stream = read32be(buf + 10) >> 4;
unsigned stream = read_b32be(buf + 10) >> 4;
unsigned sampleRate = (stream & 0xffffff00) >> 8;
unsigned channels = ((stream & 0xe0) >> 5) + 1;
unsigned bitsPerSample = (stream & 0x1f) + 1;
uint64_t samples = ((uint64_t)(buf[13] & 0x0f) << 32) + (uint32_t)read32be(buf + 14);
uint64_t samples = ((uint64_t)(buf[13] & 0x0f) << 32) + (uint32_t)read_b32be(buf + 14);
if (debug)
qDebug("%s: sr:%u ch:%u bps:%u", __FUNCTION__, sampleRate, channels, bitsPerSample);
if (sampleRate == 0)
Expand Down Expand Up @@ -118,12 +118,12 @@ bool FLACParser::parse(MediaFile * file, MediaInfo * info, bool debug)
offset = 0;

unsigned char * vp = vorbis;
vp += read32le(vp) + 4; // pass vendor string
unsigned count = read32le(vp); // comment list length;
vp += read_b32le(vp) + 4; // pass vendor string
unsigned count = read_b32le(vp); // comment list length;
vp += 4;
while (count > 0)
{
unsigned len = read32le(vp);
unsigned len = read_b32le(vp);
vp += 4;
if ((vp + len) > ve)
break; // buffer overflow
Expand Down
12 changes: 6 additions & 6 deletions backend/NosonMediaScanner/id3parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1070,9 +1070,9 @@ static int _parse_vbr_headers(FILE * fp, off_t mpeg_offset, struct mpeg_header *
flags = buf[7];

if (flags & 1)
nframes = read32be(&buf[8]);
nframes = read_b32be(&buf[8]);
if (flags & 2)
size = read32be(&buf[8 + !!(flags & 1) * 4]);
size = read_b32be(&buf[8 + !!(flags & 1) * 4]);

goto proceed;
}
Expand All @@ -1082,11 +1082,11 @@ static int _parse_vbr_headers(FILE * fp, off_t mpeg_offset, struct mpeg_header *
fseek(fp, mpeg_offset + 36, SEEK_SET);
if (fread(buf, 1, sizeof(buf), fp) != sizeof(buf))
return -1;

if (memcmp(buf, "VBRI", 4) == 0 && read16be(buf) == 1)
if (memcmp(buf, "VBRI", 4) == 0 && read_b16be(buf) == 1)
{
size = read32be(&buf[10]);
nframes = read32be(&buf[14]);
size = read_b32be(&buf[10]);
nframes = read_b32be(&buf[14]);

goto proceed;
}
Expand Down
16 changes: 8 additions & 8 deletions backend/NosonMediaScanner/m4aparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ int M4AParser::nextChild(unsigned char * buf, uint64_t * remaining, FILE * fp, u
if (fread(buf, 1, M4A_HEADER_SIZE, fp) == M4A_HEADER_SIZE)
{
*remaining -= M4A_HEADER_SIZE;
*child = (unsigned)read32be(buf + 4);
*childSize = (uint32_t)read32be(buf);
*child = (unsigned)read_b32be(buf + 4);
*childSize = (uint32_t)read_b32be(buf);
if (*childSize == 1)
{
// size of 1 means the real size follows the header in next 8 bytes (64bits)
if (*remaining < 8 || fread(buf, 1, 8, fp) != 8)
return -1; // error
*remaining -= 8;
*childSize = (((uint64_t)read32be(buf) << 32) | (uint32_t)read32be(buf + 4)) - M4A_HEADER_SIZE - 8;
*childSize = (((uint64_t)read_b32be(buf) << 32) | (uint32_t)read_b32be(buf + 4)) - M4A_HEADER_SIZE - 8;
}
else
{
Expand Down Expand Up @@ -155,7 +155,7 @@ int M4AParser::loadDataValue(uint64_t * remaining, FILE * fp, char ** alloc, uns
*remaining -= size;
*allocSize = size;
*alloc = _alloc;
return (read32be(_alloc) & 0x00ffffff); // return datatype
return (read_b32be(_alloc) & 0x00ffffff); // return datatype
}
return r;
}
Expand All @@ -182,12 +182,12 @@ int M4AParser::loadU32Value(uint64_t * remaining, FILE * fp, unsigned * u32)
int r = loadDataValue(remaining, fp, &alloc, &allocSize);
if (r == 0 && allocSize >= 12) // 0 = datatype implicit
{
*u32 = read32be(alloc + 8) & 0xffffffff;
*u32 = read_b32be(alloc + 8) & 0xffffffff;
//qDebug("%s: %u", __FUNCTION__, *u32);
}
else if (r == 2 && allocSize >= 10) // 2 = datatype u16
{
*u32 = read16be(alloc + 8) & 0xffff;
*u32 = read_b16be(alloc + 8) & 0xffff;
//qDebug("%s: %u", __FUNCTION__, *u32);
}
if (alloc)
Expand Down Expand Up @@ -303,8 +303,8 @@ int M4AParser::parse_mvhd(uint64_t * remaining, FILE * fp, MediaInfo * info)
if (*remaining < MVHD_SIZE || fread(buf, 1, MVHD_SIZE, fp) != MVHD_SIZE)
return -1;
*remaining -= MVHD_SIZE;
unsigned scale = read32be(buf + 12);
unsigned duration = read32be(buf + 16);
unsigned scale = read_b32be(buf + 12);
unsigned duration = read_b32be(buf + 16);
if (scale != 0)
info->duration = (int) (duration / scale);
else
Expand Down
24 changes: 12 additions & 12 deletions backend/NosonMediaScanner/oggparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ bool OGGParser::parse(MediaFile * file, MediaInfo * info, bool debug)
return false;
}
//char stream_structure_version = read8(buf + 4);
unsigned char header_type_flag = (unsigned char)read8(buf + 5);
uint64_t granule_position = ((uint64_t)read32le(buf + 6)) + ((uint64_t)read32le(buf + 10) << 32);
unsigned char header_type_flag = (unsigned char)read_b8(buf + 5);
uint64_t granule_position = ((uint64_t)read_b32le(buf + 6)) + ((uint64_t)read_b32le(buf + 10) << 32);
//uint32_t bitstream_serial _number = read32le(buf + 14);
//uint32_t page_sequence_number = (uint32_t)read32le(buf + 18);
//uint32_t CRC_checksum = read32le(buf + 22);
unsigned char number_page_segments = (unsigned char)read8(buf + 26);
unsigned char number_page_segments = (unsigned char)read_b8(buf + 26);

uint32_t segment_table = 0;
if (fread(lacing, 1, number_page_segments, fp) != number_page_segments)
Expand All @@ -85,7 +85,7 @@ bool OGGParser::parse(MediaFile * file, MediaInfo * info, bool debug)
}

for (int i = 0; i < number_page_segments; ++i)
segment_table += (unsigned char)read8(lacing + i);
segment_table += (unsigned char)read_b8(lacing + i);

// bit 0x04: this is the last page of a logical bitstream (eos)
if ((header_type_flag & 0x04) == 0x04)
Expand Down Expand Up @@ -147,7 +147,7 @@ bool OGGParser::parse(MediaFile * file, MediaInfo * info, bool debug)
}

// check for vorbis header
unsigned char block = (unsigned char)read8(packet.data);
unsigned char block = (unsigned char)read_b8(packet.data);
if ((block & 0x01) == 0x01 && packet.datalen > 7 &&
memcmp(packet.data + 1, "vorbis", 6) == 0)
{
Expand Down Expand Up @@ -232,10 +232,10 @@ bool OGGParser::fill_packet(packet_t * packet, uint32_t len, FILE * fp)
bool OGGParser::parse_identification(packet_t * packet, MediaInfo *info, bool debug)
{
unsigned char * vorbis = packet->data;
int channels = read8(vorbis + 11);
int sampleRate = read32le(vorbis + 12);
int bitRateMaximum = read32le(vorbis + 16);
int bitRateNominal = read32le(vorbis + 20);
int channels = read_b8(vorbis + 11);
int sampleRate = read_b32le(vorbis + 12);
int bitRateMaximum = read_b32le(vorbis + 16);
int bitRateNominal = read_b32le(vorbis + 20);
//int bitRateMinimum = read32le(vorbis + 24);
if (sampleRate == 0)
return false; // invalid sample rate
Expand All @@ -255,12 +255,12 @@ bool OGGParser::parse_comment(packet_t * packet, MediaInfo *info, bool debug)
{
unsigned char * ve = packet->data + packet->datalen;
unsigned char * vp = packet->data + 7; // pass magic string
vp += read32le(vp) + 4; // pass vendor string
int count = read32le(vp); // comment list length;
vp += read_b32le(vp) + 4; // pass vendor string
int count = read_b32le(vp); // comment list length;
vp += 4;
while (count > 0)
{
int len = read32le(vp);
int len = read_b32le(vp);
vp += 4;
if ((vp + len) > ve)
break; // buffer overflow
Expand Down

0 comments on commit 681583a

Please sign in to comment.