-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_as2_content.go
149 lines (131 loc) · 3.88 KB
/
read_as2_content.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
package main
import (
"bufio"
"crypto/sha1"
"encoding/base64"
"encoding/csv"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
)
func sanitizeFile(filename string) error {
// Read the file content
content, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
// Replace "�" with "_"
modifiedContent := strings.ReplaceAll(string(content), "�", "_")
// Write the modified content back to the file
err = ioutil.WriteFile(filename, []byte(modifiedContent), 0644)
if err != nil {
return err
}
return nil
}
func fileSize(filename string) int64 {
println("Getting file size for: " + filename)
fi, err := os.Stat(filename)
if err != nil {
return -1
}
return fi.Size()
}
func readObjects(filename string, endline string) {
println("Reading file: " + filename)
if filename == "" {
filename = "msg_as2_att.txt"
}
f, err := os.Open(filename)
if err != nil {
fmt.Print("Error opening file: " + filename + "\n")
panic(err)
}
defer f.Close()
var content [][]string
var files [][]string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
println("Reading line: " + scanner.Text())
line := scanner.Text()
if strings.HasPrefix(line, endline) {
break
}
fmt.Print("Processing line: " + line + "\n")
content = append(content, strings.Split(line, ";,"))
}
for _, line := range content {
println("Processing line: " + strings.Join(line, ";"))
for _, element := range line {
files = append(files, strings.Split(element, ";"))
}
}
i := 0
tmpFilename := ""
tmpContent := ""
tmpFileID := ""
tmpType := ""
os.MkdirAll("sha1files", os.ModePerm)
os.MkdirAll("files", os.ModePerm)
csvFile, err := os.Create("as2_files.csv")
if err != nil {
println("Error creating file")
panic(err)
}
defer csvFile.Close()
writer := csv.NewWriter(csvFile)
defer writer.Flush()
writer.Write([]string{"p_msg_as2;type;filename;filesize;sha1hash"})
for _, file := range files {
println("Processing file: " + strings.TrimSpace(file[1]) + " " + strings.TrimSpace(file[4]))
if len(file) > 5 && len(strings.TrimSpace(file[8])) > 76 {
data, err := base64.StdEncoding.DecodeString(strings.TrimSpace(file[8]))
if err != nil {
panic(err)
}
filename := filepath.Join("files/", "as2_msg_file_"+strconv.Itoa(i)+"_"+strings.TrimSpace(file[5]))
err = ioutil.WriteFile(filename, data, 0644)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(filepath.Join("sha1files/", fmt.Sprintf("%x", sha1.Sum(data))), data, 0644)
if err != nil {
panic(err)
}
writer.Write([]string{strings.TrimSpace(file[1]) + ";" + strings.TrimSpace(file[4]) + ";" + filename + "; " + strconv.FormatInt(fileSize(filename), 10) + "; " + fmt.Sprintf("%x", sha1.Sum(data))})
tmpFilename = filename
tmpFileID = strings.TrimSpace(file[1])
tmpType = strings.TrimSpace(file[4])
i++
} else if len(file) > 5 && len(strings.TrimSpace(file[8])) == 76 && strings.TrimSpace(file[1]) != tmpFileID {
data, err := base64.StdEncoding.DecodeString(tmpContent)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(tmpFilename, data, 0644)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(filepath.Join("sha1files/", fmt.Sprintf("%x", sha1.Sum(data))), data, 0644)
if err != nil {
panic(err)
}
writer.Write([]string{tmpFileID + ";" + tmpType + ";" + tmpFilename + "; " + strconv.FormatInt(fileSize(tmpFilename), 10) + "; " + fmt.Sprintf("%x", sha1.Sum(data))})
tmpFilename = filepath.Join("files/", "as2_msg_file_"+strconv.Itoa(i)+"_"+strings.TrimSpace(file[5]))
tmpContent = strings.TrimSpace(file[8])
tmpFileID = strings.TrimSpace(file[1])
tmpType = strings.TrimSpace(file[4])
i++
} else if len(strings.TrimSpace(file[0])) <= 76 {
tmpContent += strings.TrimSpace(file[0])
fmt.Println("writing content for " + tmpFilename)
}
}
fmt.Println("done")
}
func main() {
readObjects("msg_as2_att.txt", "#EndOfLine#")
}