/
enhancer.go
179 lines (151 loc) · 5.04 KB
/
enhancer.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
// SPDX-License-Identifier: Apache-2.0
package identifier
import (
"regexp"
"strings"
"github.com/CycloneDX/license-scanner/licenses"
)
type Enhancements struct {
AddNotes string
AddTextBlocks bool
FlagAcceptable bool
FlagCopyrights bool
FlagKeywords bool
// FlagKeywords []string // TODO: JavaScript used this as a bool and later as a list
}
const AlphaNumericPattern = `/[a-zA-Z0-9]+/`
// CopyrightPattern recognizes a block to be flagged as COPYRIGHT
// const COPYRIGHT_PATTERN = /^[^a-z0-9\n]*(?:All rights reserved\.?\s*)?(?:[ \t]*(?:Copyright|Copr\.?|\(c\)|[\xA9]))+\s+[^\n\r]+(?:[^a-z0-9]*All rights reserved\.?)?/gmi;
// The legacy pattern required one-or-more white-spaces AND one-or-more non-carriage-return chars after Copyright. Here we are minimally requiring just the one white-space (or more...).
const CopyrightPattern = `(?i)[^a-z0-9\n]*(?:All rights reserved\.?\s*)?(?:[ \t]*(?:Copyright|Copr\.?|\(c\)|[\xA9]))+\s+[^\n\r]*(?:[^a-z0-9]*All rights reserved\.?)?`
var (
CopyrightRegexp = regexp.MustCompile(CopyrightPattern)
AlphaNumericRegexp = regexp.MustCompile(AlphaNumericPattern)
)
var DefaultKeywordList = []string{
`public domain`,
`Affero`,
`[a-z]*gpl`,
`[a-z-]*commercial[a-z]*`,
`[a-z-]*licen[cs][a-z]*`,
}
// FromOptions adds enabled enhancements (in order)
func FromOptions(licenseResults *IdentifierResults, enhancements Enhancements, licenseLibrary *licenses.LicenseLibrary) error {
if licenseResults == nil {
return nil // no-op, no crash
}
if enhancements.FlagCopyrights {
flagCopyrights(licenseResults)
}
if enhancements.FlagAcceptable {
flagAcceptable(licenseResults, licenseLibrary)
}
if enhancements.FlagKeywords {
if err := flagKeywords(licenseResults); err != nil {
return err
}
}
if len(enhancements.AddNotes) > 0 {
licenseResults.Notes = enhancements.AddNotes
}
if enhancements.FlagAcceptable {
flagEmptyBlocks(licenseResults)
}
return nil
}
func flagCopyrights(licenseResults *IdentifierResults) {
if licenseResults == nil {
return
}
licenseResults.CopyRightStatements = identifyPatternInBlocks(licenseResults, CopyrightRegexp, "COPYRIGHT")
}
func flagAcceptable(licenseResults *IdentifierResults, licenseLibrary *licenses.LicenseLibrary) {
// Don't look for acceptable text if no licenses were found.
if licenseResults == nil || len(licenseResults.Matches) == 0 {
return
}
for _, pattern := range licenseLibrary.AcceptablePatternsMap {
_ = identifyPatternInBlocks(licenseResults, pattern, "ACCEPTABLE")
}
}
func flagKeywords(licenseResults *IdentifierResults) error {
if licenseResults == nil {
return nil
}
keywords := DefaultKeywordList
re, err := regexp.Compile(`(?i)\b` + strings.Join(keywords, "|"+`\b`))
if err != nil {
return err
}
licenseResults.KeywordMatches = identifyPatternInBlocks(licenseResults, re, "KEYWORD")
return nil
}
func flagEmptyBlocks(licenseResults *IdentifierResults) {
if licenseResults == nil || len(licenseResults.Blocks) == 0 {
return
}
offsetToBlock := 0
for _, block := range licenseResults.Blocks {
if len(block.Matches) > 0 {
offsetToBlock += len(block.Text)
continue
}
if !AlphaNumericRegexp.MatchString(block.Text) {
block.Matches = []string{"ACCEPTABLE"}
licenseResults.AcceptablePatternMatches = append(licenseResults.AcceptablePatternMatches, PatternMatch{
Text: block.Text, Begins: offsetToBlock, Ends: offsetToBlock + len(block.Text) - 1,
})
}
offsetToBlock += len(block.Text)
}
}
func identifyPatternInBlocks(licenseResults *IdentifierResults, pattern *regexp.Regexp, label string) []PatternMatch {
if licenseResults == nil {
return nil
}
var patternMatches []PatternMatch
// Iterate over all the blocks.
var newBlocks []Block
offsetToBlock := 0
for _, block := range licenseResults.Blocks {
text := block.Text
// If the block matches anything, move on without searching it.
if len(block.Matches) > 0 {
offsetToBlock += len(text)
newBlocks = append(newBlocks, block) // build new array including old blocks
continue
}
matches := pattern.FindAllStringIndex(text, -1)
// If there were no matches, move on.
if len(matches) == 0 {
offsetToBlock += len(text)
newBlocks = append(newBlocks, block)
continue
}
// Otherwise, create new blocks out of the matching and remaining text.
prev := 0
for _, newBlockMatch := range matches {
start := newBlockMatch[0]
end := newBlockMatch[1]
// If there are pre-match characters, create a block.
if start > prev {
newBlocks = append(newBlocks, Block{Text: text[prev:start]})
}
// append a block for the match
match := text[start:end]
prev = end
newBlocks = append(newBlocks, Block{Text: match, Matches: []string{label}})
// create a new pattern match object.
patternMatches = append(patternMatches, PatternMatch{
Text: match, Begins: offsetToBlock + start, Ends: offsetToBlock + end - 1,
})
}
if prev < len(text) {
newBlocks = append(newBlocks, Block{Text: text[prev:]})
}
// Increment the offset.
offsetToBlock += len(text)
}
licenseResults.Blocks = newBlocks
return patternMatches
}