ReactOS 0.4.15-dev-8119-g4fb2fdb
mszip.cxx
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS cabinet manager
4 * FILE: tools/cabman/mszip.cxx
5 * PURPOSE: CAB codec for MSZIP compressed data
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Colin Finck <mail@colinfinck.de>
8 * NOTES: The ZLIB does the real work. Get the full version
9 * from http://www.cdrom.com/pub/infozip/zlib/
10 * REVISIONS:
11 * CSH 21/03-2001 Created
12 * CSH 15/08-2003 Made it portable
13 * CF 04/05-2007 Made it compatible with 64-bit operating systems
14 */
15#include <stdio.h>
16#include "mszip.h"
17
18
19/* Memory functions */
20
22{
23 DPRINT(DEBUG_MEMORY, ("items = (%d) size = (%d)\n", items, size));
24 return malloc(items * size);
25}
26
28{
29 DPRINT(DEBUG_MEMORY, ("\n"));
31}
32
33
34/* CMSZipCodec */
35
37/*
38 * FUNCTION: Default constructor
39 */
40{
44}
45
46
48/*
49 * FUNCTION: Default destructor
50 */
51{
52}
53
54
56 void* InputBuffer,
57 ULONG InputLength,
58 PULONG OutputLength)
59/*
60 * FUNCTION: Compresses data in a buffer
61 * ARGUMENTS:
62 * OutputBuffer = Pointer to buffer to place compressed data
63 * InputBuffer = Pointer to buffer with data to be compressed
64 * InputLength = Length of input buffer
65 * OutputLength = Address of buffer to place size of compressed data
66 */
67{
68 PUSHORT Magic;
69
70 DPRINT(MAX_TRACE, ("InputLength (%u).\n", (UINT)InputLength));
71
72 Magic = (PUSHORT)OutputBuffer;
73 *Magic = MSZIP_MAGIC;
74
75 ZStream.next_in = (unsigned char*)InputBuffer;
76 ZStream.avail_in = InputLength;
77 ZStream.next_out = ((unsigned char *)OutputBuffer + 2);
79
80 /* WindowBits is passed < 0 to tell that there is no zlib header */
84 -MAX_WBITS,
85 8, /* memLevel */
87 if (Status != Z_OK)
88 {
89 DPRINT(MIN_TRACE, ("deflateInit() returned (%d).\n", Status));
90 return CS_NOMEMORY;
91 }
92
94 if ((Status != Z_OK) && (Status != Z_STREAM_END))
95 {
96 DPRINT(MIN_TRACE, ("deflate() returned (%d) (%s).\n", Status, ZStream.msg));
97 if (Status == Z_MEM_ERROR)
98 return CS_NOMEMORY;
99 return CS_BADSTREAM;
100 }
101
102 *OutputLength = ZStream.total_out + 2;
103
105 if (Status != Z_OK)
106 {
107 DPRINT(MIN_TRACE, ("deflateEnd() returned (%d).\n", Status));
108 return CS_BADSTREAM;
109 }
110
111 return CS_SUCCESS;
112}
113
114
116 void* InputBuffer,
117 ULONG InputLength,
118 PULONG OutputLength)
119/*
120 * FUNCTION: Uncompresses data in a buffer
121 * ARGUMENTS:
122 * OutputBuffer = Pointer to buffer to place uncompressed data
123 * InputBuffer = Pointer to buffer with data to be uncompressed
124 * InputLength = Length of input buffer
125 * OutputLength = Address of buffer to place size of uncompressed data
126 */
127{
128 USHORT Magic;
129
130 DPRINT(MAX_TRACE, ("InputLength (%u).\n", (UINT)InputLength));
131
132 Magic = *((PUSHORT)InputBuffer);
133
134 if (Magic != MSZIP_MAGIC)
135 {
136 DPRINT(MID_TRACE, ("Bad MSZIP block header magic (0x%X)\n", Magic));
137 return CS_BADSTREAM;
138 }
139
140 ZStream.next_in = ((unsigned char*)InputBuffer + 2);
141 ZStream.avail_in = InputLength - 2;
142 ZStream.next_out = (unsigned char*)OutputBuffer;
144
145 /* WindowBits is passed < 0 to tell that there is no zlib header.
146 * Note that in this case inflate *requires* an extra "dummy" byte
147 * after the compressed stream in order to complete decompression and
148 * return Z_STREAM_END.
149 */
151 if (Status != Z_OK)
152 {
153 DPRINT(MIN_TRACE, ("inflateInit2() returned (%d).\n", Status));
154 return CS_BADSTREAM;
155 }
156
157 while ((ZStream.total_out < CAB_BLOCKSIZE + 12) &&
158 (ZStream.total_in < InputLength - 2))
159 {
161 if (Status == Z_STREAM_END) break;
162 if (Status != Z_OK)
163 {
164 DPRINT(MIN_TRACE, ("inflate() returned (%d) (%s).\n", Status, ZStream.msg));
165 if (Status == Z_MEM_ERROR)
166 return CS_NOMEMORY;
167 return CS_BADSTREAM;
168 }
169 }
170
171 *OutputLength = ZStream.total_out;
172
174 if (Status != Z_OK)
175 {
176 DPRINT(MIN_TRACE, ("inflateEnd() returned (%d).\n", Status));
177 return CS_BADSTREAM;
178 }
179 return CS_SUCCESS;
180}
181
182/* EOF */
#define MIN_TRACE
Definition: debug.h:14
#define MID_TRACE
Definition: debug.h:15
#define MAX_TRACE
Definition: debug.h:16
#define CS_NOMEMORY
Definition: cabinet.h:42
#define CS_SUCCESS
Definition: cabinet.h:41
#define CS_BADSTREAM
Definition: cabinet.h:43
voidpf MSZipAlloc(voidpf opaque, uInt items, uInt size)
Definition: cabinet.c:317
void MSZipFree(voidpf opaque, voidpf address)
Definition: cabinet.c:323
#define CAB_BLOCKSIZE
Definition: cabinet.c:51
virtual ULONG Uncompress(void *OutputBuffer, void *InputBuffer, ULONG InputLength, PULONG OutputLength) override
Definition: mszip.cxx:115
virtual ~CMSZipCodec()
Definition: mszip.cxx:47
CMSZipCodec()
Definition: mszip.cxx:36
virtual ULONG Compress(void *OutputBuffer, void *InputBuffer, ULONG InputLength, PULONG OutputLength) override
Definition: mszip.cxx:55
z_stream ZStream
Definition: mszip.h:37
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
int inflate(z_streamp strm, int flush)
Definition: inflate.c:1257
int inflateEnd(z_streamp strm)
Definition: inflate.c:1910
#define Z_DEFLATED
Definition: zlib.h:146
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:137
void FAR * voidpf
Definition: zlib.h:42
#define Z_STREAM_END
Definition: zlib.h:115
#define Z_FINISH
Definition: zlib.h:109
unsigned int uInt
Definition: zlib.h:38
#define Z_OK
Definition: zlib.h:114
int deflate(z_streamp strm, int flush) DECLSPEC_HIDDEN
Definition: deflate.c:815
#define MAX_WBITS
Definition: zlib.h:151
#define Z_NO_FLUSH
Definition: zlib.h:105
int deflateEnd(z_streamp strm) DECLSPEC_HIDDEN
Definition: deflate.c:1130
#define Z_MEM_ERROR
Definition: zlib.h:120
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:130
#define DEBUG_MEMORY
Definition: debug.h:18
Status
Definition: gdiplustypes.h:25
GLsizeiptr size
Definition: glext.h:5919
GLuint address
Definition: glext.h:9393
voidpf MSZipAlloc(voidpf opaque, uInt items, uInt size)
Definition: mszip.cxx:21
void MSZipFree(voidpf opaque, voidpf address)
Definition: mszip.cxx:27
unsigned int UINT
Definition: ndis.h:50
static TCHAR * items[]
Definition: page1.c:45
unsigned short USHORT
Definition: pedump.c:61
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
Definition: zlib.h:1814
#define inflateInit2(strm, windowBits)
Definition: zlib.h:1817
#define MSZIP_MAGIC
Definition: mszip.h:13
#define DPRINT
Definition: sndvol32.h:73
uInt avail_in
Definition: zlib.h:60
z_const Bytef * next_in
Definition: zlib.h:59
alloc_func zalloc
Definition: zlib.h:70
uInt avail_out
Definition: zlib.h:64
Bytef * next_out
Definition: zlib.h:63
z_const char * msg
Definition: zlib.h:67
free_func zfree
Definition: zlib.h:71
uLong total_in
Definition: zlib.h:61
voidpf opaque
Definition: zlib.h:72
uLong total_out
Definition: zlib.h:65
uint32_t * PULONG
Definition: typedefs.h:59
uint16_t * PUSHORT
Definition: typedefs.h:56
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR OutputBuffer
Definition: wdfiotarget.h:863
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR InputBuffer
Definition: wdfiotarget.h:953