Skip to content

Commit

Permalink
Define and use macros for bit-swapping integer constants.
Browse files Browse the repository at this point in the history
Using non-constant expressions as switch lables is not portable. Fix
this by using special macros.
  • Loading branch information
foo86 committed May 5, 2015
1 parent 68ba36e commit 9067bfb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
16 changes: 16 additions & 0 deletions libdcadec/common.h
Expand Up @@ -74,20 +74,36 @@ static inline uint16_t dca_bswap16(uint16_t x) { return (x << 8) | (x >> 8); }

#define dca_countof(x) (sizeof(x) / sizeof((x)[0]))

#define DCA_BSWAP16_C(x) ((((x) & 0x00ff) << 8) | (((x) & 0xff00) >> 8))
#define DCA_BSWAP32_C(x) ((DCA_BSWAP16_C(x) << 16) | (DCA_BSWAP16_C(x >> 16)))
#define DCA_BSWAP64_C(x) ((DCA_BSWAP32_C(x) << 32) | (DCA_BSWAP32_C(x >> 32)))

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define DCA_16LE(x) ((uint16_t)(x))
#define DCA_32LE(x) ((uint32_t)(x))
#define DCA_64LE(x) ((uint64_t)(x))
#define DCA_16BE(x) dca_bswap16(x)
#define DCA_32BE(x) dca_bswap32(x)
#define DCA_64BE(x) dca_bswap64(x)
#define DCA_16LE_C(x) (x)
#define DCA_32LE_C(x) (x)
#define DCA_64LE_C(x) (x)
#define DCA_16BE_C(x) DCA_BSWAP16_C(x)
#define DCA_32BE_C(x) DCA_BSWAP32_C(x)
#define DCA_64BE_C(x) DCA_BSWAP64_C(x)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define DCA_16LE(x) dca_bswap16(x)
#define DCA_32LE(x) dca_bswap32(x)
#define DCA_64LE(x) dca_bswap64(x)
#define DCA_16BE(x) ((uint16_t)(x))
#define DCA_32BE(x) ((uint32_t)(x))
#define DCA_64BE(x) ((uint64_t)(x))
#define DCA_16LE_C(x) DCA_BSWAP16_C(x)
#define DCA_32LE_C(x) DCA_BSWAP32_C(x)
#define DCA_64LE_C(x) DCA_BSWAP64_C(x)
#define DCA_16BE_C(x) (x)
#define DCA_32BE_C(x) (x)
#define DCA_64BE_C(x) (x)
#else
#error Unsupported byte order
#endif
Expand Down
8 changes: 4 additions & 4 deletions libdcadec/core_decoder.c
Expand Up @@ -1895,7 +1895,7 @@ static int parse_optional_info(struct core_decoder *core, int flags)
size_t hdr_size, dist;

switch (core->bits.data[sync_pos]) {
case DCA_32BE(SYNC_WORD_XCH):
case DCA_32BE_C(SYNC_WORD_XCH):
core->bits.index = (sync_pos + 1) * 32;
hdr_size = bits_get(&core->bits, 10) + 1;
// XCH comes last after all other extension streams. The
Expand All @@ -1910,15 +1910,15 @@ static int parse_optional_info(struct core_decoder *core, int flags)
}
break;

case DCA_32BE(SYNC_WORD_XXCH):
case DCA_32BE_C(SYNC_WORD_XXCH):
core->bits.index = (sync_pos + 1) * 32;
hdr_size = bits_get(&core->bits, 6) + 1;
if (!bits_check_crc(&core->bits, (sync_pos + 1) * 32,
sync_pos * 32 + hdr_size * 8))
xxch_pos = sync_pos + 1;
break;

case DCA_32BE(SYNC_WORD_X96):
case DCA_32BE_C(SYNC_WORD_X96):
// X96 comes last after all other extension streams (and can't
// coexist with XCH apparently). The distance between X96 sync
// word and end of the core frame must be equal to X96 frame
Expand All @@ -1932,7 +1932,7 @@ static int parse_optional_info(struct core_decoder *core, int flags)
}
break;

