open_memstream.c 3.9 KB

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