/*
 * 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: hdrmodlrd.c,v 1.5 2006/05/02 17:13:40 ca Exp $")

#include "sm/assert.h"
#include "sm/error.h"
#include "sm/mta.h"
#include "sm/hdrmod.h"
#include "sm/reccom.h"

/*
**  SM_HDRMODL_RD -- read header modification list from an RCB
**
**	Parameters:
**		sm_hdrmodhd -- header of header modification list
**		rcb -- RCB to read from
**		rttp -- record type for type and position
**		rthdr -- record type for header
**
**	Returns:
**		usual sm_error code
*/

sm_ret_T
sm_hdrmodl_rd(sm_hdrmodhd_P sm_hdrmodhd, sm_rcb_P rcb, uint32_t rttp, uint32_t rthdr)
{
	sm_hdrmod_P sm_hdrmod;
	uint32_t l, rt, n1, n2;
	sm_ret_T ret;

	ret = SM_SUCCESS;
	while (!SM_RCB_ISEOB(rcb))
	{
		ret = sm_rcb_peek2uint32(rcb, &l, &rt);
		if (sm_is_err(ret))
			return ret;
		if (rt != rttp || l != 8)
			return SM_SUCCESS;
		ret = sm_hdrmod_new(&sm_hdrmodhd, true, &sm_hdrmod);
		if (sm_is_err(ret))
			return ret;
		ret = sm_rcb_get4uint32(rcb, &l, &rt, &n1, &n2);
		if (sm_is_err(ret) || rt != rttp || l != 8)
		{
			return sm_is_err(ret) ? ret
				: sm_err_perm(SM_E_PR_ERR);
		}
		sm_hdrmod->sm_hm_type = n1;
		sm_hdrmod->sm_hm_pos = n2;

		if (SM_RCB_ISEOB(rcb))
			break;
		ret = sm_rcb_peek2uint32(rcb, &l, &rt);
		if (sm_is_err(ret))
		{
			sm_hdrmod_rm_last(&sm_hdrmodhd);
			return ret;
		}
		if (rttp == rt && 8 == l)
			continue;
		if (rt != rthdr)
			break;

		ret = sm_rcb_get2uint32(rcb, &l, &rt);
		if (sm_is_err(ret) || rt != rthdr || l > SM_MAXHDRLEN)
		{
			return sm_is_err(ret) ? ret
				: sm_err_perm(SM_E_PR_ERR);
		}
		ret = sm_rcb_getncstr(rcb, &sm_hdrmod->sm_hm_hdr, l);
		if (sm_is_err(ret))
			return ret;
	}
	return ret;
}


syntax highlighted by Code2HTML, v. 0.9.1