blob: 5718cee6876a15d24db024ab3832a8a8709e6a8d [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))
Alex Deymoe1526cf2015-10-12 17:48:28 -070020#define MAX(a, b) ((a) > (b) ? (a) : (b))
Gilad Arnold99b53742013-04-30 09:24:14 -070021#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) \
Alex Deymoe1526cf2015-10-12 17:48:28 -070033 ((uintmax_t)(s) < (uintmax_t)LLONG_MAX ? (long long)(s) : LLONG_MAX)
Gilad Arnold99b53742013-04-30 09:24:14 -070034#define MAX_ALLOWED(t) LLONG_MAX_BOUND(INT_TYPE_MAX(t))
35
36/* Get the type of a struct field. */
Alex Deymoe1526cf2015-10-12 17:48:28 -070037#define FIELD_TYPE(t, f) typeof(((t*)0)->f)
Gilad Arnold99b53742013-04-30 09:24:14 -070038
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. */
Alex Deymoe1526cf2015-10-12 17:48:28 -070046const char* read_llong(const char* s,
47 long long* val_p,
48 long long min_val,
49 long long max_val,
50 char delim,
51 int may_end) {
52 assert(val_p);
53 const char* next_s;
54 errno = 0;
55 long long val = strtoll(s, (char**)&next_s, 10);
56 if (((val == LLONG_MAX || val == LLONG_MIN) && errno == ERANGE) ||
57 next_s == s || val < min_val || val > max_val ||
58 (*next_s ? *next_s != delim : !may_end))
59 return NULL; /* bad value or delimiter */
60 *val_p = val;
61 if (*next_s)
62 next_s++; /* skip delimeter */
63 return next_s;
Gilad Arnold99b53742013-04-30 09:24:14 -070064}
65
66
67/* Reads a comma-separated list of "offset:length" extents from |ex_str|. If
68 * |ex_arr| is NULL, then |ex_count| is ignored and it attempts to parse valid
69 * extents until the end of the string is reached. Otherwise, stores up to
70 * |ex_count| extents into |ex_arr|, which must be of at least this size.
71 * Returns the number of correctly parsed extents, or -1 if a malformed extent
72 * was found. */
Alex Deymoe1526cf2015-10-12 17:48:28 -070073static ssize_t extents_read(const char* ex_str, ex_t* ex_arr, size_t ex_count) {
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 =
83 read_llong(ex_str, &raw_off, -1,
84 MAX_ALLOWED(FIELD_TYPE(ex_t, off)), ':', 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;
Gilad Arnold99b53742013-04-30 09:24:14 -070092 }
Alex Deymoe1526cf2015-10-12 17:48:28 -070093 }
94 return i;
Gilad Arnold99b53742013-04-30 09:24:14 -070095}
96
97
Alex Deymoe1526cf2015-10-12 17:48:28 -070098ex_t* extents_parse(const char* ex_str, ex_t* ex_arr, size_t* ex_count_p) {
99 /* Sanity checks: a string must be provided; if an array is provided, an
100 * array count must be given as well. */
101 if (!ex_str || (ex_arr && !ex_count_p))
102 return NULL;
Gilad Arnold99b53742013-04-30 09:24:14 -0700103
Alex Deymoe1526cf2015-10-12 17:48:28 -0700104 /* Parse string and count extents. */
105 ssize_t ret = extents_read(ex_str, NULL, 0);
106 if (ret < 0)
107 return NULL; /* parsing error */
108 size_t ex_count = (size_t)ret;
Gilad Arnold99b53742013-04-30 09:24:14 -0700109
Alex Deymoe1526cf2015-10-12 17:48:28 -0700110 /* Input is good, commit to extent count. */
111 if (ex_count_p) {
112 size_t alloc_ex_count = *ex_count_p;
113 *ex_count_p = ex_count;
114 if (ex_arr && alloc_ex_count < ex_count)
115 return NULL; /* insufficient allocated space */
116 }
117 if (ex_count == 0)
118 return NULL; /* no extents, nothing to do */
Gilad Arnold99b53742013-04-30 09:24:14 -0700119
Alex Deymoe1526cf2015-10-12 17:48:28 -0700120 /* Allocate extent array, if needed. */
121 if (!(ex_arr || (ex_arr = (ex_t*)malloc(sizeof(ex_t) * ex_count))))
122 return NULL; /* allocation failed */
Gilad Arnold99b53742013-04-30 09:24:14 -0700123
Alex Deymoe1526cf2015-10-12 17:48:28 -0700124 /* Populate the extent array. */
125 extents_read(ex_str, ex_arr, ex_count);
Gilad Arnold99b53742013-04-30 09:24:14 -0700126
Alex Deymoe1526cf2015-10-12 17:48:28 -0700127 return ex_arr;
Gilad Arnold99b53742013-04-30 09:24:14 -0700128}