ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

mszip.cxx
Go to the documentation of this file.
00001 /*
00002  * COPYRIGHT:   See COPYING in the top level directory
00003  * PROJECT:     ReactOS cabinet manager
00004  * FILE:        tools/cabman/mszip.cpp
00005  * PURPOSE:     CAB codec for MSZIP compressed data
00006  * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
00007  *              Colin Finck <mail@colinfinck.de>
00008  * NOTES:       The ZLIB does the real work. Get the full version
00009  *              from http://www.cdrom.com/pub/infozip/zlib/
00010  * REVISIONS:
00011  *   CSH 21/03-2001 Created
00012  *   CSH 15/08-2003 Made it portable
00013  *   CF  04/05-2007 Made it compatible with 64-bit operating systems
00014  */
00015 #include <stdio.h>
00016 #include "mszip.h"
00017 
00018 
00019 /* Memory functions */
00020 
00021 voidpf MSZipAlloc(voidpf opaque, uInt items, uInt size)
00022 {
00023     DPRINT(DEBUG_MEMORY, ("items = (%d)  size = (%d)\n", items, size));
00024     return AllocateMemory(items * size);
00025 }
00026 
00027 void MSZipFree (voidpf opaque, voidpf address)
00028 {
00029     DPRINT(DEBUG_MEMORY, ("\n"));
00030     FreeMemory(address);
00031 }
00032 
00033 
00034 /* CMSZipCodec */
00035 
00036 CMSZipCodec::CMSZipCodec()
00037 /*
00038  * FUNCTION: Default constructor
00039  */
00040 {
00041     ZStream.zalloc = MSZipAlloc;
00042     ZStream.zfree  = MSZipFree;
00043     ZStream.opaque = (voidpf)0;
00044 }
00045 
00046 
00047 CMSZipCodec::~CMSZipCodec()
00048 /*
00049  * FUNCTION: Default destructor
00050  */
00051 {
00052 }
00053 
00054 
00055 ULONG CMSZipCodec::Compress(void* OutputBuffer,
00056                             void* InputBuffer,
00057                             ULONG InputLength,
00058                             PULONG OutputLength)
00059 /*
00060  * FUNCTION: Compresses data in a buffer
00061  * ARGUMENTS:
00062  *     OutputBuffer   = Pointer to buffer to place compressed data
00063  *     InputBuffer    = Pointer to buffer with data to be compressed
00064  *     InputLength    = Length of input buffer
00065  *     OutputLength   = Address of buffer to place size of compressed data
00066  */
00067 {
00068     PUSHORT Magic;
00069 
00070     DPRINT(MAX_TRACE, ("InputLength (%u).\n", (UINT)InputLength));
00071 
00072     Magic  = (PUSHORT)OutputBuffer;
00073     *Magic = MSZIP_MAGIC;
00074 
00075     ZStream.next_in   = (unsigned char*)InputBuffer;
00076     ZStream.avail_in  = InputLength;
00077     ZStream.next_out  = (unsigned char*)((unsigned long)OutputBuffer + 2);
00078     ZStream.avail_out = CAB_BLOCKSIZE + 12;
00079 
00080     /* WindowBits is passed < 0 to tell that there is no zlib header */
00081     Status = deflateInit2(&ZStream,
00082                           Z_BEST_COMPRESSION,
00083                           Z_DEFLATED,
00084                           -MAX_WBITS,
00085                           8, /* memLevel */
00086                           Z_DEFAULT_STRATEGY);
00087     if (Status != Z_OK)
00088     {
00089         DPRINT(MIN_TRACE, ("deflateInit() returned (%d).\n", Status));
00090         return CS_NOMEMORY;
00091     }
00092 
00093     Status = deflate(&ZStream, Z_FINISH);
00094     if ((Status != Z_OK) && (Status != Z_STREAM_END))
00095     {
00096         DPRINT(MIN_TRACE, ("deflate() returned (%d) (%s).\n", Status, ZStream.msg));
00097         if (Status == Z_MEM_ERROR)
00098             return CS_NOMEMORY;
00099         return CS_BADSTREAM;
00100     }
00101 
00102     *OutputLength = ZStream.total_out + 2;
00103 
00104     Status = deflateEnd(&ZStream);
00105     if (Status != Z_OK)
00106     {
00107         DPRINT(MIN_TRACE, ("deflateEnd() returned (%d).\n", Status));
00108         return CS_BADSTREAM;
00109     }
00110 
00111     return CS_SUCCESS;
00112 }
00113 
00114 
00115 ULONG CMSZipCodec::Uncompress(void* OutputBuffer,
00116                               void* InputBuffer,
00117                               ULONG InputLength,
00118                               PULONG OutputLength)
00119 /*
00120  * FUNCTION: Uncompresses data in a buffer
00121  * ARGUMENTS:
00122  *     OutputBuffer = Pointer to buffer to place uncompressed data
00123  *     InputBuffer  = Pointer to buffer with data to be uncompressed
00124  *     InputLength  = Length of input buffer
00125  *     OutputLength = Address of buffer to place size of uncompressed data
00126  */
00127 {
00128     USHORT Magic;
00129 
00130     DPRINT(MAX_TRACE, ("InputLength (%u).\n", (UINT)InputLength));
00131 
00132     Magic = *((PUSHORT)InputBuffer);
00133 
00134     if (Magic != MSZIP_MAGIC)
00135     {
00136         DPRINT(MID_TRACE, ("Bad MSZIP block header magic (0x%X)\n", Magic));
00137         return CS_BADSTREAM;
00138     }
00139 
00140     ZStream.next_in   = (unsigned char*)((unsigned long)InputBuffer + 2);
00141     ZStream.avail_in  = InputLength - 2;
00142     ZStream.next_out  = (unsigned char*)OutputBuffer;
00143     ZStream.avail_out = CAB_BLOCKSIZE + 12;
00144 
00145     /* WindowBits is passed < 0 to tell that there is no zlib header.
00146      * Note that in this case inflate *requires* an extra "dummy" byte
00147      * after the compressed stream in order to complete decompression and
00148      * return Z_STREAM_END.
00149      */
00150     Status = inflateInit2(&ZStream, -MAX_WBITS);
00151     if (Status != Z_OK)
00152     {
00153         DPRINT(MIN_TRACE, ("inflateInit2() returned (%d).\n", Status));
00154         return CS_BADSTREAM;
00155     }
00156 
00157     while ((ZStream.total_out < CAB_BLOCKSIZE + 12) &&
00158         (ZStream.total_in < InputLength - 2))
00159     {
00160         Status = inflate(&ZStream, Z_NO_FLUSH);
00161         if (Status == Z_STREAM_END) break;
00162         if (Status != Z_OK)
00163         {
00164             DPRINT(MIN_TRACE, ("inflate() returned (%d) (%s).\n", Status, ZStream.msg));
00165             if (Status == Z_MEM_ERROR)
00166                 return CS_NOMEMORY;
00167             return CS_BADSTREAM;
00168         }
00169     }
00170 
00171     *OutputLength = ZStream.total_out;
00172 
00173     Status = inflateEnd(&ZStream);
00174     if (Status != Z_OK)
00175     {
00176         DPRINT(MIN_TRACE, ("inflateEnd() returned (%d).\n", Status));
00177         return CS_BADSTREAM;
00178     }
00179     return CS_SUCCESS;
00180 }
00181 
00182 /* EOF */

Generated on Sat May 26 2012 04:36:34 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.