oldfmemopen.c 6.0 KB

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