Debian Codegraph @

debian/patches/Byte-swap-integer-I-O-to-support-big-endian-architectures.patch

raw symbols SCIP
1 From: Gregor Riepl <[email protected]>
2 Date: Fri, 19 Dec 2025 13:02:38 +0100
3 Subject: Byte-swap integer I/O to support big endian architectures
4
5 Origin: https://github.com/prusa3d/libbgcode/pull/61
6 Bug: https://github.com/prusa3d/libbgcode/issues/47
7 Bug-Debian: https://bugs.debian.org/1062014
8 Forwarded: yes
9 ---
10 src/LibBGCode/binarize/binarize.cpp | 34 +++++++---------------
11 src/LibBGCode/core/core.cpp | 56 +++++++++++++------------------------
12 src/LibBGCode/core/core_impl.hpp | 43 +++++++++++++++++++++++++++-
13 3 files changed, 72 insertions(+), 61 deletions(-)
14
15 diff --git a/src/LibBGCode/binarize/binarize.cpp b/src/LibBGCode/binarize/binarize.cpp
16 index cb1b2ba..64d5801 100644
17 --- a/src/LibBGCode/binarize/binarize.cpp
18 +++ b/src/LibBGCode/binarize/binarize.cpp
19 @@ -18,22 +18,6 @@ using namespace core;
20
21 namespace binarize {
22
23 -template<class T>
24 -static bool write_to_file(FILE& file, const T* data, size_t data_size)
25 -{
26 - const size_t wsize = fwrite(static_cast<const void*>(data), 1, data_size, &file);
27 - return !ferror(&file) && wsize == data_size;
28 -}
29 -
30 -template<class T>
31 -static bool read_from_file(FILE& file, T *data, size_t data_size)
32 -{
33 - static_assert(!std::is_const_v<T>, "Type of output buffer cannot be const!");
34 -
35 - const size_t rsize = fread(static_cast<void *>(data), 1, data_size, &file);
36 - return !ferror(&file) && rsize == data_size;
37 -}
38 -
39 void update_checksum(Checksum& checksum, const ThumbnailBlock &th)
40 {
41 checksum.append(th.params.format);
42 @@ -436,7 +420,7 @@ core::EResult write(const BaseMetadataBlock &block, FILE& file, core::EBlockType
43 return res;
44
45 // write block payload
46 - if (!write_to_file(file, &block.encoding_type, sizeof(block.encoding_type)))
47 + if (!write_to_file_le(file, block.encoding_type))
48 return EResult::WriteError;
49 if (!out_data.empty()) {
50 if (!write_to_file(file, out_data.data(), out_data.size()))
51 @@ -459,7 +443,7 @@ EResult BaseMetadataBlock::read_data(FILE& file, const BlockHeader& block_header
52 {
53 const ECompressionType compression_type = (ECompressionType)block_header.compression;
54
55 - if (!read_from_file(file, (void*)&encoding_type, sizeof(encoding_type)))
56 + if (!read_from_file_le(file, encoding_type))
57 return EResult::ReadError;
58 if (encoding_type > metadata_encoding_types_count())
59 return EResult::InvalidMetadataEncodingType;
60 @@ -468,7 +452,7 @@ EResult BaseMetadataBlock::read_data(FILE& file, const BlockHeader& block_header
61 const size_t data_size = (compression_type == ECompressionType::None) ? block_header.uncompressed_size : block_header.compressed_size;
62 if (data_size > 0) {
63 data.resize(data_size);
64 - if (!read_from_file(file, (void*)data.data(), data_size))
65 + if (!read_from_file(file, data.data(), data_size))
66 return EResult::ReadError;
67 }
68
69 @@ -654,7 +638,7 @@ EResult ThumbnailBlock::read_data(FILE& file, const FileHeader& file_header, con
70 return EResult::InvalidThumbnailDataSize;
71
72 data.resize(block_header.uncompressed_size);
73 - if (!read_from_file(file, (void*)data.data(), block_header.uncompressed_size))
74 + if (!read_from_file(file, data.data(), block_header.uncompressed_size))
75 return EResult::ReadError;
76
77 const EChecksumType checksum_type = (EChecksumType)file_header.checksum_type;
78 @@ -699,7 +683,7 @@ EResult GCodeBlock::write(FILE& file, ECompressionType compression_type, EChecks
79 return res;
80
81 // write block payload
82 - if (!write_to_file(file, &encoding_type, sizeof(encoding_type)))
83 + if (!write_to_file_le(file, encoding_type))
84 return EResult::WriteError;
85 if (!out_data.empty()) {
86 if (!write_to_file(file, out_data.data(), out_data.size()))
87 @@ -712,8 +696,10 @@ EResult GCodeBlock::write(FILE& file, ECompressionType compression_type, EChecks
88 // update checksum with block header
89 update_checksum(cs, block_header);
90 // update checksum with block payload
91 + std::array<std::byte, sizeof(encoding_type)> swapped_encoding;
92 + store_integer_le(encoding_type, swapped_encoding.data());
93 std::vector<uint8_t> data_to_encode =
94 - encode(reinterpret_cast<const std::byte*>(&encoding_type), sizeof(encoding_type));
95 + encode(swapped_encoding.data(), swapped_encoding.size());
96 cs.append(data_to_encode.data(), data_to_encode.size());
97 if (!out_data.empty())
98 cs.append(static_cast<unsigned char *>(out_data.data()), out_data.size());
99 @@ -729,7 +715,7 @@ EResult GCodeBlock::read_data(FILE& file, const FileHeader& file_header, const B
100 {
101 const ECompressionType compression_type = (ECompressionType)block_header.compression;
102
103 - if (!read_from_file(file, (void*)&encoding_type, sizeof(encoding_type)))
104 + if (!read_from_file_le(file, encoding_type))
105 return EResult::ReadError;
106 if (encoding_type > gcode_encoding_types_count())
107 return EResult::InvalidGCodeEncodingType;
108 @@ -738,7 +724,7 @@ EResult GCodeBlock::read_data(FILE& file, const FileHeader& file_header, const B
109 const size_t data_size = (compression_type == ECompressionType::None) ? block_header.uncompressed_size : block_header.compressed_size;
110 if (data_size > 0) {
111 data.resize(data_size);
112 - if (!read_from_file(file, (void*)data.data(), data_size))
113 + if (!read_from_file(file, data.data(), data_size))
114 return EResult::ReadError;
115 }
116
117 diff --git a/src/LibBGCode/core/core.cpp b/src/LibBGCode/core/core.cpp
118 index 12d78b6..4763749 100644
119 --- a/src/LibBGCode/core/core.cpp
120 +++ b/src/LibBGCode/core/core.cpp
121 @@ -3,22 +3,6 @@
122
123 namespace bgcode { namespace core {
124
125 -template<class T>
126 -static bool write_to_file(FILE& file, const T* data, size_t data_size)
127 -{
128 - const size_t wsize = fwrite(static_cast<const void*>(data), 1, data_size, &file);
129 - return !ferror(&file) && wsize == data_size;
130 -}
131 -
132 -template<class T>
133 -static bool read_from_file(FILE& file, T *data, size_t data_size)
134 -{
135 - static_assert(!std::is_const_v<T>, "Type of output buffer cannot be const!");
136 -
137 - const size_t rsize = fread(static_cast<void *>(data), 1, data_size, &file);
138 - return !ferror(&file) && rsize == data_size;
139 -}
140 -
141 EResult verify_block_checksum(FILE& file, const FileHeader& file_header,
142 const BlockHeader& block_header, std::byte* buffer, size_t buffer_size)
143 {
144 @@ -112,11 +96,11 @@ EResult FileHeader::write(FILE& file) const
145 if (checksum_type >= checksum_types_count())
146 return EResult::InvalidChecksumType;
147
148 - if (!write_to_file(file, &magic, sizeof(magic)))
149 + if (!write_to_file_le(file, magic))
150 return EResult::WriteError;
151 - if (!write_to_file(file, &version, sizeof(version)))
152 + if (!write_to_file_le(file, version))
153 return EResult::WriteError;
154 - if (!write_to_file(file, &checksum_type, sizeof(checksum_type)))
155 + if (!write_to_file_le(file, checksum_type))
156 return EResult::WriteError;
157
158 return EResult::Success;
159 @@ -124,17 +108,17 @@ EResult FileHeader::write(FILE& file) const
160
161 EResult FileHeader::read(FILE& file, const uint32_t* const max_version)
162 {
163 - if (!read_from_file(file, &magic, sizeof(magic)))
164 + if (!read_from_file_le(file, magic))
165 return EResult::ReadError;
166 if (magic != MAGICi32)
167 return EResult::InvalidMagicNumber;
168
169 - if (!read_from_file(file, &version, sizeof(version)))
170 + if (!read_from_file_le(file, version))
171 return EResult::ReadError;
172 if (max_version != nullptr && version > *max_version)
173 return EResult::InvalidVersionNumber;
174
175 - if (!read_from_file(file, &checksum_type, sizeof(checksum_type)))
176 + if (!read_from_file_le(file, checksum_type))
177 return EResult::ReadError;
178 if (checksum_type >= checksum_types_count())
179 return EResult::InvalidChecksumType;
180 @@ -157,14 +141,14 @@ long BlockHeader::get_position() const
181 EResult BlockHeader::write(FILE& file)
182 {
183 m_position = ftell(&file);
184 - if (!write_to_file(file, &type, sizeof(type)))
185 + if (!write_to_file_le(file, type))
186 return EResult::WriteError;
187 - if (!write_to_file(file, &compression, sizeof(compression)))
188 + if (!write_to_file_le(file, compression))
189 return EResult::WriteError;
190 - if (!write_to_file(file, &uncompressed_size, sizeof(uncompressed_size)))
191 + if (!write_to_file_le(file, uncompressed_size))
192 return EResult::WriteError;
193 if (compression != (uint16_t)ECompressionType::None) {
194 - if (!write_to_file(file, &compressed_size, sizeof(compressed_size)))
195 + if (!write_to_file_le(file, compressed_size))
196 return EResult::WriteError;
197 }
198 return EResult::Success;
199 @@ -173,20 +157,20 @@ EResult BlockHeader::write(FILE& file)
200 EResult BlockHeader::read(FILE& file)
201 {
202 m_position = ftell(&file);
203 - if (!read_from_file(file, &type, sizeof(type)))
204 + if (!read_from_file_le(file, type))
205 return EResult::ReadError;
206 if (type >= block_types_count())
207 return EResult::InvalidBlockType;
208
209 - if (!read_from_file(file, &compression, sizeof(compression)))
210 + if (!read_from_file_le(file, compression))
211 return EResult::ReadError;
212 if (compression >= compression_types_count())
213 return EResult::InvalidCompressionType;
214
215 - if (!read_from_file(file, &uncompressed_size, sizeof(uncompressed_size)))
216 + if (!read_from_file_le(file, uncompressed_size))
217 return EResult::ReadError;
218 if (compression != (uint16_t)ECompressionType::None) {
219 - if (!read_from_file(file, &compressed_size, sizeof(compressed_size)))
220 + if (!read_from_file_le(file, compressed_size))
221 return EResult::ReadError;
222 }
223
224 @@ -199,21 +183,21 @@ size_t BlockHeader::get_size() const {
225 }
226
227 EResult ThumbnailParams::write(FILE& file) const {
228 - if (!write_to_file(file, &format, sizeof(format)))
229 + if (!write_to_file_le(file, format))
230 return EResult::WriteError;
231 - if (!write_to_file(file, &width, sizeof(width)))
232 + if (!write_to_file_le(file, width))
233 return EResult::WriteError;
234 - if (!write_to_file(file, &height, sizeof(height)))
235 + if (!write_to_file_le(file, height))
236 return EResult::WriteError;
237 return EResult::Success;
238 }
239
240 EResult ThumbnailParams::read(FILE& file){
241 - if (!read_from_file(file, &format, sizeof(format)))
242 + if (!read_from_file_le(file, format))
243 return EResult::ReadError;
244 - if (!read_from_file(file, &width, sizeof(width)))
245 + if (!read_from_file_le(file, width))
246 return EResult::ReadError;
247 - if (!read_from_file(file, &height, sizeof(height)))
248 + if (!read_from_file_le(file, height))
249 return EResult::ReadError;
250 return EResult::Success;
251 }
252 diff --git a/src/LibBGCode/core/core_impl.hpp b/src/LibBGCode/core/core_impl.hpp
253 index 2c38849..ac0a91d 100644
254 --- a/src/LibBGCode/core/core_impl.hpp
255 +++ b/src/LibBGCode/core/core_impl.hpp
256 @@ -120,7 +120,7 @@ public:
257
258 // Append any aritmetic data to the checksum (shorthand for aritmetic types)
259 template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
260 - void append(T& data) { append(reinterpret_cast<const std::byte*>(&data), sizeof(data)); }
261 + void append(T& data);
262
263 // Returns true if the given checksum is equal to this one
264 bool matches(Checksum& other);
265 @@ -168,12 +168,53 @@ void Checksum::append(const BufT *data, size_t size)
266 }
267 }
268
269 +template<typename T, typename>
270 +void Checksum::append(T& data)
271 +{
272 + std::array<std::byte, sizeof(T)> temp;
273 + store_integer_le(data, temp.begin(), temp.size());
274 + append(temp.data(), temp.size());
275 +}
276 +
277 static constexpr auto MAGICi32 = load_integer<uint32_t>(std::begin(MAGIC), std::end(MAGIC));
278
279 constexpr auto checksum_types_count() noexcept { auto v = to_underlying(EChecksumType::CRC32); ++v; return v;}
280 constexpr auto block_types_count() noexcept { auto v = to_underlying(EBlockType::Thumbnail); ++v; return v; }
281 constexpr auto compression_types_count() noexcept { auto v = to_underlying(ECompressionType::Heatshrink_12_4); ++v; return v; }
282
283 +template<class BufT>
284 +static bool write_to_file(FILE& file, const BufT* data, size_t data_size)
285 +{
286 + const size_t wsize = fwrite(static_cast<const void*>(data), 1, data_size, &file);
287 + return !ferror(&file) && wsize == data_size;
288 +}
289 +
290 +template<class T>
291 +static bool write_to_file_le(FILE& file, const T &data)
292 +{
293 + std::array<std::byte, sizeof(T)> temp;
294 + store_integer_le(data, temp.begin(), temp.size());
295 + return write_to_file(file, temp.data(), temp.size());
296 +}
297 +
298 +template<class BufT>
299 +static bool read_from_file(FILE& file, BufT *data, size_t data_size)
300 +{
301 + const size_t rsize = fread(static_cast<BufT *>(data), 1, data_size, &file);
302 + return !ferror(&file) && rsize == data_size;
303 +}
304 +
305 +template<class T>
306 +static bool read_from_file_le(FILE& file, T &data)
307 +{
308 + std::array<std::byte, sizeof(T)> temp;
309 + if (!read_from_file(file, temp.data(), temp.size())) {
310 + return false;
311 + }
312 + data = load_integer<T>(temp.begin(), temp.end());
313 + return true;
314 +}
315 +
316 } // namespace core
317 } // namespace bgcode
318