FOSSology  4.4.0
Open Source License Compliance by Open Source Software
copyscan.cc
1 /*
2  SPDX-FileCopyrightText: © 2015,2022, Siemens AG
3  Author: Florian Krügel
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #include "copyscan.hpp"
9 #include <cctype>
10 #include <algorithm>
11 #include "regexConfProvider.hpp"
12 
13 const string copyrightType("statement");
21 {
23  rcp.maybeLoad("copyright");
24 
25  regCopyright = rx::regex(rcp.getRegexValue("copyright","REG_COPYRIGHT"),
26  rx::regex_constants::icase);
27 
28  regException = rx::regex(rcp.getRegexValue("copyright","REG_EXCEPTION"),
29  rx::regex_constants::icase);
30  regNonBlank = rx::regex(rcp.getRegexValue("copyright","REG_NON_BLANK"));
31 
32  regSimpleCopyright = rx::regex(rcp.getRegexValue("copyright","REG_SIMPLE_COPYRIGHT"),
33  rx::regex_constants::icase);
34  regSpdxCopyright = rx::regex(rcp.getRegexValue("copyright","REG_SPDX_COPYRIGHT"),
35  rx::regex_constants::icase);
36 }
37 
46 void hCopyrightScanner::ScanString(const string& s, list<match>& out) const
47 {
48 
49  string::const_iterator begin = s.begin();
50  string::const_iterator pos = begin;
51  string::const_iterator end = s.end();
52  while (pos != end)
53  {
54  // Find potential copyright statement
55  rx::smatch results;
56  if (!rx::regex_search(pos, end, results, regCopyright))
57  // No further copyright statement found
58  break;
59  string::const_iterator foundPos = results[0].first;
60 
61  if (!rx::regex_match(foundPos, end, regException))
62  {
73  string::const_iterator j = find(foundPos, end, '\n');
74  while (j != end)
75  {
76  string::const_iterator beginOfLine = j;
77  ++beginOfLine;
78  string::const_iterator endOfLine = find(beginOfLine, end, '\n');
79  if (rx::regex_search(beginOfLine, endOfLine, regSpdxCopyright)){
80  // Found end
81  break;
82  }
83  if (rx::regex_search(beginOfLine, endOfLine, regSimpleCopyright)
84  || !rx::regex_match(beginOfLine, endOfLine, regNonBlank))
85  {
86  // Found end
87  break;
88  }
89  j = endOfLine;
90  }
91  if (j - foundPos >= 301)
92  // Truncate
93  out.push_back(match(foundPos - begin, (foundPos - begin) + 300, copyrightType));
94  else
95  {
96  out.push_back(match(foundPos - begin, j - begin, copyrightType));
97  }
98  pos = j;
99  }
100  else
101  {
102  // An exception: this is not a copyright statement: continue at the end of this statement
103  pos = results[0].second;
104  }
105  }
106 }
107 
Provide regex using conf file.
void maybeLoad(const std::string &identity)
Check if identity already loaded in RegexMap, if not load them.
const char * getRegexValue(const std::string &name, const std::string &key)
Get the regex as string from the RegexMap.
rx::regex regNonBlank
Definition: copyscan.hpp:35
void ScanString(const string &s, list< match > &results) const
Scan a given string for copyright statements.
Definition: copyscan.cc:46
rx::regex regSpdxCopyright
Definition: copyscan.hpp:35
hCopyrightScanner()
Constructor for default hCopyrightScanner.
Definition: copyscan.cc:20
rx::regex regCopyright
Definition: copyscan.hpp:35
rx::regex regSimpleCopyright
Definition: copyscan.hpp:35
rx::regex regException
Definition: copyscan.hpp:35
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:37
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308
Store the results of a regex match.
Definition: scanners.hpp:28