[Horus] Memory overflow check added in compress / uncompress methods to BACKUP RAM
This commit is contained in:
@@ -50,14 +50,14 @@ void rambackupWrite()
|
|||||||
{
|
{
|
||||||
copyRadioData(&ramBackupUncompressed.radio, &g_eeGeneral);
|
copyRadioData(&ramBackupUncompressed.radio, &g_eeGeneral);
|
||||||
copyModelData(&ramBackupUncompressed.model, &g_model);
|
copyModelData(&ramBackupUncompressed.model, &g_model);
|
||||||
ramBackup->size = compress(ramBackup->data, (const uint8_t *)&ramBackupUncompressed, sizeof(ramBackupUncompressed));
|
ramBackup->size = compress(ramBackup->data, 4094, (const uint8_t *)&ramBackupUncompressed, sizeof(ramBackupUncompressed));
|
||||||
TRACE("RamBackupWrite sdsize=%d backupsize=%d rlcsize=%d", sizeof(ModelData)+sizeof(RadioData), sizeof(Backup::RamBackupUncompressed), ramBackup->size);
|
TRACE("RamBackupWrite sdsize=%d backupsize=%d rlcsize=%d", sizeof(ModelData)+sizeof(RadioData), sizeof(Backup::RamBackupUncompressed), ramBackup->size);
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
// TODO move this code to non regression tests
|
// TODO move this code to non regression tests
|
||||||
Backup::RamBackupUncompressed ramBackupRestored;
|
Backup::RamBackupUncompressed ramBackupRestored;
|
||||||
if (uncompress((uint8_t *)&ramBackupRestored, ramBackup->data, ramBackup->size) != sizeof(ramBackupUncompressed))
|
if (uncompress((uint8_t *)&ramBackupRestored, sizeof(ramBackupRestored), ramBackup->data, ramBackup->size) != sizeof(ramBackupUncompressed))
|
||||||
TRACE("ERROR size");
|
TRACE("ERROR uncompress");
|
||||||
if (memcmp(&ramBackupUncompressed, &ramBackupRestored, sizeof(ramBackupUncompressed)) != 0)
|
if (memcmp(&ramBackupUncompressed, &ramBackupRestored, sizeof(ramBackupUncompressed)) != 0)
|
||||||
TRACE("ERROR restore");
|
TRACE("ERROR restore");
|
||||||
#endif
|
#endif
|
||||||
@@ -68,7 +68,7 @@ bool rambackupRestore()
|
|||||||
if (ramBackup->size == 0)
|
if (ramBackup->size == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (uncompress((uint8_t *)&ramBackupUncompressed, ramBackup->data, ramBackup->size) != sizeof(ramBackupUncompressed))
|
if (uncompress((uint8_t *)&ramBackupUncompressed, sizeof(ramBackupUncompressed), ramBackup->data, ramBackup->size) != sizeof(ramBackupUncompressed))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
memset(&g_eeGeneral, 0, sizeof(g_eeGeneral));
|
memset(&g_eeGeneral, 0, sizeof(g_eeGeneral));
|
||||||
|
|||||||
+37
-12
@@ -22,7 +22,13 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
unsigned int compress(uint8_t * dst, const uint8_t * src, unsigned int len)
|
#define CHECK_DST_SIZE() \
|
||||||
|
if (cur-dst >= dstsize) { \
|
||||||
|
TRACE("RLC encoding size too big"); \
|
||||||
|
return 0; \
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int compress(uint8_t * dst, unsigned int dstsize, const uint8_t * src, unsigned int srcsize)
|
||||||
{
|
{
|
||||||
uint8_t * cur = dst;
|
uint8_t * cur = dst;
|
||||||
bool run0 = (src[0] == 0);
|
bool run0 = (src[0] == 0);
|
||||||
@@ -31,28 +37,34 @@ unsigned int compress(uint8_t * dst, const uint8_t * src, unsigned int len)
|
|||||||
|
|
||||||
for (unsigned int i=1; 1; i++) {
|
for (unsigned int i=1; 1; i++) {
|
||||||
bool cur0 = (src[i] == 0);
|
bool cur0 = (src[i] == 0);
|
||||||
if (i==len || cur0!=run0 || cnt==0x3f || (cnt0 && cnt==0xf)) {
|
if (i==srcsize || cur0!=run0 || cnt==0x3f || (cnt0 && cnt==0xf)) {
|
||||||
if (run0) {
|
if (run0) {
|
||||||
assert(cnt0==0);
|
assert(cnt0==0);
|
||||||
if (cnt<8 && i!=len)
|
if (cnt<8 && i!=srcsize) {
|
||||||
cnt0 = cnt;
|
cnt0 = cnt;
|
||||||
else
|
}
|
||||||
*cur++ = (cnt|0x40);
|
else {
|
||||||
|
CHECK_DST_SIZE();
|
||||||
|
*cur++ = (cnt | 0x40);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (cnt0) {
|
if (cnt0) {
|
||||||
|
CHECK_DST_SIZE();
|
||||||
*cur++ = (0x80 | (cnt0<<4) | cnt);
|
*cur++ = (0x80 | (cnt0<<4) | cnt);
|
||||||
cnt0 = 0;
|
cnt0 = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
CHECK_DST_SIZE();
|
||||||
*cur++ = cnt;
|
*cur++ = cnt;
|
||||||
}
|
}
|
||||||
for (int j=0; j<cnt; j++) {
|
for (int j=0; j<cnt; j++) {
|
||||||
|
CHECK_DST_SIZE();
|
||||||
*cur++ = src[i - cnt + j];
|
*cur++ = src[i - cnt + j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cnt = 0;
|
cnt = 0;
|
||||||
if (i==len) break;
|
if (i==srcsize) break;
|
||||||
run0 = cur0;
|
run0 = cur0;
|
||||||
}
|
}
|
||||||
cnt++;
|
cnt++;
|
||||||
@@ -61,7 +73,14 @@ unsigned int compress(uint8_t * dst, const uint8_t * src, unsigned int len)
|
|||||||
return cur-dst;
|
return cur-dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int uncompress(uint8_t * dst, const uint8_t * src, unsigned int len)
|
#undef CHECK_DST_SIZE
|
||||||
|
#define CHECK_DST_SIZE() \
|
||||||
|
if (cur-dst >= dstsize) { \
|
||||||
|
TRACE("RLC decoding size too big"); \
|
||||||
|
return 0; \
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int uncompress(uint8_t * dst, unsigned int dstsize, const uint8_t * src, unsigned int srcsize)
|
||||||
{
|
{
|
||||||
uint8_t * cur = dst;
|
uint8_t * cur = dst;
|
||||||
uint8_t zeroes = 0;
|
uint8_t zeroes = 0;
|
||||||
@@ -70,22 +89,26 @@ unsigned int uncompress(uint8_t * dst, const uint8_t * src, unsigned int len)
|
|||||||
for( ; 1; ) {
|
for( ; 1; ) {
|
||||||
if (zeroes > 0) {
|
if (zeroes > 0) {
|
||||||
for (int i = 0; i < zeroes; i++) {
|
for (int i = 0; i < zeroes; i++) {
|
||||||
|
CHECK_DST_SIZE();
|
||||||
*cur++ = 0;
|
*cur++ = 0;
|
||||||
}
|
}
|
||||||
zeroes = 0;
|
zeroes = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len == 0)
|
if (srcsize == 0) {
|
||||||
return cur-dst;
|
return cur - dst;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i=0; i<bRlc; i++) {
|
for (int i=0; i<bRlc; i++) {
|
||||||
|
CHECK_DST_SIZE();
|
||||||
*cur++ = *src++;
|
*cur++ = *src++;
|
||||||
if (--len == 0)
|
if (--srcsize == 0) {
|
||||||
return cur-dst;
|
return cur - dst;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bRlc = *src++;
|
bRlc = *src++;
|
||||||
--len;
|
--srcsize;
|
||||||
|
|
||||||
if (!(bRlc & 0x7f)) {
|
if (!(bRlc & 0x7f)) {
|
||||||
TRACE("RLC decoding error");
|
TRACE("RLC decoding error");
|
||||||
@@ -102,3 +125,5 @@ unsigned int uncompress(uint8_t * dst, const uint8_t * src, unsigned int len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#undef CHECK_DST_SIZE
|
||||||
@@ -67,8 +67,8 @@ void postModelLoad(bool newModel);
|
|||||||
#if defined(RAMBACKUP)
|
#if defined(RAMBACKUP)
|
||||||
void rambackupWrite();
|
void rambackupWrite();
|
||||||
bool rambackupRestore();
|
bool rambackupRestore();
|
||||||
unsigned int compress(uint8_t * dst, const uint8_t * src, unsigned int len);
|
unsigned int compress(uint8_t * dst, unsigned int dstsize, const uint8_t * src, unsigned int len);
|
||||||
unsigned int uncompress(uint8_t * dst, const uint8_t * src, unsigned int len);
|
unsigned int uncompress(uint8_t * dst, unsigned int dstsize, const uint8_t * src, unsigned int len);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif // _STORAGE_H_
|
#endif // _STORAGE_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user