summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLioncash <mathew1800@gmail.com>2018-10-13 09:30:09 -0400
committerLioncash <mathew1800@gmail.com>2018-10-13 09:50:08 -0400
commit6467b01de2debf86db8c409160ab50d24d32149e (patch)
tree7eae87a7d9906a9509d8856bb7368bcf6114eb6a
parent781fd7983cf543fc98685a38c82c5dd4576514dc (diff)
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.
-rw-r--r--src/core/crypto/partition_data_manager.cpp36
1 files changed, 16 insertions, 20 deletions
diff --git a/src/core/crypto/partition_data_manager.cpp b/src/core/crypto/partition_data_manager.cpp
index a311ad846..ed5e2b145 100644
--- a/src/core/crypto/partition_data_manager.cpp
+++ b/src/core/crypto/partition_data_manager.cpp
@@ -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);
}
}