[PATCH] d3dxof: Add support for mszip compressed files (try 2)
Christian Costa
titan.costa at wanadoo.fr
Mon Apr 12 04:21:33 CDT 2010
--
Add some tests with real compressed templates and objects files.
Add some comments.
MSZIP decompression code is taken from cabinet dll (fdi.c and cabinet.h)
with minimum modifications.
---
dlls/d3dxof/Makefile.in | 1
dlls/d3dxof/d3dxof.c | 94 +++++-
dlls/d3dxof/d3dxof_private.h | 3
dlls/d3dxof/mszip.c | 645 ++++++++++++++++++++++++++++++++++++++++++
dlls/d3dxof/mszip.h | 130 ++++++++
dlls/d3dxof/tests/d3dxof.c | 71 ++++-
6 files changed, 928 insertions(+), 16 deletions(-)
create mode 100644 dlls/d3dxof/mszip.c
create mode 100644 dlls/d3dxof/mszip.h
-------------- next part --------------
diff --git a/dlls/d3dxof/Makefile.in b/dlls/d3dxof/Makefile.in
index 751349c..897f2e5 100644
--- a/dlls/d3dxof/Makefile.in
+++ b/dlls/d3dxof/Makefile.in
@@ -9,6 +9,7 @@ IMPORTS = dxguid uuid ole32 advapi32 kernel32
C_SRCS = \
d3dxof.c \
main.c \
+ mszip.c \
parsing.c \
regsvr.c
diff --git a/dlls/d3dxof/d3dxof.c b/dlls/d3dxof/d3dxof.c
index 507664f..bfa761b 100644
--- a/dlls/d3dxof/d3dxof.c
+++ b/dlls/d3dxof/d3dxof.c
@@ -1,7 +1,7 @@
/*
* Implementation of DirectX File Interfaces
*
- * Copyright 2004, 2008 Christian Costa
+ * Copyright 2004, 2008, 2010 Christian Costa
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -140,6 +140,8 @@ static HRESULT WINAPI IDirectXFileImpl_CreateEnumObject(IDirectXFile* iface, LPV
HANDLE file_mapping = 0;
LPBYTE buffer = NULL;
HGLOBAL resource_data = 0;
+ LPBYTE decomp_buffer = NULL;
+ DWORD decomp_size = 0;
LPBYTE file_buffer;
DWORD file_size;
@@ -265,9 +267,32 @@ static HRESULT WINAPI IDirectXFileImpl_CreateEnumObject(IDirectXFile* iface, LPV
if ((header[2] == XOFFILE_FORMAT_BINARY_MSZIP) || (header[2] == XOFFILE_FORMAT_TEXT_MSZIP))
{
- FIXME("Compressed format %s not supported yet\n", debugstr_fourcc(header[2]));
- hr = DXFILEERR_BADALLOC;
- goto error;
+ int err;
+ DWORD comp_size;
+
+ /* 0-15 -> xfile header, 16-17 -> decompressed size w/ header, 18-19 -> null,
+ 20-21 -> decompressed size w/o header, 22-23 -> size of MSZIP compressed data,
+ 24-xx -> compressed MSZIP data */
+ decomp_size = ((WORD*)file_buffer)[10];
+ comp_size = ((WORD*)file_buffer)[11];
+
+ TRACE("Compressed format %s detected: compressed_size = %x, decompressed_size = %x\n",
+ debugstr_fourcc(header[2]), comp_size, decomp_size);
+
+ decomp_buffer = HeapAlloc(GetProcessHeap(), 0, decomp_size);
+ if (!decomp_buffer)
+ {
+ ERR("Out of memory\n");
+ hr = DXFILEERR_BADALLOC;
+ goto error;
+ }
+ err = mszip_decompress(comp_size, decomp_size, (char*)file_buffer + 24, (char*)decomp_buffer);
+ if (err)
+ {
+ WARN("Error while decomrpessing mszip archive %d\n", err);
+ hr = DXFILEERR_BADALLOC;
+ goto error;
+ }
}
if ((header[3] != XOFFILE_FORMAT_FLOAT_BITS_32) && (header[3] != XOFFILE_FORMAT_FLOAT_BITS_64))
@@ -286,16 +311,26 @@ static HRESULT WINAPI IDirectXFileImpl_CreateEnumObject(IDirectXFile* iface, LPV
object->hFile = hFile;
object->file_mapping = file_mapping;
object->buffer = buffer;
+ object->decomp_buffer = decomp_buffer;
object->pDirectXFile = This;
object->buf.pdxf = This;
- object->buf.txt = (header[2] == XOFFILE_FORMAT_TEXT);
+ object->buf.txt = (header[2] == XOFFILE_FORMAT_TEXT) || (header[2] == XOFFILE_FORMAT_TEXT_MSZIP);
object->buf.token_present = FALSE;
TRACE("File size is %d bytes\n", file_size);
- /* Go to data after header */
- object->buf.buffer = file_buffer + 16;
- object->buf.rem_bytes = file_size - 16;
+ if (decomp_size)
+ {
+ /* Use decompressed data */
+ object->buf.buffer = decomp_buffer;
+ object->buf.rem_bytes = decomp_size;
+ }
+ else
+ {
+ /* Go to data after header */
+ object->buf.buffer = file_buffer + 16;
+ object->buf.rem_bytes = file_size - 16;
+ }
*ppEnumObj = (LPDIRECTXFILEENUMOBJECT)object;
@@ -334,6 +369,7 @@ error:
CloseHandle(hFile);
if (resource_data)
FreeResource(resource_data);
+ HeapFree(GetProcessHeap(), 0, decomp_buffer);
*ppEnumObj = NULL;
return hr;
@@ -356,6 +392,8 @@ static HRESULT WINAPI IDirectXFileImpl_RegisterTemplates(IDirectXFile* iface, LP
IDirectXFileImpl *This = (IDirectXFileImpl *)iface;
DWORD token_header;
parse_buffer buf;
+ LPBYTE decomp_buffer = NULL;
+ DWORD decomp_size = 0;
buf.buffer = pvData;
buf.rem_bytes = cbSize;
@@ -400,11 +438,34 @@ static HRESULT WINAPI IDirectXFileImpl_RegisterTemplates(IDirectXFile* iface, LP
if ((token_header == XOFFILE_FORMAT_BINARY_MSZIP) || (token_header == XOFFILE_FORMAT_TEXT_MSZIP))
{
- FIXME("Compressed format %s not supported yet\n", debugstr_fourcc(token_header));
- return DXFILEERR_BADALLOC;
+ int err;
+ DWORD comp_size;
+
+ /* 0-15 -> xfile header, 16-17 -> decompressed size w/ header, 18-19 -> null,
+ 20-21 -> decompressed size w/o header, 22-23 -> size of MSZIP compressed data,
+ 24-xx -> compressed MSZIP data */
+ decomp_size = ((WORD*)pvData)[10];
+ comp_size = ((WORD*)pvData)[11];
+
+ TRACE("Compressed format %s detected: compressed_size = %x, decompressed_size = %x\n",
+ debugstr_fourcc(token_header), comp_size, decomp_size);
+
+ decomp_buffer = HeapAlloc(GetProcessHeap(), 0, decomp_size);
+ if (!decomp_buffer)
+ {
+ ERR("Out of memory\n");
+ return DXFILEERR_BADALLOC;
+ }
+ err = mszip_decompress(comp_size, decomp_size, (char*)pvData + 24, (char*)decomp_buffer);
+ if (err)
+ {
+ WARN("Error while decomrpessing mszip archive %d\n", err);
+ HeapFree(GetProcessHeap(), 0, decomp_buffer);
+ return DXFILEERR_BADALLOC;
+ }
}
- if (token_header == XOFFILE_FORMAT_TEXT)
+ if ((token_header == XOFFILE_FORMAT_TEXT) || (token_header == XOFFILE_FORMAT_TEXT_MSZIP))
buf.txt = TRUE;
read_bytes(&buf, &token_header, 4);
@@ -414,7 +475,13 @@ static HRESULT WINAPI IDirectXFileImpl_RegisterTemplates(IDirectXFile* iface, LP
TRACE("Header is correct\n");
- while (buf.rem_bytes)
+ if (decomp_size)
+ {
+ buf.buffer = decomp_buffer;
+ buf.rem_bytes = decomp_size;
+ }
+
+ while (buf.rem_bytes && is_template_available(&buf))
{
if (!parse_template(&buf))
{
@@ -437,6 +504,8 @@ static HRESULT WINAPI IDirectXFileImpl_RegisterTemplates(IDirectXFile* iface, LP
DPRINTF("%s - %s\n", This->xtemplates[i].name, debugstr_guid(&This->xtemplates[i].class_id));
}
+ HeapFree(GetProcessHeap(), 0, decomp_buffer);
+
return DXFILE_OK;
}
@@ -1024,6 +1093,7 @@ static ULONG WINAPI IDirectXFileEnumObjectImpl_Release(IDirectXFileEnumObject* i
}
else if (This->source == DXFILELOAD_FROMRESOURCE)
FreeResource(This->resource_data);
+ HeapFree(GetProcessHeap(), 0, This->decomp_buffer);
HeapFree(GetProcessHeap(), 0, This);
}
diff --git a/dlls/d3dxof/d3dxof_private.h b/dlls/d3dxof/d3dxof_private.h
index a8534ac..1bd2a6d 100644
--- a/dlls/d3dxof/d3dxof_private.h
+++ b/dlls/d3dxof/d3dxof_private.h
@@ -151,6 +151,7 @@ typedef struct {
HANDLE file_mapping;
LPBYTE buffer;
HGLOBAL resource_data;
+ LPBYTE decomp_buffer;
parse_buffer buf;
IDirectXFileImpl* pDirectXFile;
ULONG nb_xobjects;
@@ -173,4 +174,6 @@ void dump_template(xtemplate* templates_array, xtemplate* ptemplate);
BOOL is_template_available(parse_buffer * buf);
BOOL parse_object(parse_buffer * buf);
+int mszip_decompress(int inlen, int outlen, char* inbuffer, char* outbuffer);
+
#endif /* __D3DXOF_PRIVATE_INCLUDED__ */
diff --git a/dlls/d3dxof/mszip.c b/dlls/d3dxof/mszip.c
new file mode 100644
index 0000000..3cf89cc
--- /dev/null
+++ b/dlls/d3dxof/mszip.c
@@ -0,0 +1,645 @@
+/*
+ * MSZIP decompression (taken from fdi.c of cabinet dll)
+ *
+ * Copyright 2000-2002 Stuart Caie
+ * Copyright 2002 Patrik Stridvall
+ * Copyright 2003 Greg Turner
+ * Copyright 2010 Christian Costa
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include <stdarg.h>
+
+#include "windef.h"
+#include "winbase.h"
+
+#include "wine/debug.h"
+
+#include "mszip.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(d3dxof);
+
+THOSE_ZIP_CONSTS;
+
+/********************************************************
+ * Ziphuft_free (internal)
+ */
+static void fdi_Ziphuft_free(HFDI hfdi, struct Ziphuft *t)
+{
+ register struct Ziphuft *p, *q;
+
+ /* Go through linked list, freeing from the allocated (t[-1]) address. */
+ p = t;
+ while (p != NULL)
+ {
+ q = (--p)->v.t;
+ PFDI_FREE(hfdi, p);
+ p = q;
+ }
+}
+
+/*********************************************************
+ * fdi_Ziphuft_build (internal)
+ */
+static cab_LONG fdi_Ziphuft_build(cab_ULONG *b, cab_ULONG n, cab_ULONG s, const cab_UWORD *d, const cab_UWORD *e,
+struct Ziphuft **t, cab_LONG *m, fdi_decomp_state *decomp_state)
+{
+ cab_ULONG a; /* counter for codes of length k */
+ cab_ULONG el; /* length of EOB code (value 256) */
+ cab_ULONG f; /* i repeats in table every f entries */
+ cab_LONG g; /* maximum code length */
+ cab_LONG h; /* table level */
+ register cab_ULONG i; /* counter, current code */
+ register cab_ULONG j; /* counter */
+ register cab_LONG k; /* number of bits in current code */
+ cab_LONG *l; /* stack of bits per table */
+ register cab_ULONG *p; /* pointer into ZIP(c)[],ZIP(b)[],ZIP(v)[] */
+ register struct Ziphuft *q; /* points to current table */
+ struct Ziphuft r; /* table entry for structure assignment */
+ register cab_LONG w; /* bits before this table == (l * h) */
+ cab_ULONG *xp; /* pointer into x */
+ cab_LONG y; /* number of dummy codes added */
+ cab_ULONG z; /* number of entries in current table */
+
+ l = ZIP(lx)+1;
+
+ /* Generate counts for each bit length */
+ el = n > 256 ? b[256] : ZIPBMAX; /* set length of EOB code, if any */
+
+ for(i = 0; i < ZIPBMAX+1; ++i)
+ ZIP(c)[i] = 0;
+ p = b; i = n;
+ do
+ {
+ ZIP(c)[*p]++; p++; /* assume all entries <= ZIPBMAX */
+ } while (--i);
+ if (ZIP(c)[0] == n) /* null input--all zero length codes */
+ {
+ *t = NULL;
+ *m = 0;
+ return 0;
+ }
+
+ /* Find minimum and maximum length, bound *m by those */
+ for (j = 1; j <= ZIPBMAX; j++)
+ if (ZIP(c)[j])
+ break;
+ k = j; /* minimum code length */
+ if ((cab_ULONG)*m < j)
+ *m = j;
+ for (i = ZIPBMAX; i; i--)
+ if (ZIP(c)[i])
+ break;
+ g = i; /* maximum code length */
+ if ((cab_ULONG)*m > i)
+ *m = i;
+
+ /* Adjust last length count to fill out codes, if needed */
+ for (y = 1 << j; j < i; j++, y <<= 1)
+ if ((y -= ZIP(c)[j]) < 0)
+ return 2; /* bad input: more codes than bits */
+ if ((y -= ZIP(c)[i]) < 0)
+ return 2;
+ ZIP(c)[i] += y;
+
+ /* Generate starting offsets LONGo the value table for each length */
+ ZIP(x)[1] = j = 0;
+ p = ZIP(c) + 1; xp = ZIP(x) + 2;
+ while (--i)
+ { /* note that i == g from above */
+ *xp++ = (j += *p++);
+ }
+
+ /* Make a table of values in order of bit lengths */
+ p = b; i = 0;
+ do{
+ if ((j = *p++) != 0)
+ ZIP(v)[ZIP(x)[j]++] = i;
+ } while (++i < n);
+
+
+ /* Generate the Huffman codes and for each, make the table entries */
+ ZIP(x)[0] = i = 0; /* first Huffman code is zero */
+ p = ZIP(v); /* grab values in bit order */
+ h = -1; /* no tables yet--level -1 */
+ w = l[-1] = 0; /* no bits decoded yet */
+ ZIP(u)[0] = NULL; /* just to keep compilers happy */
+ q = NULL; /* ditto */
+ z = 0; /* ditto */
+
+ /* go through the bit lengths (k already is bits in shortest code) */
+ for (; k <= g; k++)
+ {
+ a = ZIP(c)[k];
+ while (a--)
+ {
+ /* here i is the Huffman code of length k bits for value *p */
+ /* make tables up to required level */
+ while (k > w + l[h])
+ {
+ w += l[h++]; /* add bits already decoded */
+
+ /* compute minimum size table less than or equal to *m bits */
+ if ((z = g - w) > (cab_ULONG)*m) /* upper limit */
+ z = *m;
+ if ((f = 1 << (j = k - w)) > a + 1) /* try a k-w bit table */
+ { /* too few codes for k-w bit table */
+ f -= a + 1; /* deduct codes from patterns left */
+ xp = ZIP(c) + k;
+ while (++j < z) /* try smaller tables up to z bits */
+ {
+ if ((f <<= 1) <= *++xp)
+ break; /* enough codes to use up j bits */
+ f -= *xp; /* else deduct codes from patterns */
+ }
+ }
+ if ((cab_ULONG)w + j > el && (cab_ULONG)w < el)
+ j = el - w; /* make EOB code end at table */
+ z = 1 << j; /* table entries for j-bit table */
+ l[h] = j; /* set table size in stack */
+
+ /* allocate and link in new table */
+ if (!(q = PFDI_ALLOC(CAB(hfdi), (z + 1)*sizeof(struct Ziphuft))))
+ {
+ if(h)
+ fdi_Ziphuft_free(CAB(hfdi), ZIP(u)[0]);
+ return 3; /* not enough memory */
+ }
+ *t = q + 1; /* link to list for Ziphuft_free() */
+ *(t = &(q->v.t)) = NULL;
+ ZIP(u)[h] = ++q; /* table starts after link */
+
+ /* connect to last table, if there is one */
+ if (h)
+ {
+ ZIP(x)[h] = i; /* save pattern for backing up */
+ r.b = (cab_UBYTE)l[h-1]; /* bits to dump before this table */
+ r.e = (cab_UBYTE)(16 + j); /* bits in this table */
+ r.v.t = q; /* pointer to this table */
+ j = (i & ((1 << w) - 1)) >> (w - l[h-1]);
+ ZIP(u)[h-1][j] = r; /* connect to last table */
+ }
+ }
+
+ /* set up table entry in r */
+ r.b = (cab_UBYTE)(k - w);
+ if (p >= ZIP(v) + n)
+ r.e = 99; /* out of values--invalid code */
+ else if (*p < s)
+ {
+ r.e = (cab_UBYTE)(*p < 256 ? 16 : 15); /* 256 is end-of-block code */
+ r.v.n = *p++; /* simple code is just the value */
+ }
+ else
+ {
+ r.e = (cab_UBYTE)e[*p - s]; /* non-simple--look up in lists */
+ r.v.n = d[*p++ - s];
+ }
+
+ /* fill code-like entries with r */
+ f = 1 << (k - w);
+ for (j = i >> w; j < z; j += f)
+ q[j] = r;
+
+ /* backwards increment the k-bit code i */
+ for (j = 1 << (k - 1); i & j; j >>= 1)
+ i ^= j;
+ i ^= j;
+
+ /* backup over finished tables */
+ while ((i & ((1 << w) - 1)) != ZIP(x)[h])
+ w -= l[--h]; /* don't need to update q */
+ }
+ }
+
+ /* return actual size of base table */
+ *m = l[0];
+
+ /* Return true (1) if we were given an incomplete table */
+ return y != 0 && g != 1;
+}
+
+/*********************************************************
+ * fdi_Zipinflate_codes (internal)
+ */
+static cab_LONG fdi_Zipinflate_codes(const struct Ziphuft *tl, const struct Ziphuft *td,
+ cab_LONG bl, cab_LONG bd, fdi_decomp_state *decomp_state)
+{
+ register cab_ULONG e; /* table entry flag/number of extra bits */
+ cab_ULONG n, d; /* length and index for copy */
+ cab_ULONG w; /* current window position */
+ const struct Ziphuft *t; /* pointer to table entry */
+ cab_ULONG ml, md; /* masks for bl and bd bits */
+ register cab_ULONG b; /* bit buffer */
+ register cab_ULONG k; /* number of bits in bit buffer */
+
+ /* make local copies of globals */
+ b = ZIP(bb); /* initialize bit buffer */
+ k = ZIP(bk);
+ w = ZIP(window_posn); /* initialize window position */
+
+ /* inflate the coded data */
+ ml = Zipmask[bl]; /* precompute masks for speed */
+ md = Zipmask[bd];
+
+ for(;;)
+ {
+ ZIPNEEDBITS((cab_ULONG)bl)
+ if((e = (t = tl + (b & ml))->e) > 16)
+ do
+ {
+ if (e == 99)
+ return 1;
+ ZIPDUMPBITS(t->b)
+ e -= 16;
+ ZIPNEEDBITS(e)
+ } while ((e = (t = t->v.t + (b & Zipmask[e]))->e) > 16);
+ ZIPDUMPBITS(t->b)
+ if (e == 16) /* then it's a literal */
+ CAB(outbuf)[w++] = (cab_UBYTE)t->v.n;
+ else /* it's an EOB or a length */
+ {
+ /* exit if end of block */
+ if(e == 15)
+ break;
+
+ /* get length of block to copy */
+ ZIPNEEDBITS(e)
+ n = t->v.n + (b & Zipmask[e]);
+ ZIPDUMPBITS(e);
+
+ /* decode distance of block to copy */
+ ZIPNEEDBITS((cab_ULONG)bd)
+ if ((e = (t = td + (b & md))->e) > 16)
+ do {
+ if (e == 99)
+ return 1;
+ ZIPDUMPBITS(t->b)
+ e -= 16;
+ ZIPNEEDBITS(e)
+ } while ((e = (t = t->v.t + (b & Zipmask[e]))->e) > 16);
+ ZIPDUMPBITS(t->b)
+ ZIPNEEDBITS(e)
+ d = w - t->v.n - (b & Zipmask[e]);
+ ZIPDUMPBITS(e)
+ do
+ {
+ d &= ZIPWSIZE - 1;
+ e = ZIPWSIZE - max(d, w);
+ e = min(e, n);
+ n -= e;
+ do
+ {
+ CAB(outbuf)[w++] = CAB(outbuf)[d++];
+ } while (--e);
+ } while (n);
+ }
+ }
+
+ /* restore the globals from the locals */
+ ZIP(window_posn) = w; /* restore global window pointer */
+ ZIP(bb) = b; /* restore global bit buffer */
+ ZIP(bk) = k;
+
+ /* done */
+ return 0;
+}
+
+/***********************************************************
+ * Zipinflate_stored (internal)
+ */
+static cab_LONG fdi_Zipinflate_stored(fdi_decomp_state *decomp_state)
+/* "decompress" an inflated type 0 (stored) block. */
+{
+ cab_ULONG n; /* number of bytes in block */
+ cab_ULONG w; /* current window position */
+ register cab_ULONG b; /* bit buffer */
+ register cab_ULONG k; /* number of bits in bit buffer */
+
+ /* make local copies of globals */
+ b = ZIP(bb); /* initialize bit buffer */
+ k = ZIP(bk);
+ w = ZIP(window_posn); /* initialize window position */
+
+ /* go to byte boundary */
+ n = k & 7;
+ ZIPDUMPBITS(n);
+
+ /* get the length and its complement */
+ ZIPNEEDBITS(16)
+ n = (b & 0xffff);
+ ZIPDUMPBITS(16)
+ ZIPNEEDBITS(16)
+ if (n != ((~b) & 0xffff))
+ return 1; /* error in compressed data */
+ ZIPDUMPBITS(16)
+
+ /* read and output the compressed data */
+ while(n--)
+ {
+ ZIPNEEDBITS(8)
+ CAB(outbuf)[w++] = (cab_UBYTE)b;
+ ZIPDUMPBITS(8)
+ }
+
+ /* restore the globals from the locals */
+ ZIP(window_posn) = w; /* restore global window pointer */
+ ZIP(bb) = b; /* restore global bit buffer */
+ ZIP(bk) = k;
+ return 0;
+}
+
+/******************************************************
+ * fdi_Zipinflate_fixed (internal)
+ */
+static cab_LONG fdi_Zipinflate_fixed(fdi_decomp_state *decomp_state)
+{
+ struct Ziphuft *fixed_tl;
+ struct Ziphuft *fixed_td;
+ cab_LONG fixed_bl, fixed_bd;
+ cab_LONG i; /* temporary variable */
+ cab_ULONG *l;
+
+ l = ZIP(ll);
+
+ /* literal table */
+ for(i = 0; i < 144; i++)
+ l[i] = 8;
+ for(; i < 256; i++)
+ l[i] = 9;
+ for(; i < 280; i++)
+ l[i] = 7;
+ for(; i < 288; i++) /* make a complete, but wrong code set */
+ l[i] = 8;
+ fixed_bl = 7;
+ if((i = fdi_Ziphuft_build(l, 288, 257, Zipcplens, Zipcplext, &fixed_tl, &fixed_bl, decomp_state)))
+ return i;
+
+ /* distance table */
+ for(i = 0; i < 30; i++) /* make an incomplete code set */
+ l[i] = 5;
+ fixed_bd = 5;
+ if((i = fdi_Ziphuft_build(l, 30, 0, Zipcpdist, Zipcpdext, &fixed_td, &fixed_bd, decomp_state)) > 1)
+ {
+ fdi_Ziphuft_free(CAB(hfdi), fixed_tl);
+ return i;
+ }
+
+ /* decompress until an end-of-block code */
+ i = fdi_Zipinflate_codes(fixed_tl, fixed_td, fixed_bl, fixed_bd, decomp_state);
+
+ fdi_Ziphuft_free(CAB(hfdi), fixed_td);
+ fdi_Ziphuft_free(CAB(hfdi), fixed_tl);
+ return i;
+}
+
+/**************************************************************
+ * fdi_Zipinflate_dynamic (internal)
+ */
+static cab_LONG fdi_Zipinflate_dynamic(fdi_decomp_state *decomp_state)
+ /* decompress an inflated type 2 (dynamic Huffman codes) block. */
+{
+ cab_LONG i; /* temporary variables */
+ cab_ULONG j;
+ cab_ULONG *ll;
+ cab_ULONG l; /* last length */
+ cab_ULONG m; /* mask for bit lengths table */
+ cab_ULONG n; /* number of lengths to get */
+ struct Ziphuft *tl; /* literal/length code table */
+ struct Ziphuft *td; /* distance code table */
+ cab_LONG bl; /* lookup bits for tl */
+ cab_LONG bd; /* lookup bits for td */
+ cab_ULONG nb; /* number of bit length codes */
+ cab_ULONG nl; /* number of literal/length codes */
+ cab_ULONG nd; /* number of distance codes */
+ register cab_ULONG b; /* bit buffer */
+ register cab_ULONG k; /* number of bits in bit buffer */
+
+ /* make local bit buffer */
+ b = ZIP(bb);
+ k = ZIP(bk);
+ ll = ZIP(ll);
+
+ /* read in table lengths */
+ ZIPNEEDBITS(5)
+ nl = 257 + (b & 0x1f); /* number of literal/length codes */
+ ZIPDUMPBITS(5)
+ ZIPNEEDBITS(5)
+ nd = 1 + (b & 0x1f); /* number of distance codes */
+ ZIPDUMPBITS(5)
+ ZIPNEEDBITS(4)
+ nb = 4 + (b & 0xf); /* number of bit length codes */
+ ZIPDUMPBITS(4)
+ if(nl > 288 || nd > 32)
+ return 1; /* bad lengths */
+
+ /* read in bit-length-code lengths */
+ for(j = 0; j < nb; j++)
+ {
+ ZIPNEEDBITS(3)
+ ll[Zipborder[j]] = b & 7;
+ ZIPDUMPBITS(3)
+ }
+ for(; j < 19; j++)
+ ll[Zipborder[j]] = 0;
+
+ /* build decoding table for trees--single level, 7 bit lookup */
+ bl = 7;
+ if((i = fdi_Ziphuft_build(ll, 19, 19, NULL, NULL, &tl, &bl, decomp_state)) != 0)
+ {
+ if(i == 1)
+ fdi_Ziphuft_free(CAB(hfdi), tl);
+ return i; /* incomplete code set */
+ }
+
+ /* read in literal and distance code lengths */
+ n = nl + nd;
+ m = Zipmask[bl];
+ i = l = 0;
+ while((cab_ULONG)i < n)
+ {
+ ZIPNEEDBITS((cab_ULONG)bl)
+ j = (td = tl + (b & m))->b;
+ ZIPDUMPBITS(j)
+ j = td->v.n;
+ if (j < 16) /* length of code in bits (0..15) */
+ ll[i++] = l = j; /* save last length in l */
+ else if (j == 16) /* repeat last length 3 to 6 times */
+ {
+ ZIPNEEDBITS(2)
+ j = 3 + (b & 3);
+ ZIPDUMPBITS(2)
+ if((cab_ULONG)i + j > n)
+ return 1;
+ while (j--)
+ ll[i++] = l;
+ }
+ else if (j == 17) /* 3 to 10 zero length codes */
+ {
+ ZIPNEEDBITS(3)
+ j = 3 + (b & 7);
+ ZIPDUMPBITS(3)
+ if ((cab_ULONG)i + j > n)
+ return 1;
+ while (j--)
+ ll[i++] = 0;
+ l = 0;
+ }
+ else /* j == 18: 11 to 138 zero length codes */
+ {
+ ZIPNEEDBITS(7)
+ j = 11 + (b & 0x7f);
+ ZIPDUMPBITS(7)
+ if ((cab_ULONG)i + j > n)
+ return 1;
+ while (j--)
+ ll[i++] = 0;
+ l = 0;
+ }
+ }
+
+ /* free decoding table for trees */
+ fdi_Ziphuft_free(CAB(hfdi), tl);
+
+ /* restore the global bit buffer */
+ ZIP(bb) = b;
+ ZIP(bk) = k;
+
+ /* build the decoding tables for literal/length and distance codes */
+ bl = ZIPLBITS;
+ if((i = fdi_Ziphuft_build(ll, nl, 257, Zipcplens, Zipcplext, &tl, &bl, decomp_state)) != 0)
+ {
+ if(i == 1)
+ fdi_Ziphuft_free(CAB(hfdi), tl);
+ return i; /* incomplete code set */
+ }
+ bd = ZIPDBITS;
+ fdi_Ziphuft_build(ll + nl, nd, 0, Zipcpdist, Zipcpdext, &td, &bd, decomp_state);
+
+ /* decompress until an end-of-block code */
+ if(fdi_Zipinflate_codes(tl, td, bl, bd, decomp_state))
+ return 1;
+
+ /* free the decoding tables, return */
+ fdi_Ziphuft_free(CAB(hfdi), tl);
+ fdi_Ziphuft_free(CAB(hfdi), td);
+ return 0;
+}
+
+/*****************************************************
+ * fdi_Zipinflate_block (internal)
+ */
+static cab_LONG fdi_Zipinflate_block(cab_LONG *e, fdi_decomp_state *decomp_state) /* e == last block flag */
+{ /* decompress an inflated block */
+ cab_ULONG t; /* block type */
+ register cab_ULONG b; /* bit buffer */
+ register cab_ULONG k; /* number of bits in bit buffer */
+
+ /* make local bit buffer */
+ b = ZIP(bb);
+ k = ZIP(bk);
+
+ /* read in last block bit */
+ ZIPNEEDBITS(1)
+ *e = (cab_LONG)b & 1;
+ ZIPDUMPBITS(1)
+
+ /* read in block type */
+ ZIPNEEDBITS(2)
+ t = b & 3;
+ ZIPDUMPBITS(2)
+
+ /* restore the global bit buffer */
+ ZIP(bb) = b;
+ ZIP(bk) = k;
+
+ /* inflate that block type */
+ if(t == 2)
+ return fdi_Zipinflate_dynamic(decomp_state);
+ if(t == 0)
+ return fdi_Zipinflate_stored(decomp_state);
+ if(t == 1)
+ return fdi_Zipinflate_fixed(decomp_state);
+ /* bad block type */
+ return 2;
+}
+
+/****************************************************
+ * ZIPfdi_decomp(internal)
+ */
+static int ZIPfdi_decomp(int inlen, int outlen, fdi_decomp_state *decomp_state)
+{
+ cab_LONG e; /* last block flag */
+
+ TRACE("(inlen == %d, outlen == %d)\n", inlen, outlen);
+
+ ZIP(inpos) = CAB(inbuf);
+ ZIP(bb) = ZIP(bk) = ZIP(window_posn) = 0;
+
+ if(outlen > ZIPWSIZE)
+ return DECR_DATAFORMAT;
+
+ /* CK = Chris Kirmse, official Microsoft purloiner */
+ if(ZIP(inpos)[0] != 0x43 || ZIP(inpos)[1] != 0x4B)
+ return DECR_ILLEGALDATA;
+
+ ZIP(inpos) += 2;
+
+ do {
+ if(fdi_Zipinflate_block(&e, decomp_state))
+ return DECR_ILLEGALDATA;
+ } while(!e);
+
+ /* return success */
+ return DECR_OK;
+}
+
+void * __cdecl fdi_alloc(ULONG cb)
+{
+ return HeapAlloc(GetProcessHeap(), 0, cb);
+}
+
+void __cdecl fdi_free(void *pv)
+{
+ HeapFree(GetProcessHeap(), 0, pv);
+}
+
+int mszip_decompress(unsigned int inlen, unsigned int outlen, char* inbuffer, char* outbuffer)
+{
+ int ret;
+ fdi_decomp_state decomp_state;
+ FDI_Int fdi;
+
+ TRACE("(%u, %u, %p, %p)\n", inlen, outlen, inbuffer, outbuffer);
+
+ if ((inlen > CAB_INPUTMAX) || (outlen > CAB_BLOCKMAX))
+ {
+ FIXME("Big file not supported yet (inlen = %u, outlen = %u)\n", inlen, outlen);
+ return DECR_DATAFORMAT;
+ }
+
+ fdi.pfnalloc = fdi_alloc;
+ fdi.pfnfree = fdi_free;
+ decomp_state.hfdi = (void*)&fdi;
+
+ memcpy(decomp_state.inbuf, inbuffer, inlen);
+
+ ret = ZIPfdi_decomp(inlen, outlen, &decomp_state);
+
+ memcpy(outbuffer, decomp_state.outbuf, outlen);
+
+ return ret;
+}
diff --git a/dlls/d3dxof/mszip.h b/dlls/d3dxof/mszip.h
new file mode 100644
index 0000000..23ff677
--- /dev/null
+++ b/dlls/d3dxof/mszip.h
@@ -0,0 +1,130 @@
+/*
+ * MSZIP decompression header (taken from cabinet.h of cabinet dll)
+ *
+ * Copyright 2002 Greg Turner
+ * Copyright 2005 Gerold Jens Wucherpfennig
+ * Copyright 2010 Christian Costa
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include "fdi.h"
+
+typedef unsigned char cab_UBYTE; /* 8 bits */
+typedef UINT16 cab_UWORD; /* 16 bits */
+typedef UINT32 cab_ULONG; /* 32 bits */
+typedef INT32 cab_LONG; /* 32 bits */
+
+typedef struct {
+ unsigned int FDI_Intmagic;
+ PFNALLOC pfnalloc;
+ PFNFREE pfnfree;
+ PFNOPEN pfnopen;
+ PFNREAD pfnread;
+ PFNWRITE pfnwrite;
+ PFNCLOSE pfnclose;
+ PFNSEEK pfnseek;
+ PERF perf;
+} FDI_Int, *PFDI_Int;
+
+/* cast an HFDI into a PFDI_Int */
+#define PFDI_INT(hfdi) ((PFDI_Int)(hfdi))
+
+#define PFDI_ALLOC(hfdi, size) ((*PFDI_INT(hfdi)->pfnalloc) (size))
+#define PFDI_FREE(hfdi, ptr) ((*PFDI_INT(hfdi)->pfnfree) (ptr))
+
+/* MSZIP stuff */
+#define ZIPWSIZE 0x8000 /* window size */
+#define ZIPLBITS 9 /* bits in base literal/length lookup table */
+#define ZIPDBITS 6 /* bits in base distance lookup table */
+#define ZIPBMAX 16 /* maximum bit length of any code */
+#define ZIPN_MAX 288 /* maximum number of codes in any set */
+
+struct Ziphuft {
+ cab_UBYTE e; /* number of extra bits or operation */
+ cab_UBYTE b; /* number of bits in this code or subcode */
+ union {
+ cab_UWORD n; /* literal, length base, or distance base */
+ struct Ziphuft *t; /* pointer to next level of table */
+ } v;
+};
+
+struct ZIPstate {
+ cab_ULONG window_posn; /* current offset within the window */
+ cab_ULONG bb; /* bit buffer */
+ cab_ULONG bk; /* bits in bit buffer */
+ cab_ULONG ll[288+32]; /* literal/length and distance code lengths */
+ cab_ULONG c[ZIPBMAX+1]; /* bit length count table */
+ cab_LONG lx[ZIPBMAX+1]; /* memory for l[-1..ZIPBMAX-1] */
+ struct Ziphuft *u[ZIPBMAX]; /* table stack */
+ cab_ULONG v[ZIPN_MAX]; /* values in order of bit length */
+ cab_ULONG x[ZIPBMAX+1]; /* bit offsets, then code stack */
+ cab_UBYTE *inpos;
+};
+
+#define CAB(x) (decomp_state->x)
+#define ZIP(x) (decomp_state->methods.zip.x)
+#define DECR_OK (0)
+#define DECR_DATAFORMAT (1)
+#define DECR_ILLEGALDATA (2)
+#define DECR_NOMEMORY (3)
+#define DECR_CHECKSUM (4)
+#define DECR_INPUT (5)
+#define DECR_OUTPUT (6)
+#define DECR_USERABORT (7)
+
+#define ZIPNEEDBITS(n) {while(k<(n)){cab_LONG c=*(ZIP(inpos)++);\
+ b|=((cab_ULONG)c)<<k;k+=8;}}
+#define ZIPDUMPBITS(n) {b>>=(n);k-=(n);}
+
+/* CAB data blocks are <= 32768 bytes in uncompressed form. Uncompressed
+ * blocks have zero growth. MSZIP guarantees that it won't grow above
+ * uncompressed size by more than 12 bytes. LZX guarantees it won't grow
+ * more than 6144 bytes.
+ */
+#define CAB_BLOCKMAX (32768)
+#define CAB_INPUTMAX (CAB_BLOCKMAX+6144)
+
+typedef struct fdi_cds_fwd {
+ void *hfdi; /* the hfdi we are using */
+ cab_UBYTE inbuf[CAB_INPUTMAX+2]; /* +2 for lzx bitbuffer overflows! */
+ cab_UBYTE outbuf[CAB_BLOCKMAX];
+ union {
+ struct ZIPstate zip;
+ } methods;
+} fdi_decomp_state;
+
+/* Tables for deflate from PKZIP's appnote.txt. */
+
+#define THOSE_ZIP_CONSTS \
+static const cab_UBYTE Zipborder[] = /* Order of the bit length code lengths */ \
+{ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; \
+static const cab_UWORD Zipcplens[] = /* Copy lengths for literal codes 257..285 */ \
+{ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, \
+ 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; \
+static const cab_UWORD Zipcplext[] = /* Extra bits for literal codes 257..285 */ \
+{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, \
+ 4, 5, 5, 5, 5, 0, 99, 99}; /* 99==invalid */ \
+static const cab_UWORD Zipcpdist[] = /* Copy offsets for distance codes 0..29 */ \
+{ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, \
+513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; \
+static const cab_UWORD Zipcpdext[] = /* Extra bits for distance codes */ \
+{ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, \
+10, 11, 11, 12, 12, 13, 13}; \
+/* And'ing with Zipmask[n] masks the lower n bits */ \
+static const cab_UWORD Zipmask[17] = { \
+ 0x0000, 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff, \
+ 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff \
+}
diff --git a/dlls/d3dxof/tests/d3dxof.c b/dlls/d3dxof/tests/d3dxof.c
index f1d020a..0ad4f53 100644
--- a/dlls/d3dxof/tests/d3dxof.c
+++ b/dlls/d3dxof/tests/d3dxof.c
@@ -46,6 +46,16 @@ char template[] =
"DWORD flags;\n"
"}\n";
+/* Same version as above compressed with mszip */
+char compressed_template[] =
+"xof 0302tzip0064\x71\x00\x00\x00\x61\x00\x5a\x00"
+"\x43\x4B\x2B\x49\xCD\x2D\xC8\x49\x2C\x49\x55\xF0\x48\x4D\x4C\x49"
+"\x2D\xE2\xAA\xE6\xB2\x31\x76\xB1\x30\x72\x74\x32\x31\xD6\x35\x33"
+"\x72\x71\xD4\x35\x34\x74\x76\xD3\x75\x74\x32\xB6\xD4\x35\x30\x30"
+"\x32\x70\x74\x33\x37\x74\x35\x31\x36\xB6\xE3\x0A\xF7\x0F\x72\x51"
+"\xC8\x4D\xCC\xCA\x2F\xB2\x86\xB2\x33\xF3\x40\x6C\x17\x30\x27\x2D"
+"\x27\x31\xBD\xD8\x9A\xAB\x96\x8B\x0B\x00";
+
char object[] =
"xof 0302txt 0064\n"
"Header Object\n"
@@ -53,6 +63,12 @@ char object[] =
"1; 2; 3;\n"
"}\n";
+/* Same version as above compressed with mszip */
+char compressed_object[] =
+"xof 0302tzip0064\x2c\x00\x00\x00\x1c\x00\x20\x00"
+"\x43\x4b\xf3\x48\x4d\x4c\x49\x2d\x52\xf0\x4f\xca\x4a\x4d\x2e\xe1"
+"\xaa\xe6\x32\xb4\x56\x30\xb2\x56\x30\xb6\xe6\xaa\xe5\xe2\x02\x00";
+
char empty_txt_file[] = "xof 0302txt 0064";
char empty_bin_file[] = "xof 0302bin 0064";
/* MSZip data is generated with the command "MAKECAB.EXE /D Compress=ON /D CompressionType=MSZip file packed"
@@ -232,10 +248,10 @@ static void test_file_types(void)
ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
hr = IDirectXFile_RegisterTemplates(dxfile, empty_tzip_file, sizeof(empty_tzip_file) - 1);
- todo_wine ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
+ ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
hr = IDirectXFile_RegisterTemplates(dxfile, empty_bzip_file, sizeof(empty_bzip_file) - 1);
- todo_wine ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
+ ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
hr = IDirectXFile_RegisterTemplates(dxfile, empty_cmp_file, sizeof(empty_cmp_file) - 1);
ok(hr == DXFILEERR_BADFILETYPE, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
@@ -258,13 +274,13 @@ static void test_file_types(void)
lminfo.lpMemory = empty_tzip_file;
lminfo.dSize = sizeof(empty_tzip_file) - 1;
hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
- todo_wine ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
+ ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
if (hr == DXFILE_OK) IDirectXFileEnumObject_Release(enum_object);
lminfo.lpMemory = empty_bzip_file;
lminfo.dSize = sizeof(empty_bzip_file) - 1;
hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
- todo_wine ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
+ ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
if (hr == DXFILE_OK) IDirectXFileEnumObject_Release(enum_object);
lminfo.lpMemory = empty_cmp_file;
@@ -280,6 +296,52 @@ static void test_file_types(void)
IDirectXFile_Release(dxfile);
}
+static void test_compressed_files(void)
+{
+ HRESULT hr;
+ LPDIRECTXFILE dxfile = NULL;
+ LPDIRECTXFILEENUMOBJECT enum_object;
+ LPDIRECTXFILEDATA file_data;
+ DXFILELOADMEMORY lminfo;
+ BYTE* data;
+ DWORD size;
+
+ if (!pDirectXFileCreate)
+ {
+ win_skip("DirectXFileCreate is not available\n");
+ return;
+ }
+
+ hr = pDirectXFileCreate(&dxfile);
+ ok(hr == DXFILE_OK, "DirectXFileCreate: %x\n", hr);
+ if (!dxfile)
+ {
+ skip("Couldn't create DirectXFile interface\n");
+ return;
+ }
+
+ hr = IDirectXFile_RegisterTemplates(dxfile, compressed_template, sizeof(compressed_template) - 1);
+ ok(hr == DXFILE_OK, "IDirectXFileImpl_RegisterTemplates: %x\n", hr);
+
+ lminfo.lpMemory = compressed_object;
+ lminfo.dSize = sizeof(compressed_object) - 1;
+ hr = IDirectXFile_CreateEnumObject(dxfile, &lminfo, DXFILELOAD_FROMMEMORY, &enum_object);
+ ok(hr == DXFILE_OK, "IDirectXFile_CreateEnumObject: %x\n", hr);
+
+ hr = IDirectXFileEnumObject_GetNextDataObject(enum_object, &file_data);
+ ok(hr == DXFILE_OK, "IDirectXFileEnumObject_GetNextDataObject: %x\n", hr);
+
+ hr = IDirectXFileData_GetData(file_data, NULL, &size, (void**)&data);
+ ok(hr == DXFILE_OK, "IDirectXFileData_GetData: %x\n", hr);
+
+ ok(size == 8, "Retrieved data size is wrong\n");
+ ok((*((WORD*)data) == 1) && (*((WORD*)(data+2)) == 2) && (*((DWORD*)(data+4)) == 3), "Retrieved data is wrong\n");
+
+ IDirectXFileData_Release(file_data);
+ IDirectXFileEnumObject_Release(enum_object);
+ IDirectXFile_Release(dxfile);
+}
+
/* Set it to 1 to expand the string when dumping the object. This is useful when there is
* only one string in a sub-object (very common). Use with care, this may lead to a crash. */
#define EXPAND_STRING 0
@@ -455,6 +517,7 @@ START_TEST(d3dxof)
test_refcount();
test_CreateEnumObject();
test_file_types();
+ test_compressed_files();
test_dump();
FreeLibrary(hd3dxof);
More information about the wine-patches
mailing list