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