I'm trying to use memcpy as a strncat since i need to preserve the null terminators in the strings I'm having a rough time trying to understand what i'm missing in this code, I don't know how to check it but i don't think the final string is correct, this is the code:
char *reass_tcp = malloc(sizeof(char) * 22 + 1);
char *a = "hel\0lo";
char *b = "W\0orld";
char *c = "wha\0ts\0up\r"; //last chunk of payload so it has the carriage
//return which i included here
int len = 0;
memcpy(reass_tcp, a, actualsize(a));
len += actualsize(a);
memcpy(reass_tcp+len, b, actualsize(b));
len += actualsize(b);
memcpy(reass_tcp+len, c, actualsize(c));
return reass_tcp;
The original code is a bit more complex but it reduces to the problem above. I have access to a function actualsize(): this function returns the actual size of the string including the middle null terminators but not the one included in all C-strings (the one at the end of the string). It does however include the carriage return contained at the end of all tcp packets. e.g.
a = "hel\0lo";
actualsize(a) --> returns 6
c = "wha\0ts\0up\r"
actualsize(c) --> returns 10
UPDATE: this is what the scenario looks like:
- i have 3 tcp payload chunks where the last one only contains the carriage return
- i manually added a null terminator to all chunks
- my goal is to combine them, including the null terminators contained in-between the single strings, to form the original payload