blob: 92fe61f2f2bb1fd0945d197b83993bb0f7fcab20 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001diff --git a/libavb/avb_crc32.c b/libavb/avb_crc32.c
2index 9abed54..491fb36 100644
3--- a/libavb/avb_crc32.c
4+++ b/libavb/avb_crc32.c
5@@ -42,11 +42,12 @@
6 * CRC32 code derived from work by Gary S. Brown.
7 */
8
9-#include "avb_sysdeps.h"
10+//#include "avb_sysdeps.h"
11+#include "avb_util.h"
12
13 /* Code taken from FreeBSD 8 */
14
15-static uint32_t crc32_tab[] = {
16+static uint32_t iavb_crc32_tab[] = {
17 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
18 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
19 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
20@@ -98,16 +99,16 @@ static uint32_t crc32_tab[] = {
21 * in sys/libkern.h, where it can be inlined.
22 */
23
24-static uint32_t crc32(uint32_t crc_in, const uint8_t* buf, int size) {
25+static uint32_t iavb_crc32(uint32_t crc_in, const uint8_t* buf, int size) {
26 const uint8_t* p = buf;
27 uint32_t crc;
28
29 crc = crc_in ^ ~0U;
30 while (size--)
31- crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
32+ crc = iavb_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
33 return crc ^ ~0U;
34 }
35
36 uint32_t avb_crc32(const uint8_t* buf, size_t size) {
37- return crc32(0, buf, size);
38+ return iavb_crc32(0, buf, size);
39 }
40diff --git a/libavb/avb_util.c b/libavb/avb_util.c
41index 43662b4..4105d38 100644
42--- a/libavb/avb_util.c
43+++ b/libavb/avb_util.c
44@@ -24,31 +24,7 @@
45
46 #include "avb_util.h"
47
48-#include <stdarg.h>
49-
50-uint32_t avb_be32toh(uint32_t in) {
51- uint8_t* d = (uint8_t*)&in;
52- uint32_t ret;
53- ret = ((uint32_t)d[0]) << 24;
54- ret |= ((uint32_t)d[1]) << 16;
55- ret |= ((uint32_t)d[2]) << 8;
56- ret |= ((uint32_t)d[3]);
57- return ret;
58-}
59-
60-uint64_t avb_be64toh(uint64_t in) {
61- uint8_t* d = (uint8_t*)&in;
62- uint64_t ret;
63- ret = ((uint64_t)d[0]) << 56;
64- ret |= ((uint64_t)d[1]) << 48;
65- ret |= ((uint64_t)d[2]) << 40;
66- ret |= ((uint64_t)d[3]) << 32;
67- ret |= ((uint64_t)d[4]) << 24;
68- ret |= ((uint64_t)d[5]) << 16;
69- ret |= ((uint64_t)d[6]) << 8;
70- ret |= ((uint64_t)d[7]);
71- return ret;
72-}
73+//#include <stdarg.h>
74
75 /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
76 uint32_t avb_htobe32(uint32_t in) {
77@@ -63,341 +39,3 @@ uint32_t avb_htobe32(uint32_t in) {
78 return ret.word;
79 }
80
81-/* Converts a 64-bit unsigned integer from host to big-endian byte order. */
82-uint64_t avb_htobe64(uint64_t in) {
83- union {
84- uint64_t word;
85- uint8_t bytes[8];
86- } ret;
87- ret.bytes[0] = (in >> 56) & 0xff;
88- ret.bytes[1] = (in >> 48) & 0xff;
89- ret.bytes[2] = (in >> 40) & 0xff;
90- ret.bytes[3] = (in >> 32) & 0xff;
91- ret.bytes[4] = (in >> 24) & 0xff;
92- ret.bytes[5] = (in >> 16) & 0xff;
93- ret.bytes[6] = (in >> 8) & 0xff;
94- ret.bytes[7] = in & 0xff;
95- return ret.word;
96-}
97-
98-int avb_safe_memcmp(const void* s1, const void* s2, size_t n) {
99- const unsigned char* us1 = s1;
100- const unsigned char* us2 = s2;
101- int result = 0;
102-
103- if (0 == n) {
104- return 0;
105- }
106-
107- /*
108- * Code snippet without data-dependent branch due to Nate Lawson
109- * (nate@root.org) of Root Labs.
110- */
111- while (n--) {
112- result |= *us1++ ^ *us2++;
113- }
114-
115- return result != 0;
116-}
117-
118-bool avb_safe_add_to(uint64_t* value, uint64_t value_to_add) {
119- uint64_t original_value;
120-
121- avb_assert(value != NULL);
122-
123- original_value = *value;
124-
125- *value += value_to_add;
126- if (*value < original_value) {
127- avb_error("Overflow when adding values.\n");
128- return false;
129- }
130-
131- return true;
132-}
133-
134-bool avb_safe_add(uint64_t* out_result, uint64_t a, uint64_t b) {
135- uint64_t dummy;
136- if (out_result == NULL) {
137- out_result = &dummy;
138- }
139- *out_result = a;
140- return avb_safe_add_to(out_result, b);
141-}
142-
143-bool avb_validate_utf8(const uint8_t* data, size_t num_bytes) {
144- size_t n;
145- unsigned int num_cc;
146-
147- for (n = 0, num_cc = 0; n < num_bytes; n++) {
148- uint8_t c = data[n];
149-
150- if (num_cc > 0) {
151- if ((c & (0x80 | 0x40)) == 0x80) {
152- /* 10xx xxxx */
153- } else {
154- goto fail;
155- }
156- num_cc--;
157- } else {
158- if (c < 0x80) {
159- num_cc = 0;
160- } else if ((c & (0x80 | 0x40 | 0x20)) == (0x80 | 0x40)) {
161- /* 110x xxxx */
162- num_cc = 1;
163- } else if ((c & (0x80 | 0x40 | 0x20 | 0x10)) == (0x80 | 0x40 | 0x20)) {
164- /* 1110 xxxx */
165- num_cc = 2;
166- } else if ((c & (0x80 | 0x40 | 0x20 | 0x10 | 0x08)) ==
167- (0x80 | 0x40 | 0x20 | 0x10)) {
168- /* 1111 0xxx */
169- num_cc = 3;
170- } else {
171- goto fail;
172- }
173- }
174- }
175-
176- if (num_cc != 0) {
177- goto fail;
178- }
179-
180- return true;
181-
182-fail:
183- return false;
184-}
185-
186-bool avb_str_concat(char* buf,
187- size_t buf_size,
188- const char* str1,
189- size_t str1_len,
190- const char* str2,
191- size_t str2_len) {
192- uint64_t combined_len;
193-
194- if (!avb_safe_add(&combined_len, str1_len, str2_len)) {
195- avb_error("Overflow when adding string sizes.\n");
196- return false;
197- }
198-
199- if (combined_len > buf_size - 1) {
200- avb_error("Insufficient buffer space.\n");
201- return false;
202- }
203-
204- avb_memcpy(buf, str1, str1_len);
205- avb_memcpy(buf + str1_len, str2, str2_len);
206- buf[combined_len] = '\0';
207-
208- return true;
209-}
210-
211-void* avb_malloc(size_t size) {
212- void* ret = avb_malloc_(size);
213- if (ret == NULL) {
214- avb_error("Failed to allocate memory.\n");
215- return NULL;
216- }
217- return ret;
218-}
219-
220-void* avb_calloc(size_t size) {
221- void* ret = avb_malloc(size);
222- if (ret == NULL) {
223- return NULL;
224- }
225-
226- avb_memset(ret, '\0', size);
227- return ret;
228-}
229-
230-char* avb_strdup(const char* str) {
231- size_t len = avb_strlen(str);
232- char* ret = avb_malloc(len + 1);
233- if (ret == NULL) {
234- return NULL;
235- }
236-
237- avb_memcpy(ret, str, len);
238- ret[len] = '\0';
239-
240- return ret;
241-}
242-
243-const char* avb_strstr(const char* haystack, const char* needle) {
244- size_t n, m;
245-
246- /* Look through |haystack| and check if the first character of
247- * |needle| matches. If so, check the rest of |needle|.
248- */
249- for (n = 0; haystack[n] != '\0'; n++) {
250- if (haystack[n] != needle[0]) {
251- continue;
252- }
253-
254- for (m = 1;; m++) {
255- if (needle[m] == '\0') {
256- return haystack + n;
257- }
258-
259- if (haystack[n + m] != needle[m]) {
260- break;
261- }
262- }
263- }
264-
265- return NULL;
266-}
267-
268-const char* avb_strv_find_str(const char* const* strings,
269- const char* str,
270- size_t str_size) {
271- size_t n;
272- for (n = 0; strings[n] != NULL; n++) {
273- if (avb_strlen(strings[n]) == str_size &&
274- avb_memcmp(strings[n], str, str_size) == 0) {
275- return strings[n];
276- }
277- }
278- return NULL;
279-}
280-
281-char* avb_replace(const char* str, const char* search, const char* replace) {
282- char* ret = NULL;
283- size_t ret_len = 0;
284- size_t search_len, replace_len;
285- const char* str_after_last_replace;
286-
287- search_len = avb_strlen(search);
288- replace_len = avb_strlen(replace);
289-
290- str_after_last_replace = str;
291- while (*str != '\0') {
292- const char* s;
293- size_t num_before;
294- size_t num_new;
295-
296- s = avb_strstr(str, search);
297- if (s == NULL) {
298- break;
299- }
300-
301- num_before = s - str;
302-
303- if (ret == NULL) {
304- num_new = num_before + replace_len + 1;
305- ret = avb_malloc(num_new);
306- if (ret == NULL) {
307- goto out;
308- }
309- avb_memcpy(ret, str, num_before);
310- avb_memcpy(ret + num_before, replace, replace_len);
311- ret[num_new - 1] = '\0';
312- ret_len = num_new - 1;
313- } else {
314- char* new_str;
315- num_new = ret_len + num_before + replace_len + 1;
316- new_str = avb_malloc(num_new);
317- if (ret == NULL) {
318- goto out;
319- }
320- avb_memcpy(new_str, ret, ret_len);
321- avb_memcpy(new_str + ret_len, str, num_before);
322- avb_memcpy(new_str + ret_len + num_before, replace, replace_len);
323- new_str[num_new - 1] = '\0';
324- avb_free(ret);
325- ret = new_str;
326- ret_len = num_new - 1;
327- }
328-
329- str = s + search_len;
330- str_after_last_replace = str;
331- }
332-
333- if (ret == NULL) {
334- ret = avb_strdup(str_after_last_replace);
335- if (ret == NULL) {
336- goto out;
337- }
338- } else {
339- size_t num_remaining = avb_strlen(str_after_last_replace);
340- size_t num_new = ret_len + num_remaining + 1;
341- char* new_str = avb_malloc(num_new);
342- if (ret == NULL) {
343- goto out;
344- }
345- avb_memcpy(new_str, ret, ret_len);
346- avb_memcpy(new_str + ret_len, str_after_last_replace, num_remaining);
347- new_str[num_new - 1] = '\0';
348- avb_free(ret);
349- ret = new_str;
350- ret_len = num_new - 1;
351- }
352-
353-out:
354- return ret;
355-}
356-
357-/* We only support a limited amount of strings in avb_strdupv(). */
358-#define AVB_STRDUPV_MAX_NUM_STRINGS 32
359-
360-char* avb_strdupv(const char* str, ...) {
361- va_list ap;
362- const char* strings[AVB_STRDUPV_MAX_NUM_STRINGS];
363- size_t lengths[AVB_STRDUPV_MAX_NUM_STRINGS];
364- size_t num_strings, n;
365- uint64_t total_length;
366- char *ret = NULL, *dest;
367-
368- num_strings = 0;
369- total_length = 0;
370- va_start(ap, str);
371- do {
372- size_t str_len = avb_strlen(str);
373- strings[num_strings] = str;
374- lengths[num_strings] = str_len;
375- if (!avb_safe_add_to(&total_length, str_len)) {
376- avb_fatal("Overflow while determining total length.\n");
377- break;
378- }
379- num_strings++;
380- if (num_strings == AVB_STRDUPV_MAX_NUM_STRINGS) {
381- avb_fatal("Too many strings passed.\n");
382- break;
383- }
384- str = va_arg(ap, const char*);
385- } while (str != NULL);
386- va_end(ap);
387-
388- ret = avb_malloc(total_length + 1);
389- if (ret == NULL) {
390- goto out;
391- }
392-
393- dest = ret;
394- for (n = 0; n < num_strings; n++) {
395- avb_memcpy(dest, strings[n], lengths[n]);
396- dest += lengths[n];
397- }
398- *dest = '\0';
399- avb_assert(dest == ret + total_length);
400-
401-out:
402- return ret;
403-}
404-
405-const char* avb_basename(const char* str) {
406- int64_t n;
407- size_t len;
408-
409- len = avb_strlen(str);
410- if (len >= 2) {
411- for (n = len - 2; n >= 0; n--) {
412- if (str[n] == '/') {
413- return str + n + 1;
414- }
415- }
416- }
417- return str;
418-}
419diff --git a/libavb/avb_util.h b/libavb/avb_util.h
420index 07c3258..589e64c 100644
421--- a/libavb/avb_util.h
422+++ b/libavb/avb_util.h
423@@ -22,254 +22,20 @@
424 * SOFTWARE.
425 */
426
427-#if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION)
428-#error "Never include this file directly, include libavb.h instead."
429-#endif
430-
431 #ifndef AVB_UTIL_H_
432 #define AVB_UTIL_H_
433
434-#include "avb_sysdeps.h"
435-
436+#include <stdint.h>
437+#include <sys/types.h>
438 #ifdef __cplusplus
439 extern "C" {
440 #endif
441-
442-#define AVB_STRINGIFY(x) #x
443-#define AVB_TO_STRING(x) AVB_STRINGIFY(x)
444-
445-#ifdef AVB_ENABLE_DEBUG
446-/* Aborts the program if |expr| is false.
447- *
448- * This has no effect unless AVB_ENABLE_DEBUG is defined.
449- */
450-#define avb_assert(expr) \
451- do { \
452- if (!(expr)) { \
453- avb_fatal("assert fail: " #expr "\n"); \
454- } \
455- } while (0)
456-#else
457-#define avb_assert(expr)
458-#endif
459-
460-/* Aborts the program if reached.
461- *
462- * This has no effect unless AVB_ENABLE_DEBUG is defined.
463- */
464-#ifdef AVB_ENABLE_DEBUG
465-#define avb_assert_not_reached() \
466- do { \
467- avb_fatal("assert_not_reached()\n"); \
468- } while (0)
469-#else
470-#define avb_assert_not_reached()
471-#endif
472-
473-/* Aborts the program if |addr| is not word-aligned.
474- *
475- * This has no effect unless AVB_ENABLE_DEBUG is defined.
476- */
477-#define avb_assert_aligned(addr) \
478- avb_assert((((uintptr_t)addr) & (AVB_ALIGNMENT_SIZE - 1)) == 0)
479-
480-#ifdef AVB_ENABLE_DEBUG
481-/* Print functions, used for diagnostics.
482- *
483- * These have no effect unless AVB_ENABLE_DEBUG is defined.
484- */
485-#define avb_debug(message) \
486- do { \
487- avb_printv(avb_basename(__FILE__), \
488- ":", \
489- AVB_TO_STRING(__LINE__), \
490- ": DEBUG: ", \
491- message, \
492- NULL); \
493- } while (0)
494-#define avb_debugv(message, ...) \
495- do { \
496- avb_printv(avb_basename(__FILE__), \
497- ":", \
498- AVB_TO_STRING(__LINE__), \
499- ": DEBUG: ", \
500- message, \
501- ##__VA_ARGS__); \
502- } while (0)
503-#else
504-#define avb_debug(message)
505-#define avb_debugv(message, ...)
506-#endif
507-
508-/* Prints out a message. This is typically used if a runtime-error
509- * occurs.
510- */
511-#define avb_error(message) \
512- do { \
513- avb_printv(avb_basename(__FILE__), \
514- ":", \
515- AVB_TO_STRING(__LINE__), \
516- ": ERROR: ", \
517- message, \
518- NULL); \
519- } while (0)
520-#define avb_errorv(message, ...) \
521- do { \
522- avb_printv(avb_basename(__FILE__), \
523- ":", \
524- AVB_TO_STRING(__LINE__), \
525- ": ERROR: ", \
526- message, \
527- ##__VA_ARGS__); \
528- } while (0)
529-
530-/* Prints out a message and calls avb_abort().
531- */
532-#define avb_fatal(message) \
533- do { \
534- avb_printv(avb_basename(__FILE__), \
535- ":", \
536- AVB_TO_STRING(__LINE__), \
537- ": FATAL: ", \
538- message, \
539- NULL); \
540- avb_abort(); \
541- } while (0)
542-#define avb_fatalv(message, ...) \
543- do { \
544- avb_printv(avb_basename(__FILE__), \
545- ":", \
546- AVB_TO_STRING(__LINE__), \
547- ": FATAL: ", \
548- message, \
549- ##__VA_ARGS__); \
550- avb_abort(); \
551- } while (0)
552-
553-/* Converts a 32-bit unsigned integer from big-endian to host byte order. */
554-uint32_t avb_be32toh(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
555-
556-/* Converts a 64-bit unsigned integer from big-endian to host byte order. */
557-uint64_t avb_be64toh(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
558-
559 /* Converts a 32-bit unsigned integer from host to big-endian byte order. */
560-uint32_t avb_htobe32(uint32_t in) AVB_ATTR_WARN_UNUSED_RESULT;
561-
562-/* Converts a 64-bit unsigned integer from host to big-endian byte order. */
563-uint64_t avb_htobe64(uint64_t in) AVB_ATTR_WARN_UNUSED_RESULT;
564-
565-/* Compare |n| bytes starting at |s1| with |s2| and return 0 if they
566- * match, 1 if they don't. Returns 0 if |n|==0, since no bytes
567- * mismatched.
568- *
569- * Time taken to perform the comparison is only dependent on |n| and
570- * not on the relationship of the match between |s1| and |s2|.
571- *
572- * Note that unlike avb_memcmp(), this only indicates inequality, not
573- * whether |s1| is less than or greater than |s2|.
574- */
575-int avb_safe_memcmp(const void* s1,
576- const void* s2,
577- size_t n) AVB_ATTR_WARN_UNUSED_RESULT;
578-
579-/* Adds |value_to_add| to |value| with overflow protection.
580- *
581- * Returns false if the addition overflows, true otherwise. In either
582- * case, |value| is always modified.
583- */
584-bool avb_safe_add_to(uint64_t* value,
585- uint64_t value_to_add) AVB_ATTR_WARN_UNUSED_RESULT;
586-
587-/* Adds |a| and |b| with overflow protection, returning the value in
588- * |out_result|.
589- *
590- * It's permissible to pass NULL for |out_result| if you just want to
591- * check that the addition would not overflow.
592- *
593- * Returns false if the addition overflows, true otherwise.
594- */
595-bool avb_safe_add(uint64_t* out_result,
596- uint64_t a,
597- uint64_t b) AVB_ATTR_WARN_UNUSED_RESULT;
598-
599-/* Checks if |num_bytes| data at |data| is a valid UTF-8
600- * string. Returns true if valid UTF-8, false otherwise.
601- */
602-bool avb_validate_utf8(const uint8_t* data,
603- size_t num_bytes) AVB_ATTR_WARN_UNUSED_RESULT;
604-
605-/* Concatenates |str1| (of |str1_len| bytes) and |str2| (of |str2_len|
606- * bytes) and puts the result in |buf| which holds |buf_size|
607- * bytes. The result is also guaranteed to be NUL terminated. Fail if
608- * there is not enough room in |buf| for the resulting string plus
609- * terminating NUL byte.
610- *
611- * Returns true if the operation succeeds, false otherwise.
612- */
613-bool avb_str_concat(char* buf,
614- size_t buf_size,
615- const char* str1,
616- size_t str1_len,
617- const char* str2,
618- size_t str2_len);
619-
620-/* Like avb_malloc_() but prints a error using avb_error() if memory
621- * allocation fails.
622- */
623-void* avb_malloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
624-
625-/* Like avb_malloc() but sets the memory with zeroes. */
626-void* avb_calloc(size_t size) AVB_ATTR_WARN_UNUSED_RESULT;
627-
628-/* Duplicates a NUL-terminated string. Returns NULL on OOM. */
629-char* avb_strdup(const char* str) AVB_ATTR_WARN_UNUSED_RESULT;
630-
631-/* Duplicates a NULL-terminated array of NUL-terminated strings by
632- * concatenating them. The returned string will be
633- * NUL-terminated. Returns NULL on OOM.
634- */
635-char* avb_strdupv(const char* str,
636- ...) AVB_ATTR_WARN_UNUSED_RESULT AVB_ATTR_SENTINEL;
637-
638-/* Finds the first occurrence of |needle| in the string |haystack|
639- * where both strings are NUL-terminated strings. The terminating NUL
640- * bytes are not compared.
641- *
642- * Returns NULL if not found, otherwise points into |haystack| for the
643- * first occurrence of |needle|.
644- */
645-const char* avb_strstr(const char* haystack,
646- const char* needle) AVB_ATTR_WARN_UNUSED_RESULT;
647-
648-/* Finds the first occurrence of |str| in the NULL-terminated string
649- * array |strings|. Each element in |strings| must be
650- * NUL-terminated. The string given by |str| need not be
651- * NUL-terminated but its size must be given in |str_size|.
652- *
653- * Returns NULL if not found, otherwise points into |strings| for the
654- * first occurrence of |str|.
655- */
656-const char* avb_strv_find_str(const char* const* strings,
657- const char* str,
658- size_t str_size);
659-
660-/* Replaces all occurrences of |search| with |replace| in |str|.
661- *
662- * Returns a newly allocated string or NULL if out of memory.
663- */
664-char* avb_replace(const char* str,
665- const char* search,
666- const char* replace) AVB_ATTR_WARN_UNUSED_RESULT;
667+uint32_t avb_htobe32(uint32_t in);
668
669 /* Calculates the CRC-32 for data in |buf| of size |buf_size|. */
670 uint32_t avb_crc32(const uint8_t* buf, size_t buf_size);
671
672-/* Returns the basename of |str|. This is defined as the last path
673- * component, assuming the normal POSIX separator '/'. If there are no
674- * separators, returns |str|.
675- */
676-const char* avb_basename(const char* str);
677-
678 #ifdef __cplusplus
679 }
680 #endif