File indexing completed on 2024-05-12 05:45:31

0001 /*
0002     Copyright Hannah von Reth <vonreth@kde.org>
0003 
0004     Redistribution and use in source and binary forms, with or without
0005     modification, are permitted provided that the following conditions
0006     are met:
0007     1. Redistributions of source code must retain the above copyright
0008        notice, this list of conditions and the following disclaimer.
0009     2. Redistributions in binary form must reproduce the above copyright
0010        notice, this list of conditions and the following disclaimer in the
0011        documentation and/or other materials provided with the distribution.
0012 
0013     THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0014     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0015     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0016     ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0017     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0018     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0019     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0020     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0021     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0022     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0023     SUCH DAMAGE.
0024 */
0025 #include "kshimgen_p.h"
0026 
0027 #include <algorithm>
0028 
0029 void KShimGenPrivate::setPayload(const std::filesystem::path &dest,
0030                                  const std::vector<uint8_t> &payload)
0031 {
0032     std::vector<char> dataOut;
0033     {
0034         std::ifstream in(dest.string(), std::ios::in | std::ios::binary);
0035         if (!in.is_open()) {
0036             kLog2(KLog::Type::Error) << "Failed to open in: " << dest.string();
0037             return exit(1);
0038         }
0039         in.seekg(0, std::ios::end);
0040         dataOut.resize(in.tellg(), 0);
0041         in.seekg(0);
0042         in.read(dataOut.data(), dataOut.size());
0043         in.close();
0044     }
0045 
0046     // look for the end mark and search for the start from there
0047     const KShimPayLoad findPayLoad { 0, KShimDataDef };
0048     const char *_start = reinterpret_cast<const char *>(&findPayLoad);
0049     auto cmdIt = std::search(dataOut.begin(), dataOut.end(), _start, _start + sizeof(KShimPayLoad));
0050     if (cmdIt == dataOut.end()) {
0051         kLog2(KLog::Type::Error) << "Failed to patch binary, please report your compiler";
0052         exit(1);
0053     }
0054     KShimPayLoad *outPayLoad = reinterpret_cast<KShimPayLoad *>(&*cmdIt);
0055 
0056     if (payload.size() > KShimLib::DataStorageSize) {
0057         kLog2(KLog::Type::Error) << "Data buffer is too small " << payload.size() << " > "
0058                                  << KShimLib::DataStorageSize << " :\n"
0059                                  << payload.data();
0060         return exit(1);
0061     }
0062     outPayLoad->size = payload.size();
0063     std::copy(payload.cbegin(), payload.cend(), outPayLoad->cmd);
0064 
0065     {
0066         std::ofstream out(dest.string(), std::ios::out | std::ios::binary);
0067         if (!out.is_open()) {
0068             kLog2(KLog::Type::Error) << "Failed to open out: " << dest.string();
0069             exit(1);
0070         }
0071         out.write(dataOut.data(), dataOut.size());
0072         out.close();
0073     }
0074 }