~nabijaczleweli/klapki

ref: cb46a59c26acba4f7b96ec175bc2175034628b88 klapki/src/state_parse.cpp -rw-r--r-- 4.8 KiB
cb46a59cнаб Fold into TRY_OPT() in main(). Don't try to LTO on GCC 1 year, 30 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
// 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 <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)
		return ENODATA;

	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
		// fmt::print(stderr, "variant: {} ({})\n", variant, variant.size());

		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());

		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
			// fmt::print(stderr, "initrd: {} ({})\n", idir, idir.size());

			if(idir.empty())
				break;
			else if(idir == SUB)
				new_entry.initrd_dirnames.emplace_back();
			else
				new_entry.initrd_dirnames.emplace_back(std::move(idir));
		}

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

	return 0;
}