-
Notifications
You must be signed in to change notification settings - Fork 3
/
magic.go
210 lines (170 loc) · 4.64 KB
/
magic.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
202
203
204
205
206
207
208
209
210
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
// Go bindings for libmagic.
package magic
//#cgo LDFLAGS: -lmagic
//#include <stdlib.h>
//#include <magic.h>
import "C"
import (
"errors"
"unsafe"
)
var (
ConnectionError = errors.New("Magic: Connection is closed.")
ConnectionFailure = errors.New("Magic: Failed to open magic database.")
)
// Path is not documented anywhere, so I have no idea what it does.
// Use at your own discretion.
func Path(file string, flags int) string {
cf := C.CString(file)
defer C.free(unsafe.Pointer(cf))
cr := C.magic_getpath(cf, C.int(flags))
if cr == nil {
return ""
}
return C.GoString(cr)
}
// Magic exposes a set of methods which allow
// us to interact with the magic database as defined
// by libmagic.
type Magic struct {
ptr C.magic_t // Holds the database descriptor.
}
// Open attempts to create a new connection to
// the magic database.
func Open(flags int) (*Magic, error) {
ptr := C.magic_open(C.int(flags))
if ptr == nil {
return nil, ConnectionFailure
}
return &Magic{ptr}, nil
}
// Close closes the database connection.
func (m *Magic) Close() (err error) {
if m.ptr == nil {
return ConnectionError
}
//C.magic_close(m.ptr)
m.ptr = nil
return
}
// File returns a textual description of the contents of the filename argument.
// If the filename is nil, then stdin is used.
func (m *Magic) File(file string) (string, error) {
if m.ptr == nil {
return "", ConnectionError
}
cf := C.CString(file)
defer C.free(unsafe.Pointer(cf))
cr := C.magic_file(m.ptr, cf)
if cr == nil {
return "", m.check()
}
r := C.GoString(cr)
C.free(unsafe.Pointer(cr))
return r, nil
}
// Descriptor returns a textual description of the contents of the
// file descriptor argument.
func (m *Magic) Descriptor(fd int) (string, error) {
if m.ptr == nil {
return "", ConnectionError
}
cr := C.magic_descriptor(m.ptr, C.int(fd))
if cr == nil {
return "", m.check()
}
r := C.GoString(cr)
C.free(unsafe.Pointer(cr))
return r, nil
}
// Buffer returns a textual description of the contents of the buffer argument.
func (m *Magic) Buffer(data []byte) (string, error) {
if m.ptr == nil {
return "", ConnectionError
}
ptr := unsafe.Pointer(&data)
sz := C.size_t(len(data))
cr := C.magic_buffer(m.ptr, ptr, sz)
if cr == nil {
return "", m.check()
}
r := C.GoString(cr)
C.free(unsafe.Pointer(cr))
return r, nil
}
// SetFlags sets the given flags.
//
// Note that using both MIME flags together can also return extra
// information on the charset.
func (m *Magic) SetFlags(flags int) error {
if m.ptr == nil {
return ConnectionError
}
if C.magic_setflags(m.ptr, C.int(flags)) < 0 {
return errors.New("Magic: FlagPreserveATime is not supported by this system.")
}
return m.check()
}
// Load must be used to load the the colon separated list of database files
// passed in as filename, or "" for the default database file before any
// magic queries can performed.
func (m *Magic) Load(file string) error {
if m.ptr == nil {
return ConnectionError
}
cf := C.CString(file)
defer C.free(unsafe.Pointer(cf))
C.magic_load(m.ptr, cf)
return m.check()
}
// Compile can be used to compile the the colon separated list of database
// files passed in as filename, or "" for the default database.
// The compiled files created are named from the basename(1) of each
// file argument with ``.mgc'' appended to it.
func (m *Magic) Compile(file string) error {
if m.ptr == nil {
return ConnectionError
}
cf := C.CString(file)
defer C.free(unsafe.Pointer(cf))
C.magic_compile(m.ptr, cf)
return m.check()
}
// Check can be used to check the validity of entries
// in the colon separated database files passed in as filename,
// or "" the default database.
func (m *Magic) Check(file string) error {
if m.ptr == nil {
return ConnectionError
}
cf := C.CString(file)
defer C.free(unsafe.Pointer(cf))
C.magic_check(m.ptr, cf)
return m.check()
}
// List dumps all magic entries in a human readable format, dumping first the
// entries that are matched against binary files and then the ones that match
// text files. It takes and optional filename argument which is a colon
// separated list of database files, or "" for the default database.
func (m *Magic) List(file string) error {
if m.ptr == nil {
return ConnectionError
}
cf := C.CString(file)
defer C.free(unsafe.Pointer(cf))
C.magic_list(m.ptr, cf)
return m.check()
}
// check checks libmagic for any pending errors.
func (m *Magic) check() error {
if m.ptr == nil {
return ConnectionError
}
cr := C.magic_error(m.ptr)
if cr == nil {
return nil
}
return errors.New(C.GoString(cr))
}