open_memstream.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #include <features.h>
  8. #ifdef __USE_GNU
  9. #include "_stdio.h"
  10. #ifndef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  11. #error no custom streams!
  12. #endif
  13. #define COOKIE ((__oms_cookie *) cookie)
  14. #define MEMSTREAM_BUFSIZ 256
  15. typedef struct {
  16. char *buf;
  17. size_t len;
  18. size_t pos;
  19. size_t eof;
  20. char **bufloc;
  21. size_t *sizeloc;
  22. size_t saved_at; /* Where a data byte was replaced by the */
  23. char saved_ch; /* terminator, to put it back later. */
  24. char have_saved;
  25. } __oms_cookie;
  26. /* POSIX wants the terminator in place and the size updated after every
  27. * fflush(), but a cookie stream has no flush callback: __stdio_wcommit() only
  28. * reaches oms_write() when the FILE buffer holds something. So both are kept
  29. * current at all times instead. Seeking back over existing data would destroy
  30. * the byte at the new position, so that one byte is remembered and restored
  31. * once the position moves on -- glibc writes the terminator from its flush hook
  32. * (_IO_mem_sync) and leaves the data alone, and programs may rely on that. */
  33. static void oms_term(register __oms_cookie *c)
  34. {
  35. if (c->have_saved) {
  36. c->buf[c->saved_at] = c->saved_ch;
  37. c->have_saved = 0;
  38. }
  39. if (c->pos < c->eof) {
  40. c->saved_at = c->pos;
  41. c->saved_ch = c->buf[c->pos];
  42. c->have_saved = 1;
  43. }
  44. c->buf[c->pos] = 0;
  45. *c->sizeloc = c->pos;
  46. }
  47. /* Nothing to do here, as memstreams are write-only. */
  48. /* static ssize_t oms_read(void *cookie, char *buf, size_t bufsize) */
  49. /* { */
  50. /* } */
  51. static ssize_t oms_write(register void *cookie, const char *buf, size_t bufsize)
  52. {
  53. register char *newbuf;
  54. size_t count;
  55. /* Note: we already know bufsize < SSIZE_MAX... */
  56. count = COOKIE->len - COOKIE->pos - 1;
  57. assert(COOKIE->pos < COOKIE->len); /* Always nul-terminate! */
  58. if (bufsize > count) {
  59. newbuf = realloc(COOKIE->buf, COOKIE->len + bufsize - count);
  60. if (newbuf) {
  61. *COOKIE->bufloc = COOKIE->buf = newbuf;
  62. COOKIE->len += (bufsize - count);
  63. } else {
  64. bufsize = count;
  65. if (count == 0) {
  66. __set_errno(EFBIG); /* TODO: check glibc errno setting... */
  67. return -1;
  68. }
  69. }
  70. }
  71. /* bufsize is at least 1 here, so the saved byte -- which sits at pos --
  72. * is about to be overwritten and must not be restored. */
  73. COOKIE->have_saved = 0;
  74. memcpy(COOKIE->buf + COOKIE->pos, buf, bufsize);
  75. COOKIE->pos += bufsize;
  76. if (COOKIE->pos > COOKIE->eof) {
  77. COOKIE->eof = COOKIE->pos; /* Buffer defined this far. */
  78. }
  79. oms_term(COOKIE);
  80. return bufsize;
  81. }
  82. static int oms_seek(register void *cookie, __offmax_t *pos, int whence)
  83. {
  84. __offmax_t p = *pos;
  85. register char *buf;
  86. size_t leastlen;
  87. /* Note: fseek already checks that whence is legal, so don't check here
  88. * unless debugging. */
  89. assert(((unsigned int) whence) <= 2);
  90. /* POSIX leaves it implementation-defined whether SEEK_END refers to the
  91. * buffer length or to the size an fflush() would report; glibc uses the
  92. * latter, which for a write-only stream is the position, so both bases are
  93. * the same here. musl uses the former. */
  94. if (whence != SEEK_SET) {
  95. p += COOKIE->pos;
  96. }
  97. /* Note: glibc only allows seeking in the buffer. We'll actually restrict
  98. * to the data. */
  99. /* Check for offset < 0, offset >= too big (need nul), or overflow... */
  100. if (((uintmax_t) p) >= SIZE_MAX - 1) {
  101. return -1;
  102. }
  103. leastlen = ((size_t) p) + 1; /* New pos + 1 for nul if necessary. */
  104. if (leastlen >= COOKIE->len) { /* Need to grow buffer... */
  105. buf = realloc(COOKIE->buf, leastlen);
  106. if (buf) {
  107. *COOKIE->bufloc = COOKIE->buf = buf;
  108. COOKIE->len = leastlen;
  109. memset(buf + COOKIE->eof, 0, leastlen - COOKIE->eof); /* 0-fill */
  110. } else {
  111. /* TODO: check glibc errno setting... */
  112. return -1;
  113. }
  114. }
  115. *pos = COOKIE->pos = --leastlen;
  116. if (leastlen > COOKIE->eof) { /* Seeked past the end: 0-fill the gap. */
  117. memset(COOKIE->buf + COOKIE->eof, 0, leastlen - COOKIE->eof);
  118. COOKIE->eof = leastlen;
  119. }
  120. oms_term(COOKIE);
  121. return 0;
  122. }
  123. static int oms_close(void *cookie)
  124. {
  125. free(cookie);
  126. return 0;
  127. }
  128. #undef COOKIE
  129. static const cookie_io_functions_t _oms_io_funcs = {
  130. NULL, oms_write, oms_seek, oms_close
  131. };
  132. /* TODO: If we have buffers enabled, it might be worthwile to add a pointer
  133. * to the FILE in the cookie and operate directly on the buffer itself
  134. * (ie replace the FILE buffer with the cookie buffer and update FILE bufstart,
  135. * etc. whenever we seek). */
  136. FILE *open_memstream(char **bufloc, size_t *sizeloc)
  137. {
  138. register __oms_cookie *cookie;
  139. register FILE *fp;
  140. if ((cookie = malloc(sizeof(__oms_cookie))) != NULL) {
  141. if ((cookie->buf = malloc(cookie->len = MEMSTREAM_BUFSIZ)) == NULL) {
  142. goto EXIT_cookie;
  143. }
  144. *cookie->buf = 0; /* Set nul terminator for buffer. */
  145. *(cookie->bufloc = bufloc) = cookie->buf;
  146. *(cookie->sizeloc = sizeloc) = cookie->eof = cookie->pos = 0;
  147. cookie->have_saved = 0;
  148. #ifndef __BCC__
  149. fp = fopencookie(cookie, "w", _oms_io_funcs);
  150. #else
  151. fp = fopencookie(cookie, "w", &_oms_io_funcs);
  152. #endif
  153. /* Note: We don't need to worry about locking fp in the thread case
  154. * as the only possible access would be a close or flush with
  155. * nothing currently in the FILE's write buffer. */
  156. if (fp != NULL) {
  157. __STDIO_STREAM_VALIDATE(fp);
  158. return fp;
  159. }
  160. free(cookie->buf);
  161. }
  162. EXIT_cookie:
  163. free(cookie);
  164. return NULL;
  165. }
  166. libc_hidden_def(open_memstream)
  167. #endif