aboutsummaryrefslogtreecommitdiff
path: root/dep/CascLib/src/CascDecompress.cpp
blob: 19d7d0a88c485379807d58f035bd6c2b07f0ac2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*****************************************************************************/
/* CascDecompress.cpp                     Copyright (c) Ladislav Zezula 2014 */
/*---------------------------------------------------------------------------*/
/* Decompression functions                                                   */
/*---------------------------------------------------------------------------*/
/*   Date    Ver   Who  Comment                                              */
/* --------  ----  ---  -------                                              */
/* 02.05.14  1.00  Lad  The first version of CascDecompress.cpp              */
/*****************************************************************************/

#define __CASCLIB_SELF__
#include "CascLib.h"
#include "CascCommon.h"

//-----------------------------------------------------------------------------
// Public functions

DWORD CascDecompress(LPBYTE pbOutBuffer, PDWORD pcbOutBuffer, LPBYTE pbInBuffer, DWORD cbInBuffer)
{
    z_stream z;                        // Stream information for zlib
    DWORD dwErrCode = ERROR_FILE_CORRUPT;
    uInt cbOutBuffer = *pcbOutBuffer;
    int nResult;

    // Fill the stream structure for zlib
    z.next_in   = pbInBuffer;
    z.avail_in  = cbInBuffer;
    z.total_in  = cbInBuffer;
    z.next_out  = pbOutBuffer;
    z.avail_out = cbOutBuffer;
    z.total_out = 0;
    z.zalloc    = NULL;
    z.zfree     = NULL;

    // Reset the total number of output bytes
    cbOutBuffer = 0;

    // Initialize the decompression structure
    if((nResult = inflateInit(&z)) == Z_OK)
    {
        // Call zlib to decompress the data
        nResult = inflate(&z, Z_NO_FLUSH);
        if(nResult == Z_OK || nResult == Z_STREAM_END)
        {
            // Give the size of the uncompressed data
            cbOutBuffer = z.total_out;
            dwErrCode = ERROR_SUCCESS;
        }

        inflateEnd(&z);
    }

    // Give the caller the number of bytes needed
    pcbOutBuffer[0] = cbOutBuffer;
    return dwErrCode;
}