open_memstream.c 4.0 KB

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