File: open_memstream.c

package info (click to toggle)
android-platform-system-core 21-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,624 kB
  • ctags: 11,983
  • sloc: ansic: 70,139; cpp: 14,766; asm: 1,774; sh: 875; yacc: 137; python: 131; makefile: 120; lex: 103; java: 79
file content (381 lines) | stat: -rw-r--r-- 10,606 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef HAVE_OPEN_MEMSTREAM

/*
 * Implementation of the POSIX open_memstream() function, which Linux has
 * but BSD lacks.
 *
 * Summary:
 * - Works like a file-backed FILE* opened with fopen(name, "w"), but the
 *   backing is a chunk of memory rather than a file.
 * - The buffer expands as you write more data.  Seeking past the end
 *   of the file and then writing to it zero-fills the gap.
 * - The values at "*bufp" and "*sizep" should be considered read-only,
 *   and are only valid immediately after an fflush() or fclose().
 * - A '\0' is maintained just past the end of the file. This is not included
 *   in "*sizep".  (The behavior w.r.t. fseek() is not clearly defined.
 *   The spec says the null byte is written when a write() advances EOF,
 *   but it looks like glibc ensures the null byte is always found at EOF,
 *   even if you just seeked backwards.  The example on the opengroup.org
 *   page suggests that this is the expected behavior.  The null must be
 *   present after a no-op fflush(), which we can't see, so we have to save
 *   and restore it.  Annoying, but allows file truncation.)
 * - After fclose(), the caller must eventually free(*bufp).
 *
 * This is built out of funopen(), which BSD has but Linux lacks.  There is
 * no flush() operator, so we need to keep the user pointers up to date
 * after each operation.
 *
 * I don't think Windows has any of the above, but we don't need to use
 * them there, so we just supply a stub.
 */
#include <cutils/open_memstream.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>

#if 0
# define DBUG(x) printf x
#else
# define DBUG(x) ((void)0)
#endif

#ifdef HAVE_FUNOPEN

/*
 * Definition of a seekable, write-only memory stream.
 */
typedef struct {
    char**      bufp;       /* pointer to buffer pointer */
    size_t*     sizep;      /* pointer to eof */

    size_t      allocSize;  /* size of buffer */
    size_t      eof;        /* furthest point we've written to */
    size_t      offset;     /* current write offset */
    char        saved;      /* required by NUL handling */
} MemStream;

#define kInitialSize    1024

/*
 * Ensure that we have enough storage to write "size" bytes at the
 * current offset.  We also have to take into account the extra '\0'
 * that we maintain just past EOF.
 *
 * Returns 0 on success.
 */
static int ensureCapacity(MemStream* stream, int writeSize)
{
    DBUG(("+++ ensureCap off=%d size=%d\n", stream->offset, writeSize));

    size_t neededSize = stream->offset + writeSize + 1;
    if (neededSize <= stream->allocSize)
        return 0;

    size_t newSize;

    if (stream->allocSize == 0) {
        newSize = kInitialSize;
    } else {
        newSize = stream->allocSize;
        newSize += newSize / 2;             /* expand by 3/2 */
    }

    if (newSize < neededSize)
        newSize = neededSize;
    DBUG(("+++ realloc %p->%p to size=%d\n",
        stream->bufp, *stream->bufp, newSize));
    char* newBuf = (char*) realloc(*stream->bufp, newSize);
    if (newBuf == NULL)
        return -1;

    *stream->bufp = newBuf;
    stream->allocSize = newSize;
    return 0;
}

/*
 * Write data to a memstream, expanding the buffer if necessary.
 *
 * If we previously seeked beyond EOF, zero-fill the gap.
 *
 * Returns the number of bytes written.
 */
static int write_memstream(void* cookie, const char* buf, int size)
{
    MemStream* stream = (MemStream*) cookie;

    if (ensureCapacity(stream, size) < 0)
        return -1;

    /* seeked past EOF earlier? */
    if (stream->eof < stream->offset) {
        DBUG(("+++ zero-fill gap from %d to %d\n",
            stream->eof, stream->offset-1));
        memset(*stream->bufp + stream->eof, '\0',
            stream->offset - stream->eof);
    }

    /* copy data, advance write pointer */
    memcpy(*stream->bufp + stream->offset, buf, size);
    stream->offset += size;

    if (stream->offset > stream->eof) {
        /* EOF has advanced, update it and append null byte */
        DBUG(("+++ EOF advanced to %d, appending nul\n", stream->offset));
        assert(stream->offset < stream->allocSize);
        stream->eof = stream->offset;
    } else {
        /* within previously-written area; save char we're about to stomp */
        DBUG(("+++ within written area, saving '%c' at %d\n",
            *(*stream->bufp + stream->offset), stream->offset));
        stream->saved = *(*stream->bufp + stream->offset);
    }
    *(*stream->bufp + stream->offset) = '\0';
    *stream->sizep = stream->offset;

    return size;
}

/*
 * Seek within a memstream.
 *
 * Returns the new offset, or -1 on failure.
 */
