base64: Move base64 encoding support

This commit is contained in:
Sebastian Huber
2024-01-25 17:51:06 +01:00
parent 8d22b57969
commit 317df86ba3
10 changed files with 181 additions and 111 deletions

View File

@@ -3,14 +3,14 @@
/**
* @file
*
* @ingroup RTEMSDeviceIO
* @ingroup RTEMSImplBase64
*
* @brief This source file contains the implementation of
* _IO_Base64() and _IO_Base64url().
* _Base64_Encode() and _Base64url_Encode().
*/
/*
* Copyright (C) 2020, 2021 embedded brains GmbH & Co. KG
* 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.
*
@@ -27,16 +27,16 @@
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <rtems/dev/io.h>
#include <rtems/base64.h>
static void
_IO_Put(int c, void *arg, IO_Put_char put_char)
_Base64_Put(int c, void *arg, IO_Put_char put_char)
{
(*put_char)(c, arg);
}
static int
_IO_Base64_with_encoding(IO_Put_char put_char, void *arg, const void *src,
_Base64_Do_encode(IO_Put_char put_char, void *arg, const void *src,
size_t srclen, const char *wordbreak, int wordlen, const char *encoding)
{
unsigned int loops = 0;
@@ -48,12 +48,12 @@ _IO_Base64_with_encoding(IO_Put_char put_char, void *arg, const void *src,
}
while (srclen > 2) {
_IO_Put(encoding[(in[0]>>2)&0x3f], arg, put_char);
_IO_Put(encoding[((in[0]<<4)&0x30)|
_Base64_Put(encoding[(in[0]>>2)&0x3f], arg, put_char);
_Base64_Put(encoding[((in[0]<<4)&0x30)|
((in[1]>>4)&0x0f)], arg, put_char);
_IO_Put(encoding[((in[1]<<2)&0x3c)|
_Base64_Put(encoding[((in[1]<<2)&0x3c)|
((in[2]>>6)&0x03)], arg, put_char);
_IO_Put(encoding[in[2]&0x3f], arg, put_char);
_Base64_Put(encoding[in[2]&0x3f], arg, put_char);
in += 3;
srclen -= 3;
out += 4;
@@ -65,24 +65,24 @@ _IO_Base64_with_encoding(IO_Put_char put_char, void *arg, const void *src,
const char *w = wordbreak;
loops = 0;
while (*w != '\0') {
_IO_Put(*w, arg, put_char);
_Base64_Put(*w, arg, put_char);
++w;
++out;
}
}
}
if (srclen == 2) {
_IO_Put(encoding[(in[0]>>2)&0x3f], arg, put_char);
_IO_Put(encoding[((in[0]<<4)&0x30)|
_Base64_Put(encoding[(in[0]>>2)&0x3f], arg, put_char);
_Base64_Put(encoding[((in[0]<<4)&0x30)|
((in[1]>>4)&0x0f)], arg, put_char);
_IO_Put(encoding[((in[1]<<2)&0x3c)], arg, put_char);
_IO_Put('=', arg, put_char);
_Base64_Put(encoding[((in[1]<<2)&0x3c)], arg, put_char);
_Base64_Put('=', arg, put_char);
out += 4;
} else if (srclen == 1) {
_IO_Put(encoding[(in[0]>>2)&0x3f], arg, put_char);
_IO_Put(encoding[((in[0]<<4)&0x30)], arg, put_char);
_IO_Put('=', arg, put_char);
_IO_Put('=', arg, put_char);
_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;
@@ -92,10 +92,10 @@ static const char base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
int
_IO_Base64(IO_Put_char put_char, void *arg, const void *src, size_t srclen,
_Base64_Encode(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,
return _Base64_Do_encode(put_char, arg, src, srclen, wordbreak,
wordlen, base64);
}
@@ -103,9 +103,9 @@ static const char base64url[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
int
_IO_Base64url(IO_Put_char put_char, void *arg, const void *src, size_t srclen,
const char *wordbreak, int wordlen)
_Base64url_Encode(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,
return _Base64_Do_encode(put_char, arg, src, srclen, wordbreak,
wordlen, base64url);
}

View File

@@ -0,0 +1,125 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/**
* @file
*
* @ingroup RTEMSImplBase64
*
* @brief This header file provides the interfaces of the
* @ref RTEMSImplBase64.
*/
/*
* Copyright (C) 2020, 2024 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.
*/
#ifndef _RTEMS_BASE64_H
#define _RTEMS_BASE64_H
#include <rtems/dev/io.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/**
* @defgroup RTEMSImplBase64 Base64 Encoding and Decoding
*
* @ingroup RTEMSImpl
*
* @brief This group contains support functions for base64 and base64url
* encoding and decoding.
*
* @{
*/
/**
* @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 is the put character function used to output the encoded
* source buffer.
*
* @param arg is the argument passed to the put character function.
*
* @param src is the pointer to the source buffer begin.
*
* @param srclen is the length of the source buffer in bytes.
*
* @param wordbreak is the word break string.
*
* @param wordlen is the word length in bytes. If the word length is less than
* four, then a word length of four will be used.
*
* @return Returns the count of output characters.
*/
int _Base64_Encode(
IO_Put_char put_char,
void *arg,
const void *src,
size_t len,
const char *wordbreak,
int wordlen
);
/**
* @brief Outputs the source buffer in base64url encoding.
*
* After word length of output characters produced by the encoding a word break
* is produced.
*
* @param put_char is the put character function used to output the encoded
* source buffer.
*
* @param arg is the argument passed to the put character function.
*
* @param src is the pointer to the source buffer begin.
*
* @param srclen is the length of the source buffer in bytes.
*
* @param wordbreak is the word break string.
*
* @param wordlen is the word length in bytes. If the word length is less than
* four, then a word length of four will be used.
*
* @return Returns the count of output characters.
*/
int _Base64url_Encode(
IO_Put_char put_char,
void *arg,
const void *src,
size_t len,
const char *wordbreak,
int wordlen
);
/** @} */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _RTEMS_BASE64_H */

View File

@@ -106,68 +106,6 @@ 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 is the put character function used to output the encoded
* source buffer.
*
* @param arg is the argument passed to the put character function.
*
* @param src is the pointer to the source buffer begin.
*
* @param srclen is the length of the source buffer in bytes.
*
* @param wordbreak is the word break string.
*
* @param wordlen is the word length in bytes. If the word length is less than
* four, then a word length of four will be used.
*
* @return Returns 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
);
/**
* @brief Outputs the source buffer in base64url encoding.
*
* After word length of output characters produced by the encoding a word break
* is produced.
*
* @param put_char is the put character function used to output the encoded
* source buffer.
*
* @param arg is the argument passed to the put character function.
*
* @param src is the pointer to the source buffer begin.
*
* @param srclen is the length of the source buffer in bytes.
*
* @param wordbreak is the word break string.
*
* @param wordlen is the word length in bytes. If the word length is less than
* four, then a word length of four will be used.
*
* @return Returns the count of output characters.
*/
int _IO_Base64url(
IO_Put_char put_char,
void *arg,
const void *src,
size_t len,
const char *wordbreak,
int wordlen
);
/**
* @brief Issues a couple of no-operation instructions.
*

View File

@@ -43,6 +43,8 @@
#include <limits.h>
#include <string.h>
#include <rtems/base64.h>
typedef struct {
IO_Put_char put_char;
void *arg;
@@ -77,7 +79,7 @@ static void _Gcov_Base64_encode( int c, void *arg )
if ( index == RTEMS_ARRAY_SIZE( ctx->buf ) - 1 ) {
index = 0;
_IO_Base64(
_Base64_Encode(
_Gcov_Base64_put_char,
ctx,
ctx->buf,
@@ -100,5 +102,5 @@ void _Gcov_Dump_info_base64( IO_Put_char put_char, void *arg )
ctx.put_char = put_char;
ctx.arg = arg;
_Gcov_Dump_info( _Gcov_Base64_encode, &ctx );
_IO_Base64( _Gcov_Base64_put_char, &ctx, ctx.buf, ctx.index, NULL, INT_MAX );
_Base64_Encode( _Gcov_Base64_put_char, &ctx, ctx.buf, ctx.index, NULL, INT_MAX );
}

View File

@@ -35,7 +35,6 @@
*/
#include <rtems/test.h>
#include <rtems/dev/io.h>
#include <limits.h>
@@ -45,6 +44,8 @@
#include <openssl/sha.h>
#endif
#include <rtems/base64.h>
typedef struct {
SHA256_CTX sha256;
T_putchar putchar;
@@ -94,7 +95,7 @@ T_report_hash_sha256_finalize(void)
ctx = &T_report_hash_sha256_instance;
SHA256_Final(hash, &ctx->sha256);
T_printf("Y:ReportHash:SHA256:");
(void)_IO_Base64url(ctx->putchar, ctx->putchar_arg, hash,
(void)_Base64url_Encode(ctx->putchar, ctx->putchar_arg, hash,
sizeof(hash), NULL, INT_MAX);
T_printf("\n");
}

View File

@@ -30,11 +30,12 @@
#endif
#include <rtems/recorddump.h>
#include <rtems/dev/io.h>
#include <limits.h>
#include <string.h>
#include <rtems/base64.h>
typedef struct {
IO_Put_char put_char;
void *arg;
@@ -76,7 +77,7 @@ static void chunk( void *arg, const void *data, size_t length )
if ( index == RTEMS_ARRAY_SIZE( ctx->buf ) - 1 ) {
index = 0;
_IO_Base64(
_Base64_Encode(
put_char,
ctx,
ctx->buf,
@@ -94,7 +95,7 @@ static void chunk( void *arg, const void *data, size_t length )
static void flush( dump_context *ctx )
{
_IO_Base64( put_char, ctx, ctx->buf, ctx->index, NULL, INT_MAX );
_Base64_Encode( put_char, ctx, ctx->buf, ctx->index, NULL, INT_MAX );
}
void rtems_record_dump_base64( IO_Put_char put_char, void *arg )

View File

@@ -30,11 +30,12 @@
#endif
#include <rtems/recorddump.h>
#include <rtems/dev/io.h>
#include <limits.h>
#include <string.h>
#include <rtems/base64.h>
static void *dump_zalloc( void *opaque, unsigned items, unsigned size )
{
rtems_record_dump_base64_zlib_context *ctx;
@@ -96,7 +97,7 @@ static void chunk( void *arg, const void *data, size_t length )
ctx->stream.next_out = &ctx->buf[ 0 ];
ctx->stream.avail_out = sizeof( ctx->buf );
_IO_Base64( put_char, ctx, ctx->buf, sizeof( ctx->buf ), NULL, INT_MAX );
_Base64_Encode( put_char, ctx, ctx->buf, sizeof( ctx->buf ), NULL, INT_MAX );
}
}
}
@@ -115,11 +116,11 @@ static void flush( rtems_record_dump_base64_zlib_context *ctx )
ctx->stream.next_out = &ctx->buf[ 0 ];
ctx->stream.avail_out = sizeof( ctx->buf );
_IO_Base64( put_char, ctx, ctx->buf, sizeof( ctx->buf ), NULL, INT_MAX );
_Base64_Encode( put_char, ctx, ctx->buf, sizeof( ctx->buf ), NULL, INT_MAX );
}
}
_IO_Base64(
_Base64_Encode(
put_char,
ctx,
ctx->buf,

View File

@@ -39,7 +39,7 @@
#include <rtems/score/hash.h>
#include <rtems/score/assert.h>
#include <rtems/dev/io.h>
#include <rtems/base64.h>
#include <limits.h>
@@ -64,7 +64,7 @@ void _Hash_Finalize( Hash_Context *context, Hash_Control *hash )
context->hash = hash;
context->index = 0;
hash->chars[ sizeof( *hash ) - 1 ] = '\0';
n = _IO_Base64url(
n = _Base64url_Encode(
_Hash_Put_char,
context,
digest,

View File

@@ -79,6 +79,7 @@ install:
- destination: ${BSP_INCLUDEDIR}/rtems
source:
- cpukit/include/rtems/assoc.h
- cpukit/include/rtems/base64.h
- cpukit/include/rtems/bdbuf.h
- cpukit/include/rtems/bdpart.h
- cpukit/include/rtems/blkdev.h
@@ -527,6 +528,7 @@ source:
- cpukit/compression/xz/xz_crc32.c
- cpukit/compression/xz/xz_dec_lzma2.c
- cpukit/compression/xz/xz_dec_stream.c
- cpukit/base64/base64-encode.c
- cpukit/dev/flash/flashdev.c
- cpukit/dev/i2c/eeprom.c
- cpukit/dev/i2c/fpga-i2c-slave.c
@@ -539,7 +541,6 @@ source:
- cpukit/dev/i2c/ti-lm25066a.c
- cpukit/dev/i2c/ti-tmp112.c
- cpukit/dev/i2c/xilinx-axi-i2c.c
- cpukit/dev/iobase64.c
- cpukit/dev/ioprintf.c
- cpukit/dev/iorelax.c
- cpukit/dev/iovprintf.c

View File

@@ -32,6 +32,7 @@
#include "config.h"
#endif
#include <rtems/base64.h>
#include <rtems/dev/io.h>
/*
@@ -212,52 +213,52 @@ static void test_io_base64( test_context *ctx )
int n;
clear( ctx );
n = _IO_Base64( put_char, ctx, buf, 9, "\n", 0 );
n = _Base64_Encode( 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 );
n = _Base64_Encode( 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 );
n = _Base64_Encode( 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 );
n = _Base64_Encode( 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 );
n = _Base64_Encode( 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 );
n = _Base64_Encode( 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 );
n = _Base64_Encode( 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 );
n = _Base64_Encode( 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 );
n = _Base64_Encode( 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 );
n = _Base64_Encode( put_char, ctx, buf, 0, "\n", 4 );
rtems_test_assert( n == 0 );
}
@@ -267,7 +268,7 @@ static void test_io_base64url( test_context *ctx )
int n;
clear( ctx );
n = _IO_Base64url( put_char, ctx, buf, sizeof( buf ), "\n", 0 );
n = _Base64url_Encode( put_char, ctx, buf, sizeof( buf ), "\n", 0 );
rtems_test_assert( n == 9 );
rtems_test_assert( strcmp( ctx->buf, "AAA-\nAAA_" ) == 0 );
}