summaryrefslogtreecommitdiff
path: root/util/smmstoretool/data.c
blob: 778bf8a6cb21869706393baa1ce8935ef641b971 (plain)
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
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "data.h"

#include <ctype.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "utils.h"

void print_data(const uint8_t data[], size_t data_size, enum data_type type)
{
	if (data_size == 0)
		return;

	switch (type) {
	case DATA_TYPE_BOOL:
		bool value = false;
		for (size_t i = 0; i < data_size; ++i) {
			if (data[i] != 0) {
				value = true;
				break;
			}
		}
		printf("%s\n", value ? "true" : "false");
		break;
	case DATA_TYPE_UINT8:
		if (data_size != 1) {
			fprintf(stderr,
				"warning: expected size of 1, got %zu\n",
				data_size);
		}

		if (data_size >= 1)
			printf("%u\n", *(uint8_t *)data);
		break;
	case DATA_TYPE_UINT16:
		if (data_size != 2) {
			fprintf(stderr,
				"warning: expected size of 2, got %zu\n",
				data_size);
		}

		if (data_size >= 2)
			printf("%u\n", *(uint16_t *)data);
		break;
	case DATA_TYPE_UINT32:
		if (data_size != 4) {
			fprintf(stderr,
				"warning: expected size of 4, got %zu\n",
				data_size);
		}

		if (data_size >= 4)
			printf("%u\n", *(uint32_t *)data);
		break;
	case DATA_TYPE_ASCII:
		for (size_t i = 0; i < data_size; ++i) {
			char c = data[i];
			if (isprint(c))
				printf("%c", c);
		}
		printf("\n");
		break;
	case DATA_TYPE_UNICODE:
		char *chars = to_chars((const CHAR16 *)data, data_size);
		printf("%s\n", chars);
		free(chars);
		break;
	case DATA_TYPE_RAW:
		fwrite(data, 1, data_size, stdout);
		break;
	}
}

static uint64_t parse_uint(const char source[],
			   const char type[],
			   unsigned long long max)
{
	char *end;
	unsigned long long uint = strtoull(source, &end, /*base=*/0);
	if (*end != '\0') {
		fprintf(stderr, "Trailing characters in \"%s\": %s\n",
			source, end);
		return UINT64_MAX;
	}
	if (uint > max) {
		fprintf(stderr, "Invalid %s value: %llu\n", type, uint);
		return UINT64_MAX;
	}

	return uint;
}

void *make_data(const char source[], size_t *data_size, enum data_type type)
{
	switch (type) {
	void *data;
	bool boolean;
	unsigned long long uint;

	case DATA_TYPE_BOOL:
		if (str_eq(source, "true")) {
			boolean = true;
		} else if (str_eq(source, "false")) {
			boolean = false;
		} else {
			fprintf(stderr, "Invalid boolean value: \"%s\"\n",
				source);
			return NULL;
		}

		*data_size = 1;
		data = xmalloc(*data_size);
		*(uint8_t *)data = boolean;
		return data;
	case DATA_TYPE_UINT8:
		uint = parse_uint(source, "uint8", UINT8_MAX);
		if (uint == UINT64_MAX)
			return NULL;

		*data_size = 1;
		data = xmalloc(*data_size);
		*(uint8_t *)data = uint;
		return data;
	case DATA_TYPE_UINT16:
		uint = parse_uint(source, "uint16", UINT16_MAX);
		if (uint == UINT64_MAX)
			return NULL;

		*data_size = 2;
		data = xmalloc(*data_size);
		*(uint16_t *)data = uint;
		return data;
	case DATA_TYPE_UINT32:
		uint = parse_uint(source, "uint32", UINT32_MAX);
		if (uint == UINT64_MAX)
			return NULL;

		*data_size = 4;
		data = xmalloc(*data_size);
		*(uint32_t *)data = uint;
		return data;
	case DATA_TYPE_ASCII:
		*data_size = strlen(source) + 1;
		return strdup(source);
	case DATA_TYPE_UNICODE:
		return to_uchars(source, data_size);
	case DATA_TYPE_RAW:
		fprintf(stderr, "Raw data type is output only\n");
		return NULL;
	}

	return NULL;
}

bool parse_data_type(const char str[], enum data_type *type)
{
	if (str_eq(str, "bool"))
		*type = DATA_TYPE_BOOL;
	else if (str_eq(str, "uint8"))
		*type = DATA_TYPE_UINT8;
	else if (str_eq(str, "uint16"))
		*type = DATA_TYPE_UINT16;
	else if (str_eq(str, "uint32"))
		*type = DATA_TYPE_UINT32;
	else if (str_eq(str, "ascii"))
		*type = DATA_TYPE_ASCII;
	else if (str_eq(str, "unicode"))
		*type = DATA_TYPE_UNICODE;
	else if (str_eq(str, "raw"))
		*type = DATA_TYPE_RAW;
	else
		return false;

	return true;
}