Skip to content

Commit

Permalink
aws: firehose: s3: kinesis: rebrand flb_aws_base64 as flb_base64
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Fala <[email protected]>
  • Loading branch information
matthewfala committed Dec 14, 2021
1 parent 0434352 commit 3c08a68
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
* limitations under the License.
*/

#ifndef FLB_AWS_BASE64_H
#define FLB_AWS_BASE64_H
#ifndef FLB_BASE64_H
#define FLB_BASE64_H

#include <stddef.h>

#define FLB_AWS_BASE64_ERR_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
#define FLB_AWS_BASE64_ERR_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
#define FLB_BASE64_ERR_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
#define FLB_BASE64_ERR_INVALID_CHARACTER -0x002C /**< Invalid character in input. */

/**
* \brief Encode a buffer into base64 format
Expand All @@ -70,7 +70,7 @@
* \param src source buffer
* \param slen amount of data to be encoded
*
* \return 0 if successful, or FLB_AWS_BASE64_ERR_BUFFER_TOO_SMALL.
* \return 0 if successful, or FLB_BASE64_ERR_BUFFER_TOO_SMALL.
* *olen is always updated to reflect the amount
* of data that has (or would have) been written.
* If that length cannot be represented, then no data is
Expand All @@ -80,7 +80,7 @@
* \note Call this function with dlen = 0 to obtain the
* required buffer size in *olen
*/
int flb_aws_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
int flb_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen );

/**
Expand All @@ -92,15 +92,15 @@ int flb_aws_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
* \param src source buffer
* \param slen amount of data to be decoded
*
* \return 0 if successful, FLB_AWS_BASE64_ERR_BUFFER_TOO_SMALL, or
* FLB_AWS_BASE64_ERR_INVALID_CHARACTER if the input data is
* \return 0 if successful, FLB_BASE64_ERR_BUFFER_TOO_SMALL, or
* FLB_BASE64_ERR_INVALID_CHARACTER if the input data is
* not correct. *olen is always updated to reflect the amount
* of data that has (or would have) been written.
*
* \note Call this function with *dst = NULL or dlen = 0 to obtain
* the required buffer size in *olen
*/
int flb_aws_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
int flb_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen );

#endif /* base64.h */
4 changes: 2 additions & 2 deletions plugins/out_kinesis_firehose/firehose_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <fluent-bit/flb_http_client.h>
#include <fluent-bit/flb_utils.h>

#include <fluent-bit/aws/flb_aws_base64.h>
#include <fluent-bit/flb_base64.h>

#include <monkey/mk_core.h>
#include <msgpack.h>
Expand Down Expand Up @@ -277,7 +277,7 @@ static int process_event(struct flb_firehose *ctx, struct flush *buf,
}

