/*
 */

#include "sm/generic.h"
SM_RCSID("@(#)$Id: rcbputnint.c,v 1.4 2007/10/23 03:56:31 ca Exp $")

#include "sm/assert.h"
#include "sm/magic.h"
#include "sm/error.h"
#include "sm/memops.h"
#include "sm/rpool.h"
#include "sm/limits.h"
#include "sm/rcb.h"
#include "sm/str-int.h"
#include "sm/str2rcb.h"
#include "sm/reccom.h"

#if MTA_USE_RCBV_INTN
/*
**  SM_RCB_PUTNUINT32 -- Append n values to the end of a sm_rcb_P.
**		In contrast to other sm_rcb_putXuint32 functions, this one
**		"knows" about the structure of entries:
**		length, rt, value, value, ...
**
**	Parameters:
**		rcb -- sm_rcb_P object to append onto.
**		rt -- record type
**		n -- number of values to append.
**		... -- values to append.
**
**	Returns:
**		usual sm_error code; ENOMEM, SM_E_OVFLW_SC, SM_E_OVFLW_NS
**
**	Last code review:
**	Last code change:
*/

sm_ret_T
sm_rcb_putnuint32(sm_rcb_P rcb, uint32_t rt, uint n, ...)
{
	uint nsize, u, ne;
	uint32_t v;
	va_list ap;

	SM_IS_RCB(rcb);

	ne = n + 2;	/* total number of entries: n + rt + counter itself */
	if (ne <= n)
		return sm_error_perm(SM_EM_RECCOM, SM_E_OVFLW_SC);
	nsize = rcb->sm_rcb_len + sizeof(uint32_t) * ne;

	/* overflow? */
	if (nsize <= rcb->sm_rcb_len)
		return sm_error_perm(SM_EM_RECCOM, SM_E_OVFLW_SC);

	/* do we really want to do this? */
	if (rcb->sm_rcb_rw >= 0 && (int)(ne * sizeof(uint32_t)) > rcb->sm_rcb_rw)
		return sm_error_perm(SM_EM_RECCOM, SM_E_FULL);
	if (nsize > rcb->sm_rcb_size)
		SM_STR_INCREASE_R(rcb, nsize);

	/* number of bytes to follow */
	v = n * sizeof(uint32_t);
	sm_uint32_2buf(v, rcb->sm_rcb_base + rcb->sm_rcb_len);
	rcb->sm_rcb_len += sizeof(uint32_t);

	/* record type */
	sm_uint32_2buf(rt, rcb->sm_rcb_base + rcb->sm_rcb_len);
	rcb->sm_rcb_len += sizeof(uint32_t);

	va_start(ap, n);
	for (u = 0; u < n; u++)
	{
		v = va_arg(ap, uint32_t);
		sm_uint32_2buf(v, rcb->sm_rcb_base + rcb->sm_rcb_len);
		rcb->sm_rcb_len += sizeof(uint32_t);
	}
	if (rcb->sm_rcb_rw >= 0)
		rcb->sm_rcb_rw -= sizeof(uint32_t) * ne;
	return SM_SUCCESS;
}
#endif /* MTA_USE_RCBV_INTN */


syntax highlighted by Code2HTML, v. 0.9.1