1 // Copyright 2009 Google LLC 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google LLC nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 // This header provides replacements for libc functions that we need. We if 30 // call the libc functions directly we risk crashing in the dynamic linker as 31 // it tries to resolve uncached PLT entries. 32 33 #ifndef CLIENT_LINUX_LINUX_LIBC_SUPPORT_H_ 34 #define CLIENT_LINUX_LINUX_LIBC_SUPPORT_H_ 35 36 #include <stdint.h> 37 #include <limits.h> 38 #include <sys/types.h> 39 40 extern "C" { 41 42 extern size_t my_strlen(const char* s); 43 44 extern int my_strcmp(const char* a, const char* b); 45 46 extern int my_strncmp(const char* a, const char* b, size_t len); 47 48 // Parse a non-negative integer. 49 // result: (output) the resulting non-negative integer 50 // s: a NUL terminated string 51 // Return true iff successful. 52 extern bool my_strtoui(int* result, const char* s); 53 54 // Return the length of the given unsigned integer when expressed in base 10. 55 extern unsigned my_uint_len(uintmax_t i); 56 57 // Convert an unsigned integer to a string 58 // output: (output) the resulting string is written here. This buffer must be 59 // large enough to hold the resulting string. Call |my_uint_len| to get the 60 // required length. 61 // i: the unsigned integer to serialise. 62 // i_len: the length of the integer in base 10 (see |my_uint_len|). 63 extern void my_uitos(char* output, uintmax_t i, unsigned i_len); 64 65 extern const char* my_strchr(const char* haystack, char needle); 66 67 extern const char* my_strrchr(const char* haystack, char needle); 68 69 // Read a hex value 70 // result: (output) the resulting value 71 // s: a string 72 // Returns a pointer to the first invalid charactor. 73 extern const char* my_read_hex_ptr(uintptr_t* result, const char* s); 74 75 extern const char* my_read_decimal_ptr(uintptr_t* result, const char* s); 76 77 extern void my_memset(void* ip, char c, size_t len); 78 79 extern void* my_memchr(const void* src, int c, size_t len); 80 81 // The following are considered safe to use in a compromised environment. 82 // Besides, this gives the compiler an opportunity to optimize their calls. 83 #define my_memcpy memcpy 84 #define my_memmove memmove 85 #define my_memcmp memcmp 86 87 extern size_t my_strlcpy(char* s1, const char* s2, size_t len); 88 89 extern size_t my_strlcat(char* s1, const char* s2, size_t len); 90 91 extern int my_isspace(int ch); 92 93 } // extern "C" 94 95 #endif // CLIENT_LINUX_LINUX_LIBC_SUPPORT_H_ 96