/******************************************************************************* * * MIT License * * Copyright (c) 2021 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * *******************************************************************************/ #include "include_inliner.hpp" #include #include #include #include #include #include #include void Bin2Hex(std::istream& source, std::ostream& target, const std::string& variable, bool nullTerminate, size_t bufferSize, size_t lineSize) { source.seekg(0, std::ios::end); std::unique_ptr buffer(new unsigned char[bufferSize]); std::streamoff sourceSize = source.tellg(); std::streamoff blockStart = 0; if(variable.length() != 0) { target << "extern const size_t " << variable << "_SIZE;" << std::endl; target << "extern const unsigned char " << variable << "[];" << std::endl; target << "const size_t " << variable << "_SIZE = " << std::setbase(10) << sourceSize << ";" << std::endl; target << "const unsigned char " << variable << "[] = {" << std::endl; } target << std::setbase(16) << std::setfill('0'); source.seekg(0, std::ios::beg); while(blockStart < sourceSize) { source.read(reinterpret_cast(buffer.get()), bufferSize); std::streamoff pos = source.tellg(); std::streamoff blockSize = (pos < 0 ? sourceSize : pos) - blockStart; std::streamoff i = 0; while(i < blockSize) { size_t j = i; size_t end = std::min(i + lineSize, blockSize); for(; j < end; j++) target << "0x" << std::setw(2) << static_cast(buffer[j]) << ","; target << std::endl; i = end; } blockStart += blockSize; } if(nullTerminate) target << "0x00," << std::endl; if(variable.length() != 0) { target << "};" << std::endl; } } void PrintHelp() { std::cout << "Usage: bin2hex {