diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-12 19:09:58 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-12 19:09:58 +0000 |
| commit | 3633878e1f5a9c9356ba5228dc2d4bf2e65d06e7 (patch) | |
| tree | b0c427c93abbd81dcf4232ab92569aa2a8be37a9 /util/spkmodem_recv | |
| parent | 594a5a02cd3b8137689c900a0bdf44fc139f129c (diff) | |
util/spkmodem-recv: byte swap on big endian CPU
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'util/spkmodem_recv')
| -rw-r--r-- | util/spkmodem_recv/spkmodem-recv.c | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/util/spkmodem_recv/spkmodem-recv.c b/util/spkmodem_recv/spkmodem-recv.c index 6ff8055d..02f627e5 100644 --- a/util/spkmodem_recv/spkmodem-recv.c +++ b/util/spkmodem_recv/spkmodem-recv.c @@ -51,13 +51,6 @@ #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 { signed short frame[MAX_SAMPLES]; unsigned char pulse[MAX_SAMPLES]; @@ -81,10 +74,12 @@ struct decoder_state { unsigned char ascii; int debug; + int swap_bytes; }; static const char *argv0; +static int host_is_big_endian(void); static void handle_audio(struct decoder_state *st); static int valid_signal(struct decoder_state *st); static void decode_pulse(struct decoder_state *st); @@ -132,6 +127,9 @@ main(int argc, char **argv) break; } + if (host_is_big_endian()) + st.swap_bytes = 1; + setvbuf(stdout, NULL, _IONBF, 0); for (;;) @@ -140,6 +138,13 @@ main(int argc, char **argv) return EXIT_SUCCESS; } +static int +host_is_big_endian(void) +{ + unsigned int x = 1; + return (*(unsigned char *)&x == 0); +} + static void handle_audio(struct decoder_state *st) { @@ -208,6 +213,8 @@ static signed short read_sample(struct decoder_state *st) { size_t n; + signed short sample; + unsigned short u; while (st->inpos >= st->inlen) { @@ -225,7 +232,16 @@ read_sample(struct decoder_state *st) 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 |
