partition_data_manager: Reserve and insert data within output vector in DecryptPackage2()

We can just reserve the memory then perform successive insertions
instead of needing to use memcpy. This also avoids the need to zero out
the output vector's memory before performing the insertions.

We can also std::move the output std::vector into the destination so
that we don't need to make a completely new copy of the vector, getting
rid of an unnecessary allocation.

Additionally, we can use iterators to determine the beginning and end
ranges of the std::vector instances that comprise the output vector, as
the end of one range just becomes the beginning for the next successive
range, and since std::vector's iterator constructor copies data within
the range [begin, end), this is more straightforward and gets rid of the
need to have an offset variable that keeps getting incremented to
determine where to do the next std::memcpy.
This commit is contained in:
Lioncash 2018-10-13 09:30:09 -04:00
parent 781fd7983c
commit 6467b01de2

View File

@ -499,33 +499,29 @@ void PartitionDataManager::DecryptPackage2(const std::array<Key128, 0x20>& packa
continue;
}
std::vector<u8> text(kip.sections[0].size_compressed);
std::vector<u8> rodata(kip.sections[1].size_compressed);
std::vector<u8> data(kip.sections[2].size_compressed);
const u64 initial_offset = sizeof(KIPHeader) + offset;
const auto text_begin = c.cbegin() + initial_offset;
const auto text_end = text_begin + kip.sections[0].size_compressed;
const std::vector<u8> text = DecompressBLZ({text_begin, text_end});
u64 offset_sec = sizeof(KIPHeader) + offset;
std::memcpy(text.data(), c.data() + offset_sec, text.size());
offset_sec += text.size();
std::memcpy(rodata.data(), c.data() + offset_sec, rodata.size());
offset_sec += rodata.size();
std::memcpy(data.data(), c.data() + offset_sec, data.size());
const auto rodata_end = text_end + kip.sections[1].size_compressed;
const std::vector<u8> rodata = DecompressBLZ({text_end, rodata_end});
offset += sizeof(KIPHeader) + kip.sections[0].size_compressed +
kip.sections[1].size_compressed + kip.sections[2].size_compressed;
const auto data_end = rodata_end + kip.sections[2].size_compressed;
const std::vector<u8> data = DecompressBLZ({rodata_end, data_end});
text = DecompressBLZ(text);
rodata = DecompressBLZ(rodata);
data = DecompressBLZ(data);
std::vector<u8> out;
out.reserve(text.size() + rodata.size() + data.size());
out.insert(out.end(), text.begin(), text.end());
out.insert(out.end(), rodata.begin(), rodata.end());
out.insert(out.end(), data.begin(), data.end());
std::vector<u8> out(text.size() + rodata.size() + data.size());
std::memcpy(out.data(), text.data(), text.size());
std::memcpy(out.data() + text.size(), rodata.data(), rodata.size());
std::memcpy(out.data() + text.size() + rodata.size(), data.data(), data.size());
offset += sizeof(KIPHeader) + out.size();
if (name == "FS")
package2_fs[static_cast<size_t>(type)] = out;
package2_fs[static_cast<size_t>(type)] = std::move(out);
else if (name == "spl")
package2_spl[static_cast<size_t>(type)] = out;
package2_spl[static_cast<size_t>(type)] = std::move(out);
}
}