From 1466518644f3ee503043e1aea24ea28b91092d38 Mon Sep 17 00:00:00 2001 From: Amir Cohen Date: Mon, 9 Jul 2018 11:28:09 +0300 Subject: [PATCH] Fixing PR comments --- TESTS/host_tests/trng_reset.py | 2 +- TESTS/mbed_hal/trng/base64b/base64b.cpp | 103 +++++++++++++----------- TESTS/mbed_hal/trng/base64b/base64b.h | 4 +- TESTS/mbed_hal/trng/main.cpp | 6 +- TESTS/mbed_hal/trng/pithy/pithy.c | 1 - 5 files changed, 63 insertions(+), 53 deletions(-) diff --git a/TESTS/host_tests/trng_reset.py b/TESTS/host_tests/trng_reset.py index b6b4adfef2..eec7ac7977 100644 --- a/TESTS/host_tests/trng_reset.py +++ b/TESTS/host_tests/trng_reset.py @@ -16,7 +16,7 @@ limitations under the License. """ This script is the host script for trng test sequence, it send the step signaling sequence and receive and transmit data to the device after -reset if necesarry (default lading and storing while reseting the device +reset if necesarry (default loading and storing mechanism while reseting the device is NVstore, in case NVstore isn't enabled we'll use current infrastructure, for more details see main.cpp file) """ diff --git a/TESTS/mbed_hal/trng/base64b/base64b.cpp b/TESTS/mbed_hal/trng/base64b/base64b.cpp index 2f668da987..2623bfee6f 100644 --- a/TESTS/mbed_hal/trng/base64b/base64b.cpp +++ b/TESTS/mbed_hal/trng/base64b/base64b.cpp @@ -31,26 +31,26 @@ static base64_result_e Base64CharToInt(char base64, uint8_t *intVal) return BASE64_INVALID_PARAMETER; } - if ((base64 >= 'A') && (base64 <= 'Z')) - { *intVal = base64 - 'A' ; } - else if ((base64 >= 'a') && (base64 <= 'z')) - { *intVal = base64 - 'a' + 26; } - else if ((base64 >= '0') && (base64 <= '9')) - { *intVal = base64 - '0' + 52; } - else if (base64 == '+') - { *intVal = 62; } - else if (base64 == '/') - { *intVal = 63; } - else if (base64 == '=') - { *intVal = BASE_64_PAD; } - else { + if ((base64 >= 'A') && (base64 <= 'Z')) { + *intVal = base64 - 'A' ; + } else if ((base64 >= 'a') && (base64 <= 'z')) { + *intVal = base64 - 'a' + 26; + } else if ((base64 >= '0') && (base64 <= '9')) { + *intVal = base64 - '0' + 52; + } else if (base64 == '+') { + *intVal = 62; + } else if (base64 == '/') { + *intVal = 63; + } else if (base64 == '=') { + *intVal = BASE_64_PAD; + } else { return BASE64_ERROR; } return BASE64_SUCCESS; } -base64_result_e esfs_DecodeNBase64(const char *string, +base64_result_e trng_DecodeNBase64(const char *string, uint32_t stringMaxSize, void *buffer, uint32_t bufferSize, @@ -72,18 +72,20 @@ base64_result_e esfs_DecodeNBase64(const char *string, } *writePtr = 0; - while (( string[currPos] != 0 ) && - ( currPos < stringMaxSize ) && + while (( currPos < stringMaxSize ) && + ( string[currPos] != 0 ) && ( writePtr < bufferEnd ) && ( !isEndOfString )) { uint8_t val; - if (string[currPos] == 0 || currPos >= stringMaxSize) - { break; } + if (string[currPos] == 0) { + break; + } result = Base64CharToInt(string[currPos++], &val); - if (result != BASE64_SUCCESS) - { break; } + if (result != BASE64_SUCCESS) { + break; + } if (val != BASE_64_PAD) { if (bitOffset <= 2) { @@ -98,10 +100,11 @@ base64_result_e esfs_DecodeNBase64(const char *string, } } else { // found BASE_64_PAD // At most two pad characters may occur at the end of the encoded stream - if (bitOffset == 2) - { isEndOfString = true; } // The last padding byte has been processed. - else if (bitOffset != 4) - { return BASE64_ERROR; } // Incorrect padding + if (bitOffset == 2) { + isEndOfString = true; // The last padding byte has been processed. + } else if (bitOffset != 4) { + return BASE64_ERROR; // Incorrect padding + } } bitOffset = (bitOffset + 6) & 0x7; @@ -110,30 +113,34 @@ base64_result_e esfs_DecodeNBase64(const char *string, localCharsProcessed = currPos; } } - if (charsProcessed == NULL) - { localBytesWritten = (uint32_t)(writePtr - (uint8_t *)buffer); } - else - { *charsProcessed = localCharsProcessed; } - if (lengthWritten != NULL) - { *lengthWritten = localBytesWritten; } - else if (bufferSize != localBytesWritten) - { return BASE64_BUFFER_TOO_SMALL; } + if (charsProcessed == NULL) { + localBytesWritten = (uint32_t)(writePtr - (uint8_t *)buffer); + } else { + *charsProcessed = localCharsProcessed; + } + if (lengthWritten != NULL) { + *lengthWritten = localBytesWritten; + } else if (bufferSize != localBytesWritten) { + return BASE64_BUFFER_TOO_SMALL; + } // Check if additional bytes should have been processed but buffer isn't sufficient. if (( result == BASE64_SUCCESS ) && ( !isEndOfString ) && - ( string[currPos] != '=' ) && + ( currPos < stringMaxSize ) && ( string[currPos] != 0 ) && - ( currPos < stringMaxSize) ) - { return BASE64_BUFFER_TOO_SMALL; } + ( string[currPos] != '=' ) ) { + return BASE64_BUFFER_TOO_SMALL; + } - if (result != BASE64_SUCCESS) - { return result; } + if (result != BASE64_SUCCESS) { + return result; + } return BASE64_SUCCESS; } -base64_result_e esfs_EncodeBase64(const void *buffer, uint32_t bufferSize, char *string, uint32_t stringSize) +base64_result_e trng_EncodeBase64(const void *buffer, uint32_t bufferSize, char *string, uint32_t stringSize) { uint32_t bitOffset = 0; @@ -143,8 +150,9 @@ base64_result_e esfs_EncodeBase64(const void *buffer, uint32_t bufferSize, char char *writePtr = string; char *stringEnd = string + stringSize - 1; - if ((NULL == string) || (NULL == buffer) || (stringSize == 0)) - { return BASE64_INVALID_PARAMETER; } + if ((NULL == string) || (NULL == buffer) || (stringSize == 0)) { + return BASE64_INVALID_PARAMETER; + } stringSize--; while (readPtr < bufferEnd && writePtr < stringEnd) { @@ -155,14 +163,16 @@ base64_result_e esfs_EncodeBase64(const void *buffer, uint32_t bufferSize, char break; case 6: tempVal = *readPtr++ << 4; - if (readPtr < bufferEnd) - { tempVal |= *readPtr >> 4; } + if (readPtr < bufferEnd) { + tempVal |= *readPtr >> 4; + } *writePtr++ = IntToBase64Char(tempVal); break; case 4: tempVal = *readPtr++ << 2; - if (readPtr < bufferEnd) - { tempVal |= *readPtr >> 6; } + if (readPtr < bufferEnd) { + tempVal |= *readPtr >> 6; + } *writePtr++ = IntToBase64Char(tempVal); break; case 2: @@ -179,8 +189,9 @@ base64_result_e esfs_EncodeBase64(const void *buffer, uint32_t bufferSize, char } *writePtr = 0; - if ((readPtr < bufferEnd) || (bitOffset != 0)) - { return (BASE64_BUFFER_TOO_SMALL); } + if ((readPtr < bufferEnd) || (bitOffset != 0)) { + return (BASE64_BUFFER_TOO_SMALL); + } return (BASE64_SUCCESS); } diff --git a/TESTS/mbed_hal/trng/base64b/base64b.h b/TESTS/mbed_hal/trng/base64b/base64b.h index e68796ce98..cbb6b85b56 100644 --- a/TESTS/mbed_hal/trng/base64b/base64b.h +++ b/TESTS/mbed_hal/trng/base64b/base64b.h @@ -25,8 +25,8 @@ typedef enum { BASE64_ERROR = 3, } base64_result_e; -base64_result_e esfs_EncodeBase64(const void *buffer, uint32_t bufferSize, char *string, uint32_t stringSize); -base64_result_e esfs_DecodeNBase64(const char *string, uint32_t stringMaxSize, void *buffer, uint32_t bufferSize, +base64_result_e trng_EncodeBase64(const void *buffer, uint32_t bufferSize, char *string, uint32_t stringSize); +base64_result_e trng_DecodeNBase64(const char *string, uint32_t stringMaxSize, void *buffer, uint32_t bufferSize, uint32_t *lengthWritten, uint32_t *charsProcessed); diff --git a/TESTS/mbed_hal/trng/main.cpp b/TESTS/mbed_hal/trng/main.cpp index a3bea42bd3..6201eada0a 100644 --- a/TESTS/mbed_hal/trng/main.cpp +++ b/TESTS/mbed_hal/trng/main.cpp @@ -17,7 +17,7 @@ /* * The test is based on the assumption that trng will generate random data, random so * there will not be any similar patterns in it, that kind of data will be impossible to -* compress, if compression will acuur the test will result in failure. +* compress, if compression will occur the test will result in failure. * * The test is composed out of three parts: * the first, generate a trng buffer and try to compress it, at the end of first part @@ -119,7 +119,7 @@ static void compress_and_compare(char *key, char *value) /*Using base64 to decode data sent from host*/ uint32_t lengthWritten = 0; uint32_t charsProcessed = 0; - result = esfs_DecodeNBase64((const char *)value, MSG_VALUE_LEN, buffer, BUFFER_LEN, &lengthWritten, &charsProcessed); + result = trng_DecodeNBase64((const char *)value, MSG_VALUE_LEN, buffer, BUFFER_LEN, &lengthWritten, &charsProcessed); TEST_ASSERT_EQUAL(0, result); #endif memcpy(input_buf, buffer, BUFFER_LEN); @@ -181,7 +181,7 @@ static void compress_and_compare(char *key, char *value) TEST_ASSERT_EQUAL(NVSTORE_SUCCESS, result); #else /*Using base64 to encode data sending from host*/ - result = esfs_EncodeBase64(buffer, BUFFER_LEN, (char *)out_comp_buf, sizeof(out_comp_buf)); + result = trng_EncodeBase64(buffer, BUFFER_LEN, (char *)out_comp_buf, sizeof(out_comp_buf)); TEST_ASSERT_EQUAL(NVSTORE_SUCCESS, result); greentea_send_kv(MSG_TRNG_BUFFER, (const char *)out_comp_buf); #endif diff --git a/TESTS/mbed_hal/trng/pithy/pithy.c b/TESTS/mbed_hal/trng/pithy/pithy.c index 11cef26975..08f021f701 100644 --- a/TESTS/mbed_hal/trng/pithy/pithy.c +++ b/TESTS/mbed_hal/trng/pithy/pithy.c @@ -128,7 +128,6 @@ PITHY_STATIC_INLINE char *pithy_EmitCopy(char *op, size_t offset, size_t l #else #define PITHY_32BIT_MOVE64 PITHY_STATIC_INLINE uint64_t pithy_Load64(const void *p) { uint64_t t; memcpy(&t, p, sizeof(t)); return (t); } -PITHY_STATIC_INLINE void pithy_Store64(void *p, uint64_t v) { memcpy(p, &v, sizeof(v)); } #endif #else // not __arm__