Another fix to the Graph SaveStates

This commit is contained in:
jlitewski 2024-04-25 22:06:12 -04:00
commit f1340495b3
4 changed files with 36 additions and 20 deletions

4
.gitignore vendored
View file

@ -122,3 +122,7 @@ fpga_version_info.c
# docs # docs
!doc/*.json !doc/*.json
# local codeql
_codeql*
/codeql

View file

@ -3681,8 +3681,9 @@ static int CmdTestSaveState8(const char *Cmd) {
srand(time(NULL)); srand(time(NULL));
size_t length = 64; size_t length = (rand() % 256);
uint8_t *srcBuffer = (uint8_t*)calloc(length, sizeof(uint8_t)); PrintAndLogEx(DEBUG, "Testing with length = %llu", length);
uint8_t *srcBuffer = (uint8_t*)calloc(length + 1, sizeof(uint8_t));
//Set up the source buffer with random data //Set up the source buffer with random data
for(int i = 0; i < length; i++) { for(int i = 0; i < length; i++) {
@ -3690,24 +3691,23 @@ static int CmdTestSaveState8(const char *Cmd) {
} }
buffer_savestate_t test8 = save_buffer8(srcBuffer, length); buffer_savestate_t test8 = save_buffer8(srcBuffer, length);
PrintAndLogEx(DEBUG, "Save State created, length=%llu, type=%i", test8.bufferSize, test8.type); PrintAndLogEx(DEBUG, "Save State created, length = %llu, padding = %i, type = %i", test8.bufferSize, test8.padding, test8.type);
test8.clock = rand(); test8.clock = rand();
test8.offset = rand(); test8.offset = rand();
PrintAndLogEx(DEBUG, "Save State clock=%u, offset=%u", test8.clock, test8.offset); PrintAndLogEx(DEBUG, "Save State clock = %u, offset = %u", test8.clock, test8.offset);
uint8_t *destBuffer = (uint8_t*)calloc(length, sizeof(uint8_t)); uint8_t *destBuffer = (uint8_t*)calloc(length, sizeof(uint8_t));
size_t returnedLength = restore_buffer8(test8, destBuffer); size_t returnedLength = restore_buffer8(test8, destBuffer);
if(returnedLength != length) { if(returnedLength != length) {
PrintAndLogEx(FAILED, "Return Length != Buffer Length! Expected '%llu', got '%llu", g_DemodBufferLen, returnedLength); PrintAndLogEx(DEBUG, _YELLOW_("Returned length != expected length!"));
free(srcBuffer); PrintAndLogEx(WARNING, "Returned Length = %llu Buffer Length = %llu Expected = %llu", returnedLength, test8.bufferSize, length);
free(destBuffer); } else {
return PM3_EFAILED; PrintAndLogEx(DEBUG, _GREEN_("Lengths match!") "\n");
} }
PrintAndLogEx(DEBUG, _GREEN_("Lengths match!") "\n");
for(size_t i = 0; i < returnedLength; i++) {
for(size_t i = 0; i < length; i++) {
if(srcBuffer[i] != destBuffer[i]) { if(srcBuffer[i] != destBuffer[i]) {
PrintAndLogEx(FAILED, "Buffers don't match at index %lu!, Expected %i, got %i", i, srcBuffer[i], destBuffer[i]); PrintAndLogEx(FAILED, "Buffers don't match at index %lu!, Expected %i, got %i", i, srcBuffer[i], destBuffer[i]);
free(srcBuffer); free(srcBuffer);

View file

@ -475,7 +475,8 @@ buffer_savestate_t save_buffer32(uint32_t *src, size_t length) {
buffer_savestate_t bst = { buffer_savestate_t bst = {
.type = sizeof(uint32_t), .type = sizeof(uint32_t),
.bufferSize = length, .bufferSize = length,
.buffer = savedBuffer .buffer = savedBuffer,
.padding = 0
}; };
return bst; return bst;
@ -491,7 +492,8 @@ buffer_savestate_t save_bufferS32(int32_t *src, size_t length) {
buffer_savestate_t bst = { buffer_savestate_t bst = {
.type = (sizeof(int32_t) >> 8), .type = (sizeof(int32_t) >> 8),
.bufferSize = length, .bufferSize = length,
.buffer = savedBuffer .buffer = savedBuffer,
.padding = 0
}; };
return bst; return bst;
@ -503,8 +505,11 @@ buffer_savestate_t save_buffer8(uint8_t *src, size_t length) {
// 1/4 of the size needed // 1/4 of the size needed
size_t buffSize = (length / 4); size_t buffSize = (length / 4);
PrintAndLogEx(DEBUG, "(save_buffer8) buffSize = %llu, length = %llu", buffSize, length);
if (length % 4) { if (length % 4) {
buffSize++; buffSize++;
PrintAndLogEx(DEBUG, "(save_buffer8) new buffSize = %llu", buffSize);
} }
// calloc the memory needed // calloc the memory needed
@ -520,7 +525,8 @@ buffer_savestate_t save_buffer8(uint8_t *src, size_t length) {
buffer_savestate_t bst = { buffer_savestate_t bst = {
.type = sizeof(uint8_t), .type = sizeof(uint8_t),
.bufferSize = buffSize, .bufferSize = buffSize,
.buffer = savedBuffer .buffer = savedBuffer,
.padding = ((buffSize * 4) - length)
}; };
return bst; return bst;
@ -558,13 +564,18 @@ size_t restore_buffer8(buffer_savestate_t saveState, uint8_t *dest) {
} }
size_t index = 0; size_t index = 0;
size_t length = ((saveState.bufferSize * 4) - saveState.padding);
// Unpack the array // Unpack the array
for(size_t i = 0; i < saveState.bufferSize; i++) { for(size_t i = 0; i < saveState.bufferSize; i++) {
dest[index++] = saveState.buffer[i]; dest[index++] = saveState.buffer[i];
if(index == length) break;
dest[index++] = (saveState.buffer[i] >> 8) & 0xFF; dest[index++] = (saveState.buffer[i] >> 8) & 0xFF;
if(index == length) break;
dest[index++] = (saveState.buffer[i] >> 16) & 0xFF; dest[index++] = (saveState.buffer[i] >> 16) & 0xFF;
if(index == length) break;
dest[index++] = (saveState.buffer[i] >> 24) & 0xFF; dest[index++] = (saveState.buffer[i] >> 24) & 0xFF;
if(index == length) break;
} }
return index; return index;

View file

@ -26,11 +26,12 @@ extern "C" {
#endif #endif
typedef struct { typedef struct {
const uint8_t type; //Used for sanity checks const uint8_t type; // Used for sanity checks
const uint32_t *buffer; const uint32_t *buffer; // The storage buffer for this save state
const size_t bufferSize; const size_t bufferSize; // The size of the buffer
uint32_t offset; const uint8_t padding; // The amount of padding at the end of the buffer, if needed
uint32_t clock; //Not used by all buffers uint32_t offset; // (optional) Any offset the buffer needs after restoring
uint32_t clock; // (optional) Clock data for the buffer
} buffer_savestate_t; } buffer_savestate_t;
void AppendGraph(bool redraw, uint16_t clock, int bit); void AppendGraph(bool redraw, uint16_t clock, int bit);