[PATCH 1/4] dev/io: Make base64 encoding tables public

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Jan 15 09:45:59 UTC 2024


This makes them reusable.  Change the character type to uint8_t.
---
 cpukit/dev/iobase64.c         | 24 +++++++++++++++++-------
 cpukit/include/rtems/dev/io.h | 10 ++++++++++
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/cpukit/dev/iobase64.c b/cpukit/dev/iobase64.c
index 0ac70d3ddb..36ca4adea7 100644
--- a/cpukit/dev/iobase64.c
+++ b/cpukit/dev/iobase64.c
@@ -37,7 +37,7 @@ _IO_Put(int c, void *arg, IO_Put_char put_char)
 
 static int
 _IO_Base64_with_encoding(IO_Put_char put_char, void *arg, const void *src,
-    size_t srclen, const char *wordbreak, int wordlen, const char *encoding)
+    size_t srclen, const char *wordbreak, int wordlen, const uint8_t *encoding)
 {
 	unsigned int loops = 0;
 	const unsigned char *in = src;
@@ -88,24 +88,34 @@ _IO_Base64_with_encoding(IO_Put_char put_char, void *arg, const void *src,
 	return out;
 }
 
-static const char base64[] =
-	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+const uint8_t _IO_Base64_table[64] = {
+    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+    'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
+    'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
+    't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
+    '8', '9', '+', '/'
+};
 
 int
 _IO_Base64(IO_Put_char put_char, void *arg, const void *src, size_t srclen,
     const char *wordbreak, int wordlen)
 {
 	return _IO_Base64_with_encoding(put_char, arg, src, srclen, wordbreak,
-	    wordlen, base64);
+	    wordlen, _IO_Base64_table);
 }
 
-static const char base64url[] =
-	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
+const uint8_t _IO_Base64url_table[64] = {
+    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+    'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
+    'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
+    't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
+    '8', '9', '-', '_'
+};
 
 int
 _IO_Base64url(IO_Put_char put_char, void *arg, const void *src, size_t srclen,
     const char *wordbreak, int wordlen)
 {
 	return _IO_Base64_with_encoding(put_char, arg, src, srclen, wordbreak,
-	    wordlen, base64url);
+	    wordlen, _IO_Base64url_table);
 }
diff --git a/cpukit/include/rtems/dev/io.h b/cpukit/include/rtems/dev/io.h
index 93f384a551..efe0a32d0a 100644
--- a/cpukit/include/rtems/dev/io.h
+++ b/cpukit/include/rtems/dev/io.h
@@ -106,6 +106,16 @@ int _IO_Vprintf(
   va_list      ap
 );
 
+/**
+ * @brief Maps a 6-bit integer to the corresponding base64 encoding.
+ */
+extern const uint8_t _IO_Base64_table[ 64 ];
+
+/**
+ * @brief Maps a 6-bit integer to the corresponding base64url encoding.
+ */
+extern const uint8_t _IO_Base64url_table[ 64 ];
+
 /**
  * @brief Outputs the source buffer in base64 encoding.
  *
-- 
2.35.3



More information about the devel mailing list