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 290
|
//
// /home/ms/source/sidplay/libsidplay/fformat/RCS/pp_.cpp,v
//
// For more info get ``depp.cpp'' and ``ppcl.cpp''.
#include "pp_.h"
// exports
bool depp(ifstream& source, ubyte** destRef);
bool ppIsCompressed();
udword ppUncompressedLen(); // return the length of the uncompressed data
const char* ppErrorString;
static const char PP_ID[] = "PP20";
static const udword PP_BITS_FAST = 0x09090909;
static const udword PP_BITS_MEDIOCRE = 0x090a0a0a;
static const udword PP_BITS_GOOD = 0x090a0b0b;
static const udword PP_BITS_VERYGOOD = 0x090a0c0c;
static const udword PP_BITS_BEST = 0x090a0c0d;
static const char text_packeddatacorrupt[] = "PowerPacker: Packed data is corrupt";
static const char text_unrecognized[] = "PowerPacker: Unrecognized compression method";
static const char text_uncompressed[] = "Not compressed with PowerPacker (PP20)";
static const char text_notenoughmemory[] = "Not enough free memory";
static const char text_fast[] = "PowerPacker: fast compression";
static const char text_mediocre[] = "PowerPacker: mediocre compression";
static const char text_good[] = "PowerPacker: good compression";
static const char text_verygood[] = "PowerPacker: very good compression";
static const char text_best[] = "PowerPacker: best compression";
static ubyte* sourceBuf;
static ubyte* readPtr;
static ubyte* writePtr;
static ubyte* startPtr;
static udword current; // compressed data longword
static int bits; // number of bits in 'current' to evaluate
static ubyte efficiency[4];
static udword outputLen;
static bool isCompressed;
static bool globalError;
// Move four bytes to Motorola big-endian double-word.
static inline void bytesTOudword()
{
readPtr -= 4;
if ( readPtr < sourceBuf )
{
ppErrorString = text_packeddatacorrupt;
globalError = true;
}
else
current = readEndian(*readPtr,*(readPtr+1),*(readPtr+2),*(readPtr+3));
}
static void ppFreeMem()
{
if (sourceBuf != 0) // should not be required
delete[] sourceBuf;
sourceBuf = 0;
}
static inline udword ppRead(int count)
{
udword data = 0;
// read 'count' bits of packed data
for (; count > 0; count--)
{
// equal to shift left
data += data;
// merge bit 0
data = (data | (current&1));
current = (current >> 1);
if ((--bits) == 0)
{
bytesTOudword();
bits = 32;
}
}
return data;
}
static inline void ppBytes()
{
udword count, add;
count = (add = ppRead(2));
while (add == 3)
{
add = ppRead(2);
count += add;
}
count++;
for (; count > 0 ; count--)
{
if (writePtr > startPtr)
{
*(--writePtr) = (ubyte)ppRead(8);
}
else
{
ppErrorString = text_packeddatacorrupt;
globalError = true;
}
}
}
static inline void ppSequence()
{
udword offset, length, add;
int offsetBitLen;
length = ppRead( 2 ); // length -2
offsetBitLen = (int)efficiency[length];
length += 2;
if ( length != 5 )
offset = ppRead( offsetBitLen );
else
{
if ( ppRead( 1 ) == 0 )
offsetBitLen = 7;
offset = ppRead( offsetBitLen );
add = ppRead( 3 );
length += add;
while ( add == 7 )
{
add = ppRead(3);
length += add;
}
}
for ( ; length > 0 ; length-- )
{
if ( writePtr > startPtr )
{
--writePtr;
*writePtr = *(writePtr+1+offset);
}
else
{
ppErrorString = text_packeddatacorrupt;
globalError = true;
}
}
}
udword ppUncompressedLen()
{
return outputLen;
}
bool ppIsCompressed()
{
return isCompressed;
}
// returns: false = file is not in (supported) PowerPacker format,
// or some error (=> ppErrorString) has occured
// true = successful return (size via ppUncompressedLen())
bool depp( ifstream& source, ubyte** destRef )
{
globalError = false; // assume no error
isCompressed = false;
outputLen = 0;
// Check for header signature.
source.seekg(0,ios::beg);
char sig[5];
source.read(sig,4);
sig[4] = 0;
if ( strcmp(sig,PP_ID) != 0 )
{
ppErrorString = text_uncompressed;
return false;
}
// Load efficiency table.
source.read(efficiency,4);
udword eff = readEndian(efficiency[0],efficiency[1],efficiency[2],efficiency[3]);
if (( eff != PP_BITS_FAST ) &&
( eff != PP_BITS_MEDIOCRE ) &&
( eff != PP_BITS_GOOD ) &&
( eff != PP_BITS_VERYGOOD ) &&
( eff != PP_BITS_BEST ))
{
ppErrorString = text_unrecognized;
return false;
}
// We set this flag here and not before previous block, because we hope
// that every file with a valid signature and a valid efficiency table
// actually is PP-compressed.
isCompressed = true;
// Uncompressed size is stored at end of source file.
#if defined(HAVE_SEEKG_OFFSET)
udword inputlen = (source.seekg(0,ios::end)).offset();
#else
source.seekg( 0, ios::end );
udword inputlen = (udword)source.tellg();
#endif
source.seekg( 0, ios::beg );
// Get memory for source file.
if (( sourceBuf = new ubyte[inputlen]) == 0 )
{
ppErrorString = text_notenoughmemory;
return false;
}
// For 16-bit system (like Windows 3.x) we would have to change
// this to be able to load beyond the 64KB segment boundary.
udword restfilelen = inputlen;
while ( restfilelen > INT_MAX ) {
source.read( (ubyte*)sourceBuf + (inputlen - restfilelen), INT_MAX );
restfilelen -= INT_MAX;
}
if ( restfilelen > 0 )
source.read( (ubyte*)sourceBuf + (inputlen - restfilelen), restfilelen );
// reset file pointer
source.seekg( 0, ios::beg );
// backwards decompression
readPtr = sourceBuf + inputlen -4;
// uncompressed length in bits 31-8 of last dword
outputLen = readEndian(0,*readPtr,*(readPtr+1),*(readPtr+2));
// Free any previously existing destination buffer.
if ( *destRef != 0 )
delete[] *destRef;
// Allocate memory for output data.
if (( *destRef = new ubyte[outputLen]) == 0 )
{
ppErrorString = text_notenoughmemory;
return false;
}
switch ( eff)
{
case PP_BITS_FAST:
ppErrorString = text_fast;
break;
case PP_BITS_MEDIOCRE:
ppErrorString = text_mediocre;
break;
case PP_BITS_GOOD:
ppErrorString = text_good;
break;
case PP_BITS_VERYGOOD:
ppErrorString = text_verygood;
break;
case PP_BITS_BEST:
ppErrorString = text_best;
break;
}
// put destptr to end of uncompressed data
writePtr = *destRef + outputLen;
// lowest dest. address for range-checks
startPtr = *destRef;
// read number of unused bits in 1st data dword
// from lowest bits 7-0 of last dword
bits = 32 - *(sourceBuf + inputlen -1);
// decompress data
bytesTOudword();
if ( bits != 32 )
current = ( current >> ( 32 - bits ));
do
{
if ( ppRead( 1) == 0 )
ppBytes();
if ( writePtr > *destRef )
ppSequence();
if ( globalError )
{
ppFreeMem();
return false;
}
} while ( writePtr > *destRef );
// Finished.
ppFreeMem();
return true;
}
|