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 +++++++++++++++++++++++++++++++++++++ src/brle-enc.c | 94 +++++++++++++++++++++++++++++++++++++++ src/config.h | 9 ++++ src/gendata.c | 49 +++++++++++++++++++++ src/lzw-dec.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/lzw-enc.c | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rle-dec.c | 59 +++++++++++++++++++++++++ src/rle-enc.c | 57 ++++++++++++++++++++++++ 8 files changed, 611 insertions(+) create mode 100644 src/brle-dec.c create mode 100644 src/brle-enc.c create mode 100644 src/config.h create mode 100644 src/gendata.c create mode 100644 src/lzw-dec.c create mode 100644 src/lzw-enc.c create mode 100644 src/rle-dec.c create mode 100644 src/rle-enc.c (limited to 'src') 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; +} 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 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + +FILE *istream; +FILE *ostream; + +char +getbit (void) +{ + static int byte; + static int i = 8; + if (i > 7) { + byte = fgetc (istream); + if (byte == EOF) + return -1; + i = 0; + } + char bit = byte >> i & 1; + i += 1; + return bit; +} + +bool +put4bit (uint8_t data) +{ + static uint8_t byte; + static bool nonempty = false; + if (nonempty) { + byte |= data << 4; + fputc (byte, ostream); + nonempty = false; + } else { + byte = data & (1 << 4) - 1; + nonempty = true; + } + return nonempty; +} + +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 = getbit (); + bool nonempty; + while (current != -1) { + uint8_t count = 1; + char next; + while ((next = getbit ()) == current && count < (1 << 3) - 1) + ++count; + + nonempty = put4bit (current << 3 | count); + + current = next; + } + if (nonempty) + put4bit (0); + + if (fclose (ostream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + + if (fclose (istream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} 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 @@ +#ifndef INCLUDED_CONFIG_H +#define INCLUDED_CONFIG_H + +#define ALPHABET L"йёпводЙЁПВОД0123456789&. " +// #define LETTER_COUNT 10000 +#define LETTER_COUNT 1000000 +#define CCS "UTF8" + +#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 @@ +#include +#include +#include +#include +#include +#include +#include +#include "config.h" + +int +main (int argc, char *argv[]) +{ + if (argc != 2) { + fprintf (stderr, "Usage: %s {outfile}\n", basename (argv[0])); + return EXIT_FAILURE; + } + + FILE *stream = fopen (argv[1], "w,ccs="CCS); + if (stream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + srand (time (NULL)); + size_t letter_count = 15; + for (int i = 0; i < 15; ++i) + fputwc (ALPHABET[rand () % wcslen (ALPHABET)], stream); + while (letter_count < LETTER_COUNT) { + int count = rand () % 15 + 1; + size_t letters_left = LETTER_COUNT - letter_count; + count = count > letters_left ? letters_left : count; + if (rand () % 2) { + for (int i = 0; i < count; ++i) + fputwc (ALPHABET[rand () % wcslen (ALPHABET)], stream); + } else { + wchar_t wc = ALPHABET[rand () % wcslen (ALPHABET)]; + for (int i = 0; i < count; ++i) + fputwc (wc, stream); + } + letter_count += count; + } + + if (fclose (stream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + +FILE *istream; + +uint16_t next_input_code () +{ + static uint8_t buf[3]; + static uint16_t next; + static int i = 0; + + if (i) { + if (next != -1) + i = 0; + return next; + } else { + size_t read; + read = fread (buf, 1, 3, istream); + i = 1; + if (read == 3) { + next = buf[1] >> 4 | buf[2] << 4; + } else if (read == 2) { + next = -1; + } else + return -1; + + return buf[0] | (buf[1] & ((1 << 4) - 1)) << 8; + } +} + +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; + } + + // initialize dictionary + wchar_t *dict[1 << 11] = { NULL }; + size_t next_code; + for (next_code = 0; next_code < wcslen (ALPHABET); ++next_code) { + dict[next_code] = malloc (2*sizeof (wchar_t)); + dict[next_code][0] = ALPHABET[next_code]; + dict[next_code][1] = L'\0'; + // printf ("%d %ls\n", next_code, dict[next_code]); + } // putchar ('\n'); + + istream = fopen (argv[1], "rb"); + if (istream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + FILE *ostream = fopen (argv[2], "w,ccs="CCS); + if (ostream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + uint16_t old = next_input_code (); + if (old == -1) { + return EXIT_SUCCESS; + } + uint16_t new; + wchar_t *seq = NULL; + wchar_t wc; + fputws (dict[old], ostream); + while ((new = next_input_code ()) != (uint16_t) -1) { + if (dict[new]) { + size_t new_len = wcslen (dict[new]); + seq = realloc (seq, (new_len + 1)*sizeof (wchar_t)); + wcscpy (seq, dict[new]); + } else { + size_t old_len = wcslen (dict[old]); + seq = realloc (seq, (old_len + 2)*sizeof (wchar_t)); + memcpy (seq, dict[old], old_len*sizeof (wchar_t)); + seq[old_len + 0] = wc; + seq[old_len + 1] = L'\0'; + } + + fputws (seq, ostream); + + wc = seq[0]; + + if (next_code < 1 << 11 - 1) { + size_t old_len = wcslen (dict[old]); + dict[next_code] = malloc ((old_len + 2)*sizeof (wchar_t)); + memcpy (dict[next_code], dict[old], old_len*sizeof (wchar_t)); + dict[next_code][old_len + 0] = wc; + dict[next_code][old_len + 1] = L'\0'; + ++next_code; + } + + old = new; + // fflush (stdout); + } // putchar ('\n'); + + if (fclose (istream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + if (fclose (ostream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} 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 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "config.h" + +int +main (int argc, char *argv[]) +{ + setlocale (LC_ALL, ""); + errno = 0; + + if (argc != 3) { + fprintf (stderr, "Usage: %s {infile} {outfile}\n", basename (argv[0])); + return EXIT_FAILURE; + } + + // initialize dictionary + hcreate ((1 << 12)*6/5); + size_t next_code = 0; + for (next_code = 0; next_code < wcslen (ALPHABET); ++next_code) { + wchar_t wseq[2]; + wseq[0] = ALPHABET[next_code]; + wseq[1] = L'\0'; + char *seq = malloc (3); + wcstombs (seq, wseq, 3); + ENTRY entry = { + .key = seq, + .data = (void *)next_code, + }; + // printf ("%d %s\n", next_code, seq); + ENTRY *ep = hsearch (entry, ENTER); + } // putchar ('\n'); + + FILE *istream = fopen (argv[1], "r,ccs="CCS); + if (istream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + FILE *ostream = fopen (argv[2], "wb"); + if (ostream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + size_t len = 1; + wchar_t *wseq = malloc ((len + 1)*sizeof (wchar_t)); + char *seq = malloc (2*len + 1); + wseq[0] = fgetwc (istream); + wseq[1] = L'\0'; + uint8_t buf[3] = { 0 }; + int bufcount = 0; + size_t dictvol = next_code; + while (wseq[len - 1] != WEOF) { + wchar_t wc = fgetwc (istream); + if (wc == WEOF) + break; + ++len; + wseq = realloc (wseq, (len + 1)*sizeof (wchar_t)); + seq = realloc (seq, 2*len + 1); + wseq[len - 1] = wc; + wseq[len] = L'\0'; + wcstombs (seq, wseq, 2*len + 1); + ENTRY entry = { + .key = seq, + }; + ENTRY *ep = hsearch (entry, FIND); + if (ep == NULL) { + if (next_code < 1 << 11 - 1) { + entry.key = strdup (entry.key); + entry.data = (void *) next_code++; + hsearch (entry, ENTER); + entry.key = seq; + dictvol += wcslen (wseq); + } + + wseq[len - 1] = '\0'; + wcstombs (seq, wseq, 2*len + 1); + ENTRY *ep = hsearch (entry, FIND); + // printf ("%llu %s\n", ep->data, ep->key); + uint16_t c = (uint16_t) ep->data; + if (bufcount) { + buf[1] |= (c & ((1 << 4) - 1)) << 4; + buf[2] = c >> 4; + bufcount = 0; + fwrite (buf, 1, 3, ostream); + } else { + buf[0] = c & ((1 << 8) - 1); + buf[1] = c >> 8; + bufcount = 1; + } + + wseq[0] = wc; + wseq[1] = L'\0'; + len = 1; + } + } + wcstombs (seq, wseq, 2*len + 1); + ENTRY entry = { + .key = seq, + }; + ENTRY *ep = hsearch (entry, FIND); + // printf ("%llu %s\n", ep->data, ep->key); + uint16_t c = (uint16_t) ep->data; + if (bufcount) { + buf[1] |= (c & ((1 << 4) - 1)) << 4; + buf[2] = c >> 4; + fwrite (buf, 1, 3, ostream); + } else { + buf[0] = c & ((1 << 8) - 1); + buf[1] = c >> 8; + fwrite (buf, 1, 2, ostream); + } + + printf ("Coding cost: %.2f\n", (float) dictvol/next_code); + + if (fclose (istream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + + if (fclose (ostream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} 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 @@ +#include +#include +#include +#include +#include + +#include "config.h" + +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; + } + + FILE *istream = fopen (argv[1], "r,ccs="CCS); + if (istream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + FILE *ostream = fopen (argv[2], "w,ccs="CCS); + // FILE *ostream = stdout; + if (ostream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + wint_t current = fgetwc (istream); + while (current != WEOF) { + wint_t next = fgetwc (istream); + if (next == current) { + size_t count; + fwscanf (istream, L"%llu", &count); + fgetwc (istream); + while (count--) + fputwc (current, ostream); + current = fgetwc (istream); + } else { + fputwc (current, ostream); + current = next; + } + } + + if (fclose (ostream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + + if (fclose (istream) == EOF) { + perror (NULL); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} 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 @@ +#include +#include +#include +#include +#include + +#include "config.h" + +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; + } + + FILE *istream = fopen (argv[1], "r,ccs="CCS); + if (istream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + FILE *ostream = fopen (argv[2], "w,ccs="CCS); + if (ostream == NULL) { + perror (NULL); + return EXIT_FAILURE; + } + + wint_t current = fgetwc (istream); + while (current != WEOF) { + size_t count = 1; + wint_t next; + while ((next = fgetwc (istream)) == current) + ++count; + + if (count == 1) + fwprintf (ostream, L"%lc", current); + else + fwprintf (ostream, L"%lc%lc%llu ", current, current, count); + + current = next; + } + + 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