1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2016 The Android Open Source Project
10 uint32_t avb_be32toh(uint32_t in) {
11 uint8_t* d = (uint8_t*)∈
13 ret = ((uint32_t)d[0]) << 24;
14 ret |= ((uint32_t)d[1]) << 16;
15 ret |= ((uint32_t)d[2]) << 8;
16 ret |= ((uint32_t)d[3]);
20 uint64_t avb_be64toh(uint64_t in) {
21 uint8_t* d = (uint8_t*)∈
23 ret = ((uint64_t)d[0]) << 56;
24 ret |= ((uint64_t)d[1]) << 48;
25 ret |= ((uint64_t)d[2]) << 40;
26 ret |= ((uint64_t)d[3]) << 32;
27 ret |= ((uint64_t)d[4]) << 24;
28 ret |= ((uint64_t)d[5]) << 16;
29 ret |= ((uint64_t)d[6]) << 8;
30 ret |= ((uint64_t)d[7]);
34 /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
35 uint32_t avb_htobe32(uint32_t in) {
40 ret.bytes[0] = (in >> 24) & 0xff;
41 ret.bytes[1] = (in >> 16) & 0xff;
42 ret.bytes[2] = (in >> 8) & 0xff;
43 ret.bytes[3] = in & 0xff;
47 /* Converts a 64-bit unsigned integer from host to big-endian byte order. */
48 uint64_t avb_htobe64(uint64_t in) {
53 ret.bytes[0] = (in >> 56) & 0xff;
54 ret.bytes[1] = (in >> 48) & 0xff;
55 ret.bytes[2] = (in >> 40) & 0xff;
56 ret.bytes[3] = (in >> 32) & 0xff;
57 ret.bytes[4] = (in >> 24) & 0xff;
58 ret.bytes[5] = (in >> 16) & 0xff;
59 ret.bytes[6] = (in >> 8) & 0xff;
60 ret.bytes[7] = in & 0xff;
64 int avb_safe_memcmp(const void* s1, const void* s2, size_t n) {
65 const unsigned char* us1 = s1;
66 const unsigned char* us2 = s2;
74 * Code snippet without data-dependent branch due to Nate Lawson
75 * (nate@root.org) of Root Labs.
78 result |= *us1++ ^ *us2++;
84 bool avb_safe_add_to(uint64_t* value, uint64_t value_to_add) {
85 uint64_t original_value;
87 avb_assert(value != NULL);
89 original_value = *value;
91 *value += value_to_add;
92 if (*value < original_value) {
93 avb_error("Overflow when adding values.\n");
100 bool avb_safe_add(uint64_t* out_result, uint64_t a, uint64_t b) {
102 if (out_result == NULL) {
106 return avb_safe_add_to(out_result, b);
109 bool avb_validate_utf8(const uint8_t* data, size_t num_bytes) {
113 for (n = 0, num_cc = 0; n < num_bytes; n++) {
117 if ((c & (0x80 | 0x40)) == 0x80) {
126 } else if ((c & (0x80 | 0x40 | 0x20)) == (0x80 | 0x40)) {
129 } else if ((c & (0x80 | 0x40 | 0x20 | 0x10)) == (0x80 | 0x40 | 0x20)) {
132 } else if ((c & (0x80 | 0x40 | 0x20 | 0x10 | 0x08)) ==
133 (0x80 | 0x40 | 0x20 | 0x10)) {
152 bool avb_str_concat(char* buf,
158 uint64_t combined_len;
160 if (!avb_safe_add(&combined_len, str1_len, str2_len)) {
161 avb_error("Overflow when adding string sizes.\n");
165 if (combined_len > buf_size - 1) {
166 avb_error("Insufficient buffer space.\n");
170 avb_memcpy(buf, str1, str1_len);
171 avb_memcpy(buf + str1_len, str2, str2_len);
172 buf[combined_len] = '\0';
177 void* avb_malloc(size_t size) {
178 void* ret = avb_malloc_(size);
180 avb_error("Failed to allocate memory.\n");
186 void* avb_calloc(size_t size) {
187 void* ret = avb_malloc(size);
192 avb_memset(ret, '\0', size);
196 char* avb_strdup(const char* str) {
197 size_t len = avb_strlen(str);
198 char* ret = avb_malloc(len + 1);
203 avb_memcpy(ret, str, len);
209 const char* avb_strstr(const char* haystack, const char* needle) {
212 /* Look through |haystack| and check if the first character of
213 * |needle| matches. If so, check the rest of |needle|.
215 for (n = 0; haystack[n] != '\0'; n++) {
216 if (haystack[n] != needle[0]) {
221 if (needle[m] == '\0') {
225 if (haystack[n + m] != needle[m]) {
234 const char* avb_strv_find_str(const char* const* strings,
238 for (n = 0; strings[n] != NULL; n++) {
239 if (avb_strlen(strings[n]) == str_size &&
240 avb_memcmp(strings[n], str, str_size) == 0) {
247 char* avb_replace(const char* str, const char* search, const char* replace) {
250 size_t search_len, replace_len;
251 const char* str_after_last_replace;
253 search_len = avb_strlen(search);
254 replace_len = avb_strlen(replace);
256 str_after_last_replace = str;
257 while (*str != '\0') {
262 s = avb_strstr(str, search);
267 num_before = s - str;
270 num_new = num_before + replace_len + 1;
271 ret = avb_malloc(num_new);
275 avb_memcpy(ret, str, num_before);
276 avb_memcpy(ret + num_before, replace, replace_len);
277 ret[num_new - 1] = '\0';
278 ret_len = num_new - 1;
281 num_new = ret_len + num_before + replace_len + 1;
282 new_str = avb_malloc(num_new);
283 if (new_str == NULL) {
286 avb_memcpy(new_str, ret, ret_len);
287 avb_memcpy(new_str + ret_len, str, num_before);
288 avb_memcpy(new_str + ret_len + num_before, replace, replace_len);
289 new_str[num_new - 1] = '\0';
292 ret_len = num_new - 1;
295 str = s + search_len;
296 str_after_last_replace = str;
300 ret = avb_strdup(str_after_last_replace);
305 size_t num_remaining = avb_strlen(str_after_last_replace);
306 size_t num_new = ret_len + num_remaining + 1;
307 char* new_str = avb_malloc(num_new);
308 if (new_str == NULL) {
311 avb_memcpy(new_str, ret, ret_len);
312 avb_memcpy(new_str + ret_len, str_after_last_replace, num_remaining);
313 new_str[num_new - 1] = '\0';
316 ret_len = num_new - 1;
323 /* We only support a limited amount of strings in avb_strdupv(). */
324 #define AVB_STRDUPV_MAX_NUM_STRINGS 32
326 char* avb_strdupv(const char* str, ...) {
328 const char* strings[AVB_STRDUPV_MAX_NUM_STRINGS];
329 size_t lengths[AVB_STRDUPV_MAX_NUM_STRINGS];
330 size_t num_strings, n;
331 uint64_t total_length;
332 char *ret = NULL, *dest;
338 size_t str_len = avb_strlen(str);
339 strings[num_strings] = str;
340 lengths[num_strings] = str_len;
341 if (!avb_safe_add_to(&total_length, str_len)) {
342 avb_fatal("Overflow while determining total length.\n");
346 if (num_strings == AVB_STRDUPV_MAX_NUM_STRINGS) {
347 avb_fatal("Too many strings passed.\n");
350 str = va_arg(ap, const char*);
351 } while (str != NULL);
354 ret = avb_malloc(total_length + 1);
360 for (n = 0; n < num_strings; n++) {
361 avb_memcpy(dest, strings[n], lengths[n]);
365 avb_assert(dest == ret + total_length);
371 const char* avb_basename(const char* str) {
375 len = avb_strlen(str);
377 for (n = len - 2; n >= 0; n--) {
386 void avb_uppercase(char* str) {
388 for (i = 0; str[i] != '\0'; ++i) {
389 if (str[i] <= 0x7A && str[i] >= 0x61) {
395 char* avb_bin2hex(const uint8_t* data, size_t data_len) {
396 const char hex_digits[17] = "0123456789abcdef";
400 hex_data = avb_malloc(data_len * 2 + 1);
401 if (hex_data == NULL) {
405 for (n = 0; n < data_len; n++) {
406 hex_data[n * 2] = hex_digits[data[n] >> 4];
407 hex_data[n * 2 + 1] = hex_digits[data[n] & 0x0f];
409 hex_data[n * 2] = '\0';