fmemopen.c 4.0 KB

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