From 648cecf1aa943845559c08ca96503c0a6d8907f9 Mon Sep 17 00:00:00 2001 From: NarcolepticK Date: Fri, 7 Sep 2018 23:09:14 -0400 Subject: [PATCH] service/cecd: Utilize CryptoPP::Base64Encoder for message id --- externals/cryptopp/CMakeLists.txt | 1 + src/core/hle/service/cecd/cecd.cpp | 46 ++++++++++++------------------ src/core/hle/service/cecd/cecd.h | 2 +- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/externals/cryptopp/CMakeLists.txt b/externals/cryptopp/CMakeLists.txt index fb2ba3588..db40f06cb 100644 --- a/externals/cryptopp/CMakeLists.txt +++ b/externals/cryptopp/CMakeLists.txt @@ -131,6 +131,7 @@ set(cryptopp_SOURCES cryptopp/algparam.cpp cryptopp/asn.cpp cryptopp/authenc.cpp + cryptopp/base64.cpp cryptopp/basecode.cpp cryptopp/ccm.cpp cryptopp/crc-simd.cpp diff --git a/src/core/hle/service/cecd/cecd.cpp b/src/core/hle/service/cecd/cecd.cpp index 9e649a5c1..87eabac78 100644 --- a/src/core/hle/service/cecd/cecd.cpp +++ b/src/core/hle/service/cecd/cecd.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/file_util.h" #include "common/logging/log.h" #include "common/string_util.h" @@ -691,30 +692,23 @@ void Module::Interface::OpenAndRead(Kernel::HLERequestContext& ctx) { open_mode.check); } -std::string Module::EncodeBase64(const std::vector& in, const std::string& dictionary) const { +std::string Module::EncodeBase64(const std::vector& in) const { + using namespace CryptoPP; + using Name::EncodingLookupArray; + using Name::InsertLineBreaks; + using Name::Pad; + std::string out; - out.reserve((in.size() * 4) / 3); - int b; - for (int i = 0; i < in.size(); i += 3) { - b = (in[i] & 0xFC) >> 2; - out += dictionary[b]; - b = (in[i] & 0x03) << 4; - if (i + 1 < in.size()) { - b |= (in[i + 1] & 0xF0) >> 4; - out += dictionary[b]; - b = (in[i + 1] & 0x0F) << 2; - if (i + 2 < in.size()) { - b |= (in[i + 2] & 0xC0) >> 6; - out += dictionary[b]; - b = in[i + 2] & 0x3F; - out += dictionary[b]; - } else { - out += dictionary[b]; - } - } else { - out += dictionary[b]; - } - } + Base64Encoder encoder; + AlgorithmParameters params = + MakeParameters(EncodingLookupArray(), (const byte*)base64_dict.data())(InsertLineBreaks(), + false)(Pad(), false); + + encoder.IsolatedInitialize(params); + encoder.Attach(new StringSink(out)); + encoder.Put(in.data(), in.size()); + encoder.MessageEnd(); + return out; } @@ -732,11 +726,9 @@ std::string Module::GetCecDataPathTypeAsString(const CecDataPathType type, const case CecDataPathType::OutboxIndex: return fmt::format("/CEC/{:08x}/OutBox__/OBIndex_____", program_id); case CecDataPathType::InboxMsg: - return fmt::format("/CEC/{:08x}/InBox___/_{}", program_id, - EncodeBase64(msg_id, base64_dict)); + return fmt::format("/CEC/{:08x}/InBox___/_{}", program_id, EncodeBase64(msg_id)); case CecDataPathType::OutboxMsg: - return fmt::format("/CEC/{:08x}/OutBox__/_{}", program_id, - EncodeBase64(msg_id, base64_dict)); + return fmt::format("/CEC/{:08x}/OutBox__/_{}", program_id, EncodeBase64(msg_id)); case CecDataPathType::RootDir: return "/CEC"; case CecDataPathType::MboxDir: diff --git a/src/core/hle/service/cecd/cecd.h b/src/core/hle/service/cecd/cecd.h index 777f825ce..4b0eff1f0 100644 --- a/src/core/hle/service/cecd/cecd.h +++ b/src/core/hle/service/cecd/cecd.h @@ -589,7 +589,7 @@ private: 0x26, 0x00, 0x01, 0x00}; /// Encoding function used for the message id - std::string EncodeBase64(const std::vector& in, const std::string& dictionary) const; + std::string EncodeBase64(const std::vector& in) const; std::string GetCecDataPathTypeAsString(const CecDataPathType type, const u32 program_id, const std::vector& msg_id = std::vector()) const;