blob: e01c94667d2acc5ee01a4e049ed2261f59dac397 [file] [log] [blame]
Alex Deymoa5cff222015-04-08 14:10:30 -07001// 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 Deymodcd423b2017-09-13 20:54:24 +02005#include "bsdiff/extents.h"
Alex Deymoa5cff222015-04-08 14:10:30 -07006
Gilad Arnold99b53742013-04-30 09:24:14 -07007#include <assert.h>
8#include <errno.h>
9#include <limits.h>
10#include <stdint.h>
11#include <stdlib.h>
12
Alex Deymo20891f92015-10-12 17:28:04 -070013#include <algorithm>
14#include <limits>
Gilad Arnold99b53742013-04-30 09:24:14 -070015
Alex Deymo20891f92015-10-12 17:28:04 -070016namespace bsdiff {
Gilad Arnold99b53742013-04-30 09:24:14 -070017
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 Deymo20891f92015-10-12 17:28:04 -070027#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 Arnold99b53742013-04-30 09:24:14 -070030
31/* Get the type of a struct field. */
Alex Deymo20891f92015-10-12 17:28:04 -070032#define FIELD_TYPE(t, f) decltype(((t*)0)->f)
Gilad Arnold99b53742013-04-30 09:24:14 -070033
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 Deymoe1526cf2015-10-12 17:48:28 -070041const 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 Arnold99b53742013-04-30 09:24:14 -070059}
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 Deymoe1526cf2015-10-12 17:48:28 -070068static 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 Deymo20891f92015-10-12 17:28:04 -070079 MAX_ALLOWED(FIELD_TYPE(ex_t, off)), ':', false)) &&
Alex Deymoe1526cf2015-10-12 17:48:28 -070080 (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 Arnold99b53742013-04-30 09:24:14 -070087 }
Alex Deymoe1526cf2015-10-12 17:48:28 -070088 }
89 return i;
Gilad Arnold99b53742013-04-30 09:24:14 -070090}
91
92
Alex Deymo437b7af2015-10-14 20:13:58 -070093bool 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 Arnold99b53742013-04-30 09:24:14 -070097
Alex Deymoe1526cf2015-10-12 17:48:28 -070098 /* Parse string and count extents. */
99 ssize_t ret = extents_read(ex_str, NULL, 0);
100 if (ret < 0)
Alex Deymo437b7af2015-10-14 20:13:58 -0700101 return false; // parsing error.
Gilad Arnold99b53742013-04-30 09:24:14 -0700102
Alex Deymo437b7af2015-10-14 20:13:58 -0700103 // Input is good, commit to extent count.
104 extents->resize(ret);
105 if (ret == 0)
106 return true; // No extents, nothing to do.
Gilad Arnold99b53742013-04-30 09:24:14 -0700107
Alex Deymo437b7af2015-10-14 20:13:58 -0700108 // Populate the extent array.
109 extents_read(ex_str, extents->data(), extents->size());
110 return true;
Gilad Arnold99b53742013-04-30 09:24:14 -0700111}
Alex Deymo20891f92015-10-12 17:28:04 -0700112
113} // namespace bsdiff