]>
Commit | Line | Data |
---|---|---|
fdb7d390 | 1 | /* fmemopen implementation. |
f7a9f785 | 2 | Copyright (C) 2015-2016 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 | |
34 | typedef struct fmemopen_cookie_struct fmemopen_cookie_t; | |
35 | struct 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 | 46 | static ssize_t |
46e4bd3b UD |
47 | fmemopen_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 | { |
fdb7d390 | 53 | if ((size_t) c->pos == c->maxpos) |
de153e7f | 54 | return 0; |
46e4bd3b UD |
55 | s = c->size - c->pos; |
56 | } | |
57 | ||
58 | memcpy (b, &(c->buffer[c->pos]), s); | |
59 | ||
60 | c->pos += s; | |
6dd67bd5 | 61 | if ((size_t) c->pos > c->maxpos) |
46e4bd3b UD |
62 | c->maxpos = c->pos; |
63 | ||
64 | return s; | |
65 | } | |
66 | ||
67 | ||
1a511d31 | 68 | static ssize_t |
46e4bd3b UD |
69 | fmemopen_write (void *cookie, const char *b, size_t s) |
70 | { | |
fdb7d390 AZ |
71 | fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie;; |
72 | _IO_off64_t pos = c->append ? c->maxpos : c->pos; | |
46e4bd3b UD |
73 | int addnullc; |
74 | ||
fdb7d390 | 75 | addnullc = (s == 0 || b[s - 1] != '\0'); |
46e4bd3b | 76 | |
fdb7d390 | 77 | if (pos + s + addnullc > c->size) |
46e4bd3b | 78 | { |
7c2ce714 | 79 | if ((size_t) (c->pos + addnullc) >= c->size) |
de153e7f UD |
80 | { |
81 | __set_errno (ENOSPC); | |
32053042 | 82 | return 0; |
de153e7f | 83 | } |
fdb7d390 | 84 | s = c->size - pos - addnullc; |
46e4bd3b UD |
85 | } |
86 | ||
fdb7d390 | 87 | memcpy (&(c->buffer[pos]), b, s); |
46e4bd3b | 88 | |
fdb7d390 AZ |
89 | pos += s; |
90 | if ((size_t) pos > c->maxpos) | |
46e4bd3b | 91 | { |
fdb7d390 | 92 | c->maxpos = pos; |
46e4bd3b UD |
93 | if (addnullc) |
94 | c->buffer[c->maxpos] = '\0'; | |
95 | } | |
96 | ||
97 | return s; | |
98 | } | |
99 | ||
100 | ||
1a511d31 | 101 | static int |
de153e7f | 102 | fmemopen_seek (void *cookie, _IO_off64_t *p, int w) |
46e4bd3b UD |
103 | { |
104 | _IO_off64_t np; | |
fdb7d390 | 105 | fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie; |
46e4bd3b UD |
106 | |
107 | switch (w) | |
108 | { | |
46e4bd3b UD |
109 | case SEEK_SET: |
110 | np = *p; | |
111 | break; | |
112 | ||
113 | case SEEK_CUR: | |
114 | np = c->pos + *p; | |
115 | break; | |
116 | ||
117 | case SEEK_END: | |
fdb7d390 | 118 | np = c->maxpos + *p; |
46e4bd3b UD |
119 | break; |
120 | ||
c6cc2207 UD |
121 | default: |
122 | return -1; | |
46e4bd3b UD |
123 | } |
124 | ||
6dd67bd5 | 125 | if (np < 0 || (size_t) np > c->size) |
46e4bd3b UD |
126 | return -1; |
127 | ||
1e7cceb9 | 128 | *p = c->pos = np; |
46e4bd3b | 129 | |
1e7cceb9 | 130 | return 0; |
46e4bd3b UD |
131 | } |
132 | ||
133 | ||
1a511d31 | 134 | static int |
46e4bd3b UD |
135 | fmemopen_close (void *cookie) |
136 | { | |
fdb7d390 | 137 | fmemopen_cookie_t *c = (fmemopen_cookie_t *) cookie; |
46e4bd3b | 138 | |
3146ad42 AJ |
139 | if (c->mybuffer) |
140 | free (c->buffer); | |
46e4bd3b UD |
141 | free (c); |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | ||
147 | FILE * | |
fdb7d390 | 148 | __fmemopen (void *buf, size_t len, const char *mode) |
46e4bd3b UD |
149 | { |
150 | cookie_io_functions_t iof; | |
151 | fmemopen_cookie_t *c; | |
63e952d9 | 152 | FILE *result; |
46e4bd3b | 153 | |
787813b1 | 154 | c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1); |
46e4bd3b UD |
155 | if (c == NULL) |
156 | return NULL; | |
157 | ||
158 | c->mybuffer = (buf == NULL); | |
159 | ||
160 | if (c->mybuffer) | |
161 | { | |
162 | c->buffer = (char *) malloc (len); | |
163 | if (c->buffer == NULL) | |
164 | { | |
165 | free (c); | |
166 | return NULL; | |
167 | } | |
168 | c->buffer[0] = '\0'; | |
169 | } | |
170 | else | |
1b99d99f | 171 | { |
a1ffb40e | 172 | if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf)) |
0292b0dd UD |
173 | { |
174 | free (c); | |
fdb7d390 AZ |
175 | __set_errno (EINVAL); |
176 | return NULL; | |
0292b0dd | 177 | } |
1b99d99f UD |
178 | |
179 | c->buffer = buf; | |
46e4bd3b | 180 | |
fdb7d390 AZ |
181 | /* POSIX states that w+ mode should truncate the buffer. */ |
182 | if (mode[0] == 'w' && mode[1] == '+') | |
13a804de | 183 | c->buffer[0] = '\0'; |
46e4bd3b | 184 | |
787813b1 AZ |
185 | if (mode[0] == 'a') |
186 | c->maxpos = strnlen (c->buffer, len); | |
13a804de | 187 | } |
46e4bd3b | 188 | |
fdb7d390 AZ |
189 | |
190 | /* Mode | starting position (cookie::pos) | size (cookie::size) | |
191 | ------ |----------------------------------|----------------------------- | |
192 | read | beginning of the buffer | size argument | |
193 | write | beginning of the buffer | zero | |
194 | append | first null or size buffer + 1 | first null or size argument | |
195 | */ | |
196 | ||
13a804de | 197 | c->size = len; |
46e4bd3b | 198 | |
fdb7d390 AZ |
199 | if (mode[0] == 'r') |
200 | c->maxpos = len; | |
201 | ||
202 | c->append = mode[0] == 'a'; | |
203 | if (c->append) | |
c6cc2207 | 204 | c->pos = c->maxpos; |
46e4bd3b | 205 | else |
c6cc2207 | 206 | c->pos = 0; |
46e4bd3b UD |
207 | |
208 | iof.read = fmemopen_read; | |
209 | iof.write = fmemopen_write; | |
210 | iof.seek = fmemopen_seek; | |
211 | iof.close = fmemopen_close; | |
212 | ||
63e952d9 PP |
213 | result = _IO_fopencookie (c, mode, iof); |
214 | if (__glibc_unlikely (result == NULL)) | |
215 | { | |
216 | if (c->mybuffer) | |
217 | free (c->buffer); | |
218 | ||
219 | free (c); | |
220 | } | |
221 | ||
222 | return result; | |
46e4bd3b | 223 | } |
fdb7d390 AZ |
224 | libc_hidden_def (__fmemopen) |
225 | versioned_symbol (libc, __fmemopen, fmemopen, GLIBC_2_22); |