Skip to content

Commit a1a47ea

Browse files
author
Enno
committed
- separate TSCrunch into a library and a cli tool
- implements the io.WriterTo interface and accepts an io.Reader as input - added multitscrunch cli tool, for crunching many files in parallel - separated the boot binary into it's own embedded boot.prg file for easy future updates - add go.mod and go.sum to support Go modules
1 parent 28fcca8 commit a1a47ea

File tree

6 files changed

+447
-282
lines changed

6 files changed

+447
-282
lines changed

boot.prg

229 Bytes
Binary file not shown.

cmd/multitscrunch/main.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"os"
8+
"runtime/pprof"
9+
"sync"
10+
"time"
11+
12+
"github.com/staD020/TSCrunch"
13+
)
14+
15+
func usage() {
16+
fmt.Println("TSCrunch 1.3 - binary cruncher, by Antonio Savona")
17+
fmt.Println("Multi-hack by burg, quickly compile multiple files")
18+
fmt.Println("Usage: tscrunch [-raw] [-i] [-q] infile infile infile")
19+
fmt.Println(" -p : input file is a prg, first 2 bytes are discarded.")
20+
fmt.Println(" -i : inplace crunching (disables -raw)")
21+
fmt.Println(" -q : quiet mode")
22+
}
23+
24+
func main() {
25+
err := run()
26+
if err != nil {
27+
log.Printf("error: %v\n", err)
28+
usage()
29+
os.Exit(1)
30+
}
31+
}
32+
33+
func run() error {
34+
t0 := time.Now()
35+
opt := TSCrunch.Options{}
36+
var cpuProfile string
37+
flag.StringVar(&cpuProfile, "cpuprofile", "", "write cpu profile to `file`")
38+
flag.BoolVar(&opt.PRG, "p", false, "")
39+
flag.BoolVar(&opt.QUIET, "q", false, "")
40+
flag.BoolVar(&opt.INPLACE, "i", false, "")
41+
flag.Usage = usage
42+
flag.Parse()
43+
44+
if cpuProfile != "" {
45+
f, err := os.Create(cpuProfile)
46+
if err != nil {
47+
return fmt.Errorf("could not create CPU profile %q: %w", cpuProfile, err)
48+
}
49+
defer f.Close()
50+
if err := pprof.StartCPUProfile(f); err != nil {
51+
return fmt.Errorf("could not start CPU profile: %w", err)
52+
}
53+
defer pprof.StopCPUProfile()
54+
}
55+
56+
inFiles := flag.Args()
57+
if len(inFiles) < 1 {
58+
return fmt.Errorf("not enough args")
59+
}
60+
61+
crunchFiles(opt, inFiles)
62+
63+
if !opt.QUIET {
64+
fmt.Printf("elapsed: %s\n", time.Since(t0))
65+
}
66+
return nil
67+
}
68+
69+
func crunchFiles(opt TSCrunch.Options, ff []string) {
70+
wg := &sync.WaitGroup{}
71+
wg.Add(len(ff))
72+
for _, file := range ff {
73+
go func(file string) {
74+
defer wg.Done()
75+
t1 := time.Now()
76+
in, err := os.Open(file)
77+
if err != nil {
78+
log.Printf("error: %v\n", err)
79+
return
80+
}
81+
defer in.Close()
82+
t, err := TSCrunch.New(opt, in)
83+
if err != nil {
84+
log.Printf("error: %v\n", err)
85+
return
86+
}
87+
f, err := os.Create(file + ".lz")
88+
if err != nil {
89+
log.Printf("error: %v\n", err)
90+
return
91+
}
92+
defer f.Close()
93+
_, err = t.WriteTo(f)
94+
if err != nil {
95+
log.Printf("error: %v\n", err)
96+
return
97+
}
98+
99+
if !opt.QUIET {
100+
fmt.Printf("crunching %q took %s\n\n", file, time.Since(t1))
101+
}
102+
}(file)
103+
}
104+
wg.Wait()
105+
}