tmp_buf_ptr = buf->tmp_buf + buf->tmp_buf_offset;
ret = flb_aws_base64_encode((unsigned char *) buf->event_buf, size, &b64_len,
ret = flb_base64_encode((unsigned char *) buf->event_buf, size, &b64_len,
(unsigned char *) tmp_buf_ptr, written);
if (ret != 0) {
flb_errno();
Expand Down
4 changes: 2 additions & 2 deletions plugins/out_kinesis_streams/kinesis_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_http_client.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/aws/flb_aws_base64.h>
#include <fluent-bit/flb_base64.h>

#include <monkey/mk_core.h>
#include <msgpack.h>
Expand Down Expand Up @@ -342,7 +342,7 @@ static int process_event(struct flb_kinesis *ctx, struct flush *buf,
}

tmp_buf_ptr = buf->tmp_buf + buf->tmp_buf_offset;
ret = flb_aws_base64_encode((unsigned char *) buf->event_buf, size, &b64_len,
ret = flb_base64_encode((unsigned char *) buf->event_buf, size, &b64_len,
(unsigned char *) tmp_buf_ptr, written);
if (ret != 0) {
flb_errno();
Expand Down
4 changes: 2 additions & 2 deletions plugins/out_s3/s3.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <fluent-bit/flb_signv4.h>
#include <fluent-bit/flb_scheduler.h>
#include <fluent-bit/flb_gzip.h>
#include <fluent-bit/aws/flb_aws_base64.h>
#include <fluent-bit/flb_base64.h>
#include <stdlib.h>
#include <sys/stat.h>

Expand Down Expand Up @@ -1442,7 +1442,7 @@ int get_md5_base64(char *buf, size_t buf_size, char *md5_str, size_t md5_str_siz
return ret;
}

ret = flb_aws_base64_encode((unsigned char*) md5_str, md5_str_size, &olen, md5_bin,
ret = flb_base64_encode((unsigned char*) md5_str, md5_str_size, &olen, md5_bin,
sizeof(md5_bin));
if (ret != 0) {
return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set(src
flb_thread_pool.c
flb_routes_mask.c
flb_event.c
flb_base64.c
)

# Multiline subsystem
Expand Down Expand Up @@ -149,7 +150,6 @@ if(FLB_AWS)
"aws/flb_aws_imds.c"
"aws/flb_aws_credentials_http.c"
"aws/flb_aws_credentials_profile.c"
"aws/flb_aws_base64.c"
)
if(FLB_HAVE_AWS_CREDENTIAL_PROCESS)
set(src
Expand Down
20 changes: 10 additions & 10 deletions src/aws/flb_aws_base64.c → src/flb_base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
* limitations under the License.
*/

#include <fluent-bit/aws/flb_aws_base64.h>
#include <fluent-bit/flb_base64.h>

#include <stdint.h>

Expand Down Expand Up @@ -85,7 +85,7 @@ static const unsigned char base64_dec_map[128] =
/*
* Encode a buffer into base64 format
*/
int flb_aws_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
int flb_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen )
{
size_t i, n;
Expand All @@ -103,15 +103,15 @@ int flb_aws_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
if( n > ( BASE64_SIZE_T_MAX - 1 ) / 4 )
{
*olen = BASE64_SIZE_T_MAX;
return( FLB_AWS_BASE64_ERR_BUFFER_TOO_SMALL );
return( FLB_BASE64_ERR_BUFFER_TOO_SMALL );
}

n *= 4;

if( ( dlen < n + 1 ) || ( NULL == dst ) )
{
*olen = n + 1;
return( FLB_AWS_BASE64_ERR_BUFFER_TOO_SMALL );
return( FLB_BASE64_ERR_BUFFER_TOO_SMALL );
}

n = ( slen / 3 ) * 3;
Expand Down Expand Up @@ -152,7 +152,7 @@ int flb_aws_base64_encode( unsigned char *dst, size_t dlen, size_t *olen,
/*
* Decode a base64-formatted buffer
*/
int flb_aws_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
int flb_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen )
{
size_t i, n;
Expand Down Expand Up @@ -183,16 +183,16 @@ int flb_aws_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,

/* Space inside a line is an error */
if( x != 0 )
return( FLB_AWS_BASE64_ERR_INVALID_CHARACTER );
return( FLB_BASE64_ERR_INVALID_CHARACTER );

if( src[i] == '=' && ++j > 2 )
return( FLB_AWS_BASE64_ERR_INVALID_CHARACTER );
return( FLB_BASE64_ERR_INVALID_CHARACTER );

if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
return( FLB_AWS_BASE64_ERR_INVALID_CHARACTER );
return( FLB_BASE64_ERR_INVALID_CHARACTER );

if( base64_dec_map[src[i]] < 64 && j != 0 )
return( FLB_AWS_BASE64_ERR_INVALID_CHARACTER );
return( FLB_BASE64_ERR_INVALID_CHARACTER );

n++;
}
Expand All @@ -213,7 +213,7 @@ int flb_aws_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
if( dst == NULL || dlen < n )
{
*olen = n;
return( FLB_AWS_BASE64_ERR_BUFFER_TOO_SMALL );
return( FLB_BASE64_ERR_BUFFER_TOO_SMALL );
}

for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
Expand Down
3 changes: 2 additions & 1 deletion tests/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(UNIT_TESTS_FILES
input_chunk.c
flb_time.c
multiline.c
base64.c
)

if (NOT WIN32)
Expand Down Expand Up @@ -160,7 +161,7 @@ function(prepare_unit_tests TEST_PREFIX SOURCEFILES)
endforeach()
endfunction(prepare_unit_tests)

prepare_unit_tests(flb-it- ${UNIT_TESTS_FILES})
prepare_unit_tests(flb-it- "${UNIT_TESTS_FILES}")

if(FLB_AWS)
add_subdirectory(aws)
Expand Down
4 changes: 2 additions & 2 deletions tests/internal/aws/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# AWS unit tests
set(UNIT_TESTS_FILES
base64.c
placeholder.c
)

# Prepare list of unit tests
prepare_unit_tests(flb-it-aws_ ${UNIT_TESTS_FILES})
prepare_unit_tests(flb-it-aws_ "${UNIT_TESTS_FILES}")
11 changes: 11 additions & 0 deletions tests/internal/aws/placeholder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "../flb_tests_internal.h"

static void test_placeholder()
{
return;
}

TEST_LIST = {
{ "placeholder" , test_placeholder },
{ 0 }
};
10 changes: 5 additions & 5 deletions tests/internal/aws/base64.c → tests/internal/base64.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/aws/flb_aws_base64.h>
#include <fluent-bit/flb_base64.h>

#include "../flb_tests_internal.h"
#include "flb_tests_internal.h"

static void b64_basic_test_encode()
{
Expand All @@ -12,7 +12,7 @@ static void b64_basic_test_encode()
size_t olen;
out[16] = 'X';

flb_aws_base64_encode((unsigned char *) out, 100, &olen, (unsigned char *)data, 11);
flb_base64_encode((unsigned char *) out, 100, &olen, (unsigned char *)data, 11);

TEST_CHECK(strlen(out) == 16 && olen == 16);
TEST_MSG("Base64 encode failed to output result of expected length");
Expand All @@ -28,11 +28,11 @@ static void b64_basic_test_encode()
static void b64_basic_test_decode()
{
char* data = "SGVsbG8gd29ybGQ=";
char out[100];
char out[100] = { 0 };
char* expect = "Hello world";
size_t olen;

flb_aws_base64_decode((unsigned char *) out, 100, &olen, (unsigned char *)data, 16);
flb_base64_decode((unsigned char *) out, 100, &olen, (unsigned char *)data, 16);

TEST_CHECK(strlen(out) == 11 && olen == 11);
TEST_MSG("Base64 decode failed to output result of expected length");
Expand Down

0 comments on commit 3c08a68

Please sign in to comment.