-
Notifications
You must be signed in to change notification settings - Fork 0
/
CutFileDichotomy.go
130 lines (106 loc) · 3.63 KB
/
CutFileDichotomy.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
// This tool cuts a target file recursively
// I used this script to identify malware in archive file without any compression
/*
Copyright (C) 2023 Maurice Lambert
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 <https://www.gnu.org/licenses/>.
*/
// Compilation on Linux:
// env GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -trimpath -o CutFileDichotomy.exe CutFileDichotomy.go
package main
import (
"strconv"
"fmt"
"os"
)
type FileSize struct {
path string
size int64
}
/*
This function writes in a new file a chunk of the source file.
*/
func read_write(file *os.File, newfile FileSize, start int64, maxsize int64) {
halfFile, err := os.Create(newfile.path)
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating a half file:", err)
return
}
defer halfFile.Close()
buffer := make([]byte, newfile.size)
_, err = file.ReadAt(buffer, start)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading a half file:", err)
return
}
_, err = halfFile.Write(buffer)
if err != nil {
fmt.Fprintln(os.Stderr, "Error writing a half file:", err)
return
}
cut(newfile, maxsize)
}
/*
This function cuts a file on the middle and write two files
(one for the first part and one for the second).
*/
func cut(filesize FileSize, maxsize int64) {
if (filesize.size < maxsize) {
return
}
new_file_size := (filesize.size / 2) + 1
file, err := os.Open(filesize.path)
if err != nil {
fmt.Fprintln(os.Stderr, "Error opening a file:", err)
return
}
defer file.Close()
newfile := FileSize{filesize.path + "0", new_file_size}
read_write(file, newfile, 0, maxsize)
newfile.path = filesize.path + "1"
read_write(file, newfile, filesize.size - new_file_size, maxsize)
}
/*
The main function to starts the executable.
*/
func main() {
if len(os.Args) < 2 || len(os.Args) > 3 {
fmt.Fprintln(os.Stderr, "USAGES: CutFileDichotomy.exe [string:filename] (integer:size=500)")
return
}
size := 500;
if len(os.Args) == 3 {
size1, err := strconv.Atoi(os.Args[2])
size = size1
if err != nil {
fmt.Fprintln(os.Stderr, "USAGES: CutFileDichotomy.exe [string:filename] (integer:size=500)")
fmt.Fprintln(os.Stderr, "Invalid 'size' parameter, should be an integer...")
return
}
}
fmt.Println("Start cutting file recursively...")
startFile := FileSize{os.Args[1], 0}
file, err := os.Open(startFile.path)
if err != nil {
fmt.Fprintln(os.Stderr, "Error opening the source file:", err)
return
}
fileInfo, err := file.Stat()
if err != nil {
file.Close()
fmt.Fprintln(os.Stderr, "Error getting file information:", err)
return
}
startFile.size = fileInfo.Size()
file.Close()
cut(startFile, int64(size))
fmt.Println("File successfully cut")
}