open_memstream.c 4.0 KB

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