summaryrefslogtreecommitdiffstats
path: root/cpukit/base64
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--cpukit/base64/base64-decode.c165
-rw-r--r--cpukit/base64/base64-encode.c121
2 files changed, 286 insertions, 0 deletions
diff --git a/cpukit/base64/base64-decode.c b/cpukit/base64/base64-decode.c
new file mode 100644
index 0000000000..5739266528
--- /dev/null
+++ b/cpukit/base64/base64-decode.c
@@ -0,0 +1,165 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ *
+ * @ingroup RTEMSImplBase64
+ *
+ * @brief This source file contains the implementation of
+ * _Base64_Decode_initialize() and _Base64_Decode().
+ */
+
+/*
+ * Copyright (C) 2023 embedded brains GmbH & Co. KG
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <rtems/base64.h>
+
+#define SPACE 253
+
+#define PAD 254
+
+#define INVALID 255
+
+const uint8_t _Base64_Decoding[128] = {
+ ['A'] = 0, ['B'] = 1, ['C'] = 2, ['D'] = 3,
+ ['E'] = 4, ['F'] = 5, ['G'] = 6, ['H'] = 7,
+ ['I'] = 8, ['J'] = 9, ['K'] = 10, ['L'] = 11,
+ ['M'] = 12, ['N'] = 13, ['O'] = 14, ['P'] = 15,
+ ['Q'] = 16, ['R'] = 17, ['S'] = 18, ['T'] = 19,
+ ['U'] = 20, ['V'] = 21, ['W'] = 22, ['X'] = 23,
+ ['Y'] = 24, ['Z'] = 25, ['a'] = 26, ['b'] = 27,
+ ['c'] = 28, ['d'] = 29, ['e'] = 30, ['f'] = 31,
+ ['g'] = 32, ['h'] = 33, ['i'] = 34, ['j'] = 35,
+ ['k'] = 36, ['l'] = 37, ['m'] = 38, ['n'] = 39,
+ ['o'] = 40, ['p'] = 41, ['q'] = 42, ['r'] = 43,
+ ['s'] = 44, ['t'] = 45, ['u'] = 46, ['v'] = 47,
+ ['w'] = 48, ['x'] = 49, ['y'] = 50, ['z'] = 51,
+ ['0'] = 52, ['1'] = 53, ['2'] = 54, ['3'] = 55,
+ ['4'] = 56, ['5'] = 57, ['6'] = 58, ['7'] = 59,
+ ['8'] = 60, ['9'] = 61, ['+'] = 62, ['-'] = 62,
+ ['/'] = 63, ['_'] = 63, ['='] = PAD, [' '] = SPACE,
+ ['\t'] = SPACE, ['\n'] = SPACE, ['\v'] = SPACE, ['\f'] = SPACE,
+ ['\r'] = SPACE, [0] = INVALID, [1] = INVALID, [2] = INVALID,
+ [3] = INVALID, [4] = INVALID, [5] = INVALID, [6] = INVALID,
+ [7] = INVALID, [8] = INVALID, [14] = INVALID, [15] = INVALID,
+ [16] = INVALID, [17] = INVALID, [18] = INVALID, [19] = INVALID,
+ [20] = INVALID, [21] = INVALID, [22] = INVALID, [23] = INVALID,
+ [24] = INVALID, [25] = INVALID, [26] = INVALID, [27] = INVALID,
+ [28] = INVALID, [29] = INVALID, [30] = INVALID, [31] = INVALID,
+ [33] = INVALID, [34] = INVALID, [35] = INVALID, [36] = INVALID,
+ [37] = INVALID, [38] = INVALID, [39] = INVALID, [40] = INVALID,
+ [41] = INVALID, [42] = INVALID, [44] = INVALID, [46] = INVALID,
+ [58] = INVALID, [59] = INVALID, [60] = INVALID, [62] = INVALID,
+ [63] = INVALID, [64] = INVALID, [91] = INVALID, [92] = INVALID,
+ [93] = INVALID, [94] = INVALID, [96] = INVALID, [123] = INVALID,
+ [124] = INVALID, [125] = INVALID, [126] = INVALID, [127] = INVALID};
+
+void _Base64_Decode_initialize(Base64_Decode_control* self,
+ uint8_t* target,
+ size_t target_size) {
+ self->state = BASE64_DECODE_STATE_0;
+ self->target = target;
+ self->target_end = target + target_size;
+}
+
+Base64_Decode_status _Base64_Decode(Base64_Decode_control* self, char ch) {
+ uint8_t decoded_ch;
+ uint8_t next_ch;
+ uint8_t* target;
+ const uint8_t* target_end;
+ Base64_Decode_state next_state;
+
+ if ((unsigned char)ch >= 128) {
+ return BASE64_DECODE_INVALID_INPUT;
+ }
+
+ decoded_ch = _Base64_Decoding[(unsigned char)ch];
+
+ if (decoded_ch == SPACE) {
+ return BASE64_DECODE_SUCCESS;
+ }
+
+ target = self->target;
+
+ if (decoded_ch == PAD) {
+ self->target_end = target;
+ return BASE64_DECODE_SUCCESS;
+ }
+
+ if (decoded_ch == INVALID) {
+ return BASE64_DECODE_INVALID_INPUT;
+ }
+
+ target_end = self->target_end;
+
+ if (target == target_end) {
+ return BASE64_DECODE_OVERFLOW;
+ }
+
+ switch (self->state) {
+ case BASE64_DECODE_STATE_0:
+ *target = decoded_ch << 2;
+ next_state = BASE64_DECODE_STATE_1;
+ break;
+
+ case BASE64_DECODE_STATE_1:
+ *target |= decoded_ch >> 4;
+ next_ch = (decoded_ch & 0x0fU) << 4;
+ ++target;
+
+ if (target != target_end) {
+ *target = next_ch;
+ } else if (next_ch != 0) {
+ return BASE64_DECODE_OVERFLOW;
+ }
+
+ next_state = BASE64_DECODE_STATE_2;
+ break;
+
+ case BASE64_DECODE_STATE_2:
+ *target |= decoded_ch >> 2;
+ next_ch = (decoded_ch & 0x03U) << 6;
+ ++target;
+
+ if (target != target_end) {
+ *target = next_ch;
+ } else if (next_ch != 0) {
+ return BASE64_DECODE_OVERFLOW;
+ }
+
+ next_state = BASE64_DECODE_STATE_3;
+ break;
+
+ default: /* BASE64_DECODE_STATE_3 */
+ *target |= decoded_ch;
+ ++target;
+ next_state = BASE64_DECODE_STATE_0;
+ break;
+ }
+
+ self->state = next_state;
+ self->target = target;
+ return BASE64_DECODE_SUCCESS;
+}
diff --git a/cpukit/base64/base64-encode.c b/cpukit/base64/base64-encode.c
new file mode 100644
index 0000000000..0110f3ff67
--- /dev/null
+++ b/cpukit/base64/base64-encode.c
@@ -0,0 +1,121 @@
+/* SPDX-License-Identifier: ISC */
+
+/**
+ * @file
+ *
+ * @ingroup RTEMSImplBase64
+ *
+ * @brief This source file contains the implementation of
+ * _Base64_Encode() and _Base64url_Encode().
+ */
+
+/*
+ * Copyright (C) 2020, 2024 embedded brains GmbH & Co. KG
+ * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+ * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <rtems/base64.h>
+
+static void
+_Base64_Put(int c, void *arg, IO_Put_char put_char)
+{
+ (*put_char)(c, arg);
+}
+
+static int
+_Base64_Do_encode(IO_Put_char put_char, void *arg, const void *src,
+ size_t srclen, const char *wordbreak, int wordlen, const uint8_t *encoding)
+{
+ unsigned int loops = 0;
+ const unsigned char *in = src;
+ int out = 0;
+
+ if (wordlen < 4) {
+ wordlen = 4;
+ }
+
+ while (srclen > 2) {
+ _Base64_Put(encoding[(in[0]>>2)&0x3f], arg, put_char);
+ _Base64_Put(encoding[((in[0]<<4)&0x30)|
+ ((in[1]>>4)&0x0f)], arg, put_char);
+ _Base64_Put(encoding[((in[1]<<2)&0x3c)|
+ ((in[2]>>6)&0x03)], arg, put_char);
+ _Base64_Put(encoding[in[2]&0x3f], arg, put_char);
+ in += 3;
+ srclen -= 3;
+ out += 4;
+
+ loops++;
+ if (srclen != 0 &&
+ (int)((loops + 1) * 4) >= wordlen)
+ {
+ const char *w = wordbreak;
+ loops = 0;
+ while (*w != '\0') {
+ _Base64_Put(*w, arg, put_char);
+ ++w;
+ ++out;
+ }
+ }
+ }
+ if (srclen == 2) {
+ _Base64_Put(encoding[(in[0]>>2)&0x3f], arg, put_char);
+ _Base64_Put(encoding[((in[0]<<4)&0x30)|
+ ((in[1]>>4)&0x0f)], arg, put_char);
+ _Base64_Put(encoding[((in[1]<<2)&0x3c)], arg, put_char);
+ _Base64_Put('=', arg, put_char);
+ out += 4;
+ } else if (srclen == 1) {
+ _Base64_Put(encoding[(in[0]>>2)&0x3f], arg, put_char);
+ _Base64_Put(encoding[((in[0]<<4)&0x30)], arg, put_char);
+ _Base64_Put('=', arg, put_char);
+ _Base64_Put('=', arg, put_char);
+ out += 4;
+ }
+ return out;
+}
+
+const uint8_t _Base64_Encoding[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
+_Base64_Encode(IO_Put_char put_char, void *arg, const void *src, size_t srclen,
+ const char *wordbreak, int wordlen)
+{
+ return _Base64_Do_encode(put_char, arg, src, srclen, wordbreak,
+ wordlen, _Base64_Encoding);
+}
+
+const uint8_t _Base64url_Encoding[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
+_Base64url_Encode(IO_Put_char put_char, void *arg, const void *src,
+ size_t srclen, const char *wordbreak, int wordlen)
+{
+ return _Base64_Do_encode(put_char, arg, src, srclen, wordbreak,
+ wordlen, _Base64url_Encoding);
+}