1
- //
1
+ //
2
2
// Copyright (c) .NET Foundation and Contributors
3
3
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4
4
// 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
595
595
#if (SUPPORT_ANY_BASE_CONVERSION == TRUE)
596
596
597
597
size_t outputLength;
598
- char *outArray = NULL ;
598
+ unsigned char *outArray = NULL ;
599
599
char *outArrayWitLineBreak = NULL ;
600
- uint8_t *inArrayPointer = NULL ;
601
- int32_t lineBreakCount;
600
+ unsigned char *inArrayPointer = NULL ;
601
+ size_t lineBreakCount;
602
602
uint16_t offsetIndex = 0 ;
603
- uint8_t count = 0 ;
604
- uint16_t result;
603
+ size_t count = 0 ;
604
+ int result;
605
605
606
606
CLR_RT_HeapBlock_Array *inArray = stack.Arg0 ().DereferenceArray ();
607
607
size_t offset = (size_t )stack.Arg1 ().NumericByRef ().s4 ;
@@ -610,14 +610,14 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S
610
610
611
611
FAULT_ON_NULL_ARG (inArray);
612
612
613
- inArrayPointer = ( uint8_t *) inArray->GetFirstElement ();
613
+ inArrayPointer = inArray->GetFirstElement ();
614
614
inArrayPointer += (offset * sizeof (uint8_t ));
615
615
616
616
// compute base64 string length
617
617
outputLength = 4 * ((length + 2 ) / 3 );
618
618
619
619
// 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 );
621
621
622
622
// check if have allocation
623
623
if (outArray == NULL )
@@ -627,8 +627,7 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S
627
627
628
628
// perform the operation
629
629
// 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);
632
631
633
632
if (result != 0 )
634
633
{
@@ -645,7 +644,7 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S
645
644
// break
646
645
outArrayWitLineBreak = (char *)platform_malloc (outputLength + (lineBreakCount * 2 ) + 2 );
647
646
648
- for (int i = 0 ; i <= lineBreakCount; i++)
647
+ for (size_t i = 0 ; i <= lineBreakCount; i++)
649
648
{
650
649
// how many chars to copy
651
650
if (outputLength > 76 )
@@ -694,7 +693,7 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S
694
693
{
695
694
// set a return result in the stack argument using the appropriate SetResult according to the variable type (a
696
695
// string here)
697
- NANOCLR_CHECK_HRESULT (stack.SetResult_String (outArray));
696
+ NANOCLR_CHECK_HRESULT (stack.SetResult_String (( const char *) outArray));
698
697
}
699
698
700
699
// need to free memory from arrays
@@ -721,11 +720,11 @@ HRESULT Library_corlib_native_System_Convert::FromBase64String___STATIC__SZARRAY
721
720
#if (SUPPORT_ANY_BASE_CONVERSION == TRUE)
722
721
723
722
CLR_RT_HeapBlock_String *inString = NULL ;
724
- uint32_t outputLength;
725
- char *outArray = NULL ;
723
+ size_t outputLength;
724
+ unsigned char *outArray = NULL ;
726
725
CLR_UINT8 *returnArray;
727
- uint16_t result;
728
- uint32_t length;
726
+ int result;
727
+ size_t length;
729
728
730
729
inString = stack.Arg0 ().DereferenceString ();
731
730
FAULT_ON_NULL (inString);
@@ -738,19 +737,21 @@ HRESULT Library_corlib_native_System_Convert::FromBase64String___STATIC__SZARRAY
738
737
outputLength = length / 4 * 3 ;
739
738
740
739
// alloc output array
741
- outArray = (char *)platform_malloc (outputLength + 1 );
740
+ outArray = (unsigned char *)platform_malloc (outputLength + 1 );
742
741
// check malloc success
743
742
if (outArray == NULL )
744
743
{
745
744
NANOCLR_SET_AND_LEAVE (CLR_E_OUT_OF_MEMORY);
746
745
}
747
746
747
+ memset (outArray, 0 , outputLength + 1 );
748
+
748
749
// perform the operation
749
750
// need to tweak the parameter with the output length because it includes room for the terminator
750
751
result = mbedtls_base64_decode (
751
- ( unsigned char *) outArray,
752
- (size_t )( outputLength + 1 ),
753
- ( size_t *) &outputLength,
752
+ outArray,
753
+ (outputLength + 1 ),
754
+ &outputLength,
754
755
(const unsigned char *)inString->StringText (),
755
756
length);
756
757
0 commit comments