Blame SOURCES/0464-kern-buffer-Add-variable-sized-heap-buffer.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Chris Coulson <chris.coulson@canonical.com>
80913e
Date: Thu, 7 Jan 2021 15:15:43 +0000
80913e
Subject: [PATCH] kern/buffer: Add variable sized heap buffer
80913e
80913e
Add a new variable sized heap buffer type (grub_buffer_t) with simple
80913e
operations for appending data, accessing the data and maintaining
80913e
a read cursor.
80913e
80913e
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
---
80913e
 grub-core/Makefile.core.def |   1 +
80913e
 grub-core/kern/buffer.c     | 117 +++++++++++++++++++++++++++++++++++
80913e
 include/grub/buffer.h       | 144 ++++++++++++++++++++++++++++++++++++++++++++
80913e
 3 files changed, 262 insertions(+)
80913e
 create mode 100644 grub-core/kern/buffer.c
80913e
 create mode 100644 include/grub/buffer.h
80913e
80913e
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
80913e
index 02fbecd4b81..612df2e9c40 100644
80913e
--- a/grub-core/Makefile.core.def
80913e
+++ b/grub-core/Makefile.core.def
80913e
@@ -112,6 +112,7 @@ kernel = {
80913e
   arm_efi_startup = kern/arm/efi/startup.S;
80913e
   arm64_efi_startup = kern/arm64/efi/startup.S;
80913e
 
80913e
+  common = kern/buffer.c;
80913e
   common = kern/command.c;
80913e
   common = kern/corecmd.c;
80913e
   common = kern/device.c;
80913e
diff --git a/grub-core/kern/buffer.c b/grub-core/kern/buffer.c
80913e
new file mode 100644
80913e
index 00000000000..9f5f8b86705
80913e
--- /dev/null
80913e
+++ b/grub-core/kern/buffer.c
80913e
@@ -0,0 +1,117 @@
80913e
+/*
80913e
+ *  GRUB  --  GRand Unified Bootloader
80913e
+ *  Copyright (C) 2021  Free Software Foundation, Inc.
80913e
+ *
80913e
+ *  GRUB is free software: you can redistribute it and/or modify
80913e
+ *  it under the terms of the GNU General Public License as published by
80913e
+ *  the Free Software Foundation, either version 3 of the License, or
80913e
+ *  (at your option) any later version.
80913e
+ *
80913e
+ *  GRUB is distributed in the hope that it will be useful,
80913e
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
80913e
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80913e
+ *  GNU General Public License for more details.
80913e
+ *
80913e
+ *  You should have received a copy of the GNU General Public License
80913e
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
80913e
+ */
80913e
+
80913e
+#include <grub/buffer.h>
80913e
+#include <grub/err.h>
80913e
+#include <grub/misc.h>
80913e
+#include <grub/mm.h>
80913e
+#include <grub/safemath.h>
80913e
+#include <grub/types.h>
80913e
+
80913e
+grub_buffer_t
80913e
+grub_buffer_new (grub_size_t sz)
80913e
+{
80913e
+  struct grub_buffer *ret;
80913e
+
80913e
+  ret = (struct grub_buffer *) grub_malloc (sizeof (*ret));
80913e
+  if (ret == NULL)
80913e
+    return NULL;
80913e
+
80913e
+  ret->data = (grub_uint8_t *) grub_malloc (sz);
80913e
+  if (ret->data == NULL)
80913e
+    {
80913e
+      grub_free (ret);
80913e
+      return NULL;
80913e
+    }
80913e
+
80913e
+  ret->sz = sz;
80913e
+  ret->pos = 0;
80913e
+  ret->used = 0;
80913e
+
80913e
+  return ret;
80913e
+}
80913e
+
80913e
+void
80913e
+grub_buffer_free (grub_buffer_t buf)
80913e
+{
80913e
+  grub_free (buf->data);
80913e
+  grub_free (buf);
80913e
+}
80913e
+
80913e
+grub_err_t
80913e
+grub_buffer_ensure_space (grub_buffer_t buf, grub_size_t req)
80913e
+{
80913e
+  grub_uint8_t *d;
80913e
+  grub_size_t newsz = 1;
80913e
+
80913e
+  /* Is the current buffer size adequate? */
80913e
+  if (buf->sz >= req)
80913e
+    return GRUB_ERR_NONE;
80913e
+
80913e
+  /* Find the smallest power-of-2 size that satisfies the request. */
80913e
+  while (newsz < req)
80913e
+    {
80913e
+      if (newsz == 0)
80913e
+	return grub_error (GRUB_ERR_OUT_OF_RANGE,
80913e
+			   N_("requested buffer size is too large"));
80913e
+      newsz <<= 1;
80913e
+    }
80913e
+
80913e
+  d = (grub_uint8_t *) grub_realloc (buf->data, newsz);
80913e
+  if (d == NULL)
80913e
+    return grub_errno;
80913e
+
80913e
+  buf->data = d;
80913e
+  buf->sz = newsz;
80913e
+
80913e
+  return GRUB_ERR_NONE;
80913e
+}
80913e
+
80913e
+void *
80913e
+grub_buffer_take_data (grub_buffer_t buf)
80913e
+{
80913e
+  void *data = buf->data;
80913e
+
80913e
+  buf->data = NULL;
80913e
+  buf->sz = buf->pos = buf->used = 0;
80913e
+
80913e
+  return data;
80913e
+}
80913e
+
80913e
+void
80913e
+grub_buffer_reset (grub_buffer_t buf)
80913e
+{
80913e
+  buf->pos = buf->used = 0;
80913e
+}
80913e
+
80913e
+grub_err_t
80913e
+grub_buffer_advance_read_pos (grub_buffer_t buf, grub_size_t n)
80913e
+{
80913e
+  grub_size_t newpos;
80913e
+
80913e
+  if (grub_add (buf->pos, n, &newpos))
80913e
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
80913e
+
80913e
+  if (newpos > buf->used)
80913e
+    return grub_error (GRUB_ERR_OUT_OF_RANGE,
80913e
+		       N_("new read is position beyond the end of the written data"));
80913e
+
80913e
+  buf->pos = newpos;
80913e
+
80913e
+  return GRUB_ERR_NONE;
80913e
+}
80913e
diff --git a/include/grub/buffer.h b/include/grub/buffer.h
80913e
new file mode 100644
80913e
index 00000000000..f4b10cf2810
80913e
--- /dev/null
80913e
+++ b/include/grub/buffer.h
80913e
@@ -0,0 +1,144 @@
80913e
+/*
80913e
+ *  GRUB  --  GRand Unified Bootloader
80913e
+ *  Copyright (C) 2021  Free Software Foundation, Inc.
80913e
+ *
80913e
+ *  GRUB is free software: you can redistribute it and/or modify
80913e
+ *  it under the terms of the GNU General Public License as published by
80913e
+ *  the Free Software Foundation, either version 3 of the License, or
80913e
+ *  (at your option) any later version.
80913e
+ *
80913e
+ *  GRUB is distributed in the hope that it will be useful,
80913e
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
80913e
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80913e
+ *  GNU General Public License for more details.
80913e
+ *
80913e
+ *  You should have received a copy of the GNU General Public License
80913e
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
80913e
+ */
80913e
+
80913e
+#ifndef GRUB_BUFFER_H
80913e
+#define GRUB_BUFFER_H	1
80913e
+
80913e
+#include <grub/err.h>
80913e
+#include <grub/misc.h>
80913e
+#include <grub/mm.h>
80913e
+#include <grub/safemath.h>
80913e
+#include <grub/types.h>
80913e
+
80913e
+struct grub_buffer
80913e
+{
80913e
+  grub_uint8_t *data;
80913e
+  grub_size_t sz;
80913e
+  grub_size_t pos;
80913e
+  grub_size_t used;
80913e
+};
80913e
+
80913e
+/*
80913e
+ * grub_buffer_t represents a simple variable sized byte buffer with
80913e
+ * read and write cursors. It currently only implements
80913e
+ * functionality required by the only user in GRUB (append byte[s],
80913e
+ * peeking data at a specified position and updating the read cursor.
80913e
+ * Some things that this doesn't do yet are:
80913e
+ * - Reading a portion of the buffer by copying data from the current
80913e
+ *   read position in to a caller supplied destination buffer and then
80913e
+ *   automatically updating the read cursor.
80913e
+ * - Dropping the read part at the start of the buffer when an append
80913e
+ *   requires more space.
80913e
+ */
80913e
+typedef struct grub_buffer *grub_buffer_t;
80913e
+
80913e
+/* Allocate a new buffer with the specified initial size. */
80913e
+extern grub_buffer_t grub_buffer_new (grub_size_t sz);
80913e
+
80913e
+/* Free the buffer and its resources. */
80913e
+extern void grub_buffer_free (grub_buffer_t buf);
80913e
+
80913e
+/* Return the number of unread bytes in this buffer. */
80913e
+static inline grub_size_t
80913e
+grub_buffer_get_unread_bytes (grub_buffer_t buf)
80913e
+{
80913e
+  return buf->used - buf->pos;
80913e
+}
80913e
+
80913e
+/*
80913e
+ * Ensure that the buffer size is at least the requested
80913e
+ * number of bytes.
80913e
+ */
80913e
+extern grub_err_t grub_buffer_ensure_space (grub_buffer_t buf, grub_size_t req);
80913e
+
80913e
+/*
80913e
+ * Append the specified number of bytes from the supplied
80913e
+ * data to the buffer.
80913e
+ */
80913e
+static inline grub_err_t
80913e
+grub_buffer_append_data (grub_buffer_t buf, const void *data, grub_size_t len)
80913e
+{
80913e
+  grub_size_t req;
80913e
+
80913e
+  if (grub_add (buf->used, len, &req))
80913e
+    return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected"));
80913e
+
80913e
+  if (grub_buffer_ensure_space (buf, req) != GRUB_ERR_NONE)
80913e
+    return grub_errno;
80913e
+
80913e
+  grub_memcpy (&buf->data[buf->used], data, len);
80913e
+  buf->used = req;
80913e
+
80913e
+  return GRUB_ERR_NONE;
80913e
+}
80913e
+
80913e
+/* Append the supplied character to the buffer. */
80913e
+static inline grub_err_t
80913e
+grub_buffer_append_char (grub_buffer_t buf, char c)
80913e
+{
80913e
+  return grub_buffer_append_data (buf, &c, 1);
80913e
+}
80913e
+
80913e
+/*
80913e
+ * Forget and return the underlying data buffer. The caller
80913e
+ * becomes the owner of this buffer, and must free it when it
80913e
+ * is no longer required.
80913e
+ */
80913e
+extern void *grub_buffer_take_data (grub_buffer_t buf);
80913e
+
80913e
+/* Reset this buffer. Note that this does not deallocate any resources. */
80913e
+void grub_buffer_reset (grub_buffer_t buf);
80913e
+
80913e
+/*
80913e
+ * Return a pointer to the underlying data buffer at the specified
80913e
+ * offset from the current read position. Note that this pointer may
80913e
+ * become invalid if the buffer is mutated further.
80913e
+ */
80913e
+static inline void *
80913e
+grub_buffer_peek_data_at (grub_buffer_t buf, grub_size_t off)
80913e
+{
80913e
+  if (grub_add (buf->pos, off, &off))
80913e
+    {
80913e
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow is detected."));
80913e
+      return NULL;
80913e
+    }
80913e
+
80913e
+  if (off >= buf->used)
80913e
+    {
80913e
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("peek out of range"));
80913e
+      return NULL;
80913e
+    }
80913e
+
80913e
+  return &buf->data[off];
80913e
+}
80913e
+
80913e
+/*
80913e
+ * Return a pointer to the underlying data buffer at the current
80913e
+ * read position. Note that this pointer may become invalid if the
80913e
+ * buffer is mutated further.
80913e
+ */
80913e
+static inline void *
80913e
+grub_buffer_peek_data (grub_buffer_t buf)
80913e
+{
80913e
+  return grub_buffer_peek_data_at (buf, 0);
80913e
+}
80913e
+
80913e
+/* Advance the read position by the specified number of bytes. */
80913e
+extern grub_err_t grub_buffer_advance_read_pos (grub_buffer_t buf, grub_size_t n);
80913e
+
80913e
+#endif /* GRUB_BUFFER_H */