bufio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* bufio.c - buffered io access */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/err.h>
  20. #include <grub/types.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/fs.h>
  24. #include <grub/bufio.h>
  25. GRUB_EXPORT(grub_bufio_open);
  26. GRUB_EXPORT(grub_buffile_open);
  27. #define GRUB_BUFIO_DEF_SIZE 8192
  28. #define GRUB_BUFIO_MAX_SIZE 1048576
  29. struct grub_bufio
  30. {
  31. grub_file_t file;
  32. grub_size_t block_size;
  33. grub_size_t buffer_len;
  34. char buffer[0];
  35. };
  36. typedef struct grub_bufio *grub_bufio_t;
  37. static struct grub_fs grub_bufio_fs;
  38. grub_file_t
  39. grub_bufio_open (grub_file_t io, int size)
  40. {
  41. grub_file_t file;
  42. grub_bufio_t bufio = 0;
  43. file = (grub_file_t) grub_malloc (sizeof (*file));
  44. if (! file)
  45. return 0;
  46. if (size == 0)
  47. size = GRUB_BUFIO_DEF_SIZE;
  48. else if (size > GRUB_BUFIO_MAX_SIZE)
  49. size = GRUB_BUFIO_MAX_SIZE;
  50. if ((size < 0) || ((unsigned) size > io->size))
  51. size = ((io->size > GRUB_BUFIO_MAX_SIZE) ? GRUB_BUFIO_MAX_SIZE :
  52. io->size);
  53. bufio = grub_malloc (sizeof (struct grub_bufio) + size);
  54. if (! bufio)
  55. {
  56. grub_free (file);
  57. return 0;
  58. }
  59. bufio->file = io;
  60. bufio->block_size = size;
  61. bufio->buffer_len = 0;
  62. file->device = io->device;
  63. file->offset = 0;
  64. file->size = io->size;
  65. file->data = bufio;
  66. file->read_hook = 0;
  67. file->fs = &grub_bufio_fs;
  68. return file;
  69. }
  70. grub_file_t
  71. grub_buffile_open (const char *name, int size)
  72. {
  73. grub_file_t io, file;
  74. io = grub_file_open (name);
  75. if (! io)
  76. return 0;
  77. file = grub_bufio_open (io, size);
  78. if (! file)
  79. {
  80. grub_file_close (io);
  81. return 0;
  82. }
  83. return file;
  84. }
  85. static grub_ssize_t
  86. grub_bufio_read (grub_file_t file, char *buf, grub_size_t len)
  87. {
  88. grub_size_t res = len;
  89. grub_bufio_t bufio = file->data;
  90. grub_uint32_t pos;
  91. if ((file->offset >= bufio->file->offset) &&
  92. (file->offset < bufio->file->offset + bufio->buffer_len))
  93. {
  94. grub_size_t n;
  95. pos = file->offset - bufio->file->offset;
  96. n = bufio->buffer_len - pos;
  97. if (n > len)
  98. n = len;
  99. grub_memcpy (buf, &bufio->buffer[pos], n);
  100. len -= n;
  101. if (! len)
  102. return res;
  103. buf += n;
  104. bufio->file->offset += bufio->buffer_len;
  105. pos = 0;
  106. }
  107. else
  108. {
  109. bufio->file->offset = grub_divmod64 (file->offset, bufio->block_size,
  110. &pos);
  111. bufio->file->offset *= bufio->block_size;
  112. }
  113. if (pos + len >= bufio->block_size)
  114. {
  115. if (pos)
  116. {
  117. grub_size_t n;
  118. bufio->file->fs->read (bufio->file, bufio->buffer,
  119. bufio->block_size);
  120. if (grub_errno)
  121. return -1;
  122. n = bufio->block_size - pos;
  123. grub_memcpy (buf, &bufio->buffer[pos], n);
  124. len -= n;
  125. buf += n;
  126. bufio->file->offset += bufio->block_size;
  127. pos = 0;
  128. }
  129. while (len >= bufio->block_size)
  130. {
  131. bufio->file->fs->read (bufio->file, buf, bufio->block_size);
  132. if (grub_errno)
  133. return -1;
  134. len -= bufio->block_size;
  135. buf += bufio->block_size;
  136. bufio->file->offset += bufio->block_size;
  137. }
  138. if (! len)
  139. {
  140. bufio->buffer_len = 0;
  141. return res;
  142. }
  143. }
  144. bufio->buffer_len = bufio->file->size - bufio->file->offset;
  145. if (bufio->buffer_len > bufio->block_size)
  146. bufio->buffer_len = bufio->block_size;
  147. bufio->file->fs->read (bufio->file, bufio->buffer, bufio->buffer_len);
  148. if (grub_errno)
  149. return -1;
  150. grub_memcpy (buf, &bufio->buffer[pos], len);
  151. return res;
  152. }
  153. static grub_err_t
  154. grub_bufio_close (grub_file_t file)
  155. {
  156. grub_bufio_t bufio = file->data;
  157. grub_file_close (bufio->file);
  158. grub_free (bufio);
  159. file->device = 0;
  160. return grub_errno;
  161. }
  162. static struct grub_fs grub_bufio_fs =
  163. {
  164. .name = "bufio",
  165. .dir = 0,
  166. .open = 0,
  167. .read = grub_bufio_read,
  168. .close = grub_bufio_close,
  169. .label = 0,
  170. .next = 0
  171. };