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,60 +1,73 @@
/*
* 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;
static char IntToBase64Char(uint8_t intVal) static char IntToBase64Char(uint8_t intVal)
{ {
const char* base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const char *base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
return base64Digits[intVal & 0x3F]; return base64Digits[intVal & 0x3F];
} }
#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;
} }
return BASE64_SUCCESS; return BASE64_SUCCESS;
} }
base64_result_e esfs_DecodeNBase64(const char* string, base64_result_e esfs_DecodeNBase64(const char *string,
uint32_t stringMaxSize, uint32_t stringMaxSize,
void* buffer, void *buffer,
uint32_t bufferSize, uint32_t bufferSize,
uint32_t* lengthWritten, uint32_t *lengthWritten,
uint32_t* charsProcessed) uint32_t *charsProcessed)
{ {
base64_result_e result = BASE64_SUCCESS; base64_result_e result = BASE64_SUCCESS;
uint32_t bitOffset =0; uint32_t bitOffset = 0;
uint8_t* writePtr = (uint8_t*)buffer; uint8_t *writePtr = (uint8_t *)buffer;
uint8_t* bufferEnd = (uint8_t*)buffer + bufferSize; uint8_t *bufferEnd = (uint8_t *)buffer + bufferSize;
uint8_t tempVal = 0; uint8_t tempVal = 0;
uint32_t currPos = 0; uint32_t currPos = 0;
uint32_t localBytesWritten = 0; uint32_t localBytesWritten = 0;
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,105 +75,94 @@ 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 ) &&
( !isEndOfString ) && ( !isEndOfString ) &&
( 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;
} }
base64_result_e esfs_EncodeBase64(const void* buffer, uint32_t bufferSize, char* string, uint32_t stringSize) base64_result_e esfs_EncodeBase64(const void *buffer, uint32_t bufferSize, char *string, uint32_t stringSize)
{ {
uint32_t bitOffset = 0; uint32_t bitOffset = 0;
const uint8_t* readPtr = (const uint8_t*)buffer; const uint8_t *readPtr = (const uint8_t *)buffer;
const uint8_t* bufferEnd = (const uint8_t*)buffer + bufferSize; const uint8_t *bufferEnd = (const uint8_t *)buffer + bufferSize;
char* writePtr = string; char *writePtr = string;
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>
@ -7,10 +23,10 @@ typedef enum {
BASE64_INVALID_PARAMETER = 1, BASE64_INVALID_PARAMETER = 1,
BASE64_BUFFER_TOO_SMALL = 2, BASE64_BUFFER_TOO_SMALL = 2,
BASE64_ERROR = 3, BASE64_ERROR = 3,
}base64_result_e; } base64_result_e;
base64_result_e esfs_EncodeBase64(const void* buffer, uint32_t bufferSize, char* string, uint32_t stringSize); 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 esfs_DecodeNBase64(const char *string, uint32_t stringMaxSize, void *buffer, uint32_t bufferSize,
uint32_t* lengthWritten, uint32_t* charsProcessed); uint32_t *lengthWritten, uint32_t *charsProcessed);

View File

@ -157,10 +157,10 @@ typedef unsigned char u8;
#if LZF_USE_OFFSETS #if LZF_USE_OFFSETS
# define LZF_HSLOT_BIAS ((const u8 *)in_data) # define LZF_HSLOT_BIAS ((const u8 *)in_data)
typedef unsigned int LZF_HSLOT; typedef unsigned int LZF_HSLOT;
#else #else
# define LZF_HSLOT_BIAS 0 # define LZF_HSLOT_BIAS 0
typedef const u8 *LZF_HSLOT; typedef const u8 *LZF_HSLOT;
#endif #endif
typedef LZF_HSLOT LZF_STATE[1 << (HLOG)]; typedef LZF_HSLOT LZF_STATE[1 << (HLOG)];
@ -168,9 +168,9 @@ typedef LZF_HSLOT LZF_STATE[1 << (HLOG)];
#if !STRICT_ALIGN #if !STRICT_ALIGN
/* for unaligned accesses we need a 16 bit datatype. */ /* for unaligned accesses we need a 16 bit datatype. */
# if USHRT_MAX == 65535 # if USHRT_MAX == 65535
typedef unsigned short u16; typedef unsigned short u16;
# elif UINT_MAX == 65535 # elif UINT_MAX == 65535
typedef unsigned int u16; typedef unsigned int u16;
# else # else
# undef STRICT_ALIGN # undef STRICT_ALIGN
# define STRICT_ALIGN 1 # define STRICT_ALIGN 1

View File

