~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to parts/documentation/protocols/chm/chmfile.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
5
 
 
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.
 
10
 
 
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.
 
15
*/
 
16
 
 
17
#include <qfile.h>
 
18
#include "chmfile.h"
 
19
#include "decompress.h"
 
20
 
 
21
uint Chm::getEncInt(QFile& f, uint &value) const
 
22
{
 
23
    int c;
 
24
    uint result = 0;
 
25
    ulong count = 0;
 
26
 
 
27
    do
 
28
    {
 
29
        c = f.getch();
 
30
        result <<= 7;
 
31
        result |= (c & 0x7F);
 
32
        count++;
 
33
    } while (c & 0x80);
 
34
 
 
35
    value = result;
 
36
    return count;
 
37
}
 
38
 
 
39
uint Chm::getName(QFile& f, QString& name) const
 
40
{
 
41
    int len = f.getch();
 
42
    char *buf = new char[len];
 
43
    f.readBlock(buf, len);
 
44
    name = QString::fromUtf8(buf, len);
 
45
    if (name.startsWith("/"))
 
46
        name = name.lower();
 
47
    delete buf;
 
48
    return len + 1;
 
49
}
 
50
 
 
51
uint Chm::getIntel32(QFile& f) const
 
52
{
 
53
    uint value = f.getch() | f.getch() << 8 | f.getch() << 16 | f.getch() << 24;
 
54
    return value;
 
55
}
 
56
 
 
57
uint Chm::getIntel64(QFile& f) const
 
58
{
 
59
    uint value = getIntel32(f);
 
60
    f.at(f.at() + 4);
 
61
    return value;
 
62
}
 
63
 
 
64
bool Chm::getChunk(QFile& f, uint chunkSize, ChmDirectoryMap& directoryMap) const
 
65
{
 
66
    char tag[4];
 
67
    if (f.readBlock(tag, 4) != 4) return false;
 
68
 
 
69
    if (!qstrncmp(tag, "PMGL", 4))
 
70
    {
 
71
        uint quickref_length = getIntel32(f);
 
72
        f.at(f.at() + 12);
 
73
 
 
74
        uint pos = 20;
 
75
        while (pos < chunkSize - quickref_length)
 
76
        {
 
77
            uint section, offset, length;
 
78
            QString name;
 
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);
 
86
        }
 
87
 
 
88
        return (f.at(f.at() + quickref_length));
 
89
    }
 
90
    else if (!qstrncmp(tag, "PMGI", 4))
 
91
    {
 
92
        // evaluation of the index chunk is not yet implemented => skip it
 
93
        return f.at(f.at() + chunkSize - 4);
 
94
    }
 
95
    else
 
96
    {
 
97
        return false;
 
98
    }
 
99
}
 
100
 
 
101
bool Chm::read(const QString& fileSpec, ChmDirectoryMap& dirMap, QByteArray& contents) const
 
102
{
 
103
    QFile f(fileSpec);
 
104
    if (!f.open(IO_ReadOnly)) return false;
 
105
 
 
106
    // read CHM file header
 
107
    char tag[4];
 
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;
 
111
 
 
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);
 
117
 
 
118
        uint contentStart = 0;
 
119
        if (chm_version >= 3) contentStart = getIntel32(f);
 
120
 
 
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;
 
129
 
 
130
    // read directory table
 
131
    for (uint i = 0; i < num_directory_chunks; i++)
 
132
        if (!getChunk(f, directory_chunk_size, dirMap)) return false;
 
133
 
 
134
    // current position is start of content area
 
135
        if (chm_version < 3) contentStart = f.at();
 
136
 
 
137
    // read reset table
 
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];
 
148
 
 
149
    for (uint i = 0; i < numResetTableEntries; i++)
 
150
        resetTable[i] = getIntel64(f);
 
151
 
 
152
    resetTable[numResetTableEntries] = compressedLength;
 
153
 
 
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;
 
160
 
 
161
        f.close();
 
162
 
 
163
    // allocate buffer for uncompressed contents
 
164
    char *uncompressedContents = new char[uncompressedLength];
 
165
 
 
166
    // get window size
 
167
        uint window = 1;
 
168
        uint tmp = blockSize;
 
169
        while (tmp >>= 1) window++;
 
170
        
 
171
        // decompress
 
172
    uint outlen = uncompressedLength;
 
173
    int res = 1;
 
174
 
 
175
    for (uint i = 0; i < numResetTableEntries; i++)
 
176
    {
 
177
        if (!(i & 1)) LZXinit(window);
 
178
 
 
179
        uint inlen = resetTable[i+1] - resetTable[i];
 
180
        res = LZXdecompress((uchar*)&compressedContents[resetTable[i]],
 
181
                            inlen,
 
182
                            (uchar*)uncompressedContents + i * blockSize,
 
183
                            (outlen < blockSize) ? outlen : blockSize);
 
184
        if (res) break;
 
185
        outlen -= blockSize;
 
186
    }
 
187
 
 
188
    delete [] resetTable;
 
189
    delete [] compressedContents;
 
190
 
 
191
    if (res == 0)
 
192
        contents.duplicate(uncompressedContents, uncompressedLength);
 
193
 
 
194
        delete [] uncompressedContents;
 
195
 
 
196
    return (res == 0);
 
197
}