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