static fpos_t seek_memstream(void* cookie, fpos_t offset, int whence)
{
    MemStream* stream = (MemStream*) cookie;
    off_t newPosn = (off_t) offset;

    if (whence == SEEK_CUR) {
        newPosn += stream->offset;
    } else if (whence == SEEK_END) {
        newPosn += stream->eof;
    }

    if (newPosn < 0 || ((fpos_t)((size_t) newPosn)) != newPosn) {
        /* bad offset - negative or huge */
        DBUG(("+++ bogus seek offset %ld\n", (long) newPosn));
        errno = EINVAL;
        return (fpos_t) -1;
    }

    if (stream->offset < stream->eof) {
        /*
         * We were pointing to an area we'd already written to, which means
         * we stomped on a character and must now restore it.
         */
        DBUG(("+++ restoring char '%c' at %d\n",
            stream->saved, stream->offset));
        *(*stream->bufp + stream->offset) = stream->saved;
    }

    stream->offset = (size_t) newPosn;

    if (stream->offset < stream->eof) {
        /*
         * We're seeked backward into the stream.  Preserve the character
         * at EOF and stomp it with a NUL.
         */
        stream->saved = *(*stream->bufp + stream->offset);
        *(*stream->bufp + stream->offset) = '\0';
        *stream->sizep = stream->offset;
    } else {
        /*
         * We're positioned at, or possibly beyond, the EOF.  We want to
         * publish the current EOF, not the current position.
         */
        *stream->sizep = stream->eof;
    }

    return newPosn;
}

/*
 * Close the memstream.  We free everything but the data buffer.
 */
static int close_memstream(void* cookie)
{
    free(cookie);
    return 0;
}

/*
 * Prepare a memstream.
 */
FILE* open_memstream(char** bufp, size_t* sizep)
{
    FILE* fp;
    MemStream* stream;

    if (bufp == NULL || sizep == NULL) {
        errno = EINVAL;
        return NULL;
    }

    stream = (MemStream*) calloc(1, sizeof(MemStream));
    if (stream == NULL)
        return NULL;

    fp = funopen(stream,
        NULL, write_memstream, seek_memstream, close_memstream);
    if (fp == NULL) {
        free(stream);
        return NULL;
    }

    *sizep = 0;
    *bufp = NULL;
    stream->bufp = bufp;
    stream->sizep = sizep;

    return fp;
}

#else /*not HAVE_FUNOPEN*/
FILE* open_memstream(char** bufp, size_t* sizep)
{
    abort();
}
#endif /*HAVE_FUNOPEN*/



#if 0
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * Simple regression test.
 *
 * To test on desktop Linux with valgrind, it's possible to make a simple
 * change to open_memstream() to use fopencookie instead:
 *
 *  cookie_io_functions_t iofuncs =
 *      { NULL, write_memstream, seek_memstream, close_memstream };
 *  fp = fopencookie(stream, "w", iofuncs);
 *
 * (Some tweaks to seek_memstream are also required, as that takes a
 * pointer to an offset rather than an offset, and returns 0 or -1.)
 */
int testMemStream(void)
{
    FILE *stream;
    char *buf;
    size_t len;
    off_t eob;

    printf("Test1\n");

    /* std example */
    stream = open_memstream(&buf, &len);
    fprintf(stream, "hello my world");
    fflush(stream);
    printf("buf=%s, len=%zu\n", buf, len);
    eob = ftello(stream);
    fseeko(stream, 0, SEEK_SET);
    fprintf(stream, "good-bye");
    fseeko(stream, eob, SEEK_SET);
    fclose(stream);
    printf("buf=%s, len=%zu\n", buf, len);
    free(buf);

    printf("Test2\n");

    /* std example without final seek-to-end */
    stream = open_memstream(&buf, &len);
    fprintf(stream, "hello my world");
    fflush(stream);
    printf("buf=%s, len=%zu\n", buf, len);
    eob = ftello(stream);
    fseeko(stream, 0, SEEK_SET);
    fprintf(stream, "good-bye");
    //fseeko(stream, eob, SEEK_SET);
    fclose(stream);
    printf("buf=%s, len=%zu\n", buf, len);
    free(buf);

    printf("Test3\n");

    /* fancy example; should expand buffer with writes */
    static const int kCmpLen = 1024 + 128;
    char* cmp = malloc(kCmpLen);
    memset(cmp, 0, 1024);
    memset(cmp+1024, 0xff, kCmpLen-1024);
    sprintf(cmp, "This-is-a-tes1234");
    sprintf(cmp + 1022, "abcdef");

    stream = open_memstream (&buf, &len);
    setvbuf(stream, NULL, _IONBF, 0);   /* note: crashes in glibc with this */
    fprintf(stream, "This-is-a-test");
    fseek(stream, -1, SEEK_CUR);    /* broken in glibc; can use {13,SEEK_SET} */
    fprintf(stream, "1234");
    fseek(stream, 1022, SEEK_SET);
    fputc('a', stream);
    fputc('b', stream);
    fputc('c', stream);
    fputc('d', stream);
    fputc('e', stream);
    fputc('f', stream);
    fflush(stream);

    if (memcmp(buf, cmp, len+1) != 0) {
        printf("mismatch\n");
    } else {
        printf("match\n");
    }

    printf("Test4\n");
    stream = open_memstream (&buf, &len);
    fseek(stream, 5000, SEEK_SET);
    fseek(stream, 4096, SEEK_SET);
    fseek(stream, -1, SEEK_SET);        /* should have no effect */
    fputc('x', stream);
    if (ftell(stream) == 4097)
        printf("good\n");
    else
        printf("BAD: offset is %ld\n", ftell(stream));

    printf("DONE\n");

    return 0;
}

/* expected output:
Test1
buf=hello my world, len=14
buf=good-bye world, len=14
Test2
buf=hello my world, len=14
buf=good-bye, len=8
Test3
match
Test4
good
DONE
*/

#endif

#endif /*!HAVE_OPEN_MEMSTREAM*/