/src/openvswitch/lib/dynamic-string.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2016 Nicira, Inc. |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at: |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | #include <config.h> |
18 | | #include "openvswitch/dynamic-string.h" |
19 | | #include <inttypes.h> |
20 | | #include <stdlib.h> |
21 | | #include <string.h> |
22 | | #include <time.h> |
23 | | #include "timeval.h" |
24 | | #include "util.h" |
25 | | |
26 | | /* Initializes 'ds' as an empty string buffer. */ |
27 | | void |
28 | | ds_init(struct ds *ds) |
29 | 6.22M | { |
30 | 6.22M | ds->string = NULL; |
31 | 6.22M | ds->length = 0; |
32 | 6.22M | ds->allocated = 0; |
33 | 6.22M | } |
34 | | |
35 | | /* Sets 'ds''s length to 0, effectively clearing any existing content. Does |
36 | | * not free any memory. */ |
37 | | void |
38 | | ds_clear(struct ds *ds) |
39 | 33.8M | { |
40 | 33.8M | ds->length = 0; |
41 | 33.8M | } |
42 | | |
43 | | /* Reduces 'ds''s length to no more than 'new_length'. (If its length is |
44 | | * already 'new_length' or less, does nothing.) */ |
45 | | void |
46 | | ds_truncate(struct ds *ds, size_t new_length) |
47 | 0 | { |
48 | 0 | if (ds->length > new_length) { |
49 | 0 | ds->length = new_length; |
50 | 0 | ds->string[new_length] = '\0'; |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | | /* Ensures that at least 'min_length + 1' bytes (including space for a null |
55 | | * terminator) are allocated for ds->string, allocating or reallocating memory |
56 | | * as necessary. */ |
57 | | void |
58 | | ds_reserve(struct ds *ds, size_t min_length) |
59 | 62.4M | { |
60 | 62.4M | if (min_length > ds->allocated || !ds->string) { |
61 | 23.6M | ds->allocated += MAX(min_length, ds->allocated); |
62 | 23.6M | ds->allocated = MAX(8, ds->allocated); |
63 | 23.6M | ds->string = xrealloc(ds->string, ds->allocated + 1); |
64 | 23.6M | } |
65 | 62.4M | } |
66 | | |
67 | | /* Appends space for 'n' bytes to the end of 'ds->string', increasing |
68 | | * 'ds->length' by the same amount, and returns the first appended byte. The |
69 | | * caller should fill in all 'n' bytes starting at the return value. */ |
70 | | char * |
71 | | ds_put_uninit(struct ds *ds, size_t n) |
72 | 48.2M | { |
73 | 48.2M | ds_reserve(ds, ds->length + n); |
74 | 48.2M | ds->length += n; |
75 | 48.2M | ds->string[ds->length] = '\0'; |
76 | 48.2M | return &ds->string[ds->length - n]; |
77 | 48.2M | } |
78 | | |
79 | | void |
80 | | ds_put_char__(struct ds *ds, char c) |
81 | 1.22M | { |
82 | 1.22M | *ds_put_uninit(ds, 1) = c; |
83 | 1.22M | } |
84 | | |
85 | | /* Appends unicode code point 'uc' to 'ds' in UTF-8 encoding. */ |
86 | | void |
87 | | ds_put_utf8(struct ds *ds, int uc) |
88 | 60.7k | { |
89 | 60.7k | if (uc <= 0x7f) { |
90 | 10.1k | ds_put_char(ds, uc); |
91 | 50.6k | } else if (uc <= 0x7ff) { |
92 | 24.1k | ds_put_char(ds, 0xc0 | (uc >> 6)); |
93 | 24.1k | ds_put_char(ds, 0x80 | (uc & 0x3f)); |
94 | 26.4k | } else if (uc <= 0xffff) { |
95 | 14.0k | ds_put_char(ds, 0xe0 | (uc >> 12)); |
96 | 14.0k | ds_put_char(ds, 0x80 | ((uc >> 6) & 0x3f)); |
97 | 14.0k | ds_put_char(ds, 0x80 | (uc & 0x3f)); |
98 | 14.0k | } else if (uc <= 0x10ffff) { |
99 | 12.3k | ds_put_char(ds, 0xf0 | (uc >> 18)); |
100 | 12.3k | ds_put_char(ds, 0x80 | ((uc >> 12) & 0x3f)); |
101 | 12.3k | ds_put_char(ds, 0x80 | ((uc >> 6) & 0x3f)); |
102 | 12.3k | ds_put_char(ds, 0x80 | (uc & 0x3f)); |
103 | 12.3k | } else { |
104 | | /* Invalid code point. Insert the Unicode general substitute |
105 | | * REPLACEMENT CHARACTER. */ |
106 | 0 | ds_put_utf8(ds, 0xfffd); |
107 | 0 | } |
108 | 60.7k | } |
109 | | |
110 | | void |
111 | | ds_put_char_multiple(struct ds *ds, char c, size_t n) |
112 | 17.5M | { |
113 | 17.5M | memset(ds_put_uninit(ds, n), c, n); |
114 | 17.5M | } |
115 | | |
116 | | void |
117 | | ds_put_buffer(struct ds *ds, const char *s, size_t n) |
118 | 14.8M | { |
119 | 14.8M | memcpy(ds_put_uninit(ds, n), s, n); |
120 | 14.8M | } |
121 | | |
122 | | void |
123 | | ds_put_cstr(struct ds *ds, const char *s) |
124 | 14.5M | { |
125 | 14.5M | size_t s_len = strlen(s); |
126 | 14.5M | memcpy(ds_put_uninit(ds, s_len), s, s_len); |
127 | 14.5M | } |
128 | | |
129 | | void |
130 | | ds_put_and_free_cstr(struct ds *ds, char *s) |
131 | 14.9k | { |
132 | 14.9k | ds_put_cstr(ds, s); |
133 | 14.9k | free(s); |
134 | 14.9k | } |
135 | | |
136 | | void |
137 | | ds_put_format(struct ds *ds, const char *format, ...) |
138 | 297M | { |
139 | 297M | va_list args; |
140 | | |
141 | 297M | va_start(args, format); |
142 | 297M | ds_put_format_valist(ds, format, args); |
143 | 297M | va_end(args); |
144 | 297M | } |
145 | | |
146 | | void |
147 | | ds_put_format_valist(struct ds *ds, const char *format, va_list args_) |
148 | 297M | { |
149 | 297M | va_list args; |
150 | 297M | size_t available; |
151 | 297M | int needed; |
152 | | |
153 | 297M | va_copy(args, args_); |
154 | 297M | available = ds->string ? ds->allocated - ds->length + 1 : 0; |
155 | 297M | needed = vsnprintf(ds->string |
156 | 297M | ? &ds->string[ds->length] |
157 | 297M | : NULL, |
158 | 297M | available, format, args); |
159 | 297M | va_end(args); |
160 | | |
161 | 297M | if (needed < available) { |
162 | 283M | ds->length += needed; |
163 | 283M | } else { |
164 | 14.1M | ds_reserve(ds, ds->length + needed); |
165 | | |
166 | 14.1M | va_copy(args, args_); |
167 | 14.1M | available = ds->allocated - ds->length + 1; |
168 | 14.1M | needed = vsnprintf(&ds->string[ds->length], |
169 | 14.1M | available, format, args); |
170 | 14.1M | va_end(args); |
171 | | |
172 | 14.1M | ovs_assert(needed < available); |
173 | 14.1M | ds->length += needed; |
174 | 14.1M | } |
175 | 297M | } |
176 | | |
177 | | void |
178 | | ds_put_printable(struct ds *ds, const char *s, size_t n) |
179 | 34.5k | { |
180 | 34.5k | ds_reserve(ds, ds->length + n); |
181 | 612k | while (n-- > 0) { |
182 | 577k | unsigned char c = *s++; |
183 | 577k | if (c < 0x20 || c > 0x7e || c == '\\' || c == '"') { |
184 | 502k | ds_put_format(ds, "\\%03o", (int) c); |
185 | 502k | } else { |
186 | 74.8k | ds_put_char(ds, c); |
187 | 74.8k | } |
188 | 577k | } |
189 | 34.5k | } |
190 | | |
191 | | /* Writes the current time with optional millisecond resolution to 'string' |
192 | | * based on 'template'. |
193 | | * The current time is either localtime or UTC based on 'utc'. */ |
194 | | void |
195 | | ds_put_strftime_msec(struct ds *ds, const char *template, long long int when, |
196 | | bool utc) |
197 | 0 | { |
198 | 0 | struct tm_msec tm; |
199 | 0 | if (utc) { |
200 | 0 | gmtime_msec(when, &tm); |
201 | 0 | } else { |
202 | 0 | localtime_msec(when, &tm); |
203 | 0 | } |
204 | |
|
205 | 0 | ds_reserve(ds, 64); |
206 | 0 | for (;;) { |
207 | 0 | size_t avail = ds->allocated - ds->length + 1; |
208 | 0 | char *dest = &ds->string[ds->length]; |
209 | 0 | size_t used = strftime_msec(dest, avail, template, &tm); |
210 | 0 | if (used) { |
211 | 0 | ds->length += used; |
212 | 0 | return; |
213 | 0 | } |
214 | 0 | ds_reserve(ds, ds->length + (avail < 32 ? 64 : 2 * avail)); |
215 | 0 | } |
216 | 0 | } |
217 | | |
218 | | /* Returns a malloc()'d string for time 'when' based on 'template', in local |
219 | | * time or UTC based on 'utc'. */ |
220 | | char * |
221 | | xastrftime_msec(const char *template, long long int when, bool utc) |
222 | 0 | { |
223 | 0 | struct ds s; |
224 | |
|
225 | 0 | ds_init(&s); |
226 | 0 | ds_put_strftime_msec(&s, template, when, utc); |
227 | 0 | return s.string; |
228 | 0 | } |
229 | | |
230 | | int |
231 | | ds_get_line(struct ds *ds, FILE *file) |
232 | 0 | { |
233 | 0 | ds_clear(ds); |
234 | 0 | for (;;) { |
235 | 0 | int c = getc(file); |
236 | 0 | if (c == EOF) { |
237 | 0 | return ds->length ? 0 : EOF; |
238 | 0 | } else if (c == '\n') { |
239 | 0 | return 0; |
240 | 0 | } else { |
241 | 0 | ds_put_char(ds, c); |
242 | 0 | } |
243 | 0 | } |
244 | 0 | } |
245 | | |
246 | | /* Reads a line from 'file' into 'ds', clearing anything initially in 'ds'. |
247 | | * Deletes comments introduced by "#" and skips lines that contains only white |
248 | | * space (after deleting comments). |
249 | | * |
250 | | * If 'line_numberp' is nonnull, increments '*line_numberp' by the number of |
251 | | * lines read from 'file'. |
252 | | * |
253 | | * Returns 0 if successful, EOF if no non-blank line was found. */ |
254 | | int |
255 | | ds_get_preprocessed_line(struct ds *ds, FILE *file, int *line_numberp) |
256 | 0 | { |
257 | 0 | while (!ds_get_line(ds, file)) { |
258 | 0 | char *line = ds_cstr(ds); |
259 | 0 | char *comment; |
260 | |
|
261 | 0 | if (line_numberp) { |
262 | 0 | ++*line_numberp; |
263 | 0 | } |
264 | | |
265 | | /* Delete comments. */ |
266 | 0 | comment = strchr(line, '#'); |
267 | 0 | if (comment) { |
268 | 0 | *comment = '\0'; |
269 | 0 | } |
270 | | |
271 | | /* Return successfully unless the line is all spaces. */ |
272 | 0 | if (line[strspn(line, " \t\n")] != '\0') { |
273 | 0 | return 0; |
274 | 0 | } |
275 | 0 | } |
276 | 0 | return EOF; |
277 | 0 | } |
278 | | |
279 | | /* Reads a line from 'file' into 'ds' and does some preprocessing on it: |
280 | | * |
281 | | * - If the line begins with #, prints it on stdout and reads the next line. |
282 | | * |
283 | | * - Otherwise, if the line contains an # somewhere else, strips it and |
284 | | * everything following it (as a comment). |
285 | | * |
286 | | * - If (after comment removal) the line contains only white space, prints |
287 | | * a blank line on stdout and reads the next line. |
288 | | * |
289 | | * - Otherwise, returns the line to the caller. |
290 | | * |
291 | | * This is useful in some of the OVS tests, where we want to check that parsing |
292 | | * and then re-formatting some kind of data does not change it, but we also |
293 | | * want to be able to put comments in the input. |
294 | | * |
295 | | * Returns 0 if successful, EOF if no non-blank line was found. */ |
296 | | int |
297 | | ds_get_test_line(struct ds *ds, FILE *file) |
298 | 0 | { |
299 | 0 | for (;;) { |
300 | 0 | char *s, *comment; |
301 | 0 | int retval; |
302 | |
|
303 | 0 | retval = ds_get_line(ds, file); |
304 | 0 | if (retval) { |
305 | 0 | return retval; |
306 | 0 | } |
307 | | |
308 | 0 | s = ds_cstr(ds); |
309 | 0 | if (*s == '#') { |
310 | 0 | puts(s); |
311 | 0 | continue; |
312 | 0 | } |
313 | | |
314 | 0 | comment = strchr(s, '#'); |
315 | 0 | if (comment) { |
316 | 0 | *comment = '\0'; |
317 | 0 | } |
318 | 0 | if (s[strspn(s, " \t\n")] == '\0') { |
319 | 0 | putchar('\n'); |
320 | 0 | continue; |
321 | 0 | } |
322 | | |
323 | 0 | return 0; |
324 | 0 | } |
325 | 0 | } |
326 | | |
327 | | char * |
328 | | ds_cstr(struct ds *ds) |
329 | 21.1M | { |
330 | 21.1M | if (!ds->string) { |
331 | 146 | ds_reserve(ds, 0); |
332 | 146 | } |
333 | 21.1M | ds->string[ds->length] = '\0'; |
334 | 21.1M | return ds->string; |
335 | 21.1M | } |
336 | | |
337 | | const char * |
338 | | ds_cstr_ro(const struct ds *ds) |
339 | 0 | { |
340 | 0 | return ds_cstr(CONST_CAST(struct ds *, ds)); |
341 | 0 | } |
342 | | |
343 | | /* Returns a null-terminated string representing the current contents of 'ds', |
344 | | * which the caller is expected to free with free(), then clears the contents |
345 | | * of 'ds'. */ |
346 | | char * |
347 | | ds_steal_cstr(struct ds *ds) |
348 | 6.17M | { |
349 | 6.17M | char *s = ds_cstr(ds); |
350 | 6.17M | ds_init(ds); |
351 | 6.17M | return s; |
352 | 6.17M | } |
353 | | |
354 | | void |
355 | | ds_destroy(struct ds *ds) |
356 | 14.8k | { |
357 | 14.8k | free(ds->string); |
358 | 14.8k | } |
359 | | |
360 | | /* Swaps the content of 'a' and 'b'. */ |
361 | | void |
362 | | ds_swap(struct ds *a, struct ds *b) |
363 | 0 | { |
364 | 0 | struct ds temp = *a; |
365 | 0 | *a = *b; |
366 | 0 | *b = temp; |
367 | 0 | } |
368 | | |
369 | | void |
370 | | ds_put_hex(struct ds *ds, const void *buf_, size_t size) |
371 | 133k | { |
372 | 133k | const uint8_t *buf = buf_; |
373 | 133k | bool printed = false; |
374 | 133k | int i; |
375 | | |
376 | 5.25M | for (i = 0; i < size; i++) { |
377 | 5.12M | uint8_t val = buf[i]; |
378 | 5.12M | if (val || printed) { |
379 | 230k | if (!printed) { |
380 | 61.6k | ds_put_format(ds, "0x%"PRIx8, val); |
381 | 168k | } else { |
382 | 168k | ds_put_format(ds, "%02"PRIx8, val); |
383 | 168k | } |
384 | 230k | printed = true; |
385 | 230k | } |
386 | 5.12M | } |
387 | 133k | if (!printed) { |
388 | 71.5k | ds_put_char(ds, '0'); |
389 | 71.5k | } |
390 | 133k | } |
391 | | |
392 | | static void |
393 | | ds_put_hex_dump__(struct ds *ds, const void *buf_, size_t size, |
394 | | uintptr_t ofs, bool ascii, bool skip_zero_lines) |
395 | 4.53M | { |
396 | 4.53M | const uint8_t *buf = buf_; |
397 | 4.53M | const size_t per_line = 16; /* Maximum bytes per line. */ |
398 | | |
399 | 16.5M | while (size > 0) { |
400 | 11.9M | size_t start, end, n; |
401 | 11.9M | size_t i; |
402 | | |
403 | | /* Number of bytes on this line. */ |
404 | 11.9M | start = ofs % per_line; |
405 | 11.9M | end = per_line; |
406 | 11.9M | if (end - start > size) |
407 | 2.86M | end = start + size; |
408 | 11.9M | n = end - start; |
409 | | |
410 | 11.9M | if (skip_zero_lines && is_all_zeros(&buf[start], n)) { |
411 | 0 | goto next; |
412 | 0 | } |
413 | | |
414 | | /* Print line. */ |
415 | 11.9M | ds_put_format(ds, "%08"PRIxMAX" ", |
416 | 11.9M | (uintmax_t) ROUND_DOWN(ofs, per_line)); |
417 | 11.9M | for (i = 0; i < start; i++) { |
418 | 0 | ds_put_format(ds, " "); |
419 | 0 | } |
420 | 182M | for (; i < end; i++) { |
421 | 170M | ds_put_format(ds, "%02x%c", |
422 | 170M | buf[i - start], i == per_line / 2 - 1? '-' : ' '); |
423 | 170M | } |
424 | 11.9M | if (ascii) { |
425 | 32.6M | for (; i < per_line; i++) |
426 | 20.6M | ds_put_format(ds, " "); |
427 | 11.9M | ds_put_format(ds, "|"); |
428 | 11.9M | for (i = 0; i < start; i++) |
429 | 0 | ds_put_format(ds, " "); |
430 | 182M | for (; i < end; i++) { |
431 | 170M | int c = buf[i - start]; |
432 | 170M | ds_put_char(ds, c >= 32 && c < 127 ? c : '.'); |
433 | 170M | } |
434 | 32.6M | for (; i < per_line; i++) |
435 | 20.6M | ds_put_format(ds, " "); |
436 | 11.9M | ds_put_format(ds, "|"); |
437 | 11.9M | } else { |
438 | 0 | ds_chomp(ds, ' '); |
439 | 0 | } |
440 | 11.9M | ds_put_format(ds, "\n"); |
441 | 11.9M | next: |
442 | 11.9M | ofs += n; |
443 | 11.9M | buf += n; |
444 | 11.9M | size -= n; |
445 | 11.9M | } |
446 | 4.53M | } |
447 | | |
448 | | /* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per |
449 | | * line. Numeric offsets are also included, starting at 'ofs' for the first |
450 | | * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters |
451 | | * are also rendered alongside. */ |
452 | | void |
453 | | ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size, |
454 | | uintptr_t ofs, bool ascii) |
455 | 4.53M | { |
456 | 4.53M | ds_put_hex_dump__(ds, buf_, size, ofs, ascii, false); |
457 | 4.53M | } |
458 | | |
459 | | /* Same as 'ds_put_hex_dump', but doesn't print lines that only contains |
460 | | * zero bytes. */ |
461 | | void |
462 | | ds_put_sparse_hex_dump(struct ds *ds, const void *buf_, size_t size, |
463 | | uintptr_t ofs, bool ascii) |
464 | 0 | { |
465 | 0 | ds_put_hex_dump__(ds, buf_, size, ofs, ascii, true); |
466 | 0 | } |
467 | | |
468 | | int |
469 | | ds_last(const struct ds *ds) |
470 | 78.1k | { |
471 | 78.1k | return ds->length > 0 ? (unsigned char) ds->string[ds->length - 1] : EOF; |
472 | 78.1k | } |
473 | | |
474 | | bool |
475 | | ds_chomp(struct ds *ds, int c) |
476 | 2.96M | { |
477 | 2.96M | if (ds->length > 0 && ds->string[ds->length - 1] == (char) c) { |
478 | 1.03M | ds->string[--ds->length] = '\0'; |
479 | 1.03M | return true; |
480 | 1.93M | } else { |
481 | 1.93M | return false; |
482 | 1.93M | } |
483 | 2.96M | } |
484 | | |
485 | | void |
486 | | ds_clone(struct ds *dst, struct ds *source) |
487 | 0 | { |
488 | 0 | if (!source->allocated) { |
489 | 0 | ds_init(dst); |
490 | 0 | return; |
491 | 0 | } |
492 | 0 | dst->length = source->length; |
493 | 0 | dst->allocated = dst->length; |
494 | 0 | dst->string = xmalloc(dst->allocated + 1); |
495 | 0 | memcpy(dst->string, source->string, dst->allocated + 1); |
496 | 0 | } |