From 795a8ecaf5656d299139e2c41fab19573bdac0c6 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sat, 7 Nov 2026 05:33:00 +0300 Subject: publish --- src/brle-dec.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/brle-dec.c (limited to 'src/brle-dec.c') diff --git a/src/brle-dec.c b/src/brle-dec.c new file mode 100644 index 0000000..298c945 --- /dev/null +++ b/src/brle-dec.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + +FILE *istream; +FILE *ostream; + +uint8_t +nputbit (uint8_t bit, uint8_t n) +{ + static uint16_t buff; + static uint8_t i = 0; + if (bit) + buff |= ((1 << n) - 1) << i; + i += n; + + if (i >= 8) { + fputc (buff & (1 << 8) - 1, ostream); + buff >>= 8; + i -= 8; + } + + return i; +} + +uint8_t +get4bit (void) +{ + static int byte; + static bool empty = true; + if (empty) { + byte = fgetc (istream); + if (byte == EOF) + return -1; + empty = false; + } else { + empty = true; + byte >>= 4; + } + return byte & (1 << 4) - 1; +} + +int +main (int argc, char *argv[]) +{ + setlocale (LC_ALL, ""); + + if (argc != 3) { + fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0])); + return EXIT_FAILURE; + } + + istream = fopen (argv[1], "rb"); + if (istream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + ostream = fopen (argv[2], "wb"); + if (ostream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + char current; + uint8_t buffered; + while ((current = get4bit ()) != -1) + buffered = nputbit (current >> 3, current & (1 << 3) - 1); + if (buffered) + nputbit (0, 8 - buffered); + + if (fclose (ostream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + + if (fclose (istream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} -- cgit v1.3.1