File: bin2typed.cpp

package info (click to toggle)
rocrand 6.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 102,860 kB
  • sloc: cpp: 203,022; f90: 2,500; python: 1,417; sh: 359; xml: 212; asm: 90; makefile: 50
file content (120 lines) | stat: -rw-r--r-- 4,262 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2023-2024 Advanced Micro Devices, Inc. All rights reserved.
//
// 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 "utils.hpp"

#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
#include <memory>
#include <string_view>

#include <cstddef>

#include <stdint.h>

template<typename T>
void write_array(const std::string_view type_name,
                 const std::byte*       bytes,
                 const size_t           size_bytes,
                 const std::string_view symbol,
                 std::ostream&          os)
{
    const size_t size_elements = size_bytes / sizeof(T);

    rocrand_tools::write_preamble(os, "bin2typed")
        << "#include \"rocrand/rocrandapi.h\"\n"
        << "\n"
        << "// clang-format off\n"
        << "extern \"C\" ROCRANDAPI const " << type_name << " " << symbol << '[' << size_elements
        << "] =\n"
        << '{';

    const T* values = reinterpret_cast<const T*>(bytes);

    for(size_t k = 0; k < size_elements; k++)
    {
        constexpr size_t bits = 8 * sizeof(T);
        if(k % bits == 0)
        {
            os << "\n    ";
        }
        os << "0x" << std::hex << std::setw(sizeof(T) * 2) << std::setfill('0') << values[k]
           << ", ";
    }
    os << '\n' << "};\n";
}

bool write_as_array_of(const std::string_view type,
                       const std::byte*       bytes,
                       const size_t           size_bytes,
                       const std::string_view symbol,
                       std::ostream&          os)
{
    if(type == "unsigned int")
    {
        write_array<unsigned int>(type, bytes, size_bytes, symbol, os);
        return true;
    }
    if(type == "unsigned long long")
    {
        write_array<uint64_t>(type, bytes, size_bytes, symbol, os);
        return true;
    }
    std::cerr << "Unexpected type:" << type
              << " expected \"unsigned int\" or \"unsigned long long\"";
    return false;
}

int main(int argc, char const* argv[])
{
    using namespace std::string_view_literals;
    if(argc != 5 || argv[1] == "--help"sv)
    {
        std::cout
            << "Convert <binary file> to a C source file treating it as an array with the name "
               "<symbol> of unsigned ints or long longs"
            << " and write it to <output>\n"
            << "Usage:\n"
            << "  " << argv[0]
            << " <binary file> <symbol> \"unsigned int\"|\"unsigned long long\" <output>\n";
        return -1;
    }

    const std::filesystem::path binary_path(argv[1]);
    const std::string_view      symbol(argv[2]);
    const std::string_view      type(argv[3]);
    const std::filesystem::path output_path(argv[4]);

    std::ifstream input(binary_path, std::ios_base::in | std::ios_base::binary);
    std::ofstream output(output_path, std::ios_base::out | std::ios_base::trunc);

    const std::uintmax_t input_size  = std::filesystem::file_size(binary_path);
    auto                 input_bytes = std::make_unique<std::byte[]>(input_size);

    input.read(reinterpret_cast<char*>(input_bytes.get()), input_size);

    if(!write_as_array_of(type, input_bytes.get(), input_size, symbol, output))
    {
        return -1;
    }
    return 0;
}