blob: 92fe61f2f2bb1fd0945d197b83993bb0f7fcab20 [file] [log] [blame]
diff --git a/libavb/avb_crc32.c b/libavb/avb_crc32.c
index 9abed54..491fb36 100644
--- a/libavb/avb_crc32.c
+++ b/libavb/avb_crc32.c
@@ -42,11 +42,12 @@
* CRC32 code derived from work by Gary S. Brown.
*/
-#include "avb_sysdeps.h"
+//#include "avb_sysdeps.h"
+#include "avb_util.h"
/* Code taken from FreeBSD 8 */
-static uint32_t crc32_tab[] = {
+static uint32_t iavb_crc32_tab[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
@@ -98,16 +99,16 @@ static uint32_t crc32_tab[] = {
* in sys/libkern.h, where it can be inlined.
*/
-static uint32_t crc32(uint32_t crc_in, const uint8_t* buf, int size) {
+static uint32_t iavb_crc32(uint32_t crc_in, const uint8_t* buf, int size) {
const uint8_t* p = buf;
uint32_t crc;
crc = crc_in ^ ~0U;
while (size--)
- crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
+ crc = iavb_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
return crc ^ ~0U;
}
uint32_t avb_crc32(const uint8_t* buf, size_t size) {
- return crc32(0, buf, size);
+ return iavb_crc32(0, buf, size);
}
diff --git a/libavb/avb_util.c b/libavb/avb_util.c
index 43662b4..4105d38 100644
--- a/libavb/avb_util.c
+++ b/libavb/avb_util.c
@@ -24,31 +24,7 @@
#include "avb_util.h"
-#include <stdarg.h>
-
-uint32_t avb_be32toh(uint32_t in) {
- uint8_t* d = (uint8_t*)&in;
- uint32_t ret;
- ret = ((uint32_t)d[0]) << 24;
- ret |= ((uint32_t)d[1]) << 16;
- ret |= ((uint32_t)d[2]) << 8;
- ret |= ((uint32_t)d[3]);
- return ret;
-}
-
-uint64_t avb_be64toh(uint64_t in) {
- uint8_t* d = (uint8_t*)&in;
- uint64_t ret;
- ret = ((uint64_t)d[0]) << 56;
- ret |= ((uint64_t)d[1]) << 48;
- ret |= ((uint64_t)d[2]) << 40;
- ret |= ((uint64_t)d[3]) << 32;
- ret |= ((uint64_t)d[4]) << 24;
- ret |= ((uint64_t)d[5]) << 16;
- ret |= ((uint64_t)d[6]) << 8;
- ret |= ((uint64_t)d[7]);
- return ret;
-}
+//#include <stdarg.h>
/* Converts a 32-bit unsigned integer from host to big-endian byte order. */
uint32_t avb_htobe32(uint32_t in) {
@@ -63,341 +39,3 @@ uint32_t avb_htobe32(uint32_t in) {
return ret.word;
}
-/* Converts a 64-bit unsigned integer from host to big-endian byte order. */
-uint64_t avb_htobe64(uint64_t in) {
- union {
- uint64_t word;
- uint8_t bytes[8];
- } ret;
- ret.bytes[0] = (in >> 56) & 0xff;
- ret.bytes[1] = (in >> 48) & 0xff;
- ret.bytes[2] = (in >> 40) & 0xff;
- ret.bytes[3] = (in >> 32) & 0xff;
- ret.bytes[4] = (in >> 24) & 0xff;
- ret.bytes[5] = (in >> 16) & 0xff;
- ret.bytes[6] = (in >> 8) & 0xff;
- ret.bytes[7] = in & 0xff;
- return ret.word;
-}
-
-int avb_safe_memcmp(const void* s1, const void* s2, size_t n) {
- const unsigned char* us1 = s1;
- const unsigned char* us2 = s2;
- int result = 0;
-
- if (0 == n) {
- return 0;
- }
-
- /*
- * Code snippet without data-dependent branch due to Nate Lawson
- * (nate@root.org) of Root Labs.
- */
- while (n--) {
- result |= *us1++ ^ *us2++;
- }
-
- return result != 0;
-}
-
-bool avb_safe_add_to(uint64_t* value, uint64_t value_to_add) {
- uint64_t original_value;
-
- avb_assert(value != NULL);
-
- original_value = *value;
-
- *value += value_to_add;
- if (*value < original_value) {
- avb_error("Overflow when adding values.\n");
- return false;
- }
-
- return true;
-}
-
-bool avb_safe_add(uint64_t* out_result, uint64_t a, uint64_t b) {
- uint64_t dummy;
- if (out_result == NULL) {
- out_result = &dummy;
- }
- *out_result = a;
- return avb_safe_add_to(out_result, b);
-}
-
-bool avb_validate_utf8(const uint8_t* data, size_t num_bytes) {
- size_t n;
- unsigned int num_cc;
-
- for (n = 0, num_cc = 0; n < num_bytes; n++) {
- uint8_t c = data[n];
-
- if (num_cc > 0) {
- if ((c & (0x80 | 0x40)) == 0x80) {
- /* 10xx xxxx */
- } else {
- goto fail;
- }
- num_cc--;
- } else {
- if (c < 0x80) {
- num_cc = 0;
- } else if ((c & (0x80 | 0x40 | 0x20)) == (0x80 | 0x40)) {
- /* 110x xxxx */
- num_cc = 1;
- } else if ((c & (0x80 | 0x40 | 0x20 | 0x10)) == (0x80 | 0x40 | 0x20)) {
- /* 1110 xxxx */
- num_cc = 2;
- } else if ((c & (0x80 | 0x40 | 0x20 | 0x10 | 0x08)) ==
- (0x80 | 0x40 | 0x20 | 0x10)) {
- /* 1111 0xxx */
- num_cc = 3;
- } else {
- goto fail;
- }
- }
- }
-
- if (num_cc != 0) {
- goto fail;
- }
-
- return true;
-
-fail:
- return false;
-}
-
-bool avb_str_concat(char* buf,
- size_t buf_size,
- const char* str1,
- size_t str1_len,
- const char* str2,
- size_t str2_len) {
- uint64_t combined_len;
-
- if (!avb_safe_add(&combined_len, str1_len, str2_len)) {
- avb_error("Overflow when adding string sizes.\n");
- return false;
- }
-
- if (combined_len > buf_size - 1) {
- avb_error("Insufficient buffer space.\n");
- return false;
- }
-
- avb_memcpy(buf, str1, str1_len);
- avb_memcpy(buf + str1_len, str2, str2_len);
- buf[combined_len] = '\0';
-
- return true;
-}
-
-void* avb_malloc(size_t size) {
- void* ret = avb_malloc_(size);
- if (ret == NULL) {
- avb_error("Failed to allocate memory.\n");
- return NULL;
- }
- return ret;
-}
-
-void* avb_calloc(size_t size) {
- void* ret = avb_malloc(size);
- if (ret == NULL) {
- return NULL;
- }
-
- avb_memset(ret, '\0', size);
- return ret;
-}
-
-char* avb_strdup(const char* str) {
- size_t len = avb_strlen(str);
- char* ret = avb_malloc(len + 1);
- if (ret == NULL) {
- return NULL;
- }
-
- avb_memcpy(ret, str, len);
- ret[len] = '\0';
-
- return ret;
-}
-
-const char* avb_strstr(const char* haystack, const char* needle) {
- size_t n, m;
-
- /* Look through |haystack| and check if the first character of
- * |needle| matches. If so, check the rest of |needle|.
- */
- for (n = 0; haystack[n] != '\0'; n++) {
- if (haystack[n] != needle[0]) {
- continue;
- }
-
- for (m = 1;; m++) {
- if (needle[m] == '\0') {
- return haystack + n;
- }
-
- if (haystack[n + m] != needle[m]) {
- break;
- }
- }
- }
-
- return NULL;
-}
-
-const char* avb_strv_find_str(const char* const* strings,
- const char* str,
- size_t str_size) {
- size_t n;
- for (n = 0; strings[n] != NULL; n++) {
- if (avb_strlen(strings[n]) == str_size &&
- avb_memcmp(strings[n], str, str_size) == 0) {
- return strings[n];
- }
- }
- return NULL;
-}
-
-char* avb_replace(const char* str, const char* search, const char* replace) {
- char* ret = NULL;
- size_t ret_len = 0;
- size_t search_len, replace_len;
- const char* str_after_last_replace;
-
- search_len = avb_strlen(search);
- replace_len = avb_strlen(replace);
-
- str_after_last_replace = str;
- while (*str != '\0') {
- const char* s;
- size_t num_before;
- size_t num_new;
-
- s = avb_strstr(str, search);
- if (s == NULL) {
- break;
- }
-
- num_before = s - str;
-
- if (ret == NULL) {
- num_new = num_before + replace_len + 1;
- ret = avb_malloc(num_new);
- if (ret == NULL) {
- goto out;
- }
- avb_memcpy(ret, str, num_before);
- avb_memcpy(ret + num_before, replace, replace_len);
- ret[num_new - 1] = '\0';
- ret_len = num_new - 1;
- } else {
- char* new_str;
- num_new = ret_len + num_before + replace_len + 1;
- new_str = avb_malloc(num_new);
- if (ret == NULL) {
- goto out;
- }
- avb_memcpy(new_str, ret, ret_len);
- avb_memcpy(new_str + ret_len, str, num_before);
- avb_memcpy(new_str + ret_len + num_before, replace, replace_len);
- new_str[num_new - 1] = '\0';
- avb_free(ret);
- ret = new_str;
- ret_len = num_new - 1;
- }
-
- str = s + search_len;
- str_after_last_replace = str;
- }
-
- if (ret == NULL) {
- ret = avb_strdup(str_after_last_replace);
- if (ret == NULL) {
- goto out;
- }
- } else {
- size_t num_remaining = avb_strlen(str_after_last_replace);
- size_t num_new = ret_len + num_remaining + 1;
- char* new_str = avb_malloc(num_new);
- if (ret == NULL) {
- goto out;
- }
- avb_memcpy(new_str, ret, ret_len);
- avb_memcpy(new_str + ret_len, str_after_last_replace, num_remaining);
- new_str[num_new - 1] = '\0';
- avb_free(ret);
- ret = new_str;
- ret_len = num_new - 1;
- }
-
-out:
- return ret;
-}
-
-/* We only support a limited amount of strings in avb_strdupv(). */
-#define AVB_STRDUPV_MAX_NUM_STRINGS 32
-
-char* avb_strdupv(const char* str, ...) {
- va_list ap;
- const char* strings[AVB_STRDUPV_MAX_NUM_STRINGS];
- size_t lengths[AVB_STRDUPV_MAX_NUM_STRINGS];
- size_t num_strings, n;
- uint64_t total_length;
- char *ret = NULL, *dest;
-
- num_strings = 0;
- total_length = 0;
- va_start(ap, str);
- do {
- size_t str_len = avb_strlen(str);
- strings[num_strings] = str;
- lengths[num_strings] = str_len;
- if (!avb_safe_add_to(&total_length, str_len)) {
- avb_fatal("Overflow while determining total length.\n");
- break;
- }
- num_strings++;
- if (num_strings == AVB_STRDUPV_MAX_NUM_STRINGS) {
- avb_fatal("Too many strings passed.\n");
- break;
- }
- str = va_arg(ap, const char*);
- } while (str != NULL);
- va_end(ap);
-
- ret = avb_malloc(total_length + 1);
- if (ret == NULL) {
- goto out;
- }
-
- dest = ret;
- for (n = 0; n < num_strings; n++) {
- avb_memcpy(dest, strings[n], lengths[n]);
- dest += lengths[n];
- }
- *dest = '\0';
- avb_assert(dest == ret + total_length);
-
-out:
- return ret;
-}
-
-const char* avb_basename(const char* str) {
- int64_t n;
- size_t len;
-
- len = avb_strlen(str);
- if (len >= 2) {
- for (n = len - 2; n >= 0; n--) {
- if (str[n] == '/') {
- return str + n + 1;
- }
- }
- }
- return str;
-}
diff --git a/libavb/avb_util.h b/libavb/avb_util.h
index 07c3258..589e64c 100644
--- a/libavb/avb_util.h
+++ b/libavb/avb_util.h
@@ -22,254 +22,20 @@
* SOFTWARE.
*/
-#if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
-#error "Never include this file directly, include libavb.h instead."
-#endif
-
#ifndef AVB_UTIL_H_
#define AVB_UTIL_H_
-#include "avb_sysdeps.h"
-
+#include <stdint.h>
+#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
-
-#define AVB_STRINGIFY(x) #x
-#define AVB_TO_STRING(x) AVB_STRINGIFY(x)
-
-#ifdef AVB_ENABLE_DEBUG
-/* Aborts the program if |expr| is false.
- *
- * This has no effect unless AVB_ENABLE_DEBUG is defined.
- */
-#define avb_assert(expr) \
- do { \
- if (!(expr)) { \
- avb_fatal("assert fail: " #expr "\n"); \
- } \
- } while (0)
-#else
-#define avb_assert(expr)
-#endif
-
-/* Aborts the program if reached.
- *
- * This has no effect unless AVB_ENABLE_DEBUG is defined.
- */
-#ifdef AVB_ENABLE_DEBUG
-#define avb_assert_not_reached() \
- do { \
- avb_fatal("assert_not_reached()\n"); \
- } while (0)
-#else
-#define avb_assert_not_reached()
-#endif
-
-/* Aborts the program if |addr| is not word-aligned.
- *
- * This has no effect unless AVB_ENABLE_DEBUG is defined.
- */
-#define avb_assert_aligned(addr) \
- avb_assert((((uintptr_t)addr) & (AVB_ALIGNMENT_SIZE - 1)) == 0)
-
-#ifdef AVB_ENABLE_DEBUG
-/* Print functions, used for diagnostics.
- *
- * These have no effect unless AVB_ENABLE_DEBUG is defined.
- */
-#define avb_debug(message) \
- do { \
- avb_printv(avb_basename(__FILE__), \
- ":", \
- AVB_TO_STRING(__LINE__), \
- ": DEBUG: ", \
- message, \
- NULL); \
- } while (0)
-#define avb_debugv(message, ...) \
- do { \
- avb_printv(avb_basename(__FILE__), \
- ":", \
- AVB_TO_STRING(__LINE__), \
- ": DEBUG: ", \
- message, \
- ##__VA_ARGS__); \
- } while (0)
-#else
-#define avb_debug(message)
-#define avb_debugv(message, ...)
-#endif
-
-/* Prints out a message. This is typically used if a runtime-error
- * occurs.
- */
-#define avb_error(message) \
- do { \
- avb_printv(avb_basename(__FILE__), \
- ":", \
- AVB_TO_STRING(__LINE__), \
- ": ERROR: ", \
- message, \
- NULL); \
- } while (0)
-#define avb_errorv(message, ...) \
- do { \
- avb_printv(avb_basename(__FILE__), \
- ":", \
- AVB_TO_STRING(__LINE__), \
- ": ERROR: ", \
- message, \
- ##__VA_ARGS__); \
- } while (0)
-
-/* Prints out a message and calls avb_abort().
- */
-#define avb_fatal(message) \
- do { \
- avb_printv(avb_basename(__FILE__), \
- ":", \
- AVB_TO_STRING(__LINE__), \
- ": FATAL: ", \
- message, \
- NULL); \
- avb_abort(); \
- } while (0)
-#define avb_fatalv(message, ...) \
- do { \
- avb_printv(avb_basename(__FILE__), \
- ":", \
- AVB_TO_STRING(__LINE__), \
- ": FATAL: ", \
- message, \
- ##__VA_ARGS__); \
- avb_abort(); \
- } while (0)
-
-/* Converts a 32-bit unsigned integer from big-endian to host byte order. */
-uint32_t avb_be32toh(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Converts a 64-bit unsigned integer from big-endian to host byte order. */
-uint64_t avb_be64toh(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
-
/* Converts a 32-bit unsigned integer from host to big-endian byte order. */
-uint32_t avb_htobe32(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Converts a 64-bit unsigned integer from host to big-endian byte order. */
-uint64_t avb_htobe64(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Compare |n| bytes starting at |s1| with |s2| and return 0 if they
- * match, 1 if they don't. Returns 0 if |n|==0, since no bytes
- * mismatched.
- *
- * Time taken to perform the comparison is only dependent on |n| and
- * not on the relationship of the match between |s1| and |s2|.
- *
- * Note that unlike avb_memcmp(), this only indicates inequality, not
- * whether |s1| is less than or greater than |s2|.
- */
-int avb_safe_memcmp(const void* s1,
- const void* s2,
- size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Adds |value_to_add| to |value| with overflow protection.
- *
- * Returns false if the addition overflows, true otherwise. In either
- * case, |value| is always modified.
- */
-bool avb_safe_add_to(uint64_t* value,
- uint64_t value_to_add) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Adds |a| and |b| with overflow protection, returning the value in
- * |out_result|.
- *
- * It's permissible to pass NULL for |out_result| if you just want to
- * check that the addition would not overflow.
- *
- * Returns false if the addition overflows, true otherwise.
- */
-bool avb_safe_add(uint64_t* out_result,
- uint64_t a,
- uint64_t b) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Checks if |num_bytes| data at |data| is a valid UTF-8
- * string. Returns true if valid UTF-8, false otherwise.
- */
-bool avb_validate_utf8(const uint8_t* data,
- size_t num_bytes) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Concatenates |str1| (of |str1_len| bytes) and |str2| (of |str2_len|
- * bytes) and puts the result in |buf| which holds |buf_size|
- * bytes. The result is also guaranteed to be NUL terminated. Fail if
- * there is not enough room in |buf| for the resulting string plus
- * terminating NUL byte.
- *
- * Returns true if the operation succeeds, false otherwise.
- */
-bool avb_str_concat(char* buf,
- size_t buf_size,
- const char* str1,
- size_t str1_len,
- const char* str2,
- size_t str2_len);
-
-/* Like avb_malloc_() but prints a error using avb_error() if memory
- * allocation fails.
- */
-void* avb_malloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Like avb_malloc() but sets the memory with zeroes. */
-void* avb_calloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Duplicates a NUL-terminated string. Returns NULL on OOM. */
-char* avb_strdup(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Duplicates a NULL-terminated array of NUL-terminated strings by
- * concatenating them. The returned string will be
- * NUL-terminated. Returns NULL on OOM.
- */
-char* avb_strdupv(const char* str,
- ...) AVB_ATTR_WARN_UNUSED_RESULT AVB_ATTR_SENTINEL;
-
-/* Finds the first occurrence of |needle| in the string |haystack|
- * where both strings are NUL-terminated strings. The terminating NUL
- * bytes are not compared.
- *
- * Returns NULL if not found, otherwise points into |haystack| for the
- * first occurrence of |needle|.
- */
-const char* avb_strstr(const char* haystack,
- const char* needle) AVB_ATTR_WARN_UNUSED_RESULT;
-
-/* Finds the first occurrence of |str| in the NULL-terminated string
- * array |strings|. Each element in |strings| must be
- * NUL-terminated. The string given by |str| need not be
- * NUL-terminated but its size must be given in |str_size|.
- *
- * Returns NULL if not found, otherwise points into |strings| for the
- * first occurrence of |str|.
- */
-const char* avb_strv_find_str(const char* const* strings,
- const char* str,
- size_t str_size);
-
-/* Replaces all occurrences of |search| with |replace| in |str|.
- *
- * Returns a newly allocated string or NULL if out of memory.
- */
-char* avb_replace(const char* str,
- const char* search,
- const char* replace) AVB_ATTR_WARN_UNUSED_RESULT;
+uint32_t avb_htobe32(uint32_t in);
/* Calculates the CRC-32 for data in |buf| of size |buf_size|. */
uint32_t avb_crc32(const uint8_t* buf, size_t buf_size);
-/* Returns the basename of |str|. This is defined as the last path
- * component, assuming the normal POSIX separator '/'. If there are no
- * separators, returns |str|.
- */
-const char* avb_basename(const char* str);
-
#ifdef __cplusplus
}
#endif