summaryrefslogtreecommitdiff
path: root/libsidplay2/sidplay-libs-2.1.0/libsidplay/src/sidtune/PP20.cpp
blob: 8bec5cfe0deafdfe0cbbeb47780218be945b3719 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * /home/ms/files/source/libsidtune/RCS/PP20.cpp,v
 *
 * PowerPacker (AMIGA) "PP20" format decompressor.
 * Copyright (C) Michael Schwendt <mschwendt@yahoo.com>
 *
 *  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.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "PP20.h"

#include <string.h>
#ifdef PP20_HAVE_EXCEPTIONS
#include <new>
#endif

/* Read a big-endian 32-bit word from four bytes in memory.
   No endian-specific optimizations applied. */
inline udword_ppt readBEdword(const ubyte_ppt ptr[4])
{
    return ( (((udword_ppt)ptr[0])<<24) + (((udword_ppt)ptr[1])<<16) +
             (((udword_ppt)ptr[2])<<8) + ((udword_ppt)ptr[3]) );
}

const char _pp20_txt_packeddatacorrupt[] = "PowerPacker: Packed data is corrupt";
const char _pp20_txt_unrecognized[]    = "PowerPacker: Unrecognized compression method";
const char _pp20_txt_uncompressed[] = "Not compressed with PowerPacker (PP20)";
const char _pp20_txt_notenoughmemory[] = "Not enough free memory";
const char _pp20_txt_fast[] = "PowerPacker: fast compression";
const char _pp20_txt_mediocre[] = "PowerPacker: mediocre compression";
const char _pp20_txt_good[] = "PowerPacker: good compression";
const char _pp20_txt_verygood[] = "PowerPacker: very good compression";
const char _pp20_txt_best[] = "PowerPacker: best compression";
const char _pp20_txt_na[] = "N/A";

const char* PP20::PP_ID = "PP20";

PP20::PP20()
{
    statusString = _pp20_txt_uncompressed;
}

bool PP20::isCompressed(const void* source, const udword_ppt size)
{
    // Check minimum input size, PP20 ID, and efficiency table.
    if ( size<8 )
    {
        return false;
    }
    // We hope that every file with a valid signature and a valid
    // efficiency table is PP-compressed actually.
    const char* idPtr = (const char*)source;
    if ( strncmp(idPtr,PP_ID,4) != 0 )
    {
        statusString = _pp20_txt_uncompressed;
        return false;
    }
    return checkEfficiency(idPtr+4);
}

bool PP20::checkEfficiency(const void* source)
{
    const udword_ppt PP_BITS_FAST = 0x09090909;
    const udword_ppt PP_BITS_MEDIOCRE = 0x090a0a0a;
    const udword_ppt PP_BITS_GOOD = 0x090a0b0b;
    const udword_ppt PP_BITS_VERYGOOD = 0x090a0c0c;
    const udword_ppt PP_BITS_BEST = 0x090a0c0d;

    // Copy efficiency table.
    memcpy(efficiency,(const ubyte_ppt*)source,4);
    udword_ppt eff = readBEdword(efficiency);
    if (( eff != PP_BITS_FAST ) &&
        ( eff != PP_BITS_MEDIOCRE ) &&
        ( eff != PP_BITS_GOOD ) &&
        ( eff != PP_BITS_VERYGOOD ) &&
        ( eff != PP_BITS_BEST ))
    {
        statusString = _pp20_txt_unrecognized;
        return false;
    }

    // Define string describing compression encoding used.
    switch ( eff)
    {
        case PP_BITS_FAST:
            statusString = _pp20_txt_fast;
            break;
         case PP_BITS_MEDIOCRE:
            statusString = _pp20_txt_mediocre;
            break;
         case PP_BITS_GOOD:
            statusString = _pp20_txt_good;
            break;
         case PP_BITS_VERYGOOD:
            statusString = _pp20_txt_verygood;
            break;
         case PP_BITS_BEST:
            statusString = _pp20_txt_best;
            break;
    }
    
    return true;
}

