SlHelpers
CVEHashMap.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #pragma once
4 
5 #include <algorithm>
6 #include <filesystem>
7 #include <optional>
8 #include <set>
9 #include <string>
10 #include <unordered_map>
11 #include <vector>
12 
13 namespace SlCVEs {
14 
18 class CVEHashMap {
19 public:
21  using CVEHashMapTy = std::unordered_multimap<std::string, std::string>;
23  using SHAHashMapTy = std::unordered_map<std::string, std::string>;
24 
26  enum struct ShaSize {
27  Long,
28  Short
29  };
30 
31  CVEHashMap() = delete;
32 
42  static std::optional<CVEHashMap> create(const std::filesystem::path &vsource,
43  ShaSize shaSize, const std::string &branch,
44  unsigned year, bool rejected);
45 
51  std::string get_cve(const std::string &sha_commit) const {
52  const auto it = m_shaHashMap.find(sha_commit);
53  if (it != m_shaHashMap.cend())
54  return it->second;
55 
56  return {};
57  }
58 
64  std::vector<std::string> get_shas(const std::string &cve_number) const { //requires (S == ShaSize::Long)
65  std::vector<std::string> ret;
66  const auto range = m_cveHashMap.equal_range(cve_number);
67  std::transform(range.first, range.second, std::back_inserter(ret),
68  [](const auto &p) { return p.second; });
69  return ret;
70  }
71 
76  std::set<std::string> get_all_cves() const { //requires (S == ShaSize::Long)
77  std::set<std::string> ret;
78  std::transform(m_cveHashMap.cbegin(), m_cveHashMap.cend(),
79  std::inserter(ret, ret.end()),
80  [](const auto &p) { return p.first; });
81  return ret;
82  }
83 
84 private:
85  CVEHashMap(CVEHashMapTy cveMap, SHAHashMapTy shaMap) :
86  m_cveHashMap(std::move(cveMap)), m_shaHashMap(std::move(shaMap)) {}
87 
88  CVEHashMapTy m_cveHashMap;
89  SHAHashMapTy m_shaHashMap;
90 };
91 
92 }
std::set< std::string > get_all_cves() const
Get all stored CVE numbers.
Definition: CVEHashMap.h:76
std::vector< std::string > get_shas(const std::string &cve_number) const
Get SHAs for cve_number.
Definition: CVEHashMap.h:64
std::unordered_multimap< std::string, std::string > CVEHashMapTy
CVE -> upstream SHA mapping.
Definition: CVEHashMap.h:21
std::unordered_map< std::string, std::string > SHAHashMapTy
Upstream SHA -> CVE mapping.
Definition: CVEHashMap.h:23
Definition: CVE.h:8
A map between CVE numbers and upstream SHAs.
Definition: CVEHashMap.h:18
ShaSize
Store Long or Short SHAs.
Definition: CVEHashMap.h:26
static std::optional< CVEHashMap > create(const std::filesystem::path &vsource, ShaSize shaSize, const std::string &branch, unsigned year, bool rejected)
Create a new CVEHashMap.
std::string get_cve(const std::string &sha_commit) const
Get CVE number for sha_commit.
Definition: CVEHashMap.h:51