Repository navigation
Expand file tree
/
Copy pathp2bin.cpp
More file actions
124 lines (96 loc) · 3.49 KB
/
Copy pathp2bin.cpp
File metadata and controls
124 lines (96 loc) · 3.49 KB
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
121
122
123
124
/*
* Copyright (C) 2023-2025 Clownacy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* Documentation of AS's code file format can be found here:
http://john.ccac.rwth-aachen.de:8000/as/as_EN.html#sect_5_1_ */
/* Terminology in this code reflects the above documentation. */
#include "p2bin.h"
#include <array>
#include <exception>
#include <format>
#include <optional>
#include "utilities/io.h"
bool ConvertObjectFileToFlatBinary(std::istream &input_stream, std::ostream &output_stream, std::ostream &error_stream)
{
Utilities::IO::StreamBinaryReader input(input_stream);
std::optional<unsigned long> base_address;
const auto &ProcessSegment = [&]()
{
const auto start_address = input.ReadU32LE();
const auto length = input.ReadU16LE();
if (!base_address.has_value())
base_address = start_address;
else if (start_address < *base_address)
throw std::runtime_error("Error: Segment located before start of file.\n");
else
output_stream.seekp(start_address - *base_address);
/* Copy segment data. */
Utilities::IO::CopyBytes(input.stream, output_stream, length);
};
const auto &ProcessRecords = [&]()
{
for (;;)
{
const auto record_header = input.ReadU8();
switch (record_header)
{
case 0:
/* Creator string. This marks the end of the file. */
return;
case 0x80:
/* Entry point. We don't care about this. */
input.ReadU32LE();
break;
case 0x81:
{
/* Arbitrary segment. */
input.ReadU8(); /* Processor family. We don't care about this. */
input.ReadU8(); /* Segment. We don't care about this. */
const auto granularity = input.ReadU8();
if (granularity != 1)
throw std::runtime_error(std::format("Error: Unsupported granularity of {} (only 1 is supported).\n", granularity));
ProcessSegment();
break;
}
default:
if (record_header >= 0x80)
throw std::runtime_error(std::format("Error: Unrecognised record header value (0x{:02X}).\n", record_header));
/* Legacy CODE segment. */
ProcessSegment();
break;
}
}
};
bool success = false;
const auto previous_exceptions = input.stream.exceptions();
input.stream.exceptions(input.stream.badbit | input.stream.eofbit | input.stream.failbit);
try
{
/* Read and check the header's magic number. */
const auto magic = input.ReadU16BE();
/* TODO: Use error message manager for this. */
if (magic != 0x8914)
throw std::runtime_error(std::format("Error: Invalid header magic value - expected 0x8914 but got 0x{:04X}.\nInput file is either corrupt or not a valid AS code file.\n", magic));
ProcessRecords();
success = true;
}
catch (const std::runtime_error &error)
{
error_stream << error.what();
}
input.stream.exceptions(previous_exceptions);
return success;
}