fmemopen.c 4.0 KB

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