So today I ran into a little bit of a snag, I was trying to copy an unknown amount of data into a buffer and then strcat That into another buffer. However during this I had a slight issue where the unknown data coming in the first buffer even though it fits into the destination buffer once it gets strcat into the 2nd buffer it may not. So I had identify the size of the unknown data, the issue was that I was pulling this data during runtime via SPIflash so I crafted this nice little function below to give me an idea of the size of the data that is in the 2nd buffer then in my main function count down the remaining bytes as I take the data in to the first unknown buffer, then chopping off the rest to prevent the chip from crashing in a buffer overflow.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
uint16 StrLenFlash(uint32_t address, char * Terminator) { int i = 0; int index = 0; int sizeofTerminator = os_strlen(Terminator); char buffer[4]; uint16 addressindex = 0; while(true) { system_soft_wdt_feed(); if (spi_flash_read(address+(addressindex*4), buffer, 4) != 0) { os_printf("Read failed at offset=0x%02x index=%d\r\n", address, index); return 0; } addressindex++; for (i=0; i<=3; i++) { if (buffer[i] == *(Terminator+index)) { //os_printf("Found match %c | %c\r\n", buffer[i], *(Terminator+index)); index++; } else { index = 0; } if (index == sizeofTerminator) { //os_printf("EOF Found"); return addressindex*4; } } } } |