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"
using namespace std;
static char IntToBase64Char(uint8_t intVal)
{
const char* base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char *base64Digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
return base64Digits[intVal & 0x3F];
}
#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;
}
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;
{ *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;
{ *intVal = 62; }
else if (base64 == '/')
*intVal = 63;
{ *intVal = 63; }
else if (base64 == '=')
*intVal = BASE_64_PAD;
else
{
{ *intVal = BASE_64_PAD; }
else {
return BASE64_ERROR;
}
return BASE64_SUCCESS;
}
base64_result_e esfs_DecodeNBase64(const char* string,
uint32_t stringMaxSize,
void* buffer,
uint32_t bufferSize,
uint32_t* lengthWritten,
uint32_t* charsProcessed)
base64_result_e esfs_DecodeNBase64(const char *string,
uint32_t stringMaxSize,
void *buffer,
uint32_t bufferSize,
uint32_t *lengthWritten,
uint32_t *charsProcessed)
{
base64_result_e result = BASE64_SUCCESS;
uint32_t bitOffset =0;
uint8_t* writePtr = (uint8_t*)buffer;
uint8_t* bufferEnd = (uint8_t*)buffer + bufferSize;
uint32_t bitOffset = 0;
uint8_t *writePtr = (uint8_t *)buffer;
uint8_t *bufferEnd = (uint8_t *)buffer + bufferSize;
uint8_t tempVal = 0;
uint32_t currPos = 0;
uint32_t localBytesWritten = 0;
uint32_t localCharsProcessed = 0;
bool isEndOfString = false;
if ((NULL == string) || (NULL == buffer) || (bufferSize == 0))
{
if ((NULL == string) || (NULL == buffer) || (bufferSize == 0)) {
return BASE64_INVALID_PARAMETER;
}
@ -62,105 +75,94 @@ base64_result_e esfs_DecodeNBase64(const char* string,
while (( string[currPos] != 0 ) &&
( currPos < stringMaxSize ) &&
( writePtr < bufferEnd ) &&
( !isEndOfString ))
{
( !isEndOfString )) {
uint8_t val;
if (string[currPos] == 0 || currPos >= stringMaxSize)
break;
{ break; }
result = Base64CharToInt(string[currPos++], &val);
if (result != BASE64_SUCCESS)
break;
{ break; }
if (val != BASE_64_PAD)
{
if(bitOffset <= 2)
{
if (val != BASE_64_PAD) {
if (bitOffset <= 2) {
tempVal |= val << (2 - bitOffset);
if (bitOffset == 2)
{
if (bitOffset == 2) {
*writePtr++ = tempVal;
tempVal = 0;
}
}
else
{
} else {
*writePtr++ = (uint8_t)(tempVal | (val >> (bitOffset - 2)));
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
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)
return BASE64_ERROR; // Incorrect padding
{ return BASE64_ERROR; } // Incorrect padding
}
bitOffset = (bitOffset + 6) & 0x7;
if (bitOffset == 0)
{
localBytesWritten = (uint32_t)(writePtr - (uint8_t*)buffer);
if (bitOffset == 0) {
localBytesWritten = (uint32_t)(writePtr - (uint8_t *)buffer);
localCharsProcessed = currPos;
}
}
if (charsProcessed == NULL)
localBytesWritten = (uint32_t)(writePtr - (uint8_t*)buffer);
else
*charsProcessed = localCharsProcessed;
{ localBytesWritten = (uint32_t)(writePtr - (uint8_t *)buffer); }
else
{ *charsProcessed = localCharsProcessed; }
if (lengthWritten != NULL)
*lengthWritten = localBytesWritten;
{ *lengthWritten = 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.
if (( result == BASE64_SUCCESS ) &&
( !isEndOfString ) &&
( string[currPos] != '=' ) &&
( string[currPos] != 0 ) &&
( currPos < stringMaxSize) )
return BASE64_BUFFER_TOO_SMALL;
( !isEndOfString ) &&
( string[currPos] != '=' ) &&
( string[currPos] != 0 ) &&
( currPos < stringMaxSize) )
{ return BASE64_BUFFER_TOO_SMALL; }
if (result != BASE64_SUCCESS)
return result;
{ return result; }
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;
const uint8_t* readPtr = (const uint8_t*)buffer;
const uint8_t* bufferEnd = (const uint8_t*)buffer + bufferSize;
const uint8_t *readPtr = (const uint8_t *)buffer;
const uint8_t *bufferEnd = (const uint8_t *)buffer + bufferSize;
char* writePtr = string;
char* stringEnd = string + stringSize - 1;
char *writePtr = string;
char *stringEnd = string + stringSize - 1;
if ((NULL == string) || (NULL == buffer) || (stringSize == 0))
return BASE64_INVALID_PARAMETER;
{ return BASE64_INVALID_PARAMETER; }
stringSize--;
while(readPtr < bufferEnd && writePtr < stringEnd)
{
while (readPtr < bufferEnd && writePtr < stringEnd) {
uint8_t tempVal = 0;
switch (bitOffset)
{
switch (bitOffset) {
case 0:
*writePtr++ = IntToBase64Char(*readPtr >> 2); // take upper 6 bits
break;
case 6:
tempVal = *readPtr++ << 4;
if (readPtr < bufferEnd)
tempVal |= *readPtr >> 4;
{ tempVal |= *readPtr >> 4; }
*writePtr++ = IntToBase64Char(tempVal);
break;
case 4:
tempVal = *readPtr++ << 2;
if (readPtr < bufferEnd)
tempVal |= *readPtr >> 6;
{ tempVal |= *readPtr >> 6; }
*writePtr++ = IntToBase64Char(tempVal);
break;
case 2:
@ -171,15 +173,14 @@ base64_result_e esfs_EncodeBase64(const void* buffer, uint32_t bufferSize, char*
}
bitOffset = (bitOffset + 6) & 0x7;
}
while (bitOffset > 0 && writePtr < stringEnd)
{
while (bitOffset > 0 && writePtr < stringEnd) {
*writePtr++ = '=';
bitOffset = (bitOffset + 6) & 0x7;
}
*writePtr = 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 <stdlib.h>
#include <string>
@ -7,10 +23,10 @@ typedef enum {
BASE64_INVALID_PARAMETER = 1,
BASE64_BUFFER_TOO_SMALL = 2,
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_DecodeNBase64(const char* string, uint32_t stringMaxSize, void* buffer, uint32_t bufferSize,
uint32_t* lengthWritten, uint32_t* charsProcessed);
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,
uint32_t *lengthWritten, uint32_t *charsProcessed);

View File

@ -1,16 +1,16 @@
/*
* Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de>
*
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
@ -73,7 +73,7 @@
* and lzf_c.c.
*
*/
unsigned int
unsigned int
lzf_compress (const void *const in_data, unsigned int in_len,
void *out_data, unsigned int out_len,
unsigned char **htab);
@ -93,7 +93,7 @@ lzf_compress (const void *const in_data, unsigned int in_len,
*
* This function is very fast, about as fast as a copying loop.
*/
unsigned int
unsigned int
lzf_decompress (const void *const in_data, unsigned int in_len,
void *out_data, unsigned int out_len);

View File

@ -1,16 +1,16 @@
/*
* Copyright (c) 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de>
*
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
@ -157,10 +157,10 @@ typedef unsigned char u8;
#if LZF_USE_OFFSETS
# define LZF_HSLOT_BIAS ((const u8 *)in_data)
typedef unsigned int LZF_HSLOT;
typedef unsigned int LZF_HSLOT;
#else
# define LZF_HSLOT_BIAS 0
typedef const u8 *LZF_HSLOT;
typedef const u8 *LZF_HSLOT;
#endif
typedef LZF_HSLOT LZF_STATE[1 << (HLOG)];
@ -168,9 +168,9 @@ typedef LZF_HSLOT LZF_STATE[1 << (HLOG)];
#if !STRICT_ALIGN
/* for unaligned accesses we need a 16 bit datatype. */
# if USHRT_MAX == 65535
typedef unsigned short u16;
typedef unsigned short u16;
# elif UINT_MAX == 65535
typedef unsigned int u16;
typedef unsigned int u16;
# else
# undef STRICT_ALIGN
# define STRICT_ALIGN 1

View File

@ -1,16 +1,16 @@
/*
* Copyright (c) 2000-2010 Marc Alexander Lehmann <schmorp@schmorp.de>
*
*
* Redistribution and use in source and binary forms, with or without modifica-
* tion, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
@ -97,195 +97,204 @@
unsigned int
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
, LZF_STATE htab
#endif
)
)
{
#if !LZF_STATE_ARG
LZF_STATE htab;
LZF_STATE htab;
#endif
const u8 *ip = (const u8 *)in_data;
u8 *op = (u8 *)out_data;
const u8 *in_end = ip + in_len;
u8 *out_end = op + out_len;
const u8 *ref;
const u8 *ip = (const u8 *)in_data;
u8 *op = (u8 *)out_data;
const u8 *in_end = ip + in_len;
u8 *out_end = op + out_len;
const u8 *ref;
/* 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
* 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
* and fails to support both assumptions is windows 64 bit, we make a
* special workaround for it.
*/
/* 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
* 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
* and fails to support both assumptions is windows 64 bit, we make a
* special workaround for it.
*/
#if defined (WIN32) && defined (_M_X64)
unsigned _int64 off; /* workaround for missing POSIX compliance */
unsigned _int64 off; /* workaround for missing POSIX compliance */
#else
unsigned long off;
unsigned long off;
#endif
unsigned int hval;
int lit;
unsigned int hval;
int lit;
if (!in_len || !out_len)
return 0;
if (!in_len || !out_len)
{ return 0; }
#if INIT_HTAB
memset (htab, 0, sizeof (htab));
memset (htab, 0, sizeof (htab));
#endif
lit = 0; op++; /* start run */
lit = 0;
op++; /* start run */
hval = FRST (ip);
while (ip < in_end - 2)
{
LZF_HSLOT *hslot;
hval = FRST (ip);
while (ip < in_end - 2) {
LZF_HSLOT *hslot;
hval = NEXT (hval, ip);
hslot = htab + IDX (hval);
ref = *hslot + LZF_HSLOT_BIAS; *hslot = ip - LZF_HSLOT_BIAS;
hval = NEXT (hval, ip);
hslot = htab + IDX (hval);
ref = *hslot + LZF_HSLOT_BIAS;
*hslot = ip - LZF_HSLOT_BIAS;
if (1
if (1
#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
&& (off = (unsigned long)(uintptr_t)(ip - ref - 1)) < MAX_OFF
&& ref > (u8 *)in_data
&& ref[2] == ip[2]
&& (off = (unsigned long)(uintptr_t)(ip - ref - 1)) < MAX_OFF
&& ref > (u8 *)in_data
&& ref[2] == ip[2]
#if STRICT_ALIGN
&& ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0])
&& ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0])
#else
&& *(u16 *)ref == *(u16 *)ip
&& *(u16 *)ref == *(u16 *)ip
#endif
)
{
/* match found at *ref++ */
unsigned int len = 2;
unsigned int maxlen = (unsigned int)((uintptr_t)in_end - (uintptr_t)ip) - len;
maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
) {
/* match found at *ref++ */
unsigned int len = 2;
unsigned int maxlen = (unsigned int)((uintptr_t)in_end - (uintptr_t)ip) - len;
maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
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 */
return 0;
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 */
{ return 0; }
op [- lit - 1] = lit - 1; /* stop run */
op -= !lit; /* undo run if length is zero */
op [- lit - 1] = lit - 1; /* stop run */
op -= !lit; /* undo run if length is zero */
for (;;)
{
if (expect_true (maxlen > 16))
{
len++; 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;
for (;;) {
if (expect_true (maxlen > 16)) {
len++;
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++; 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++;
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++; 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++;
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++; 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++;
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; }
}
do
len++;
while (len < maxlen && ref[len] == ip[len]);
do
{ len++; }
while (len < maxlen && ref[len] == ip[len]);
break;
break;
}
len -= 2; /* len is now #octets - 1 */
ip++;
len -= 2; /* len is now #octets - 1 */
ip++;
if (len < 7)
{
*op++ = (u8)((off >> 8) + (len << 5));
}
else
{
*op++ = (u8)((off >> 8) + ( 7 << 5));
*op++ = len - 7;
if (len < 7) {
*op++ = (u8)((off >> 8) + (len << 5));
} else {
*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))
break;
if (expect_false (ip >= in_end - 2))
{ break; }
#if ULTRA_FAST || VERY_FAST
--ip;
--ip;
# if VERY_FAST && !ULTRA_FAST
--ip;
--ip;
# endif
hval = FRST (ip);
hval = FRST (ip);
hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++;
hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++;
# if VERY_FAST && !ULTRA_FAST
hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++;
hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++;
# endif
#else
ip -= len + 1;
ip -= len + 1;
do
{
hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++;
}
while (len--);
do {
hval = NEXT (hval, ip);
htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
ip++;
} while (len--);
#endif
}
else
{
/* one more literal byte we must copy */
if (expect_false (op >= out_end))
return 0;
} else {
/* one more literal byte we must copy */
if (expect_false (op >= out_end))
{ return 0; }
lit++; *op++ = *ip++;
lit++;
*op++ = *ip++;
if (expect_false (lit == MAX_LIT))
{
op [- lit - 1] = lit - 1; /* stop run */
lit = 0; op++; /* start run */
if (expect_false (lit == MAX_LIT)) {
op [- lit - 1] = lit - 1; /* stop run */
lit = 0;
op++; /* start run */
}
}
}
if (op + 3 > out_end) /* at most 3 bytes can be missing here */
return 0;
if (op + 3 > out_end) /* at most 3 bytes can be missing here */
{ return 0; }
while (ip < in_end)
{
lit++; *op++ = *ip++;
while (ip < in_end) {
lit++;
*op++ = *ip++;
if (expect_false (lit == MAX_LIT))
{
op [- lit - 1] = lit - 1; /* stop run */
lit = 0; op++; /* start run */
if (expect_false (lit == MAX_LIT)) {
op [- lit - 1] = lit - 1; /* stop run */
lit = 0;
op++; /* start run */
}
}
op [- lit - 1] = lit - 1; /* end run */
op -= !lit; /* undo run if length is zero */
op [- lit - 1] = lit - 1; /* end run */
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

@ -15,23 +15,23 @@
*/
/*
* The test is based on the assumption that trng will generate random data, random so
* 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.
*
* 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
* we will reset the device.
* 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
* we will reset the device.
* In second part we will generate a trng buffer with a different buffer size and try to
* compress it.
* In the third part we will again generate a trng buffer to see that the same trng output
* In the third part we will again generate a trng buffer to see that the same trng output
* is not generated as the stored trng buffer from part one (before reseting), the new trng data will
* be concatenated to the trng data from the first part and then try to compress it
* be concatenated to the trng data from the first part and then try to compress it
* together, if there are similar patterns the compression will succeed.
*
* We need to store and load the first part data before and after reset, the mechanism
* we chose is NVstore, mainly because its simplicity and the fact it is not platform
* dependent, in case a specific board does not support NVstore we will use the
* We need to store and load the first part data before and after reset, the mechanism
* we chose is NVstore, mainly because its simplicity and the fact it is not platform
* dependent, in case a specific board does not support NVstore we will use the
* mbed greentea platform for sending and receving the data from the device to the
* host running the test and back, the problem with this mechanism is that it doesn't handle
* well certain characters, especially non ASCII ones, so we used the base64 algorithm
@ -85,15 +85,13 @@ static int fill_buffer_trng(uint8_t *buffer, trng_t *trng_obj, size_t trng_len)
trng_init(trng_obj);
memset(buffer, 0, BUFFER_LEN);
while (true)
{
while (true) {
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!");
temp_size += output_length;
temp_in_buf += output_length;
trng_len -= output_length;
if (temp_size >= trng_len)
{
if (temp_size >= trng_len) {
break;
}
}
@ -113,12 +111,11 @@ static void compress_and_compare(char *key, char *value)
unsigned char htab[32][32] = {0};
#if NVSTORE_RESET
NVStore &nvstore = NVStore::get_instance();
NVStore& nvstore = NVStore::get_instance();
#endif
/*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
uint16_t actual = 0;
result = nvstore.get(NVKEY, sizeof(buffer), buffer, actual);
@ -126,8 +123,8 @@ static void compress_and_compare(char *key, char *value)
#else
/*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);
uint32_t charsProcessed = 0;
result = esfs_DecodeNBase64((const char *)value, MSG_VALUE_LEN, buffer, BUFFER_LEN, &lengthWritten, &charsProcessed);
TEST_ASSERT_EQUAL(0, result);
#endif
memcpy(input_buf, buffer, BUFFER_LEN);
@ -138,45 +135,36 @@ static void compress_and_compare(char *key, char *value)
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*/
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0)
{
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) {
printf("\n******TRNG_TEST_STEP1*****\n");
out_comp_buf_len = BUFFER_LEN + (BUFFER_LEN / 4);
comp_res = lzf_compress((const void *)buffer,
(unsigned int)sizeof(buffer),
(void *)out_comp_buf,
out_comp_buf_len,
comp_res = lzf_compress((const void *)buffer,
(unsigned int)sizeof(buffer),
(void *)out_comp_buf,
out_comp_buf_len,
(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));
}
else
{
} else {
printf("trng_get_bytes for buffer size %d was unsuccessful", sizeof(buffer));
TEST_ASSERT(false);
}
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");
result = fill_buffer_trng(temp_buff, &trng_obj, sizeof(temp_buff));
TEST_ASSERT_EQUAL(0, result);
out_comp_buf_len = 2 * BUFFER_LEN + (BUFFER_LEN / 2);
comp_res = lzf_compress((const void *)temp_buff,
(unsigned int)sizeof(temp_buff),
(void *)out_comp_buf,
out_comp_buf_len,
comp_res = lzf_compress((const void *)temp_buff,
(unsigned int)sizeof(temp_buff),
(void *)out_comp_buf,
out_comp_buf_len,
(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));
}
else
{
} else {
printf("trng_get_bytes for buffer size %d was unsuccessful", sizeof(temp_buff));
TEST_ASSERT(false);
}
@ -185,18 +173,15 @@ static void compress_and_compare(char *key, char *value)
printf("******TRNG_TEST_STEP3*****\n");
memcpy(input_buf + BUFFER_LEN, buffer, BUFFER_LEN);
comp_res = lzf_compress((const void *)input_buf,
(unsigned int)sizeof(input_buf),
(void *)out_comp_buf,
out_comp_buf_len,
comp_res = lzf_compress((const void *)input_buf,
(unsigned int)sizeof(input_buf),
(void *)out_comp_buf,
out_comp_buf_len,
(unsigned char **)htab);
if (comp_res >= BUFFER_LEN)
{
if (comp_res >= BUFFER_LEN) {
printf("compression for concatenated buffer after reset was successful");
}
else
{
} else {
printf("compression for concatenated buffer after reset was unsuccessful");
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*/
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0)
{
if (strcmp(key, MSG_TRNG_TEST_STEP1) == 0) {
int result = 0;
#if NVSTORE_RESET
result = nvstore.set(NVKEY, sizeof(buffer), buffer);
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 = esfs_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
@ -235,22 +219,21 @@ void trng_test()
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*/
compress_and_compare(key, value);
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
try to compress them both*/
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);
return STATUS_CONTINUE;
}