-
Notifications
You must be signed in to change notification settings - Fork 62
/
kernel_version_parser.cpp
73 lines (60 loc) · 2.09 KB
/
kernel_version_parser.cpp
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
#pragma once
#include "kernel_version_parser.h"
#include <sstream>
#ifndef MIN
#define MIN(x, y)(x < y) ? (x) : (y)
#endif // !MIN
KernelVersionParser::KernelVersionParser(const std::vector<char>& file_buf) : m_file_buf(file_buf)
{
}
KernelVersionParser::~KernelVersionParser()
{
}
// Helper function to extract and print the version number starting from the given index
std::string KernelVersionParser::extract_version(const std::vector<char>& buffer, size_t start_index) {
std::string version;
// Extract until we hit a non-version character or end of buffer
while (start_index < buffer.size() && (isdigit(buffer[start_index]) || buffer[start_index] == '.')) {
version.push_back(buffer[start_index]);
++start_index;
}
return version;
}
// Function to search for Linux version patterns using memcmp
std::string KernelVersionParser::find_kernel_versions() {
const size_t safe_end = MIN(m_file_buf.size(), 256);
const char* prefix = "Linux version ";
const size_t prefix_len = strlen(prefix);
for (size_t i = 0; i + prefix_len <= m_file_buf.size() - safe_end; ++i) {
if (memcmp(m_file_buf.data() + i, prefix, prefix_len) == 0 && isdigit(m_file_buf[i + prefix_len])) {
return extract_version(m_file_buf, i + prefix_len);
}
}
return {};
}
// Helper function to split the version string and convert to integers
std::vector<int> KernelVersionParser::parse_version(const std::string& version) {
std::vector<int> parts;
std::stringstream ss(version);
std::string part;
while (getline(ss, part, '.')) {
parts.push_back(std::stoi(part));
}
// Ensure we always have at least three parts (fill missing parts with zero)
while (parts.size() < 3) {
parts.push_back(0);
}
return parts;
}
// Function to compare two version numbers
bool KernelVersionParser::is_version_less_equal(const std::string& v1, const std::string& v2) {
auto parts1 = parse_version(v1);
auto parts2 = parse_version(v2);
// Compare major, minor, and patch versions
for (int i = 0; i < 3; ++i) {
if (parts1[i] < parts2[i]) return true;
if (parts1[i] > parts2[i]) return false;
}
// If all parts are equal
return true;
}