G
Grey Alien
I am retrieving BLOBs from a database into an ADT (kinda like a tagged
union) that is capable of holding (null-terminated) strings amongst many
other data types. The BLOBs are represented as a byte stream (i.e. a
char array), but will be stored in the ADT as a string.
The existing API for retrieving data from the ADT assumes that all
strings are null terminated. However, I have a new set of functions that
will retrieve the BLOBs from the ADT, but I want the BLOBs to be
stored in such a way that the extension of storing BLOBs in the ADT will
be "backward compatable" in the sense that the existing functions (that
expect null terminated strings) will continue to "work" (return
meaningless strings rather than causing the app to crash).
In order to do this, I have to convert the byte stream into a "safe"
(i.e. null terminated) string. This is how I have implemented this
requirement:
char * make_safestring(const char* val)
{
size_t size = (val/sizeof(char)) + 1 ;
char * safestr = calloc(size, sizeof(char)) ;
memmove(safestr, val, size);
safestr[size] = '\0' ;
return safestr ;
}
Q1). Will this work as planned - i.e. will allow a byte stream to be
"converted" (i.e. padded with a '\0') so that functions that expect a
null terminated strings will continue to work
Q2). Have I missed anything ?
union) that is capable of holding (null-terminated) strings amongst many
other data types. The BLOBs are represented as a byte stream (i.e. a
char array), but will be stored in the ADT as a string.
The existing API for retrieving data from the ADT assumes that all
strings are null terminated. However, I have a new set of functions that
will retrieve the BLOBs from the ADT, but I want the BLOBs to be
stored in such a way that the extension of storing BLOBs in the ADT will
be "backward compatable" in the sense that the existing functions (that
expect null terminated strings) will continue to "work" (return
meaningless strings rather than causing the app to crash).
In order to do this, I have to convert the byte stream into a "safe"
(i.e. null terminated) string. This is how I have implemented this
requirement:
char * make_safestring(const char* val)
{
size_t size = (val/sizeof(char)) + 1 ;
char * safestr = calloc(size, sizeof(char)) ;
memmove(safestr, val, size);
safestr[size] = '\0' ;
return safestr ;
}
Q1). Will this work as planned - i.e. will allow a byte stream to be
"converted" (i.e. padded with a '\0') so that functions that expect a
null terminated strings will continue to work
Q2). Have I missed anything ?