2 * Copyright (C) 2016 The Android Open Source Project
4 * SPDX-License-Identifier: MIT
11 uint32_t avb_be32toh(uint32_t in) {
12 uint8_t* d = (uint8_t*)∈
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]);
21 uint64_t avb_be64toh(uint64_t in) {
22 uint8_t* d = (uint8_t*)∈
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]);
35 /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
36 uint32_t avb_htobe32(uint32_t in) {
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;
48 /* Converts a 64-bit unsigned integer from host to big-endian byte order. */
49 uint64_t avb_htobe64(uint64_t in) {
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;
65 int avb_safe_memcmp(const void* s1, const void* s2, size_t n) {
66 const unsigned char* us1 = s1;
67 const unsigned char* us2 = s2;
75 * Code snippet without data-dependent branch due to Nate Lawson
76 * (nate@root.org) of Root Labs.
79 result |= *us1++ ^ *us2++;
85 bool avb_safe_add_to(uint64_t* value, uint64_t value_to_add) {
86 uint64_t original_value;
88 avb_assert(value != NULL);
90 original_value = *value;
92 *value += value_to_add;
93 if (*value < original_value) {
94 avb_error("Overflow when adding values.\n");
101 bool avb_safe_add(uint64_t* out_result, uint64_t a, uint64_t b) {
103 if (out_result == NULL) {
107 return avb_safe_add_to(out_result, b);
110 bool avb_validate_utf8(const uint8_t* data, size_t num_bytes) {
114 for (n = 0, num_cc = 0; n < num_bytes; n++) {
118 if ((c & (0x80 | 0x40)) == 0x80) {
127 } else if ((c & (0x80 | 0x40 | 0x20)) == (0x80 | 0x40)) {
130 } else if ((c & (0x80 | 0x40 | 0x20 | 0x10)) == (0x80 | 0x40 | 0x20)) {
133 } else if ((c & (0x80 | 0x40 | 0x20 | 0x10 | 0x08)) ==
134 (0x80 | 0x40 | 0x20 | 0x10)) {
153 bool avb_str_concat(char* buf,
159 uint64_t combined_len;
161 if (!avb_safe_add(&combined_len, str1_len, str2_len)) {
162 avb_error("Overflow when adding string sizes.\n");
166 if (combined_len > buf_size - 1) {
167 avb_error("Insufficient buffer space.\n");
171 avb_memcpy(buf, str1, str1_len);
172 avb_memcpy(buf + str1_len, str2, str2_len);
173 buf[combined_len] = '\0';
178 void* avb_malloc(size_t size) {
179 void* ret = avb_malloc_(size);
181 avb_error("Failed to allocate memory.\n");
187 void* avb_calloc(size_t size) {
188 void* ret = avb_malloc(size);
193 avb_memset(ret, '\0', size);
197 char* avb_strdup(const char* str) {
198 size_t len = avb_strlen(str);
199 char* ret = avb_malloc(len + 1);
204 avb_memcpy(ret, str, len);
210 const char* avb_strstr(const char* haystack, const char* needle) {
213 /* Look through |haystack| and check if the first character of
214 * |needle| matches. If so, check the rest of |needle|.
216 for (n = 0; haystack[n] != '\0'; n++) {
217 if (haystack[n] != needle[0]) {
222 if (needle[m] == '\0') {
226 if (haystack[n + m] != needle[m]) {
235 const char* avb_strv_find_str(const char* const* strings,
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) {
248 char* avb_replace(const char* str, const char* search, const char* replace) {
251 size_t search_len, replace_len;
252 const char* str_after_last_replace;
254 search_len = avb_strlen(search);
255 replace_len = avb_strlen(replace);
257 str_after_last_replace = str;
258 while (*str != '\0') {
263 s = avb_strstr(str, search);
268 num_before = s - str;
271 num_new = num_before + replace_len + 1;
272 ret = avb_malloc(num_new);
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;
282 num_new = ret_len + num_before + replace_len + 1;
283 new_str = avb_malloc(num_new);
284 if (new_str == NULL) {
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';
293 ret_len = num_new - 1;
296 str = s + search_len;
297 str_after_last_replace = str;
301 ret = avb_strdup(str_after_last_replace);
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) {
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';
317 ret_len = num_new - 1;
324 /* We only support a limited amount of strings in avb_strdupv(). */
325 #define AVB_STRDUPV_MAX_NUM_STRINGS 32
327 char* avb_strdupv(const char* str, ...) {
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;
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");
347 if (num_strings == AVB_STRDUPV_MAX_NUM_STRINGS) {
348 avb_fatal("Too many strings passed.\n");
351 str = va_arg(ap, const char*);
352 } while (str != NULL);
355 ret = avb_malloc(total_length + 1);
361 for (n = 0; n < num_strings; n++) {
362 avb_memcpy(dest, strings[n], lengths[n]);
366 avb_assert(dest == ret + total_length);
372 const char* avb_basename(const char* str) {
376 len = avb_strlen(str);
378 for (n = len - 2; n >= 0; n--) {
387 void avb_uppercase(char* str) {
389 for (i = 0; str[i] != '\0'; ++i) {
390 if (str[i] <= 0x7A && str[i] >= 0x61) {
396 char* avb_bin2hex(const uint8_t* data, size_t data_len) {
397 const char hex_digits[17] = "0123456789abcdef";
401 hex_data = avb_malloc(data_len * 2 + 1);
402 if (hex_data == NULL) {
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];
410 hex_data[n * 2] = '\0';