Teensy 4.1: Dealing with Base64 Strings - handling and maximum length?

jimmie

Well-known member
I have a PC application which acts as a TCP Client. Periodically, it sends a Base64 String to a TCP server. It is a long string because it contains an image. I can reduce the size of that string by reducing the image resolution.

I would like to use a Teensy 4.1 to act as the TCP Server. What is the maximum String length I can receive?

All I need is to append a small string to the string I receive and then send the new string to another TCP client.

I know that it is not recommended to use Strings in the Teensy code. Where can I find code that shows the proper way of handling this Base64 string? What is the maximum string length that can be handled?

Thanks in advance.
 
Do you need to store the string? In that case you are limited by the amout of RAM available, which depends on how much you need for other purposes.

Can you stream the string directly to the TCP client and not have to store all of it?
 
Thank you. I did not realize that one can stream directly without first saving the String.

So, in this case, one would send each byte as it is received? Is there any example code?

Thanks again for your time.
 
Normally with BASE64, you would process 4 encoded bytes at a time, decoding them back to the original 3 bytes.

For example code, check out the first answer on this Stack Overflow question.

https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c

In the base64_decode() function you can see this inner code:

Code:
        uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];

        uint32_t triple = (sextet_a << 3 * 6)
        + (sextet_b << 2 * 6)
        + (sextet_c << 1 * 6)
        + (sextet_d << 0 * 6);

        if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;

So you could use as little as 4 bytes of memory.

Reading only 4 bytes at a time and writing output data 3 bytes at a time might be way to use the least memory, but it might not be so efficient, because reading from a stream involves some overhead which is roughly the same for 4 bytes as for a much larger read. You could pretty easily read something like 1024 bytes at a time (so you suffer the overhead of reading from a stream much less) and run this code 256 times to decode it to the original 768 bytes, and then repeat for each 1024 byte chunk of the input.
 
Thank you very much Paul. This code is very helpful.

I was not aware that one can add memory to the Teensy 4.1. I prefer to read the entire stream because I need to extract some limited information from it before re-sending it to another TCP client.

I estimate my String to be a maximum of about 4 million bytes. So in this case, is it better to use a Teensy 4.1 with an additional 16 MB PSRAM or 8 MB of PSRAM + 128 MB of Flash?

Thanks in advance for your time.
 
Back
Top