util/spkmodem-recv: byte swap on big endian CPU

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-12 19:09:58 +00:00
parent 594a5a02cd
commit 3633878e1f
+24 -8
View File
@@ -51,13 +51,6 @@
#define READ_BUF 4096 #define READ_BUF 4096
/* TODO: handle this at runtime instead (bswap) */
#if defined(__BYTE_ORDER__)
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#error "spkmodem-recv requires little-endian samples"
#endif
#endif
struct decoder_state { struct decoder_state {
signed short frame[MAX_SAMPLES]; signed short frame[MAX_SAMPLES];
unsigned char pulse[MAX_SAMPLES]; unsigned char pulse[MAX_SAMPLES];
@@ -81,10 +74,12 @@ struct decoder_state {
unsigned char ascii; unsigned char ascii;
int debug; int debug;
int swap_bytes;
}; };
static const char *argv0; static const char *argv0;
static int host_is_big_endian(void);
static void handle_audio(struct decoder_state *st); static void handle_audio(struct decoder_state *st);
static int valid_signal(struct decoder_state *st); static int valid_signal(struct decoder_state *st);
static void decode_pulse(struct decoder_state *st); static void decode_pulse(struct decoder_state *st);
@@ -132,6 +127,9 @@ main(int argc, char **argv)
break; break;
} }
if (host_is_big_endian())
st.swap_bytes = 1;
setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0);
for (;;) for (;;)
@@ -140,6 +138,13 @@ main(int argc, char **argv)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
static int
host_is_big_endian(void)
{
unsigned int x = 1;
return (*(unsigned char *)&x == 0);
}
static void static void
handle_audio(struct decoder_state *st) handle_audio(struct decoder_state *st)
{ {
@@ -208,6 +213,8 @@ static signed short
read_sample(struct decoder_state *st) read_sample(struct decoder_state *st)
{ {
size_t n; size_t n;
signed short sample;
unsigned short u;
while (st->inpos >= st->inlen) { while (st->inpos >= st->inlen) {
@@ -225,7 +232,16 @@ read_sample(struct decoder_state *st)
st->inlen = n; st->inlen = n;
} }
return st->inbuf[st->inpos++]; sample = st->inbuf[st->inpos++];
if (st->swap_bytes) {
u = (unsigned short)sample;
u = (u >> 8) | (u << 8);
sample = (signed short)u;
}
return sample;
} }
static int static int