aboutsummaryrefslogtreecommitdiff
path: root/stdio-common/printf_buffer_as_file.c
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2022-12-19 18:56:54 +0100
committerFlorian Weimer <fweimer@redhat.com>2022-12-19 18:56:54 +0100
commit659fe9fdd14b0772f4e9722b751b9b010665e053 (patch)
tree3098a69345fbd3474154bbba45e8f21de449f266 /stdio-common/printf_buffer_as_file.c
parentffde06c915d10c0717a0980508ccb28506c6ec63 (diff)
downloadglibc-659fe9fdd14b0772f4e9722b751b9b010665e053.zip
glibc-659fe9fdd14b0772f4e9722b751b9b010665e053.tar.gz
glibc-659fe9fdd14b0772f4e9722b751b9b010665e053.tar.bz2
stdio-common: Introduce buffers for implementing printf
These buffers will eventually be used instead of FILE * objects to implement printf functions. The multibyte buffer is struct __printf_buffer, the wide buffer is struct __wprintf_buffer. To enable writing type-generic code, the header files printf_buffer-char.h and printf_buffer-wchar_t.h define the Xprintf macro differently, enabling Xprintf (buffer) to stand for __printf_buffer and __wprintf_buffer as appropriate. For common cases, macros like Xprintf_buffer are provided as a more syntactically convenient shortcut. Buffer-specific flush callbacks are implemented with a switch statement instead of a function pointer, to avoid hardening issues similar to those of libio vtables. struct __printf_buffer_as_file is needed to support custom printf specifiers because the public interface for that requires passing a FILE *, which is why there is a trapdoor back from these buffers to FILE * streams. Since the immediate user of these interfaces knows when processing has finished, there is no flush callback for the end of processing, only a flush callback for the intermediate buffer flush. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'stdio-common/printf_buffer_as_file.c')
-rw-r--r--stdio-common/printf_buffer_as_file.c148
1 files changed, 148 insertions, 0 deletions
diff --git a/stdio-common/printf_buffer_as_file.c b/stdio-common/printf_buffer_as_file.c
new file mode 100644
index 0000000..f27b000
--- /dev/null
+++ b/stdio-common/printf_buffer_as_file.c
@@ -0,0 +1,148 @@
+/* FILE * interface to a struct __printf_buffer. Multibyte version.
+ Copyright (C) 2022 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <printf_buffer_as_file.h>
+
+#include <assert.h>
+#include <printf_buffer.h>
+
+/* Commit the data directly written through the stdio stream. */
+static void
+__printf_buffer_as_file_commit (struct __printf_buffer_as_file *file)
+{
+ /* Check that the write pointers in the file stream are consistent
+ with the next buffer. */
+ assert (file->stream._IO_write_ptr >= file->next->write_ptr);
+ assert (file->stream._IO_write_ptr <= file->next->write_end);
+ assert (file->stream._IO_write_base == file->next->write_base);
+ assert (file->stream._IO_write_end == file->next->write_end);
+
+ file->next->write_ptr = file->stream._IO_write_ptr;
+}
+
+/* Pointer the FILE * write buffer into the active printf_buffer
+ area. */
+static void
+__printf_buffer_as_file_switch_to_buffer (struct __printf_buffer_as_file *file)
+{
+ file->stream._IO_write_base = file->next->write_base;
+ file->stream._IO_write_ptr = file->next->write_ptr;
+ file->stream._IO_write_end = file->next->write_end;
+}
+
+/* Only a small subset of the vtable functions is implemented here,
+ following _IO_obstack_jumps. */
+
+static int
+__printf_buffer_as_file_overflow (FILE *fp, int ch)
+{
+ struct __printf_buffer_as_file *file = (struct __printf_buffer_as_file *) fp;
+
+ __printf_buffer_as_file_commit (file);
+
+ /* EOF means only a flush is requested. */
+ if (ch != EOF)
+ __printf_buffer_putc (file->next, ch);
+
+ /* Ensure that flushing actually produces room. */
+ if (!__printf_buffer_has_failed (file->next)
+ && file->next->write_ptr == file->next->write_end)
+ __printf_buffer_flush (file->next);
+
+ __printf_buffer_as_file_switch_to_buffer (file);
+
+ if (!__printf_buffer_has_failed (file->next))
+ return (unsigned char) ch;
+ else
+ return EOF;
+}
+
+static size_t
+__printf_buffer_as_file_xsputn (FILE *fp, const void *buf, size_t len)
+{
+ struct __printf_buffer_as_file *file = (struct __printf_buffer_as_file *) fp;
+
+ __printf_buffer_as_file_commit (file);
+
+ /* Copy the data. */
+ __printf_buffer_write (file->next, buf, len);
+
+ __printf_buffer_as_file_switch_to_buffer (file);
+
+ if (!__printf_buffer_has_failed (file->next))
+ return len;
+ else
+ /* We may actually have written something. But the stream is
+ corrupted in this case anyway, so try not to divine the write
+ count here. */
+ return 0;
+}
+
+static const struct _IO_jump_t _IO_printf_buffer_as_file_jumps libio_vtable =
+{
+ JUMP_INIT_DUMMY,
+ JUMP_INIT(finish, NULL),
+ JUMP_INIT(overflow, __printf_buffer_as_file_overflow),
+ JUMP_INIT(underflow, NULL),
+ JUMP_INIT(uflow, NULL),
+ JUMP_INIT(pbackfail, NULL),
+ JUMP_INIT(xsputn, __printf_buffer_as_file_xsputn),
+ JUMP_INIT(xsgetn, NULL),
+ JUMP_INIT(seekoff, NULL),
+ JUMP_INIT(seekpos, NULL),
+ JUMP_INIT(setbuf, NULL),
+ JUMP_INIT(sync, NULL),
+ JUMP_INIT(doallocate, NULL),
+ JUMP_INIT(read, NULL),
+ JUMP_INIT(write, NULL),
+ JUMP_INIT(seek, NULL),
+ JUMP_INIT(close, NULL),
+ JUMP_INIT(stat, NULL),
+ JUMP_INIT(showmanyc, NULL),
+ JUMP_INIT(imbue, NULL)
+};
+
+void
+__printf_buffer_as_file_init (struct __printf_buffer_as_file *file,
+ struct __printf_buffer *next)
+{
+ file->stream._lock = NULL;
+ _IO_no_init (&file->stream, _IO_USER_LOCK, -1, NULL, NULL);
+ file->vtable = &_IO_printf_buffer_as_file_jumps;
+
+ /* Set up the write buffer from the next buffer. */
+ file->next = next;
+ __printf_buffer_as_file_switch_to_buffer (file);
+
+ /* Mark the read area as inactive, by making all pointers equal. */
+ file->stream._IO_read_base = file->stream._IO_write_base;
+ file->stream._IO_read_ptr = file->stream._IO_write_base;
+ file->stream._IO_read_end = file->stream._IO_write_base;
+}
+
+bool
+__printf_buffer_as_file_terminate (struct __printf_buffer_as_file *file)
+{
+ if (file->stream._flags & _IO_ERR_SEEN)
+ return false;
+ else
+ {
+ __printf_buffer_as_file_commit (file);
+ return true;
+ }
+}