Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FromBase64 #1944

Merged
merged 1 commit into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/CLR/CorLib/corlib_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/CLR/CorLib/corlib_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

//--//

Expand Down
82 changes: 31 additions & 51 deletions src/CLR/CorLib/corlib_native_System_Convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand Down Expand Up @@ -582,78 +583,52 @@ 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)
{
// internal error occurred
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,
Expand All @@ -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)
Expand Down