blob: 8b9854c2ba27c30a71e552f3ba141dbe18d9e4c0 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
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 Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/memiter.h"
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010018
Andrew Scull18c78fc2018-08-20 12:57:41 +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{
61 size_t len = strlen(str);
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000062
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010063 if (len != it->limit - it->next) {
64 return false;
65 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +000066
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010067 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 */
74bool 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 */
97bool 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 Filho9ee60e92018-07-23 18:56:56 +0100122
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 */
128bool memiter_advance(struct memiter *it, size_t v)
129{
130 const char *p = it->next + v;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000131
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100132 if (p < it->next || p > it->limit) {
133 return false;
134 }
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000135
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100136 it->next = p;
Wedson Almeida Filho81568c42019-01-04 13:33:02 +0000137
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +0100138 return true;
139}