oldfmemopen.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* Fmemopen implementation.
  2. Copyright (C) 2000-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /*
  16. * fmemopen() - "my" version of a string stream
  17. * Hanno Mueller, kontakt@hanno.de
  18. *
  19. *
  20. * I needed fmemopen() for an application that I currently work on,
  21. * but couldn't find it in libio. The following snippet of code is an
  22. * attempt to implement what glibc's documentation describes.
  23. *
  24. *
  25. *
  26. * I already see some potential problems:
  27. *
  28. * - I never used the "original" fmemopen(). I am sure that "my"
  29. * fmemopen() behaves differently than the original version.
  30. *
  31. * - The documentation doesn't say whether a string stream allows
  32. * seeks. I checked the old fmemopen implementation in glibc's stdio
  33. * directory, wasn't quite able to see what is going on in that
  34. * source, but as far as I understand there was no seek there. For
  35. * my application, I needed fseek() and ftell(), so it's here.
  36. *
  37. * - "append" mode and fseek(p, SEEK_END) have two different ideas
  38. * about the "end" of the stream.
  39. *
  40. * As described in the documentation, when opening the file in
  41. * "append" mode, the position pointer will be set to the first null
  42. * character of the string buffer (yet the buffer may already
  43. * contain more data). For fseek(), the last byte of the buffer is
  44. * used as the end of the stream.
  45. *
  46. * - It is unclear to me what the documentation tries to say when it
  47. * explains what happens when you use fmemopen with a NULL
  48. * buffer.
  49. *
  50. * Quote: "fmemopen [then] allocates an array SIZE bytes long. This
  51. * is really only useful if you are going to write things to the
  52. * buffer and then read them back in again."
  53. *
  54. * What does that mean if the original fmemopen() did not allow
  55. * seeking? How do you read what you just wrote without seeking back
  56. * to the beginning of the stream?
  57. *
  58. * - I think there should be a second version of fmemopen() that does
  59. * not add null characters for each write. (At least in my
  60. * application, I am not actually using strings but binary data and
  61. * so I don't need the stream to add null characters on its own.)
  62. */
  63. #include "libioP.h"
  64. #if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_22)
  65. #include <errno.h>
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <stdint.h>
  69. #include <string.h>
  70. #include <sys/types.h>
  71. typedef struct fmemopen_cookie_struct fmemopen_cookie_t;
  72. struct fmemopen_cookie_struct
  73. {
  74. char *buffer;
  75. int mybuffer;
  76. int binmode;
  77. size_t size;
  78. off64_t pos;
  79. size_t maxpos;
  80. };
  81. static ssize_t
  82. fmemopen_read (void *cookie, char *b, size_t s)
  83. {
  84. fmemopen_cookie_t *c;
  85. c = (fmemopen_cookie_t *) cookie;
  86. if (c->pos + s > c->size)
  87. {
  88. if ((size_t) c->pos == c->size)
  89. return 0;
  90. s = c->size - c->pos;
  91. }
  92. memcpy (b, &(c->buffer[c->pos]), s);
  93. c->pos += s;
  94. if ((size_t) c->pos > c->maxpos)
  95. c->maxpos = c->pos;
  96. return s;
  97. }
  98. static ssize_t
  99. fmemopen_write (void *cookie, const char *b, size_t s)
  100. {
  101. fmemopen_cookie_t *c;
  102. int addnullc;
  103. c = (fmemopen_cookie_t *) cookie;
  104. addnullc = c->binmode == 0 && (s == 0 || b[s - 1] != '\0');
  105. if (c->pos + s + addnullc > c->size)
  106. {
  107. if ((size_t) (c->pos + addnullc) >= c->size)
  108. {
  109. __set_errno (ENOSPC);
  110. return 0;
  111. }
  112. s = c->size - c->pos - addnullc;
  113. }
  114. memcpy (&(c->buffer[c->pos]), b, s);
  115. c->pos += s;
  116. if ((size_t) c->pos > c->maxpos)
  117. {
  118. c->maxpos = c->pos;
  119. if (addnullc)
  120. c->buffer[c->maxpos] = '\0';
  121. }
  122. return s;
  123. }
  124. static int
  125. fmemopen_seek (void *cookie, off64_t *p, int w)
  126. {
  127. off64_t np;
  128. fmemopen_cookie_t *c;
  129. c = (fmemopen_cookie_t *) cookie;
  130. switch (w)
  131. {
  132. case SEEK_SET:
  133. np = *p;
  134. break;
  135. case SEEK_CUR:
  136. np = c->pos + *p;
  137. break;
  138. case SEEK_END:
  139. np = (c->binmode ? c->size : c->maxpos) - *p;
  140. break;
  141. default:
  142. return -1;
  143. }
  144. if (np < 0 || (size_t) np > c->size)
  145. return -1;
  146. *p = c->pos = np;
  147. return 0;
  148. }
  149. static int
  150. fmemopen_close (void *cookie)
  151. {
  152. fmemopen_cookie_t *c;
  153. c = (fmemopen_cookie_t *) cookie;
  154. if (c->mybuffer)
  155. free (c->buffer);
  156. free (c);
  157. return 0;
  158. }
  159. FILE *
  160. __old_fmemopen (void *buf, size_t len, const char *mode)
  161. {
  162. cookie_io_functions_t iof;
  163. fmemopen_cookie_t *c;
  164. FILE *result;
  165. if (__glibc_unlikely (len == 0))
  166. {
  167. einval:
  168. __set_errno (EINVAL);
  169. return NULL;
  170. }
  171. c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t));
  172. if (c == NULL)
  173. return NULL;
  174. c->mybuffer = (buf == NULL);
  175. if (c->mybuffer)
  176. {
  177. c->buffer = (char *) malloc (len);
  178. if (c->buffer == NULL)
  179. {
  180. free (c);
  181. return NULL;
  182. }
  183. c->buffer[0] = '\0';
  184. c->maxpos = 0;
  185. }
  186. else
  187. {
  188. if (__glibc_unlikely ((uintptr_t) len > -(uintptr_t) buf))
  189. {
  190. free (c);
  191. goto einval;
  192. }
  193. c->buffer = buf;
  194. if (mode[0] == 'w')
  195. c->buffer[0] = '\0';
  196. c->maxpos = strnlen (c->buffer, len);
  197. }
  198. c->size = len;
  199. if (mode[0] == 'a')
  200. c->pos = c->maxpos;
  201. else
  202. c->pos = 0;
  203. c->binmode = mode[0] != '\0' && mode[1] == 'b';
  204. iof.read = fmemopen_read;
  205. iof.write = fmemopen_write;
  206. iof.seek = fmemopen_seek;
  207. iof.close = fmemopen_close;
  208. result = _IO_fopencookie (c, mode, iof);
  209. if (__glibc_unlikely (result == NULL))
  210. {
  211. if (c->mybuffer)
  212. free (c->buffer);
  213. free (c);
  214. }
  215. return result;
  216. }
  217. compat_symbol (libc, __old_fmemopen, fmemopen, GLIBC_2_2);
  218. #endif