blob: 3331d37b5477d5f4019ac07d411cf5323613a12d [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 Brazdil7a462ec2019-08-15 12:27:47 +010019#include "hf/dlog.h"
Andrew Scull8d9e1212019-04-05 13:52:55 +010020#include "hf/std.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010021
22/**
23 * Initialises the given memory iterator.
24 */
25void 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 */
34static 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 */
50static 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 */
60bool memiter_iseq(const struct memiter *it, const char *str)
61{
Andrew Scull55baca62019-04-05 14:56:20 +010062 size_t it_len = it->limit - it->next;
63 size_t len = strnlen_s(str, it_len + 1);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000064
Andrew Scull55baca62019-04-05 14:56:20 +010065 if (len != it_len) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010066 return false;
67 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000068
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010069 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 */
76bool 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/**
David Brazdil7a462ec2019-08-15 12:27:47 +010097 * Prints the contents of memory covered by the iterator to dlog. It does *not*
98 * assume that the string is null-terminated.
99 */
100void memiter_dlog_str(struct memiter *it)
101{
102 const char *p;
103
104 for (p = it->next; p < it->limit; ++p) {
105 dlog("%c", *p);
106 }
107}
108
109/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100110 * Parses the next string that represents a 64-bit number.
111 */
112bool memiter_parse_uint(struct memiter *it, uint64_t *value)
113{
114 uint64_t v = 0;
115
116 /* Skip all white space and fail if we reach the end of the buffer. */
117 memiter_skip_space(it);
118 if (it->next >= it->limit) {
119 return false;
120 }
121
122 /* Fail if it's not a number. */
123 if (*it->next < '0' || *it->next > '9') {
124 return false;
125 }
126
127 /* Parse the number. */
128 do {
129 v = v * 10 + *it->next - '0';
130 it->next++;
131 } while (it->next < it->limit && *it->next >= '0' && *it->next <= '9');
132
133 *value = v;
134
135 return true;
136}
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100137
138/**
139 * Advances the iterator by the given number of bytes. Returns true if the
140 * iterator was advanced without going over its limit; returns false and leaves
141 * the iterator unmodified otherwise.
142 */
143bool memiter_advance(struct memiter *it, size_t v)
144{
145 const char *p = it->next + v;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000146
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100147 if (p < it->next || p > it->limit) {
148 return false;
149 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000150
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100151 it->next = p;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000152
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100153 return true;
154}
David Brazdil7a462ec2019-08-15 12:27:47 +0100155
David Brazdil74e9c3b2019-08-28 11:09:08 +0100156const void *memiter_base(const struct memiter *it)
David Brazdil7a462ec2019-08-15 12:27:47 +0100157{
158 return (const void *)it->next;
159}
160
161/**
162 * Returns the number of bytes in interval [it.next, it.limit).
163 */
David Brazdil74e9c3b2019-08-28 11:09:08 +0100164size_t memiter_size(const struct memiter *it)
David Brazdil7a462ec2019-08-15 12:27:47 +0100165{
166 return it->limit - it->next;
167}