open_memstream.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. typedef struct {
  15. char *buf;
  16. size_t len;
  17. size_t pos;
  18. size_t eof;
  19. char **bufloc;
  20. size_t *sizeloc;
  21. } __oms_cookie;
  22. /* Nothing to do here, as memstreams are write-only. */
  23. /* static ssize_t oms_read(void *cookie, char *buf, size_t bufsize) */
  24. /* { */
  25. /* } */
  26. static ssize_t oms_write(register void *cookie, const char *buf, size_t bufsize)
  27. {
  28. register char *newbuf;
  29. size_t count;
  30. /* Note: we already know bufsize < SSIZE_MAX... */
  31. count = COOKIE->len - COOKIE->pos - 1;
  32. assert(COOKIE->pos < COOKIE->len); /* Always nul-terminate! */
  33. if (bufsize > count) {
  34. newbuf = realloc(COOKIE->buf, COOKIE->len + bufsize - count);
  35. if (newbuf) {
  36. *COOKIE->bufloc = COOKIE->buf = newbuf;
  37. COOKIE->len += (bufsize - count);
  38. } else {
  39. bufsize = count;
  40. if (count == 0) {
  41. __set_errno(EFBIG); /* TODO: check glibc errno setting... */
  42. return -1;
  43. }
  44. }
  45. }
  46. memcpy(COOKIE->buf + COOKIE->pos, buf, bufsize);
  47. COOKIE->pos += bufsize;
  48. if (COOKIE->pos > COOKIE->eof) {
  49. *COOKIE->sizeloc = COOKIE->eof = COOKIE->pos;
  50. COOKIE->buf[COOKIE->eof] = 0; /* Need to nul-terminate. */
  51. }
  52. return bufsize;
  53. }
  54. static int oms_seek(register void *cookie, __offmax_t *pos, int whence)
  55. {
  56. __offmax_t p = *pos;
  57. register char *buf;
  58. size_t leastlen;
  59. /* Note: fseek already checks that whence is legal, so don't check here
  60. * unless debugging. */
  61. assert(((unsigned int) whence) <= 2);
  62. if (whence != SEEK_SET) {
  63. p += (whence == SEEK_CUR) ? COOKIE->pos : /* SEEK_END */ COOKIE->eof;
  64. }
  65. /* Note: glibc only allows seeking in the buffer. We'll actually restrict
  66. * to the data. */
  67. /* Check for offset < 0, offset >= too big (need nul), or overflow... */
  68. if (((uintmax_t) p) >= SIZE_MAX - 1) {
  69. return -1;
  70. }
  71. leastlen = ((size_t) p) + 1; /* New pos + 1 for nul if necessary. */
  72. if (leastlen >= COOKIE->len) { /* Need to grow buffer... */
  73. buf = realloc(COOKIE->buf, leastlen);
  74. if (buf) {
  75. *COOKIE->bufloc = COOKIE->buf = buf;
  76. COOKIE->len = leastlen;
  77. memset(buf + COOKIE->eof, 0, leastlen - COOKIE->eof); /* 0-fill */
  78. } else {
  79. /* TODO: check glibc errno setting... */
  80. return -1;
  81. }
  82. }
  83. *pos = COOKIE->pos = --leastlen;
  84. if (leastlen > COOKIE->eof) {
  85. memset(COOKIE->buf + COOKIE->eof, 0, leastlen - COOKIE->eof);
  86. *COOKIE->sizeloc = COOKIE->eof;
  87. }
  88. return 0;
  89. }
  90. static int oms_close(void *cookie)
  91. {
  92. free(cookie);
  93. return 0;
  94. }
  95. #undef COOKIE
  96. static const cookie_io_functions_t _oms_io_funcs = {
  97. NULL, oms_write, oms_seek, oms_close
  98. };
  99. /* TODO: If we have buffers enabled, it might be worthwile to add a pointer
  100. * to the FILE in the cookie and operate directly on the buffer itself
  101. * (ie replace the FILE buffer with the cookie buffer and update FILE bufstart,
  102. * etc. whenever we seek). */
  103. FILE *open_memstream(char **bufloc, size_t *sizeloc)
  104. {
  105. register __oms_cookie *cookie;
  106. register FILE *fp;
  107. if ((cookie = malloc(sizeof(__oms_cookie))) != NULL) {
  108. if ((cookie->buf = malloc(cookie->len = BUFSIZ)) == NULL) {
  109. goto EXIT_cookie;
  110. }
  111. *cookie->buf = 0; /* Set nul terminator for buffer. */
  112. *(cookie->bufloc = bufloc) = cookie->buf;
  113. *(cookie->sizeloc = sizeloc) = cookie->eof = cookie->pos = 0;
  114. #ifndef __BCC__
  115. fp = fopencookie(cookie, "w", _oms_io_funcs);
  116. #else
  117. fp = fopencookie(cookie, "w", &_oms_io_funcs);
  118. #endif
  119. /* Note: We don't need to worry about locking fp in the thread case
  120. * as the only possible access would be a close or flush with
  121. * nothing currently in the FILE's write buffer. */
  122. if (fp != NULL) {
  123. __STDIO_STREAM_VALIDATE(fp);
  124. return fp;
  125. }
  126. }
  127. free(cookie->buf);
  128. EXIT_cookie:
  129. free(cookie);
  130. return NULL;
  131. }
  132. libc_hidden_def(open_memstream)
  133. #endif