1 1.2 christos /* $NetBSD: open_memstream.c,v 1.2 2024/01/23 15:32:54 christos Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2013 Advanced Computing Technologies LLC 5 1.1 christos * Written by: John H. Baldwin <jhb (at) FreeBSD.org> 6 1.1 christos * All rights reserved. 7 1.1 christos * 8 1.1 christos * Redistribution and use in source and binary forms, with or without 9 1.1 christos * modification, are permitted provided that the following conditions 10 1.1 christos * are met: 11 1.1 christos * 1. Redistributions of source code must retain the above copyright 12 1.1 christos * notice, this list of conditions and the following disclaimer. 13 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 christos * notice, this list of conditions and the following disclaimer in the 15 1.1 christos * documentation and/or other materials provided with the distribution. 16 1.1 christos * 17 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 1.1 christos * SUCH DAMAGE. 28 1.1 christos */ 29 1.1 christos 30 1.1 christos #include <sys/cdefs.h> 31 1.1 christos #if 0 32 1.1 christos __FBSDID("$FreeBSD: head/lib/libc/stdio/open_memstream.c 247411 2013-02-27 19:50:46Z jhb $"); 33 1.1 christos #endif 34 1.2 christos __RCSID("$NetBSD: open_memstream.c,v 1.2 2024/01/23 15:32:54 christos Exp $"); 35 1.1 christos 36 1.1 christos #include "namespace.h" 37 1.1 christos #include <assert.h> 38 1.1 christos #include <errno.h> 39 1.1 christos #include <limits.h> 40 1.1 christos #include <stdio.h> 41 1.1 christos #include <stdlib.h> 42 1.1 christos #include <string.h> 43 1.1 christos #include <wchar.h> 44 1.1 christos 45 1.1 christos #define OFF_MAX LLONG_MAX 46 1.1 christos 47 1.1 christos struct memstream { 48 1.1 christos char **bufp; 49 1.1 christos size_t *sizep; 50 1.1 christos size_t len; 51 1.1 christos size_t offset; 52 1.1 christos }; 53 1.1 christos 54 1.2 christos static __inline size_t 55 1.2 christos off_t_to_size_t(off_t off) 56 1.2 christos { 57 1.2 christos if (off < 0 || off >= SSIZE_MAX) 58 1.2 christos return SSIZE_MAX - 1; 59 1.2 christos return (size_t)off; 60 1.2 christos } 61 1.2 christos 62 1.1 christos static int 63 1.1 christos memstream_grow(struct memstream *ms, off_t newoff) 64 1.1 christos { 65 1.1 christos char *buf; 66 1.1 christos size_t newsize; 67 1.1 christos 68 1.2 christos newsize = off_t_to_size_t(newoff); 69 1.1 christos if (newsize > ms->len) { 70 1.1 christos buf = realloc(*ms->bufp, newsize + 1); 71 1.1 christos if (buf != NULL) { 72 1.1 christos #ifdef DEBUG 73 1.1 christos fprintf(stderr, "MS: %p growing from %zd to %zd\n", 74 1.1 christos ms, ms->len, newsize); 75 1.1 christos #endif 76 1.1 christos memset(buf + ms->len + 1, 0, newsize - ms->len); 77 1.1 christos *ms->bufp = buf; 78 1.1 christos ms->len = newsize; 79 1.1 christos return (1); 80 1.1 christos } 81 1.1 christos return (0); 82 1.1 christos } 83 1.1 christos return (1); 84 1.1 christos } 85 1.1 christos 86 1.1 christos static void 87 1.1 christos memstream_update(struct memstream *ms) 88 1.1 christos { 89 1.1 christos 90 1.1 christos *ms->sizep = ms->len < ms->offset ? ms->len : ms->offset; 91 1.1 christos } 92 1.1 christos 93 1.1 christos static ssize_t 94 1.1 christos memstream_write(void *cookie, const void *buf, size_t len) 95 1.1 christos { 96 1.1 christos struct memstream *ms; 97 1.1 christos size_t tocopy; 98 1.1 christos off_t more; 99 1.1 christos 100 1.1 christos ms = cookie; 101 1.1 christos more = ms->offset; 102 1.1 christos more += len; 103 1.1 christos if (!memstream_grow(ms, more)) 104 1.1 christos return (-1); 105 1.1 christos tocopy = ms->len - ms->offset; 106 1.1 christos if (len < tocopy) 107 1.1 christos tocopy = len; 108 1.1 christos memcpy(*ms->bufp + ms->offset, buf, tocopy); 109 1.1 christos ms->offset += tocopy; 110 1.1 christos memstream_update(ms); 111 1.1 christos #ifdef DEBUG 112 1.1 christos fprintf(stderr, "MS: write(%p, %zu) = %zu\n", ms, len, tocopy); 113 1.1 christos #endif 114 1.1 christos return (ssize_t)tocopy; 115 1.1 christos } 116 1.1 christos 117 1.1 christos static off_t 118 1.1 christos memstream_seek(void *cookie, off_t pos, int whence) 119 1.1 christos { 120 1.1 christos struct memstream *ms; 121 1.1 christos #ifdef DEBUG 122 1.1 christos size_t old; 123 1.1 christos #endif 124 1.1 christos 125 1.1 christos ms = cookie; 126 1.1 christos #ifdef DEBUG 127 1.1 christos old = ms->offset; 128 1.1 christos #endif 129 1.1 christos switch (whence) { 130 1.1 christos case SEEK_SET: 131 1.1 christos /* _fseeko() checks for negative offsets. */ 132 1.1 christos assert(pos >= 0); 133 1.2 christos ms->offset = off_t_to_size_t(pos); 134 1.1 christos break; 135 1.1 christos case SEEK_CUR: 136 1.1 christos /* This is only called by _ftello(). */ 137 1.1 christos assert(pos == 0); 138 1.1 christos break; 139 1.1 christos case SEEK_END: 140 1.1 christos if (pos < 0) { 141 1.1 christos if (pos + (ssize_t)ms->len < 0) { 142 1.1 christos #ifdef DEBUG 143 1.1 christos fprintf(stderr, 144 1.1 christos "MS: bad SEEK_END: pos %jd, len %zu\n", 145 1.1 christos (intmax_t)pos, ms->len); 146 1.1 christos #endif 147 1.1 christos errno = EINVAL; 148 1.1 christos return (-1); 149 1.1 christos } 150 1.1 christos } else { 151 1.1 christos if (OFF_MAX - (ssize_t)ms->len < pos) { 152 1.1 christos #ifdef DEBUG 153 1.1 christos fprintf(stderr, 154 1.1 christos "MS: bad SEEK_END: pos %jd, len %zu\n", 155 1.1 christos (intmax_t)pos, ms->len); 156 1.1 christos #endif 157 1.1 christos errno = EOVERFLOW; 158 1.1 christos return (-1); 159 1.1 christos } 160 1.1 christos } 161 1.2 christos ms->offset = off_t_to_size_t(ms->len + pos); 162 1.1 christos break; 163 1.1 christos } 164 1.1 christos memstream_update(ms); 165 1.1 christos #ifdef DEBUG 166 1.1 christos fprintf(stderr, "MS: seek(%p, %jd, %d) %zu -> %zu\n", ms, 167 1.1 christos (intmax_t)pos, whence, old, ms->offset); 168 1.1 christos #endif 169 1.1 christos return (ms->offset); 170 1.1 christos } 171 1.1 christos 172 1.1 christos static int 173 1.1 christos memstream_close(void *cookie) 174 1.1 christos { 175 1.1 christos 176 1.1 christos free(cookie); 177 1.1 christos return (0); 178 1.1 christos } 179 1.1 christos 180 1.1 christos FILE * 181 1.1 christos open_memstream(char **bufp, size_t *sizep) 182 1.1 christos { 183 1.1 christos struct memstream *ms; 184 1.1 christos int save_errno; 185 1.1 christos FILE *fp; 186 1.1 christos 187 1.1 christos if (bufp == NULL || sizep == NULL) { 188 1.1 christos errno = EINVAL; 189 1.1 christos return (NULL); 190 1.1 christos } 191 1.1 christos *bufp = calloc(1, 1); 192 1.1 christos if (*bufp == NULL) 193 1.1 christos return (NULL); 194 1.1 christos ms = malloc(sizeof(*ms)); 195 1.1 christos if (ms == NULL) { 196 1.1 christos save_errno = errno; 197 1.1 christos free(*bufp); 198 1.1 christos *bufp = NULL; 199 1.1 christos errno = save_errno; 200 1.1 christos return (NULL); 201 1.1 christos } 202 1.1 christos ms->bufp = bufp; 203 1.1 christos ms->sizep = sizep; 204 1.1 christos ms->len = 0; 205 1.1 christos ms->offset = 0; 206 1.1 christos memstream_update(ms); 207 1.1 christos fp = funopen2(ms, NULL, memstream_write, memstream_seek, 208 1.1 christos NULL, memstream_close); 209 1.1 christos if (fp == NULL) { 210 1.1 christos save_errno = errno; 211 1.1 christos free(ms); 212 1.1 christos free(*bufp); 213 1.1 christos *bufp = NULL; 214 1.1 christos errno = save_errno; 215 1.1 christos return (NULL); 216 1.1 christos } 217 1.1 christos fwide(fp, -1); 218 1.1 christos return (fp); 219 1.1 christos } 220