Commit 72d9aaa6 authored by Christian Costa's avatar Christian Costa Committed by Alexandre Julliard

d3dxof: Add support for mszip compressed files.

parent 9e67f954
......@@ -9,6 +9,7 @@ IMPORTS = dxguid uuid ole32 advapi32 kernel32
C_SRCS = \
d3dxof.c \
main.c \
mszip.c \
parsing.c \
regsvr.c
......
/*
* 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);
}
......
......@@ -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__ */
/*
* 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 \
}
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment