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.

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;
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax