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
0 commit comments