blob: 5ff54ee5ef6722ca7999221a988b23c235bab5fa [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
Andrew Scull8d9e1212019-04-05 13:52:55 +010019#include "hf/std.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010020
21/**
22 * Initialises the given memory iterator.
23 */
24void 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 */
33static 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 */
49static 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 */
59bool memiter_iseq(const struct memiter *it, const char *str)
60{
Andrew Scull55baca62019-04-05 14:56:20 +010061 size_t it_len = it->limit - it->next;
62 size_t len = strnlen_s(str, it_len + 1);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000063
Andrew Scull55baca62019-04-05 14:56:20 +010064 if (len != it_len) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010065 return false;
66 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000067
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010068 return memcmp(it->next, str, len) == 0;
69}
70
71/**
72 * Retrieves the next string that is delimited by whitespaces. The result is
73 * stored in "str".
74 */
75bool memiter_parse_str(struct memiter *it, struct memiter *str)
76{
77 /* Skip all white space and fail if we reach the end of the buffer. */
78 memiter_skip_space(it);
79 if (it->next >= it->limit) {
80 return false;
81 }
82
83 str->next = it->next;
84
85 /* Find the end of the string. */
86 while (it->next < it->limit && !memiter_isspace(it)) {
87 it->next++;
88 }
89
90 str->limit = it->next;
91
92 return true;
93}
94
95/**
96 * Parses the next string that represents a 64-bit number.
97 */
98bool memiter_parse_uint(struct memiter *it, uint64_t *value)
99{
100 uint64_t v = 0;
101
102 /* Skip all white space and fail if we reach the end of the buffer. */
103 memiter_skip_space(it);
104 if (it->next >= it->limit) {
105 return false;
106 }
107
108 /* Fail if it's not a number. */
109 if (*it->next < '0' || *it->next > '9') {
110 return false;
111 }
112
113 /* Parse the number. */
114 do {
115 v = v * 10 + *it->next - '0';
116 it->next++;
117 } while (it->next < it->limit && *it->next >= '0' && *it->next <= '9');
118
119 *value = v;
120
121 return true;
122}
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100123
124/**
125 * Advances the iterator by the given number of bytes. Returns true if the
126 * iterator was advanced without going over its limit; returns false and leaves
127 * the iterator unmodified otherwise.
128 */
129bool memiter_advance(struct memiter *it, size_t v)
130{
131 const char *p = it->next + v;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000132
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100133 if (p < it->next || p > it->limit) {
134 return false;
135 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000136
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100137 it->next = p;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000138
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100139 return true;
140}