// Move four bytes to Motorola big-endian double-word.
inline void PP20::bytesTOdword()
{
    readPtr -= 4;
    if ( readPtr < sourceBeg )
    {
        statusString = _pp20_txt_packeddatacorrupt;
        globalError = true;
    }
    else 
    {
        current = readBEdword(readPtr);
    }
}

inline udword_ppt PP20::readBits(int count)
{
    udword_ppt data = 0;
    // read 'count' bits of packed data
    for (; count > 0; count--)
    {
        // equal to shift left
        data += data;
        // merge bit 0
        data |= (current&1);
        current >>= 1;
        if (--bits == 0)
        {
            bytesTOdword();
            bits = 32;
        }
    }
    return data;
}

inline void PP20::bytes()
{
    udword_ppt count, add;
    count = (add = readBits(2));
    while (add == 3)
    {
        add = readBits(2);
        count += add;
    }
    for ( ++count; count > 0 ; count--)
    {
        if (writePtr > destBeg)
        {
            *(--writePtr) = (ubyte_ppt)readBits(8);
        }
        else
        {
            statusString = _pp20_txt_packeddatacorrupt;
            globalError = true;
        }
    }
}

inline void PP20::sequence()
{
    udword_ppt offset, add;
    udword_ppt length = readBits(2);  // is length-2
    int offsetBitLen = (int)efficiency[length];
    length += 2;
    if ( length != 5 )
        offset = readBits( offsetBitLen );
    else
    {
        if ( readBits(1) == 0 )
            offsetBitLen = 7;
        offset = readBits( offsetBitLen );
        add = readBits(3);
        length += add;
        while ( add == 7 )
        {
            add = readBits(3);
            length += add;
        }
    }
    for ( ; length > 0 ; length-- )
    {
        if ( writePtr > destBeg )
        {
            --writePtr;
            *writePtr = *(writePtr+1+offset);
        }
        else
        {
            statusString = _pp20_txt_packeddatacorrupt;
            globalError = true;
        }
    }
}

udword_ppt PP20::decompress(const void* source, 
                                  udword_ppt size,
                                  ubyte_ppt** destRef)
{
    globalError = false;  // assume no error
    
    sourceBeg = (const ubyte_ppt*)source;
    readPtr = sourceBeg;

    if ( !isCompressed(readPtr,size) )
    {
        return 0;
    }
    
    // Uncompressed size is stored at end of source file.
    // Backwards decompression.
    readPtr += (size-4);

    udword_ppt lastDword = readBEdword(readPtr);
    // Uncompressed length in bits 31-8 of last dword.
    udword_ppt outputLen = lastDword>>8;
    
    // Allocate memory for output data.
    ubyte_ppt* dest;
#ifdef PP20_HAVE_EXCEPTIONS
    if (( dest = new(std::nothrow) ubyte_ppt[outputLen]) == 0 )
#else
    if (( dest = new ubyte_ppt[outputLen]) == 0 )
#endif
    {
        statusString = _pp20_txt_notenoughmemory;
        return 0;
    }
  
    // Lowest dest. address for range-checks.
    destBeg = dest;
    // Put destptr to end of uncompressed data.
    writePtr = dest+outputLen;

    // Read number of unused bits in 1st data dword
    // from lowest bits 7-0 of last dword.
    bits = 32 - (lastDword&0xFF);
    
    // Main decompression loop.
    bytesTOdword();
    if ( bits != 32 )
        current >>= (32-bits);
    do
    {
        if ( readBits(1) == 0 )  
            bytes();
        if ( writePtr > dest )  
            sequence();
        if ( globalError )
        {
            // statusString already set.
            outputLen = 0;  // unsuccessful decompression
            break;
        }
    } while ( writePtr > dest );

    // Finished.

    if (outputLen > 0)  // successful
    {
        // Free any previously existing destination buffer.
        if ( *destRef != 0 )
        {
            delete[] *destRef;
        }
        *destRef = dest;
    }
    else
    {
        delete[] dest;
    }

    return outputLen;
}