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 | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 19 | #include "hf/dlog.h" |
Andrew Scull | 8d9e121 | 2019-04-05 13:52:55 +0100 | [diff] [blame] | 20 | #include "hf/std.h" |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Initialises the given memory iterator. |
| 24 | */ |
| 25 | void memiter_init(struct memiter *it, const void *data, size_t size) |
| 26 | { |
| 27 | it->next = data; |
| 28 | it->limit = it->next + size; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Determines if the next character is a whitespace. |
| 33 | */ |
| 34 | static bool memiter_isspace(struct memiter *it) |
| 35 | { |
| 36 | switch (*it->next) { |
| 37 | case ' ': |
| 38 | case '\t': |
| 39 | case '\n': |
| 40 | case '\r': |
| 41 | return true; |
| 42 | default: |
| 43 | return false; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Moves iterator to the next non-whitespace character. |
| 49 | */ |
| 50 | static void memiter_skip_space(struct memiter *it) |
| 51 | { |
| 52 | while (it->next < it->limit && memiter_isspace(it)) { |
| 53 | it->next++; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Compares the iterator to a null-terminated string. |
| 59 | */ |
| 60 | bool memiter_iseq(const struct memiter *it, const char *str) |
| 61 | { |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 62 | size_t it_len = it->limit - it->next; |
| 63 | size_t len = strnlen_s(str, it_len + 1); |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 64 | |
Andrew Scull | 55baca6 | 2019-04-05 14:56:20 +0100 | [diff] [blame] | 65 | if (len != it_len) { |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 66 | return false; |
| 67 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 68 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 69 | return memcmp(it->next, str, len) == 0; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Retrieves the next string that is delimited by whitespaces. The result is |
| 74 | * stored in "str". |
| 75 | */ |
| 76 | bool memiter_parse_str(struct memiter *it, struct memiter *str) |
| 77 | { |
| 78 | /* Skip all white space and fail if we reach the end of the buffer. */ |
| 79 | memiter_skip_space(it); |
| 80 | if (it->next >= it->limit) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | str->next = it->next; |
| 85 | |
| 86 | /* Find the end of the string. */ |
| 87 | while (it->next < it->limit && !memiter_isspace(it)) { |
| 88 | it->next++; |
| 89 | } |
| 90 | |
| 91 | str->limit = it->next; |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Parses the next string that represents a 64-bit number. |
| 98 | */ |
| 99 | bool memiter_parse_uint(struct memiter *it, uint64_t *value) |
| 100 | { |
| 101 | uint64_t v = 0; |
| 102 | |
| 103 | /* Skip all white space and fail if we reach the end of the buffer. */ |
| 104 | memiter_skip_space(it); |
| 105 | if (it->next >= it->limit) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | /* Fail if it's not a number. */ |
| 110 | if (*it->next < '0' || *it->next > '9') { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | /* Parse the number. */ |
| 115 | do { |
| 116 | v = v * 10 + *it->next - '0'; |
| 117 | it->next++; |
| 118 | } while (it->next < it->limit && *it->next >= '0' && *it->next <= '9'); |
| 119 | |
| 120 | *value = v; |
| 121 | |
| 122 | return true; |
| 123 | } |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Advances the iterator by the given number of bytes. Returns true if the |
| 127 | * iterator was advanced without going over its limit; returns false and leaves |
| 128 | * the iterator unmodified otherwise. |
| 129 | */ |
| 130 | bool memiter_advance(struct memiter *it, size_t v) |
| 131 | { |
| 132 | const char *p = it->next + v; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 133 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 134 | if (p < it->next || p > it->limit) { |
| 135 | return false; |
| 136 | } |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 137 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 138 | it->next = p; |
Wedson Almeida Filho | 81568c4 | 2019-01-04 13:33:02 +0000 | [diff] [blame] | 139 | |
Wedson Almeida Filho | 9ee60e9 | 2018-07-23 18:56:56 +0100 | [diff] [blame] | 140 | return true; |
| 141 | } |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 142 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 143 | const void *memiter_base(const struct memiter *it) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 144 | { |
| 145 | return (const void *)it->next; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns the number of bytes in interval [it.next, it.limit). |
| 150 | */ |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 151 | size_t memiter_size(const struct memiter *it) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 152 | { |
| 153 | return it->limit - it->next; |
| 154 | } |