1
/* This library is free software; you can redistribute it and/or
2
modify it under the terms of the GNU Library General Public
3
License as published by the Free Software Foundation; either
4
version 2 of the License, or (at your option) any later version.
6
This library is distributed in the hope that it will be useful,
7
but WITHOUT ANY WARRANTY; without even the implied warranty of
8
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9
Library General Public License for more details.
11
You should have received a copy of the GNU Library General Public License
12
along with this library; see the file COPYING.LIB. If not, write to
13
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14
Boston, MA 02111-1307, USA.
19
#include "decompress.h"
21
uint Chm::getEncInt(QFile& f, uint &value) const
39
uint Chm::getName(QFile& f, QString& name) const
42
char *buf = new char[len];
43
f.readBlock(buf, len);
44
name = QString::fromUtf8(buf, len);
45
if (name.startsWith("/"))
51
uint Chm::getIntel32(QFile& f) const
53
uint value = f.getch() | f.getch() << 8 | f.getch() << 16 | f.getch() << 24;
57
uint Chm::getIntel64(QFile& f) const
59
uint value = getIntel32(f);
64
bool Chm::getChunk(QFile& f, uint chunkSize, ChmDirectoryMap& directoryMap) const
67
if (f.readBlock(tag, 4) != 4) return false;
69
if (!qstrncmp(tag, "PMGL", 4))
71
uint quickref_length = getIntel32(f);
75
while (pos < chunkSize - quickref_length)
77
uint section, offset, length;
79
pos += getName(f, name);
80
pos += getEncInt(f, section);
81
pos += getEncInt(f, offset);
82
pos += getEncInt(f, length);
83
directoryMap[name] = ChmDirTableEntry(section, offset, length);
84
if (name.endsWith(".hhc"))
85
directoryMap["/@contents"] = ChmDirTableEntry(section, offset, length);
88
return (f.at(f.at() + quickref_length));
90
else if (!qstrncmp(tag, "PMGI", 4))
92
// evaluation of the index chunk is not yet implemented => skip it
93
return f.at(f.at() + chunkSize - 4);
101
bool Chm::read(const QString& fileSpec, ChmDirectoryMap& dirMap, QByteArray& contents) const
104
if (!f.open(IO_ReadOnly)) return false;
106
// read CHM file header
108
if (f.readBlock(tag, 4) != 4 || qstrncmp(tag, "ITSF", 4)) return false;
109
uint chm_version = getIntel32(f);
110
if (!f.at(f.at() + 0x30)) return false;
112
// read header section table
113
uint section_0_offset = getIntel64(f);
114
uint section_0_length = getIntel64(f);
115
uint section_1_offset = getIntel64(f);
116
uint section_1_length = getIntel64(f);
118
uint contentStart = 0;
119
if (chm_version >= 3) contentStart = getIntel32(f);
121
// read directory header
122
if (!f.at(section_1_offset)) return false;
123
if (f.readBlock(tag, 4) != 4 || qstrncmp(tag, "ITSP", 4)) return false;
124
if (!f.at(f.at() + 12)) return false;
125
uint directory_chunk_size = getIntel32(f);
126
if (!f.at(f.at() + 24)) return false;
127
uint num_directory_chunks = getIntel32(f);
128
if (!f.at(f.at() + 36)) return false;
130
// read directory table
131
for (uint i = 0; i < num_directory_chunks; i++)
132
if (!getChunk(f, directory_chunk_size, dirMap)) return false;
134
// current position is start of content area
135
if (chm_version < 3) contentStart = f.at();
138
if (!f.at(contentStart)) return false;
139
uint resetTableOffset =
140
dirMap["::DataSpace/Storage/MSCompressed/Transform/{7FC28940-9D31-11D0-9B27-00A0C91E9C7C}/InstanceData/ResetTable"].offset;
141
if (!f.at(f.at() + resetTableOffset + 4)) return false;
142
uint numResetTableEntries = getIntel32(f);
143
if (!f.at(f.at() + 8)) return false;
144
uint uncompressedLength = getIntel64(f);
145
uint compressedLength = getIntel64(f);
146
uint blockSize = getIntel64(f);
147
uint *resetTable = new uint[numResetTableEntries + 1];
149
for (uint i = 0; i < numResetTableEntries; i++)
150
resetTable[i] = getIntel64(f);
152
resetTable[numResetTableEntries] = compressedLength;
154
// read compressed contents
155
if (!f.at(contentStart)) return false;
156
uint contentsOffset = dirMap["::DataSpace/Storage/MSCompressed/Content"].offset;
157
if (!f.at(f.at() + contentsOffset)) return false;
158
char *compressedContents = new char[compressedLength];
159
if ((uint)f.readBlock(compressedContents, compressedLength) != compressedLength) return false;
163
// allocate buffer for uncompressed contents
164
char *uncompressedContents = new char[uncompressedLength];
168
uint tmp = blockSize;
169
while (tmp >>= 1) window++;
172
uint outlen = uncompressedLength;
175
for (uint i = 0; i < numResetTableEntries; i++)
177
if (!(i & 1)) LZXinit(window);
179
uint inlen = resetTable[i+1] - resetTable[i];
180
res = LZXdecompress((uchar*)&compressedContents[resetTable[i]],
182
(uchar*)uncompressedContents + i * blockSize,
183
(outlen < blockSize) ? outlen : blockSize);
188
delete [] resetTable;
189
delete [] compressedContents;
192
contents.duplicate(uncompressedContents, uncompressedLength);
194
delete [] uncompressedContents;