/*
 * Copyright (c) 2005 Sendmail, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include "sm/generic.h"
SM_RCSID("@(#)$Id: rcbgetdata.c,v 1.1 2005/12/02 17:13:38 ca Exp $")

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

/*
**  SM_RCB_GETDATA -- Create data from rcb (n bytes).
**
**	Parameters:
**		rcb -- sm_rcb_P object to read from
**		data -- (pointer to) data in rcb
**		n -- number of bytes to read
**
**	Returns:
**		usual sm_error code
**
**	Side Effects: none on error
**
**	Last code review:
**	Last code change:
*/

sm_ret_T
sm_rcb_getdata(sm_rcb_P rcb, const uchar **data, uint n)
{
	uint la;

	SM_IS_RCB(rcb);
	SM_REQUIRE(data != NULL);
#if SM_RCB_CHECK
	SM_REQUIRE(rcb->sm_rcb_state == SM_RCB_DEC);
#endif
	SM_REQUIRE(n < INT_MAX);
	la = SM_ALIGN4(n);
	SM_ASSERT(la >= n);
	SM_ASSERT(rcb->sm_rcb_rw >= 0);
	SM_ASSERT((uint)rcb->sm_rcb_rw + la >= (uint)rcb->sm_rcb_rw);
	if ((uint)rcb->sm_rcb_rw + la > rcb->sm_rcb_len)
		return sm_error_perm(SM_EM_RECCOM, SM_E_OVFLW_NS);
	SM_ASSERT(rcb->sm_rcb_rw + la < INT_MAX);
	*data = rcb->sm_rcb_base + rcb->sm_rcb_rw;
	rcb->sm_rcb_rw += la;
	return SM_SUCCESS;
}


syntax highlighted by Code2HTML, v. 0.9.1