-
Notifications
You must be signed in to change notification settings - Fork 29
/
siegfried.go
209 lines (179 loc) · 6.59 KB
/
siegfried.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
// Copyright 2014 Richard Lehane. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Core siegfried defaults
package config
import (
"io"
"net/http"
"path/filepath"
"time"
)
var siegfried = struct {
version [3]int // Siegfried version (i.e. of the sf tool)
home string // Home directory used by both sf and roy tools
signature string // Name of signature file
conf string // Name of the conf file
magic []byte // Magic bytes to ID signature file
// Defaults for processing bytematcher signatures. These control the segmentation.
distance int // The acceptable distance between two frames before they will be segmented (default is 8192)
rng int // The acceptable range between two frames before they will be segmented (default is 0-2049)
choices int // The acceptable number of plain sequences generated from a single segment
// Config for using the update service.
updateURL string // URL for the update service (a JSON file that indicates whether update necessary and where can be found)
updateTimeout time.Duration
updateTransport *http.Transport
// Archivematica format policy registry service
fpr string
// DEBUG and SLOW modes
debug bool
slow bool
out io.Writer
checkpoint int64
}{
version: [3]int{1, 7, 11},
signature: "default.sig",
conf: "sf.conf",
magic: []byte{'s', 'f', 0x00, 0xFF},
distance: 8192,
rng: 4096,
choices: 128,
updateURL: "https://www.itforarchivists.com/siegfried/update", // "http://localhost:8081/siegfried/update",
updateTimeout: 30 * time.Second,
updateTransport: &http.Transport{Proxy: http.ProxyFromEnvironment},
fpr: "/tmp/siegfried",
checkpoint: 524288, // point at which to report slow signatures (must be power of two)
}
// GETTERS
// Version reports the siegfried version.
func Version() [3]int {
return siegfried.version
}
// Home reports the siegfried HOME location (e.g. /usr/home/siegfried).
func Home() string {
return siegfried.home
}
// Home makes a path local to Home() if it is relative
func Local(base string) string {
if filepath.Dir(base) == "." {
return filepath.Join(siegfried.home, base)
}
return base
}
// Signature returns the path to the siegfried signature file.
func Signature() string {
return Local(siegfried.signature)
}
// SignatureBase returns the filename of the siegfried signature file.
func SignatureBase() string {
return siegfried.signature
}
// Conf returns the path to the siegfried configuration file.
func Conf() string {
return Local(siegfried.conf)
}
// Magic returns the magic string encoded at the start of a siegfried signature file.
func Magic() []byte {
return siegfried.magic
}
// Distance is a bytematcher setting. It controls the absolute widths at which segments in signatures are split.
// E.g. if segments are separated by a minimum of 50 and maximum of 100 bytes, the distance is 100.
// A short distance means a smaller Aho Corasick search tree and more patterns to follow-up.
// A long distance means a larger Aho Corasick search tree and more signatures immediately satisfied without follow-up pattern matching.
func Distance() int {
return siegfried.distance
}
// Range is a bytematcher setting. It controls the relative widths at which segments in signatures are split.
// E.g. if segments are separated by a minimum of 50 and maximum of 100 bytes, the range is 50.
// A small range means a smaller Aho Corasick search tree and more patterns to follow-up.
// A large range means a larger Aho Corasick search tree and more signatures immediately satisfied without follow-up pattern matching.
func Range() int {
return siegfried.rng
}
// Choices is a bytematcher setting. It controls the number of tolerable strings produced by processing signature segments.
// E.g. signature has two adjoining frames ("PDF") and ("1.1" OR "1.2") it can be processed into two search strings: "PDF1.1" and "PDF1.2".
// A low number of choices means a smaller Aho Corasick search tree and more patterns to follow-up.
// A large of choices means a larger Aho Corasick search tree and more signatures immediately satisfied without follow-up pattern matching.
func Choices() int {
return siegfried.choices
}
// UpdateOptions returns the update URL, timeout and transport for the sf -update command.
func UpdateOptions() (string, time.Duration, *http.Transport) {
return siegfried.updateURL, siegfried.updateTimeout, siegfried.updateTransport
}
// Fpr reports whether sf is being run in -fpr (Archivematica format policy registry) mode.
func Fpr() string {
return siegfried.fpr
}
// Debug reports whether debug logging is activated.
func Debug() bool {
return siegfried.debug
}
// Slow reports whether slow logging is activated.
func Slow() bool {
return siegfried.slow
}
// Out reports the target for logging messages (STDOUT or STDIN).
func Out() io.Writer {
return siegfried.out
}
// Checkpoint reports the offset at which slow logging should trigger.
func Checkpoint(i int64) bool {
return i == siegfried.checkpoint
}
// SETTERS
// SetHome sets the siegfried HOME location (e.g. /usr/home/siegfried).
func SetHome(h string) {
siegfried.home = h
}
// SetSignature sets the signature filename or filepath.
func SetSignature(s string) {
siegfried.signature = s
}
// SetConf sets the configuration filename or filepath.
func SetConf(s string) {
siegfried.conf = s
}
// SetDistance sets the distance variable for the bytematcher.
func SetDistance(i int) func() private {
return func() private {
siegfried.distance = i
return private{}
}
}
// SetRange sets the range variable for the bytematcher.
func SetRange(i int) func() private {
return func() private {
siegfried.rng = i
return private{}
}
}
// SetDistance sets the choices variable for the bytematcher.
func SetChoices(i int) func() private {
return func() private {
siegfried.choices = i
return private{}
}
}
// SetDebug sets degub logging on.
func SetDebug() {
siegfried.debug = true
}
// SetSlow sets slow logging on.
func SetSlow() {
siegfried.slow = true
}
// SetOut sets the target for logging.
func SetOut(o io.Writer) {
siegfried.out = o
}