Skip to content

Commit 4e9cd74

Browse files
authored
Fix base64 encode/decode and To/From Base64 string (#3082)
***NO_CI***
1 parent 35df688 commit 4e9cd74

File tree

4 files changed

+131
-94
lines changed

4 files changed

+131
-94
lines changed

src/CLR/CorLib/corlib_native_System_Convert.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
44
// Portions Copyright (C) 2002-2019 Free Software Foundation, Inc. All rights reserved.
@@ -595,13 +595,13 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S
595595
#if (SUPPORT_ANY_BASE_CONVERSION == TRUE)
596596

597597
size_t outputLength;
598-
char *outArray = NULL;
598+
unsigned char *outArray = NULL;
599599
char *outArrayWitLineBreak = NULL;
600-
uint8_t *inArrayPointer = NULL;
601-
int32_t lineBreakCount;
600+
unsigned char *inArrayPointer = NULL;
601+
size_t lineBreakCount;
602602
uint16_t offsetIndex = 0;
603-
uint8_t count = 0;
604-
uint16_t result;
603+
size_t count = 0;
604+
int result;
605605

606606
CLR_RT_HeapBlock_Array *inArray = stack.Arg0().DereferenceArray();
607607
size_t offset = (size_t)stack.Arg1().NumericByRef().s4;
@@ -610,14 +610,14 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S
610610

611611
FAULT_ON_NULL_ARG(inArray);
612612

613-
inArrayPointer = (uint8_t *)inArray->GetFirstElement();
613+
inArrayPointer = inArray->GetFirstElement();
614614
inArrayPointer += (offset * sizeof(uint8_t));
615615

616616
// compute base64 string length
617617
outputLength = 4 * ((length + 2) / 3);
618618

619619
// need malloc with base64 string length plus string terminator (+1)
620-
outArray = (char *)platform_malloc(outputLength + 1);
620+
outArray = (unsigned char *)platform_malloc(outputLength + 1);
621621

622622
// check if have allocation
623623
if (outArray == NULL)
@@ -627,8 +627,7 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S
627627

628628
// perform the operation
629629
// need to tweak the parameter with the output length because it includes room for the terminator
630-
result =
631-
mbedtls_base64_encode((unsigned char *)outArray, (outputLength + 1), &outputLength, inArrayPointer, length);
630+
result = mbedtls_base64_encode(outArray, (outputLength + 1), &outputLength, inArrayPointer, length);
632631

633632
if (result != 0)
634633
{
@@ -645,7 +644,7 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S
645644
// break
646645
outArrayWitLineBreak = (char *)platform_malloc(outputLength + (lineBreakCount * 2) + 2);
647646

648-
for (int i = 0; i <= lineBreakCount; i++)
647+
for (size_t i = 0; i <= lineBreakCount; i++)
649648
{
650649
// how many chars to copy
651650
if (outputLength > 76)
@@ -694,7 +693,7 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S
694693
{
695694
// set a return result in the stack argument using the appropriate SetResult according to the variable type (a
696695
// string here)
697-
NANOCLR_CHECK_HRESULT(stack.SetResult_String(outArray));
696+
NANOCLR_CHECK_HRESULT(stack.SetResult_String((const char *)outArray));
698697
}
699698

700699
// need to free memory from arrays
@@ -721,11 +720,11 @@ HRESULT Library_corlib_native_System_Convert::FromBase64String___STATIC__SZARRAY
721720
#if (SUPPORT_ANY_BASE_CONVERSION == TRUE)
722721

723722
CLR_RT_HeapBlock_String *inString = NULL;
724-
uint32_t outputLength;
725-
char *outArray = NULL;
723+
size_t outputLength;
724+
unsigned char *outArray = NULL;
726725
CLR_UINT8 *returnArray;
727-
uint16_t result;
728-
uint32_t length;
726+
int result;
727+
size_t length;
729728

730729
inString = stack.Arg0().DereferenceString();
731730
FAULT_ON_NULL(inString);
@@ -738,19 +737,21 @@ HRESULT Library_corlib_native_System_Convert::FromBase64String___STATIC__SZARRAY
738737
outputLength = length / 4 * 3;
739738

740739
// alloc output array
741-
outArray = (char *)platform_malloc(outputLength + 1);
740+
outArray = (unsigned char *)platform_malloc(outputLength + 1);
742741
// check malloc success
743742
if (outArray == NULL)
744743
{
745744
NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_MEMORY);
746745
}
747746

747+
memset(outArray, 0, outputLength + 1);
748+
748749
// perform the operation
749750
// need to tweak the parameter with the output length because it includes room for the terminator
750751
result = mbedtls_base64_decode(
751-
(unsigned char *)outArray,
752-
(size_t)(outputLength + 1),
753-
(size_t *)&outputLength,
752+
outArray,
753+
(outputLength + 1),
754+
&outputLength,
754755
(const unsigned char *)inString->StringText(),
755756
length);
756757

src/CLR/Helpers/Base64/base64.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@
1818

1919
#include "base64.h"
2020

21-
// from MbedTLS common.h
22-
#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) )
23-
#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) )
24-
#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) )
25-
#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) )
26-
#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) )
27-
#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) )
28-
#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) )
29-
#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) )
30-
3121
unsigned char mbedtls_ct_uchar_mask_of_range( unsigned char low,
3222
unsigned char high,
3323
unsigned char c )
@@ -237,4 +227,4 @@ __nfweak int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *ole
237227
return( 0 );
238228
}
239229

240-
// clang-format on
230+
// clang-format on

src/CLR/Helpers/Base64/base64.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// Portions Copyright (c) The Mbed TLS Contributors. All Rights Reserved.
44
// See LICENSE file in the project root for full license information.
@@ -15,9 +15,14 @@
1515
// thus there will be no duplicate code //
1616
//////////////////////////////////////////////////////////////////////////////////////////////
1717

18-
#define BASE64_SIZE_T_MAX ((size_t)-1) /* SIZE_T_MAX is not standard */
19-
#define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
20-
#define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
18+
#define BASE64_SIZE_T_MAX ((size_t) - 1) /* SIZE_T_MAX is not standard */
19+
#define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */
20+
#define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */
21+
22+
// from MbedTLS common.h
23+
#define MBEDTLS_BYTE_0(x) ((uint8_t)((x) & 0xff))
24+
#define MBEDTLS_BYTE_1(x) ((uint8_t)(((x) >> 8) & 0xff))
25+
#define MBEDTLS_BYTE_2(x) ((uint8_t)(((x) >> 16) & 0xff))
2126

2227
#ifdef __cplusplus
2328
extern "C"

0 commit comments

Comments
 (0)