blob: 577bc61578cbb228b97982d9eb61fb50ec9b8866 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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 Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/memiter.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010018
David Brazdilb856be62020-03-25 10:14:55 +000019#include "hf/check.h"
David Brazdil7a462ec2019-08-15 12:27:47 +010020#include "hf/dlog.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010021#include "hf/std.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010022
23/**
24 * Initialises the given memory iterator.
25 */
26void 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 */
35static 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 */
51static 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 */
61bool memiter_iseq(const struct memiter *it, const char *str)
62{
Andrew Scull55baca62019-04-05 14:56:20 +010063 size_t it_len = it->limit - it->next;
64 size_t len = strnlen_s(str, it_len + 1);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000065
Andrew Scull55baca62019-04-05 14:56:20 +010066 if (len != it_len) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010067 return false;
68 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000069
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010070 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 */
77bool 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 */
100bool 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 Filho9ee60e92018-07-23 18:56:56 +0100125
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 */
131bool memiter_advance(struct memiter *it, size_t v)
132{
133 const char *p = it->next + v;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000134
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100135 if (p < it->next || p > it->limit) {
136 return false;
137 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000138
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100139 it->next = p;
David Brazdilb856be62020-03-25 10:14:55 +0000140 return true;
141}
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000142
David Brazdilb856be62020-03-25 10:14:55 +0000143/**
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 */
148bool 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 */
167bool 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 Filho9ee60e92018-07-23 18:56:56 +0100175 return true;
176}
David Brazdil7a462ec2019-08-15 12:27:47 +0100177
David Brazdil74e9c3b2019-08-28 11:09:08 +0100178const void *memiter_base(const struct memiter *it)
David Brazdil7a462ec2019-08-15 12:27:47 +0100179{
180 return (const void *)it->next;
181}
182
183/**
184 * Returns the number of bytes in interval [it.next, it.limit).
185 */
David Brazdil74e9c3b2019-08-28 11:09:08 +0100186size_t memiter_size(const struct memiter *it)
David Brazdil7a462ec2019-08-15 12:27:47 +0100187{
188 return it->limit - it->next;
189}