open_memstream.c 4.1 KB

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