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