]>
Commit | Line | Data |
---|---|---|
fdb7d390 | 1 | /* fmemopen implementation. |
04277e02 | 2 | Copyright (C) 2015-2019 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 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://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 <stdio.h> |
26 | #include <stdlib.h> | |
69553a9b | 27 | #include <stdint.h> |
46e4bd3b UD |
28 | #include <string.h> |
29 | #include <sys/types.h> | |
3ba06713 UD |
30 | #include "libioP.h" |
31 | ||
46e4bd3b UD |
32 | |
33 | typedef struct fmemopen_cookie_struct fmemopen_cookie_t; | |
34 | struct fmemopen_cookie_struct | |
35 | { | |
fdb7d390 AZ |
36 | char *buffer; /* memory buffer. */ |
37 | int mybuffer; /* allocated my buffer? */ | |
38 | int append; /* buffer open for append? */ | |
39 | size_t size; /* buffer length in bytes. */ | |
9964a145 | 40 | off64_t pos; /* current position at the buffer. */ |
fdb7d390 | 41 | size_t maxpos; /* max position in buffer. */ |
46e4bd3b UD |
42 | }; |
43 | ||
44 | ||
1a511d31 | 45 | static ssize_t |
46e4bd3b UD |
46 | fmemopen_read (void *cookie, char *b, size_t s) |
47 | { | |
fdb7d390 | 48 | fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie; |
46e4bd3b | 49 | |
fdb7d390 | 50 | if (c->pos + s > c->maxpos) |
46e4bd3b | 51 | { |
b65b205f AZ |
52 | s = c->maxpos - c->pos; |
53 | if ((size_t) c->pos > c->maxpos) | |
54 | s = 0; | |
46e4bd3b UD |
55 | } |
56 | ||
57 | memcpy (b, &(c->buffer[c->pos]), s); | |
58 | ||
59 | c->pos += s; | |
46e4bd3b UD |
60 | |
61 | return s; | |
62 | } | |
63 | ||
64 | ||
1a511d31 | 65 | static ssize_t |
46e4bd3b UD |
66 | fmemopen_write (void *cookie, const char *b, size_t s) |
67 | { | |
fdb7d390 | 68 | fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;; |
9964a145 | 69 | off64_t pos = c->append ? c->maxpos : c->pos; |
b65b205f | 70 | int addnullc = (s == 0 || b[s - 1] != '\0'); |
46e4bd3b | 71 | |
b65b205f | 72 | if (pos + s > c->size) |
46e4bd3b | 73 | { |
7c2ce714 | 74 | if ((size_t) (c->pos + addnullc) >= c->size) |
de153e7f UD |
75 | { |
76 | __set_errno (ENOSPC); | |
32053042 | 77 | return 0; |
de153e7f | 78 | } |
b65b205f | 79 | s = c->size - pos; |
46e4bd3b UD |
80 | } |
81 | ||
fdb7d390 | 82 | memcpy (&(c->buffer[pos]), b, s); |
46e4bd3b | 83 | |
b65b205f | 84 | c->pos = pos + s; |
f9123b50 | 85 | if ((size_t) c->pos > c->maxpos) |
46e4bd3b | 86 | { |
f9123b50 | 87 | c->maxpos = c->pos; |
b65b205f | 88 | if (c->maxpos < c->size && addnullc) |
46e4bd3b | 89 | c->buffer[c->maxpos] = '\0'; |
b65b205f AZ |
90 | /* A null byte is written in a stream open for update iff it fits. */ |
91 | else if (c->append == 0 && addnullc != 0) | |
92 | c->buffer[c->size-1] = '\0'; | |
46e4bd3b UD |
93 | } |
94 | ||
95 | return s; | |
96 | } | |
97 | ||
98 | ||
1a511d31 | 99 | static int |
9964a145 | 100 | fmemopen_seek (void *cookie, off64_t *p, int w) |
46e4bd3b | 101 | { |
9964a145 | 102 | off64_t np; |
fdb7d390 | 103 | fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie; |
46e4bd3b UD |
104 | |
105 | switch (w) | |
106 | { | |
46e4bd3b UD |
107 | case SEEK_SET: |
108 | np = *p; | |
109 | break; | |
110 | ||
111 | case SEEK_CUR: | |
112 | np = c->pos + *p; | |
113 | break; | |
114 | ||
115 | case SEEK_END: | |
fdb7d390 | 116 | np = c->maxpos + *p; |
46e4bd3b UD |
117 | break; |
118 | ||
c6cc2207 UD |
119 | default: |
120 | return -1; | |
46e4bd3b UD |
121 | } |
122 | ||
6dd67bd5 | 123 | if (np < 0 || (size_t) np > c->size) |
b65b205f AZ |
124 | { |
125 | __set_errno (EINVAL); | |
126 | return -1; | |
127 | } | |
46e4bd3b | 128 | |
1e7cceb9 | 129 | *p = c->pos = np; |
46e4bd3b | 130 | |
1e7cceb9 | 131 | return 0; |
46e4bd3b UD |
132 | } |
133 | ||
134 | ||
1a511d31 | 135 | static int |
46e4bd3b UD |
136 | fmemopen_close (void *cookie) |
137 | { | |
fdb7d390 | 138 | fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie; |
46e4bd3b | 139 | |
3146ad42 AJ |
140 | if (c->mybuffer) |
141 | free (c->buffer); | |
46e4bd3b UD |
142 | free (c); |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
147 | ||
148 | FILE * | |
fdb7d390 | 149 | __fmemopen (void *buf, size_t len, const char *mode) |
46e4bd3b UD |
150 | { |
151 | cookie_io_functions_t iof; | |
152 | fmemopen_cookie_t *c; | |
63e952d9 | 153 | FILE *result; |
46e4bd3b | 154 | |
787813b1 | 155 | c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1); |
46e4bd3b UD |
156 | if (c == NULL) |
157 | return NULL; | |
158 | ||
159 | c->mybuffer = (buf == NULL); | |
160 | ||
161 | if (c->mybuffer) | |
162 | { | |
163 | c->buffer = (char *) malloc (len); | |
164 | if (c->buffer == NULL) | |
165 | { | |
166 | free (c); | |
167 | return NULL; | |
168 | } | |
169 | c->buffer[0] = '\0'; | |
170 | } | |
171 | else | |
1b99d99f | 172 | { |
a1ffb40e | 173 | if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf)) |
0292b0dd UD |
174 | { |
175 | free (c); | |
fdb7d390 AZ |
176 | __set_errno (EINVAL); |
177 | return NULL; | |
0292b0dd | 178 | } |
1b99d99f UD |
179 | |
180 | c->buffer = buf; | |
46e4bd3b | 181 | |
fdb7d390 AZ |
182 | /* POSIX states that w+ mode should truncate the buffer. */ |
183 | if (mode[0] == 'w' && mode[1] == '+') | |
13a804de | 184 | c->buffer[0] = '\0'; |
46e4bd3b | 185 | |
787813b1 AZ |
186 | if (mode[0] == 'a') |
187 | c->maxpos = strnlen (c->buffer, len); | |
13a804de | 188 | } |
46e4bd3b | 189 | |
fdb7d390 AZ |
190 | |
191 | /* Mode | starting position (cookie::pos) | size (cookie::size) | |
192 | ------ |----------------------------------|----------------------------- | |
193 | read | beginning of the buffer | size argument | |
194 | write | beginning of the buffer | zero | |
195 | append | first null or size buffer + 1 | first null or size argument | |
196 | */ | |
197 | ||
13a804de | 198 | c->size = len; |
46e4bd3b | 199 | |
fdb7d390 AZ |
200 | if (mode[0] == 'r') |
201 | c->maxpos = len; | |
202 | ||
203 | c->append = mode[0] == 'a'; | |
204 | if (c->append) | |
c6cc2207 | 205 | c->pos = c->maxpos; |
46e4bd3b | 206 | else |
c6cc2207 | 207 | c->pos = 0; |
46e4bd3b UD |
208 | |
209 | iof.read = fmemopen_read; | |
210 | iof.write = fmemopen_write; | |
211 | iof.seek = fmemopen_seek; | |
212 | iof.close = fmemopen_close; | |
213 | ||
63e952d9 PP |
214 | result = _IO_fopencookie (c, mode, iof); |
215 | if (__glibc_unlikely (result == NULL)) | |
216 | { | |
217 | if (c->mybuffer) | |
218 | free (c->buffer); | |
219 | ||
220 | free (c); | |
221 | } | |
222 | ||
223 | return result; | |
46e4bd3b | 224 | } |
fdb7d390 AZ |
225 | libc_hidden_def (__fmemopen) |
226 | versioned_symbol (libc, __fmemopen, fmemopen, GLIBC_2_22); |