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 /* Compare |n| bytes in two strings. 79 * 80 * Return an integer less than, equal to, or greater than zero if the 81 * first |n| bytes of |s1| is found, respectively, to be less than, 82 * to match, or be greater than the first |n| bytes of |s2|. 83 */ 84 int avb_strncmp(const char* s1, const char* s2, size_t n); 85 86 /* Copy |n| bytes from |src| to |dest|. */ 87 void* avb_memcpy(void* dest, const void* src, size_t n); 88 89 /* Set |n| bytes starting at |s| to |c|. Returns |dest|. */ 90 void* avb_memset(void* dest, const int c, size_t n); 91 92 /* Prints out a message. The string passed must be a NUL-terminated 93 * UTF-8 string. 94 */ 95 void avb_print(const char* message); 96 97 /* Prints out a vector of strings. Each argument must point to a 98 * NUL-terminated UTF-8 string and NULL should be the last argument. 99 */ 100 void avb_printv(const char* message, ...) AVB_ATTR_SENTINEL; 101 102 /* Aborts the program or reboots the device. */ 103 void avb_abort(void) AVB_ATTR_NO_RETURN; 104 105 /* Allocates |size| bytes. Returns NULL if no memory is available, 106 * otherwise a pointer to the allocated memory. 107 * 108 * The memory is not initialized. 109 * 110 * The pointer returned is guaranteed to be word-aligned. 111 * 112 * The memory should be freed with avb_free() when you are done with it. 113 */ 114 void* avb_malloc_(size_t size) AVB_ATTR_WARN_UNUSED_RESULT; 115 116 /* Frees memory previously allocated with avb_malloc(). */ 117 void avb_free(void* ptr); 118 119 /* Returns the lenght of |str|, excluding the terminating NUL-byte. */ 120 size_t avb_strlen(const char* str) AVB_ATTR_WARN_UNUSED_RESULT; 121 122 /* Divide the |dividend| by 10 and saves back to the pointer. Return the 123 * remainder. */ 124 uint32_t avb_div_by_10(uint64_t* dividend); 125 126 #ifdef __cplusplus 127 } 128 #endif 129 130 #endif /* AVB_SYSDEPS_H_ */ 131