Planeshift

linux_libc_support.h

Go to the documentation of this file.
00001 // Copyright (c) 2009, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // This header provides replacements for libc functions that we need. We if
00031 // call the libc functions directly we risk crashing in the dynamic linker as
00032 // it tries to resolve uncached PLT entries.
00033 
00034 #ifndef CLIENT_LINUX_LINUX_LIBC_SUPPORT_H_
00035 #define CLIENT_LINUX_LINUX_LIBC_SUPPORT_H_
00036 
00037 #include <stdint.h>
00038 #include <limits.h>
00039 #include <sys/types.h>
00040 
00041 extern "C" {
00042 
00043 extern size_t my_strlen(const char* s);
00044 
00045 extern int my_strcmp(const char* a, const char* b);
00046 
00047 extern int my_strncmp(const char* a, const char* b, size_t len);
00048 
00049 // Parse a non-negative integer.
00050 //   result: (output) the resulting non-negative integer
00051 //   s: a NUL terminated string
00052 // Return true iff successful.
00053 extern bool my_strtoui(int* result, const char* s);
00054 
00055 // Return the length of the given unsigned integer when expressed in base 10.
00056 extern unsigned my_uint_len(uintmax_t i);
00057 
00058 // Convert an unsigned integer to a string
00059 //   output: (output) the resulting string is written here. This buffer must be
00060 //     large enough to hold the resulting string. Call |my_uint_len| to get the
00061 //     required length.
00062 //   i: the unsigned integer to serialise.
00063 //   i_len: the length of the integer in base 10 (see |my_uint_len|).
00064 extern void my_uitos(char* output, uintmax_t i, unsigned i_len);
00065 
00066 extern const char* my_strchr(const char* haystack, char needle);
00067 
00068 extern const char* my_strrchr(const char* haystack, char needle);
00069 
00070 // Read a hex value
00071 //   result: (output) the resulting value
00072 //   s: a string
00073 // Returns a pointer to the first invalid charactor.
00074 extern const char* my_read_hex_ptr(uintptr_t* result, const char* s);
00075 
00076 extern const char* my_read_decimal_ptr(uintptr_t* result, const char* s);
00077 
00078 extern void my_memset(void* ip, char c, size_t len);
00079 
00080 extern void* my_memchr(const void* src, int c, size_t len);
00081 
00082 // The following are considered safe to use in a compromised environment.
00083 // Besides, this gives the compiler an opportunity to optimize their calls.
00084 #define my_memcpy  memcpy
00085 #define my_memmove memmove
00086 #define my_memcmp  memcmp
00087 
00088 extern size_t my_strlcpy(char* s1, const char* s2, size_t len);
00089 
00090 extern size_t my_strlcat(char* s1, const char* s2, size_t len);
00091 
00092 extern int my_isspace(int ch);
00093 
00094 }  // extern "C"
00095 
00096 #endif  // CLIENT_LINUX_LINUX_LIBC_SUPPORT_H_