buffer.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. grub_free (buf->data);
  46. grub_free (buf);
  47. }
  48. grub_err_t
  49. grub_buffer_ensure_space (grub_buffer_t buf, grub_size_t req)
  50. {
  51. grub_uint8_t *d;
  52. grub_size_t newsz = 1;
  53. /* Is the current buffer size adequate? */
  54. if (buf->sz >= req)
  55. return GRUB_ERR_NONE;
  56. /* Find the smallest power-of-2 size that satisfies the request. */
  57. while (newsz < req)
  58. {
  59. if (newsz == 0)
  60. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  61. N_("requested buffer size is too large"));
  62. newsz <<= 1;
  63. }
  64. d = (grub_uint8_t *) grub_realloc (buf->data, newsz);
  65. if (d == NULL)
  66. return grub_errno;
  67. buf->data = d;
  68. buf->sz = newsz;
  69. return GRUB_ERR_NONE;
  70. }
  71. void *
  72. grub_buffer_take_data (grub_buffer_t buf)
  73. {
  74. void *data = buf->data;
  75. buf->data = NULL;
  76. buf->sz = buf->pos = buf->used = 0;
  77. return data;
  78. }
  79. void
  80. grub_buffer_reset (grub_buffer_t buf)
  81. {
  82. buf->pos = buf->used = 0;
  83. }
  84. grub_err_t
  85. grub_buffer_advance_read_pos (grub_buffer_t buf, grub_size_t n)
  86. {
  87. grub_size_t newpos;
  88. if (grub_add (buf->pos, n, &newpos))
  89. return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
  90. if (newpos > buf->used)
  91. return grub_error (GRUB_ERR_OUT_OF_RANGE,
  92. N_("new read is position beyond the end of the written data"));
  93. buf->pos = newpos;
  94. return GRUB_ERR_NONE;
  95. }