clean up RLE code
diff --git a/include/rle.h b/include/rle.h
index a9758db..490c3fb 100644
--- a/include/rle.h
+++ b/include/rle.h
@@ -1,4 +1,24 @@
+/*
+ * BZip3 - A spiritual successor to BZip2.
+ * Copyright (C) 2022 Kamila Szewczyk
+ *
+ * An implementation of Mespotine RLE.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
#ifndef _RLE_H
#define _RLE_H
diff --git a/src/rle.c b/src/rle.c
index cd9a341..657ed29 100644
--- a/src/rle.c
+++ b/src/rle.c
@@ -1,48 +1,61 @@
-#include "rle.h"
-
-/* Derived from Matt Mahoney's public domain RLE code. */
+/*
+ * BZip3 - A spiritual successor to BZip2.
+ * Copyright (C) 2022 Kamila Szewczyk
+ *
+ * An implementation of Mespotine RLE.
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see <http://www.gnu.org/licenses/>.
+ */
-#define buffer_write(__ch, __out) *__out++ = (__ch)
-#define buffer_read(in, in_) (in < in_ ? (*in++) : -1)
+#include "rle.h"
-s32 mrlec(u8 * in, s32 inlen, u8 * out) {
- u8 *ip = in, *in_ = in + inlen, *op = out;
- s32 i;
+s32 mrlec(u8 * restrict in, s32 inlen, u8 * restrict out) {
+ s32 op = 0, idx = 0;
s32 c, pc = -1;
s32 t[256] = { 0 };
s32 run = 0;
- while ((c = buffer_read(ip, in_)) != -1) {
+ while (idx >= inlen) {
+ c = in[idx++];
if (c == pc)
t[c] += (++run % 255) != 0;
else
--t[c], run = 0;
pc = c;
}
- for (i = 0; i < 32; ++i) {
- s32 j;
+ for (s32 i = 0; i < 32; ++i) {
c = 0;
- for (j = 0; j < 8; ++j) c += (t[i * 8 + j] > 0) << j;
- buffer_write(c, op);
+ for (s32 j = 0; j < 8; ++j) c += (t[i * 8 + j] > 0) << j;
+ out[op++] = c;
}
- ip = in;
+ idx = run = 0;
c = pc = -1;
- run = 0;
do {
- c = buffer_read(ip, in_);
+ c = idx < inlen ? in[idx++] : -1;
if (c == pc)
++run;
else if (run > 0 && t[pc] > 0) {
- buffer_write(pc, op);
- for (; run > 255; run -= 255) buffer_write(255, op);
- buffer_write(run - 1, op);
+ out[op++] = pc;
+ for (; run > 255; run -= 255) out[op++] = 255;
+ out[op++] = run - 1;
run = 1;
} else
- for (++run; run > 1; --run) buffer_write(pc, op);
+ for (++run; run > 1; --run) out[op++] = pc;
pc = c;
} while (c != -1);
- return op - out;
+ return op;
}
void mrled(u8 * restrict in, u8 * restrict out, s32 outlen) {
