lib/dgr.c
/** Takes the list of DGR records and puts them into a compact byte
 * stream. The format is:
 *   
 * label character string<br>
 * Null terminator at end of string<br>
 * An integer indicating the size of the data that follows.<br>
 * A buffer of the data.<br>
 *
 * @param size The size of the data being serialized.
 * @return A serialized array of bytes (to be free()'d by the caller)
*/
Takes the list of DGR records and puts them into a compact byte
stream. The format is.

label character string
Null terminator at end of string
An integer indicating the size of the data that follows.
A buffer of the data.

@param size The size of the data being serialized.
@return A serialized array of bytes (to be free()'d by the caller)
Takes the list of DGR records and puts them into a compact byte stream . The format is . label character string Null terminator at end of string An integer indicating the size of the data that follows . A buffer of the data . @param size The size of the data being serialized . @return A serialized array of bytes ( to be free () ' d by the caller )
char* dgr_serialize(int *size)
{
	int spaceNeeded = 0;
	for(int i=0; i<dgr_list_size; i++)
		spaceNeeded += strlen(dgr_list[i].name)+1+sizeof(int)+dgr_list[i].size;
	*size = spaceNeeded;
	if(spaceNeeded == 0)
		return NULL;
	char *serialized = malloc(spaceNeeded);
	char *ptr = serialized;
	for(int i=0; i<dgr_list_size; i++)
	{
		int bytesPrinted = sprintf(ptr, "%s", dgr_list[i].name);
		ptr += bytesPrinted+1; 
		memcpy(ptr, &(dgr_list[i].size), sizeof(int));
		ptr += sizeof(int);
		memcpy(ptr, dgr_list[i].buffer, dgr_list[i].size);
		ptr += dgr_list[i].size;
	}
	return serialized;
}
char * dgr_serialize ( int * size ) { int spaceNeeded = 0 ; for ( int i = 0 ; i < dgr_list_size ; i ++ ) spaceNeeded += strlen ( dgr_list [ i ] . name ) + 1 + sizeof ( int ) + dgr_list [ i ] . size ; * size = spaceNeeded ; if ( spaceNeeded == 0 ) return NULL ; char * serialized = malloc ( spaceNeeded ) ; char * ptr = serialized ; for ( int i = 0 ; i < dgr_list_size ; i ++ ) { int bytesPrinted = sprintf ( ptr , " " , dgr_list [ i ] . name ) ; ptr += bytesPrinted + 1 ; memcpy ( ptr , & ( dgr_list [ i ] . size ) , sizeof ( int ) ) ; ptr += sizeof ( int ) ; memcpy ( ptr , dgr_list [ i ] . buffer , dgr_list [ i ] . size ) ; ptr += dgr_list [ i ] . size ; } return serialized ; }
Takes the list of DGR records and puts them into a compact byte
 stream.
Takes the list of DGR records and puts them into a compact byte stream .
// extra byte for null terminated string.