Line data Source code
1 : // Copyright (c) 2012, 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 source file provides replacements for libc functions that we need. If
31 : // we call the libc functions directly we risk crashing in the dynamic linker
32 : // as it tries to resolve uncached PLT entries.
33 :
34 : #include "common/linux/linux_libc_support.h"
35 :
36 : #include <stddef.h>
37 :
38 : extern "C" {
39 :
40 0 : size_t my_strlen(const char* s) {
41 0 : size_t len = 0;
42 0 : while (*s++) len++;
43 0 : return len;
44 : }
45 :
46 0 : int my_strcmp(const char* a, const char* b) {
47 : for (;;) {
48 0 : if (*a < *b)
49 0 : return -1;
50 0 : else if (*a > *b)
51 0 : return 1;
52 0 : else if (*a == 0)
53 0 : return 0;
54 0 : a++;
55 0 : b++;
56 : }
57 : }
58 :
59 0 : int my_strncmp(const char* a, const char* b, size_t len) {
60 0 : for (size_t i = 0; i < len; ++i) {
61 0 : if (*a < *b)
62 0 : return -1;
63 0 : else if (*a > *b)
64 0 : return 1;
65 0 : else if (*a == 0)
66 0 : return 0;
67 0 : a++;
68 0 : b++;
69 : }
70 :
71 0 : return 0;
72 : }
73 :
74 : // Parse a non-negative integer.
75 : // result: (output) the resulting non-negative integer
76 : // s: a NUL terminated string
77 : // Return true iff successful.
78 0 : bool my_strtoui(int* result, const char* s) {
79 0 : if (*s == 0)
80 0 : return false;
81 0 : int r = 0;
82 0 : for (;; s++) {
83 0 : if (*s == 0)
84 0 : break;
85 0 : const int old_r = r;
86 0 : r *= 10;
87 0 : if (*s < '0' || *s > '9')
88 0 : return false;
89 0 : r += *s - '0';
90 0 : if (r < old_r)
91 0 : return false;
92 0 : }
93 :
94 0 : *result = r;
95 0 : return true;
96 : }
97 :
98 : // Return the length of the given unsigned integer when expressed in base 10.
99 0 : unsigned my_uint_len(uintmax_t i) {
100 0 : if (!i)
101 0 : return 1;
102 :
103 0 : int len = 0;
104 0 : while (i) {
105 0 : len++;
106 0 : i /= 10;
107 : }
108 :
109 0 : return len;
110 : }
111 :
112 : // Convert an unsigned integer to a string
113 : // output: (output) the resulting string is written here. This buffer must be
114 : // large enough to hold the resulting string. Call |my_uint_len| to get the
115 : // required length.
116 : // i: the unsigned integer to serialise.
117 : // i_len: the length of the integer in base 10 (see |my_uint_len|).
118 0 : void my_uitos(char* output, uintmax_t i, unsigned i_len) {
119 0 : for (unsigned index = i_len; index; --index, i /= 10)
120 0 : output[index - 1] = '0' + (i % 10);
121 0 : }
122 :
123 0 : const char* my_strchr(const char* haystack, char needle) {
124 0 : while (*haystack && *haystack != needle)
125 0 : haystack++;
126 0 : if (*haystack == needle)
127 0 : return haystack;
128 0 : return (const char*) 0;
129 : }
130 :
131 0 : const char* my_strrchr(const char* haystack, char needle) {
132 0 : const char* ret = NULL;
133 0 : while (*haystack) {
134 0 : if (*haystack == needle)
135 0 : ret = haystack;
136 0 : haystack++;
137 : }
138 0 : return ret;
139 : }
140 :
141 0 : void* my_memchr(const void* src, int needle, size_t src_len) {
142 0 : const unsigned char* p = (const unsigned char*)src;
143 0 : const unsigned char* p_end = p + src_len;
144 0 : for (; p < p_end; ++p) {
145 0 : if (*p == needle)
146 0 : return (void*)p;
147 : }
148 0 : return NULL;
149 : }
150 :
151 : // Read a hex value
152 : // result: (output) the resulting value
153 : // s: a string
154 : // Returns a pointer to the first invalid charactor.
155 0 : const char* my_read_hex_ptr(uintptr_t* result, const char* s) {
156 0 : uintptr_t r = 0;
157 :
158 0 : for (;; ++s) {
159 0 : if (*s >= '0' && *s <= '9') {
160 0 : r <<= 4;
161 0 : r += *s - '0';
162 0 : } else if (*s >= 'a' && *s <= 'f') {
163 0 : r <<= 4;
164 0 : r += (*s - 'a') + 10;
165 0 : } else if (*s >= 'A' && *s <= 'F') {
166 0 : r <<= 4;
167 0 : r += (*s - 'A') + 10;
168 : } else {
169 : break;
170 : }
171 : }
172 :
173 0 : *result = r;
174 0 : return s;
175 : }
176 :
177 0 : const char* my_read_decimal_ptr(uintptr_t* result, const char* s) {
178 0 : uintptr_t r = 0;
179 :
180 0 : for (;; ++s) {
181 0 : if (*s >= '0' && *s <= '9') {
182 0 : r *= 10;
183 0 : r += *s - '0';
184 : } else {
185 : break;
186 : }
187 : }
188 0 : *result = r;
189 0 : return s;
190 : }
191 :
192 0 : void my_memset(void* ip, char c, size_t len) {
193 0 : char* p = (char *) ip;
194 0 : while (len--)
195 0 : *p++ = c;
196 0 : }
197 :
198 0 : size_t my_strlcpy(char* s1, const char* s2, size_t len) {
199 0 : size_t pos1 = 0;
200 0 : size_t pos2 = 0;
201 :
202 0 : while (s2[pos2] != '\0') {
203 0 : if (pos1 + 1 < len) {
204 0 : s1[pos1] = s2[pos2];
205 0 : pos1++;
206 : }
207 0 : pos2++;
208 : }
209 0 : if (len > 0)
210 0 : s1[pos1] = '\0';
211 :
212 0 : return pos2;
213 : }
214 :
215 0 : size_t my_strlcat(char* s1, const char* s2, size_t len) {
216 0 : size_t pos1 = 0;
217 :
218 0 : while (pos1 < len && s1[pos1] != '\0')
219 0 : pos1++;
220 :
221 0 : if (pos1 == len)
222 0 : return pos1;
223 :
224 0 : return pos1 + my_strlcpy(s1 + pos1, s2, len - pos1);
225 : }
226 :
227 0 : int my_isspace(int ch) {
228 : // Matches the C locale.
229 0 : const char spaces[] = " \t\f\n\r\t\v";
230 0 : for (size_t i = 0; i < sizeof(spaces); i++) {
231 0 : if (ch == spaces[i])
232 0 : return 1;
233 : }
234 0 : return 0;
235 : }
236 :
237 : } // extern "C"
|