1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2016 The Android Open Source Project
12 uint32_t avb_be32toh(uint32_t in) {
13 uint8_t* d = (uint8_t*)∈
15 ret = ((uint32_t)d[0]) << 24;
16 ret |= ((uint32_t)d[1]) << 16;
17 ret |= ((uint32_t)d[2]) << 8;
18 ret |= ((uint32_t)d[3]);
22 uint64_t avb_be64toh(uint64_t in) {
23 uint8_t* d = (uint8_t*)∈
25 ret = ((uint64_t)d[0]) << 56;
26 ret |= ((uint64_t)d[1]) << 48;
27 ret |= ((uint64_t)d[2]) << 40;
28 ret |= ((uint64_t)d[3]) << 32;
29 ret |= ((uint64_t)d[4]) << 24;
30 ret |= ((uint64_t)d[5]) << 16;
31 ret |= ((uint64_t)d[6]) << 8;
32 ret |= ((uint64_t)d[7]);
36 /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
37 uint32_t avb_htobe32(uint32_t in) {
42 ret.bytes[0] = (in >> 24) & 0xff;
43 ret.bytes[1] = (in >> 16) & 0xff;
44 ret.bytes[2] = (in >> 8) & 0xff;
45 ret.bytes[3] = in & 0xff;
49 /* Converts a 64-bit unsigned integer from host to big-endian byte order. */
50 uint64_t avb_htobe64(uint64_t in) {
55 ret.bytes[0] = (in >> 56) & 0xff;
56 ret.bytes[1] = (in >> 48) & 0xff;
57 ret.bytes[2] = (in >> 40) & 0xff;
58 ret.bytes[3] = (in >> 32) & 0xff;
59 ret.bytes[4] = (in >> 24) & 0xff;
60 ret.bytes[5] = (in >> 16) & 0xff;
61 ret.bytes[6] = (in >> 8) & 0xff;
62 ret.bytes[7] = in & 0xff;
66 int avb_safe_memcmp(const void* s1, const void* s2, size_t n) {
67 const unsigned char* us1 = s1;
68 const unsigned char* us2 = s2;
76 * Code snippet without data-dependent branch due to Nate Lawson
80 result |= *us1++ ^ *us2++;
86 bool avb_safe_add_to(uint64_t* value, uint64_t value_to_add) {
87 uint64_t original_value;
89 avb_assert(value != NULL);
91 original_value = *value;
93 *value += value_to_add;
94 if (*value < original_value) {
95 avb_error("Overflow when adding values.\n");
102 bool avb_safe_add(uint64_t* out_result, uint64_t a, uint64_t b) {
104 if (out_result == NULL) {
108 return avb_safe_add_to(out_result, b);
111 bool avb_validate_utf8(const uint8_t* data, size_t num_bytes) {
115 for (n = 0, num_cc = 0; n < num_bytes; n++) {
119 if ((c & (0x80 | 0x40)) == 0x80) {
128 } else if ((c & (0x80 | 0x40 | 0x20)) == (0x80 | 0x40)) {
131 } else if ((c & (0x80 | 0x40 | 0x20 | 0x10)) == (0x80 | 0x40 | 0x20)) {
134 } else if ((c & (0x80 | 0x40 | 0x20 | 0x10 | 0x08)) ==
135 (0x80 | 0x40 | 0x20 | 0x10)) {
154 bool avb_str_concat(char* buf,
160 uint64_t combined_len;
162 if (!avb_safe_add(&combined_len, str1_len, str2_len)) {
163 avb_error("Overflow when adding string sizes.\n");
167 if (combined_len > buf_size - 1) {
168 avb_error("Insufficient buffer space.\n");
172 avb_memcpy(buf, str1, str1_len);
173 avb_memcpy(buf + str1_len, str2, str2_len);
174 buf[combined_len] = '\0';
179 void* avb_malloc(size_t size) {
180 void* ret = avb_malloc_(size);
182 avb_error("Failed to allocate memory.\n");
188 void* avb_calloc(size_t size) {
189 void* ret = avb_malloc(size);
194 avb_memset(ret, '\0', size);
198 char* avb_strdup(const char* str) {
199 size_t len = avb_strlen(str);
200 char* ret = avb_malloc(len + 1);
205 avb_memcpy(ret, str, len);
211 const char* avb_strstr(const char* haystack, const char* needle) {
214 /* Look through |haystack| and check if the first character of
215 * |needle| matches. If so, check the rest of |needle|.
217 for (n = 0; haystack[n] != '\0'; n++) {
218 if (haystack[n] != needle[0]) {
223 if (needle[m] == '\0') {
227 if (haystack[n + m] != needle[m]) {
236 const char* avb_strv_find_str(const char* const* strings,
240 for (n = 0; strings[n] != NULL; n++) {
241 if (avb_strlen(strings[n]) == str_size &&
242 avb_memcmp(strings[n], str, str_size) == 0) {
249 char* avb_replace(const char* str, const char* search, const char* replace) {
252 size_t search_len, replace_len;
253 const char* str_after_last_replace;
255 search_len = avb_strlen(search);
256 replace_len = avb_strlen(replace);
258 str_after_last_replace = str;
259 while (*str != '\0') {
264 s = avb_strstr(str, search);
269 num_before = s - str;
272 num_new = num_before + replace_len + 1;
273 ret = avb_malloc(num_new);
277 avb_memcpy(ret, str, num_before);
278 avb_memcpy(ret + num_before, replace, replace_len);
279 ret[num_new - 1] = '\0';
280 ret_len = num_new - 1;
283 num_new = ret_len + num_before + replace_len + 1;
284 new_str = avb_malloc(num_new);
285 if (new_str == NULL) {
288 avb_memcpy(new_str, ret, ret_len);
289 avb_memcpy(new_str + ret_len, str, num_before);
290 avb_memcpy(new_str + ret_len + num_before, replace, replace_len);
291 new_str[num_new - 1] = '\0';
294 ret_len = num_new - 1;
297 str = s + search_len;
298 str_after_last_replace = str;
302 ret = avb_strdup(str_after_last_replace);
307 size_t num_remaining = avb_strlen(str_after_last_replace);
308 size_t num_new = ret_len + num_remaining + 1;
309 char* new_str = avb_malloc(num_new);
310 if (new_str == NULL) {
313 avb_memcpy(new_str, ret, ret_len);
314 avb_memcpy(new_str + ret_len, str_after_last_replace, num_remaining);
315 new_str[num_new - 1] = '\0';
318 ret_len = num_new - 1;
325 /* We only support a limited amount of strings in avb_strdupv(). */
326 #define AVB_STRDUPV_MAX_NUM_STRINGS 32
328 char* avb_strdupv(const char* str, ...) {
330 const char* strings[AVB_STRDUPV_MAX_NUM_STRINGS];
331 size_t lengths[AVB_STRDUPV_MAX_NUM_STRINGS];
332 size_t num_strings, n;
333 uint64_t total_length;
334 char *ret = NULL, *dest;
340 size_t str_len = avb_strlen(str);
341 strings[num_strings] = str;
342 lengths[num_strings] = str_len;
343 if (!avb_safe_add_to(&total_length, str_len)) {
344 avb_fatal("Overflow while determining total length.\n");
348 if (num_strings == AVB_STRDUPV_MAX_NUM_STRINGS) {
349 avb_fatal("Too many strings passed.\n");
352 str = va_arg(ap, const char*);
353 } while (str != NULL);
356 ret = avb_malloc(total_length + 1);
362 for (n = 0; n < num_strings; n++) {
363 avb_memcpy(dest, strings[n], lengths[n]);
367 avb_assert(dest == ret + total_length);
373 const char* avb_basename(const char* str) {
377 len = avb_strlen(str);
379 for (n = len - 2; n >= 0; n--) {
388 void avb_uppercase(char* str) {
390 for (i = 0; str[i] != '\0'; ++i) {
391 if (str[i] <= 0x7A && str[i] >= 0x61) {
397 char* avb_bin2hex(const uint8_t* data, size_t data_len) {
398 const char hex_digits[17] = "0123456789abcdef";
402 hex_data = avb_malloc(data_len * 2 + 1);
403 if (hex_data == NULL) {
407 for (n = 0; n < data_len; n++) {
408 hex_data[n * 2] = hex_digits[data[n] >> 4];
409 hex_data[n * 2 + 1] = hex_digits[data[n] & 0x0f];
411 hex_data[n * 2] = '\0';