-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrle-enc.awk
68 lines (62 loc) · 1.18 KB
/
rle-enc.awk
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
# Encode input using Konami RLE compression
#
# Intended to be run with LC_ALL=C and 'basenc --base16 -w2' as input and
# 'basenc --base16 -d' on output
# input is pairs of hex digits each followed by newline
# output is chunks of hex digits, with newlines between each chunk
BEGIN {
runlen=0
}
runlen != 0 && lastbyte == $0 {
runlen=runlen+1
if (runlen == 128) {
printf("%02X", runlen)
print lastbyte
runlen=0
}
next
}
runlen != 0 && lastbyte != $0 {
printf("%02X", runlen)
print lastbyte
runlen=0
}
{
literal=""
secondlastbyte=$0
if (!getline) {
print "01" secondlastbyte
exit
}
if (secondlastbyte == $0) {
lastbyte=$0
runlen=2
next
}
lastbyte=$0
while (getline) {
if (secondlastbyte == lastbyte && lastbyte == $0) {
printf("%02X", length(literal)/2+128)
print literal
runlen=3
next
}
literal=literal secondlastbyte
if (length(literal)/2+2 == 126) {
printf("%02X", length(literal)/2+2+128)
print literal lastbyte $0
next
}
secondlastbyte=lastbyte
lastbyte=$0
}
printf("%02X", length(literal)/2+2+128)
print literal secondlastbyte lastbyte
}
END {
if (runlen != 0) {
printf("%02X", runlen)
print lastbyte
}
print "FF"
}