| 1 |
/* |
| 2 |
* serialization_utils.cpp |
| 3 |
* ----------------------- |
| 4 |
* Purpose: Serializing data to and from MPTM files. |
| 5 |
* Notes : (currently none) |
| 6 |
* Authors: OpenMPT Devs |
| 7 |
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details. |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
#include "stdafx.h" |
| 12 |
|
| 13 |
#include "serialization_utils.h" |
| 14 |
|
| 15 |
#include "mpt/base/utility.hpp" |
| 16 |
#include "mpt/io/io.hpp" |
| 17 |
#include "mpt/io/io_stdstream.hpp" |
| 18 |
|
| 19 |
#include <array> |
| 20 |
#include <istream> |
| 21 |
#include <ostream> |
| 22 |
#include <sstream> |
| 23 |
|
| 24 |
#include "misc_util.h" |
| 25 |
|
| 26 |
|
| 27 |
OPENMPT_NAMESPACE_BEGIN |
| 28 |
|
| 29 |
|
| 30 |
namespace srlztn |
| 31 |
{ |
| 32 |
|
| 33 |
|
| 34 |
static const uint8 HeaderId_FlagByte = 0; |
| 35 |
|
| 36 |
// Indexing starts from 0. |
| 37 |
static inline bool Testbit(uint8 val, uint8 bitindex) {return ((val & (1 << bitindex)) != 0);} |
| 38 |
|
| 39 |
static inline void Setbit(uint8& val, uint8 bitindex, bool newval) |
| 40 |
{ |
| 41 |
if(newval) |
| 42 |
{ |
| 43 |
val |= static_cast<uint8>(1u << bitindex); |
| 44 |
} else |
| 45 |
{ |
| 46 |
val &= static_cast<uint8>(~(1u << bitindex)); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
#ifdef SSB_LOGGING |
| 52 |
bool ID::IsPrintable() const |
| 53 |
{ |
| 54 |
for(std::size_t i = 0; i < m_ID.length(); ++i) |
| 55 |
{ |
| 56 |
if(m_ID[i] <= 0 || isprint(static_cast<unsigned char>(m_ID[i])) == 0) |
| 57 |
{ |
| 58 |
return false; |
| 59 |
} |
| 60 |
} |
| 61 |
return true; |
| 62 |
} |
| 63 |
#endif |
| 64 |
|
| 65 |
|
| 66 |
//Format: First bit tells whether the size indicator is 1 or 2 bytes. |
| 67 |
static void WriteAdaptive12String(std::ostream& oStrm, const std::string& str) |
| 68 |
{ |
| 69 |
uint16 s = static_cast<uint16>(str.size()); |
| 70 |
LimitMax(s, uint16(std::numeric_limits<uint16>::max() / 2)); |
| 71 |
mpt::IO::WriteAdaptiveInt16LE(oStrm, s); |
| 72 |
oStrm.write(str.c_str(), s); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
void WriteItemString(std::ostream& oStrm, const std::string &str) |
| 77 |
{ |
| 78 |
uint32 id = static_cast<uint32>(std::min(str.size(), static_cast<std::size_t>((std::numeric_limits<uint32>::max() >> 4)))) << 4; |
| 79 |
id |= 12; // 12 == 1100b |
| 80 |
Binarywrite<uint32>(oStrm, id); |
| 81 |
id >>= 4; |
| 82 |
if(id > 0) |
| 83 |
oStrm.write(str.data(), id); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
void ReadItemString(std::istream& iStrm, std::string& str, const std::size_t) |
| 88 |
{ |
| 89 |
// bits 0,1: Bytes per char type: 1,2,3,4. |
| 90 |
// bits 2,3: Bytes in size indicator, 1,2,3,4 |
| 91 |
uint32 id = 0; |
| 92 |
Binaryread(iStrm, id, 1); |
| 93 |
const uint8 nSizeBytes = (id & 12) >> 2; // 12 == 1100b |
| 94 |
if (nSizeBytes > 0) |
| 95 |
{ |
| 96 |
uint8 bytes = std::min(uint8(3), nSizeBytes); |
| 97 |
uint8 v2 = 0; |
| 98 |
uint8 v3 = 0; |
| 99 |
uint8 v4 = 0; |
| 100 |
if(bytes >= 1) Binaryread(iStrm, v2); |
| 101 |
if(bytes >= 2) Binaryread(iStrm, v3); |
| 102 |
if(bytes >= 3) Binaryread(iStrm, v4); |
| 103 |
id &= 0xff; |
| 104 |
id |= (v2 << 8) | (v3 << 16) | (v4 << 24); |
| 105 |
} |
| 106 |
// Limit to 1 MB. |
| 107 |
str.resize(std::min(id >> 4, uint32(1000000))); |
| 108 |
for(size_t i = 0; i < str.size(); i++) |
| 109 |
iStrm.read(&str[i], 1); |
| 110 |
|
| 111 |
id = (id >> 4) - static_cast<uint32>(str.size()); |
| 112 |
if(id > 0) |
| 113 |
iStrm.ignore(id); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
#ifdef SSB_LOGGING |
| 118 |
mpt::ustring ID::AsString() const |
| 119 |
{ |
| 120 |
if(IsPrintable()) |
| 121 |
{ |
| 122 |
return mpt::ToUnicode(mpt::Charset::ISO8859_1, m_ID); |
| 123 |
} |
| 124 |
if(m_ID.length() > 8) |
| 125 |
{ |
| 126 |
return mpt::ustring(); |
| 127 |
} |
| 128 |
uint64le val; |
| 129 |
val.set(0); |
| 130 |
std::memcpy(&val, m_ID.data(), m_ID.length()); |
| 131 |
return mpt::ufmt::val(val); |
| 132 |
} |
| 133 |
#endif |
| 134 |
|
| 135 |
|
| 136 |
const char Ssb::s_EntryID[3] = {'2','2','8'}; |
| 137 |
|
| 138 |
|
| 139 |
Ssb::Ssb() |
| 140 |
: m_Status(Status{}) |
| 141 |
, m_nFixedEntrySize(0) |
| 142 |
, m_posStart(0) |
| 143 |
, m_nIdbytes(IdSizeVariable) |
| 144 |
, m_nCounter(0) |
| 145 |
, m_Flags((1 << RwfWMapStartPosEntry) + (1 << RwfWMapSizeEntry) + (1 << RwfWVersionNum)) |
| 146 |
{ |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
SsbWrite::SsbWrite(std::ostream& os) |
| 152 |
: oStrm(os) |
| 153 |
, m_posEntrycount(0) |
| 154 |
, m_posMapPosField(0) |
| 155 |
{ |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
SsbRead::SsbRead(std::istream& is) |
| 161 |
: iStrm(is) |
| 162 |
, m_nReadVersion(0) |
| 163 |
, m_rposMapBegin(0) |
| 164 |
, m_posMapEnd(0) |
| 165 |
, m_posDataBegin(0) |
| 166 |
, m_rposEndofHdrData(0) |
| 167 |
, m_nReadEntrycount(0) |
| 168 |
, m_nNextReadHint(0) |
| 169 |
{ |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
void SsbWrite::AddWriteNote(const Status s) |
| 175 |
{ |
| 176 |
m_Status.level = static_cast<StatusLevel>(mpt::to_underlying(m_Status.level) | mpt::to_underlying(s.level)); |
| 177 |
m_Status.messages = static_cast<StatusMessages>(mpt::to_underlying(m_Status.messages) | mpt::to_underlying(s.messages)); |
| 178 |
SSB_LOG(MPT_UFORMAT("{}: 0x{} 0x{}")(U_("Write note: "), mpt::ufmt::hex(mpt::to_underlying(s.level)), mpt::ufmt::hex(mpt::to_underlying(s.messages)))); |
| 179 |
} |
| 180 |
|
| 181 |
void SsbRead::AddReadNote(const Status s) |
| 182 |
{ |
| 183 |
m_Status.level = static_cast<StatusLevel>(mpt::to_underlying(m_Status.level) | mpt::to_underlying(s.level)); |
| 184 |
m_Status.messages = static_cast<StatusMessages>(mpt::to_underlying(m_Status.messages) | mpt::to_underlying(s.messages)); |
| 185 |
SSB_LOG(MPT_UFORMAT("{}: 0x{} 0x{}")(U_("Read note: "), mpt::ufmt::hex(mpt::to_underlying(s.level)), mpt::ufmt::hex(mpt::to_underlying(s.messages)))); |
| 186 |
} |
| 187 |
|
| 188 |
#ifdef SSB_LOGGING |
| 189 |
void SsbRead::LogReadEntry(const ReadEntry &pRe, const std::size_t nNum) |
| 190 |
{ |
| 191 |
SSB_LOG(MPT_UFORMAT("Read entry: {{num, id, rpos, size, desc}} = {{{}, {}, {}, {}, {}}}")( |
| 192 |
nNum, |
| 193 |
(pRe.nIdLength < 30 && m_Idarray.size() > 0) ? ID(&m_Idarray[pRe.nIdpos], pRe.nIdLength).AsString() : U_(""), |
| 194 |
pRe.rposStart, |
| 195 |
(pRe.nSize != invalidDatasize) ? mpt::ufmt::val(pRe.nSize) : U_(""), |
| 196 |
U_(""))); |
| 197 |
} |
| 198 |
#endif |
| 199 |
|
| 200 |
#ifdef SSB_LOGGING |
| 201 |
// Called after writing an entry. |
| 202 |
void SsbWrite::LogWriteEntry(const ID &id, const std::size_t nEntryNum, const std::size_t nBytecount, const std::streamoff rposStart) |
| 203 |
{ |
| 204 |
SSB_LOG(MPT_UFORMAT("Wrote entry: {{num, id, rpos, size}} = {{{}, {}, {}, {}}}")(nEntryNum, id.AsString(), rposStart, nBytecount)); |
| 205 |
} |
| 206 |
#endif |
| 207 |
|
| 208 |
|
| 209 |
void SsbRead::ResetReadstatus() |
| 210 |
{ |
| 211 |
m_Status = Status{}; |
| 212 |
m_Idarray.reserve(32); |
| 213 |
m_Idarray.push_back(0); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
void SsbWrite::WriteMapItem(const ID &id, |
| 218 |
const std::streamoff& rposDataStart, |
| 219 |
const std::size_t& nDatasize, |
| 220 |
const std::string &pszDesc) |
| 221 |
{ |
| 222 |
SSB_LOG(MPT_UFORMAT("Writing map entry: id={}, rpos={}, size={}")( |
| 223 |
(id.GetSize() > 0) ? id.AsString() : U_(""), |
| 224 |
rposDataStart, |
| 225 |
nDatasize)); |
| 226 |
|
| 227 |
std::ostringstream mapStream; |
| 228 |
|
| 229 |
if(m_nIdbytes > 0) |
| 230 |
{ |
| 231 |
if (m_nIdbytes != IdSizeVariable && id.GetSize() != m_nIdbytes) |
| 232 |
{ AddWriteNote(SNW_CHANGING_IDSIZE_WITH_FIXED_IDSIZESETTING); return; } |
| 233 |
|
| 234 |
if (m_nIdbytes == IdSizeVariable) //Variablesize ID? |
| 235 |
mpt::IO::WriteAdaptiveInt16LE(mapStream, static_cast<uint16>(id.GetSize())); |
| 236 |
|
| 237 |
if(id.GetSize() > 0) |
| 238 |
mapStream.write(id.AsSpan().data(), id.AsSpan().size()); |
| 239 |
} |
| 240 |
|
| 241 |
if(m_Flags[RwfWMapStartPosEntry]) // Startpos |
| 242 |
mpt::IO::WriteAdaptiveInt64LE(mapStream, rposDataStart); |
| 243 |
if(m_Flags[RwfWMapSizeEntry]) // Entrysize |
| 244 |
mpt::IO::WriteAdaptiveInt64LE(mapStream, nDatasize); |
| 245 |
if(m_Flags[RwfWMapDescEntry]) // Entry descriptions |
| 246 |
WriteAdaptive12String(mapStream, pszDesc); |
| 247 |
|
| 248 |
m_MapStreamString.append(mapStream.str()); |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
void SsbWrite::BeginWrite(const ID &id, const uint64& nVersion) |
| 254 |
{ |
| 255 |
SSB_LOG(MPT_UFORMAT("Write header with ID = {}")(id.AsString())); |
| 256 |
|
| 257 |
ResetWritestatus(); |
| 258 |
|
| 259 |
if(!oStrm.good()) |
| 260 |
{ AddWriteNote(SNRW_BADGIVEN_STREAM); return; } |
| 261 |
|
| 262 |
// Start bytes. |
| 263 |
oStrm.write(s_EntryID, sizeof(s_EntryID)); |
| 264 |
|
| 265 |
m_posStart = static_cast<std::streamoff>(oStrm.tellp()) - static_cast<std::streamoff>(sizeof(s_EntryID)); |
| 266 |
|
| 267 |
// Object ID. |
| 268 |
{ |
| 269 |
uint8 idsize = static_cast<uint8>(id.GetSize()); |
| 270 |
Binarywrite<uint8>(oStrm, idsize); |
| 271 |
if(idsize > 0) oStrm.write(id.AsSpan().data(), id.AsSpan().size()); |
| 272 |
} |
| 273 |
|
| 274 |
// Form header. |
| 275 |
uint8 header = 0; |
| 276 |
|
| 277 |
m_Flags[RwfWMapStartPosEntry] = m_Flags[RwfWMapStartPosEntry] && m_nFixedEntrySize == 0; |
| 278 |
m_Flags[RwfWMapSizeEntry] = m_Flags[RwfWMapSizeEntry] && m_nFixedEntrySize == 0; |
| 279 |
|
| 280 |
header = (m_nIdbytes != 4) ? (m_nIdbytes & 3) : 3; //0,1 : Bytes per IDtype, 0,1,2,4 |
| 281 |
Setbit(header, 2, m_Flags[RwfWMapStartPosEntry]); //2 : Startpos in map? |
| 282 |
Setbit(header, 3, m_Flags[RwfWMapSizeEntry]); //3 : Datasize in map? |
| 283 |
Setbit(header, 4, m_Flags[RwfWVersionNum]); //4 : Version numeric field? |
| 284 |
Setbit(header, 7, m_Flags[RwfWMapDescEntry]); //7 : Entrydescriptions in map? |
| 285 |
|
| 286 |
// Write header |
| 287 |
Binarywrite<uint8>(oStrm, header); |
| 288 |
|
| 289 |
// Additional options. |
| 290 |
uint8 tempU8 = 0; |
| 291 |
Setbit(tempU8, 0, (m_nIdbytes == IdSizeVariable) || (m_nIdbytes == 3) || (m_nIdbytes > 4)); |
| 292 |
Setbit(tempU8, 1, m_nFixedEntrySize != 0); |
| 293 |
|
| 294 |
const uint8 flags = tempU8; |
| 295 |
if(flags != s_DefaultFlagbyte) |
| 296 |
{ |
| 297 |
mpt::IO::WriteAdaptiveInt32LE(oStrm, 2); //Headersize - now it is 2. |
| 298 |
Binarywrite<uint8>(oStrm, HeaderId_FlagByte); |
| 299 |
Binarywrite<uint8>(oStrm, flags); |
| 300 |
} |
| 301 |
else |
| 302 |
mpt::IO::WriteAdaptiveInt32LE(oStrm, 0); |
| 303 |
|
| 304 |
if(Testbit(header, 4)) // Version(numeric)? |
| 305 |
mpt::IO::WriteAdaptiveInt64LE(oStrm, nVersion); |
| 306 |
|
| 307 |
if(Testbit(flags, 0)) // Custom IDbytecount? |
| 308 |
{ |
| 309 |
uint8 n = (m_nIdbytes == IdSizeVariable) ? 1 : static_cast<uint8>((m_nIdbytes << 1)); |
| 310 |
Binarywrite<uint8>(oStrm, n); |
| 311 |
} |
| 312 |
|
| 313 |
if(Testbit(flags, 1)) // Fixedsize entries? |
| 314 |
mpt::IO::WriteAdaptiveInt32LE(oStrm, m_nFixedEntrySize); |
| 315 |
|
| 316 |
//Entrycount. Reserve two bytes(max uint16_max / 4 entries), actual value is written after writing data. |
| 317 |
m_posEntrycount = static_cast<std::streamoff>(oStrm.tellp()); |
| 318 |
Binarywrite<uint16>(oStrm, 0); |
| 319 |
|
| 320 |
m_Flags[RwfRwHasMap] = (m_nIdbytes != 0 || m_Flags[RwfWMapStartPosEntry] || m_Flags[RwfWMapSizeEntry] || m_Flags[RwfWMapDescEntry]); |
| 321 |
|
| 322 |
m_posMapPosField = static_cast<std::streamoff>(oStrm.tellp()); |
| 323 |
if(m_Flags[RwfRwHasMap]) // Mapping begin pos(reserve space - actual value is written after writing data) |
| 324 |
Binarywrite<uint64>(oStrm, 0); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
void SsbRead::OnReadEntry(const ReadEntry* pE, const ID &id, const std::streamoff& posReadBegin) |
| 329 |
{ |
| 330 |
#ifdef SSB_LOGGING |
| 331 |
if(pE) |
| 332 |
{ |
| 333 |
LogReadEntry(*pE, m_nCounter); |
| 334 |
} else if(!m_Flags[RwfRMapHasId]) // Not ID's in map. |
| 335 |
{ |
| 336 |
ReadEntry e; |
| 337 |
e.rposStart = posReadBegin - m_posStart; |
| 338 |
e.nSize = mpt::saturate_cast<std::size_t>(static_cast<std::streamoff>(static_cast<std::streamoff>(iStrm.tellg()) - posReadBegin)); |
| 339 |
LogReadEntry(e, m_nCounter); |
| 340 |
} else // Entry not found. |
| 341 |
{ |
| 342 |
SSB_LOG(MPT_UFORMAT("No entry with id {} found.")(id.AsString())); |
| 343 |
} |
| 344 |
#else |
| 345 |
MPT_UNREFERENCED_PARAMETER(id); |
| 346 |
MPT_UNREFERENCED_PARAMETER(posReadBegin); |
| 347 |
#endif |
| 348 |
const bool entryFound = (pE || !m_Flags[RwfRMapHasId]); |
| 349 |
if(entryFound) |
| 350 |
{ |
| 351 |
m_nCounter++; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
void SsbWrite::OnWroteItem(const ID &id, const std::streamoff& posBeforeWrite) |
| 357 |
{ |
| 358 |
const std::streamoff nRawEntrySize = static_cast<std::streamoff>(oStrm.tellp()) - posBeforeWrite; |
| 359 |
|
| 360 |
MPT_MAYBE_CONSTANT_IF(!mpt::in_range<std::size_t>(nRawEntrySize)) |
| 361 |
{ |
| 362 |
AddWriteNote(SNW_INSUFFICIENT_DATASIZETYPE); |
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
if(m_Flags[RwfRMapHasSize] && (static_cast<uint64>(nRawEntrySize) > (std::numeric_limits<std::size_t>::max() >> 2))) |
| 367 |
{ AddWriteNote(SNW_DATASIZETYPE_OVERFLOW); return; } |
| 368 |
|
| 369 |
std::size_t nEntrySize = static_cast<std::size_t>(nRawEntrySize); |
| 370 |
|
| 371 |
// Handle fixed size entries: |
| 372 |
if (m_nFixedEntrySize > 0) |
| 373 |
{ |
| 374 |
if(nEntrySize <= m_nFixedEntrySize) |
| 375 |
{ |
| 376 |
for(uint32 i = 0; i<m_nFixedEntrySize-nEntrySize; i++) |
| 377 |
oStrm.put(0); |
| 378 |
nEntrySize = m_nFixedEntrySize; |
| 379 |
} |
| 380 |
else |
| 381 |
{ AddWriteNote(SNW_INSUFFICIENT_FIXEDSIZE); return; } |
| 382 |
} |
| 383 |
if(m_Flags[RwfRwHasMap]) |
| 384 |
WriteMapItem(id, posBeforeWrite - m_posStart, nEntrySize, ""); |
| 385 |
|
| 386 |
#ifdef SSB_LOGGING |
| 387 |
LogWriteEntry(id, m_nCounter, nEntrySize, posBeforeWrite - m_posStart); |
| 388 |
#endif |
| 389 |
|
| 390 |
m_nCounter++; |
| 391 |
if(m_nCounter >= static_cast<uint16>(std::numeric_limits<uint16>::max() >> 2)) |
| 392 |
{ |
| 393 |
FinishWrite(); |
| 394 |
AddWriteNote(SNW_MAX_WRITE_COUNT_REACHED); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
void SsbRead::BeginRead(const ID &id, const uint64& nVersion) |
| 400 |
{ |
| 401 |
SSB_LOG(MPT_UFORMAT("Read header with expected ID = {}")(id.AsString())); |
| 402 |
|
| 403 |
ResetReadstatus(); |
| 404 |
|
| 405 |
if (!iStrm.good()) |
| 406 |
{ AddReadNote(SNRW_BADGIVEN_STREAM); return; } |
| 407 |
|
| 408 |
m_posStart = static_cast<std::streamoff>(iStrm.tellg()); |
| 409 |
|
| 410 |
// Start bytes. |
| 411 |
{ |
| 412 |
char temp[sizeof(s_EntryID)]; |
| 413 |
ArrayReader<char>(sizeof(s_EntryID))(iStrm, temp, sizeof(s_EntryID)); |
| 414 |
if(std::memcmp(temp, s_EntryID, sizeof(s_EntryID))) |
| 415 |
{ |
| 416 |
AddReadNote(SNR_STARTBYTE_MISMATCH); |
| 417 |
return; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
// Compare IDs. |
| 422 |
uint8 storedIdLen = 0; |
| 423 |
Binaryread<uint8>(iStrm, storedIdLen); |
| 424 |
std::array<char, 256> storedIdBuf; |
| 425 |
storedIdBuf = {}; |
| 426 |
if(storedIdLen > 0) |
| 427 |
{ |
| 428 |
iStrm.read(storedIdBuf.data(), storedIdLen); |
| 429 |
} |
| 430 |
if(!(id == ID(storedIdBuf.data(), storedIdLen))) |
| 431 |
{ |
| 432 |
AddReadNote(SNR_OBJECTCLASS_IDMISMATCH); |
| 433 |
} |
| 434 |
if(HasFailed()) |
| 435 |
{ |
| 436 |
SSB_LOG(U_("ID mismatch, terminating read.")); |
| 437 |
return; |
| 438 |
} |
| 439 |
|
| 440 |
SSB_LOG(U_("ID match, continuing reading.")); |
| 441 |
|
| 442 |
// Header |
| 443 |
uint8 tempU8; |
| 444 |
Binaryread<uint8>(iStrm, tempU8); |
| 445 |
const uint8 header = tempU8; |
| 446 |
m_nIdbytes = ((header & 3) == 3) ? 4 : (header & 3); |
| 447 |
if (Testbit(header, 6)) |
| 448 |
m_Flags[RwfRTwoBytesDescChar] = true; |
| 449 |
|
| 450 |
// Read headerdata size |
| 451 |
uint32 tempU32 = 0; |
| 452 |
mpt::IO::ReadAdaptiveInt32LE(iStrm, tempU32); |
| 453 |
const uint32 headerdatasize = tempU32; |
| 454 |
|
| 455 |
// If headerdatasize != 0, read known headerdata and ignore rest. |
| 456 |
uint8 flagbyte = s_DefaultFlagbyte; |
| 457 |
if(headerdatasize >= 2) |
| 458 |
{ |
| 459 |
Binaryread<uint8>(iStrm, tempU8); |
| 460 |
if(tempU8 == HeaderId_FlagByte) |
| 461 |
Binaryread<uint8>(iStrm, flagbyte); |
| 462 |
|
| 463 |
iStrm.ignore( (tempU8 == HeaderId_FlagByte) ? headerdatasize - 2 : headerdatasize - 1); |
| 464 |
} |
| 465 |
|
| 466 |
uint64 tempU64 = 0; |
| 467 |
|
| 468 |
// Read version numeric if available. |
| 469 |
if (Testbit(header, 4)) |
| 470 |
{ |
| 471 |
mpt::IO::ReadAdaptiveInt64LE(iStrm, tempU64); |
| 472 |
m_nReadVersion = tempU64; |
| 473 |
if(tempU64 > nVersion) |
| 474 |
AddReadNote(SNR_LOADING_OBJECT_WITH_LARGER_VERSION); |
| 475 |
} |
| 476 |
|
| 477 |
if (Testbit(header, 5)) |
| 478 |
{ |
| 479 |
Binaryread<uint8>(iStrm, tempU8); |
| 480 |
iStrm.ignore(tempU8); |
| 481 |
} |
| 482 |
|
| 483 |
if(Testbit(flagbyte, 0)) // Custom ID? |
| 484 |
{ |
| 485 |
Binaryread<uint8>(iStrm, tempU8); |
| 486 |
if ((tempU8 & 1) != 0) |
| 487 |
m_nIdbytes = IdSizeVariable; |
| 488 |
else |
| 489 |
m_nIdbytes = (tempU8 >> 1); |
| 490 |
if(m_nIdbytes == 0) |
| 491 |
AddReadNote(SNR_NO_ENTRYIDS_WITH_CUSTOMID_DEFINED); |
| 492 |
} |
| 493 |
|
| 494 |
m_nFixedEntrySize = 0; |
| 495 |
if(Testbit(flagbyte, 1)) // Fixedsize entries? |
| 496 |
mpt::IO::ReadAdaptiveInt32LE(iStrm, m_nFixedEntrySize); |
| 497 |
|
| 498 |
m_Flags[RwfRMapHasStartpos] = Testbit(header, 2); |
| 499 |
m_Flags[RwfRMapHasSize] = Testbit(header, 3); |
| 500 |
m_Flags[RwfRMapHasId] = (m_nIdbytes > 0); |
| 501 |
m_Flags[RwfRMapHasDesc] = Testbit(header, 7); |
| 502 |
m_Flags[RwfRwHasMap] = m_Flags[RwfRMapHasId] || m_Flags[RwfRMapHasStartpos] || m_Flags[RwfRMapHasSize] || m_Flags[RwfRMapHasDesc]; |
| 503 |
|
| 504 |
if(!m_Flags[RwfRwHasMap]) |
| 505 |
{ |
| 506 |
SSB_LOG(U_("No map in the file.")); |
| 507 |
} |
| 508 |
|
| 509 |
if (Testbit(flagbyte, 2)) // Object description? |
| 510 |
{ |
| 511 |
uint16 size = 0; |
| 512 |
mpt::IO::ReadAdaptiveInt16LE(iStrm, size); |
| 513 |
iStrm.ignore(size * (m_Flags[RwfRTwoBytesDescChar] ? 2 : 1)); |
| 514 |
} |
| 515 |
|
| 516 |
if(Testbit(flagbyte, 3)) |
| 517 |
iStrm.ignore(5); |
| 518 |
|
| 519 |
// Read entrycount |
| 520 |
mpt::IO::ReadAdaptiveInt64LE(iStrm, tempU64); |
| 521 |
if(tempU64 > 16000) |
| 522 |
// The current code can only write 16383 entries because it uses a Adaptive64LE with a fixed size=2 |
| 523 |
// Additionally, 16000 is an arbitrary limit to avoid an out-of-memory DoS when caching the map. |
| 524 |
{ AddReadNote(SNR_TOO_MANY_ENTRIES_TO_READ); return; } |
| 525 |
|
| 526 |
m_nReadEntrycount = static_cast<std::size_t>(tempU64); |
| 527 |
if(m_nReadEntrycount == 0) |
| 528 |
AddReadNote(SNR_ZEROENTRYCOUNT); |
| 529 |
|
| 530 |
// Read map rpos if map exists. |
| 531 |
if(m_Flags[RwfRwHasMap]) |
| 532 |
{ |
| 533 |
mpt::IO::ReadAdaptiveInt64LE(iStrm, tempU64); |
| 534 |
if(!mpt::in_range<std::streamoff>(tempU64)) |
| 535 |
{ AddReadNote(SNR_INSUFFICIENT_STREAM_OFFTYPE); return; } |
| 536 |
} |
| 537 |
|
| 538 |
const std::streamoff rawEndOfHdrData = static_cast<std::streamoff>(iStrm.tellg()) - m_posStart; |
| 539 |
|
| 540 |
if(rawEndOfHdrData < 0) |
| 541 |
{ |
| 542 |
AddReadNote(SNR_INSUFFICIENT_RPOSTYPE); |
| 543 |
return; |
| 544 |
} |
| 545 |
|
| 546 |
m_rposEndofHdrData = rawEndOfHdrData; |
| 547 |
m_rposMapBegin = (m_Flags[RwfRwHasMap]) ? static_cast<std::streamoff>(tempU64) : m_rposEndofHdrData; |
| 548 |
|
| 549 |
if(!m_Flags[RwfRwHasMap]) |
| 550 |
m_posMapEnd = m_posStart + m_rposEndofHdrData; |
| 551 |
|
| 552 |
m_Flags[RwfRHeaderIsRead] = true; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
void SsbRead::CacheMap() |
| 557 |
{ |
| 558 |
if(m_Flags[RwfRwHasMap] || m_nFixedEntrySize > 0) |
| 559 |
{ |
| 560 |
iStrm.seekg(m_posStart + m_rposMapBegin, std::ios::beg); |
| 561 |
|
| 562 |
if(iStrm.fail()) |
| 563 |
{ AddReadNote(SNR_BADSTREAM_AFTER_MAPHEADERSEEK); return; } |
| 564 |
|
| 565 |
SSB_LOG(MPT_UFORMAT("Reading map from rpos: {}")(m_rposMapBegin)); |
| 566 |
|
| 567 |
mapData.resize(m_nReadEntrycount); |
| 568 |
m_Idarray.reserve(m_nReadEntrycount * 4); |
| 569 |
|
| 570 |
//Read map |
| 571 |
for(std::size_t i = 0; i<m_nReadEntrycount; i++) |
| 572 |
{ |
| 573 |
if(iStrm.fail()) |
| 574 |
{ AddReadNote(SNR_BADSTREAM_AT_MAP_READ); return; } |
| 575 |
|
| 576 |
// Read ID. |
| 577 |
uint16 nIdsize = m_nIdbytes; |
| 578 |
if(nIdsize == IdSizeVariable) //Variablesize ID |
| 579 |
mpt::IO::ReadAdaptiveInt16LE(iStrm, nIdsize); |
| 580 |
const size_t nOldEnd = m_Idarray.size(); |
| 581 |
if (nIdsize > 0 && (Util::MaxValueOfType(nOldEnd) - nOldEnd >= nIdsize)) |
| 582 |
{ |
| 583 |
m_Idarray.resize(nOldEnd + nIdsize); |
| 584 |
iStrm.read(&m_Idarray[nOldEnd], nIdsize); |
| 585 |
} |
| 586 |
mapData[i].nIdLength = nIdsize; |
| 587 |
mapData[i].nIdpos = nOldEnd; |
| 588 |
|
| 589 |
// Read position. |
| 590 |
if(m_Flags[RwfRMapHasStartpos]) |
| 591 |
{ |
| 592 |
uint64 tempU64; |
| 593 |
mpt::IO::ReadAdaptiveInt64LE(iStrm, tempU64); |
| 594 |
if(!mpt::in_range<std::streamoff>(tempU64)) |
| 595 |
{ AddReadNote(SNR_INSUFFICIENT_STREAM_OFFTYPE); return; } |
| 596 |
mapData[i].rposStart = static_cast<std::streamoff>(tempU64); |
| 597 |
} |
| 598 |
|
| 599 |
// Read entry size. |
| 600 |
if (m_nFixedEntrySize > 0) |
| 601 |
mapData[i].nSize = m_nFixedEntrySize; |
| 602 |
else if(m_Flags[RwfRMapHasSize]) // Map has datasize field. |
| 603 |
{ |
| 604 |
uint64 tempU64; |
| 605 |
mpt::IO::ReadAdaptiveInt64LE(iStrm, tempU64); |
| 606 |
if(!mpt::in_range<std::streamoff>(tempU64)) |
| 607 |
{ AddReadNote(SNR_INSUFFICIENT_STREAM_OFFTYPE); return; } |
| 608 |
mapData[i].nSize = static_cast<std::size_t>(tempU64); |
| 609 |
} |
| 610 |
|
| 611 |
// If there's no entry startpos in map, count start pos from datasizes. |
| 612 |
// Here readentry.rposStart is set to relative position from databegin. |
| 613 |
if(mapData[i].nSize != invalidDatasize && !m_Flags[RwfRMapHasStartpos]) |
| 614 |
mapData[i].rposStart = (i > 0) ? mapData[i-1].rposStart + mapData[i-1].nSize : 0; |
| 615 |
|
| 616 |
if(m_Flags[RwfRMapHasDesc]) // Map has entrydescriptions? |
| 617 |
{ |
| 618 |
uint16 size = 0; |
| 619 |
mpt::IO::ReadAdaptiveInt16LE(iStrm, size); |
| 620 |
if(m_Flags[RwfRTwoBytesDescChar]) |
| 621 |
iStrm.ignore(size * 2); |
| 622 |
else |
| 623 |
iStrm.ignore(size); |
| 624 |
} |
| 625 |
} |
| 626 |
m_posMapEnd = static_cast<std::streamoff>(iStrm.tellg()); |
| 627 |
SSB_LOG(MPT_UFORMAT("End of map(rpos): {}")(m_posMapEnd - m_posStart)); |
| 628 |
} |
| 629 |
|
| 630 |
m_Flags[RwfRMapCached] = true; |
| 631 |
m_posDataBegin = (m_rposMapBegin == m_rposEndofHdrData) ? m_posMapEnd : m_posStart + m_rposEndofHdrData; |
| 632 |
iStrm.seekg(m_posDataBegin, std::ios::beg); |
| 633 |
|
| 634 |
// If there are no positions in the map but there are entry sizes, rposStart will |
| 635 |
// be relative to data start. Now that posDataBegin is known, make them relative to |
| 636 |
// startpos. |
| 637 |
if(!m_Flags[RwfRMapHasStartpos] && (m_Flags[RwfRMapHasSize] || m_nFixedEntrySize > 0)) |
| 638 |
{ |
| 639 |
const std::streamoff offset = m_posDataBegin - m_posStart; |
| 640 |
for(size_t i = 0; i < m_nReadEntrycount; i++) |
| 641 |
mapData[i].rposStart += offset; |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
const ReadEntry* SsbRead::Find(const ID &id) |
| 647 |
{ |
| 648 |
iStrm.clear(); |
| 649 |
if(!m_Flags[RwfRMapCached]) |
| 650 |
CacheMap(); |
| 651 |
|
| 652 |
if(m_nFixedEntrySize > 0 && !m_Flags[RwfRMapHasStartpos] && !m_Flags[RwfRMapHasSize]) |
| 653 |
iStrm.seekg(m_posDataBegin + static_cast<std::streamoff>(m_nFixedEntrySize * m_nCounter), std::ios::beg); |
| 654 |
|
| 655 |
if(m_Flags[RwfRMapHasId]) |
| 656 |
{ |
| 657 |
const size_t nEntries = mapData.size(); |
| 658 |
for(size_t i0 = 0; i0 < nEntries; i0++) |
| 659 |
{ |
| 660 |
const size_t i = (i0 + m_nNextReadHint) % nEntries; |
| 661 |
if(mapData[i].nIdpos < m_Idarray.size() && id == ID(&m_Idarray[mapData[i].nIdpos], mapData[i].nIdLength)) |
| 662 |
{ |
| 663 |
m_nNextReadHint = (i + 1) % nEntries; |
| 664 |
if (mapData[i].rposStart != 0) |
| 665 |
iStrm.seekg(m_posStart + mapData[i].rposStart, std::ios::beg); |
| 666 |
return &mapData[i]; |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
return nullptr; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
void SsbWrite::FinishWrite() |
| 675 |
{ |
| 676 |
const std::streamoff posDataEnd = static_cast<std::streamoff>(oStrm.tellp()); |
| 677 |
|
| 678 |
std::streamoff posMapStart = static_cast<std::streamoff>(oStrm.tellp()); |
| 679 |
|
| 680 |
SSB_LOG(MPT_UFORMAT("Writing map to rpos: {}")(posMapStart - m_posStart)); |
| 681 |
|
| 682 |
if(m_Flags[RwfRwHasMap]) // Write map |
| 683 |
{ |
| 684 |
oStrm.write(m_MapStreamString.c_str(), m_MapStreamString.length()); |
| 685 |
} |
| 686 |
|
| 687 |
const std::streamoff posMapEnd = static_cast<std::streamoff>(oStrm.tellp()); |
| 688 |
|
| 689 |
// Write entry count. |
| 690 |
oStrm.seekp(m_posEntrycount, std::ios::beg); |
| 691 |
|
| 692 |
// Write a fixed size=2 Adaptive64LE because space for this value has already been reserved berforehand. |
| 693 |
mpt::IO::WriteAdaptiveInt64LE(oStrm, m_nCounter, 2); |
| 694 |
|
| 695 |
if(m_Flags[RwfRwHasMap]) |
| 696 |
{ // Write map start position. |
| 697 |
oStrm.seekp(m_posMapPosField, std::ios::beg); |
| 698 |
const uint64 rposMap = posMapStart - m_posStart; |
| 699 |
|
| 700 |
// Write a fixed size=8 Adaptive64LE because space for this value has already been reserved berforehand. |
| 701 |
mpt::IO::WriteAdaptiveInt64LE(oStrm, rposMap, 8); |
| 702 |
|
| 703 |
} |
| 704 |
|
| 705 |
// Seek to end. |
| 706 |
oStrm.seekp(std::max(posMapEnd, posDataEnd), std::ios::beg); |
| 707 |
|
| 708 |
SSB_LOG(MPT_UFORMAT("End of stream(rpos): {}")(static_cast<std::streamoff>(oStrm.tellp()) - m_posStart)); |
| 709 |
} |
| 710 |
|
| 711 |
} // namespace srlztn |
| 712 |
|
| 713 |
|
| 714 |
OPENMPT_NAMESPACE_END |