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