diff options
| author | 2026-11-07 02:33:00 +0000 | |
|---|---|---|
| committer | 2026-11-07 02:38:20 +0000 | |
| commit | 795a8ecaf5656d299139e2c41fab19573bdac0c6 (patch) | |
| tree | 9d0529ad1abd7959256b139b2c9c6b7fa3e72e07 /src/lzw-dec.c | |
| download | lab-00-795a8ecaf5656d299139e2c41fab19573bdac0c6.tar.gz | |
Diffstat (limited to 'src/lzw-dec.c')
| -rw-r--r-- | src/lzw-dec.c | 119 |
1 files changed, 119 insertions, 0 deletions
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 | |||
| 12 | FILE *istream; | ||
| 13 | |||
| 14 | uint16_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 | |||
| 39 | int | ||
| 40 | main (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 | } | ||
