fmemopen.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. libc_hidden_proto(memcpy)
  11. libc_hidden_proto(fopencookie)
  12. #ifndef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  13. #error no custom streams!
  14. #endif
  15. typedef struct {
  16. size_t pos;
  17. size_t len;
  18. size_t eof;
  19. int dynbuf;
  20. unsigned char *buf;
  21. FILE *fp;
  22. } __fmo_cookie;
  23. #define COOKIE ((__fmo_cookie *) cookie)
  24. static ssize_t fmo_read(register void *cookie, char *buf, size_t bufsize)
  25. {
  26. size_t count = COOKIE->len - COOKIE->pos;
  27. /* Note: 0 < bufsize < SSIZE_MAX because of _stdio_READ. */
  28. if (!count) { /* EOF! */
  29. return 0;
  30. }
  31. if (bufsize > count) {
  32. bufsize = count;
  33. }
  34. memcpy(buf, COOKIE->buf + COOKIE->pos, bufsize);
  35. COOKIE->pos += bufsize;
  36. return bufsize;
  37. }
  38. static ssize_t fmo_write(register void *cookie, const char *buf, size_t bufsize)
  39. {
  40. size_t count;
  41. /* Note: bufsize < SSIZE_MAX because of _stdio_WRITE. */
  42. /* If appending, need to seek to end of file!!!! */
  43. if (COOKIE->fp->__modeflags & __FLAG_APPEND) {
  44. COOKIE->pos = COOKIE->eof;
  45. }
  46. count = COOKIE->len - COOKIE->pos;
  47. if (bufsize > count) {
  48. bufsize = count;
  49. if (count == 0) { /* We're at the end of the buffer... */
  50. __set_errno(EFBIG);
  51. return -1;
  52. }
  53. }
  54. memcpy(COOKIE->buf + COOKIE->pos, buf, bufsize);
  55. COOKIE->pos += bufsize;
  56. if (COOKIE->pos > COOKIE->eof) {
  57. COOKIE->eof = COOKIE->pos;
  58. if (bufsize < count) { /* New eof and still room in buffer? */
  59. *(COOKIE->buf + COOKIE->pos) = 0;
  60. }
  61. }
  62. return bufsize;
  63. }
  64. /* glibc doesn't allow seeking, but it has in-buffer seeks... we don't. */
  65. static int fmo_seek(register void *cookie, __offmax_t *pos, int whence)
  66. {
  67. __offmax_t p = *pos;
  68. /* Note: fseek already checks that whence is legal, so don't check here
  69. * unless debugging. */
  70. assert(((unsigned int) whence) <= 2);
  71. if (whence != SEEK_SET) {
  72. p += (whence == SEEK_CUR) ? COOKIE->pos : /* SEEK_END */ COOKIE->eof;
  73. }
  74. /* Note: glibc only allows seeking in the buffer. We'll actually restrict
  75. * to the data. */
  76. /* Check for offset < 0, offset > eof, or offset overflow... */
  77. if (((uintmax_t) p) > COOKIE->eof) {
  78. return -1;
  79. }
  80. COOKIE->pos = *pos = p;
  81. return 0;
  82. }
  83. static int fmo_close(register void *cookie)
  84. {
  85. if (COOKIE->dynbuf) {
  86. free(COOKIE->buf);
  87. }
  88. free(cookie);
  89. return 0;
  90. }
  91. #undef COOKIE
  92. static const cookie_io_functions_t _fmo_io_funcs = {
  93. fmo_read, fmo_write, fmo_seek, fmo_close
  94. };
  95. /* TODO: If we have buffers enabled, it might be worthwile to add a pointer
  96. * to the FILE in the cookie and have read, write, and seek operate directly
  97. * on the buffer itself (ie replace the FILE buffer with the cookie buffer
  98. * and update FILE bufstart, etc. whenever we seek). */
  99. FILE *fmemopen(void *s, size_t len, const char *modes)
  100. {
  101. FILE *fp;
  102. register __fmo_cookie *cookie;
  103. size_t i;
  104. if ((cookie = malloc(sizeof(__fmo_cookie))) != NULL) {
  105. cookie->len = len;
  106. cookie->eof = cookie->pos = 0; /* pos and eof adjusted below. */
  107. cookie->dynbuf = 0;
  108. if (((cookie->buf = s) == NULL) && (len > 0)) {
  109. if ((cookie->buf = malloc(len)) == NULL) {
  110. goto EXIT_cookie;
  111. }
  112. cookie->dynbuf = 1;
  113. *cookie->buf = 0; /* If we're appending, treat as empty file. */
  114. }
  115. #ifndef __BCC__
  116. fp = fopencookie(cookie, modes, _fmo_io_funcs);
  117. #else
  118. fp = fopencookie(cookie, modes, &_fmo_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. cookie->fp = fp;
  125. if (fp->__modeflags & __FLAG_READONLY) {
  126. cookie->eof = len;
  127. }
  128. if ((fp->__modeflags & __FLAG_APPEND) && (len > 0)) {
  129. for (i = 0 ; i < len ; i++) {
  130. if (cookie->buf[i] == 0) {
  131. break;
  132. }
  133. }
  134. cookie->eof = cookie->pos = i; /* Adjust eof and pos. */
  135. }
  136. __STDIO_STREAM_VALIDATE(fp);
  137. return fp;
  138. }
  139. }
  140. if (!s) {
  141. free(cookie->buf);
  142. }
  143. EXIT_cookie:
  144. free(cookie);
  145. return NULL;
  146. }
  147. #endif