forked from Josh-Murray/fuzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileTypeAnalyser.go
80 lines (68 loc) · 1.18 KB
/
fileTypeAnalyser.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
package main
import (
"encoding/csv"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
)
func check(e error) {
if e != nil {
log.Fatal(e)
}
}
func isValidXML(data []byte) bool {
err := xml.Unmarshal(data, new(interface{}))
if err == nil {
return true
}
return false
//return xml.Unmarshal(data, new(interface{})) != nil
}
func isValidCSV(file string) bool {
csvFile, _ := os.Open(file)
reader := csv.NewReader(csvFile)
for {
_, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Println(err)
return false
}
}
return true
}
func detectType(file string) {
f, err := os.Open(file)
check(err)
f.Close()
data, err := ioutil.ReadFile(file)
check(err)
//fmt.Println(data)
ext := filepath.Ext(file)
fmt.Print("Extension is: ", ext)
if json.Valid(data) {
fmt.Print(" valid json")
} else {
fmt.Print(" invalid json")
}
if isValidXML(data) {
fmt.Print(" valid xml")
} else {
fmt.Print(" invalid xml")
fmt.Println(xml.Unmarshal(data, new(interface{})))
}
if isValidCSV(file) {
fmt.Print(" valid csv")
} else {
fmt.Print("invalid csv")
}
}
//func main() {
// detectType("mutator.go")
//}