1ad30f8e7SGabor Kovesdan /* $NetBSD: citrus_memstream.h,v 1.3 2005/05/14 17:55:42 tshiozak Exp $ */
2ad30f8e7SGabor Kovesdan
3ad30f8e7SGabor Kovesdan /*-
4*d915a14eSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause
5*d915a14eSPedro F. Giffuni *
6ad30f8e7SGabor Kovesdan * Copyright (c)2003 Citrus Project,
7ad30f8e7SGabor Kovesdan * All rights reserved.
8ad30f8e7SGabor Kovesdan *
9ad30f8e7SGabor Kovesdan * Redistribution and use in source and binary forms, with or without
10ad30f8e7SGabor Kovesdan * modification, are permitted provided that the following conditions
11ad30f8e7SGabor Kovesdan * are met:
12ad30f8e7SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright
13ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer.
14ad30f8e7SGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright
15ad30f8e7SGabor Kovesdan * notice, this list of conditions and the following disclaimer in the
16ad30f8e7SGabor Kovesdan * documentation and/or other materials provided with the distribution.
17ad30f8e7SGabor Kovesdan *
18ad30f8e7SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ad30f8e7SGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ad30f8e7SGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ad30f8e7SGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ad30f8e7SGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ad30f8e7SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ad30f8e7SGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ad30f8e7SGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ad30f8e7SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ad30f8e7SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ad30f8e7SGabor Kovesdan * SUCH DAMAGE.
29ad30f8e7SGabor Kovesdan *
30ad30f8e7SGabor Kovesdan */
31ad30f8e7SGabor Kovesdan
32ad30f8e7SGabor Kovesdan #ifndef _CITRUS_MEMSTREAM_H_
33ad30f8e7SGabor Kovesdan #define _CITRUS_MEMSTREAM_H_
34ad30f8e7SGabor Kovesdan
35ad30f8e7SGabor Kovesdan struct _citrus_memory_stream {
36ad30f8e7SGabor Kovesdan struct _citrus_region ms_region;
37ad30f8e7SGabor Kovesdan size_t ms_pos;
38ad30f8e7SGabor Kovesdan };
39ad30f8e7SGabor Kovesdan
40ad30f8e7SGabor Kovesdan __BEGIN_DECLS
41ad30f8e7SGabor Kovesdan const char *_citrus_memory_stream_getln(
42ad30f8e7SGabor Kovesdan struct _citrus_memory_stream * __restrict,
43ad30f8e7SGabor Kovesdan size_t * __restrict);
44ad30f8e7SGabor Kovesdan const char *_citrus_memory_stream_matchline(
45ad30f8e7SGabor Kovesdan struct _citrus_memory_stream * __restrict,
46ad30f8e7SGabor Kovesdan const char * __restrict, size_t * __restrict, int);
47ad30f8e7SGabor Kovesdan void *_citrus_memory_stream_chr(struct _citrus_memory_stream *,
48ad30f8e7SGabor Kovesdan struct _citrus_region *, char);
49ad30f8e7SGabor Kovesdan void _citrus_memory_stream_skip_ws(struct _citrus_memory_stream *);
50ad30f8e7SGabor Kovesdan __END_DECLS
51ad30f8e7SGabor Kovesdan
52ad30f8e7SGabor Kovesdan static __inline int
_citrus_memory_stream_iseof(struct _citrus_memory_stream * ms)53ad30f8e7SGabor Kovesdan _citrus_memory_stream_iseof(struct _citrus_memory_stream *ms)
54ad30f8e7SGabor Kovesdan {
55ad30f8e7SGabor Kovesdan
56ad30f8e7SGabor Kovesdan return (ms->ms_pos >= _citrus_region_size(&ms->ms_region));
57ad30f8e7SGabor Kovesdan }
58ad30f8e7SGabor Kovesdan
59ad30f8e7SGabor Kovesdan static __inline void
_citrus_memory_stream_bind(struct _citrus_memory_stream * __restrict ms,const struct _citrus_region * __restrict r)60ad30f8e7SGabor Kovesdan _citrus_memory_stream_bind(struct _citrus_memory_stream * __restrict ms,
61ad30f8e7SGabor Kovesdan const struct _citrus_region * __restrict r)
62ad30f8e7SGabor Kovesdan {
63ad30f8e7SGabor Kovesdan
64ad30f8e7SGabor Kovesdan ms->ms_region = *r;
65ad30f8e7SGabor Kovesdan ms->ms_pos = 0;
66ad30f8e7SGabor Kovesdan }
67ad30f8e7SGabor Kovesdan
68ad30f8e7SGabor Kovesdan static __inline void
_citrus_memory_stream_bind_ptr(struct _citrus_memory_stream * __restrict ms,void * ptr,size_t sz)69ad30f8e7SGabor Kovesdan _citrus_memory_stream_bind_ptr(struct _citrus_memory_stream * __restrict ms,
70ad30f8e7SGabor Kovesdan void *ptr, size_t sz)
71ad30f8e7SGabor Kovesdan {
72ad30f8e7SGabor Kovesdan struct _citrus_region r;
73ad30f8e7SGabor Kovesdan
74ad30f8e7SGabor Kovesdan _citrus_region_init(&r, ptr, sz);
75ad30f8e7SGabor Kovesdan _citrus_memory_stream_bind(ms, &r);
76ad30f8e7SGabor Kovesdan }
77ad30f8e7SGabor Kovesdan
78ad30f8e7SGabor Kovesdan static __inline void
_citrus_memory_stream_rewind(struct _citrus_memory_stream * ms)79ad30f8e7SGabor Kovesdan _citrus_memory_stream_rewind(struct _citrus_memory_stream *ms)
80ad30f8e7SGabor Kovesdan {
81ad30f8e7SGabor Kovesdan
82ad30f8e7SGabor Kovesdan ms->ms_pos = 0;
83ad30f8e7SGabor Kovesdan }
84ad30f8e7SGabor Kovesdan
85ad30f8e7SGabor Kovesdan static __inline size_t
_citrus_memory_stream_tell(struct _citrus_memory_stream * ms)86ad30f8e7SGabor Kovesdan _citrus_memory_stream_tell(struct _citrus_memory_stream *ms)
87ad30f8e7SGabor Kovesdan {
88ad30f8e7SGabor Kovesdan
89ad30f8e7SGabor Kovesdan return (ms->ms_pos);
90ad30f8e7SGabor Kovesdan }
91ad30f8e7SGabor Kovesdan
92ad30f8e7SGabor Kovesdan static __inline size_t
_citrus_memory_stream_remainder(struct _citrus_memory_stream * ms)93ad30f8e7SGabor Kovesdan _citrus_memory_stream_remainder(struct _citrus_memory_stream *ms)
94ad30f8e7SGabor Kovesdan {
95ad30f8e7SGabor Kovesdan size_t sz;
96ad30f8e7SGabor Kovesdan
97ad30f8e7SGabor Kovesdan sz = _citrus_region_size(&ms->ms_region);
98ad30f8e7SGabor Kovesdan if (ms->ms_pos>sz)
99ad30f8e7SGabor Kovesdan return (0);
100ad30f8e7SGabor Kovesdan return (sz-ms->ms_pos);
101ad30f8e7SGabor Kovesdan }
102ad30f8e7SGabor Kovesdan
103ad30f8e7SGabor Kovesdan static __inline int
_citrus_memory_stream_seek(struct _citrus_memory_stream * ms,size_t pos,int w)104ad30f8e7SGabor Kovesdan _citrus_memory_stream_seek(struct _citrus_memory_stream *ms, size_t pos, int w)
105ad30f8e7SGabor Kovesdan {
106ad30f8e7SGabor Kovesdan size_t sz;
107ad30f8e7SGabor Kovesdan
108ad30f8e7SGabor Kovesdan sz = _citrus_region_size(&ms->ms_region);
109ad30f8e7SGabor Kovesdan
110ad30f8e7SGabor Kovesdan switch (w) {
111ad30f8e7SGabor Kovesdan case SEEK_SET:
112ad30f8e7SGabor Kovesdan if (pos >= sz)
113ad30f8e7SGabor Kovesdan return (-1);
114ad30f8e7SGabor Kovesdan ms->ms_pos = pos;
115ad30f8e7SGabor Kovesdan break;
116ad30f8e7SGabor Kovesdan case SEEK_CUR:
117ad30f8e7SGabor Kovesdan pos += (ssize_t)ms->ms_pos;
118ad30f8e7SGabor Kovesdan if (pos >= sz)
119ad30f8e7SGabor Kovesdan return (-1);
120ad30f8e7SGabor Kovesdan ms->ms_pos = pos;
121ad30f8e7SGabor Kovesdan break;
122ad30f8e7SGabor Kovesdan case SEEK_END:
123ad30f8e7SGabor Kovesdan if (sz < pos)
124ad30f8e7SGabor Kovesdan return (-1);
125ad30f8e7SGabor Kovesdan ms->ms_pos = sz - pos;
126ad30f8e7SGabor Kovesdan break;
127ad30f8e7SGabor Kovesdan }
128ad30f8e7SGabor Kovesdan return (0);
129ad30f8e7SGabor Kovesdan }
130ad30f8e7SGabor Kovesdan
131ad30f8e7SGabor Kovesdan static __inline int
_citrus_memory_stream_getc(struct _citrus_memory_stream * ms)132ad30f8e7SGabor Kovesdan _citrus_memory_stream_getc(struct _citrus_memory_stream *ms)
133ad30f8e7SGabor Kovesdan {
134ad30f8e7SGabor Kovesdan
135ad30f8e7SGabor Kovesdan if (_citrus_memory_stream_iseof(ms))
136ad30f8e7SGabor Kovesdan return (EOF);
137ad30f8e7SGabor Kovesdan return (_citrus_region_peek8(&ms->ms_region, ms->ms_pos++));
138ad30f8e7SGabor Kovesdan }
139ad30f8e7SGabor Kovesdan
140ad30f8e7SGabor Kovesdan static __inline void
_citrus_memory_stream_ungetc(struct _citrus_memory_stream * ms,int ch)141ad30f8e7SGabor Kovesdan _citrus_memory_stream_ungetc(struct _citrus_memory_stream *ms, int ch)
142ad30f8e7SGabor Kovesdan {
143ad30f8e7SGabor Kovesdan
144ad30f8e7SGabor Kovesdan if (ch != EOF && ms->ms_pos > 0)
145ad30f8e7SGabor Kovesdan ms->ms_pos--;
146ad30f8e7SGabor Kovesdan }
147ad30f8e7SGabor Kovesdan
148ad30f8e7SGabor Kovesdan static __inline int
_citrus_memory_stream_peek(struct _citrus_memory_stream * ms)149ad30f8e7SGabor Kovesdan _citrus_memory_stream_peek(struct _citrus_memory_stream *ms)
150ad30f8e7SGabor Kovesdan {
151ad30f8e7SGabor Kovesdan
152ad30f8e7SGabor Kovesdan if (_citrus_memory_stream_iseof(ms))
153ad30f8e7SGabor Kovesdan return (EOF);
154ad30f8e7SGabor Kovesdan return (_citrus_region_peek8(&ms->ms_region, ms->ms_pos));
155ad30f8e7SGabor Kovesdan }
156ad30f8e7SGabor Kovesdan
157ad30f8e7SGabor Kovesdan static __inline void *
_citrus_memory_stream_getregion(struct _citrus_memory_stream * ms,struct _citrus_region * r,size_t sz)158ad30f8e7SGabor Kovesdan _citrus_memory_stream_getregion(struct _citrus_memory_stream *ms,
159ad30f8e7SGabor Kovesdan struct _citrus_region *r, size_t sz)
160ad30f8e7SGabor Kovesdan {
161ad30f8e7SGabor Kovesdan void *ret;
162ad30f8e7SGabor Kovesdan
163ad30f8e7SGabor Kovesdan if (ms->ms_pos + sz > _citrus_region_size(&ms->ms_region))
164ad30f8e7SGabor Kovesdan return (NULL);
165ad30f8e7SGabor Kovesdan
166ad30f8e7SGabor Kovesdan ret = _citrus_region_offset(&ms->ms_region, ms->ms_pos);
167ad30f8e7SGabor Kovesdan ms->ms_pos += sz;
168ad30f8e7SGabor Kovesdan if (r)
169ad30f8e7SGabor Kovesdan _citrus_region_init(r, ret, sz);
170ad30f8e7SGabor Kovesdan
171ad30f8e7SGabor Kovesdan return (ret);
172ad30f8e7SGabor Kovesdan }
173ad30f8e7SGabor Kovesdan
174ad30f8e7SGabor Kovesdan static __inline int
_citrus_memory_stream_get8(struct _citrus_memory_stream * ms,uint8_t * rval)175ad30f8e7SGabor Kovesdan _citrus_memory_stream_get8(struct _citrus_memory_stream *ms, uint8_t *rval)
176ad30f8e7SGabor Kovesdan {
177ad30f8e7SGabor Kovesdan
178ad30f8e7SGabor Kovesdan if (ms->ms_pos + 1 > _citrus_region_size(&ms->ms_region))
179ad30f8e7SGabor Kovesdan return (-1);
180ad30f8e7SGabor Kovesdan
181ad30f8e7SGabor Kovesdan *rval = _citrus_region_peek8(&ms->ms_region, ms->ms_pos);
182ad30f8e7SGabor Kovesdan ms->ms_pos += 2;
183ad30f8e7SGabor Kovesdan
184ad30f8e7SGabor Kovesdan return (0);
185ad30f8e7SGabor Kovesdan }
186ad30f8e7SGabor Kovesdan
187ad30f8e7SGabor Kovesdan static __inline int
_citrus_memory_stream_get16(struct _citrus_memory_stream * ms,uint16_t * rval)188ad30f8e7SGabor Kovesdan _citrus_memory_stream_get16(struct _citrus_memory_stream *ms, uint16_t *rval)
189ad30f8e7SGabor Kovesdan {
190ad30f8e7SGabor Kovesdan
191ad30f8e7SGabor Kovesdan if (ms->ms_pos + 2 > _citrus_region_size(&ms->ms_region))
192ad30f8e7SGabor Kovesdan return (-1);
193ad30f8e7SGabor Kovesdan
194ad30f8e7SGabor Kovesdan *rval = _citrus_region_peek16(&ms->ms_region, ms->ms_pos);
195ad30f8e7SGabor Kovesdan ms->ms_pos += 2;
196ad30f8e7SGabor Kovesdan
197ad30f8e7SGabor Kovesdan return (0);
198ad30f8e7SGabor Kovesdan }
199ad30f8e7SGabor Kovesdan
200ad30f8e7SGabor Kovesdan static __inline int
_citrus_memory_stream_get32(struct _citrus_memory_stream * ms,uint32_t * rval)201ad30f8e7SGabor Kovesdan _citrus_memory_stream_get32(struct _citrus_memory_stream *ms, uint32_t *rval)
202ad30f8e7SGabor Kovesdan {
203ad30f8e7SGabor Kovesdan
204ad30f8e7SGabor Kovesdan if (ms->ms_pos + 4 > _citrus_region_size(&ms->ms_region))
205ad30f8e7SGabor Kovesdan return (-1);
206ad30f8e7SGabor Kovesdan
207ad30f8e7SGabor Kovesdan *rval = _citrus_region_peek32(&ms->ms_region, ms->ms_pos);
208ad30f8e7SGabor Kovesdan ms->ms_pos += 4;
209ad30f8e7SGabor Kovesdan
210ad30f8e7SGabor Kovesdan return (0);
211ad30f8e7SGabor Kovesdan }
212ad30f8e7SGabor Kovesdan
213ad30f8e7SGabor Kovesdan static __inline int
_citrus_memory_stream_getln_region(struct _citrus_memory_stream * ms,struct _citrus_region * r)214ad30f8e7SGabor Kovesdan _citrus_memory_stream_getln_region(struct _citrus_memory_stream *ms,
215ad30f8e7SGabor Kovesdan struct _citrus_region *r)
216ad30f8e7SGabor Kovesdan {
217ad30f8e7SGabor Kovesdan const char *ptr;
218ad30f8e7SGabor Kovesdan size_t sz;
219ad30f8e7SGabor Kovesdan
220ad30f8e7SGabor Kovesdan ptr = _citrus_memory_stream_getln(ms, &sz);
221ad30f8e7SGabor Kovesdan if (ptr)
222ad30f8e7SGabor Kovesdan _citrus_region_init(r, __DECONST(void *, ptr), sz);
223ad30f8e7SGabor Kovesdan
224ad30f8e7SGabor Kovesdan return (ptr == NULL);
225ad30f8e7SGabor Kovesdan }
226ad30f8e7SGabor Kovesdan
227ad30f8e7SGabor Kovesdan #endif
228