blob: d72250c042a0394f5a117847b4d264c7eb7526b1 [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/**
97 * Parses the next string that represents a 64-bit number.
98 */
99bool 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 Filho9ee60e92018-07-23 18:56:56 +0100124
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 */
130bool memiter_advance(struct memiter *it, size_t v)
131{
132 const char *p = it->next + v;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000133
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100134 if (p < it->next || p > it->limit) {
135 return false;
136 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000137
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100138 it->next = p;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000139
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100140 return true;
141}
David Brazdil7a462ec2019-08-15 12:27:47 +0100142
David Brazdil74e9c3b2019-08-28 11:09:08 +0100143const void *memiter_base(const struct memiter *it)
David Brazdil7a462ec2019-08-15 12:27:47 +0100144{
145 return (const void *)it->next;
146}
147
148/**
149 * Returns the number of bytes in interval [it.next, it.limit).
150 */
David Brazdil74e9c3b2019-08-28 11:09:08 +0100151size_t memiter_size(const struct memiter *it)
David Brazdil7a462ec2019-08-15 12:27:47 +0100152{
153 return it->limit - it->next;
154}