Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

ZLib.cpp

Go to the documentation of this file.
00001 //
00002 // ZLib.cpp
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 // CZLib makes it easier to use the zlib compression library
00023 // http://wiki.shareaza.com/static/Developers.Code.CZLib
00024 
00025 // Copy in the contents of these files here before compiling
00026 #include "StdAfx.h"
00027 #include "Shareaza.h"
00028 #include "ZLib.h"
00029 #include <zlib.h>
00030 
00031 // If we are compiling in debug mode, replace the text "THIS_FILE" in the code with the name of this file
00032 #ifdef _DEBUG
00033 #undef THIS_FILE
00034 static char THIS_FILE[]=__FILE__;
00035 #define new DEBUG_NEW
00036 #endif
00037 
00039 // CZLib compression
00040 
00041 // Takes a pointer to memory and how many bytes are there
00042 // Compresses the memory into a new buffer this function allocates
00043 // Returns a pointer to the new buffer, and writes its size under pnOutput
00044 LPBYTE CZLib::Compress(LPCVOID pInput, DWORD nInput, DWORD* pnOutput, DWORD nSuggest)
00045 {
00046         // If we were given nSuggest, use it as the output buffer size, otherwise call compressBound to set it
00047         *pnOutput = nSuggest ? nSuggest : compressBound( nInput ); // compressBound just uses math to guess, it doesn't look at the data
00048 
00049         // Allocate a new buffer of pnOutput bytes
00050         BYTE* pBuffer = new BYTE[ *pnOutput ];
00051 
00052         // Compress the data at pInput into pBuffer, putting how many bytes it wrote under pnOutput
00053         if ( compress(            // Compress data from one buffer to another, returns Z_OK 0 false if it works
00054                 pBuffer,              // The output buffer where ZLib can write compressed data
00055                 pnOutput,             // Reads how much space it has there, writes how much space it used
00056                 (const BYTE *)pInput, // The source buffer with data to compress
00057                 nInput ) )            // The number of bytes there
00058         {
00059                 // The compress function reported error
00060                 delete [] pBuffer; // Delete our temporary buffer
00061                 return NULL;       // Report error
00062         }
00063 
00064         // The pBuffer buffer is too big, make a new one exactly the right size, copy the data, delete the first, and return the second
00065         BYTE* pOutput = new BYTE[ *pnOutput ];     // Allocate a new buffer exactly big enough to hold the bytes compress wrote
00066         CopyMemory( pOutput, pBuffer, *pnOutput ); // Copy the compressed bytes from the old buffer to the new one
00067         delete [] pBuffer;                         // Delete the old buffer
00068         return pOutput;                            // Return the new one
00069 }
00070 
00072 // CZLib decompression
00073 
00074 // Takes a pointer to compressed input bytes, and how many are there
00075 // Decompresses the memory into a new buffer this function allocates
00076 // Returns a pointer to the new buffer, and writes its size under pnOutput
00077 LPBYTE CZLib::Decompress(LPCVOID pInput, DWORD nInput, DWORD* pnOutput, DWORD nSuggest)
00078 {
00079         // Guess how big the data will be decompressed, use nSuggest, or just guess it will be 6 times as big
00080         *pnOutput = nSuggest ? nSuggest : nInput * 6;
00081 
00082         // Allocate a buffer that big
00083         BYTE* pBuffer = new BYTE[ *pnOutput ];
00084 
00085         // Uncompress the data from pInput into pBuffer, writing how big it is now in pnOutput
00086         if ( int nError = uncompress( // Uncompress data
00087                 pBuffer,                  // Destination buffer where uncompress can write uncompressed data
00088                 pnOutput,                 // Reads how much space it has there, and writes how much space it used
00089                 (const BYTE *)pInput,     // Source buffer of compressed data
00090                 nInput ) )                // Number of bytes there
00091         {
00092                 // The uncompress function returned an error, delete the buffer we allocated and return error
00093                 delete [] pBuffer;
00094                 return NULL;
00095         }
00096 
00097         // The pBuffer buffer is bigger than necessary, move its bytes into one perfectly sized, and return it
00098         BYTE* pOutput = new BYTE[ *pnOutput ];     // Make a new buffer exactly the right size
00099         CopyMemory( pOutput, pBuffer, *pnOutput ); // Copy the data from the one that's too big
00100         delete [] pBuffer;                         // Delete the one that's too big
00101         return pOutput;                            // Return a pointer to the perfectly sized one
00102 }

Generated on Thu Dec 15 10:39:54 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2