aboutsummaryrefslogtreecommitdiff
path: root/src
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
downloadlab-00-795a8ecaf5656d299139e2c41fab19573bdac0c6.tar.gz
publishHEADmaster
Diffstat (limited to 'src')
-rw-r--r--src/brle-dec.c89
-rw-r--r--src/brle-enc.c94
-rw-r--r--src/config.h9
-rw-r--r--src/gendata.c49
-rw-r--r--src/lzw-dec.c119
-rw-r--r--src/lzw-enc.c135
-rw-r--r--src/rle-dec.c59
-rw-r--r--src/rle-enc.c57
8 files changed, 611 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}
diff --git a/src/brle-enc.c b/src/brle-enc.c
new file mode 100644
index 0000000..d14c462
--- /dev/null
+++ b/src/brle-enc.c
@@ -0,0 +1,94 @@
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
14char
15getbit (void)
16{
17 static int byte;
18 static int i = 8;
19 if (i > 7) {
20 byte = fgetc (istream);
21 if (byte == EOF)
22 return -1;
23 i = 0;
24 }
25 char bit = byte >> i & 1;
26 i += 1;
27 return bit;
28}
29
30bool
31put4bit (uint8_t data)
32{
33 static uint8_t byte;
34 static bool nonempty = false;
35 if (nonempty) {
36 byte |= data << 4;
37 fputc (byte, ostream);
38 nonempty = false;
39 } else {
40 byte = data & (1 << 4) - 1;
41 nonempty = true;
42 }
43 return nonempty;
44}
45
46int
47main (int argc, char *argv[])
48{
49 setlocale (LC_ALL, "");
50
51 if (argc != 3) {
52 fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0]));
53 return EXIT_FAILURE;
54 }
55
56 istream = fopen (argv[1], "rb");
57 if (istream == NULL) {
58 perror (NULL);
59 return EXIT_FAILURE;
60 }
61
62 ostream = fopen (argv[2], "wb");
63 if (ostream == NULL) {
64 perror (NULL);
65 return EXIT_FAILURE;
66 }
67
68 char current = getbit ();
69 bool nonempty;
70 while (current != -1) {
71 uint8_t count = 1;
72 char next;
73 while ((next = getbit ()) == current && count < (1 << 3) - 1)
74 ++count;
75
76 nonempty = put4bit (current << 3 | count);
77
78 current = next;
79 }
80 if (nonempty)
81 put4bit (0);
82
83 if (fclose (ostream) == EOF) {
84 perror (NULL);
85 return EXIT_FAILURE;
86 }
87
88 if (fclose (istream) == EOF) {
89 perror (NULL);
90 return EXIT_FAILURE;
91 }
92
93 return EXIT_SUCCESS;
94}
diff --git a/src/config.h b/src/config.h
new file mode 100644
index 0000000..d551f87
--- /dev/null
+++ b/src/config.h
@@ -0,0 +1,9 @@
1#ifndef INCLUDED_CONFIG_H
2#define INCLUDED_CONFIG_H
3
4#define ALPHABET L"йёпводЙЁПВОД0123456789&. "
5// #define LETTER_COUNT 10000
6#define LETTER_COUNT 1000000
7#define CCS "UTF8"
8
9#endif
diff --git a/src/gendata.c b/src/gendata.c
new file mode 100644
index 0000000..cb92133
--- /dev/null
+++ b/src/gendata.c
@@ -0,0 +1,49 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <libgen.h>
4#include <errno.h>
5#include <time.h>
6#include <wchar.h>
7#include <unistd.h>
8#include "config.h"
9
10int
11main (int argc, char *argv[])
12{
13 if (argc != 2) {
14 fprintf (stderr, "Usage: %s {outfile}\n", basename (argv[0]));
15 return EXIT_FAILURE;
16 }
17
18 FILE *stream = fopen (argv[1], "w,ccs="CCS);
19 if (stream == NULL) {
20 perror (NULL);
21 return EXIT_FAILURE;
22 }
23
24 srand (time (NULL));
25 size_t letter_count = 15;
26 for (int i = 0; i < 15; ++i)
27 fputwc (ALPHABET[rand () % wcslen (ALPHABET)], stream);
28 while (letter_count < LETTER_COUNT) {
29 int count = rand () % 15 + 1;
30 size_t letters_left = LETTER_COUNT - letter_count;
31 count = count > letters_left ? letters_left : count;
32 if (rand () % 2) {
33 for (int i = 0; i < count; ++i)
34 fputwc (ALPHABET[rand () % wcslen (ALPHABET)], stream);
35 } else {
36 wchar_t wc = ALPHABET[rand () % wcslen (ALPHABET)];
37 for (int i = 0; i < count; ++i)
38 fputwc (wc, stream);
39 }
40 letter_count += count;
41 }
42
43 if (fclose (stream) == EOF) {
44 perror (NULL);
45 return EXIT_FAILURE;
46 }
47
48 return EXIT_SUCCESS;
49}
diff --git a/src/lzw-dec.c b/src/lzw-dec.c
new file mode 100644
index 0000000..e5125dc
--- /dev/null
+++ b/src/lzw-dec.c
@@ -0,0 +1,119 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <wchar.h>
5#include <errno.h>
6#include <locale.h>
7#include <inttypes.h>
8#include <libgen.h>
9
10#include "config.h"
11
12FILE *istream;
13
14uint16_t next_input_code ()
15{
16 static uint8_t buf[3];
17 static uint16_t next;
18 static int i = 0;
19
20 if (i) {
21 if (next != -1)
22 i = 0;
23 return next;
24 } else {
25 size_t read;
26 read = fread (buf, 1, 3, istream);
27 i = 1;
28 if (read == 3) {
29 next = buf[1] >> 4 | buf[2] << 4;
30 } else if (read == 2) {
31 next = -1;
32 } else
33 return -1;
34
35 return buf[0] | (buf[1] & ((1 << 4) - 1)) << 8;
36 }
37}
38
39int
40main (int argc, char *argv[])
41{
42 setlocale (LC_ALL, "");
43
44 if (argc != 3) {
45 fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0]));
46 return EXIT_FAILURE;
47 }
48
49 // initialize dictionary
50 wchar_t *dict[1 << 11] = { NULL };
51 size_t next_code;
52 for (next_code = 0; next_code < wcslen (ALPHABET); ++next_code) {
53 dict[next_code] = malloc (2*sizeof (wchar_t));
54 dict[next_code][0] = ALPHABET[next_code];
55 dict[next_code][1] = L'\0';
56 // printf ("%d %ls\n", next_code, dict[next_code]);
57 } // putchar ('\n');
58
59 istream = fopen (argv[1], "rb");
60 if (istream == NULL) {
61 perror (NULL);
62 return EXIT_FAILURE;
63 }
64
65 FILE *ostream = fopen (argv[2], "w,ccs="CCS);
66 if (ostream == NULL) {
67 perror (NULL);
68 return EXIT_FAILURE;
69 }
70
71 uint16_t old = next_input_code ();
72 if (old == -1) {
73 return EXIT_SUCCESS;
74 }
75 uint16_t new;
76 wchar_t *seq = NULL;
77 wchar_t wc;
78 fputws (dict[old], ostream);
79 while ((new = next_input_code ()) != (uint16_t) -1) {
80 if (dict[new]) {
81 size_t new_len = wcslen (dict[new]);
82 seq = realloc (seq, (new_len + 1)*sizeof (wchar_t));
83 wcscpy (seq, dict[new]);
84 } else {
85 size_t old_len = wcslen (dict[old]);
86 seq = realloc (seq, (old_len + 2)*sizeof (wchar_t));
87 memcpy (seq, dict[old], old_len*sizeof (wchar_t));
88 seq[old_len + 0] = wc;
89 seq[old_len + 1] = L'\0';
90 }
91
92 fputws (seq, ostream);
93
94 wc = seq[0];
95
96 if (next_code < 1 << 11 - 1) {
97 size_t old_len = wcslen (dict[old]);
98 dict[next_code] = malloc ((old_len + 2)*sizeof (wchar_t));
99 memcpy (dict[next_code], dict[old], old_len*sizeof (wchar_t));
100 dict[next_code][old_len + 0] = wc;
101 dict[next_code][old_len + 1] = L'\0';
102 ++next_code;
103 }
104
105 old = new;
106 // fflush (stdout);
107 } // putchar ('\n');
108
109 if (fclose (istream) == EOF) {
110 perror (NULL);
111 return EXIT_FAILURE;
112 }
113 if (fclose (ostream) == EOF) {
114 perror (NULL);
115 return EXIT_FAILURE;
116 }
117
118 return EXIT_SUCCESS;
119}
diff --git a/src/lzw-enc.c b/src/lzw-enc.c
new file mode 100644
index 0000000..0c6db3b
--- /dev/null
+++ b/src/lzw-enc.c
@@ -0,0 +1,135 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <search.h>
5#include <wchar.h>
6#include <errno.h>
7#include <locale.h>
8#include <inttypes.h>
9#include <libgen.h>
10
11#include "config.h"
12
13int
14main (int argc, char *argv[])
15{
16 setlocale (LC_ALL, "");
17 errno = 0;
18
19 if (argc != 3) {
20 fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0]));
21 return EXIT_FAILURE;
22 }
23
24 // initialize dictionary
25 hcreate ((1 << 12)*6/5);
26 size_t next_code = 0;
27 for (next_code = 0; next_code < wcslen (ALPHABET); ++next_code) {
28 wchar_t wseq[2];
29 wseq[0] = ALPHABET[next_code];
30 wseq[1] = L'\0';
31 char *seq = malloc (3);
32 wcstombs (seq, wseq, 3);
33 ENTRY entry = {
34 .key = seq,
35 .data = (void *)next_code,
36 };
37 // printf ("%d %s\n", next_code, seq);
38 ENTRY *ep = hsearch (entry, ENTER);
39 } // putchar ('\n');
40
41 FILE *istream = fopen (argv[1], "r,ccs="CCS);
42 if (istream == NULL) {
43 perror (NULL);
44 return EXIT_FAILURE;
45 }
46
47 FILE *ostream = fopen (argv[2], "wb");
48 if (ostream == NULL) {
49 perror (NULL);
50 return EXIT_FAILURE;
51 }
52
53 size_t len = 1;
54 wchar_t *wseq = malloc ((len + 1)*sizeof (wchar_t));
55 char *seq = malloc (2*len + 1);
56 wseq[0] = fgetwc (istream);
57 wseq[1] = L'\0';
58 uint8_t buf[3] = { 0 };
59 int bufcount = 0;
60 size_t dictvol = next_code;
61 while (wseq[len - 1] != WEOF) {
62 wchar_t wc = fgetwc (istream);
63 if (wc == WEOF)
64 break;
65 ++len;
66 wseq = realloc (wseq, (len + 1)*sizeof (wchar_t));
67 seq = realloc (seq, 2*len + 1);
68 wseq[len - 1] = wc;
69 wseq[len] = L'\0';
70 wcstombs (seq, wseq, 2*len + 1);
71 ENTRY entry = {
72 .key = seq,
73 };
74 ENTRY *ep = hsearch (entry, FIND);
75 if (ep == NULL) {
76 if (next_code < 1 << 11 - 1) {
77 entry.key = strdup (entry.key);
78 entry.data = (void *) next_code++;
79 hsearch (entry, ENTER);
80 entry.key = seq;
81 dictvol += wcslen (wseq);
82 }
83
84 wseq[len - 1] = '\0';
85 wcstombs (seq, wseq, 2*len + 1);
86 ENTRY *ep = hsearch (entry, FIND);
87 // printf ("%llu %s\n", ep->data, ep->key);
88 uint16_t c = (uint16_t) ep->data;
89 if (bufcount) {
90 buf[1] |= (c & ((1 << 4) - 1)) << 4;
91 buf[2] = c >> 4;
92 bufcount = 0;
93 fwrite (buf, 1, 3, ostream);
94 } else {
95 buf[0] = c & ((1 << 8) - 1);
96 buf[1] = c >> 8;
97 bufcount = 1;
98 }
99
100 wseq[0] = wc;
101 wseq[1] = L'\0';
102 len = 1;
103 }
104 }
105 wcstombs (seq, wseq, 2*len + 1);
106 ENTRY entry = {
107 .key = seq,
108 };
109 ENTRY *ep = hsearch (entry, FIND);
110 // printf ("%llu %s\n", ep->data, ep->key);
111 uint16_t c = (uint16_t) ep->data;
112 if (bufcount) {
113 buf[1] |= (c & ((1 << 4) - 1)) << 4;
114 buf[2] = c >> 4;
115 fwrite (buf, 1, 3, ostream);
116 } else {
117 buf[0] = c & ((1 << 8) - 1);
118 buf[1] = c >> 8;
119 fwrite (buf, 1, 2, ostream);
120 }
121
122 printf ("Coding cost: %.2f\n", (float) dictvol/next_code);
123
124 if (fclose (istream) == EOF) {
125 perror (NULL);
126 return EXIT_FAILURE;
127 }
128
129 if (fclose (ostream) == EOF) {
130 perror (NULL);
131 return EXIT_FAILURE;
132 }
133
134 return EXIT_SUCCESS;
135}
diff --git a/src/rle-dec.c b/src/rle-dec.c
new file mode 100644
index 0000000..02a006c
--- /dev/null
+++ b/src/rle-dec.c
@@ -0,0 +1,59 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <wchar.h>
4#include <locale.h>
5#include <libgen.h>
6
7#include "config.h"
8
9int
10main (int argc, char *argv[])
11{
12 setlocale (LC_ALL, "");
13
14 if (argc != 3) {
15 fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0]));
16 return EXIT_FAILURE;
17 }
18
19 FILE *istream = fopen (argv[1], "r,ccs="CCS);
20 if (istream == NULL) {
21 perror (NULL);
22 return EXIT_FAILURE;
23 }
24
25 FILE *ostream = fopen (argv[2], "w,ccs="CCS);
26 // FILE *ostream = stdout;
27 if (ostream == NULL) {
28 perror (NULL);
29 return EXIT_FAILURE;
30 }
31
32 wint_t current = fgetwc (istream);
33 while (current != WEOF) {
34 wint_t next = fgetwc (istream);
35 if (next == current) {
36 size_t count;
37 fwscanf (istream, L"%llu", &count);
38 fgetwc (istream);
39 while (count--)
40 fputwc (current, ostream);
41 current = fgetwc (istream);
42 } else {
43 fputwc (current, ostream);
44 current = next;
45 }
46 }
47
48 if (fclose (ostream) == EOF) {
49 perror (NULL);
50 return EXIT_FAILURE;
51 }
52
53 if (fclose (istream) == EOF) {
54 perror (NULL);
55 return EXIT_FAILURE;
56 }
57
58 return EXIT_SUCCESS;
59}
diff --git a/src/rle-enc.c b/src/rle-enc.c
new file mode 100644
index 0000000..6620a81
--- /dev/null
+++ b/src/rle-enc.c
@@ -0,0 +1,57 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <wchar.h>
4#include <locale.h>
5#include <libgen.h>
6
7#include "config.h"
8
9int
10main (int argc, char *argv[])
11{
12 setlocale (LC_ALL, "");
13
14 if (argc != 3) {
15 fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0]));
16 return EXIT_FAILURE;
17 }
18
19 FILE *istream = fopen (argv[1], "r,ccs="CCS);
20 if (istream == NULL) {
21 perror (NULL);
22 return EXIT_FAILURE;
23 }
24
25 FILE *ostream = fopen (argv[2], "w,ccs="CCS);
26 if (ostream == NULL) {
27 perror (NULL);
28 return EXIT_FAILURE;
29 }
30
31 wint_t current = fgetwc (istream);
32 while (current != WEOF) {
33 size_t count = 1;
34 wint_t next;
35 while ((next = fgetwc (istream)) == current)
36 ++count;
37
38 if (count == 1)
39 fwprintf (ostream, L"%lc", current);
40 else
41 fwprintf (ostream, L"%lc%lc%llu ", current, current, count);
42
43 current = next;
44 }
45
46 if (fclose (ostream) == EOF) {
47 perror (NULL);
48 return EXIT_FAILURE;
49 }
50
51 if (fclose (istream) == EOF) {
52 perror (NULL);
53 return EXIT_FAILURE;
54 }
55
56 return EXIT_SUCCESS;
57}