case DCA_32BE(SYNC_WORD_XBR):
case DCA_32BE_C(SYNC_WORD_XBR):
core->bits.index = (sync_pos + 1) * 32;
hdr_size = bits_get(&core->bits, 6) + 1;
if (!bits_check_crc(&core->bits, (sync_pos + 1) * 32,
Expand Down
4 changes: 2 additions & 2 deletions libdcadec/dca_context.c
Expand Up @@ -734,7 +734,7 @@ DCADEC_API int dcadec_context_parse(struct dcadec_context *dca, uint8_t *data, s

dca->packet = 0;

if (DCA_MEM32NE(data) == DCA_32BE(SYNC_WORD_CORE)) {
if (DCA_MEM32NE(data) == DCA_32BE_C(SYNC_WORD_CORE)) {
if (!dca->core)
if (!(dca->core = ta_znew(dca, struct core_decoder)))
return -DCADEC_ENOMEM;
Expand All @@ -752,7 +752,7 @@ DCADEC_API int dcadec_context_parse(struct dcadec_context *dca, uint8_t *data, s
}
}

if (DCA_MEM32NE(data) == DCA_32BE(SYNC_WORD_EXSS)) {
if (DCA_MEM32NE(data) == DCA_32BE_C(SYNC_WORD_EXSS)) {
if (!dca->exss)
if (!(dca->exss = ta_znew(dca, struct exss_parser)))
return -DCADEC_ENOMEM;
Expand Down
12 changes: 6 additions & 6 deletions libdcadec/dca_frame.c
Expand Up @@ -53,22 +53,22 @@ DCADEC_API int dcadec_frame_convert_bitstream(uint8_t *dst, size_t *dst_size,
_src = memcpy(_dst, _src, src_size);

switch (DCA_MEM32NE(_src)) {
case DCA_32BE(SYNC_WORD_CORE):
case DCA_32BE(SYNC_WORD_EXSS):
case DCA_32BE_C(SYNC_WORD_CORE):
case DCA_32BE_C(SYNC_WORD_EXSS):
if (_src != _dst)
memcpy(_dst, _src, src_size);
*dst_size = src_size;
return DCADEC_BITSTREAM_BE16;

case DCA_32BE(SYNC_WORD_CORE_LE):
case DCA_32BE(SYNC_WORD_EXSS_LE):
case DCA_32BE_C(SYNC_WORD_CORE_LE):
case DCA_32BE_C(SYNC_WORD_EXSS_LE):
count = (src_size + 1) / 2;
while (count--)
*_dst++ = dca_bswap16(*_src++);
*dst_size = src_size;
return DCADEC_BITSTREAM_LE16;

case DCA_32BE(SYNC_WORD_CORE_BE14):
case DCA_32BE_C(SYNC_WORD_CORE_BE14):
count = (src_size + 15) / 16;
while (count--) {
SRC_OP(BE)
Expand All @@ -79,7 +79,7 @@ DCADEC_API int dcadec_frame_convert_bitstream(uint8_t *dst, size_t *dst_size,
*dst_size = src_size - src_size / 8;
return DCADEC_BITSTREAM_BE14;

case DCA_32BE(SYNC_WORD_CORE_LE14):
case DCA_32BE_C(SYNC_WORD_CORE_LE14):
count = (src_size + 15) / 16;
while (count--) {
SRC_OP(LE)
Expand Down
6 changes: 3 additions & 3 deletions libdcadec/dca_stream.c
Expand Up @@ -74,7 +74,7 @@ static int parse_hd_hdr(struct dcadec_stream *stream)
if (fread(header, sizeof(header), 1, stream->fp) != 1)
return fseeko(stream->fp, 0, SEEK_SET);

if (header[0] != DCA_64BE(DTSHDHDR))
if (header[0] != DCA_64BE_C(DTSHDHDR))
return fseeko(stream->fp, 0, SEEK_SET);

while (true) {
Expand All @@ -83,7 +83,7 @@ static int parse_hd_hdr(struct dcadec_stream *stream)
return -1;

switch (header[0]) {
case DCA_64BE(STRMDATA): {
case DCA_64BE_C(STRMDATA): {
off_t pos = ftello(stream->fp);
if (pos < 0)
return -1;
Expand All @@ -93,7 +93,7 @@ static int parse_hd_hdr(struct dcadec_stream *stream)
return 1;
}

case DCA_64BE(AUPR_HDR): {
case DCA_64BE_C(AUPR_HDR): {
uint8_t data[21];

if (size < sizeof(data))
Expand Down

0 comments on commit 9067bfb

Please sign in to comment.