fmemopen.c 4.0 KB

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