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