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