From a6b36334593a619d7624bb91ccab19192e4f523e Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Thu, 12 Mar 2020 15:43:17 +0100 Subject: score: Add _IO_Base64() Update #3904. --- cpukit/Makefile.am | 1 + cpukit/include/rtems/score/io.h | 27 ++++++++++++- cpukit/score/src/iobase64.c | 82 ++++++++++++++++++++++++++++++++++++++ testsuites/sptests/spprintk/init.c | 56 ++++++++++++++++++++++++++ 4 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 cpukit/score/src/iobase64.c diff --git a/cpukit/Makefile.am b/cpukit/Makefile.am index fcb64cb992..968c52fe44 100644 --- a/cpukit/Makefile.am +++ b/cpukit/Makefile.am @@ -1011,6 +1011,7 @@ librtemscpu_a_SOURCES += score/src/isr.c librtemscpu_a_SOURCES += score/src/wkspace.c librtemscpu_a_SOURCES += score/src/wkspaceisunifieddefault.c librtemscpu_a_SOURCES += score/src/wkstringduplicate.c +librtemscpu_a_SOURCES += score/src/iobase64.c librtemscpu_a_SOURCES += score/src/ioprintf.c librtemscpu_a_SOURCES += score/src/iovprintf.c librtemscpu_a_SOURCES += score/src/isrisinprogress.c diff --git a/cpukit/include/rtems/score/io.h b/cpukit/include/rtems/score/io.h index ae3c57f031..cca559cff0 100644 --- a/cpukit/include/rtems/score/io.h +++ b/cpukit/include/rtems/score/io.h @@ -23,7 +23,7 @@ extern "C" { #endif /* __cplusplus */ -typedef void ( *IO_Put_char )(int c, void *arg); +typedef void ( *IO_Put_char )( int c, void *arg ); int _IO_Printf( IO_Put_char put_char, @@ -39,6 +39,31 @@ int _IO_Vprintf( va_list ap ); +/** + * @brief Outputs the source buffer in base64 encoding. + * + * After word length of output characters produced by the encoding a word break + * is produced. + * + * @param put_char The put character function. + * @param arg The argument passed to the put character function. + * @param src The pointer to the source buffer begin. + * @param srclen The length of the source buffer in bytes. + * @param wordbreak The word break string. + * @param wordlen The word length in bytes. If the word length is less than + * four, then a word length of four will be used. + * + * @return The count of output characters. + */ +int _IO_Base64( + IO_Put_char put_char, + void *arg, + const void *src, + size_t len, + const char *wordbreak, + int wordlen +); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/cpukit/score/src/iobase64.c b/cpukit/score/src/iobase64.c new file mode 100644 index 0000000000..ab0498438d --- /dev/null +++ b/cpukit/score/src/iobase64.c @@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: ISC */ + +/* + * Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de) + * 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 + +static const char base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + +static void _IO_Put(int c, void *arg, IO_Put_char put_char) +{ + (*put_char)(c, arg); +} + +int +_IO_Base64(IO_Put_char put_char, void *arg, const void *src, size_t srclen, + const char *wordbreak, int wordlen) +{ + unsigned int loops = 0; + const unsigned char *in = src; + int out = 0; + + if (wordlen < 4) { + wordlen = 4; + } + + while (srclen > 2) { + _IO_Put(base64[(in[0]>>2)&0x3f], arg, put_char); + _IO_Put(base64[((in[0]<<4)&0x30)| + ((in[1]>>4)&0x0f)], arg, put_char); + _IO_Put(base64[((in[1]<<2)&0x3c)| + ((in[2]>>6)&0x03)], arg, put_char); + _IO_Put(base64[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') { + _IO_Put(*w, arg, put_char); + ++w; + ++out; + } + } + } + if (srclen == 2) { + _IO_Put(base64[(in[0]>>2)&0x3f], arg, put_char); + _IO_Put(base64[((in[0]<<4)&0x30)| + ((in[1]>>4)&0x0f)], arg, put_char); + _IO_Put(base64[((in[1]<<2)&0x3c)], arg, put_char); + _IO_Put('=', arg, put_char); + out += 4; + } else if (srclen == 1) { + _IO_Put(base64[(in[0]>>2)&0x3f], arg, put_char); + _IO_Put(base64[((in[0]<<4)&0x30)], arg, put_char); + _IO_Put('=', arg, put_char); + _IO_Put('=', arg, put_char); + out += 4; + } + return out; +} diff --git a/testsuites/sptests/spprintk/init.c b/testsuites/sptests/spprintk/init.c index 4ef28daf5d..0c0e4c7fb8 100644 --- a/testsuites/sptests/spprintk/init.c +++ b/testsuites/sptests/spprintk/init.c @@ -187,6 +187,61 @@ static void test_io_printf( test_context *ctx ) rtems_test_assert( strcmp( ctx->buf, "161718" ) == 0 ); } +static void test_io_base64( test_context *ctx ) +{ + unsigned char buf[] = "abcdefghi"; + int n; + + clear( ctx ); + n = _IO_Base64( put_char, ctx, buf, 9, "\n", 0 ); + rtems_test_assert( n == 14 ); + rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZ2hp" ) == 0 ); + + clear( ctx ); + n = _IO_Base64( put_char, ctx, buf, 8, "\n", 4 ); + rtems_test_assert( n == 14 ); + rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZ2g=" ) == 0 ); + + clear( ctx ); + n = _IO_Base64( put_char, ctx, buf, 7, "\n", 4 ); + rtems_test_assert( n == 14 ); + rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm\nZw==" ) == 0 ); + + clear( ctx ); + n = _IO_Base64( put_char, ctx, buf, 6, "\n", 4 ); + rtems_test_assert( n == 9 ); + rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGVm" ) == 0 ); + + clear( ctx ); + n = _IO_Base64( put_char, ctx, buf, 5, "\n", 4 ); + rtems_test_assert( n == 9 ); + rtems_test_assert( strcmp( ctx->buf, "YWJj\nZGU=" ) == 0 ); + + clear( ctx ); + n = _IO_Base64( put_char, ctx, buf, 4, "\n", 4 ); + rtems_test_assert( n == 9 ); + rtems_test_assert( strcmp( ctx->buf, "YWJj\nZA==" ) == 0 ); + + clear( ctx ); + n = _IO_Base64( put_char, ctx, buf, 3, "\n", 4 ); + rtems_test_assert( n == 4 ); + rtems_test_assert( strcmp( ctx->buf, "YWJj" ) == 0 ); + + clear( ctx ); + n = _IO_Base64( put_char, ctx, buf, 2, "\n", 4 ); + rtems_test_assert( n == 4 ); + rtems_test_assert( strcmp( ctx->buf, "YWI=" ) == 0 ); + + clear( ctx ); + n = _IO_Base64( put_char, ctx, buf, 1, "\n", 4 ); + rtems_test_assert( n == 4 ); + rtems_test_assert( strcmp( ctx->buf, "YQ==" ) == 0 ); + + clear( ctx ); + n = _IO_Base64( put_char, ctx, buf, 0, "\n", 4 ); + rtems_test_assert( n == 0 ); +} + static rtems_task Init( rtems_task_argument argument ) @@ -201,6 +256,7 @@ static rtems_task Init( do_getchark(); test_io_printf(&test_instance); + test_io_base64(&test_instance); TEST_END(); rtems_test_exit( 0 ); -- cgit v1.2.3