if ( (found_index >= 0 ) && ( found_index < exporter->ipfix_lo_template_maxsize ) ) { DPRINTF("ipfix_start_template_set: initializing new slot\n");       (*exporter).template_arr[found_index].max_fields_length = 8 * field_count + 8; DPRINTF("(*exporter).template_arr[found_index].max_fields_length %u \n", (*exporter).template_arr[found_index].max_fields_length); (*exporter).template_arr[found_index].fields_length = 8;  (*exporter).template_arr[found_index].template_fields = (char*) malloc (   (*exporter).template_arr[found_index].max_fields_length );   (*exporter).template_arr[found_index].valid = UNCLEAN;   (*exporter).template_arr[found_index].template_id = template_id; (*exporter).template_arr[found_index].field_count = field_count;     char *p_pos; char *p_end;   p_pos =  (*exporter).template_arr[found_index].template_fields;  p_end = p_pos +  (*exporter).template_arr[found_index].max_fields_length;     write_unsigned16 (&p_pos, p_end, 2);  write_unsigned16 (&p_pos, p_end, 0);  write_unsigned16 (&p_pos, p_end, template_id);  write_unsigned16 (&p_pos, p_end, field_count);    DPRINTF("ipfix_start_template_set: max_fields_len %u \n", (*exporter).template_arr[found_index].max_fields_length); DPRINTF("ipfix_start_template_set: fieldss_len %u \n", (*exporter).template_arr[found_index].fields_length); } else return -1;   return 0; }
int ipfix_start_template_set (ipfix_exporter *exporter, uint16_t template_id,  uint16_t field_count){	// are we updating an existing template?	int i;	int searching;	int found_index = -1;	DPRINTF("ipfix_start_template_set: start\n");	found_index = ipfix_find_template(exporter, template_id, COMMITED);	// have we found a template?	if (found_index >= 0) {		DPRINTF("ipfix_start_template_set: template found at index %i\n", found_index);		// we must overwrite the old template.		// first, clean up the old template:		// remove the old template from memory!		(*exporter).template_arr[found_index].valid = UNUSED;		free ((*exporter).template_arr[found_index].template_fields) ;		(*exporter).template_arr[found_index].template_fields= NULL;	} else {		/* allocate a new, free slot */		DPRINTF("ipfix_start_template_set: making new template\n");		searching = TRUE;		// check if there is a free slot at all:		if (exporter->ipfix_lo_template_current_count >= exporter->ipfix_lo_template_maxsize ) {			fprintf (stderr, "No more free slots for new templates available.\n");			fprintf (stderr, "Template not added!\n");			// do error handling:			found_index = -1;			searching = FALSE;			return -1;		}		DPRINTF("ipfix_start_template_set: found_index: %i,  searching: %i, maxsize: %i \n", found_index, searching ,(*exporter).ipfix_lo_template_maxsize);		i = 0;		// search for a free slot:		while (searching && i < exporter->ipfix_lo_template_maxsize) {			if ( (*exporter).template_arr[i].valid == UNUSED ) {				// we have found a free slot:				// increase total number of templates.				exporter->ipfix_lo_template_current_count ++;				searching = FALSE;				found_index = i;				(*exporter).template_arr[i].template_fields = NULL;				// TODO: maybe check, if this field is not null. Might only happen, when				// asynchronous threads change the template fields.				DPRINTF("ipfix_start_template_set: free slot found at %i \n", found_index);			}			i++;		}	}	/*	 initialize the slot	 test for a valid slot         */	if ( (found_index >= 0 ) && ( found_index < exporter->ipfix_lo_template_maxsize ) ) {		DPRINTF("ipfix_start_template_set: initializing new slot\n");		// allocate memory for the template's fields:		// maximum length of the data: 8 bytes / field, as each field contains:		// field type, field length (2*2bytes)		// and may contain an Enterprise Number (4 bytes)		// also, reserve 8 bytes space for the header!		(*exporter).template_arr[found_index].max_fields_length = 8 * field_count + 8;		DPRINTF("(*exporter).template_arr[found_index].max_fields_length %u \n", (*exporter).template_arr[found_index].max_fields_length);		(*exporter).template_arr[found_index].fields_length = 8;		(*exporter).template_arr[found_index].template_fields = (char*) malloc (   (*exporter).template_arr[found_index].max_fields_length );		// initialize the rest:		(*exporter).template_arr[found_index].valid = UNCLEAN;		// TODO FIXME : only TRUE and FALSE don't seem to suffice!		//    (*exporter).template_arr[found_index].valid = FALSE;		(*exporter).template_arr[found_index].template_id = template_id;		(*exporter).template_arr[found_index].field_count = field_count;		// also, write the template header fields into the buffer (except the lenght field);		//  int ret;		char *p_pos;		char *p_end;		// beginning of the buffer		p_pos =  (*exporter).template_arr[found_index].template_fields;		// end of the buffer		p_end = p_pos +  (*exporter).template_arr[found_index].max_fields_length;		// add offset to the buffer's beginning: this is, where we will write to.		//  p_pos +=  (*exporter).template_arr[found_index].fields_length;		// set ID is 2 for a template:		write_unsigned16 (&p_pos, p_end, 2);		// write 0 to the lenght field; this will be overwritten with end_template		write_unsigned16 (&p_pos, p_end, 0);		// write the template ID:		write_unsigned16 (&p_pos, p_end, template_id);		// write the field count:		write_unsigned16 (&p_pos, p_end, field_count);		// does this work?		// (*exporter).template_arr[found_index].fields_length += 8;		DPRINTF("ipfix_start_template_set: max_fields_len %u \n", (*exporter).template_arr[found_index].max_fields_length);		DPRINTF("ipfix_start_template_set: fieldss_len %u \n", (*exporter).template_arr[found_index].fields_length);	} else return -1;	return 0;}