]> git.ipfire.org Git - thirdparty/glibc.git/blame - libio/fmemopen.c
Update copyright dates not handled by scripts/update-copyrights.
[thirdparty/glibc.git] / libio / fmemopen.c
CommitLineData
fdb7d390 1/* fmemopen implementation.
bfff8b1b 2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
46e4bd3b 3 This file is part of the GNU C Library.
46e4bd3b
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
46e4bd3b
UD
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 13 Lesser General Public License for more details.
46e4bd3b 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
46e4bd3b 18
fdb7d390
AZ
19/* fmemopen() from 2.22 and forward works as defined by POSIX. It also
20 provides an older symbol, version 2.2.5, that behaves different regarding
21 SEEK_END (libio/oldfmemopen.c). */
22
46e4bd3b 23
de153e7f 24#include <errno.h>
46e4bd3b
UD
25#include <libio.h>
26#include <stdio.h>
27#include <stdlib.h>
69553a9b 28#include <stdint.h>
46e4bd3b
UD
29#include <string.h>
30#include <sys/types.h>
3ba06713
UD
31#include "libioP.h"
32
46e4bd3b
UD
33
34typedef struct fmemopen_cookie_struct fmemopen_cookie_t;
35struct fmemopen_cookie_struct
36{
fdb7d390
AZ
37 char *buffer; /* memory buffer. */
38 int mybuffer; /* allocated my buffer? */
39 int append; /* buffer open for append? */
40 size_t size; /* buffer length in bytes. */
41 _IO_off64_t pos; /* current position at the buffer. */
42 size_t maxpos; /* max position in buffer. */
46e4bd3b
UD
43};
44
45
1a511d31 46static ssize_t
46e4bd3b
UD
47fmemopen_read (void *cookie, char *b, size_t s)
48{
fdb7d390 49 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
46e4bd3b 50
fdb7d390 51 if (c->pos + s > c->maxpos)
46e4bd3b 52 {
b65b205f
AZ
53 s = c->maxpos - c->pos;
54 if ((size_t) c->pos > c->maxpos)
55 s = 0;
46e4bd3b
UD
56 }
57
58 memcpy (b, &(c->buffer[c->pos]), s);
59
60 c->pos += s;
46e4bd3b
UD
61
62 return s;
63}
64
65
1a511d31 66static ssize_t
46e4bd3b
UD
67fmemopen_write (void *cookie, const char *b, size_t s)
68{
fdb7d390
AZ
69 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;;
70 _IO_off64_t pos = c->append ? c->maxpos : c->pos;
b65b205f 71 int addnullc = (s == 0 || b[s - 1] != '\0');
46e4bd3b 72
b65b205f 73 if (pos + s > c->size)
46e4bd3b 74 {
7c2ce714 75 if ((size_t) (c->pos + addnullc) >= c->size)
de153e7f
UD
76 {
77 __set_errno (ENOSPC);
32053042 78 return 0;
de153e7f 79 }
b65b205f 80 s = c->size - pos;
46e4bd3b
UD
81 }
82
fdb7d390 83 memcpy (&(c->buffer[pos]), b, s);
46e4bd3b 84
b65b205f 85 c->pos = pos + s;
f9123b50 86 if ((size_t) c->pos > c->maxpos)
46e4bd3b 87 {
f9123b50 88 c->maxpos = c->pos;
b65b205f 89 if (c->maxpos < c->size && addnullc)
46e4bd3b 90 c->buffer[c->maxpos] = '\0';
b65b205f
AZ
91 /* A null byte is written in a stream open for update iff it fits. */
92 else if (c->append == 0 && addnullc != 0)
93 c->buffer[c->size-1] = '\0';
46e4bd3b
UD
94 }
95
96 return s;
97}
98
99
1a511d31 100static int
de153e7f 101fmemopen_seek (void *cookie, _IO_off64_t *p, int w)
46e4bd3b
UD
102{
103 _IO_off64_t np;
fdb7d390 104 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
46e4bd3b
UD
105
106 switch (w)
107 {
46e4bd3b
UD
108 case SEEK_SET:
109 np = *p;
110 break;
111
112 case SEEK_CUR:
113 np = c->pos + *p;
114 break;
115
116 case SEEK_END:
fdb7d390 117 np = c->maxpos + *p;
46e4bd3b
UD
118 break;
119
c6cc2207
UD
120 default:
121 return -1;
46e4bd3b
UD
122 }
123
6dd67bd5 124 if (np < 0 || (size_t) np > c->size)
b65b205f
AZ
125 {
126 __set_errno (EINVAL);
127 return -1;
128 }
46e4bd3b 129
1e7cceb9 130 *p = c->pos = np;
46e4bd3b 131
1e7cceb9 132 return 0;
46e4bd3b
UD
133}
134
135
1a511d31 136static int
46e4bd3b
UD
137fmemopen_close (void *cookie)
138{
fdb7d390 139 fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;
46e4bd3b 140
3146ad42
AJ
141 if (c->mybuffer)
142 free (c->buffer);
46e4bd3b
UD
143 free (c);
144
145 return 0;
146}
147
148
149FILE *
fdb7d390 150__fmemopen (void *buf, size_t len, const char *mode)
46e4bd3b
UD
151{
152 cookie_io_functions_t iof;
153 fmemopen_cookie_t *c;
63e952d9 154 FILE *result;
46e4bd3b 155
787813b1 156 c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1);
46e4bd3b
UD
157 if (c == NULL)
158 return NULL;
159
160 c->mybuffer = (buf == NULL);
161
162 if (c->mybuffer)
163 {
164 c->buffer = (char *) malloc (len);
165 if (c->buffer == NULL)
166 {
167 free (c);
168 return NULL;
169 }
170 c->buffer[0] = '\0';
171 }
172 else
1b99d99f 173 {
a1ffb40e 174 if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf))
0292b0dd
UD
175 {
176 free (c);
fdb7d390
AZ
177 __set_errno (EINVAL);
178 return NULL;
0292b0dd 179 }
1b99d99f
UD
180
181 c->buffer = buf;
46e4bd3b 182
fdb7d390
AZ
183 /* POSIX states that w+ mode should truncate the buffer. */
184 if (mode[0] == 'w' && mode[1] == '+')
13a804de 185 c->buffer[0] = '\0';
46e4bd3b 186
787813b1
AZ
187 if (mode[0] == 'a')
188 c->maxpos = strnlen (c->buffer, len);
13a804de 189 }
46e4bd3b 190
fdb7d390
AZ
191
192 /* Mode | starting position (cookie::pos) | size (cookie::size)
193 ------ |----------------------------------|-----------------------------
194 read | beginning of the buffer | size argument
195 write | beginning of the buffer | zero
196 append | first null or size buffer + 1 | first null or size argument
197 */
198
13a804de 199 c->size = len;
46e4bd3b 200
fdb7d390
AZ
201 if (mode[0] == 'r')
202 c->maxpos = len;
203
204 c->append = mode[0] == 'a';
205 if (c->append)
c6cc2207 206 c->pos = c->maxpos;
46e4bd3b 207 else
c6cc2207 208 c->pos = 0;
46e4bd3b
UD
209
210 iof.read = fmemopen_read;
211 iof.write = fmemopen_write;
212 iof.seek = fmemopen_seek;
213 iof.close = fmemopen_close;
214
63e952d9
PP
215 result = _IO_fopencookie (c, mode, iof);
216 if (__glibc_unlikely (result == NULL))
217 {
218 if (c->mybuffer)
219 free (c->buffer);
220
221 free (c);
222 }
223
224 return result;
46e4bd3b 225}
fdb7d390
AZ
226libc_hidden_def (__fmemopen)
227versioned_symbol (libc, __fmemopen, fmemopen, GLIBC_2_22);