@ -97,195 +97,204 @@
unsigned int unsigned int
lzf_compress (const void *const in_data, unsigned int in_len, lzf_compress (const void *const in_data, unsigned int in_len,
void *out_data, unsigned int out_len void *out_data, unsigned int out_len
#if LZF_STATE_ARG #if LZF_STATE_ARG
, LZF_STATE htab , LZF_STATE htab
#endif #endif
) )
{ {
#if !LZF_STATE_ARG #if !LZF_STATE_ARG
LZF_STATE htab; LZF_STATE htab;
#endif #endif
const u8 *ip = (const u8 *)in_data; const u8 *ip = (const u8 *)in_data;
u8 *op = (u8 *)out_data; u8 *op = (u8 *)out_data;
const u8 *in_end = ip + in_len; const u8 *in_end = ip + in_len;
u8 *out_end = op + out_len; u8 *out_end = op + out_len;
const u8 *ref; const u8 *ref;
/* off requires a type wide enough to hold a general pointer difference. /* off requires a type wide enough to hold a general pointer difference.
* ISO C doesn't have that (size_t might not be enough and ptrdiff_t only * ISO C doesn't have that (size_t might not be enough and ptrdiff_t only
* works for differences within a single object). We also assume that no * works for differences within a single object). We also assume that no
* no bit pattern traps. Since the only platform that is both non-POSIX * no bit pattern traps. Since the only platform that is both non-POSIX
* and fails to support both assumptions is windows 64 bit, we make a * and fails to support both assumptions is windows 64 bit, we make a
* special workaround for it. * special workaround for it.
*/ */
#if defined (WIN32) && defined (_M_X64) #if defined (WIN32) && defined (_M_X64)
unsigned _int64 off; /* workaround for missing POSIX compliance */ unsigned _int64 off; /* workaround for missing POSIX compliance */
#else #else
unsigned long off; unsigned long off;
#endif #endif
unsigned int hval; unsigned int hval;
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
&& ref < ip /* the next test will actually take care of this, but this is faster */ && ref < ip /* the next test will actually take care of this, but this is faster */
#endif #endif
&& (off = (unsigned long)(uintptr_t)(ip - ref - 1)) < MAX_OFF && (off = (unsigned long)(uintptr_t)(ip - ref - 1)) < MAX_OFF
&& ref > (u8 *)in_data && ref > (u8 *)in_data
&& ref[2] == ip[2] && ref[2] == ip[2]
#if STRICT_ALIGN #if STRICT_ALIGN
&& ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0]) && ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0])
#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; maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
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;
} }
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 {
} *op++ = (u8)((off >> 8) + ( 7 << 5));
else *op++ = len - 7;
{
*op++ = (u8)((off >> 8) + ( 7 << 5));
*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;
# if VERY_FAST && !ULTRA_FAST # if VERY_FAST && !ULTRA_FAST
--ip; --ip;
# endif # endif
hval = FRST (ip); hval = FRST (ip);
hval = NEXT (hval, ip); hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS; htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++; ip++;
# if VERY_FAST && !ULTRA_FAST # if VERY_FAST && !ULTRA_FAST
hval = NEXT (hval, ip); hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS; htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++; ip++;
# endif # endif
#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 */
{ if (expect_false (op >= out_end))
/* one more literal byte we must copy */ { return 0; }
if (expect_false (op >= out_end))
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;
lit = 0; op++; /* start run */ 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;
lit = 0; op++; /* start run */ op++; /* start run */
} }
} }
op [- lit - 1] = lit - 1; /* end run */ op [- lit - 1] = lit - 1; /* end run */
op -= !lit; /* undo run if length is zero */ op -= !lit; /* undo run if length is zero */
return (unsigned int)((uintptr_t)op - (uintptr_t)out_data); return (unsigned int)((uintptr_t)op - (uintptr_t)out_data);
} }

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;
} }
} }
@ -113,12 +111,11 @@ static void compress_and_compare(char *key, char *value)
unsigned char htab[32][32] = {0}; unsigned char htab[32][32] = {0};
#if NVSTORE_RESET #if NVSTORE_RESET
NVStore &nvstore = NVStore::get_instance(); NVStore& nvstore = NVStore::get_instance();
#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);
@ -126,8 +123,8 @@ static void compress_and_compare(char *key, char *value)
#else #else
/*Using base64 to decode data sent from host*/ /*Using base64 to decode data sent from host*/
uint32_t lengthWritten = 0; uint32_t lengthWritten = 0;
uint32_t charsProcessed = 0; uint32_t charsProcessed = 0;
result = esfs_DecodeNBase64((const char*)value, MSG_VALUE_LEN, buffer, BUFFER_LEN, &lengthWritten, &charsProcessed); result = esfs_DecodeNBase64((const char *)value, MSG_VALUE_LEN, buffer, BUFFER_LEN, &lengthWritten, &charsProcessed);
TEST_ASSERT_EQUAL(0, result); TEST_ASSERT_EQUAL(0, result);
#endif #endif
memcpy(input_buf, buffer, BUFFER_LEN); memcpy(input_buf, buffer, BUFFER_LEN);
@ -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,15 +189,14 @@ 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);
TEST_ASSERT_EQUAL(NVSTORE_SUCCESS, result); TEST_ASSERT_EQUAL(NVSTORE_SUCCESS, result);
#else #else
/*Using base64 to encode data sending from host*/ /*Using base64 to encode data sending from host*/
result = esfs_EncodeBase64(buffer, BUFFER_LEN, (char*)out_comp_buf, sizeof(out_comp_buf)); result = esfs_EncodeBase64(buffer, BUFFER_LEN, (char *)out_comp_buf, sizeof(out_comp_buf));
TEST_ASSERT_EQUAL(NVSTORE_SUCCESS, result); TEST_ASSERT_EQUAL(NVSTORE_SUCCESS, result);
greentea_send_kv(MSG_TRNG_BUFFER, (const char *)out_comp_buf); greentea_send_kv(MSG_TRNG_BUFFER, (const char *)out_comp_buf);
#endif #endif
@ -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;
} }