Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple null terminated strings in single buffer in C

In need to craft a string with the following format and place it into a single buffer[1000]. Note that \x00 is the null terminator.

@/foo\x00ACTION=add\x00SUBSYSTEM=block\x00DEVPATH=/devices/platform/goldfish_mmc.0\x00MAJOR=command\x00MINOR=1\x00DEVTYPE=harder\x00PARTN=1

So in essence I need to pack following null terminated strings into a single buffer

@/foo  
ACTION=add  
SUBSYSTEM=block  
DEVPATH=/devices/platform/goldfish_mmc.0  
MAJOR=command  
MINOR=1  
DEVTYPE=harder  
PARTN=1  

How might I go about doing this?

like image 353
Justin Bramel Avatar asked Jan 03 '23 03:01

Justin Bramel


1 Answers

You'll need to copy each string in one at a time, keeping track of where the last copy stopped and starting just after that for the next one.

char *p = buffer;
strcpy(p, "@/foo");
p += strlen(p) + 1;
strcpy(p, "ACTION=add");
p += strlen(p) + 1;
...
like image 144
dbush Avatar answered Jan 11 '23 23:01

dbush