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