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 /* 26 #if !defined(AVB_INSIDE_LIBAVB_H) && !defined(AVB_COMPILATION) 27 #error "Never include this file directly, include libavb.h instead." 28 #endif 29 */ 30 31 #ifndef AVB_SYSDEPS_H_ 32 #define AVB_SYSDEPS_H_ 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 /* Change these includes to match your platform to bring in the 39 * equivalent types available in a normal C runtime. At least things 40 * like uint8_t, uint64_t, and bool (with |false|, |true| keywords) 41 * must be present. 42 */ 43 #include <inttypes.h> 44 #include <stdbool.h> 45 #include <stddef.h> 46 #ifdef CONFIG_USE_STDINT 47 /* Provided by gcc. */ 48 #include <stdint.h> 49 #else 50 /* Type for `void *' pointers. */ 51 typedef unsigned long int uintptr_t; 52 #endif 53 54 /* If you don't have gcc or clang, these attribute macros may need to 55 * be adjusted. 56 */ 57 #define AVB_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 58 #define AVB_ATTR_PACKED __attribute__((packed)) 59 #define AVB_ATTR_NO_RETURN __attribute__((noreturn)) 60 #define AVB_ATTR_SENTINEL __attribute__((__sentinel__)) 61 62 /* Size in bytes used for alignment. */ 63 #ifdef __LP64__ 64 #define AVB_ALIGNMENT_SIZE 8 65 #else 66 #define AVB_ALIGNMENT_SIZE 4 67 #endif 68 69 /* Compare |n| bytes in |src1| and |src2|. 70 * 71 * Returns an integer less than, equal to, or greater than zero if the 72 * first |n| bytes of |src1| is found, respectively, to be less than, 73 * to match, or be greater than the first |n| bytes of |src2|. */ 74 int avb_memcmp(const void* src1, 75 const void* src2, 76 size_t n) AVB_ATTR_WARN_UNUSED_RESULT; 77 78 /* Compare two strings. 79 * 80 * Return an integer less than, equal to, or greater than zero if |s1| 81 * is found, respectively, to be less than, to match, or be greater 82 * than |s2|. 83 */ 84 int avb_strcmp(const char* s1, const char* s2); 85 86 /* Compare |n| bytes in two strings. 87 * 88 * Return an integer less than, equal to, or greater than zero if the 89 * first |n| bytes of |s1| is found, respectively, to be less than, 90 * to match, or be greater than the first |n| bytes of |s2|. 91 */ 92 int avb_strncmp(const char* s1, const char* s2, size_t n); 93 94 /* Copy |n| bytes from |src| to |dest|. */ 95 void* avb_memcpy(void* dest, const void* src, size_t n); 96 97 /* Set |n| bytes starting at |s| to |c|. Returns |dest|. */ 98 void* avb_memset(void* dest, const int c, size_t n); 99 100 /* Prints out a message. The string passed must be a NUL-terminated 101 * UTF-8 string. 102 */ 103 void avb_print(const char* message); 104 105 /* Prints out a vector of strings. Each argument must point to a 106 * NUL-terminated UTF-8 string and NULL should be the last argument. 107 */ 108 void avb_printv(const char* message, ...) AVB_ATTR_SENTINEL; 109 110 /* Aborts the program or reboots the device. */ 111 void avb_abort(void); 112 113 /* Allocates |size| bytes. Returns NULL if no memory is available, 114 * otherwise a pointer to the allocated memory. 115 * 116 * The memory is not initialized. 117 * 118 * The pointer returned is guaranteed to be word-aligned. 119 * 120 * The memory should be freed with avb_free() when you are done with it. 121 */ 122 void* avb_malloc_(size_t size) AVB_ATTR_WARN_UNUSED_RESULT; 123 124 /* Frees memory previously allocated with avb_malloc(). */ 125 void avb_free(void* ptr); 126 127 /* Returns the lenght of |str|, excluding the terminating NUL-byte. */ 128 size_t avb_strlen(const char* str) AVB_ATTR_WARN_UNUSED_RESULT; 129 130 /* Divide the |dividend| by 10 and saves back to the pointer. Return the 131 * remainder. */ 132 uint32_t avb_div_by_10(uint64_t* dividend); 133 134 #ifdef __cplusplus 135 } 136 #endif 137 138 #endif /* AVB_SYSDEPS_H_ */ 139