~nabijaczleweli/klapki

ref: bfaaf3b0ee87d924825987cdd6449a38d6d9627e klapki/src/state_config.cpp -rw-r--r-- 6.8 KiB
bfaaf3b0наб Sort our boot entries. Handle KLAPKI_EFI_ROOT better maybe? 1 year, 26 days ago
                                                                                
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// The MIT License (MIT)

// Copyright (c) 2020 наб <nabijaczleweli@nabijaczleweli.xyz>

// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#include "state.hpp"
#include <algorithm>
#include <arpa/inet.h>
#include <fmt/format.h>


#define SUB "\x1A"


int klapki::state::stated_config::parse(klapki::state::stated_config & into, const void * _data, std::size_t size) {
	if(size < 2) {
		errno = ENODATA;
		return -1;
	}

	const char * data = reinterpret_cast<const char *>(_data);
	memcpy(&into.boot_position, data, sizeof(into.boot_position));
	into.boot_position = ntohs(into.boot_position);
	data += sizeof(into.boot_position);
	size -= sizeof(into.boot_position);

	for(;;) {
		const auto variant_end = std::find_if(data, data + size, [](auto b) { return b == '\0'; });
		if(variant_end == data + size) {
			fmt::print(stderr, "extraneous data; -1\n");
			data = data + size;
			size = 0;
			break;
		}

		std::string variant{data, static_cast<std::size_t>(variant_end - data)};
		data += variant.size() + 1;  // NUL
		size -= variant.size() + 1;  // NUL

		if(std::find(std::begin(into.variants), std::end(into.variants), variant) != std::end(into.variants)) {
			fmt::print(stderr, "Duplicate variant {}?", variant);
			continue;
		}

		if(variant.empty())
			break;
		else
			into.variants.emplace_back(std::move(variant));
	}

	while(size != 0) {
		stated_config_entry new_entry{};
		if(size <= sizeof(new_entry.bootnum_hint) + sizeof(new_entry.load_option_sha)) {
			fmt::print(stderr, "extraneous data; 0\n");
			break;
		}

		memcpy(&new_entry.bootnum_hint, data, sizeof(new_entry.bootnum_hint));
		new_entry.bootnum_hint = ntohs(new_entry.bootnum_hint);
		data += sizeof(new_entry.bootnum_hint);
		size -= sizeof(new_entry.bootnum_hint);

		memcpy(&new_entry.load_option_sha, data, sizeof(new_entry.load_option_sha));
		data += sizeof(new_entry.load_option_sha);
		size -= sizeof(new_entry.load_option_sha);

		const auto kver_end = std::find_if(data, data + size, [](auto b) { return b == '\0'; });
		if(kver_end == data + size) {
			fmt::print(stderr, "extraneous data; 0.5; have: {:04X}\n", new_entry.bootnum_hint);
			break;
		}
		new_entry.version.assign(data, kver_end - data);
		data += new_entry.version.size() + 1;  // NUL
		size -= new_entry.version.size() + 1;  // NUL
		// fmt::print(stderr, "version: {} ({})\n", new_entry.version, new_entry.version.size());

		const auto kvar_end = std::find_if(data, data + size, [](auto b) { return b == '\0'; });
		if(kvar_end == data + size) {
			fmt::print(stderr, "extraneous data; 0.75; have: {:04X}\n", new_entry.bootnum_hint);
			break;
		}
		new_entry.variant.assign(data, kvar_end - data);
		data += new_entry.variant.size() + 1;  // NUL
		size -= new_entry.variant.size() + 1;  // NUL
		// fmt::print(stderr, "kernel: {} ({})\n", new_entry.variant, new_entry.variant.size());

		const auto kdir_end = std::find_if(data, data + size, [](auto b) { return b == '\0'; });
		if(kdir_end == data + size) {
			fmt::print(stderr, "extraneous data; 1; have: {:04X}\n", new_entry.bootnum_hint);
			break;
		}
		new_entry.kernel_dirname.assign(data, kdir_end - data);
		data += new_entry.kernel_dirname.size() + 1;  // NUL
		size -= new_entry.kernel_dirname.size() + 1;  // NUL
		// fmt::print(stderr, "variant: {} ({})\n", new_entry.kernel_dirname, new_entry.kernel_dirname.size());

		if(size <= sizeof(new_entry.kernel_image_sha)) {
			fmt::print(stderr, "extraneous data; 1.5\n");
			break;
		}
		memcpy(&new_entry.kernel_image_sha, data, sizeof(new_entry.kernel_image_sha));
		data += sizeof(new_entry.kernel_image_sha);
		size -= sizeof(new_entry.kernel_image_sha);

		for(;;) {
			const auto idir_end = std::find_if(data, data + size, [](auto b) { return b == '\0'; });
			if(idir_end == data + size) {
				fmt::print(stderr, "extraneous data; 2\n");
				goto end;  // break outer loop; 2020 and C++ does not have this
			}

			std::string idir{data, static_cast<std::size_t>(idir_end - data)};
			data += idir.size() + 1;  // NUL
			size -= idir.size() + 1;  // NUL

			if(idir.empty())
				break;

			shaa_t idir_sha;
			if(size <= idir_sha.size()) {
				fmt::print(stderr, "extraneous data; 2.5\n");
				break;
			}
			memcpy(&idir_sha[0], data, idir_sha.size());
			data += idir_sha.size();
			size -= idir_sha.size();

			if(idir == SUB)
				new_entry.initrd_dirnames.emplace_back(nonbase_dirname_t{}, idir_sha);
			else
				new_entry.initrd_dirnames.emplace_back(std::move(idir), idir_sha);
		}

		into.wanted_entries.emplace_back(std::move(new_entry));
	}
end:

	return 0;
}


std::vector<std::uint8_t> klapki::state::stated_config::serialise() const {
	std::vector<std::uint8_t> ret;

	auto bp  = htons(this->boot_position);
	auto cur = std::copy(reinterpret_cast<std::uint8_t *>(&bp), reinterpret_cast<std::uint8_t *>(&bp) + sizeof(bp), std::back_inserter(ret));

	for(auto && var : this->variants)
		cur = std::copy(var.data(), var.data() + var.size() + 1, cur);
	*cur++ = '\0';

	for(auto && went : this->wanted_entries) {
		auto bh = htons(went.bootnum_hint);

		cur    = std::copy(reinterpret_cast<std::uint8_t *>(&bh), reinterpret_cast<std::uint8_t *>(&bh) + sizeof(bh), cur);
		cur    = std::copy(went.load_option_sha, went.load_option_sha + sizeof(sha_t), cur);
		cur    = std::copy(std::begin(went.version), std::end(went.version), cur);
		*cur++ = '\0';
		cur    = std::copy(std::begin(went.variant), std::end(went.variant), cur);
		*cur++ = '\0';
		cur    = std::copy(std::begin(went.kernel_dirname), std::end(went.kernel_dirname), cur);
		*cur++ = '\0';
		cur    = std::copy(std::begin(went.kernel_image_sha), std::end(went.kernel_image_sha), cur);

		for(auto && [idir, isha] : went.initrd_dirnames) {
			if(idir)
				cur = std::copy(std::begin(*idir), std::end(*idir), cur);
			else
				cur = std::copy(SUB, (SUB) + std::strlen(SUB), cur);
			*cur++ = '\0';

			cur = std::copy(std::begin(isha), std::end(isha), cur);
		}
		*cur++ = '\0';
	}

	return ret;
}