Fix lisence and style

pull/8341/head
Amir Cohen 2018-05-30 17:15:20 +03:00 committed by adbridge
parent f8f67e29fc
commit 1aa1682d8c
6 changed files with 293 additions and 284 deletions

View File

@ -1,3 +1,19 @@
/*
* Copyright (c) 2018 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "base64b.h" #include "base64b.h"
using namespace std; using namespace std;
@ -11,25 +27,23 @@ static char IntToBase64Char(uint8_t intVal)
#define BASE_64_PAD 0xFF #define BASE_64_PAD 0xFF
static base64_result_e Base64CharToInt(char base64, uint8_t *intVal) static base64_result_e Base64CharToInt(char base64, uint8_t *intVal)
{ {
if (NULL == intVal) if (NULL == intVal) {
{
return BASE64_INVALID_PARAMETER; return BASE64_INVALID_PARAMETER;
} }
if ((base64 >= 'A') && (base64 <= 'Z')) if ((base64 >= 'A') && (base64 <= 'Z'))
*intVal = base64 - 'A' ; { *intVal = base64 - 'A' ; }
else if ((base64 >= 'a') && (base64 <= 'z')) else if ((base64 >= 'a') && (base64 <= 'z'))
*intVal = base64 - 'a' + 26; { *intVal = base64 - 'a' + 26; }
else if ((base64 >= '0') && (base64 <= '9')) else if ((base64 >= '0') && (base64 <= '9'))
*intVal = base64 - '0' + 52; { *intVal = base64 - '0' + 52; }
else if (base64 == '+') else if (base64 == '+')
*intVal = 62; { *intVal = 62; }
else if (base64 == '/') else if (base64 == '/')
*intVal = 63; { *intVal = 63; }
else if (base64 == '=') else if (base64 == '=')
*intVal = BASE_64_PAD; { *intVal = BASE_64_PAD; }
else else {
{
return BASE64_ERROR; return BASE64_ERROR;
} }
@ -53,8 +67,7 @@ base64_result_e esfs_DecodeNBase64(const char* string,
uint32_t localCharsProcessed = 0; uint32_t localCharsProcessed = 0;
bool isEndOfString = false; bool isEndOfString = false;
if ((NULL == string) || (NULL == buffer) || (bufferSize == 0)) if ((NULL == string) || (NULL == buffer) || (bufferSize == 0)) {
{
return BASE64_INVALID_PARAMETER; return BASE64_INVALID_PARAMETER;
} }
@ -62,58 +75,49 @@ base64_result_e esfs_DecodeNBase64(const char* string,
while (( string[currPos] != 0 ) && while (( string[currPos] != 0 ) &&
( currPos < stringMaxSize ) && ( currPos < stringMaxSize ) &&
( writePtr < bufferEnd ) && ( writePtr < bufferEnd ) &&
( !isEndOfString )) ( !isEndOfString )) {
{
uint8_t val; uint8_t val;
if (string[currPos] == 0 || currPos >= stringMaxSize) if (string[currPos] == 0 || currPos >= stringMaxSize)
break; { break; }
result = Base64CharToInt(string[currPos++], &val); result = Base64CharToInt(string[currPos++], &val);
if (result != BASE64_SUCCESS) if (result != BASE64_SUCCESS)
break; { break; }
if (val != BASE_64_PAD) if (val != BASE_64_PAD) {
{ if (bitOffset <= 2) {
if(bitOffset <= 2)
{
tempVal |= val << (2 - bitOffset); tempVal |= val << (2 - bitOffset);
if (bitOffset == 2) if (bitOffset == 2) {
{
*writePtr++ = tempVal; *writePtr++ = tempVal;
tempVal = 0; tempVal = 0;
} }
} } else {
else
{
*writePtr++ = (uint8_t)(tempVal | (val >> (bitOffset - 2))); *writePtr++ = (uint8_t)(tempVal | (val >> (bitOffset - 2)));
tempVal = (uint8_t)(val << (10 - bitOffset)); tempVal = (uint8_t)(val << (10 - bitOffset));
} }
} } else { // found BASE_64_PAD
else // found BASE_64_PAD
{
// At most two pad characters may occur at the end of the encoded stream // At most two pad characters may occur at the end of the encoded stream
if (bitOffset == 2) if (bitOffset == 2)
isEndOfString = true; // The last padding byte has been processed. { isEndOfString = true; } // The last padding byte has been processed.
else if (bitOffset != 4) else if (bitOffset != 4)
return BASE64_ERROR; // Incorrect padding { return BASE64_ERROR; } // Incorrect padding
} }
bitOffset = (bitOffset + 6) & 0x7; bitOffset = (bitOffset + 6) & 0x7;
if (bitOffset == 0) if (bitOffset == 0) {
{
localBytesWritten = (uint32_t)(writePtr - (uint8_t *)buffer); localBytesWritten = (uint32_t)(writePtr - (uint8_t *)buffer);
localCharsProcessed = currPos; localCharsProcessed = currPos;
} }
} }
if (charsProcessed == NULL) if (charsProcessed == NULL)
localBytesWritten = (uint32_t)(writePtr - (uint8_t*)buffer); { localBytesWritten = (uint32_t)(writePtr - (uint8_t *)buffer); }
else else
*charsProcessed = localCharsProcessed; { *charsProcessed = localCharsProcessed; }
if (lengthWritten != NULL) if (lengthWritten != NULL)
*lengthWritten = localBytesWritten; { *lengthWritten = localBytesWritten; }
else if (bufferSize != localBytesWritten) else if (bufferSize != localBytesWritten)
return BASE64_BUFFER_TOO_SMALL; { return BASE64_BUFFER_TOO_SMALL; }
// Check if additional bytes should have been processed but buffer isn't sufficient. // Check if additional bytes should have been processed but buffer isn't sufficient.
if (( result == BASE64_SUCCESS ) && if (( result == BASE64_SUCCESS ) &&
@ -121,10 +125,10 @@ base64_result_e esfs_DecodeNBase64(const char* string,
( string[currPos] != '=' ) && ( string[currPos] != '=' ) &&
( string[currPos] != 0 ) && ( string[currPos] != 0 ) &&
( currPos < stringMaxSize) ) ( currPos < stringMaxSize) )
return BASE64_BUFFER_TOO_SMALL; { return BASE64_BUFFER_TOO_SMALL; }
if (result != BASE64_SUCCESS) if (result != BASE64_SUCCESS)
return result; { return result; }
return BASE64_SUCCESS; return BASE64_SUCCESS;
} }
@ -140,27 +144,25 @@ base64_result_e esfs_EncodeBase64(const void* buffer, uint32_t bufferSize, char*
char *stringEnd = string + stringSize - 1; char *stringEnd = string + stringSize - 1;
if ((NULL == string) || (NULL == buffer) || (stringSize == 0)) if ((NULL == string) || (NULL == buffer) || (stringSize == 0))
return BASE64_INVALID_PARAMETER; { return BASE64_INVALID_PARAMETER; }
stringSize--; stringSize--;
while(readPtr < bufferEnd && writePtr < stringEnd) while (readPtr < bufferEnd && writePtr < stringEnd) {
{
uint8_t tempVal = 0; uint8_t tempVal = 0;
switch (bitOffset) switch (bitOffset) {
{
case 0: case 0:
*writePtr++ = IntToBase64Char(*readPtr >> 2); // take upper 6 bits *writePtr++ = IntToBase64Char(*readPtr >> 2); // take upper 6 bits
break; break;
case 6: case 6:
tempVal = *readPtr++ << 4; tempVal = *readPtr++ << 4;
if (readPtr < bufferEnd) if (readPtr < bufferEnd)
tempVal |= *readPtr >> 4; { tempVal |= *readPtr >> 4; }
*writePtr++ = IntToBase64Char(tempVal); *writePtr++ = IntToBase64Char(tempVal);
break; break;
case 4: case 4:
tempVal = *readPtr++ << 2; tempVal = *readPtr++ << 2;
if (readPtr < bufferEnd) if (readPtr < bufferEnd)
tempVal |= *readPtr >> 6; { tempVal |= *readPtr >> 6; }
*writePtr++ = IntToBase64Char(tempVal); *writePtr++ = IntToBase64Char(tempVal);
break; break;
case 2: case 2:
@ -171,15 +173,14 @@ base64_result_e esfs_EncodeBase64(const void* buffer, uint32_t bufferSize, char*
} }
bitOffset = (bitOffset + 6) & 0x7; bitOffset = (bitOffset + 6) & 0x7;
} }
while (bitOffset > 0 && writePtr < stringEnd) while (bitOffset > 0 && writePtr < stringEnd) {
{
*writePtr++ = '='; *writePtr++ = '=';
bitOffset = (bitOffset + 6) & 0x7; bitOffset = (bitOffset + 6) & 0x7;
} }
*writePtr = 0; *writePtr = 0;
if ((readPtr < bufferEnd) || (bitOffset != 0)) if ((readPtr < bufferEnd) || (bitOffset != 0))
return (BASE64_BUFFER_TOO_SMALL); { return (BASE64_BUFFER_TOO_SMALL); }
return (BASE64_SUCCESS); return (BASE64_SUCCESS);
} }

View File

@ -1,3 +1,19 @@
/*
* Copyright (c) 2018 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string> #include <string>

View File

@ -128,22 +128,23 @@ lzf_compress (const void *const in_data, unsigned int in_len,
int lit; int lit;
if (!in_len || !out_len) if (!in_len || !out_len)
return 0; { return 0; }
#if INIT_HTAB #if INIT_HTAB
memset (htab, 0, sizeof (htab)); memset (htab, 0, sizeof (htab));
#endif #endif
lit = 0; op++; /* start run */ lit = 0;
op++; /* start run */
hval = FRST (ip); hval = FRST (ip);
while (ip < in_end - 2) while (ip < in_end - 2) {
{
LZF_HSLOT *hslot; LZF_HSLOT *hslot;
hval = NEXT (hval, ip); hval = NEXT (hval, ip);
hslot = htab + IDX (hval); hslot = htab + IDX (hval);
ref = *hslot + LZF_HSLOT_BIAS; *hslot = ip - LZF_HSLOT_BIAS; ref = *hslot + LZF_HSLOT_BIAS;
*hslot = ip - LZF_HSLOT_BIAS;
if (1 if (1
#if INIT_HTAB #if INIT_HTAB
@ -157,8 +158,7 @@ lzf_compress (const void *const in_data, unsigned int in_len,
#else #else
&& *(u16 *)ref == *(u16 *)ip && *(u16 *)ref == *(u16 *)ip
#endif #endif
) ) {
{
/* match found at *ref++ */ /* match found at *ref++ */
unsigned int len = 2; unsigned int len = 2;
unsigned int maxlen = (unsigned int)((uintptr_t)in_end - (uintptr_t)ip) - len; unsigned int maxlen = (unsigned int)((uintptr_t)in_end - (uintptr_t)ip) - len;
@ -166,38 +166,52 @@ lzf_compress (const void *const in_data, unsigned int in_len,
if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */ if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */
if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */ if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */
return 0; { return 0; }
op [- lit - 1] = lit - 1; /* stop run */ op [- lit - 1] = lit - 1; /* stop run */
op -= !lit; /* undo run if length is zero */ op -= !lit; /* undo run if length is zero */
for (;;) for (;;) {
{ if (expect_true (maxlen > 16)) {
if (expect_true (maxlen > 16)) len++;
{ if (ref [len] != ip [len]) { break; }
len++; if (ref [len] != ip [len]) break; len++;
len++; if (ref [len] != ip [len]) break; if (ref [len] != ip [len]) { break; }
len++; if (ref [len] != ip [len]) break; len++;
len++; if (ref [len] != ip [len]) break; if (ref [len] != ip [len]) { break; }
len++;
if (ref [len] != ip [len]) { break; }
len++; if (ref [len] != ip [len]) break; len++;
len++; if (ref [len] != ip [len]) break; if (ref [len] != ip [len]) { break; }
len++; if (ref [len] != ip [len]) break; len++;
len++; if (ref [len] != ip [len]) break; if (ref [len] != ip [len]) { break; }
len++;
if (ref [len] != ip [len]) { break; }
len++;
if (ref [len] != ip [len]) { break; }
len++; if (ref [len] != ip [len]) break; len++;
len++; if (ref [len] != ip [len]) break; if (ref [len] != ip [len]) { break; }
len++; if (ref [len] != ip [len]) break; len++;
len++; if (ref [len] != ip [len]) break; if (ref [len] != ip [len]) { break; }
len++;
if (ref [len] != ip [len]) { break; }
len++;
if (ref [len] != ip [len]) { break; }
len++; if (ref [len] != ip [len]) break; len++;
len++; if (ref [len] != ip [len]) break; if (ref [len] != ip [len]) { break; }
len++; if (ref [len] != ip [len]) break; len++;
len++; if (ref [len] != ip [len]) break; if (ref [len] != ip [len]) { break; }
len++;
if (ref [len] != ip [len]) { break; }
len++;
if (ref [len] != ip [len]) { break; }
} }
do do
len++; { len++; }
while (len < maxlen && ref[len] == ip[len]); while (len < maxlen && ref[len] == ip[len]);
break; break;
@ -206,24 +220,22 @@ lzf_compress (const void *const in_data, unsigned int in_len,
len -= 2; /* len is now #octets - 1 */ len -= 2; /* len is now #octets - 1 */
ip++; ip++;
if (len < 7) if (len < 7) {
{
*op++ = (u8)((off >> 8) + (len << 5)); *op++ = (u8)((off >> 8) + (len << 5));
} } else {
else
{
*op++ = (u8)((off >> 8) + ( 7 << 5)); *op++ = (u8)((off >> 8) + ( 7 << 5));
*op++ = len - 7; *op++ = len - 7;
} }
*op++ = (u8)off; *op++ = (u8)off;
lit = 0; op++; /* start run */ lit = 0;
op++; /* start run */
ip += len + 1; ip += len + 1;
if (expect_false (ip >= in_end - 2)) if (expect_false (ip >= in_end - 2))
break; { break; }
#if ULTRA_FAST || VERY_FAST #if ULTRA_FAST || VERY_FAST
--ip; --ip;
@ -244,42 +256,39 @@ lzf_compress (const void *const in_data, unsigned int in_len,
#else #else
ip -= len + 1; ip -= len + 1;
do do {
{
hval = NEXT (hval, ip); hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS; htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++; ip++;
} } while (len--);
while (len--);
#endif #endif
} } else {
else
{
/* one more literal byte we must copy */ /* one more literal byte we must copy */
if (expect_false (op >= out_end)) if (expect_false (op >= out_end))
return 0; { return 0; }
lit++; *op++ = *ip++; lit++;
*op++ = *ip++;
if (expect_false (lit == MAX_LIT)) if (expect_false (lit == MAX_LIT)) {
{
op [- lit - 1] = lit - 1; /* stop run */ op [- lit - 1] = lit - 1; /* stop run */
lit = 0; op++; /* start run */ lit = 0;
op++; /* start run */
} }
} }
} }
if (op + 3 > out_end) /* at most 3 bytes can be missing here */ if (op + 3 > out_end) /* at most 3 bytes can be missing here */
return 0; { return 0; }
while (ip < in_end) while (ip < in_end) {
{ lit++;
lit++; *op++ = *ip++; *op++ = *ip++;
if (expect_false (lit == MAX_LIT)) if (expect_false (lit == MAX_LIT)) {
{
op [- lit - 1] = lit - 1; /* stop run */ op [- lit - 1] = lit - 1; /* stop run */
lit = 0; op++; /* start run */ lit = 0;
op++; /* start run */
} }
} }

View File

@ -85,15 +85,13 @@ static int fill_buffer_trng(uint8_t *buffer, trng_t *trng_obj, size_t trng_len)
trng_init(trng_obj); trng_init(trng_obj);
memset(buffer, 0, BUFFER_LEN); memset(buffer, 0, BUFFER_LEN);
while (true) while (true) {
{
trng_res = trng_get_bytes(trng_obj, temp_in_buf, trng_len, &output_length); trng_res = trng_get_bytes(trng_obj, temp_in_buf, trng_len, &output_length);
TEST_ASSERT_EQUAL_INT_MESSAGE(0, trng_res, "trng_get_bytes error!"); TEST_ASSERT_EQUAL_INT_MESSAGE(0, trng_res, "trng_get_bytes error!");
temp_size += output_length; temp_size += output_length;
temp_in_buf += output_length; temp_in_buf += output_length;
trng_len -= output_length; trng_len -= output_length;
if (temp_size >= trng_len) if (temp_size >= trng_len) {
{
break; break;
} }
} }
@ -117,8 +115,7 @@ static void compress_and_compare(char *key, char *value)
#endif #endif
/*At the begining of step 2 load trng buffer from step 1*/ /*At the begining of step 2 load trng buffer from step 1*/
if (strcmp(key, MSG_TRNG_TEST_STEP2) == 0) if (strcmp(key, MSG_TRNG_TEST_STEP2) == 0) {
{
#if NVSTORE_RESET #if NVSTORE_RESET
uint16_t actual = 0; uint16_t actual = 0;
result = nvstore.get(NVKEY, sizeof(buffer), buffer, actual); result = nvstore.get(NVKEY, sizeof(buffer), buffer, actual);
@ -138,8 +135,7 @@ static void compress_and_compare(char *key, char *value)
TEST_ASSERT_EQUAL(0, result); TEST_ASSERT_EQUAL(0, result);
/*lzf_compress will try to compress the random data, if it succeeded it means the data is not really random*/ /*lzf_compress will try to compress the random data, if it succeeded it means the data is not really random*/
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) {
{
printf("\n******TRNG_TEST_STEP1*****\n"); printf("\n******TRNG_TEST_STEP1*****\n");
out_comp_buf_len = BUFFER_LEN + (BUFFER_LEN / 4); out_comp_buf_len = BUFFER_LEN + (BUFFER_LEN / 4);
comp_res = lzf_compress((const void *)buffer, comp_res = lzf_compress((const void *)buffer,
@ -147,19 +143,14 @@ static void compress_and_compare(char *key, char *value)
(void *)out_comp_buf, (void *)out_comp_buf,
out_comp_buf_len, out_comp_buf_len,
(unsigned char **)htab); (unsigned char **)htab);
if (comp_res >= BUFFER_LEN) if (comp_res >= BUFFER_LEN) {
{
printf("trng_get_bytes for buffer size %d was successful", sizeof(buffer)); printf("trng_get_bytes for buffer size %d was successful", sizeof(buffer));
} } else {
else
{
printf("trng_get_bytes for buffer size %d was unsuccessful", sizeof(buffer)); printf("trng_get_bytes for buffer size %d was unsuccessful", sizeof(buffer));
TEST_ASSERT(false); TEST_ASSERT(false);
} }
printf("\n******FINISHED_TRNG_TEST_STEP1*****\n\n"); printf("\n******FINISHED_TRNG_TEST_STEP1*****\n\n");
} } else if (strcmp(key, MSG_TRNG_TEST_STEP2) == 0) {
else if (strcmp(key, MSG_TRNG_TEST_STEP2) == 0)
{
printf("\n******TRNG_TEST_STEP2*****\n"); printf("\n******TRNG_TEST_STEP2*****\n");
result = fill_buffer_trng(temp_buff, &trng_obj, sizeof(temp_buff)); result = fill_buffer_trng(temp_buff, &trng_obj, sizeof(temp_buff));
TEST_ASSERT_EQUAL(0, result); TEST_ASSERT_EQUAL(0, result);
@ -171,12 +162,9 @@ static void compress_and_compare(char *key, char *value)
out_comp_buf_len, out_comp_buf_len,
(unsigned char **)htab); (unsigned char **)htab);
if (comp_res >= BUFFER_LEN) if (comp_res >= BUFFER_LEN) {
{
printf("trng_get_bytes for buffer size %d was successful", sizeof(temp_buff)); printf("trng_get_bytes for buffer size %d was successful", sizeof(temp_buff));
} } else {
else
{
printf("trng_get_bytes for buffer size %d was unsuccessful", sizeof(temp_buff)); printf("trng_get_bytes for buffer size %d was unsuccessful", sizeof(temp_buff));
TEST_ASSERT(false); TEST_ASSERT(false);
} }
@ -191,12 +179,9 @@ static void compress_and_compare(char *key, char *value)
out_comp_buf_len, out_comp_buf_len,
(unsigned char **)htab); (unsigned char **)htab);
if (comp_res >= BUFFER_LEN) if (comp_res >= BUFFER_LEN) {
{
printf("compression for concatenated buffer after reset was successful"); printf("compression for concatenated buffer after reset was successful");
} } else {
else
{
printf("compression for concatenated buffer after reset was unsuccessful"); printf("compression for concatenated buffer after reset was unsuccessful");
TEST_ASSERT(false); TEST_ASSERT(false);
} }
@ -204,8 +189,7 @@ static void compress_and_compare(char *key, char *value)
} }
/*At the end of step 1 store trng buffer and reset the device*/ /*At the end of step 1 store trng buffer and reset the device*/
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) {
{
int result = 0; int result = 0;
#if NVSTORE_RESET #if NVSTORE_RESET
result = nvstore.set(NVKEY, sizeof(buffer), buffer); result = nvstore.set(NVKEY, sizeof(buffer), buffer);
@ -235,22 +219,21 @@ void trng_test()
greentea_parse_kv(key, value, MSG_KEY_LEN, MSG_VALUE_LEN); greentea_parse_kv(key, value, MSG_KEY_LEN, MSG_VALUE_LEN);
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) {
{
/*create trng data buffer and try to compress it, store it for later checks*/ /*create trng data buffer and try to compress it, store it for later checks*/
compress_and_compare(key, value); compress_and_compare(key, value);
return trng_test(); return trng_test();
} }
if (strcmp(key, MSG_TRNG_TEST_STEP2) == 0) if (strcmp(key, MSG_TRNG_TEST_STEP2) == 0) {
{
/*create another trng data buffer and concatenate it to the stored trng data buffer /*create another trng data buffer and concatenate it to the stored trng data buffer
try to compress them both*/ try to compress them both*/
compress_and_compare(key, value); compress_and_compare(key, value);
} }
} }
utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason)
{
greentea_case_failure_abort_handler(source, reason); greentea_case_failure_abort_handler(source, reason);
return STATUS_CONTINUE; return STATUS_CONTINUE;
} }