aboutsummaryrefslogtreecommitdiff
path: root/src/brle-dec.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-07 02:33:00 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-07 02:38:20 +0000
commit795a8ecaf5656d299139e2c41fab19573bdac0c6 (patch)
tree9d0529ad1abd7959256b139b2c9c6b7fa3e72e07 /src/brle-dec.c
downloadlab-00-795a8ecaf5656d299139e2c41fab19573bdac0c6.tar.gz
publishHEADmaster
Diffstat (limited to 'src/brle-dec.c')
-rw-r--r--src/brle-dec.c89
1 files changed, 89 insertions, 0 deletions
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 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <stdint.h>
4#include <wchar.h>
5#include <locale.h>
6#include <stdbool.h>
7#include <libgen.h>
8
9#include "config.h"
10
11FILE *istream;
12FILE *ostream;
13
14uint8_t
15nputbit (uint8_t bit, uint8_t n)
16{
17 static uint16_t buff;
18 static uint8_t i = 0;
19 if (bit)
20 buff |= ((1 << n) - 1) << i;
21 i += n;
22
23 if (i >= 8) {
24 fputc (buff & (1 << 8) - 1, ostream);
25 buff >>= 8;
26 i -= 8;
27 }
28
29 return i;
30}
31
32uint8_t
33get4bit (void)
34{
35 static int byte;
36 static bool empty = true;
37 if (empty) {
38 byte = fgetc (istream);
39 if (byte == EOF)
40 return -1;
41 empty = false;
42 } else {
43 empty = true;
44 byte >>= 4;
45 }
46 return byte & (1 << 4) - 1;
47}
48
49int
50main (int argc, char *argv[])
51{
52 setlocale (LC_ALL, "");
53
54 if (argc != 3) {
55 fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0]));
56 return EXIT_FAILURE;
57 }
58
59 istream = fopen (argv[1], "rb");
60 if (istream == NULL) {
61 perror (NULL);
62 return EXIT_FAILURE;
63 }
64
65 ostream = fopen (argv[2], "wb");
66 if (ostream == NULL) {
67 perror (NULL);
68 return EXIT_FAILURE;
69 }
70
71 char current;
72 uint8_t buffered;
73 while ((current = get4bit ()) != -1)
74 buffered = nputbit (current >> 3, current & (1 << 3) - 1);
75 if (buffered)
76 nputbit (0, 8 - buffered);
77
78 if (fclose (ostream) == EOF) {
79 perror (NULL);
80 return EXIT_FAILURE;
81 }
82
83 if (fclose (istream) == EOF) {
84 perror (NULL);
85 return EXIT_FAILURE;
86 }
87
88 return EXIT_SUCCESS;
89}