1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Permission is hereby granted, free of charge, to any person 5 * obtaining a copy of this software and associated documentation 6 * files (the "Software"), to deal in the Software without 7 * restriction, including without limitation the rights to use, copy, 8 * modify, merge, publish, distribute, sublicense, and/or sell copies 9 * of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 25 #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) 26 #error "Never include this file directly, include libavb.h instead." 27 #endif 28 29 #ifndef AVB_SYSDEPS_H_ 30 #define AVB_SYSDEPS_H_ 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 /* Change these includes to match your platform to bring in the 37 * equivalent types available in a normal C runtime. At least things 38 * like uint8_t, uint64_t, and bool (with |false|, |true| keywords) 39 * must be present. 40 */ 41 #include <inttypes.h> 42 #include <stdbool.h> 43 #include <stddef.h> 44 #include <stdint.h> 45 46 /* If you don't have gcc or clang, these attribute macros may need to 47 * be adjusted. 48 */ 49 #define AVB_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 50 #define AVB_ATTR_PACKED __attribute__((packed)) 51 #define AVB_ATTR_NO_RETURN __attribute__((noreturn)) 52 #define AVB_ATTR_SENTINEL __attribute__((__sentinel__)) 53 54 /* Size in bytes used for alignment. */ 55 #ifdef __LP64__ 56 #define AVB_ALIGNMENT_SIZE 8 57 #else 58 #define AVB_ALIGNMENT_SIZE 4 59 #endif 60 61 /* Compare |n| bytes in |src1| and |src2|. 62 * 63 * Returns an integer less than, equal to, or greater than zero if the 64 * first |n| bytes of |src1| is found, respectively, to be less than, 65 * to match, or be greater than the first |n| bytes of |src2|. */ 66 int avb_memcmp(const void* src1, 67 const void* src2, 68 size_t n) AVB_ATTR_WARN_UNUSED_RESULT; 69 70 /* Compare two strings. 71 * 72 * Return an integer less than, equal to, or greater than zero if |s1| 73 * is found, respectively, to be less than, to match, or be greater 74 * than |s2|. 75 */ 76 int avb_strcmp(const char* s1, const char* s2); 77 78 /* Copy |n| bytes from |src| to |dest|. */ 79 void* avb_memcpy(void* dest, const void* src, size_t n); 80 81 /* Set |n| bytes starting at |s| to |c|. Returns |dest|. */ 82 void* avb_memset(void* dest, const int c, size_t n); 83 84 /* Prints out a message. The string passed must be a NUL-terminated 85 * UTF-8 string. 86 */ 87 void avb_print(const char* message); 88 89 /* Prints out a vector of strings. Each argument must point to a 90 * NUL-terminated UTF-8 string and NULL should be the last argument. 91 */ 92 void avb_printv(const char* message, ...) AVB_ATTR_SENTINEL; 93 94 /* Aborts the program or reboots the device. */ 95 void avb_abort(void) AVB_ATTR_NO_RETURN; 96 97 /* Allocates |size| bytes. Returns NULL if no memory is available, 98 * otherwise a pointer to the allocated memory. 99 * 100 * The memory is not initialized. 101 * 102 * The pointer returned is guaranteed to be word-aligned. 103 * 104 * The memory should be freed with avb_free() when you are done with it. 105 */ 106 void* avb_malloc_(size_t size) AVB_ATTR_WARN_UNUSED_RESULT; 107 108 /* Frees memory previously allocated with avb_malloc(). */ 109 void avb_free(void* ptr); 110 111 /* Returns the lenght of |str|, excluding the terminating NUL-byte. */ 112 size_t avb_strlen(const char* str) AVB_ATTR_WARN_UNUSED_RESULT; 113 114 /* Divide the |dividend| by 10 and saves back to the pointer. Return the 115 * remainder. */ 116 uint32_t avb_div_by_10(uint64_t* dividend); 117 118 #ifdef __cplusplus 119 } 120 #endif 121 122 #endif /* AVB_SYSDEPS_H_ */ 123