/*!
 * \file    buffer_utils.c
 * \brief   Buffer check utility functions
 * \author  Lei Tang
 * \date    5/22/2015
 *
 * <hr>
 * \section LICENSE
 * Copyright Samsung Electronics, Co. Ltd. B2B TIMA team. 2015 05
 * <hr>
 */
#include "buffer_utils.h" 

/**@Validate a buffer is within the given range, including buffer>=minAddr and buffer+bufLen<=maxAddr.
 *
 * @param[in] buffer:  The starting address of the buffer
 * @param[in] bufLen: The length of the buffer. 
 * @param[in] minAddr:  The minimum address allowed for the buffer.
 * @param[in] maxAddr:  The maximum address allowed for the buffer (the address of the last byte in the buffer plus 1).
 *
 * Return 1 if the buffer passes the validation. Otherwise, return 0.
 * */
int isBufferInRange(const void *buffer, const uint32_t bufLen, const void *minAddr, const void *maxAddr) {
	uint64_t buf64=(uint64_t)buffer;
	uint64_t bufLen64=(uint64_t)bufLen;
	uint64_t min64=(uint64_t)minAddr;
	uint64_t max64=(uint64_t)maxAddr;

	if(buffer==NULL) //null buffer is not allowed
		return 0;
	if(minAddr>maxAddr) //minAddr must <=maxAddr
		return 0;
	if(buf64<min64||buf64>max64)
		return 0;
	if(bufLen64>max64)
		return 0;
	if(buf64>max64-bufLen64)
		return 0;
	return 1;
}

/**@Set all bytes in a buffer to be zero.
 *
 * @param[in] buffer:  The starting address of the buffer
 * @param[in] bufLen: The length of the buffer. 
 * Return void.
 * */
void zeroOutBuffer(const void *buffer, const uint32_t bufLen) {
	uint32_t i=0;
	uint8_t *buf=0;
	if(buffer==NULL) //null buffer is not allowed
		return;
	buf=(uint8_t *)buffer;
	for(i=0;i<bufLen;i++)
		buf[i]=0;
}

