summaryrefslogtreecommitdiff
path: root/host/lib/host_misc.c
blob: 899d807f3e8763c1e08826a622dd661b12bdabfd (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
/* Copyright 2011 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Host functions for verified boot.
 */

/* TODO: change all 'return 0', 'return 1' into meaningful return codes */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "host_common.h"

char* StrCopy(char* dest, const char* src, int dest_size)
{
	strncpy(dest, src, dest_size);
	dest[dest_size - 1] = '\0';
	return dest;
}

uint8_t* ReadFile(const char* filename, uint64_t* sizeptr)
{
	FILE* f;
	uint8_t* buf;
	long size;

	f = fopen(filename, "rb");
	if (!f) {
		fprintf(stderr, "Unable to open file %s\n", filename);
		return NULL;
	}

	fseek(f, 0, SEEK_END);
	size = ftell(f);
	if (size < 0) {
		fclose(f);
		return NULL;
	}
	rewind(f);

	buf = malloc(size);
	if (!buf) {
		fclose(f);
		return NULL;
	}

	if(1 != fread(buf, size, 1, f)) {
		fprintf(stderr, "Unable to read from file %s\n", filename);
		fclose(f);
		free(buf);
		return NULL;
	}

	fclose(f);
	if (sizeptr)
		*sizeptr = size;
	return buf;
}

char* ReadFileString(char* dest, int size, const char* filename)
{
	char* got;
	FILE* f;

	f = fopen(filename, "rt");
	if (!f)
		return NULL;

	got = fgets(dest, size, f);
	fclose(f);
	return got;
}

int ReadFileInt(const char* filename, unsigned* value)
{
	char buf[64];
	char* e = NULL;

	if (!ReadFileString(buf, sizeof(buf), filename))
		return -1;

	/* Convert to integer.  Allow characters after the int ("123 blah"). */
	*value = (unsigned)strtoul(buf, &e, 0);
	if (e == buf)
		return -1;  /* No characters consumed, so conversion failed */

	return 0;
}

int ReadFileBit(const char* filename, int bitmask)
{
	unsigned value;
	if (ReadFileInt(filename, &value) < 0)
		return -1;
	else return (value & bitmask ? 1 : 0);
}

vb2_error_t WriteFile(const char* filename, const void *data, uint64_t size)
{
	FILE *f = fopen(filename, "wb");
	if (!f) {
		fprintf(stderr, "Unable to open file %s\n", filename);
		return 1;
	}

	if (1 != fwrite(data, size, 1, f)) {
		fprintf(stderr, "Unable to write to file %s\n", filename);
		fclose(f);
		unlink(filename);  /* Delete any partial file */
		return 1;
	}

	fclose(f);
	return 0;
}

bool parse_hex(uint8_t *val, const char *str)
{
	uint8_t v = 0;
	char c;
	int digit;

	for (digit = 0; digit < 2; digit++) {
		c = *str;
		if (!c)
			return false;
		if (!isxdigit(c))
			return false;
		c = tolower(c);
		if (c >= '0' && c <= '9')
			v += c - '0';
		else
			v += 10 + c - 'a';
		if (!digit)
			v <<= 4;
		str++;
	}

	*val = v;
	return true;
}

bool parse_hash(uint8_t *buf, size_t len, const char *str)
{
	const char *s = str;
	int i;

	for (i = 0; i < len; i++) {
		/* skip whitespace */
		while (*s && isspace(*s))
			s++;
		if (!*s)
			break;
		if (!parse_hex(buf, s))
			break;

		/* on to the next byte */
		s += 2;
		buf++;
	}

	if (i != len || *s)
		return false;
	return true;
}