File: decompressor.cpp

package info (click to toggle)
kdegraphics-mobipocket 4:20.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 196 kB
  • sloc: cpp: 586; xml: 149; makefile: 3
file content (242 lines) | stat: -rw-r--r-- 7,036 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/***************************************************************************
 *   Copyright (C) 2008 by Jakub Stachowski <qbast@go2.pl>                 *
 *                                                                         *
 *   RLE decompressor based on FBReader                                    *
 *   Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>      *
 *                                                                         *
 *   Huffdic decompressor based on Python code by Igor Skochinsky          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "decompressor.h"
#include "mobipocket.h"

#include <QList>

static const unsigned char TOKEN_CODE[256] = {
	0, 1, 1, 1,		1, 1, 1, 1,		1, 0, 0, 0,		0, 0, 0, 0,
	0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,
	0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,
	0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,
	0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,
	0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,
	0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,
	0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,		0, 0, 0, 0,
	3, 3, 3, 3,		3, 3, 3, 3,		3, 3, 3, 3,		3, 3, 3, 3,
	3, 3, 3, 3,		3, 3, 3, 3,		3, 3, 3, 3,		3, 3, 3, 3,
	3, 3, 3, 3,		3, 3, 3, 3,		3, 3, 3, 3,		3, 3, 3, 3,
	3, 3, 3, 3,		3, 3, 3, 3,		3, 3, 3, 3,		3, 3, 3, 3,
	2, 2, 2, 2,		2, 2, 2, 2,		2, 2, 2, 2,		2, 2, 2, 2,
	2, 2, 2, 2,		2, 2, 2, 2,		2, 2, 2, 2,		2, 2, 2, 2,
	2, 2, 2, 2,		2, 2, 2, 2,		2, 2, 2, 2,		2, 2, 2, 2,
	2, 2, 2, 2,		2, 2, 2, 2,		2, 2, 2, 2,		2, 2, 2, 2,
};

namespace Mobipocket {

class NOOPDecompressor : public Decompressor
{
public:
    NOOPDecompressor(const PDB& p) : Decompressor(p) {}
    QByteArray decompress(const QByteArray& data) override { return data; }
};


class RLEDecompressor : public Decompressor
{
public:
    RLEDecompressor(const PDB& p) : Decompressor(p) {}
    QByteArray decompress(const QByteArray& data) override;
};

class BitReader
{
public:
    BitReader(const QByteArray& d) : pos(0), data(d)
    {
        data.append("\000\000\000\000");    //krazy:exclude=strings
        len=data.size()*8;
    }
    
    quint32 read() {
        quint32 g=0;
        quint64 r=0;
        while (g<32) {
            r=(r << 8) | (quint8)data[(pos+g)>>3];
            g=g+8 - ((pos+g) & 7);
        }
        return (r >> (g-32));
    }
    bool eat(int n) {
        pos+=n;
        return pos <= len;
    }
    
    int left() {
        return len - pos;
    }
    
private:
    int pos;
    int len;
    QByteArray data;
};

class HuffdicDecompressor : public Decompressor
{
public:
    HuffdicDecompressor(const PDB& p);
    QByteArray decompress(const QByteArray& data) override;
private:
    void unpack(BitReader reader, int depth = 0);
    QList<QByteArray> dicts;
    quint32 entry_bits;
    quint32 dict1[256];
    quint32 dict2[64];
    
    QByteArray buf;
};



QByteArray RLEDecompressor::decompress(const QByteArray& data)
{
        QByteArray ret;
        ret.reserve(8192);

		unsigned char token;
		unsigned short copyLength, N, shift;
		unsigned short shifted;
        int i=0;
        int maxIndex=data.size()-1;

		while (i<data.size()) {
			token = data.at(i++);
			switch (TOKEN_CODE[token]) {
				case 0:
				        ret.append(token);
					break;
				case 1:
					if ((i + token > maxIndex) ) {
						goto endOfLoop;
					}
					ret.append(data.mid(i,token));
					i+=token;
					break;
				case 2:
				        ret.append(' ');
				        ret.append(token ^ 0x80);
					break;
				case 3:
					if (i + 1 > maxIndex) {
						goto endOfLoop;
					}
                                        N = token;
                                        N<<=8;
                                        N+=(unsigned char)data.at(i++);
					copyLength = (N & 7) + 3;
					shift = (N & 0x3fff) / 8;
					shifted = ret.size()-shift;
					if (shifted>(ret.size()-1)) goto endOfLoop;
					for (int i=0;i<copyLength;i++) ret.append(ret.at(shifted+i));
					break;
			}
		}
endOfLoop:
    return ret;

}

quint32 readBELong(const QByteArray& data, int offset)
{
    quint32 ret=0;
    for (int i=0;i<4;i++) { ret<<=8; ret+=(unsigned char)data[offset+i]; }
    return ret;
}

HuffdicDecompressor::HuffdicDecompressor(const PDB& p) : Decompressor(p)
{
    QByteArray header=p.getRecord(0);
    quint32 huff_ofs=readBELong(header,0x70);
    quint32 huff_num=readBELong(header,0x74);
    quint32 off1,off2;

    QByteArray huff1=p.getRecord(huff_ofs);
    if (huff1.isNull()) goto fail;
    for (unsigned int i=1;i<huff_num;i++) {
        QByteArray h=p.getRecord(huff_ofs+i);
        if (h.isNull()) goto fail;
        dicts.append(h);
    }

    off1=readBELong(huff1,16);
    off2=readBELong(huff1,20);

    if (!huff1.startsWith("HUFF")) goto fail;           //krazy:exclude=strings
    if (!dicts[0].startsWith("CDIC")) goto fail;        //krazy:exclude=strings

    entry_bits=readBELong(dicts[0],12);

    memcpy(dict1,huff1.data()+off1, 256*4);
    memcpy(dict2,huff1.data()+off2, 64*4);    
    return;
fail:
    valid=false;
}

QByteArray HuffdicDecompressor::decompress(const QByteArray& data)
{
    buf.clear();
    unpack(BitReader(data));
    return buf;
}

void HuffdicDecompressor::unpack(BitReader reader,int depth) 
{
    if (depth>32) goto fail;
    while (reader.left()) {
        quint32 dw=reader.read();
        quint32 v=dict1[dw>>24];
        quint8 codelen = v & 0x1F;
        if (!codelen) goto fail;
        quint32 code = dw >> (32 - codelen);
        quint32 r=(v >> 8);
        if (!( v & 0x80))  {
            while (code < dict2[(codelen-1)*2]) {
                codelen++;
                code = dw >> (32 - codelen);
            }
            r = dict2[(codelen-1)*2+1];
        }
        r-=code;
        if (!codelen) goto fail;
        if (!reader.eat(codelen)) return;
        quint32 dict_no = r >> entry_bits;
        quint32 off1 = 16 + (r - (dict_no << entry_bits))*2;
        QByteArray dict=dicts[dict_no];
        quint32 off2 = 16 + (unsigned char)dict[off1]*256 + (unsigned char)dict[off1+1];
        quint32 blen = (unsigned char)dict[off2]*256 + (unsigned char)dict[off2+1];
        QByteArray slice=dict.mid(off2+2,(blen & 0x7fff));
        if (blen & 0x8000) buf+=slice;
        else unpack(BitReader(slice),depth+1);
    }
    return;
fail:
    valid=false;
}

Decompressor* Decompressor::create(quint8 type, const PDB& pdb) 
{
        switch (type) {
            case 1 : return new NOOPDecompressor(pdb); 
            case 2 : return new RLEDecompressor(pdb); 
            case 'H' : return  new HuffdicDecompressor(pdb);
            default : return 0;
        }

}
}