Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/memiter.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 18 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 19 | #include "hf/check.h" |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 20 | #include "hf/dlog.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 21 | #include "hf/std.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Initialises the given memory iterator. |
| 25 | */ |
| 26 | void memiter_init(struct memiter *it, const void *data, size_t size) |
| 27 | { |
| 28 | it->next = data; |
| 29 | it->limit = it->next + size; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Determines if the next character is a whitespace. |
| 34 | */ |
| 35 | static bool memiter_isspace(struct memiter *it) |
| 36 | { |
| 37 | switch (*it->next) { |
| 38 | case ' ': |
| 39 | case '\t': |
| 40 | case '\n': |
| 41 | case '\r': |
| 42 | return true; |
| 43 | default: |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Moves iterator to the next non-whitespace character. |
| 50 | */ |
| 51 | static void memiter_skip_space(struct memiter *it) |
| 52 | { |
| 53 | while (it->next < it->limit && memiter_isspace(it)) { |
| 54 | it->next++; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Compares the iterator to a null-terminated string. |
| 60 | */ |
| 61 | bool memiter_iseq(const struct memiter *it, const char *str) |
| 62 | { |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 63 | size_t it_len = it->limit - it->next; |
| 64 | size_t len = strnlen_s(str, it_len + 1); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 65 | |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 66 | if (len != it_len) { |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 67 | return false; |
| 68 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 69 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 70 | return memcmp(it->next, str, len) == 0; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Retrieves the next string that is delimited by whitespaces. The result is |
| 75 | * stored in "str". |
| 76 | */ |
| 77 | bool memiter_parse_str(struct memiter *it, struct memiter *str) |
| 78 | { |
| 79 | /* Skip all white space and fail if we reach the end of the buffer. */ |
| 80 | memiter_skip_space(it); |
| 81 | if (it->next >= it->limit) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | str->next = it->next; |
| 86 | |
| 87 | /* Find the end of the string. */ |
| 88 | while (it->next < it->limit && !memiter_isspace(it)) { |
| 89 | it->next++; |
| 90 | } |
| 91 | |
| 92 | str->limit = it->next; |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Parses the next string that represents a 64-bit number. |
| 99 | */ |
| 100 | bool memiter_parse_uint(struct memiter *it, uint64_t *value) |
| 101 | { |
| 102 | uint64_t v = 0; |
| 103 | |
| 104 | /* Skip all white space and fail if we reach the end of the buffer. */ |
| 105 | memiter_skip_space(it); |
| 106 | if (it->next >= it->limit) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | /* Fail if it's not a number. */ |
| 111 | if (*it->next < '0' || *it->next > '9') { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | /* Parse the number. */ |
| 116 | do { |
| 117 | v = v * 10 + *it->next - '0'; |
| 118 | it->next++; |
| 119 | } while (it->next < it->limit && *it->next >= '0' && *it->next <= '9'); |
| 120 | |
| 121 | *value = v; |
| 122 | |
| 123 | return true; |
| 124 | } |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 125 | |
| 126 | /** |
| 127 | * Advances the iterator by the given number of bytes. Returns true if the |
| 128 | * iterator was advanced without going over its limit; returns false and leaves |
| 129 | * the iterator unmodified otherwise. |
| 130 | */ |
| 131 | bool memiter_advance(struct memiter *it, size_t v) |
| 132 | { |
| 133 | const char *p = it->next + v; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 134 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 135 | if (p < it->next || p > it->limit) { |
| 136 | return false; |
| 137 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 138 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 139 | it->next = p; |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 140 | return true; |
| 141 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 142 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 143 | /** |
| 144 | * Decrements the limit of iterator by the given number of bytes. Returns true |
| 145 | * if the limit was reduced without going over the base; returns false and |
| 146 | * leaves the iterator unmodified otherwise. |
| 147 | */ |
| 148 | bool memiter_restrict(struct memiter *it, size_t v) |
| 149 | { |
| 150 | size_t s = memiter_size(it); |
| 151 | |
| 152 | if (v > s) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | it->limit = it->next + (s - v); |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Initializes `newit` with the first `v` bytes of `it` and advances `it` by |
| 162 | * the same number of bytes. This splits the original range into two iterators |
| 163 | * after `v` bytes. |
| 164 | * Returns true on success; returns false and leaves `it` unmodified and `newit` |
| 165 | * uninitialized otherwise. |
| 166 | */ |
| 167 | bool memiter_consume(struct memiter *it, size_t v, struct memiter *newit) |
| 168 | { |
| 169 | if (v > memiter_size(it)) { |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | memiter_init(newit, memiter_base(it), v); |
| 174 | CHECK(memiter_advance(it, v)); |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 175 | return true; |
| 176 | } |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 177 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 178 | const void *memiter_base(const struct memiter *it) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 179 | { |
| 180 | return (const void *)it->next; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns the number of bytes in interval [it.next, it.limit). |
| 185 | */ |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 186 | size_t memiter_size(const struct memiter *it) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 187 | { |
| 188 | return it->limit - it->next; |
| 189 | } |