void
test_psf_strlcpy_crlf (void)
{	const char *src = "a\nb\nc\n" ;
	char *dest ;
	int dest_len ;

	print_test_name ("Testing psf_strlcpy_crlf") ;

	for (dest_len = 3 ; dest_len < 30 ; dest_len++)
	{	dest = calloc (1, dest_len + 1) ;
		if (dest == NULL)
		{	printf ("\n\nLine %d: calloc failed!\n\n", __LINE__) ;
			exit (1) ;
			} ;

		dest [dest_len] = '\xea' ;

		psf_strlcpy_crlf (dest, src, dest_len, sizeof (*src)) ;

		if (dest [dest_len] != '\xea')
		{	printf ("\n\nLine %d: buffer overrun for dest_len == %d\n\n", __LINE__, dest_len) ;
			exit (1) ;
			} ;

		free (dest) ;
		} ;

	puts ("ok") ;
} /* test_psf_strlcpy_crlf */
Beispiel #2
0
int
broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * info, size_t datasize)
{	size_t len ;

	if (info == NULL)
		return SF_FALSE ;

	if (bc_min_size (info) > datasize)
	{	psf->error = SFE_BAD_BROADCAST_INFO_SIZE ;
		return SF_FALSE ;
		} ;

	if (datasize >= sizeof (SF_BROADCAST_INFO_16K))
	{	psf->error = SFE_BAD_BROADCAST_INFO_TOO_BIG ;
		return SF_FALSE ;
		} ;

	if (psf->broadcast_16k == NULL)
	{	if ((psf->broadcast_16k = broadcast_var_alloc ()) == NULL)
		{	psf->error = SFE_MALLOC_FAILED	 ;
			return SF_FALSE ;
			} ;
		} ;

	memcpy (psf->broadcast_16k, info, offsetof (SF_BROADCAST_INFO, coding_history)) ;

	psf_strlcpy_crlf (psf->broadcast_16k->coding_history, info->coding_history, sizeof (psf->broadcast_16k->coding_history), datasize - offsetof (SF_BROADCAST_INFO, coding_history)) ;
	len = strlen (psf->broadcast_16k->coding_history) ;

	if (len > 0 && psf->broadcast_16k->coding_history [len - 1] != '\n')
		psf_strlcat (psf->broadcast_16k->coding_history, sizeof (psf->broadcast_16k->coding_history), "\r\n") ;

	if (psf->file.mode == SFM_WRITE)
	{	char added_history [256] ;
		size_t added_history_len ;

		added_history_len = gen_coding_history (added_history, sizeof (added_history), &(psf->sf)) ;
		psf_strlcat (psf->broadcast_16k->coding_history, sizeof (psf->broadcast_16k->coding_history), added_history) ;
		} ;

	/* Force coding_history_size to be even. */
	len = strlen (psf->broadcast_16k->coding_history) ;
	len += (len & 1) ? 1 : 2 ;
	psf->broadcast_16k->coding_history_size = len ;

	/* Currently writing this version. */
	psf->broadcast_16k->version = 1 ;

	return SF_TRUE ;
} /* broadcast_var_set */