-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
201 lines (175 loc) · 5.68 KB
/
main.go
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
/*
* (c) Copyright 2021 by Einar Saukas. All rights reserved.
* (c) Copyright 2024 by Artur 'Mojzesh' Torun. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The name of its author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
"flag"
"fmt"
"os"
"strconv"
"github.com/mojzesh/zx0-go/zx0"
)
const (
MAX_OFFSET_ZX0 = 32640
MAX_OFFSET_ZX7 = 2176
DEFAULT_THREADS = 4
)
func parseInt(s string) (int, error) {
return strconv.Atoi(s)
}
func reverse(array []byte) {
for i, j := 0, len(array)-1; i < j; i, j = i+1, j-1 {
array[i], array[j] = array[j], array[i]
}
}
func zx0Fn(input []byte, skip int, backwardsMode, classicMode, quickMode bool, threads int, verbose bool, delta []int) []byte {
var mode int
if quickMode {
mode = MAX_OFFSET_ZX7
} else {
mode = MAX_OFFSET_ZX0
}
return zx0.NewCompressor().Compress(
zx0.NewOptimizer().Optimize(input, skip, mode, threads, verbose),
input, skip, backwardsMode, !classicMode && !backwardsMode, delta)
}
func dzx0Fn(input []byte, backwardsMode, classicMode bool) ([]byte, error) {
return zx0.NewDecompressor().Decompress(input, backwardsMode, !classicMode && !backwardsMode)
}
func main() {
fmt.Println("ZX0 v2.2: Optimal data compressor by Einar Saukas")
fmt.Println("Ported to Go by Artur 'Mojzesh' Torun")
// process optional parameters
var threads int
var forcedMode, classicMode, backwardsMode, quickMode, decompress bool
var skip int
flag.IntVar(&threads, "p", DEFAULT_THREADS, "Parallel processing with N threads, if p <= 0\nthen all available CPUs are used")
flag.BoolVar(&forcedMode, "f", false, "Force overwrite of output file")
flag.BoolVar(&classicMode, "c", false, "Classic file format (v1.*)")
flag.BoolVar(&backwardsMode, "b", false, "Compress backwards")
flag.BoolVar(&quickMode, "q", false, "Quick non-optimal compression")
flag.BoolVar(&decompress, "d", false, "Decompress")
flag.IntVar(&skip, "s", 0, "Skip N bytes")
flag.Parse()
args := flag.Args()
if len(args) < 1 || len(args) > 2 {
fmt.Println("Usage: zx0 [-pN] [-f] [-c] [-b] [-q] [-d] input [output.zx0]")
os.Exit(1)
}
if decompress && skip > 0 {
fmt.Println("Error: Decompressing with suffix not supported")
os.Exit(1)
}
// determine output filename
var outputName string
if len(args) == 1 {
if !decompress {
outputName = args[0] + ".zx0"
} else {
if len(args[0]) > 4 && args[0][len(args[0])-4:] == ".zx0" {
outputName = args[0][:len(args[0])-4]
} else {
fmt.Println("Error: Cannot infer output filename")
os.Exit(1)
}
}
} else {
outputName = args[1]
}
// read input file
input, err := os.ReadFile(args[0])
if err != nil {
fmt.Printf("Error: Cannot read input file %s\n", args[0])
os.Exit(1)
}
// determine input size
if len(input) == 0 {
fmt.Printf("Error: Empty input file %s\n", args[0])
os.Exit(1)
}
// validate skip against input size
if skip >= len(input) {
fmt.Printf("Error: Skipping entire input file %s\n", args[0])
os.Exit(1)
}
// check output file
if !forcedMode && fileExists(outputName) {
fmt.Printf("Error: Already existing output file %s\n", outputName)
os.Exit(1)
}
// conditionally reverse input file
if backwardsMode {
reverse(input)
}
// generate output file
var output []byte
delta := []int{0}
if !decompress {
output = zx0Fn(input, skip, backwardsMode, classicMode, quickMode, threads, true, delta)
} else {
output, err = dzx0Fn(input, backwardsMode, classicMode)
if err != nil {
fmt.Printf("Error: Invalid input file %s\n", args[0])
os.Exit(1)
}
}
// conditionally reverse output file
if backwardsMode {
reverse(output)
}
// write output file
err = os.WriteFile(outputName, output, 0644)
if err != nil {
fmt.Printf("Error: Cannot write output file %s\n", outputName)
os.Exit(1)
}
var backwardsModeStr string
if backwardsMode {
backwardsModeStr = "backwards "
} else {
backwardsModeStr = ""
}
// done!
if !decompress {
var compTypeStr string
if skip > 0 {
compTypeStr = "partially "
} else {
compTypeStr = ""
}
fmt.Printf("File %scompressed %sfrom %d to %d bytes! (delta %d)\n",
compTypeStr,
backwardsModeStr,
len(input)-skip, len(output), delta[0])
} else {
fmt.Printf("File decompressed %sfrom %d to %d bytes!\n",
backwardsModeStr,
len(input)-skip, len(output))
}
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}