started at ; stage 2 at ; ended at
the challenge for round 11 of code guessing is to compress data. submissions may be written in python, c, rust, javascript or php.
the definition of a "compression algorithm" is pretty lax, so all you have to do is make sure that your algorithm: the APIs for each language are as follows.
p == decompress(compress(p))
for all inputs p
)compress
that takes a bytes
object and returns a compressed bytes
, and a decompress
function that mirrors this.pub fn compress(data: &[u8]) -> impl Borrow<[u8]>
and a corresponding decompress
function.compress
and decompress
that take and return Uint8Array
s.NAME
compress, decompress - compress a series of bytes
SYNOPSIS
int compress(const char *data, size_t len, char **result, size_t *outlen)
int decompress(const char *data, size_t len, char **result, size_t *outlen)
DESCRIPTION
The compress() function compresses a string. The decompress() function, given the output of a previous call to compress(), returns the original string that was compressed.
Both functions have the same interface:
The `data` argument points to a series of bytes, the length of which is given by `len`.
The `result` argument may point to NULL or it may point to a pointer to a pre-allocated output buffer. If it does, `outlen` shall point to the length of this buffer. If the function is successful, the values of `*result` and `*outlen` are updated to contain the result.
RETURN VALUE
On success, compress() and decompress() return 0. On failure, they return a negative error code.
you can download all the entries
written by Oliviaguesses
comments 0
jpeg_2000.rs ASCII text
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
use std::borrow::Borrow;
#[cfg(target_os = "windows")]
const COPYRIGHT: u8 = 1;
#[cfg(not(target_os = "windows"))]
const COPYRIGHT: u8 = 0;
pub fn compress(data: &[u8]) -> impl Borrow<[u8]> {
let mut compressed_data = Vec::default();
let mut same_byte = 0;
let mut same_length = 0;
compressed_data.push(COPYRIGHT + 2);
for (idx, uncompressed_byte) in data.iter().cloned().enumerate() {
if idx % 100 == 0 {
compressed_data.push(COPYRIGHT + 2);
}
if uncompressed_byte == same_byte {
same_length += 1;
if same_length == 32767 {
compressed_data.append(&mut vec![255; 2]);
}
}
else {
if same_length > 127 {
compressed_data.push(((same_length >> 8) + 128) as u8);
compressed_data.push((same_length as u8));
}
else if same_length > 0 {
compressed_data.extend_from_slice(&[same_length as u8 + 64]);
}
let mut incomplete = false;
let new_same = match uncompressed_byte {
0 => 8,
1 => 13,
2 => 14,
3 => 15,
64 => 10,
128 => 11,
192 => 12,
255 => 9,
otherwise if false => {
todo!()
}
_ => {
incomplete = true;
1
}
};
same_byte = uncompressed_byte;
same_length = 0;
compressed_data.push(new_same);
if incomplete {
compressed_data.push(uncompressed_byte);
}
}
}
if same_length > 127 {
compressed_data.push(((same_length >> 8) + 128) as u8);
compressed_data.push((same_length as u8));
}
else if same_length > 0 {
compressed_data.extend_from_slice(&[same_length as u8 + 64]);
}
compressed_data.push(COPYRIGHT + 2);
compressed_data.push(4);
compressed_data
}
union Data {
bulk: (u8, u8),
same: u8,
delta_bottom5: u8,
bigdelta_top4: u8,
special_value: u8,
return_code: u8,
copyright_but: u8,
new: (u8, u8),
teapot: u8
}
pub fn decompress(data: &[u8]) -> impl Borrow<[u8]> {
let mut decompressed_bytes = Vec::new();
let mut same_byte = 0_u8;
let mut next_is_same = false;
let mut next_is_new = false;
let mut copyright = false;
let mut out = 0;
for compressed_byte in data.iter().cloned() {
if next_is_same {
decompressed_bytes.extend_from_slice(&vec![same_byte; compressed_byte.into()]);
next_is_same = false;
}
else if next_is_new {
same_byte = compressed_byte;
decompressed_bytes.push(same_byte);
next_is_new = false;
}
else if compressed_byte > 127 {
decompressed_bytes.append(&mut vec![same_byte; (compressed_byte as usize - 128) << 8]);
next_is_same = true;
}
else if compressed_byte > 63 {
decompressed_bytes.extend(vec![same_byte; usize::from(compressed_byte - 64)])
}
else if compressed_byte > 31 {
let movement = compressed_byte - 48;
if compressed_byte > 47 {
same_byte += movement;
}
else {
same_byte -= movement;
}
decompressed_bytes.extend([same_byte].into_iter());
}
else if compressed_byte > 15 {
let steps = compressed_byte << 4;
same_byte = same_byte.wrapping_add(steps);
decompressed_bytes.push(same_byte);
}
else if compressed_byte > 7 {
same_byte = match compressed_byte {
8 => 0,
9 => 255,
10 => 64,
11 => 128,
12 => 192,
13 => 1,
14 => 2,
15 => 3,
_ => 0
};
decompressed_bytes.push(same_byte);
}
else if compressed_byte > 3 {
let negative = (compressed_byte - 4) / 2 == 1;
let zero = (compressed_byte - 4) % 2 == 0;
out = if negative {
if zero {
-0
} else {
-1
}
} else if zero {
0
} else {
1
};
}
else if compressed_byte > 1 {
if compressed_byte == 3 {
copyright = true;
}
}
else if compressed_byte > 0 {
next_is_new = true;
}
else {
panic!("I'm a teapot");
}
}
if copyright {
panic!("Copyrighted material detected");
}
if out != -0 {
std::process::exit(out);
}
decompressed_bytes
}
#[test]
fn test() {
let input = b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafgdhjks@@@agfh@ggggfgsjdhhjjjhsdjhjsj";
let output = b"\x02\x02\x01a_\x01f\x01g\x01d\x01h\x01j\x01k\x01s\nB\x01a\x01g\x01f\x01h\n\x01gC\x01f\x01g\x01s\x01j\x01d\x01hA\x01jB\x01h\x01s\x01d\x01j\x01h\x01j\x01s\x01j\x02\x04";
assert_eq!(output, compress(input).borrow());
assert_eq!(input, decompress(compress(input).borrow()).borrow());
}
written by VilgotanLguesses
comments 0
post a comment
sus.js ASCII text, with CRLF line terminators
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function compress(sus) {
let sussy = [];
let baka = null;
for(let i=0; i<sus.length; i++) {
if(sus[i] === baka && sussy[sussy.length-1] < 255) {
sussy[sussy.length-1]++;
} else {
sussy.push(sus[i]);
sussy.push(0);
baka = sus[i];
}
}
return Uint8Array.from(sussy);
}
function decompress(sus) {
let sussy = [];
for(let i=0; i<sus.length; i+=2) {
sussy = [...sussy, ...Array(sus[i+1]+1).fill(sus[i])];
}
return Uint8Array.from(sussy);
}
written by MattiDragonguesses
comments 0
post a comment
zip.zip.py ASCII text, with CRLF line terminators
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Example of input that becomes smaller: b'aaaaaaaaaaaa'
def compress(data: bytes) -> bytes:
first,pairs=None,[]
for byte in data:first=byte if first is None else pairs.append((first,byte))
counts=[(pair,pairs.count(pair)) for pair in set(pairs)]
counts.sort(key=lambda count:count[1], reverse=True)
compressed=[count[0] for count in counts[:255]]
return bytes([len(compressed)]+[byte for pair in compressed for byte in pair]+[byte for pair in pairs for byte in ([compressed.index(pair)+1] if pair in compressed else tuple([0])+pair)]+([0, first]*(first!=None)))
def decompress(data: bytes) -> bytes:
first,compressed,literal,out=None,[],0,[]
for byte in data[1:(data[0]*2+1)]: first=byte if first is None else compressed.append((first, byte))
for byte in data[(data[0]*2+1):]:
if literal: _,literal=out.append(byte),(literal+1)%3
elif byte==0:literal=1
else:out+=list(compressed[byte-1])
return bytes(out)
written by GNU Radio Showsguesses
comments 0
post a comment
kolmo.py ASCII text
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
class BrainfuckProgram:
def __init__(self, code, ip, tape, memory, input, output):
self.code = code
self.ip = ip
self.tape = tape
self.memory = memory
self.input = input
self.output = output
def instruction(self):
self.ip += 1
match = 1
if self.code[self.ip] == "+":
self.tape[self.memory] += 1
if self.tape[self.memory] == 256:
self.tape[self.memory] = 0
if self.code[self.ip] == "-":
self.tape[self.memory] -= 1
if self.tape[self.memory] == 0:
self.tape[self.memory] = 256
if self.code[self.ip] == "[":
match = 1
if self.tape[self.memory] == 0:
while match > 0:
self.ip += 1
if self.code[self.ip] == "]":
match -= 1
elif self.code[self.ip] == "[":
match += 1
if self.code[self.ip] == "]":
match = 1
if self.tape[self.memory] != 0:
while match > 0:
self.ip -= 1
if self.code[self.ip] == "[":
match -= 1
elif self.code[self.ip] == "]":
match += 1
if self.code[self.ip] == "<":
self.memory += 1
if self.code[self.ip] == ">":
self.memory -= 1
if self.code[self.ip] == ",":
self.code[self.ip] == 0
self.input = True
if self.code[self.ip] == ".":
self.output += bytes([self.tape[self.memory]])
def decompress(code):
program = BrainfuckProgram(code, 0, [0]*30000, 0, b"", b"")
while True:
try:
program.instruction()
except:
return program.output
def code(n):
program = ""
i = 0
while n > 0:
n -= 1
program += chr(n % 256)
n //= 256
return program
def compress(text):
programs = []
i = 0
while True:
for program in programs:
try:
if program.halted:
program.instruction()
except:
if program.output == text:
return program.code
else:
program.halted = False
programs.append(BrainfuckProgram(code(i), 0, [0]*30000, 0, b"", b""))
programs[len(programs) - 1].halted = True
i += 1
written by IFcoltransGguesses
comments 0
post a comment
Whale-Ratios_or_The_Compressor.php ASCII text
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
<?php
$t1 = <<<END
HARVARD
COLLEGE
LIBRARY
Copyright, 1892,
By Elizabeth S. Melville
Made in U.S.A.
Fifth Impression, November, 1920
Sixth Impression, June, 1921
Seventh Impression, December, 1921
Eighth Impression, February, 1922
PRINTED BY C. H. SIMONDS COMPANY
BOSTON, MASS, U.S.A.
IN TOKEN
OF MY ADMIRATION FOR HIS GENIUS
THIS BOOK IS INSCRIBED
TO
Nathaniel Hawthorne
END;
$t2 = <<<END
Melville, nonetheless, inadvertently provided the context and canvass for some of mankind's
truly most Transcendent Art--proffered not by him, but by Rockwell Kent whose illustrations
accomplished what Melville couldn't--magnificently realized original images evoking Rich
Tableaus with stunning brevity and sparse suggestion.
That Kent's work was not contemporaneous with Melville's matters not Artistically, but is
fortuitous for Melville, as the juxtaposition exposes True Art's triumph over the mundane.
END;
$t3 = <<<END
MELLVILE'S NEW BOOK, MOBY DICK ;
OR THE WHALE, by the author of Typee,
Omoo, White Jacket, &c.
Lossing's Pictorial Field Book of the Revolu-
tion, part 18.
London Labor and the London Poor, part 15.
Spiritual Regeneration, a Charge deliver to
the Clergy of the Diocese of Ohio, by Chas. Pettit
McIlvaine, D. D.
This day received for sale by
TAYLOR & MAURY,
Nov 13 Booksellers, near 9th st.
HERMAN MELVILLE'S NEW NOVEL -
Moby Dick, or the Whale; 1 vol . cloth.
Nov 13 FRANCK TAYLOR
END;
$t4 = <<<END
MOBY-DICK, Or, THE WHALE. By HERMAN MEL-
VILLE 12mo.,pp. 635. Harper & Brothers.
Everybody has heard of the tradition
which is said to prevail among the old salts of
Naatucket and New-Bedford, of a ferocious
monster of a whale, who is proof against all the
arts of harpoonery, and who occasionally amuses
himself with swallowing down a boat's crew
without winking. The present volume is a
" Whaliad," or the Epic that veritable old
leviathan, who "esteemeth iron as straw, and
laughs at the spear, the dart,and the habergeon."
no one being able to "fill his skin With a barbed
iron, or his head with fish-hooks." Mr.Melville
gives us not only the romance of his history, but
a great mass of instruction on the character and
habits of his whole race, with complete details
of the wily stratagems of their pursuers.
The interest of the work pivots on a certain
Captain Ahab, whose enmity to Moby Dick, the
name of the whale-demon, has been aggravated
to monomania. In one rencounter with this ter-
ror of the seas, he suffers a signal defeat ; loses
a leg in the contest ; gets a fire in his brain ; re-
turns home a man with one idea ; feels that he
has a mission ; that he is predestined to defy his
enemy to mortal strife; devotes himself to the
fulfillment of his destiny; with the persist-
ence and cunning of insanity gets possession of
another vessel; ships a weird, supernatural
crew of which Ishmael, the narrator of the
story,is a prominent member ; and after a " wild
huntsman's chase " through unknown sees, is
the only one who remains to tell the destruction
of the ship and the doomed Captain Ahab by the
victorious, indomitable Moby-Dick.
END;
$t5 = <<<END
Survive this journey.
Go with the flow.
Accept what is.
Read page after page after page.
END;
$t6 = <<<'END'
function compress($input)
{
global $sacredtexts;
for ($i=0;$i<6;$i++)
{
$scaredtext = $sacredtexts[$i];
$j = 0;
while (TRUE)
{
$j++;
$top = substr($input, 0, $j);
$position = strpos($scaredtext, $top);
if ($position !== false && strlen($input) >= $j)
{
$good[] = [$j, $position, $i];
}
else
{
break;
}
}
}
if (empty($good))
{
$longness = 0;
$pspsps = 0;
$i = 0;
}
else
{
[$longness, $pspsps, $i] = max($good);
}
$bottom = substr($input, $longness);
$output = $longness . "0w0" . $pspsps . "0w0" . $i . "0w0" . $bottom;
return $output;
}
?><?php
function decompress($input)
{
global $sacredtexts;
$longness = explode("0w0", $input, 2)[0];
$input = explode("0w0", $input, 2)[1];
$pspsps = explode("0w0", $input, 2)[0];
$input = explode("0w0", $input, 2)[1];
$i = explode("0w0", $input, 2)[0];
$input = explode("0w0", $input, 2)[1];
return substr($sacredtexts[$i], $pspsps, $longness) . $input;
}
?><?php
//spacesavings >99.09% (input depending)
END;
?><?php
$sacredtexts = [$t1,$t2,$t3,$t4,$t5,$t6];
eval($t6);
?>
written by BeatButtonguesses
comments 0
post a comment
squish.c ASCII text
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
#include <stdlib.h>
#define __SQUISH_INITSIZE 256
#define __SQUISH_INIT \
if (len == 0) \
return 0; \
if (*result == NULL || *outlen < __SQUISH_INITSIZE) \
{ \
*outlen = __SQUISH_INITSIZE; \
*result = realloc(*result, __SQUISH_INITSIZE * sizeof(char)); \
}
#define __SQUISH_WRITE \
if (*outlen < j + 2) \
{ \
*outlen *= 2; \
*result = realloc(*result, *outlen * sizeof(char)); \
} \
(*result)[j++] = count; \
(*result)[j++] = last;
int compress(const char *data, size_t len, char **result, size_t *outlen)
{
__SQUISH_INIT;
char last = data[0];
unsigned char count = 1;
size_t j = 0;
for (size_t i = 1; i < len; ++i)
{
char now = data[i];
if (now != last || count == 255)
{
__SQUISH_WRITE;
last = now;
count = 1;
}
else
{
++count;
}
}
__SQUISH_WRITE;
*outlen = j;
*result = realloc(*result, *outlen * sizeof(char));
}
int decompress(const char *data, size_t len, char **result, size_t *outlen)
{
__SQUISH_INIT;
size_t j = 0;
for (size_t i = 0; i < len; i += 2)
{
unsigned char len = data[i];
char ch = data[i + 1];
while (*outlen - j < len)
{
*outlen *= 2;
*result = realloc(*result, *outlen * sizeof(char));
}
for (int k = j; k < j + len; ++k)
{
(*result)[k] = ch;
}
j += len;
}
*outlen = j;
*result = realloc(*result, *outlen * sizeof(char));
}
written by gollarkguesses
comments 0
post a comment
py ASCII text, with very long lines (4166)
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
import json, numpy, math, functools, base64
real_bped = numpy.frombuffer(base64.b64decode('ZQAgAHQAIABzACAAdABoAGkAbgBkACAAZQByAGEAbgB5ACAAbwAgAG8AbgAgACAAbwB1AG8AcgBnACAAZQBuAGEAbAAEAQ4BYQAgAHIAZQBhAHIAaQACASAAAwF0AAkBcwB0AGkAAQFpAHQAbwBtAGEAdAAgAHcAeQAMAWgAYQBlAAU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bAClAXQAaQFwAGEAdQBtAGUBdAARARMBBQFoAEABawEJAW8AWAF8AHwAjgGOAeIAjgCJA5UACwIVAmIAaQBFAWYCYgBsAHAAPAEWAUsBaQEtAv4CUQG3AcoBaAAVAXMCiwA9AccCbQAQAWkAGAEvAUcDRQEAAWsAJwEHAQgB8QIpAasBqwFiAOECEQEuATUBYwBiAHkAdgAgAGcACQEHA7UCXQAgACgBbwETAS8ByQE2AbkBdQJvADgBEwEAAUoCMgEYAWQAngEHASoBFAEJAoEBFwEuAR0BcgDaAXMAawF1Ar4CIQFsABsCBAIgAHMAGgIqAccBbgAaAosBZAC8AhIBaAASARABqwI2ADgALAEsAXUAAgHiALUAwwOXAIkB4QEyADAAZQAiAWsBgAFVAbICYgBzAI8BAAEPATUBbwBRAQ8B8wEGARwBbQAqAnQBAgFjAGQAYQAEAR0BAAE7ACAA3AEpAWQAmAEHAXQAzgEyAYkBIAFoAQUBdwCfAo0DcwF5ADICrQFUAWIAbAEtACAAQgFPASUBLgEUAQgBZgBpAfIBNgErAW4AYQBBARYBDAJiACkBdgARARoBEQFkACkBdQFwALsBAgFvADIB/AI8ATQBYwBEARQBQQJBAngCeAKEApECKwECAb8DCAF0AGkBcwBsAGMCCAF3AGEA1QEAAUcBUQHdATIB'), dtype=numpy.uint32)
bped_inv = {}
for i in range(256, 1024):
x = real_bped[i - 256]
a, b = x & 0xFFFF, x >> 16
bped_inv[a, b] = i
N_bits = 14
X_bits = 2 ** N_bits
mask = X_bits - 1
qfreqs = numpy.frombuffer(base64.b64decode('AQEBAQEBAQEBAUwBAQEBAQEBAQEBAQEBAQEBAQEBAQGyDTEUDwoKMko0ECE3RFY6N0tFNjctJTA4LxwXGSMeFAQsGB8WIRERDycNFxchEhweBR0jHwsKDQkHBioKIw0vLWd8pH6MbW9qciJPU2JjSYkJYaGdVDxJM0giEwwTDAECBQQEBgEBAgICAwUJAgEBAQIBAgQCAQMEAgEBAQEBBAIBBAECAgQBBgEBAgEBAQMBAQEEAQEB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QYFBQYGBgUGBQUEBQUGBQQFBQYFBgUFBQUGBQUFBQUFBQYGBgYFBQYEBQQFBQIDBQYFBQUFBQUEBQ=='), dtype=numpy.uint8)
cdf = numpy.insert(numpy.cumsum(qfreqs, dtype=numpy.uint16), 0, 0)
@functools.cache
def icdf(y):
for i, x in enumerate(cdf):
if y < x: return i - 1
def compress(s):
s = numpy.array(bytearray(s), dtype=numpy.uint16)
while True:
r = numpy.zeros_like(s)
z = 0
used = False
for pair in zip(s, s[1:]):
if used:
used = False
else:
mp = bped_inv.get(pair)
if mp:
r[z] = mp
z += 1
used = True
else:
r[z] = pair[0]
z += 1
used = False
if not used:
r[z] = s[-1]
z += 1
if z == len(s): break
s = r[:z]
def C(x, s):
s_count = int(qfreqs[s])
return (x // s_count) * X_bits + int(cdf[s]) + (x % s_count)
x = 1
for symbol in s:
x = C(x, symbol)
return len(s).to_bytes(4, 'little') + x.to_bytes(math.ceil(x.bit_length() / 8), 'little')
def decompress(s):
l, x = s[:4], s[4:]
ilen = int.from_bytes(l, 'little')
def D(x):
slot = x & mask
sym = icdf(slot)
prev_state = (x // X_bits) * int(qfreqs[sym]) + slot - int(cdf[sym])
return sym, prev_state
x = int.from_bytes(x, 'little')
syms = []
for _ in range(ilen):
sym, x = D(x)
syms.append(sym)
syms.reverse()
s = numpy.array(syms, dtype=numpy.uint16)
while True:
r = numpy.zeros(len(s) * 2, dtype=numpy.uint16)
z = 0
for c in s:
if c > 255:
x = real_bped[c - 256]
a, b = x & 0xFFFF, x >> 16
r[z] = a
z += 1
r[z] = b
z += 1
else:
r[z] = c
z += 1
if z == len(s): break
s = r[:z]
return bytes(s.astype(numpy.uint8))
written by quintopiaguesses
comments 0
post a comment
qoigentry.h ASCII text, with CRLF line terminators
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
#include <stdlib.h>
#include <string.h>
//Fast lossless image compression and decompression based on QOI
//https://qoiformat.org/qoi-specification.pdf
//Data should be image data as sequence of RBGA bytes
//Probably will fail awefully to compress nonimage data
#define CACHESIZE 43
#define OP_RGB (char)0xFE
#define OP_RGBA (char)0xFF
#define OP_INDEX (char)0
#define OP_DIFF (char)0x40
#define OP_LUMA (char)0x80
#define OP_RUN (char)0xC0
#define OP_CODE (char)0xC0
#define OP_ARGS (char)0x3F
#define OP_LUMA_ARG (char)0x3F
#define OP_INDEX_ARG (char)0x3F
#define HASH(C, H) ((C.red * 3 + C.green * 5 + C.blue * 7 + C.alpha * 11) % H)
#define LRS(a, b) ((unsigned)a >> b)
#define LOCALHASH(C, H) (H + (LRS(C.red + 8, 3) ^ LRS(C.green + 8, 3) ^ \
LRS(C.blue + 8, 3)) % \
(64 - H))
#define TUBITRANGE(a, b) ((char)(a - b) > -3 && (char)(a - b) < 2)
#define COLORRANGES(a, b) TUBITRANGE(a.red, b.red) && \
TUBITRANGE(a.green, b.green) && \
TUBITRANGE(a.blue, b.blue)
#define EQCOLOR(a, b) (a.red == b.red && a.green == b.green && \
a.blue == b.blue && a.alpha == b.alpha)
typedef struct RBGA
{
unsigned char red;
unsigned char green;
unsigned char blue;
unsigned char alpha;
} color;
int compress(const char *data, size_t len, char **result, size_t *outlen)
{
color cache[64] = {0};
color last;
color current = (color){.alpha = 255};
color temp, temp2;
int i;
char j, k, l;
int run = 0;
int ct = 0;
int clen = CACHESIZE;
if (len % 4)
{
return -1;
}
if (*result == NULL)
{
*result = (char *)malloc(5 * len);
}
for (i = 0; i < len; i += 4)
{
//Cache previous pixel
cache[HASH(current, clen)] = current;
last = current;
//Get next pixel
memcpy(¤t, &data[i], 4);
//Try to make run
if (EQCOLOR(current, last) && run < 62)
{
run++;
continue;
}
if (run)
{
(*result)[ct++] = OP_RUN | (run - 1);
run = 0;
if (EQCOLOR(current, last))
{
run++;
continue;
}
}
//Try to make exact diff with previous pixel
if (COLORRANGES(current, last) &&
current.alpha == last.alpha)
{
(*result)[ct++] = OP_DIFF | (current.red - last.red + 2 & 3) << 4 |
(current.green - last.green + 2 & 3) << 2 |
(current.blue - last.blue + 2 & 3);
continue;
}
//Try to make exact index into cache
temp = cache[HASH(current, clen)];
if (EQCOLOR(current, temp))
{
(*result)[ct++] = OP_INDEX | HASH(current, clen) & OP_INDEX_ARG;
continue;
}
//Try to make luma diff with previous pixel
j = current.green - last.green;
if (j > -33 && j < 32 && current.alpha == last.alpha)
{
k = current.red - last.red - j;
l = current.blue - last.blue - j;
if (-9 < k && -9 < l && k < 8 && l < 8)
{
(*result)[ct++] = OP_LUMA | (j + 32 & OP_LUMA_ARG);
(*result)[ct++] = (k + 8 & 15) << 4 | l + 8 & 15;
continue;
}
}
if (64 - clen)
{
//Try to make diff index into cache
j = LOCALHASH(current, clen);
temp = cache[j];
if (COLORRANGES(current, temp) &&
current.alpha == temp.alpha)
{
smalldiff:
(*result)[ct++] = OP_INDEX | j & OP_INDEX_ARG;
(*result)[ct++] = OP_DIFF | (current.red - temp.red + 2 & 3) << 4 |
(current.green - temp.green + 2 & 3) << 2 |
(current.blue - temp.blue + 2 & 3);
continue;
}
//Next just search the entire cache for the nearest color
/*(This section removeable for a slight speed boost and a
slight increase in compressed file size. I believe it
only makes a difference when encoding very dark pixels
nearish the beginning of the file.)*/
for (j = clen; j < 64; j++)
{
temp2 = cache[j];
if (COLORRANGES(current, temp2) && current.alpha == temp2.alpha)
{
temp = temp2;
goto smalldiff;
}
k = current.green - temp2.green;
if (k > -33 && k < 32)
{
k = current.red - temp.red - j;
l = current.blue - temp.blue - j;
if (-9 < k && -9 < l && k < 8 && l < 8)
{
temp = temp2;
l = j;
}
}
}
j = l;
//Try to make luma index into cache
k = current.green - temp.green;
if (k > -33 && k < 32 && current.alpha == temp.alpha)
{
k = current.red - temp.red - j;
l = current.blue - temp.blue - j;
if (-9 < k && -9 < l && k < 8 && l < 8)
{
(*result)[ct++] = OP_INDEX | j & OP_INDEX_ARG;
(*result)[ct++] = OP_LUMA | ((current.green - temp.green) + 32 & 0x3F);
(*result)[ct++] = (k + 8 & 15) << 4 | l + 8 & 15;
continue;
}
}
}
//Try to make RGB or RGBA pixel
if (current.alpha == last.alpha)
{
(*result)[ct++] = OP_RGB;
j = 3;
}
else
{
(*result)[ct++] = OP_RGBA;
j = 4;
}
memcpy(&((*result)[ct]), ¤t, j);
ct += j;
if (clen - 64)
{
cache[LOCALHASH(current, clen)] = current;
}
}
*outlen = (size_t)ct;
return 0;
}
int decompress(const char *data, size_t len, char **result, size_t *outlen)
{
color cache[64] = {0};
color current = (color){.alpha = 255};
int i = 0;
char j;
int buflen;
char *tempbuf;
int run = 0;
int clen = CACHESIZE;
if (*result == NULL)
{
buflen = 5 * len;
*result = (char *)malloc(buflen);
}
else
{
buflen = *outlen & -4;
}
*outlen = 0;
cache[HASH(current, clen)] = current;
for (i = 0; i < len; i++)
{
//Expand buffer if it is full
if (*outlen == buflen)
{
buflen *= 2;
*result = (char *)realloc(*result, buflen);
}
//Add another pixel for current run
if (run)
{
memcpy(&((*result)[*outlen]), ¤t, 4);
*outlen += 4;
run--;
i--;
continue;
}
//Decode next codeword
switch (data[i] & OP_CODE)
{
case OP_INDEX:
j = data[i] & OP_INDEX_ARG;
current = cache[j];
if (j < clen)
break;
i++;
case OP_DIFF:
if ((data[i] & OP_CODE) == OP_DIFF)
{
current.red += (LRS(data[i], 4) & 3) - 2;
current.green += (LRS(data[i], 2) & 3) - 2;
current.blue += (data[i] & 3) - 2;
break;
}
case OP_LUMA:
j = (data[i++] & OP_LUMA_ARG) - 32;
current.green += j;
current.red += j + (LRS(data[i], 4) & 0xF) - 8;
current.blue += j + (data[i] & 0xF) - 8;
break;
case OP_RUN:
if (data[i] == OP_RGB || data[i] == OP_RGBA)
{
memcpy(¤t, &data[i + 1], 3);
if (data[i] == OP_RGBA)
{
current.alpha = data[i + 4];
i++;
}
i += 3;
cache[LOCALHASH(current, clen)] = current;
}
else
{
run = (data[i] & OP_ARGS) + 1;
continue;
}
}
memcpy(&((*result)[*outlen]), ¤t, 4);
cache[HASH(current, clen)] = current;
*outlen += 4;
}
return 0;
}
written by Palaiologosguesses
comments 0
post a comment
potato.py ASCII text
1
2
3
4
5
6
7
import bz2
def compress(input):
bz2.compress(input)
def decompress(input):
bz2.decompress(input)
written by razetimeguesses
comments 0
post a comment
rasmus.php ASCII text
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
<?php /*
* wHY ARE yoU writinG pHP?
* fuck you.
*
* tested on:
* PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )
* Copyright (c) The PHP Group
* Zend Engine v3.4.0, Copyright (c) Zend Technologies
* with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
*/
error_reporting(0); // remove this for fun warnings (bigger input = more fun!)
function compress($s) {
$s = unpack("C*", $s); // why the fuck is this 1 indexed
$z = [];
foreach($s as $v)$z[] = $v;
$dict = array_count_values($s);
// PHP FP makes me want to die:
// $o = array_map(fn($k,$v) => [$k,$v], array_values($dict),array_keys($dict));
$o = [];
foreach($dict as $k => $v) $o[] = [$v, $k];
sort($o);
while (count($o) > 1) {
$row1 = array_shift($o);
$row2 = array_shift($o);
$o[] = array($row1['0'] + $row2[0], array($row1, $row2));
sort($o);
}
$d = [];
fill($d, $o[0][1][0] ? $o[0][1] : $o);
$e = '';
for($i = 0; $i < count($z); $i++) $e .= $d[$z[$i]];
$x = (8 - (strlen($e) % 8));
$e = str_pad($e, strlen($e) + $x,0,0);
$_POST = "";
foreach($d as $k=>$v) $_POST .= $k . p . $v . j;
$_GET = [];
for($i = 0; $i < strlen($e); $i += 8) {
$_GET[] = bindec(substr($e,$i,8));
}
return count($d)+1 .j.$x.j.$_POST.pack("C*", ...$_GET);
}
function fill(&$d, $t, $v = '') {
if (!$t[0][1][0]) {
$d[$t[0][1]] = $v . 0;
} else {
fill($d, $t[0][1], $v . 0);
}
if ($t[1]) {
if (!$t[1][1][0]) {
$d[$t[1][1]] = $v . 1;
} else {
fill($d, $t[1][1], $v . 1);
}
}
}
function decompress($s) {
[$GLOBALS, $p, $r] = explode(j, $s, 3);
$h = [];
$r = explode(j, $r, $GLOBALS);
$s = array_pop($r);
foreach($r as $str) {
[$k,$v] = explode(p, $str);
$h[$v] = $k;
}
$b = "";
foreach(unpack("C*",$s) as $num) {
$b .= str_pad(decbin($num),8,0,0);
}
$b = substr($b, $p);
$_ENV = "";
$f = [];
if(!$b) return "";
foreach (str_split($b) as $c) {
$_ENV .= $c;
if($h[$_ENV]) {
$f[] = $h[$_ENV];
$_ENV = "";
}
}
return pack("C*",...$f); // why the FUCK does this need a spread
}
print($a =compress("sadasdasfdsfsdadasfasdfsdfsdhfghsdhfdshgfshadgfhjsdgfjgsdjfgsdfjhagsdfjhgsdfjhghjsdgjfgahjdsghfgsdf")) ."\n";
print(decompress($a) . "\n");
?>
written by LyricLyguesses
comments 0
post a comment
main.rs ASCII text
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
use std;
use image::{self};
#[allow(unused_imports)] use bitvec::prelude::{BitArray, bitarr, bits, BitOrder, LocalBits, Lsb0, Msb0, BitSlice, BitStore, AsBits, AsMutBits, BitArr, bitbox, bitvec, BitBox, BitVec, BitPtr, BitPtrRange, BitRef};
use {bitvec::{prelude::{*}}};
use itertools::Itertools;
pub fn compress(data: &[u8]) -> impl std::borrow::Borrow<[u8]> {
if data == &[b'b', b'e', b'e', b's'] {
return vec![0];
} else if data.iter().all(|&x| x == 0) {
return [0, 0].repeat(data.len());
}
return data.into_iter().map(|&x| x).collect();
}
pub fn decompress(data: &[u8]) -> impl std::borrow::Borrow<[u8]> {
if data == &[0] {
return vec![b'b', b'e', b'e', b's'];
} else if data.iter().all(|&x| x == 0) {
return [0].repeat(data.len()/2);
}
return data.into_iter().map(|&x| x).collect();
}
pub fn main() {}
#[cfg(target_os = "macos")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut img = image::io::Reader::open("rights.png")?.decode()?;
let mut file = std::fs::File::open("src/main.rs")?;
let bytes = { let mut vec = Vec::new(); std::io::Read::read_to_end(&mut file, &mut vec)?; vec };
let mut bits = bytes.view_bits::<Lsb0>().into_iter();
let (ix, iy, iw, ih) = image::GenericImageView::bounds(&img);
for x in ix..ix+iw {
for (y, bit_chunk) in (iy..iy+ih).zip(&bits.by_ref().chunks(8)) {
let bit_vec: Vec<bool> = bit_chunk.map(|x| *x).collect();
if bit_vec.len() < 8 {
continue;
}
let byte = u8::from_le_bytes([bit_vec[7] as u8*128+bit_vec[6] as u8*64+bit_vec[5] as u8*32+bit_vec[4] as u8*16+bit_vec[3] as u8*8+bit_vec[2] as u8*4+bit_vec[1] as u8*2+bit_vec[0] as u8]);
let bit_arr = [byte>>7&1, byte>>6&1, byte>>5&1, byte>>4&1, byte>>3&1, byte>>2&1, byte>>1&1, byte>>0&1];
let (r, g, b, a) = image::Pixel::channels4(&image::GenericImageView::get_pixel(&img, x, y));
let r = r & 0b11111000 | bit_arr[0] << 2 | bit_arr[1] << 1 | bit_arr[2] << 0;
let g = g & 0b11111100 | bit_arr[3] << 1 | bit_arr[4] << 0;
let b = b & 0b11111000 | bit_arr[5] << 2 | bit_arr[6] << 1 | bit_arr[7] << 0;
let a = a;
let new_pixel = image::Pixel::from_channels(r, g, b, a);
image::GenericImage::put_pixel(&mut img, x, y, new_pixel);
}
}
image::DynamicImage::save(&img, "rights2.png");
Ok(())
}
written by Oliveguesses
comments 0
post a comment
good_compression.py ASCII text, with CRLF line terminators
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
def e(f):
if f==0:return[0]
g=(f+1).bit_length()-1
return [1]*g+[0]+list(map(int,bin(f+1)[-g:]))
def h(i):
j=[]
try:
while 1:
k=0
for l in q:k|=next(i)<<l
j.append(k)
except StopIteration:return bytes(j)
def t(s,r):
q=0
while 1:
p=o=0
while s():p+=1
for _ in range(p):o=(o<<1)|s()
o=(o|1<<p)-1
r+=[q:=q+(-2*(o&1)+1)*(o>>1)-(o&1)]
def compress(z):
w,y=[],0
for x in z:w,y=w+e((lambda i:2*abs(i)-(0>i))(x-y)),x
return h(iter(w+[1]*(-len(w)%8)))
def decompress(v):
try:t((1&c>>d for c,d in __import__("itertools").product(v,q)).__next__,u:=[])
except StopIteration:return bytes(u)
q=range(8)
written by SlaveGirlSofiaguesses
comments 0
post a comment
pz.c ASCII text
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
#include <stdio.h> /* ~~~~ ~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~~-~-~ ~~~~ */
#include <stdlib.h> /* SINGLE PAGE COMPRESSOR AND DECOMPRESSOR. MADE FOR THE ESOLANGS CODE GUESSING EVENT -- 0cad412cafd2fc03d47c72673061a2c3b873d81d */
#include <string.h> /* Contains a suffix array implementation written by Yuta Mori in 2010. Default block size is 65K. Works great on low-entropy data. */
typedef void*V;typedef int I;typedef unsigned U;typedef unsigned char Q;typedef unsigned long long R;Q tb[256];Q*iq,*oq;I ip,op,im;U lo,hi,c;unsigned short t0[256],t1[256][256],t2[2][256][17];I c1,c2,r;I bs=1<<16;
#define H(a)(cs==sizeof(I)?((I*)T)[a]:((Q*)T)[a])
#define DQ(a)unsigned short B0_##a(unsigned short p){return p-(p>>a);}
#define DR(a)unsigned short B1_##a(unsigned short p){return p+((p^65535)>>a);}
void gc(V T,I*C,I n,I k,I cs){I i;for(i=0;i<k;++i)C[i]=0;for(i=0;i<n;++i)++C[H(i)];}DQ(2);DQ(4);DQ(6);DR(2);DR(4);DR(6);
void gb(I*C,I*B,I k,I e){I i,s=0;if(e)for(i=0;i<k;++i)s+=C[i],B[i]=s;else for(i=0;i<k;++i)s+=C[i],B[i]=s-C[i];}void ls1(V T,I*SA,I*C,I*B,I n,I k,I cs){I*b,i,j,c0,c1;if(C==B)gc(T,C,n,k,cs);gb(C,B,k,0);j=n-1;b=SA+B[
c1=H(j)];--j;*b++=(H(j)<c1)?~j:j;for(i=0;i<n;++i){if(0<(j=SA[i])){if((c0=H(j))!=c1)B[c1]=b-SA,b=SA+B[c1=c0];--j;*b++=(H(j)<c1)?~j:j;SA[i]=0;}else if(j<0)SA[i]=~j;}if(C==B)gc(T,C,n,k,cs);gb(C,B,k,1);for(i=n-1,b=SA+
B[c1=0];0<=i;--i){if(0<(j=SA[i])){if((c0=H(j))!=c1)B[c1]=b-SA,b=SA+B[c1=c0];--j;*--b=(H(j)>c1)?~(j+1):j;SA[i]=0;}}}I lp1(V T,I*SA,I n,I m,I cs){I i,j,p,q,pl,ql,nm,c0,c1,diff;for(i=0;(p=SA[i])<0;++i)SA[i]=~p;if(i<m
){for(j=i,++i;;++i){if((p=SA[i])<0){SA[j++]=~p;SA[i]=0;if(j==m)break;}}}i=n-1;j=n-1;c0=H(n-1);do c1=c0;while(0<=--i&&(c0=H(i))>=c1);while(0<=i){do c1=c0;while(0<=--i&&(c0=H(i))<=c1);if(0<=i){SA[m+((i+1)>>1)]=j-i;j
=i+1;do c1=c0;while(0<=--i&&(c0=H(i))>=c1);}}for(i=0,nm=0,q=n,ql=0;i<m;++i){p=SA[i],pl=SA[m+(p>>1)],diff=1;if(pl==ql&&(q+pl)<n){for(j=0;j<pl&&H(p+j)==H(q+j);++j);if(j==pl)diff=0;}if(diff)++nm,q=p,ql=pl;SA[m+(p>>1)
]=nm;}return nm;}void ls2(V T,I*SA,I*C,I*B,I*D,I n,I k,I cs){I*b,i,j,t,d,c0,c1;gb(C,B,k,0);j=n-1;b=SA+B[c1=H(j)];--j;t=(H(j)<c1);j+=n;*b++=t&1?~j:j;for(i=0,d=0;i<n;++i){if(0<(j=SA[i])){if(n<=j)d+=1,j-=n;if((c0=H(j
))!=c1)B[c1]=b-SA,b=SA+B[c1=c0];--j;t=c0;t=(t<<1)|(H(j)<c1);if(D[t]!=d)j+=n,D[t]=d;*b++=(t&1)?~j:j;SA[i]=0;}else if(j<0)SA[i]=~j;}for(i=n-1;0<=i;--i){if(0<SA[i]){if(SA[i]<n){SA[i]+=n;for(j=i-1;SA[j]<n;--j);SA[j]-=
n;i=j;}}}gb(C,B,k,1);for(i=n-1,d+=1,b=SA+B[c1=0];0<=i;--i){if(0<(j=SA[i])){if(n<=j)d+=1,j-=n;if((c0=H(j))!=c1)B[c1]=b-SA,b=SA+B[c1=c0];--j;t=c0;t=(t<<1)|(H(j)>c1);if(D[t]!=d)j+=n,D[t]=d;*--b=(t&1)?~(j+1):j;SA[i]=0
;}}}I lp2(I*SA,I n,I m){I i,j,d,nm;for(i=0,nm=0;(j=SA[i])<0;++i){j=~j;if(n<=j)nm++;SA[i]=j;}if(i<m){for(d=i,++i;;++i){if((j=SA[i])<0){j=~j;if(n<=j)nm++;SA[d++]=j;SA[i]=0;if(d==m)break;}}}if(nm<m){for(i=m-1,d=nm+1;
0<=i;--i){if(n<=(j=SA[i]))j-=n,--d;SA[m+(j>>1)]=d;}}else{for(i=0;i<m;++i)if(n<=(j=SA[i]))j-=n,SA[i]=j;}return nm;}void isa(V T,I*SA,I*C,I*B,I n,I k,I cs){I*b,i,j,c0,c1;if(C==B)gc(T,C,n,k,cs);gb(C,B,k,0);j=n-1;b=SA
+B[c1=H(j)];*b++=(0<j&&H(j-1)<c1)?~j:j;for(i=0;i<n;++i){j=SA[i],SA[i]=~j;if(0<j){--j;if((c0=H(j))!=c1)B[c1]=b-SA,b=SA+B[c1=c0];*b++=(0<j&&H(j-1)<c1)?~j:j;}}if(C==B)gc(T,C,n,k,cs);gb(C,B,k,1);for(i=n-1,b=SA+B[c1=0]
;0<=i;--i){if(0<(j=SA[i])){--j;if((c0=H(j))!=c1)B[c1]=b-SA,b=SA+B[c1=c0];*--b=(!j||H(j-1)>c1)?~j:j;}else{SA[i]=~j;}}}I cb(V T,I*SA,I*C,I*B,I n,I k,I cs){I*b,i,j,pi=-1,c0,c1;if(C==B)gc(T,C,n,k,cs);gb(C,B,k,0);j=n-1
;b=SA+B[c1=H(j)];*b++=((0<j)&&(H(j-1)<c1))?~j:j;for(i=0;i<n;++i){if(0<(j=SA[i])){--j;SA[i]=~(I)(c0=H(j));if(c0!=c1){B[c1]=b-SA;b=SA+B[c1=c0];}*b++=(0<j&&H(j-1)<c1)?~j:j;}else if(j)SA[i]=~j;}if(C==B)gc(T,C,n,k,cs);
gb(C,B,k,1);for(i=n-1,b=SA+B[c1=0];0<=i;--i){if(0<(j=SA[i])){--j;SA[i]=(c0=H(j));if(c0!=c1){B[c1]=b-SA;b=SA+B[c1=c0];}*--b=(0<j&&H(j-1)>c1)?~(I)H(j-1):j;}else if(j)SA[i]=~j;else pi=i;}return pi;}I sm(V T,I*SA,I fs
,I n,I k,I cs,I isbwt){I*C,*B,*D,*RA,*b,i,j,m,p,q,t,nm,pi=0,nf,c0,c1;U fl;if(k<=256){C=malloc(k*sizeof(I));if(k<=fs){B=SA+(n+fs-k);fl=1;}else{B=malloc(k*sizeof(I));fl=3;}}else if(k<=fs){C=SA+(n+fs-k);if(k<=(fs-k))
{B=C-k;fl=0;}else if(k<=1024){B=malloc(k*sizeof(I));fl=2;}else B=C,fl=8;}else{B=C=malloc(k*sizeof(I));fl=12;}if(n<=0x3fffffff&&2<=n/k){if(fl&1)fl|=(k*2<=fs-k)?32:16;else if(!fl&&k*2<=fs-k*2)fl|=32;}gc(T,C,n,k,cs);
gb(C,B,k,1);for(i=0;i<n;++i)SA[i]=0;b=&t;i=n-1;j=n;m=0;c0=H(n-1);do c1=c0;while(0<=--i&&(c0=H(i))>=c1);while(0<=i){do{c1=c0;}while(0<=--i&&(c0=H(i))<=c1);if(0<=i){*b=j;b=SA+--B[c1];j=i;++m;do c1=c0;while(0<=--i&&(
c0=H(i))>=c1);}}if(1<m){if(fl&48){if(fl&16){D=malloc(k*2*sizeof(I));}else{D=B-k*2;}++B[H(j+1)];for(i=0,j=0;i<k;++i){j+=C[i];if(B[i]!=j)SA[B[i]]+=n;D[i]=D[i+k]=0;}ls2(T,SA,C,B,D,n,k,cs);nm=lp2(SA,n,m);if(fl&16)free
(D);}else{ls1(T,SA,C,B,n,k,cs);nm=lp1(T,SA,n,m,cs);}}else if(m==1)*b=j+(nm=1);else nm=0;if(nm<m){if(fl&4)free(C);if(fl&2)free(B);nf=(n+fs)-m*2;if(!(fl&13))if(k+nm<=nf)nf-=k;else fl|=8;RA=SA+m+nf;for(i=m+(n >> 1)-1
,j=m-1;m<=i;--i)if(SA[i])RA[j--]=SA[i]-1;sm(RA,SA,nf,m,nm,sizeof(I),0);i=n-1;j=m-1;c0=H(n-1);do c1=c0;while(0<=--i&&(c0=H(i))>=c1);while(0<=i){do c1=c0;while(0<=--i&&((c0=H(i))<=c1));if(0<=i){RA[j--]=i+1;do c1=c0;
while(0<=--i&&(c0=H(i))>=c1);}}for(i=0;i<m;++i){SA[i]=RA[SA[i]];}if(fl&4)C=B=malloc(k*sizeof(I));if(fl&2)B=malloc(k*sizeof(I));}if(fl&8)gc(T,C,n,k,cs);if(1<m){gb(C,B,k,1);i=m-1,j=n,p=SA[m-1],c1=H(p);do{q=B[c0=c1];
while(q<j)SA[--j]=0;do{SA[--j]=p;if(--i<0)break;p=SA[i];}while((c1=H(p))==c0);}while(0<=i);while(0<j)SA[--j]=0;}if(!isbwt)isa(T,SA,C,B,n,k,cs);else pi=cb(T,SA,C,B,n,k,cs);if(fl&5)free(C);if(fl&2)free(B);return pi;
}I AE(Q*T,Q*U,I n){I i,pi,*A=malloc(sizeof(I)*n);if(n==1){U[0]=T[0];return n;}pi=sm(T,A,0,n,256,sizeof(Q),1);if(pi<0)return pi;U[0]=T[n-1];for(i=0;i<pi;++i){U[i+1]=(Q)A[i];}for(i+=1;i<n;++i){U[i]=(Q)A[i];}free(A);
return pi+1;}Q*AD(Q*T,I n,I pi){I C[256];I*LF=malloc(sizeof(I)*n),i,t;Q*U=malloc(n);for(i=0;i<256;++i)C[i]=0;for(i=0;i<n;++i)LF[i]=C[T[i]]++;for(i=0,t=0;i<256;++i){t+=C[i];C[i]=t-C[i];}for(i=n-1,t=0;0<=i;--i){t=LF
[t]+C[U[i]=T[t]];t+=t<pi;}free(LF);return U;}I mtf(Q*str,I c){Q*q,*p;I sh=0;p=(Q*)malloc(257);memcpy(p,str,256);q=memchr(p,c,256);sh=q-p;memcpy(str+1,p,sh);str[0]=c;free(p);return sh;}void dc(Q*p,I size,Q*sym){I i
,index,c;for(i=0;i<256;i++)tb[i]=i;for(i=0;i<size;i++){c=tb[p[i]];index=mtf(tb,c);sym[i]=c;}sym[size]=0;}void en(Q*sym,I size,Q*p){I i=0;Q c;for(i=0;i<256;i++)tb[i]=i;for(i=0;i<size;i++){c=sym[i];p[i]=mtf(tb,c);}}
Q*pp(Q*s,I l,I*pir){Q*buf=malloc(l),*U=malloc(l);I pi=AE(s,U,l);en(U,l,buf);free(U);*pir=pi;return buf;}Q*dp(Q*s,I l,I pir){Q*buf=malloc(l),*dec;dc(s,l,buf);dec=AD(buf,l,pir);free(buf);return dec;}void wo(Q c){oq[
op++]=c;}Q ri(){if(ip<im)return iq[ip++];else return -1;}void b0(U p){lo+=(((R)(hi-lo)*p)>>18)+1;while((lo^hi)<(1<<24)){wo(lo>>24);lo<<=8;hi=(hi<<8)+255;}}void b1(U p){hi=lo+(((R)(hi-lo)*p)>>18);while((lo^hi)<(1<<
24)){wo(lo>>24);lo<<=8;hi=(hi<<8)+255;}}void F(){for(I i=0;i<4;++i){wo(lo>>24);lo<<=8;}}void N(){for(I i=0;i<4;++i)c=(c<<8)+ri();}I db(U p){U mid=lo+((R)(hi-lo)*p>>18);I b=(c<=mid);if(b)hi=mid;else lo=mid+1;while(
(lo^hi)<(1<<24)){lo<<=8;hi=(hi<<8)+255;c=(c<<8)+ri();}return b;}void nw(){c1=c2=r=lo=c=0;hi=(U)-1;for(I i=0;i<256;i++)t0[i]=1<<15;for(I i=0;i<256;i++)for(I j=0;j<256;j++)t1[i][j]=1<<15;for(I i=0;i<2;++i)for(I j=0;
j<256;++j)for(I k=0;k<17;++k)t2[i][j][k]=(k<<12)-(k==16);}void ed(U n){for(I i=0;i<32;++i){if(n&(1<<31))b1(1<<17);else b0(1<<17);n+=n;}}U dd(){U n=0;for(I i=0;i<32;++i){n+=n+db(1<<17);}return n;}void ef(I c){if(c1
==c2)++r;else r=0;I f=(r>2),ctx=1;while(ctx<256){I p0=t0[ctx],p1=t1[c1][ctx],p2=t1[c2][ctx],p=((p0+p1)*7+p2+p2)>>4,j=p>>12,x1=t2[f][ctx][j],x2=t2[f][ctx][j+1],ssep=x1+(((x2-x1)*(p&4095))>>12),bit=c&128;c+=c;if(bit
){b1(ssep*3+p);t0[ctx]=B1_2(t0[ctx]);t1[c1][ctx]=B1_4(t1[c1][ctx]);t2[f][ctx][j]=B1_6(t2[f][ctx][j]);t2[f][ctx][j+1]=B1_6(t2[f][ctx][j+1]);ctx+=ctx+1;}else{b0(ssep*3+p);t0[ctx]=B0_2(t0[ctx]);t1[c1][ctx]=B0_4(t1[c1
][ctx]);t2[f][ctx][j]=B0_6(t2[f][ctx][j]);t2[f][ctx][j+1]=B0_6(t2[f][ctx][j+1]);ctx+=ctx;}}c2=c1;c1=ctx&255;}I df(){if(c1==c2)++r;else r=0;I f=(r>2),ctx=1;while(ctx<256){I p0=t0[ctx],p1=t1[c1][ctx],p2=t1[c2][ctx],
p=((p0+p1)*7+p2+p2)>>4,j=p>>12,x1=t2[f][ctx][j],x2=t2[f][ctx][j+1],ssep=x1+(((x2-x1)*(p&4095))>>12),bit=db(ssep*3+p);if(bit){t0[ctx]=B1_2(t0[ctx]);t1[c1][ctx]=B1_4(t1[c1][ctx]);t2[f][ctx][j]=B1_6(t2[f][ctx][j]);t2
[f][ctx][j+1]=B1_6(t2[f][ctx][j+1]);ctx+=ctx+1;}else{t0[ctx]=B0_2(t0[ctx]);t1[c1][ctx]=B0_4(t1[c1][ctx]);t2[f][ctx][j]=B0_6(t2[f][ctx][j]);t2[f][ctx][j+1]=B0_6(t2[f][ctx][j+1]);ctx+=ctx;}}c2=c1;return c1=ctx&255;}
I compress(Q*buf,size_t size,char**res,size_t*ds){if(!size)return 1;nw();oq=malloc(size*2);op=0;I bsz=bs>size?size:bs,ch=size/bsz,pir,re;ed(size);for(I i=0;i<ch;i++){Q*p=pp(buf+i*bsz,bsz,&pir);ed(bsz);ed(pir);for(
I j=0;j<bsz;j++)ef(p[j]);free(p);}if(ch*bsz!=size){re=size-ch*bsz;Q*p=pp(buf+(size-re),re,&pir);ed(re);ed(pir);for(I j=0;j<re;j++)ef(p[j]);free(p);}ed(0);F();*ds=op;*res=oq;return 0;}I decompress(Q*buf,size_t size
,char**res,size_t*ds){iq=buf;im=size;ip=0;nw();N();I bsz=0,n,ms=dd(),idx;Q*data=NULL;oq=malloc(ms+1);op=0;while((n=dd())>0){if(!bsz){data=malloc(bsz=n);}idx=dd();for(I i=0;i<n;i++)data[i]=df();Q*buf=dp(data,n,idx)
;for(I i=0;i<n;i++)oq[op++]=buf[i];free(buf);}if(data)free(data);*ds=ms;*res=oq;return 0;}I main(I argc,char*argv[]){if(argc<3){printf("Usage: %s [-c/-d] <input> <output>\n",argv[0]);return 1;}FILE*f=fopen(argv[2]
,"rb");if(!f){printf("Could not open %s\n",argv[1]);return 1;}fseek(f,0,SEEK_END);I size=ftell(f);fseek(f,0,SEEK_SET);Q*data=malloc(size);fread(data,1,size,f);fclose(f);size_t ds;Q*dest;if(argv[1][1]=='c')compress
(data,size,&dest,&ds);else decompress(data,size,&dest,&ds);free(data);f=fopen(argv[3],"wb");if(!f){printf("Could not open %s\n",argv[2]);return 1;}fwrite(dest,1,ds,f);fclose(f);free(dest);}
post a comment