blob: 94773b77e7e332f9c44c9424c5fab6958cbb5659 [file] [log] [blame]
Tom Rini897a1d92018-06-19 11:21:44 -04001// SPDX-License-Identifier: MIT
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +03002/*
3 * Copyright (C) 2016 The Android Open Source Project
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +03004 */
5
6#include "avb_util.h"
Simon Glass336d4612020-02-03 07:36:16 -07007#include <malloc.h>
Igor Opaniukd8f9d2a2018-06-03 21:56:36 +03008
9#include <stdarg.h>
10
11uint32_t avb_be32toh(uint32_t in) {
12 uint8_t* d = (uint8_t*)&in;
13 uint32_t ret;
14 ret = ((uint32_t)d[0]) << 24;
15 ret |= ((uint32_t)d[1]) << 16;
16 ret |= ((uint32_t)d[2]) << 8;
17 ret |= ((uint32_t)d[3]);
18 return ret;
19}
20
21uint64_t avb_be64toh(uint64_t in) {
22 uint8_t* d = (uint8_t*)&in;
23 uint64_t ret;
24 ret = ((uint64_t)d[0]) << 56;
25 ret |= ((uint64_t)d[1]) << 48;
26 ret |= ((uint64_t)d[2]) << 40;
27 ret |= ((uint64_t)d[3]) << 32;
28 ret |= ((uint64_t)d[4]) << 24;
29 ret |= ((uint64_t)d[5]) << 16;
30 ret |= ((uint64_t)d[6]) << 8;
31 ret |= ((uint64_t)d[7]);
32 return ret;
33}
34
35/* Converts a 32-bit unsigned integer from host to big-endian byte order. */
36uint32_t avb_htobe32(uint32_t in) {
37 union {
38 uint32_t word;
39 uint8_t bytes[4];
40 } ret;
41 ret.bytes[0] = (in >> 24) & 0xff;
42 ret.bytes[1] = (in >> 16) & 0xff;
43 ret.bytes[2] = (in >> 8) & 0xff;
44 ret.bytes[3] = in & 0xff;
45 return ret.word;
46}
47
48/* Converts a 64-bit unsigned integer from host to big-endian byte order. */
49uint64_t avb_htobe64(uint64_t in) {
50 union {
51 uint64_t word;
52 uint8_t bytes[8];
53 } ret;
54 ret.bytes[0] = (in >> 56) & 0xff;
55 ret.bytes[1] = (in >> 48) & 0xff;
56 ret.bytes[2] = (in >> 40) & 0xff;
57 ret.bytes[3] = (in >> 32) & 0xff;
58 ret.bytes[4] = (in >> 24) & 0xff;
59 ret.bytes[5] = (in >> 16) & 0xff;
60 ret.bytes[6] = (in >> 8) & 0xff;
61 ret.bytes[7] = in & 0xff;
62 return ret.word;
63}
64
65int avb_safe_memcmp(const void* s1, const void* s2, size_t n) {
66 const unsigned char* us1 = s1;
67 const unsigned char* us2 = s2;
68 int result = 0;
69
70 if (0 == n) {
71 return 0;
72 }
73
74 /*
75 * Code snippet without data-dependent branch due to Nate Lawson
76 * (nate@root.org) of Root Labs.
77 */
78 while (n--) {
79 result |= *us1++ ^ *us2++;
80 }
81
82 return result != 0;
83}
84
85bool avb_safe_add_to(uint64_t* value, uint64_t value_to_add) {
86 uint64_t original_value;
87
88 avb_assert(value != NULL);
89
90 original_value = *value;
91
92 *value += value_to_add;
93 if (*value < original_value) {
94 avb_error("Overflow when adding values.\n");
95 return false;
96 }
97
98 return true;
99}
100
101bool avb_safe_add(uint64_t* out_result, uint64_t a, uint64_t b) {
102 uint64_t dummy;
103 if (out_result == NULL) {
104 out_result = &dummy;
105 }
106 *out_result = a;
107 return avb_safe_add_to(out_result, b);
108}
109
110bool avb_validate_utf8(const uint8_t* data, size_t num_bytes) {
111 size_t n;
112 unsigned int num_cc;
113
114 for (n = 0, num_cc = 0; n < num_bytes; n++) {
115 uint8_t c = data[n];
116
117 if (num_cc > 0) {
118 if ((c & (0x80 | 0x40)) == 0x80) {
119 /* 10xx xxxx */
120 } else {
121 goto fail;
122 }
123 num_cc--;
124 } else {
125 if (c < 0x80) {
126 num_cc = 0;
127 } else if ((c & (0x80 | 0x40 | 0x20)) == (0x80 | 0x40)) {
128 /* 110x xxxx */
129 num_cc = 1;
130 } else if ((c & (0x80 | 0x40 | 0x20 | 0x10)) == (0x80 | 0x40 | 0x20)) {
131 /* 1110 xxxx */
132 num_cc = 2;
133 } else if ((c & (0x80 | 0x40 | 0x20 | 0x10 | 0x08)) ==
134 (0x80 | 0x40 | 0x20 | 0x10)) {
135 /* 1111 0xxx */
136 num_cc = 3;
137 } else {
138 goto fail;
139 }
140 }
141 }
142
143 if (num_cc != 0) {
144 goto fail;
145 }
146
147 return true;
148
149fail:
150 return false;
151}
152
153bool avb_str_concat(char* buf,
154 size_t buf_size,
155 const char* str1,
156 size_t str1_len,
157 const char* str2,
158 size_t str2_len) {
159 uint64_t combined_len;
160
161 if (!avb_safe_add(&combined_len, str1_len, str2_len)) {
162 avb_error("Overflow when adding string sizes.\n");
163 return false;
164 }
165
166 if (combined_len > buf_size - 1) {
167 avb_error("Insufficient buffer space.\n");
168 return false;
169 }
170
171 avb_memcpy(buf, str1, str1_len);
172 avb_memcpy(buf + str1_len, str2, str2_len);
173 buf[combined_len] = '\0';
174
175 return true;
176}
177
178void* avb_malloc(size_t size) {
179 void* ret = avb_malloc_(size);
180 if (ret == NULL) {
181 avb_error("Failed to allocate memory.\n");
182 return NULL;
183 }
184 return ret;
185}
186
187void* avb_calloc(size_t size) {
188 void* ret = avb_malloc(size);
189 if (ret == NULL) {
190 return NULL;
191 }
192
193 avb_memset(ret, '\0', size);
194 return ret;
195}
196
197char* avb_strdup(const char* str) {
198 size_t len = avb_strlen(str);
199 char* ret = avb_malloc(len + 1);
200 if (ret == NULL) {
201 return NULL;
202 }
203
204 avb_memcpy(ret, str, len);
205 ret[len] = '\0';
206
207 return ret;
208}
209
210const char* avb_strstr(const char* haystack, const char* needle) {
211 size_t n, m;
212
213 /* Look through |haystack| and check if the first character of
214 * |needle| matches. If so, check the rest of |needle|.
215 */
216 for (n = 0; haystack[n] != '\0'; n++) {
217 if (haystack[n] != needle[0]) {
218 continue;
219 }
220
221 for (m = 1;; m++) {
222 if (needle[m] == '\0') {
223 return haystack + n;
224 }
225
226 if (haystack[n + m] != needle[m]) {
227 break;
228 }
229 }
230 }
231
232 return NULL;
233}
234
235const char* avb_strv_find_str(const char* const* strings,
236 const char* str,
237 size_t str_size) {
238 size_t n;
239 for (n = 0; strings[n] != NULL; n++) {
240 if (avb_strlen(strings[n]) == str_size &&
241 avb_memcmp(strings[n], str, str_size) == 0) {
242 return strings[n];
243 }
244 }
245 return NULL;
246}
247
248char* avb_replace(const char* str, const char* search, const char* replace) {
249 char* ret = NULL;
250 size_t ret_len = 0;
251 size_t search_len, replace_len;
252 const char* str_after_last_replace;
253
254 search_len = avb_strlen(search);
255 replace_len = avb_strlen(replace);
256
257 str_after_last_replace = str;
258 while (*str != '\0') {
259 const char* s;
260 size_t num_before;
261 size_t num_new;
262
263 s = avb_strstr(str, search);
264 if (s == NULL) {
265 break;
266 }
267
268 num_before = s - str;
269
270 if (ret == NULL) {
271 num_new = num_before + replace_len + 1;
272 ret = avb_malloc(num_new);
273 if (ret == NULL) {
274 goto out;
275 }
276 avb_memcpy(ret, str, num_before);
277 avb_memcpy(ret + num_before, replace, replace_len);
278 ret[num_new - 1] = '\0';
279 ret_len = num_new - 1;
280 } else {
281 char* new_str;
282 num_new = ret_len + num_before + replace_len + 1;
283 new_str = avb_malloc(num_new);
284 if (new_str == NULL) {
285 goto out;
286 }
287 avb_memcpy(new_str, ret, ret_len);
288 avb_memcpy(new_str + ret_len, str, num_before);
289 avb_memcpy(new_str + ret_len + num_before, replace, replace_len);
290 new_str[num_new - 1] = '\0';
291 avb_free(ret);
292 ret = new_str;
293 ret_len = num_new - 1;
294 }
295
296 str = s + search_len;
297 str_after_last_replace = str;
298 }
299
300 if (ret == NULL) {
301 ret = avb_strdup(str_after_last_replace);
302 if (ret == NULL) {
303 goto out;
304 }
305 } else {
306 size_t num_remaining = avb_strlen(str_after_last_replace);
307 size_t num_new = ret_len + num_remaining + 1;
308 char* new_str = avb_malloc(num_new);
309 if (new_str == NULL) {
310 goto out;
311 }
312 avb_memcpy(new_str, ret, ret_len);
313 avb_memcpy(new_str + ret_len, str_after_last_replace, num_remaining);
314 new_str[num_new - 1] = '\0';
315 avb_free(ret);
316 ret = new_str;
317 ret_len = num_new - 1;
318 }
319
320out:
321 return ret;
322}
323
324/* We only support a limited amount of strings in avb_strdupv(). */
325#define AVB_STRDUPV_MAX_NUM_STRINGS 32
326
327char* avb_strdupv(const char* str, ...) {
328 va_list ap;
329 const char* strings[AVB_STRDUPV_MAX_NUM_STRINGS];
330 size_t lengths[AVB_STRDUPV_MAX_NUM_STRINGS];
331 size_t num_strings, n;
332 uint64_t total_length;
333 char *ret = NULL, *dest;
334
335 num_strings = 0;
336 total_length = 0;
337 va_start(ap, str);
338 do {
339 size_t str_len = avb_strlen(str);
340 strings[num_strings] = str;
341 lengths[num_strings] = str_len;
342 if (!avb_safe_add_to(&total_length, str_len)) {
343 avb_fatal("Overflow while determining total length.\n");
344 break;
345 }
346 num_strings++;
347 if (num_strings == AVB_STRDUPV_MAX_NUM_STRINGS) {
348 avb_fatal("Too many strings passed.\n");
349 break;
350 }
351 str = va_arg(ap, const char*);
352 } while (str != NULL);
353 va_end(ap);
354
355 ret = avb_malloc(total_length + 1);
356 if (ret == NULL) {
357 goto out;
358 }
359
360 dest = ret;
361 for (n = 0; n < num_strings; n++) {
362 avb_memcpy(dest, strings[n], lengths[n]);
363 dest += lengths[n];
364 }
365 *dest = '\0';
366 avb_assert(dest == ret + total_length);
367
368out:
369 return ret;
370}
371
372const char* avb_basename(const char* str) {
373 int64_t n;
374 size_t len;
375
376 len = avb_strlen(str);
377 if (len >= 2) {
378 for (n = len - 2; n >= 0; n--) {
379 if (str[n] == '/') {
380 return str + n + 1;
381 }
382 }
383 }
384 return str;
385}
386
387void avb_uppercase(char* str) {
388 size_t i;
389 for (i = 0; str[i] != '\0'; ++i) {
390 if (str[i] <= 0x7A && str[i] >= 0x61) {
391 str[i] -= 0x20;
392 }
393 }
394}
395
396char* avb_bin2hex(const uint8_t* data, size_t data_len) {
397 const char hex_digits[17] = "0123456789abcdef";
398 char* hex_data;
399 size_t n;
400
401 hex_data = avb_malloc(data_len * 2 + 1);
402 if (hex_data == NULL) {
403 return NULL;
404 }
405
406 for (n = 0; n < data_len; n++) {
407 hex_data[n * 2] = hex_digits[data[n] >> 4];
408 hex_data[n * 2 + 1] = hex_digits[data[n] & 0x0f];
409 }
410 hex_data[n * 2] = '\0';
411 return hex_data;
412}