Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "src/core/support/string.h"
35 :
36 : #include <ctype.h>
37 : #include <stddef.h>
38 : #include <string.h>
39 :
40 : #include <grpc/support/alloc.h>
41 : #include <grpc/support/log.h>
42 : #include <grpc/support/port_platform.h>
43 : #include <grpc/support/useful.h>
44 :
45 167920 : char *gpr_strdup(const char *src) {
46 : char *dst;
47 : size_t len;
48 :
49 167920 : if (!src) {
50 2848 : return NULL;
51 : }
52 :
53 165072 : len = strlen(src) + 1;
54 165072 : dst = gpr_malloc(len);
55 :
56 165074 : memcpy(dst, src, len);
57 :
58 165074 : return dst;
59 : }
60 :
61 : typedef struct {
62 : size_t capacity;
63 : size_t length;
64 : char *data;
65 : } dump_out;
66 :
67 14817 : static dump_out dump_out_create(void) {
68 14817 : dump_out r = {0, 0, NULL};
69 14817 : return r;
70 : }
71 :
72 35587679 : static void dump_out_append(dump_out *out, char c) {
73 35587679 : if (out->length == out->capacity) {
74 61323 : out->capacity = GPR_MAX(8, 2 * out->capacity);
75 61323 : out->data = gpr_realloc(out->data, out->capacity);
76 : }
77 35587679 : out->data[out->length++] = c;
78 35587679 : }
79 :
80 14418 : static void hexdump(dump_out *out, const char *buf, size_t len) {
81 : static const char hex[16] = "0123456789abcdef";
82 :
83 14418 : const gpr_uint8 *const beg = (const gpr_uint8 *)buf;
84 14418 : const gpr_uint8 *const end = beg + len;
85 : const gpr_uint8 *cur;
86 :
87 8897537 : for (cur = beg; cur != end; ++cur) {
88 8883119 : if (cur != beg) dump_out_append(out, ' ');
89 8883119 : dump_out_append(out, hex[*cur >> 4]);
90 8883119 : dump_out_append(out, hex[*cur & 0xf]);
91 : }
92 14418 : }
93 :
94 14813 : static void asciidump(dump_out *out, const char *buf, size_t len) {
95 14813 : const gpr_uint8 *const beg = (const gpr_uint8 *)buf;
96 14813 : const gpr_uint8 *const end = beg + len;
97 : const gpr_uint8 *cur;
98 14813 : int out_was_empty = (out->length == 0);
99 14813 : if (!out_was_empty) {
100 14414 : dump_out_append(out, ' ');
101 14414 : dump_out_append(out, '\'');
102 : }
103 8909494 : for (cur = beg; cur != end; ++cur) {
104 8894681 : dump_out_append(out, (char)(isprint(*cur) ? *(char *)cur : '.'));
105 : }
106 14813 : if (!out_was_empty) {
107 14414 : dump_out_append(out, '\'');
108 : }
109 14813 : }
110 :
111 14817 : char *gpr_dump(const char *buf, size_t len, gpr_uint32 flags) {
112 14817 : dump_out out = dump_out_create();
113 14817 : if (flags & GPR_DUMP_HEX) {
114 14418 : hexdump(&out, buf, len);
115 : }
116 14817 : if (flags & GPR_DUMP_ASCII) {
117 14813 : asciidump(&out, buf, len);
118 : }
119 14817 : dump_out_append(&out, 0);
120 14817 : return out.data;
121 : }
122 :
123 14732 : char *gpr_dump_slice(gpr_slice s, gpr_uint32 flags) {
124 14732 : return gpr_dump((const char *)GPR_SLICE_START_PTR(s), GPR_SLICE_LENGTH(s),
125 : flags);
126 : }
127 :
128 2480 : int gpr_parse_bytes_to_uint32(const char *buf, size_t len, gpr_uint32 *result) {
129 2480 : gpr_uint32 out = 0;
130 : gpr_uint32 new;
131 : size_t i;
132 :
133 2480 : if (len == 0) return 0; /* must have some bytes */
134 :
135 6028 : for (i = 0; i < len; i++) {
136 3555 : if (buf[i] < '0' || buf[i] > '9') return 0; /* bad char */
137 3553 : new = 10 * out + (gpr_uint32)(buf[i] - '0');
138 3553 : if (new < out) return 0; /* overflow */
139 3549 : out = new;
140 : }
141 :
142 2473 : *result = out;
143 2473 : return 1;
144 : }
145 :
146 340321 : void gpr_reverse_bytes(char *str, int len) {
147 : char *p1, *p2;
148 420161 : for (p1 = str, p2 = str + len - 1; p2 > p1; ++p1, --p2) {
149 79840 : char temp = *p1;
150 79840 : *p1 = *p2;
151 79840 : *p2 = temp;
152 : }
153 340321 : }
154 :
155 344554 : int gpr_ltoa(long value, char *string) {
156 344554 : int i = 0;
157 344554 : int neg = value < 0;
158 :
159 344554 : if (value == 0) {
160 4233 : string[0] = '0';
161 4233 : string[1] = 0;
162 4233 : return 1;
163 : }
164 :
165 340321 : if (neg) value = -value;
166 1141272 : while (value) {
167 460630 : string[i++] = (char)('0' + value % 10);
168 460630 : value /= 10;
169 : }
170 340321 : if (neg) string[i++] = '-';
171 340321 : gpr_reverse_bytes(string, i);
172 340321 : string[i] = 0;
173 340321 : return i;
174 : }
175 :
176 13125 : char *gpr_strjoin(const char **strs, size_t nstrs, size_t *final_length) {
177 13125 : return gpr_strjoin_sep(strs, nstrs, "", final_length);
178 : }
179 :
180 16895 : char *gpr_strjoin_sep(const char **strs, size_t nstrs, const char *sep,
181 : size_t *final_length) {
182 16895 : const size_t sep_len = strlen(sep);
183 16895 : size_t out_length = 0;
184 : size_t i;
185 : char *out;
186 94026 : for (i = 0; i < nstrs; i++) {
187 77131 : out_length += strlen(strs[i]);
188 : }
189 16895 : out_length += 1; /* null terminator */
190 16895 : if (nstrs > 0) {
191 16857 : out_length += sep_len * (nstrs - 1); /* separators */
192 : }
193 16895 : out = gpr_malloc(out_length);
194 16896 : out_length = 0;
195 94029 : for (i = 0; i < nstrs; i++) {
196 77133 : const size_t slen = strlen(strs[i]);
197 77133 : if (i != 0) {
198 60275 : memcpy(out + out_length, sep, sep_len);
199 60275 : out_length += sep_len;
200 : }
201 77133 : memcpy(out + out_length, strs[i], slen);
202 77133 : out_length += slen;
203 : }
204 16896 : out[out_length] = 0;
205 16896 : if (final_length != NULL) {
206 6654 : *final_length = out_length;
207 : }
208 16896 : return out;
209 : }
210 :
211 : /** Finds the initial (\a begin) and final (\a end) offsets of the next
212 : * substring from \a str + \a read_offset until the next \a sep or the end of \a
213 : * str.
214 : *
215 : * Returns 1 and updates \a begin and \a end. Returns 0 otherwise. */
216 5385422 : static int slice_find_separator_offset(const gpr_slice str, const char *sep,
217 : const size_t read_offset, size_t *begin,
218 : size_t *end) {
219 : size_t i;
220 5385422 : const gpr_uint8 *str_ptr = GPR_SLICE_START_PTR(str) + read_offset;
221 5385422 : const size_t str_len = GPR_SLICE_LENGTH(str) - read_offset;
222 5385422 : const size_t sep_len = strlen(sep);
223 5385422 : if (str_len < sep_len) {
224 3 : return 0;
225 : }
226 :
227 35098729 : for (i = 0; i <= str_len - sep_len; i++) {
228 32394910 : if (memcmp(str_ptr + i, sep, sep_len) == 0) {
229 2681600 : *begin = read_offset;
230 2681600 : *end = read_offset + i;
231 2681600 : return 1;
232 : }
233 : }
234 2703819 : return 0;
235 : }
236 :
237 2703660 : void gpr_slice_split(gpr_slice str, const char *sep, gpr_slice_buffer *dst) {
238 2703660 : const size_t sep_len = strlen(sep);
239 : size_t begin, end;
240 :
241 2703660 : GPR_ASSERT(sep_len > 0);
242 :
243 2703660 : if (slice_find_separator_offset(str, sep, 0, &begin, &end) != 0) {
244 : do {
245 2703511 : gpr_slice_buffer_add_indexed(dst, gpr_slice_sub(str, begin, end));
246 2703861 : } while (slice_find_separator_offset(str, sep, end + sep_len, &begin,
247 2703557 : &end) != 0);
248 5406305 : gpr_slice_buffer_add_indexed(
249 5406305 : dst, gpr_slice_sub(str, end + sep_len, GPR_SLICE_LENGTH(str)));
250 : } else { /* no sep found, add whole input */
251 354 : gpr_slice_buffer_add_indexed(dst, gpr_slice_ref(str));
252 : }
253 2704150 : }
254 :
255 23605 : void gpr_strvec_init(gpr_strvec *sv) { memset(sv, 0, sizeof(*sv)); }
256 :
257 23604 : void gpr_strvec_destroy(gpr_strvec *sv) {
258 : size_t i;
259 105719 : for (i = 0; i < sv->count; i++) {
260 82114 : gpr_free(sv->strs[i]);
261 : }
262 23605 : gpr_free(sv->strs);
263 23605 : }
264 :
265 82116 : void gpr_strvec_add(gpr_strvec *sv, char *str) {
266 82116 : if (sv->count == sv->capacity) {
267 25508 : sv->capacity = GPR_MAX(sv->capacity + 8, sv->capacity * 2);
268 25508 : sv->strs = gpr_realloc(sv->strs, sizeof(char *) * sv->capacity);
269 : }
270 82114 : sv->strs[sv->count++] = str;
271 82114 : }
272 :
273 13123 : char *gpr_strvec_flatten(gpr_strvec *sv, size_t *final_length) {
274 13123 : return gpr_strjoin((const char **)sv->strs, sv->count, final_length);
275 : }
|