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