Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC

DNS Resolver RFC 1035

Rate me:
Please Sign up or sign in to vote.
3.50/5 (11 votes)
14 Aug 20044 min read 93.9K   2.3K   20  
DNS resolving by RFC 1035; complete library for all versions of Windows.
#if (!defined(__NEURO__BYTES__BUFFER__))
#define __NEURO__BYTES__BUFFER__

/************************************************************************
		 
		 Name   : CNeuroBuffer
		 Type   : Class
------------------------------------------------------------------------
		 Author : Akash Kava
		 Purpose: Bytes Buffer to store, allocate, reallocate etc.
		          Append etc
************************************************************************/

class CNeuroBuffer{
protected:

	BYTE *	m_pBuffer;
	int		m_nAllocatedSize;
	int		m_nSize;
	int		m_nGrowBy;
	int		m_nPointer;

	void UnsafeClear()
	{
		m_pBuffer = NULL;
		m_nAllocatedSize = 0;
		m_nSize = 0;
		m_nGrowBy = 64;
		m_nPointer = 0;
	}

public:

	CNeuroBuffer()
	{
		UnsafeClear();
	}

	CNeuroBuffer(BYTE * pBuffer,int nSize)
	{
		UnsafeClear();
		Allocate(nSize);
		Copy(pBuffer,nSize);
	}

	int GetSize()
	{
		return m_nSize;
	}

	BYTE * GetBuffer()
	{
		return m_pBuffer;
	}

	BYTE * GetBufferSetLength(int nSize)
	{
		UnsafeClear();
		Allocate(nSize);
		m_nSize = nSize;
		return m_pBuffer;
	}

	CNeuroBuffer(CNeuroBuffer & Buffer)
	{
		UnsafeClear();
		Allocate(Buffer.GetSize());
		Copy(Buffer.GetBuffer(),Buffer.GetSize());
	}

	virtual ~CNeuroBuffer()
	{
		Clear();
	}

	BOOL Allocate(int nSize)
	{
		int nAllocatedSize = ((nSize / m_nGrowBy) + 1)*m_nGrowBy;

		void * pBuffer = new BYTE [nAllocatedSize];

		if(!pBuffer)
			return false;
		
		if(m_nAllocatedSize)
		{
			memcpy(pBuffer,m_pBuffer,m_nAllocatedSize);
			delete m_pBuffer;
		}

		m_pBuffer = (BYTE *)pBuffer;
		m_nAllocatedSize = nAllocatedSize;
		return true;
	}

	BOOL Append(BYTE * pBuffer,int nSize)
	{
		if(m_nSize + nSize > m_nAllocatedSize)
		{
			if(!Allocate(m_nSize + nSize))
				return false;
		}
		memcpy((m_pBuffer + m_nSize),pBuffer,nSize);
		m_nSize += nSize;
		return true;
	}

	int Copy(BYTE * pBuffer,int nSize)
	{
		int nSizeCopied = m_nAllocatedSize > nSize ? nSize : m_nAllocatedSize;
		memcpy(m_pBuffer,pBuffer,nSizeCopied);
		m_nSize = nSizeCopied;
		return nSizeCopied;
	}

	void Clear()
	{
		if(m_nAllocatedSize)
			delete m_pBuffer;
		UnsafeClear();
	}

	void operator = (CNeuroBuffer & Buffer)
	{
		//UnsafeClear();
		Clear();
		Allocate(Buffer.GetSize());
		Copy(Buffer.GetBuffer(),Buffer.GetSize());
	}

	void operator += (CNeuroBuffer & Buffer)
	{
		Append(Buffer.GetBuffer(),Buffer.GetSize());
	}

	CString Detach()
	{
		CString Data = (LPCTSTR)m_pBuffer;
		Clear();
		return Data;
	}
};









#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Programmer with WILL

Comments and Discussions