cmd/tscrunch/main.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
TSCrunch binary cruncher, by Antonio Savona
3+
*/
4+
package main
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"log"
10+
"os"
11+
"runtime/pprof"
12+
"time"
13+
14+
"github.com/staD020/TSCrunch"
15+
)
16+
17+
func usage() {
18+
fmt.Println("TSCrunch 1.3 - binary cruncher, by Antonio Savona")
19+
fmt.Println("Usage: tscrunch [-p] [-i] [-q] [-x $addr] infile outfile")
20+
fmt.Println(" -p : input file is a prg, first 2 bytes are discarded.")
21+
fmt.Println(" -x $addr: creates a self extracting file (forces -p)")
22+
fmt.Println(" -i : inplace crunching (forces -p)")
23+
fmt.Println(" -q : quiet mode")
24+
}
25+
26+
func main() {
27+
if err := run(); err != nil {
28+
log.Printf("error: %v\n", err)
29+
usage()
30+
os.Exit(1)
31+
}
32+
}
33+
34+
func run() error {
35+
t0 := time.Now()
36+
opt := TSCrunch.Options{STATS: true}
37+
var cpuProfile string
38+
flag.StringVar(&cpuProfile, "cpuprofile", "", "write cpu profile to `file`")
39+
flag.BoolVar(&opt.PRG, "p", false, "")
40+
flag.BoolVar(&opt.QUIET, "q", false, "")
41+
flag.BoolVar(&opt.INPLACE, "i", false, "")
42+
flag.StringVar(&opt.JumpTo, "x", "", "")
43+
flag.Usage = usage
44+
flag.Parse()
45+
46+
if cpuProfile != "" {
47+
f, err := os.Create(cpuProfile)
48+
if err != nil {
49+
return fmt.Errorf("could not create CPU profile %q: %w", cpuProfile, err)
50+
}
51+
defer f.Close()
52+
if err := pprof.StartCPUProfile(f); err != nil {
53+
return fmt.Errorf("could not start CPU profile: %w", err)
54+
}
55+
defer pprof.StopCPUProfile()
56+
}
57+
58+
if flag.NArg() != 2 {
59+
return fmt.Errorf("not enough args")
60+
}
61+
62+
inFilename := flag.Args()[0]
63+
outFilename := flag.Args()[1]
64+
in, err := os.Open(inFilename)
65+
if err != nil {
66+
return err
67+
}
68+
defer in.Close()
69+
opt.STATS = true
70+
t, err := TSCrunch.New(opt, in)
71+
if err != nil {
72+
return err
73+
}
74+
out, err := os.Create(outFilename)
75+
if err != nil {
76+
return err
77+
}
78+
defer out.Close()
79+
_, err = t.WriteTo(out)
80+
if err != nil {
81+
return err
82+
}
83+
if !opt.QUIET {
84+
fmt.Printf("elapsed: %s\n", time.Since(t0))
85+
}
86+
return nil
87+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/staD020/TSCrunch
2+
3+
go 1.17
4+
5+
require github.com/RyanCarrier/dijkstra v1.1.0

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/RyanCarrier/dijkstra v1.1.0 h1:/NDihjfJA3CxFaZz8EdzTwdFKFZDvvB881OVLdakRcI=
2+
github.com/RyanCarrier/dijkstra v1.1.0/go.mod h1:5agGUBNEtUAGIANmbw09fuO3a2htPEkc1jNH01qxCWA=
3+
github.com/RyanCarrier/dijkstra-1 v0.0.0-20170512020943-0e5801a26345 h1:fgSpoKViTSqRb4hjDNj10ig5wUvO0CayCzFdLf6fuRM=
4+
github.com/RyanCarrier/dijkstra-1 v0.0.0-20170512020943-0e5801a26345/go.mod h1:OK4EvWJ441LQqGzed5NGB6vKBAE34n3z7iayPcEwr30=
5+
github.com/albertorestifo/dijkstra v0.0.0-20160910063646-aba76f725f72 h1:uGeGZl8PxSq8VZGG4QK5njJTFA4/G/x5CYORvQVXtAE=
6+
github.com/albertorestifo/dijkstra v0.0.0-20160910063646-aba76f725f72/go.mod h1:o+JdB7VetTHjLhU0N57x18B9voDBQe0paApdEAEoEfw=
7+
github.com/mattomatic/dijkstra v0.0.0-20130617153013-6f6d134eb237 h1:acuCHBjzG7MFTugvx3buC4m5rLDLaKC9J8C9jtlraRc=
8+
github.com/mattomatic/dijkstra v0.0.0-20130617153013-6f6d134eb237/go.mod h1:UOnLAUmVG5paym8pD3C4B9BQylUDC2vXFJJpT7JrlEA=

0 commit comments

Comments
 (0)