1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2013 Hudson River Trading LLC 5 * Written by: John H. Baldwin <jhb@FreeBSD.org> 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 #include "namespace.h" 31 #include <sys/param.h> 32 #include <assert.h> 33 #include <errno.h> 34 #include <limits.h> 35 #include <stdckdint.h> 36 #ifdef DEBUG 37 #include <stdint.h> 38 #endif 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <wchar.h> 43 #include "un-namespace.h" 44 45 /* XXX: There is no FPOS_MAX. This assumes fpos_t is an off_t. */ 46 #define FPOS_MAX OFF_MAX 47 48 struct memstream { 49 char **bufp; 50 size_t *sizep; 51 size_t size; 52 ssize_t len; 53 fpos_t offset; 54 }; 55 56 static int 57 memstream_grow(struct memstream *ms, fpos_t newoff) 58 { 59 char *buf; 60 ssize_t newlen; 61 62 if (newoff < 0 || newoff >= SSIZE_MAX) 63 newlen = SSIZE_MAX - 1; 64 else 65 newlen = newoff; 66 if (newlen > ms->size) { 67 size_t newsize; 68 69 if (ckd_add(&newsize, ms->size, ms->size / 2)) 70 newsize = SSIZE_MAX - 1; 71 72 newsize = MAX(newsize, newlen); 73 74 buf = realloc(*ms->bufp, newsize + 1); 75 if (buf == NULL) 76 return (0); 77 78 #ifdef DEBUG 79 fprintf(stderr, "MS: %p growing from %zd to %zd\n", 80 ms, ms->size, newsize); 81 #endif 82 memset(buf + ms->size + 1, 0, newsize - ms->size); 83 *ms->bufp = buf; 84 ms->size = newsize; 85 } 86 87 if (newlen > ms->len) 88 ms->len = newlen; 89 return (1); 90 } 91 92 static void 93 memstream_update(struct memstream *ms) 94 { 95 96 assert(ms->len >= 0 && ms->offset >= 0); 97 *ms->sizep = ms->len < ms->offset ? ms->len : ms->offset; 98 } 99 100 static int 101 memstream_write(void *cookie, const char *buf, int len) 102 { 103 struct memstream *ms; 104 ssize_t tocopy; 105 106 ms = cookie; 107 if (!memstream_grow(ms, ms->offset + len)) 108 return (-1); 109 tocopy = ms->len - ms->offset; 110 if (len < tocopy) 111 tocopy = len; 112 memcpy(*ms->bufp + ms->offset, buf, tocopy); 113 ms->offset += tocopy; 114 memstream_update(ms); 115 #ifdef DEBUG 116 fprintf(stderr, "MS: write(%p, %d) = %zd\n", ms, len, tocopy); 117 #endif 118 return (tocopy); 119 } 120 121 static fpos_t 122 memstream_seek(void *cookie, fpos_t pos, int whence) 123 { 124 struct memstream *ms; 125 #ifdef DEBUG 126 fpos_t old; 127 #endif 128 129 ms = cookie; 130 #ifdef DEBUG 131 old = ms->offset; 132 #endif 133 switch (whence) { 134 case SEEK_SET: 135 /* _fseeko() checks for negative offsets. */ 136 assert(pos >= 0); 137 ms->offset = pos; 138 break; 139 case SEEK_CUR: 140 /* This is only called by _ftello(). */ 141 assert(pos == 0); 142 break; 143 case SEEK_END: 144 if (pos < 0) { 145 if (pos + ms->len < 0) { 146 #ifdef DEBUG 147 fprintf(stderr, 148 "MS: bad SEEK_END: pos %jd, len %zd\n", 149 (intmax_t)pos, ms->len); 150 #endif 151 errno = EINVAL; 152 return (-1); 153 } 154 } else { 155 if (FPOS_MAX - ms->len < pos) { 156 #ifdef DEBUG 157 fprintf(stderr, 158 "MS: bad SEEK_END: pos %jd, len %zd\n", 159 (intmax_t)pos, ms->len); 160 #endif 161 errno = EOVERFLOW; 162 return (-1); 163 } 164 } 165 ms->offset = ms->len + pos; 166 break; 167 } 168 memstream_update(ms); 169 #ifdef DEBUG 170 fprintf(stderr, "MS: seek(%p, %jd, %d) %jd -> %jd\n", ms, (intmax_t)pos, 171 whence, (intmax_t)old, (intmax_t)ms->offset); 172 #endif 173 return (ms->offset); 174 } 175 176 static int 177 memstream_close(void *cookie) 178 { 179 180 free(cookie); 181 return (0); 182 } 183 184 FILE * 185 open_memstream(char **bufp, size_t *sizep) 186 { 187 struct memstream *ms; 188 int save_errno; 189 FILE *fp; 190 191 if (bufp == NULL || sizep == NULL) { 192 errno = EINVAL; 193 return (NULL); 194 } 195 *bufp = calloc(1, 1); 196 if (*bufp == NULL) 197 return (NULL); 198 ms = malloc(sizeof(*ms)); 199 if (ms == NULL) { 200 save_errno = errno; 201 free(*bufp); 202 *bufp = NULL; 203 errno = save_errno; 204 return (NULL); 205 } 206 ms->bufp = bufp; 207 ms->sizep = sizep; 208 ms->size = 0; 209 ms->len = 0; 210 ms->offset = 0; 211 memstream_update(ms); 212 fp = funopen(ms, NULL, memstream_write, memstream_seek, 213 memstream_close); 214 if (fp == NULL) { 215 save_errno = errno; 216 free(ms); 217 free(*bufp); 218 *bufp = NULL; 219 errno = save_errno; 220 return (NULL); 221 } 222 fwide(fp, -1); 223 return (fp); 224 } 225