From 9a54e5c11340de6351f9fa22314211ded6e0e657 Mon Sep 17 00:00:00 2001 From: josesimoes Date: Mon, 7 Jun 2021 11:07:16 +0100 Subject: [PATCH] Fix FromBase64 - Replace FromChar with FromString implementation. - Update mscorlib declarations. --- src/CLR/CorLib/corlib_native.cpp | 12 ++- src/CLR/CorLib/corlib_native.h | 2 +- .../CorLib/corlib_native_System_Convert.cpp | 82 +++++++------------ 3 files changed, 37 insertions(+), 59 deletions(-) diff --git a/src/CLR/CorLib/corlib_native.cpp b/src/CLR/CorLib/corlib_native.cpp index 308c13da0e..ce09b291d6 100644 --- a/src/CLR/CorLib/corlib_native.cpp +++ b/src/CLR/CorLib/corlib_native.cpp @@ -344,10 +344,9 @@ static const CLR_RT_MethodHandler method_lookup[] = NULL, NULL, Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__SZARRAY_U1__I4__I4__BOOLEAN, + Library_corlib_native_System_Convert::FromBase64String___STATIC__SZARRAY_U1__STRING, NULL, NULL, - Library_corlib_native_System_Convert::FromBase64CharArray___STATIC__SZARRAY_U1__SZARRAY_CHAR__I4, - NULL, NULL, NULL, NULL, @@ -1092,10 +1091,9 @@ static const CLR_RT_MethodHandler method_lookup[] = NULL, NULL, Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__SZARRAY_U1__I4__I4__BOOLEAN, + Library_corlib_native_System_Convert::FromBase64String___STATIC__SZARRAY_U1__STRING, NULL, NULL, - Library_corlib_native_System_Convert::FromBase64CharArray___STATIC__SZARRAY_U1__SZARRAY_CHAR__I4, - NULL, NULL, NULL, NULL, @@ -1486,18 +1484,18 @@ const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_mscorlib = #if (NANOCLR_REFLECTION == TRUE) - 0x1AA8C441, + 0x77B5B642, #elif (NANOCLR_REFLECTION == FALSE) - 0x0D2A29C8, + 0xCAFBFF24, #else #error "NANOCLR_REFLECTION has to be define either TRUE or FALSE. Check the build options." #endif method_lookup, - { 100, 5, 0, 10 } + { 100, 5, 0, 11 } }; // clang-format on diff --git a/src/CLR/CorLib/corlib_native.h b/src/CLR/CorLib/corlib_native.h index 71d8e146c9..bf0c7ac834 100644 --- a/src/CLR/CorLib/corlib_native.h +++ b/src/CLR/CorLib/corlib_native.h @@ -490,7 +490,7 @@ struct Library_corlib_native_System_Convert NANOCLR_NATIVE_DECLARE(NativeToInt64___STATIC__I8__STRING__BOOLEAN__I8__I8__I4); NANOCLR_NATIVE_DECLARE(NativeToDouble___STATIC__R8__STRING); NANOCLR_NATIVE_DECLARE(ToBase64String___STATIC__STRING__SZARRAY_U1__I4__I4__BOOLEAN); - NANOCLR_NATIVE_DECLARE(FromBase64CharArray___STATIC__SZARRAY_U1__SZARRAY_CHAR__I4); + NANOCLR_NATIVE_DECLARE(FromBase64String___STATIC__SZARRAY_U1__STRING); //--// diff --git a/src/CLR/CorLib/corlib_native_System_Convert.cpp b/src/CLR/CorLib/corlib_native_System_Convert.cpp index a0e93bbac7..b049c3dcbc 100644 --- a/src/CLR/CorLib/corlib_native_System_Convert.cpp +++ b/src/CLR/CorLib/corlib_native_System_Convert.cpp @@ -483,8 +483,7 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S size_t length = (size_t)stack.Arg2().NumericByRef().s4; bool insertLineBreaks = (bool)stack.Arg3().NumericByRefConst().u1; - if (inArray == NULL) - NANOCLR_SET_AND_LEAVE(CLR_E_ARGUMENT_NULL); + FAULT_ON_NULL_ARG(inArray); inArrayPointer = (uint8_t *)inArray->GetFirstElement(); inArrayPointer += (offset * sizeof(uint8_t)); @@ -497,7 +496,9 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S // check if have allocation if (outArray == NULL) + { NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_MEMORY); + } // perform the operation // need to tweak the parameter with the output length because it includes room for the terminator @@ -582,69 +583,43 @@ HRESULT Library_corlib_native_System_Convert::ToBase64String___STATIC__STRING__S NANOCLR_NOCLEANUP(); } -HRESULT Library_corlib_native_System_Convert::FromBase64CharArray___STATIC__SZARRAY_U1__SZARRAY_CHAR__I4( - CLR_RT_StackFrame &stack) +HRESULT Library_corlib_native_System_Convert::FromBase64String___STATIC__SZARRAY_U1__STRING(CLR_RT_StackFrame &stack) { NANOCLR_HEADER(); + CLR_RT_HeapBlock_String *inString = NULL; size_t outputLength; char *outArray = NULL; - uint16_t *inArrayPointerTmp = NULL; - uint8_t *inArrayPointer = NULL; - uint8_t charValue; CLR_UINT8 *returnArray; - int16_t i = 0; uint16_t result; + size_t length; - CLR_RT_HeapBlock_Array *inArray = stack.Arg0().DereferenceArray(); - size_t length = (size_t)stack.Arg1().NumericByRef().s4; - - if (inArray == NULL) - NANOCLR_SET_AND_LEAVE(CLR_E_ARGUMENT_NULL); - - outputLength = length / 4 * 3; - - // transform the 16 bits inArray to a 8 bits array so mbed knows how to convert it - inArrayPointerTmp = (uint16_t *)inArray->GetFirstElementUInt16(); - inArrayPointer = (uint8_t *)inArray->GetFirstElement(); - for (i = 0; i < (int16_t)length; i++) - { - *inArrayPointer = *inArrayPointerTmp; - inArrayPointer++; - inArrayPointerTmp++; - } + inString = stack.Arg0().DereferenceString(); + FAULT_ON_NULL(inString); - // pointer is pointing to the end - // point to last char in array and get it - inArrayPointer--; - charValue = *inArrayPointer; - // adjust output length - if (charValue == '=') - { - outputLength--; - } + FAULT_ON_NULL_ARG(inString->StringText()); - // point to before last char and get it - inArrayPointer--; - charValue = *inArrayPointer; - // adjust output length - if (charValue == '=') - { - outputLength--; - } + length = (size_t)hal_strlen_s(inString->StringText()); - // reset pointer position (-2 because above we already went back two positions) - inArrayPointer -= length - 2; + // estimate output length + outputLength = length / 4 * 3; + // alloc output array outArray = (char *)platform_malloc(outputLength + 1); // check malloc success if (outArray == NULL) + { NANOCLR_SET_AND_LEAVE(CLR_E_OUT_OF_MEMORY); + } // perform the operation // need to tweak the parameter with the output length because it includes room for the terminator - result = - mbedtls_base64_decode((unsigned char *)outArray, (outputLength + 1), &outputLength, inArrayPointer, length); + result = mbedtls_base64_decode( + (unsigned char *)outArray, + (outputLength + 1), + &outputLength, + (const unsigned char *)inString->StringText(), + length); if (result != 0) { @@ -652,8 +627,8 @@ HRESULT Library_corlib_native_System_Convert::FromBase64CharArray___STATIC__SZAR NANOCLR_SET_AND_LEAVE(CLR_E_FAIL); } - // create heap block array instance with appropriate size (the length of the output array) and type (byte which is - // uint8_t) + // create heap block array instance with appropriate size (the length of the output array) + // and type (byte which is uint8_t) NANOCLR_CHECK_HRESULT(CLR_RT_HeapBlock_Array::CreateInstance( stack.PushValueAndClear(), outputLength, @@ -665,10 +640,15 @@ HRESULT Library_corlib_native_System_Convert::FromBase64CharArray___STATIC__SZAR // copy outArray to the returnArray memcpy(returnArray, outArray, outputLength); - // need to free memory from outArray - platform_free(outArray); + NANOCLR_CLEANUP(); - NANOCLR_NOCLEANUP(); + if (outArray) + { + // need to free memory from outArray + platform_free(outArray); + } + + NANOCLR_CLEANUP_END(); } double Library_corlib_native_System_Convert::GetDoubleFractionalPart(char *str, int length)