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 
5 #include "bsdiff/extents.h"
6 
7 #include <assert.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 
13 #include <algorithm>
14 #include <limits>
15 
16 namespace bsdiff {
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. */
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())))
30 
31 /* Get the type of a struct field. */
32 #define FIELD_TYPE(t, f) decltype(((t*)0)->f)
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. */
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;
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. */
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,
79                           MAX_ALLOWED(FIELD_TYPE(ex_t, off)), ':', false)) &&
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;
87     }
88   }
89   return i;
90 }
91 
92 
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;
97 
98   /* Parse string and count extents. */
99   ssize_t ret = extents_read(ex_str, NULL, 0);
100   if (ret < 0)
101     return false;  // parsing error.
102 
103   // Input is good, commit to extent count.
104   extents->resize(ret);
105   if (ret == 0)
106     return true;  // No extents, nothing to do.
107 
108   // Populate the extent array.
109   extents_read(ex_str, extents->data(), extents->size());
110   return true;
111 }
112 
113 }  // namespace bsdiff
114