YAP 7.1.0
fmemopen-android.c
1/* Copyright (C) 2007 Eric Blake
2 * Permission to use, copy, modify, and distribute this software
3 * is freely granted, provided that this notice is preserved.
4 *
5 * Modifications for Android written Jul 2009 by Alan Viverette
6 */
7
8#if __ANDROID__
9
10/*
11FUNCTION
12<fmemopen>>---open a stream around a fixed-length string
13
14INDEX
15 fmemopen
16
17ANSI_SYNOPSIS
18 #include <stdio.h>
19 FILE *fmemopen(void *restrict <[buf]>, size_t <[size]>,
20 const char *restrict <[mode]>);
21
22DESCRIPTION
23<<fmemopen>> creates a seekable <<FILE>> stream that wraps a
24fixed-length buffer of <[size]> bytes starting at <[buf]>. The stream
25is opened with <[mode]> treated as in <<fopen>>, where append mode
26starts writing at the first NUL byte. If <[buf]> is NULL, then
27<[size]> bytes are automatically provided as if by <<malloc>>, with
28the initial size of 0, and <[mode]> must contain <<+>> so that data
29can be read after it is written.
30
31The stream maintains a current position, which moves according to
32bytes read or written, and which can be one past the end of the array.
33The stream also maintains a current file size, which is never greater
34than <[size]>. If <[mode]> starts with <<r>>, the position starts at
35<<0>>, and file size starts at <[size]> if <[buf]> was provided. If
36<[mode]> starts with <<w>>, the position and file size start at <<0>>,
37and if <[buf]> was provided, the first byte is set to NUL. If
38<[mode]> starts with <<a>>, the position and file size start at the
39location of the first NUL byte, or else <[size]> if <[buf]> was
40provided.
41
42When reading, NUL bytes have no significance, and reads cannot exceed
43the current file size. When writing, the file size can increase up to
44<[size]> as needed, and NUL bytes may be embedded in the stream (see
45<<open_memstream>> for an alternative that automatically enlarges the
46buffer). When the stream is flushed or closed after a write that
47changed the file size, a NUL byte is written at the current position
48if there is still room; if the stream is not also open for reading, a
49NUL byte is additionally written at the last byte of <[buf]> when the
50stream has exceeded <[size]>, so that a write-only <[buf]> is always
51NUL-terminated when the stream is flushed or closed (and the initial
52<[size]> should take this into account). It is not possible to seek
53outside the bounds of <[size]>. A NUL byte written during a flush is
54restored to its previous value when seeking elsewhere in the string.
55
56RETURNS
57The return value is an open FILE pointer on success. On error,
58<<NULL>> is returned, and <<errno>> will be set to EINVAL if <[size]>
59is zero or <[mode]> is invalid, ENOMEM if <[buf]> was NULL and memory
60could not be allocated, or EMFILE if too many streams are already
61open.
62
63PORTABILITY
64This function is being added to POSIX 200x, but is not in POSIX 2001.
65
66Supporting OS subroutines required: <<sbrk>>.
67*/
68
69#include <stdlib.h>
70#include <stdio.h>
71#include <errno.h>
72#include <string.h>
73#include <malloc.h>
74//#include "stdioext.h"
75
76/* Describe details of an open memstream. */
77typedef struct fmemcookie {
78 void *storage; /* storage to free on close */
79 char *buf; /* buffer start */
80 size_t pos; /* current position */
81 size_t eof; /* current file size */
82 size_t max; /* maximum file size */
83 char append; /* nonzero if appending */
84 char writeonly; /* 1 if write-only */
85 char saved; /* saved character that lived at pos before write-only NUL */
86} fmemcookie;
87
88/* Read up to non-zero N bytes into BUF from stream described by
89 COOKIE; return number of bytes read (0 on EOF). */
90static int
91fmemread(void *cookie, char *buf, int n)
92{
93 fmemcookie *c = (fmemcookie *) cookie;
94 /* Can't read beyond current size, but EOF condition is not an error. */
95 if (c->pos > c->eof)
96 return 0;
97 if (n >= c->eof - c->pos)
98 n = c->eof - c->pos;
99 memmove (buf, c->buf + c->pos, n);
100 c->pos += n;
101 return n;
102}
103
104/* Write up to non-zero N bytes of BUF into the stream described by COOKIE,
105 returning the number of bytes written or EOF on failure. */
106static int
107fmemwrite(void *cookie, const char *buf, int n)
108{
109 fmemcookie *c = (fmemcookie *) cookie;
110 int adjust = 0; /* true if at EOF, but still need to write NUL. */
111
112 /* Append always seeks to eof; otherwise, if we have previously done
113 a seek beyond eof, ensure all intermediate bytes are NUL. */
114 if (c->append)
115 c->pos = c->eof;
116 else if (c->pos > c->eof)
117 memset (c->buf + c->eof, '\0', c->pos - c->eof);
118 /* Do not write beyond EOF; saving room for NUL on write-only stream. */
119 if (c->pos + n > c->max - c->writeonly)
120 {
121 adjust = c->writeonly;
122 n = c->max - c->pos;
123 }
124 /* Now n is the number of bytes being modified, and adjust is 1 if
125 the last byte is NUL instead of from buf. Write a NUL if
126 write-only; or if read-write, eof changed, and there is still
127 room. When we are within the file contents, remember what we
128 overwrite so we can restore it if we seek elsewhere later. */
129 if (c->pos + n > c->eof)
130 {
131 c->eof = c->pos + n;
132 if (c->eof - adjust < c->max)
133 c->saved = c->buf[c->eof - adjust] = '\0';
134 }
135 else if (c->writeonly)
136 {
137 if (n)
138 {
139 c->saved = c->buf[c->pos + n - adjust];
140 c->buf[c->pos + n - adjust] = '\0';
141 }
142 else
143 adjust = 0;
144 }
145 c->pos += n;
146 if (n - adjust)
147 memmove (c->buf + c->pos - n, buf, n - adjust);
148 else
149 {
150 return EOF;
151 }
152 return n;
153}
154
155/* Seek to position POS relative to WHENCE within stream described by
156 COOKIE; return resulting position or fail with EOF. */
157static fpos_t
158fmemseek(void *cookie, fpos_t pos, int whence)
159{
160 fmemcookie *c = (fmemcookie *) cookie;
161 off_t offset = (off_t) pos;
162
163 if (whence == SEEK_CUR)
164 offset += c->pos;
165 else if (whence == SEEK_END)
166 offset += c->eof;
167 if (offset < 0)
168 {
169 offset = -1;
170 }
171 else if (offset > c->max)
172 {
173 offset = -1;
174 }
175 else
176 {
177 if (c->writeonly && c->pos < c->eof)
178 {
179 c->buf[c->pos] = c->saved;
180 c->saved = '\0';
181 }
182 c->pos = offset;
183 if (c->writeonly && c->pos < c->eof)
184 {
185 c->saved = c->buf[c->pos];
186 c->buf[c->pos] = '\0';
187 }
188 }
189 return (fpos_t) offset;
190}
191
192/* Reclaim resources used by stream described by COOKIE. */
193static int
194fmemclose(void *cookie)
195{
196 fmemcookie *c = (fmemcookie *) cookie;
197 free (c->storage);
198 return 0;
199}
200
201/* Open a memstream around buffer BUF of SIZE bytes, using MODE.
202 Return the new stream, or fail with NULL. */
203FILE *
204fmemopen(void *buf, size_t size, const char *mode)
205{
206 FILE *fp;
207 fmemcookie *c;
208 int flags;
209 int dummy;
210
211 if ((flags = __sflags (mode, &dummy)) == 0)
212 return NULL;
213 if (!size || !(buf || flags & __SAPP))
214 {
215 return NULL;
216 }
217 if ((fp = (FILE *) __sfp ()) == NULL)
218 return NULL;
219 if ((c = (fmemcookie *) malloc (sizeof *c + (buf ? 0 : size))) == NULL)
220 {
221 fp->_flags = 0; /* release */
222
223 return NULL;
224 }
225
226 c->storage = c;
227 c->max = size;
228 /* 9 modes to worry about. */
229 /* w/a, buf or no buf: Guarantee a NUL after any file writes. */
230 c->writeonly = (flags & __SWR) != 0;
231 c->saved = '\0';
232 if (!buf)
233 {
234 /* r+/w+/a+, and no buf: file starts empty. */
235 c->buf = (char *) (c + 1);
236 *(char *) buf = '\0';
237 c->pos = c->eof = 0;
238 c->append = (flags & __SAPP) != 0;
239 }
240 else
241 {
242 c->buf = (char *) buf;
243 switch (*mode)
244 {
245 case 'a':
246 /* a/a+ and buf: position and size at first NUL. */
247 buf = memchr (c->buf, '\0', size);
248 c->eof = c->pos = buf ? (char *) buf - c->buf : size;
249 if (!buf && c->writeonly)
250 /* a: guarantee a NUL within size even if no writes. */
251 c->buf[size - 1] = '\0';
252 c->append = 1;
253 break;
254 case 'r':
255 /* r/r+ and buf: read at beginning, full size available. */
256 c->pos = c->append = 0;
257 c->eof = size;
258 break;
259 case 'w':
260 /* w/w+ and buf: write at beginning, truncate to empty. */
261 c->pos = c->append = c->eof = 0;
262 *c->buf = '\0';
263 break;
264 default:
265 abort();
266 }
267 }
268
269 fp->_file = -1;
270 fp->_flags = flags;
271 fp->_cookie = c;
272 fp->_read = flags & (__SRD | __SRW) ? fmemread : NULL;
273 fp->_write = flags & (__SWR | __SRW) ? fmemwrite : NULL;
274 fp->_seek = fmemseek;
275 fp->_close = fmemclose;
276
277 return fp;
278}
279
280#endif