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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/*
OpenMW - The completely unofficial reimplementation of Morrowind
Copyright (C) 2008-2010 Nicolay Korslund
Email: < korslund@gmail.com >
WWW: https://openmw.org/
This file (bsa_file.cpp) is part of the OpenMW package.
OpenMW is distributed as free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License
version 3, as published by the Free Software Foundation.
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
version 3 along with this program. If not, see
https://www.gnu.org/licenses/ .
*/
#include "bsa_file.hpp"
#include <components/esm/fourcc.hpp>
#include <components/files/constrainedfilestream.hpp>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <filesystem>
#include <fstream>
using namespace Bsa;
/// Error handling
[[noreturn]] void BSAFile::fail(const std::string& msg) const
{
throw std::runtime_error("BSA Error: " + msg + "\nArchive: " + Files::pathToUnicodeString(mFilepath));
}
// the getHash code is from bsapack from ghostwheel
// the code is also the same as in
// https://github.com/arviceblot/bsatool_rs/commit/67cb59ec3aaeedc0849222ea387f031c33e48c81
BSAFile::Hash getHash(const std::string& name)
{
BSAFile::Hash hash;
unsigned l = (static_cast<unsigned>(name.size()) >> 1);
unsigned sum, off, temp, i, n;
for (sum = off = i = 0; i < l; i++)
{
sum ^= (((unsigned)(name[i])) << (off & 0x1F));
off += 8;
}
hash.low = sum;
for (sum = off = 0; i < name.size(); i++)
{
temp = (((unsigned)(name[i])) << (off & 0x1F));
sum ^= temp;
n = temp & 0x1F;
sum = (sum << (32 - n)) | (sum >> n); // binary "rotate right"
off += 8;
}
hash.high = sum;
return hash;
}
/// Read header information from the input source
void BSAFile::readHeader()
{
/*
* The layout of a BSA archive is as follows:
*
* - 12 bytes header, contains 3 ints:
* id number - equal to 0x100
* dirsize - size of the directory block (see below)
* numfiles - number of files
*
* ---------- start of directory block -----------
*
* - 8 bytes*numfiles, each record contains:
* fileSize
* offset into data buffer (see below)
*
* - 4 bytes*numfiles, each record is an offset into the following name buffer
*
* - name buffer, indexed by the previous table, each string is
* null-terminated. Size is (dirsize - 12*numfiles).
*
* ---------- end of directory block -------------
*
* - 8*filenum - hash table block, we currently ignore this
*
* ----------- start of data buffer --------------
*
* - The rest of the archive is file data, indexed by the
* offsets in the directory block. The offsets start at 0 at
* the beginning of this buffer.
*
*/
assert(!mIsLoaded);
std::ifstream input(mFilepath, std::ios_base::binary);
// Total archive size
std::streamoff fsize = 0;
if (input.seekg(0, std::ios_base::end))
{
fsize = input.tellg();
input.seekg(0);
}
if (fsize < 12)
fail("File too small to be a valid BSA archive");
// Get essential header numbers
size_t dirsize, filenum;
{
// First 12 bytes
uint32_t head[3];
input.read(reinterpret_cast<char*>(head), 12);
if (head[0] != 0x100)
fail("Unrecognized BSA header");
// Total number of bytes used in size/offset-table + filename
// sections.
dirsize = head[1];
// Number of files
filenum = head[2];
}
// Each file must take up at least 21 bytes of data in the bsa. So
// if files*21 overflows the file size then we are guaranteed that
// the archive is corrupt.
if ((filenum * 21 > unsigned(fsize - 12)) || (dirsize + 8 * filenum > unsigned(fsize - 12)))
fail("Directory information larger than entire archive");
// Read the offset info into a temporary buffer
std::vector<uint32_t> offsets(3 * filenum);
input.read(reinterpret_cast<char*>(offsets.data()), 12 * filenum);
// Read the string table
mStringBuf.resize(dirsize - 12 * filenum);
input.read(mStringBuf.data(), mStringBuf.size());
// Check our position
assert(input.tellg() == std::streampos(12 + dirsize));
std::vector<Hash> hashes(filenum);
static_assert(sizeof(Hash) == 8);
input.read(reinterpret_cast<char*>(hashes.data()), 8 * filenum);
// Calculate the offset of the data buffer. All file offsets are
// relative to this. 12 header bytes + directory + hash table
// (skipped)
size_t fileDataOffset = 12 + dirsize + 8 * filenum;
// Set up the the FileStruct table
mFiles.resize(filenum);
size_t endOfNameBuffer = 0;
for (size_t i = 0; i < filenum; i++)
{
FileStruct& fs = mFiles[i];
fs.fileSize = offsets[i * 2];
fs.offset = static_cast<uint32_t>(offsets[i * 2 + 1] + fileDataOffset);
auto namesOffset = offsets[2 * filenum + i];
fs.setNameInfos(namesOffset, &mStringBuf);
fs.hash = hashes[i];
if (namesOffset >= mStringBuf.size())
{
fail("Archive contains names offset outside itself");
}
const void* end = std::memchr(fs.name(), '\0', mStringBuf.size() - namesOffset);
if (!end)
{
fail("Archive contains non-zero terminated string");
}
endOfNameBuffer = std::max(endOfNameBuffer, namesOffset + std::strlen(fs.name()) + 1);
assert(endOfNameBuffer <= mStringBuf.size());
if (fs.offset + fs.fileSize > fsize)
fail("Archive contains offsets outside itself");
}
mStringBuf.resize(endOfNameBuffer);
std::sort(mFiles.begin(), mFiles.end(),
[](const FileStruct& left, const FileStruct& right) { return left.offset < right.offset; });
mIsLoaded = true;
}
/// Write header information to the output sink
void Bsa::BSAFile::writeHeader()
{
std::fstream output(mFilepath, std::ios::binary | std::ios::in | std::ios::out);
uint32_t head[3];
head[0] = 0x100;
auto fileDataOffset = mFiles.empty() ? 12 : mFiles.front().offset;
head[1] = static_cast<uint32_t>(fileDataOffset - 12 - 8 * mFiles.size());
output.seekp(0, std::ios_base::end);
head[2] = static_cast<uint32_t>(mFiles.size());
output.seekp(0);
output.write(reinterpret_cast<char*>(head), 12);
std::sort(mFiles.begin(), mFiles.end(), [](const FileStruct& left, const FileStruct& right) {
return std::make_pair(left.hash.low, left.hash.high) < std::make_pair(right.hash.low, right.hash.high);
});
size_t filenum = mFiles.size();
std::vector<uint32_t> offsets(3 * filenum);
std::vector<Hash> hashes(filenum);
for (size_t i = 0; i < filenum; i++)
{
auto& f = mFiles[i];
offsets[i * 2] = f.fileSize;
offsets[i * 2 + 1] = f.offset - fileDataOffset;
offsets[2 * filenum + i] = f.namesOffset;
hashes[i] = f.hash;
}
output.write(reinterpret_cast<char*>(offsets.data()), sizeof(uint32_t) * offsets.size());
output.write(reinterpret_cast<char*>(mStringBuf.data()), mStringBuf.size());
output.seekp(fileDataOffset - 8 * mFiles.size(), std::ios_base::beg);
output.write(reinterpret_cast<char*>(hashes.data()), sizeof(Hash) * hashes.size());
}
/// Open an archive file.
void BSAFile::open(const std::filesystem::path& file)
{
if (mIsLoaded)
close();
mFilepath = file;
if (std::filesystem::exists(file))
readHeader();
else
{
{
std::fstream(mFilepath, std::ios::binary | std::ios::out);
}
writeHeader();
mIsLoaded = true;
}
}
/// Close the archive, write the updated headers to the file
void Bsa::BSAFile::close()
{
if (mHasChanged)
writeHeader();
mFiles.clear();
mStringBuf.clear();
mIsLoaded = false;
}
Files::IStreamPtr Bsa::BSAFile::getFile(const FileStruct* file)
{
return Files::openConstrainedFileStream(mFilepath, file->offset, file->fileSize);
}
void Bsa::BSAFile::addFile(const std::string& filename, std::istream& file)
{
if (!mIsLoaded)
fail("Unable to add file " + filename + " the archive is not opened");
auto newStartOfDataBuffer = 12 + (12 + 8) * (mFiles.size() + 1) + mStringBuf.size() + filename.size() + 1;
if (mFiles.empty())
std::filesystem::resize_file(mFilepath, newStartOfDataBuffer);
std::fstream stream(mFilepath, std::ios::binary | std::ios::in | std::ios::out);
FileStruct newFile;
file.seekg(0, std::ios::end);
newFile.fileSize = static_cast<uint32_t>(file.tellg());
newFile.setNameInfos(mStringBuf.size(), &mStringBuf);
newFile.hash = getHash(filename);
if (mFiles.empty())
newFile.offset = static_cast<uint32_t>(newStartOfDataBuffer);
else
{
std::vector<char> buffer;
while (mFiles.front().offset < newStartOfDataBuffer)
{
FileStruct& firstFile = mFiles.front();
buffer.resize(firstFile.fileSize);
stream.seekg(firstFile.offset, std::ios::beg);
stream.read(buffer.data(), firstFile.fileSize);
stream.seekp(0, std::ios::end);
firstFile.offset = static_cast<uint32_t>(stream.tellp());
stream.write(buffer.data(), firstFile.fileSize);
// ensure sort order is preserved
std::rotate(mFiles.begin(), mFiles.begin() + 1, mFiles.end());
}
stream.seekp(0, std::ios::end);
newFile.offset = static_cast<uint32_t>(stream.tellp());
}
mStringBuf.insert(mStringBuf.end(), filename.begin(), filename.end());
mStringBuf.push_back('\0');
mFiles.push_back(newFile);
mHasChanged = true;
stream.seekp(0, std::ios::end);
file.seekg(0, std::ios::beg);
stream << file.rdbuf();
}
BsaVersion Bsa::BSAFile::detectVersion(const std::filesystem::path& filePath)
{
std::ifstream input(filePath, std::ios_base::binary);
// Get essential header numbers
// First 12 bytes
uint32_t head[3];
input.read(reinterpret_cast<char*>(head), sizeof(head));
if (input.gcount() != sizeof(head))
return BsaVersion::Unknown;
if (head[0] == static_cast<uint32_t>(BsaVersion::Uncompressed))
{
return BsaVersion::Uncompressed;
}
if (head[0] == static_cast<uint32_t>(BsaVersion::Compressed))
{
return BsaVersion::Compressed;
}
if (head[0] == ESM::fourCC("BTDX"))
{
if (head[2] == ESM::fourCC("GNRL"))
return BsaVersion::BA2GNRL;
if (head[2] == ESM::fourCC("DX10"))
return BsaVersion::BA2DX10;
}
return BsaVersion::Unknown;
}
|