-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrle.c
296 lines (266 loc) · 6.33 KB
/
rle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* Simple RLE (Run-Lenght Encoding) encoding/decoding utility
*
* Copyright (c) 2017 Sergey Ryazanov <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/**
* RLE algorithm: https://en.wikipedia.org/wiki/Run-length_encoding
*
* Compressed format: each run of bytes is preceded by an unsigned octet LEN,
* if LEN < 128, then the next byte will be repeated LEN times in the output
* stream, if LEN >= 128 then the subsequent (256 - LEN) bytes will be copied
* to the output stream "as is".
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#define PR_ERR(__fmt, ...) \
fprintf(stderr, "rle: " __fmt "\n", ## __VA_ARGS__)
#define PR_ERR_SYS(__fmt, ...) \
PR_ERR(__fmt ": %s", ## __VA_ARGS__, strerror(errno))
#define PR_ERR_SYS_ONLY() \
PR_ERR("%s", strerror(errno))
static const char utility_version[] = "1.0-beta";
static int decode(FILE *fin, FILE *fout)
{
int ch, len = 0;
while ((ch = fgetc(fin)) != EOF) {
if (!len) {
if (!ch)
break;
len = ch;
} else if (len < 0x80) {
do {
if (fputc(ch, fout) == EOF)
break;
} while (--len);
} else {
if (fputc(ch, fout) == EOF)
break;
len = (len + 1) % 0x100;
}
}
if (ferror(fin)) {
PR_ERR("Input error.");
return -EIO;
} else if (ferror(fout)) {
PR_ERR("Output error.");
return -EIO;
} else if (len) {
PR_ERR("Unexpected end of stream.");
return -EIO;
}
return 0;
}
static int encode(FILE *fin, FILE *fout)
{
int ch = 0, len = 0, flush = 0, repeat = 0;
uint8_t buf[0x80 + 1];
while (ch != EOF) {
ch = fgetc(fin);
if (ch != EOF)
buf[len++] = ch;
else if (ferror(fin))
break;
if (repeat) {
/* There are always at least 2 bytes in buffer */
if (buf[len - 2] != buf[len - 1]) {
flush = len - 1;
repeat = 0;
} else if (len == 0x7f || ch == EOF) {
flush = len;
repeat = 0;
}
if (flush) {
fputc(flush, fout);
fputc(buf[0], fout);
}
} else {
if (len >= 2 && buf[len - 2] == buf[len - 1]) {
flush = len - 2;
repeat = 1;
} else if (len == 0x81) {
flush = 0x80;
} else if (ch == EOF) {
flush = len;
}
if (flush) {
fputc(0x100 - flush, fout);
fwrite(buf, flush, 1, fout);
}
}
if (flush) {
if (ferror(fout))
break;
memmove(&buf[0], &buf[flush], len - flush);
len -= flush;
flush = 0;
}
}
if (ferror(fin)) {
PR_ERR("Input error.");
return -EIO;
} else if (ferror(fout)) {
PR_ERR("Output error.");
return -EIO;
}
return 0;
}
static void usage(const char *name)
{
printf(
"Usage: %s [OPTION]... [FILE]\n"
"Compress or decompress FILE in the .rle format.\n"
"\n"
" -c write to standard output and do not delete input file\n"
" -d force decompression (decoding)\n"
" -f force (file overwrite, output to a terminal, etc.)\n"
" -h display this help and exit\n"
" -k keep (do not delete) input file\n"
" -V display the version and exit\n"
"\n"
"With no FILE, or when FILE is -, read standard input and write standard output.\n"
"\n"
"Written by Sergey Ryazanov <[email protected]>\n",
name
);
}
int main(int argc, char *argv[])
{
const char *execname = basename(argv[0]);
int (*op)(FILE *, FILE *) = encode;
int force = 0, keep = 0;
char *fnin, *fnout = NULL;
size_t fnlen;
int fdout;
FILE *fin = NULL;
FILE *fout = NULL;
int ret = -EINVAL, opt;
while ((opt = getopt(argc, argv, "cdfhkV")) != -1) {
switch (opt) {
case 'c':
fout = stdout;
break;
case 'd':
op = decode;
break;
case 'f':
force = 1;
break;
case 'h':
usage(execname);
return EXIT_SUCCESS;
case 'k':
keep = 1;
break;
case 'V':
printf("rle %s\n", utility_version);
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
}
if (optind == argc) {
fnin = "-";
} else {
fnin = argv[optind];
}
if (strcmp(fnin, "-") == 0) {
keep = 1;
fin = stdin;
fout = stdout;
}
if (!fin) {
fin = fopen(fnin, "rb");
if (!fin) {
PR_ERR_SYS("%s", fnin);
goto exit;
}
}
if (!fout) {
fnlen = strlen(fnin);
if (op == encode) {
if (!force && fnlen > 4 &&
!strcmp(&fnin[fnlen - 4], ".rle")) {
PR_ERR("%s: Filename already has `.rle' suffix",
fnin);
goto exit;
}
fnout = malloc(fnlen + 4 + 1);
if (!fnout) {
PR_ERR_SYS_ONLY();
goto exit;
}
sprintf(fnout, "%s.rle", fnin);
} else {
if (fnlen <= 4 || strcmp(&fnin[fnlen - 4], ".rle")) {
PR_ERR("%s: Filename has an unknown suffix",
fnin);
goto exit;
}
fnout = malloc(fnlen - 4 + 1);
if (!fnout) {
PR_ERR_SYS_ONLY();
goto exit;
}
memcpy(fnout, fnin, fnlen - 4);
fnout[fnlen - 4] = '\0';
}
if (force && unlink(fnout) && errno != ENOENT) {
PR_ERR_SYS("%s: Can not remove", fnout);
goto exit;
}
fdout = open(fnout, O_WRONLY | O_NOCTTY | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR);
if (fdout == -1) {
PR_ERR_SYS("%s", fnout);
goto exit;
}
fout = fdopen(fdout, "wb");
if (!fout) {
close(fdout);
PR_ERR_SYS_ONLY();
goto exit;
}
}
if (op == encode && isatty(fileno(fout)) && !force) {
PR_ERR("Compressed data can not be written to a terminal.");
PR_ERR("Try `%s -h' for more information.", execname);
goto exit;
}
ret = op(fin, fout);
if (!ret && !keep) {
fclose(fin);
fin = NULL;
if (unlink(fnin)) {
PR_ERR_SYS("%s: Can not remove", fnin);
ret = -errno;
}
}
exit:
if (fin && fin != stdin)
fclose(fin);
if (fout && fout != stdout)
fclose(fout);
free(fnout);
return ret ? EXIT_SUCCESS : EXIT_FAILURE;
}