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
|
/*
* ContainerPP20.cpp
* -----------------
* Purpose: Handling of PowerPack PP20 compressed modules
* Notes : (currently none)
* Authors: Olivier Lapicque
* OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "../common/FileReader.h"
#include "Container.h"
#include "Sndfile.h"
#include <stdexcept>
OPENMPT_NAMESPACE_BEGIN
#if !defined(MPT_WITH_ANCIENT)
struct PPBITBUFFER
{
uint32 bitcount = 0;
uint32 bitbuffer = 0;
const uint8 *pStart = nullptr;
const uint8 *pSrc = nullptr;
uint32 GetBits(uint32 n);
};
uint32 PPBITBUFFER::GetBits(uint32 n)
{
uint32 result = 0;
for(uint32 i = 0; i < n; i++)
{
if(!bitcount)
{
bitcount = 8;
if(pSrc != pStart)
pSrc--;
bitbuffer = *pSrc;
}
result = (result << 1) | (bitbuffer & 1);
bitbuffer >>= 1;
bitcount--;
}
return result;
}
static bool PP20_DoUnpack(mpt::span<const uint8> src, uint8 *pDst, uint32 dstLen)
{
const std::array<uint8, 4> modeTable{src[0], src[1], src[2], src[3]};
PPBITBUFFER BitBuffer;
BitBuffer.pStart = src.data();
BitBuffer.pSrc = src.data() + src.size() - 4;
BitBuffer.GetBits(src.data()[src.size() - 1]);
uint32 bytesLeft = dstLen;
while(bytesLeft > 0)
{
if(!BitBuffer.GetBits(1))
{
uint32 count = 1, countAdd;
do
{
countAdd = BitBuffer.GetBits(2);
count += countAdd;
} while(countAdd == 3);
LimitMax(count, bytesLeft);
for(uint32 i = 0; i < count; i++)
{
pDst[--bytesLeft] = (uint8)BitBuffer.GetBits(8);
}
if(!bytesLeft)
break;
}
{
uint32 modeIndex = BitBuffer.GetBits(2);
MPT_CHECKER_ASSUME(modeIndex < 4);
uint32 count = modeIndex + 2, offset;
if(modeIndex == 3)
{
offset = BitBuffer.GetBits((BitBuffer.GetBits(1)) ? modeTable[modeIndex] : 7);
uint32 countAdd = 7;
do
{
countAdd = BitBuffer.GetBits(3);
count += countAdd;
} while(countAdd == 7);
} else
{
offset = BitBuffer.GetBits(modeTable[modeIndex]);
}
LimitMax(count, bytesLeft);
for(uint32 i = 0; i < count; i++)
{
pDst[bytesLeft - 1] = (bytesLeft + offset < dstLen) ? pDst[bytesLeft + offset] : 0;
--bytesLeft;
}
}
}
return true;
}
struct PP20header
{
char magic[4]; // "PP20"
uint8 efficiency[4];
};
MPT_BINARY_STRUCT(PP20header, 8)
static bool ValidateHeader(const PP20header &hdr)
{
if(std::memcmp(hdr.magic, "PP20", 4) != 0)
{
return false;
}
if(hdr.efficiency[0] < 9 || hdr.efficiency[0] > 15
|| hdr.efficiency[1] < 9 || hdr.efficiency[1] > 15
|| hdr.efficiency[2] < 9 || hdr.efficiency[2] > 15
|| hdr.efficiency[3] < 9 || hdr.efficiency[3] > 15)
{
return false;
}
return true;
}
CSoundFile::ProbeResult CSoundFile::ProbeFileHeaderPP20(MemoryFileReader file, const uint64 *pfilesize)
{
PP20header hdr;
if(!file.ReadStruct(hdr))
{
return ProbeWantMoreData;
}
if(!ValidateHeader(hdr))
{
return ProbeFailure;
}
MPT_UNREFERENCED_PARAMETER(pfilesize);
return ProbeSuccess;
}
bool UnpackPP20(std::vector<ContainerItem> &containerItems, FileReader &file, ContainerLoadingFlags loadFlags)
{
file.Rewind();
containerItems.clear();
PP20header hdr;
if(!file.ReadStruct(hdr))
{
return false;
}
if(!ValidateHeader(hdr))
{
return false;
}
if(loadFlags == ContainerOnlyVerifyHeader)
{
return true;
}
if(!file.CanRead(4))
{
return false;
}
containerItems.emplace_back();
containerItems.back().data_cache = std::make_unique<std::vector<char> >();
std::vector<char> & unpackedData = *(containerItems.back().data_cache);
FileReader::pos_type length = file.GetLength();
if(!mpt::in_range<uint32>(length)) return false;
// Length word must be aligned
if((length % 2u) != 0)
return false;
file.Seek(length - 4);
uint32 dstLen = file.ReadUint24BE();
if(dstLen == 0)
return false;
try
{
unpackedData.resize(dstLen);
} catch(mpt::out_of_memory e)
{
mpt::delete_out_of_memory(e);
return false;
}
file.Seek(4);
FileReader::PinnedView compressedData = file.GetPinnedView(mpt::saturate_cast<uint32>(length - 4));
bool result = PP20_DoUnpack(mpt::byte_cast<mpt::span<const uint8>>(compressedData.span()), mpt::byte_cast<uint8 *>(unpackedData.data()), dstLen);
if(result)
{
containerItems.back().file = FileReader(mpt::byte_cast<mpt::const_byte_span>(mpt::as_span(unpackedData)));
}
return result;
}
#endif // !MPT_WITH_ANCIENT
OPENMPT_NAMESPACE_END
|