When a system resource must freeze a ``char*'', it uses one of
these functions. Both of them copy the string value into the given
buffer. The first one will also fix the char* passed to be the
relative position in buffer where the copy starts.
<Off string freeze utility. >= (U->) // Freezes str into buf at curbuf. // Adjusts curbuf and avail. // Returns FALSE when not enough space is available. boolean_t freeze(const char *&str, char *buf, char *&curbuf, size_t &avail); // Freezes str into buf at curbuf. // Returns FALSE when not enough space is available. boolean_t freeze(const char *str, char *&curbuf, size_t &avail);
<Off string freeze utility dependencies. >= (U->) [D->] #include <flux/types.h> // for boolean_t, size_t et al.
It is a generic utility function which stores the given argument into the buffer, advancing the buffer pointer, subtracting the newly stored size and (the first one) adjusting the pointer to the argument given so that it is a relative value into buf.
We implement first the last one.
<Off string freeze utility implementation. >= (U->) [D->]
// Freezes str into buf at curbuf.
// Adjusts curbuf and avail.
// Returns FALSE when not enough space is available.
boolean_t freeze(const char *str, char *&curbuf, size_t &avail)
{
size_t len;
assert(str && curbuf);
if ((len=strlen(str))>=avail)
return FALSE;
else {
len++; // for the trailing zero.
strncpy(curbuf,str,len);
avail -= len;
curbuf += len;
return TRUE;
}
}
Definesfreeze(links are to index).
<Off string freeze utility dependencies. >+= (U->) [<-D] #include <assert.h> // for assert
<Off string freeze utility implementation. >+= (U->) [<-D]
// Freezes str into buf at curbuf.
// Adjusts curbuf and avail. Makes str a relative ptr into buf.
// Returns FALSE when not enough space is available.
boolean_t freeze(const char *&str, char *buf, char *&curbuf, size_t &avail)
{
assert(str && buf && curbuf);
if (freeze(str,curbuf,avail)){
str=(char*)(curbuf-buf);
return TRUE;
}
else
return FALSE;
}
These routines have counterpart melts to recover frozen strings.
<Off string melt utility. >= (U->) // Melts (and allocates memory for it) str from buf. // Str must be a relative prt into buf. char *melt(char *str, char *buf, size_t size); // Returns a melted str from curbuf of length size. // Adjusts curbuf and size. char *melt(char *&curbuf, size_t &size);
Again, we implement first the last one.
<Off string melt utility implementation. >= (U->) [D->]
// Returns a melted str from curbuf of length size.
// Adjusts curbuf and size.
char *melt(char *&curbuf, size_t &size)
{
char *s=curbuf;
if(s){
int len=strlen(s)+1;
size -= len;
curbuf += len;
}
return s;
}
Definesmelt(links are to index).
<Off string melt utility implementation. >+= (U->) [<-D]
// Melts (and allocates memory for it) str from buf.
// Str must be a relative prt into buf.
char *melt(char *str, char *buf, size_t size)
{
if (size<=(natural_t)str)
return NULL;
return strdup(buf+((natural_t)str));
}
<Off string freeze utility implementation dependencies. >= (U->) #include <string.h> // for strncpy, strlen et al.
\subsubsection{freezing utilities \cpp{} source files}
The code shown above is kept in klib/freeze.h and
klib/freeze.C, along with request_melt utilities.
<freeze.h*>= <Read the literate code instead warning. > #ifndef __FREEZE_H #define __FREEZE_H 1 <Off string freeze utility dependencies. > <request_meltutilities dependencies. > <Off string freeze utility. > <Off string melt utility. > <request_meltutilities. > <Other utilities forfreeze. > #endif // __FREEZE_H
<freeze.C*>= <Read the literate code instead warning. > #include <klib/freeze.h> // Exported interface. <Off string freeze utility implementation dependencies. > <request_meltutility implementation dependencies. > <Off string freeze utility implementation. > <Off string melt utility implementation. > <request_meltutility implementation. >
\subsection{Signing frozen resources}
To sing frozen resources these functions are provided. The first two ones will sign the buffer(s) given by the user. The last ones checks the integrity of the signed buffer.
As of today they do nothing. They should be changed to employ a secure signing algorithm.
<Off sign utilities. >= (U->)
// Signs a single user bufer in a sign-buffer.
// Returns FALSE when failed due to insufficient space in the sign-buffer.
inline boolean_t sign(char *&signbuf, size_t &sblen, char *buf, size_t len) {
(void)signbuf; (void)sblen; (void)buf; (void)len;
return TRUE; }
// The same but for a couple of buffers.
inline boolean_t sign(char *&signbuf, size_t &sblen,
char *b1, size_t l1, char *b2, size_t l2) {
(void)signbuf; (void)sblen; (void)b1; (void)l1; (void)b2; (void)l2;
return TRUE;
}
// Was this the signed buffer?
inline boolean_t sign_ok(char *signbuf, size_t sblen, char *buf, size_t len) {
(void)signbuf; (void)sblen; (void)buf; (void)len;
return TRUE;
}
inline boolean_t sign_ok(char *signbuf, size_t sblen,
char *b1, size_t l1, char *b2, size_t l2) {
(void)signbuf; (void)sblen; (void)b1; (void)l1; (void)b2; (void)l2;
return TRUE;
}
<Off sign utilities dependencies. >= (U->) #include <flux/types.h> // for boolean_t, size_t et al.
\subsubsection{Signature \cpp{} source files}
These utilities are kept in klib/sign.h
<sign.h*>= <Read the literate code instead warning. > #ifndef __SIGN_H #define __SIGN_H <Off sign utilities dependencies. > <Off sign utilities. > #endif // __SIGN_H