buffer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2021 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/buffer.h>
  19. #include <grub/err.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/safemath.h>
  23. #include <grub/types.h>
  24. grub_buffer_t
  25. grub_buffer_new (grub_size_t sz)
  26. {
  27. struct grub_buffer *ret;
  28. ret = (struct grub_buffer *) grub_malloc (sizeof (*ret));
  29. if (ret == NULL)
  30. return NULL;
  31. ret->data = (grub_uint8_t *) grub_malloc (sz);
  32. if (ret->data == NULL)
  33. {
  34. grub_free (ret);
  35. return NULL;
  36. }
  37. ret->sz = sz;
  38. ret->pos = 0;
  39. ret->used = 0;
  40. return ret;
  41. }
  42. void
  43. grub_buffer_free (grub_buffer_t buf)
  44. {
  45. if (buf != NULL)
  46. {
  47. grub_free (buf->data);
  48. grub_free (buf);
  49. }
  50. }
  51. grub_err_t
  52. grub_buffer_ensure_space (grub_buffer_t buf, grub_size_t req)
  53. {
  54. grub_uint8_t *d;
  55. grub_size_t newsz = 1;
  56. /* Is the current buffer size adequate? */
  57. if (buf->sz >= req)
  58. return GRUB_ERR_NONE;
  59. /* Find the smallest power-of-2 size that satisfies the request. */
  60. while (newsz < req)
  61. {
  62. if (newsz == 0)
  63. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  64. N_("requested buffer size is too large"));
  65. newsz <<= 1;
  66. }
  67. d = (grub_uint8_t *) grub_realloc (buf->data, newsz);
  68. if (d == NULL)
  69. return grub_errno;
  70. buf->data = d;
  71. buf->sz = newsz;
  72. return GRUB_ERR_NONE;
  73. }
  74. void *
  75. grub_buffer_take_data (grub_buffer_t buf)
  76. {
  77. void *data = buf->data;
  78. buf->data = NULL;
  79. buf->sz = buf->pos = buf->used = 0;
  80. return data;
  81. }
  82. void
  83. grub_buffer_reset (grub_buffer_t buf)
  84. {
  85. buf->pos = buf->used = 0;
  86. }
  87. grub_err_t
  88. grub_buffer_advance_read_pos (grub_buffer_t buf, grub_size_t n)
  89. {
  90. grub_size_t newpos;
  91. if (grub_add (buf->pos, n, &newpos))
  92. return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
  93. if (newpos > buf->used)
  94. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  95. N_("new read is position beyond the end of the written data"));
  96. buf->pos = newpos;
  97. return GRUB_ERR_NONE;
  98. }