forked from richardlehane/siegfried
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
117 lines (101 loc) · 4.67 KB
/
core.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
// 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.
// Package core defines the Identifier, Identification, Recorder, Matcher and Result interfaces.
// The packages within core (bytematcher, containermatcher, etc.) provide a toolkit for building identifiers based on different signature formats (such as PRONOM).
package core
import (
"errors"
"github.com/richardlehane/siegfried/config"
"github.com/richardlehane/siegfried/pkg/core/persist"
"github.com/richardlehane/siegfried/pkg/core/siegreader"
)
// Identifier describes the implementation of a signature format. E.g. there is a PRONOM identifier that implements the TNA's PRONOM format.
type Identifier interface {
Add(Matcher, MatcherType) (Matcher, error)
Recorder() Recorder // return a recorder for matching
Name() string
Details() string
Fields() []string // first element must be the identifier name
Save(*persist.LoadSaver)
String() string // A string represention of the global identifier
Inspect(...string) string // String representation of format signatures within the identifier
GraphP() string // a dot graph representation of the priority map within the identifier (if present)
Recognise(MatcherType, int) (bool, string) // do you recognise this result index?
}
// Add additional identifier types here
const (
Pronom byte = iota // Pronom is the TNA's PRONOM file format registry
MIMEInfo
LOC
)
// IdentifierLoader unmarshals an Identifer from a LoadSaver.
type IdentifierLoader func(*persist.LoadSaver) Identifier
var loaders = [8]IdentifierLoader{nil, nil, nil, nil, nil, nil, nil, nil}
// RegisterIdentifier allows external packages to add new IdentifierLoaders.
func RegisterIdentifier(id byte, l IdentifierLoader) {
loaders[int(id)] = l
}
// LoadIdentifier applies the appropriate IdentifierLoader to load an identifier.
func LoadIdentifier(ls *persist.LoadSaver) Identifier {
id := ls.LoadByte()
l := loaders[int(id)]
if l == nil {
if ls.Err == nil {
ls.Err = errors.New("bad identifier loader")
}
return nil
}
return l(ls)
}
// Recorder is a mutable object generated by an identifier. It records match results and sends identifications.
type Recorder interface {
Record(MatcherType, Result) bool // Record results for each matcher; return true if match recorded (siegfried will iterate through the identifiers until an identifier returns true).
Satisfied(MatcherType) (bool, int) // Called before matcher starts - should we continue onto this matcher?
Report(chan Identification) // Now send results.
Active(MatcherType) // Instruct Recorder that can expect results of type MatcherType.
}
// Identification is sent by an identifier when a format matches
type Identification interface {
String() string // short text that is displayed to indicate the format match
Known() bool // does this identifier produce a match
Warn() string // identification warning message
YAML() string // long text that should be displayed to indicate the format match
JSON() string // JSON match response
CSV() []string // CSV match response. Can be any length slice, but must be same length as Fields() returned by Identifier
Archive() config.Archive // does this format match any of the archive formats (zip, gzip, tar, warc, arc)
}
// Matcher does the matching (against the name/mime string or the byte stream) and sends results
type Matcher interface {
Identify(string, *siegreader.Buffer, ...int) (chan Result, error) // Given a name/MIME string and bytes, identify the file. Exclude excludes identifiers from an identification run.
String() string
}
// MatcherType is used by recorders to tell which type of matcher has sent a result
type MatcherType int
// Add additional Matchers here
const (
NameMatcher MatcherType = iota
MIMEMatcher
ContainerMatcher
ByteMatcher
TextMatcher
XMLMatcher
RIFFMatcher
)
// SignatureSet is added to a matcher. It can take any form, depending on the matcher.
type SignatureSet interface{}
// Result is a raw hit that matchers pass on to Identifiers
type Result interface {
Index() int
Basis() string
}