00001 // 00002 // Buffer.h 00003 // 00004 // Copyright (c) Shareaza Development Team, 2002-2005. 00005 // This file is part of SHAREAZA (www.shareaza.com) 00006 // 00007 // Shareaza is free software; you can redistribute it 00008 // and/or modify it under the terms of the GNU General Public License 00009 // as published by the Free Software Foundation; either version 2 of 00010 // the License, or (at your option) any later version. 00011 // 00012 // Shareaza is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with Shareaza; if not, write to the Free Software 00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 // 00021 00022 // CBuffer holds some memory, and takes care of allocating and freeing it itself 00023 // http://wiki.shareaza.com/static/Developers.Code.CBuffer 00024 00025 // Only include the lines beneath this one once 00026 #pragma once 00027 00028 // A buffer of memory that takes care of allocating and freeing itself, and has methods for compression and encoding 00029 class CBuffer 00030 { 00031 00032 public: 00033 00034 // Make a new CBuffer object, and delete one 00035 CBuffer(DWORD* pLimit = NULL); // The argument is not used (do) 00036 virtual ~CBuffer(); // The virtual keyword indicates a class that inherits from this one may override this 00037 00038 public: 00039 00040 // Memory pointers and byte counts 00041 CBuffer* m_pNext; // A pointer to the next CBuffer object, letting them be linked together in a list 00042 BYTE* m_pBuffer; // The block of allocated memory 00043 DWORD m_nLength; // The number of bytes we have written into the block 00044 DWORD m_nBuffer; // The size of the allocated block 00045 00046 public: 00047 00048 // Add and remove data from the memory block in the CBuffer object 00049 void Add(const void* pData, DWORD nLength); // Add data to the end of the block already in this object 00050 void Insert(DWORD nOffset, const void* pData, DWORD nLength); // Insert the data in the middle somewhere 00051 void Remove(DWORD nLength); // Delete the first nLength bytes from the memory block 00052 void Clear(); // Mark the entire buffer empty, not changing the size of the allocated block 00053 00054 // Add text to the buffer, does not add a null terminator 00055 void Print(LPCSTR pszText); // Add ASCII text to the buffer 00056 void Print(LPCWSTR pszText, UINT nCodePage = CP_ACP); // Convert Unicode text to ASCII and add it to the buffer 00057 00058 // Copy all or part of the data in another CBuffer object into this one 00059 DWORD AddBuffer(CBuffer* pBuffer, DWORD nLength = 0xFFFFFFFF); // Length is -1 by default to copy the whole thing 00060 void AddReversed(const void* pData, DWORD nLength); // Add data to this buffer, but with the bytes in reverse order 00061 void Prefix(LPCSTR pszText); // Add ASCII text to the start of this buffer, shifting everything else forward 00062 void EnsureBuffer(DWORD nLength); // Tell the buffer to prepare to recieve this number of additional bytes 00063 00064 public: 00065 00066 // Read the data in the buffer as text 00067 CString ReadString(DWORD nBytes, UINT nCodePage = CP_ACP); // Reads nBytes of ASCII characters as a string 00068 BOOL ReadLine(CString& strLine, BOOL bPeek = FALSE, UINT nCodePage = CP_ACP); // Reads until "\r\n" 00069 BOOL StartsWith(LPCSTR pszString, BOOL bRemove = FALSE); // Returns true if the buffer starts with this text 00070 00071 // Use the buffer with a socket 00072 DWORD Receive(SOCKET hSocket); // Move incoming data from the socket to this buffer 00073 DWORD Send(SOCKET hSocket); // Send the contents of this buffer to the computer on the far end of the socket 00074 00075 // Use the buffer with the ZLib compression library 00076 BOOL Deflate(BOOL bIfSmaller = FALSE); // Compress the data in this buffer 00077 BOOL Inflate(DWORD nSuggest = 0); // Remove the compression on the data in this buffer 00078 BOOL Ungzip(); // Delete the gzip header and then remove the compression 00079 00080 // Read and write a DIME message in the buffer 00081 void WriteDIME(DWORD nFlags, LPCSTR pszID, LPCSTR pszType, LPCVOID pBody, DWORD nBody); 00082 BOOL ReadDIME(DWORD* pnFlags, CString* psID, CString* psType, DWORD* pnBody); 00083 00084 public: 00085 00086 // Static means you can call CBuffer::ReverseBuffer without having a CBuffer object at all 00087 static void ReverseBuffer(const void* pInput, void* pOutput, DWORD nLength); 00088 };