| 1 |
/* memstream.c |
| 2 |
* |
| 3 |
* This file is licensed under the MIT license by |
| 4 |
* |
| 5 |
* Copyright (c) 2013 Ian Piumarta. |
| 6 |
* |
| 7 |
* Modifications for inclusion in macports-legacy-support |
| 8 |
* Copyright (c) 2023 Dennis Camera. |
| 9 |
* |
| 10 |
* ---------------------------------------------------------------------------- |
| 11 |
* |
| 12 |
* OPEN_MEMSTREAM(3) BSD and Linux Library Functions OPEN_MEMSTREAM(3) |
| 13 |
* |
| 14 |
* SYNOPSIS |
| 15 |
* FILE *open_memstream(char **bufp, size_t *sizep); |
| 16 |
* |
| 17 |
* DESCRIPTION |
| 18 |
* The open_memstream() function opens a stream for writing to a buffer. |
| 19 |
* The buffer is dynamically allocated (as with malloc(3)), and |
| 20 |
* automatically grows as required. After closing the stream, the caller |
| 21 |
* should free(3) this buffer. |
| 22 |
* |
| 23 |
* When the stream is closed (fclose(3)) or flushed (fflush(3)), the |
| 24 |
* locations pointed to by bufp and sizep are updated to contain, |
| 25 |
* respectively, a pointer to the buffer and the current size of the |
| 26 |
* buffer. These values remain valid only as long as the caller performs |
| 27 |
* no further output on the stream. If further output is performed, then |
| 28 |
* the stream must again be flushed before trying to access these |
| 29 |
* variables. |
| 30 |
* |
| 31 |
* A null byte is maintained at the end of the buffer. This byte is not |
| 32 |
* included in the size value stored at sizep. |
| 33 |
* |
| 34 |
* The stream's file position can be changed with fseek(3) or fseeko(3). |
| 35 |
* Moving the file position past the end of the data already written fills |
| 36 |
* the intervening space with zeros. |
| 37 |
* |
| 38 |
* RETURN VALUE |
| 39 |
* Upon successful completion open_memstream() returns a FILE pointer. |
| 40 |
* Otherwise, NULL is returned and errno is set to indicate the error. |
| 41 |
* |
| 42 |
* CONFORMING TO |
| 43 |
* POSIX.1-2008 |
| 44 |
* |
| 45 |
* ---------------------------------------------------------------------------- |
| 46 |
*/ |
| 47 |
|
| 48 |
/* MP support header */ |
| 49 |
#include "MacportsLegacySupport.h" |
| 50 |
#if __MP_LEGACY_SUPPORT_OPEN_MEMSTREAM__ |
| 51 |
|
| 52 |
#include "stdio.h" |
| 53 |
|
| 54 |
#include <stddef.h> |
| 55 |
#include <stdlib.h> |
| 56 |
#include <string.h> |
| 57 |
#include <errno.h> |
| 58 |
#include <assert.h> |
| 59 |
|
| 60 |
#define min(X, Y) (((X) < (Y)) ? (X) : (Y)) |
| 61 |
|
| 62 |
struct memstream |
| 63 |
{ |
| 64 |
int position; |
| 65 |
int size; |
| 66 |
int capacity; |
| 67 |
char *contents; |
| 68 |
char **ptr; |
| 69 |
size_t *sizeloc; |
| 70 |
}; |
| 71 |
|
| 72 |
#if MEMSTREAM_DEBUG |
| 73 |
static void memstream_print(struct memstream *ms) |
| 74 |
{ |
| 75 |
printf("memstream %p {", ms); |
| 76 |
printf(" %i", ms->position); |
| 77 |
printf(" %i", ms->size); |
| 78 |
printf(" %i", ms->capacity); |
| 79 |
printf(" %p", ms->contents); |
| 80 |
printf(" }\n"); |
| 81 |
} |
| 82 |
# define memstream_info(ARGS) printf ARGS |
| 83 |
#else |
| 84 |
# define memstream_print(ms) |
| 85 |
# define memstream_info(ARGS) |
| 86 |
#endif |
| 87 |
|
| 88 |
#define memstream_check(MS) if (!(MS)->contents) { errno= ENOMEM; return -1; } |
| 89 |
|
| 90 |
static int memstream_grow(struct memstream *ms, int minsize) |
| 91 |
{ |
| 92 |
int newcap= ms->capacity * 2; memstream_check(ms); |
| 93 |
while (newcap <= minsize) newcap *= 2; memstream_info(("grow %p to %i\n", ms, newcap)); |
| 94 |
ms->contents= realloc(ms->contents, newcap); |
| 95 |
if (!ms->contents) return -1; /* errno == ENOMEM */ |
| 96 |
memset(ms->contents + ms->capacity, 0, newcap - ms->capacity); |
| 97 |
ms->capacity= newcap; |
| 98 |
*ms->ptr= ms->contents; /* size has not changed */ |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
static int memstream_read(void *cookie, char *buf, int count) |
| 103 |
{ |
| 104 |
struct memstream *ms= (struct memstream *)cookie; memstream_check(ms); |
| 105 |
int n= min(ms->size - ms->position, count); memstream_info(("memstream_read %p %i\n", ms, count)); |
| 106 |
if (n < 1) return 0; |
| 107 |
memcpy(buf, ms->contents, n); |
| 108 |
ms->position += n; memstream_print(ms); |
| 109 |
return n; |
| 110 |
} |
| 111 |
|
| 112 |
static int memstream_write(void *cookie, const char *buf, int count) |
| 113 |
{ |
| 114 |
struct memstream *ms= (struct memstream *)cookie; memstream_check(ms); |
| 115 |
if (ms->capacity <= ms->position + count) |
| 116 |
if (memstream_grow(ms, ms->position + count) < 0) /* errno == ENOMEM */ |
| 117 |
return -1; |
| 118 |
memcpy(ms->contents + ms->position, buf, count); memstream_info(("memstream_write %p %i\n", ms, count)); |
| 119 |
ms->position += count; |
| 120 |
if (ms->size < ms->position) *ms->sizeloc= ms->size= ms->position; memstream_print(ms); |
| 121 |
assert(ms->size < ms->capacity); |
| 122 |
assert(ms->contents[ms->size] == 0); |
| 123 |
return count; |
| 124 |
} |
| 125 |
|
| 126 |
static fpos_t memstream_seek(void *cookie, fpos_t offset, int whence) |
| 127 |
{ |
| 128 |
struct memstream *ms= (struct memstream *)cookie; |
| 129 |
fpos_t pos= 0; memstream_check(ms); |
| 130 |
memstream_info(("memstream_seek %p %i %i\n", ms, (int)offset, whence)); |
| 131 |
switch (whence) { |
| 132 |
case SEEK_SET: pos= offset; break; |
| 133 |
case SEEK_CUR: pos= ms->position + offset; break; |
| 134 |
case SEEK_END: pos= ms->size + offset; break; |
| 135 |
default: errno= EINVAL; return -1; |
| 136 |
} |
| 137 |
if (pos >= ms->capacity) memstream_grow(ms, pos); |
| 138 |
ms->position= pos; |
| 139 |
if (ms->size < ms->position) *ms->sizeloc= ms->size= ms->position; memstream_print(ms); memstream_info(("=> %i\n", (int)pos)); |
| 140 |
assert(ms->size < ms->capacity && ms->contents[ms->size] == 0); |
| 141 |
return pos; |
| 142 |
} |
| 143 |
|
| 144 |
static int memstream_close(void *cookie) |
| 145 |
{ |
| 146 |
struct memstream *ms= (struct memstream *)cookie; if (!ms->contents) { free(ms); errno= ENOMEM; return -1; } |
| 147 |
ms->size= min(ms->size, ms->position); |
| 148 |
*ms->ptr= ms->contents; |
| 149 |
*ms->sizeloc= ms->size; assert(ms->size < ms->capacity); |
| 150 |
ms->contents[ms->size]= 0; |
| 151 |
free(ms); |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
FILE *open_memstream(char **ptr, size_t *sizeloc) |
| 156 |
{ |
| 157 |
if (ptr && sizeloc) { |
| 158 |
struct memstream *ms= calloc(1, sizeof(struct memstream)); |
| 159 |
FILE *fp= 0; if (!ms) return 0; /* errno == ENOMEM */ |
| 160 |
ms->position= ms->size= 0; |
| 161 |
ms->capacity= 4096; |
| 162 |
ms->contents= calloc(ms->capacity, 1); if (!ms->contents) { free(ms); return 0; } /* errno == ENOMEM */ |
| 163 |
ms->ptr= ptr; |
| 164 |
ms->sizeloc= sizeloc; |
| 165 |
memstream_print(ms); |
| 166 |
fp= funopen(ms, memstream_read, memstream_write, memstream_seek, memstream_close); |
| 167 |
if (!fp) { |
| 168 |
free(ms->contents); |
| 169 |
free(ms); |
| 170 |
return 0; /* errno set by funopen */ |
| 171 |
} |
| 172 |
*ptr= ms->contents; |
| 173 |
*sizeloc= ms->size; |
| 174 |
return fp; |
| 175 |
} |
| 176 |
errno= EINVAL; |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
#endif /* __MP_LEGACY_SUPPORT_OPEN_MEMSTREAM__ */ |