blob: 5f99e858d9d50e70a23f8e8e496417f6cecea118 [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
5#include "extents.h"
6
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
Gilad Arnold99b53742013-04-30 09:24:14 -070013
14#define TRUE 1
15#define FALSE 0
16
17/* Minimum/maximum values for arbitrary integer types. */
18#define UNSIGNED_INT_MAX(t) (~((t)0))
19#define SIGNED_INT_MAX(t) ((t)((uintmax_t)UNSIGNED_INT_MAX(t) >> 1))
20#define MAX(a,b) ((a) > (b) ? (a) : (b))
21#define INT_TYPE_MAX(t) MAX(UNSIGNED_INT_MAX(t), SIGNED_INT_MAX(t))
22
23/* The maximum accepted value for a given integer type when parsed as a signed
24 * long long integer. This is defined to be the smaller of the maximum value
25 * that can be represented by this type and LLONG_MAX. This bound allows us to
26 * properly check that parsed values do not exceed the capacity of their
27 * intended store, regardless of how its size relates to that of a signed long
28 * long integer. Note: this may mean that we are losing the most significant
29 * bit of an unsigned 64-bit field (e.g. size_t on some platforms), however
30 * still permitting values up to 2^62, which is more than enough for all
31 * practical purposes. */
32#define LLONG_MAX_BOUND(s) \
33 ((uintmax_t)(s) < (uintmax_t)LLONG_MAX ? (long long)(s) : LLONG_MAX)
34#define MAX_ALLOWED(t) LLONG_MAX_BOUND(INT_TYPE_MAX(t))
35
36/* Get the type of a struct field. */
37#define FIELD_TYPE(t, f) typeof(((t *)0)->f)
38
39
40/* Reads a long long integer from |s| into |*val_p|. Returns a pointer to the
41 * character immediately following the specified |delim|, unless (a) parsing
42 * failed (overflow or no valid digits); (b) the read value is less than
43 * |min_val| or greater than |max_val|; (c) the delimiter character is not
44 * |delim|, or the string ends although |may_end| is false. In any of these
45 * cases, returns NULL. */
46const char *
47read_llong(const char *s, long long *val_p, long long min_val,
48 long long max_val, char delim, int may_end)
49{
50 assert(val_p);
51 const char *next_s;
52 errno = 0;
53 long long val = strtoll(s, (char **)&next_s, 10);
54 if (((val == LLONG_MAX || val == LLONG_MIN) && errno == ERANGE) ||
55 next_s == s || val < min_val || val > max_val ||
56 (*next_s ? *next_s != delim : !may_end))
57 return NULL; /* bad value or delimiter */
58 *val_p = val;
59 if (*next_s)
60 next_s++; /* skip delimeter */
61 return next_s;
62}
63
64
65/* Reads a comma-separated list of "offset:length" extents from |ex_str|. If
66 * |ex_arr| is NULL, then |ex_count| is ignored and it attempts to parse valid
67 * extents until the end of the string is reached. Otherwise, stores up to
68 * |ex_count| extents into |ex_arr|, which must be of at least this size.
69 * Returns the number of correctly parsed extents, or -1 if a malformed extent
70 * was found. */
71static ssize_t
72extents_read(const char *ex_str, ex_t *ex_arr, size_t ex_count)
73{
74 size_t i;
75 size_t last_i = ex_count - 1;
76 if (!ex_arr) {
77 ex_count = SIZE_MAX;
78 last_i = 0;
79 }
80 for (i = 0; *ex_str && i < ex_count; i++) {
81 long long raw_off = 0, raw_len = 0;
82 if (!((ex_str = read_llong(ex_str, &raw_off, -1,
83 MAX_ALLOWED(FIELD_TYPE(ex_t, off)),
84 ':', FALSE)) &&
85 (ex_str = read_llong(ex_str, &raw_len, 1,
86 MAX_ALLOWED(FIELD_TYPE(ex_t, len)),
87 ',', i >= last_i))))
88 return -1; /* parsing error */
89 if (ex_arr) {
90 ex_arr[i].off = raw_off;
91 ex_arr[i].len = raw_len;
92 }
93 }
94 return i;
95}
96
97
98ex_t *
99extents_parse(const char *ex_str, ex_t *ex_arr, size_t *ex_count_p)
100{
101 /* Sanity checks: a string must be provided; if an array is provided, an
102 * array count must be given as well. */
103 if (!ex_str || (ex_arr && !ex_count_p))
104 return NULL;
105
106 /* Parse string and count extents. */
107 ssize_t ret = extents_read(ex_str, NULL, 0);
108 if (ret < 0)
109 return NULL; /* parsing error */
110 size_t ex_count = (size_t)ret;
111
112 /* Input is good, commit to extent count. */
113 if (ex_count_p) {
114 size_t alloc_ex_count = *ex_count_p;
115 *ex_count_p = ex_count;
116 if (ex_arr && alloc_ex_count < ex_count)
117 return NULL; /* insufficient allocated space */
118 }
119 if (ex_count == 0)
120 return NULL; /* no extents, nothing to do */
121
122 /* Allocate extent array, if needed. */
123 if (!(ex_arr || (ex_arr = (ex_t *)malloc(sizeof(ex_t) * ex_count))))
124 return NULL; /* allocation failed */
125
126 /* Populate the extent array. */
127 extents_read(ex_str, ex_arr, ex_count);
128
129 return ex_arr;
130}