Alex Deymo | a5cff22 | 2015-04-08 14:10:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | dcd423b | 2017-09-13 20:54:24 +0200 | [diff] [blame] | 5 | #include "bsdiff/extents.h" |
Alex Deymo | a5cff22 | 2015-04-08 14:10:30 -0700 | [diff] [blame] | 6 | |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | #include <errno.h> |
| 9 | #include <limits.h> |
| 10 | #include <stdint.h> |
| 11 | #include <stdlib.h> |
| 12 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <limits> |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 15 | |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 16 | namespace bsdiff { |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 17 | |
| 18 | /* The maximum accepted value for a given integer type when parsed as a signed |
| 19 | * long long integer. This is defined to be the smaller of the maximum value |
| 20 | * that can be represented by this type and LLONG_MAX. This bound allows us to |
| 21 | * properly check that parsed values do not exceed the capacity of their |
| 22 | * intended store, regardless of how its size relates to that of a signed long |
| 23 | * long integer. Note: this may mean that we are losing the most significant |
| 24 | * bit of an unsigned 64-bit field (e.g. size_t on some platforms), however |
| 25 | * still permitting values up to 2^62, which is more than enough for all |
| 26 | * practical purposes. */ |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 27 | #define MAX_ALLOWED(t) \ |
| 28 | (std::min(static_cast<uint64_t>(std::numeric_limits<t>::max()), \ |
| 29 | static_cast<uint64_t>(std::numeric_limits<long long>::max()))) |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 30 | |
| 31 | /* Get the type of a struct field. */ |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 32 | #define FIELD_TYPE(t, f) decltype(((t*)0)->f) |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 33 | |
| 34 | |
| 35 | /* Reads a long long integer from |s| into |*val_p|. Returns a pointer to the |
| 36 | * character immediately following the specified |delim|, unless (a) parsing |
| 37 | * failed (overflow or no valid digits); (b) the read value is less than |
| 38 | * |min_val| or greater than |max_val|; (c) the delimiter character is not |
| 39 | * |delim|, or the string ends although |may_end| is false. In any of these |
| 40 | * cases, returns NULL. */ |
Alex Deymo | e1526cf | 2015-10-12 17:48:28 -0700 | [diff] [blame] | 41 | const char* read_llong(const char* s, |
| 42 | long long* val_p, |
| 43 | long long min_val, |
| 44 | long long max_val, |
| 45 | char delim, |
| 46 | int may_end) { |
| 47 | assert(val_p); |
| 48 | const char* next_s; |
| 49 | errno = 0; |
| 50 | long long val = strtoll(s, (char**)&next_s, 10); |
| 51 | if (((val == LLONG_MAX || val == LLONG_MIN) && errno == ERANGE) || |
| 52 | next_s == s || val < min_val || val > max_val || |
| 53 | (*next_s ? *next_s != delim : !may_end)) |
| 54 | return NULL; /* bad value or delimiter */ |
| 55 | *val_p = val; |
| 56 | if (*next_s) |
| 57 | next_s++; /* skip delimeter */ |
| 58 | return next_s; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
| 62 | /* Reads a comma-separated list of "offset:length" extents from |ex_str|. If |
| 63 | * |ex_arr| is NULL, then |ex_count| is ignored and it attempts to parse valid |
| 64 | * extents until the end of the string is reached. Otherwise, stores up to |
| 65 | * |ex_count| extents into |ex_arr|, which must be of at least this size. |
| 66 | * Returns the number of correctly parsed extents, or -1 if a malformed extent |
| 67 | * was found. */ |
Alex Deymo | e1526cf | 2015-10-12 17:48:28 -0700 | [diff] [blame] | 68 | static ssize_t extents_read(const char* ex_str, ex_t* ex_arr, size_t ex_count) { |
| 69 | size_t i; |
| 70 | size_t last_i = ex_count - 1; |
| 71 | if (!ex_arr) { |
| 72 | ex_count = SIZE_MAX; |
| 73 | last_i = 0; |
| 74 | } |
| 75 | for (i = 0; *ex_str && i < ex_count; i++) { |
| 76 | long long raw_off = 0, raw_len = 0; |
| 77 | if (!((ex_str = |
| 78 | read_llong(ex_str, &raw_off, -1, |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 79 | MAX_ALLOWED(FIELD_TYPE(ex_t, off)), ':', false)) && |
Alex Deymo | e1526cf | 2015-10-12 17:48:28 -0700 | [diff] [blame] | 80 | (ex_str = read_llong(ex_str, &raw_len, 1, |
| 81 | MAX_ALLOWED(FIELD_TYPE(ex_t, len)), ',', |
| 82 | i >= last_i)))) |
| 83 | return -1; /* parsing error */ |
| 84 | if (ex_arr) { |
| 85 | ex_arr[i].off = raw_off; |
| 86 | ex_arr[i].len = raw_len; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 87 | } |
Alex Deymo | e1526cf | 2015-10-12 17:48:28 -0700 | [diff] [blame] | 88 | } |
| 89 | return i; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 93 | bool ParseExtentStr(const char* ex_str, std::vector<ex_t>* extents) { |
| 94 | // Sanity check: a string must be provided. |
| 95 | if (!ex_str) |
| 96 | return false; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 97 | |
Alex Deymo | e1526cf | 2015-10-12 17:48:28 -0700 | [diff] [blame] | 98 | /* Parse string and count extents. */ |
| 99 | ssize_t ret = extents_read(ex_str, NULL, 0); |
| 100 | if (ret < 0) |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 101 | return false; // parsing error. |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 102 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 103 | // Input is good, commit to extent count. |
| 104 | extents->resize(ret); |
| 105 | if (ret == 0) |
| 106 | return true; // No extents, nothing to do. |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 107 | |
Alex Deymo | 437b7af | 2015-10-14 20:13:58 -0700 | [diff] [blame] | 108 | // Populate the extent array. |
| 109 | extents_read(ex_str, extents->data(), extents->size()); |
| 110 | return true; |
Gilad Arnold | 99b5374 | 2013-04-30 09:24:14 -0700 | [diff] [blame] | 111 | } |
Alex Deymo | 20891f9 | 2015-10-12 17:28:04 -0700 | [diff] [blame] | 112 | |
| 113 | } // namespace bsdiff |