Blender V5.3
blo_core_bhead.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_filereader.hh"
6
7#include "BLO_core_bhead.hh"
8
9namespace blender {
10
11static BHead bhead_from_bhead4(const BHead4 &bhead4)
12{
13 BHead bhead;
14 bhead.code = bhead4.code;
15 bhead.len = bhead4.len;
16 bhead.old = reinterpret_cast<const void *>(uintptr_t(bhead4.old));
17 bhead.SDNAnr = bhead4.SDNAnr;
18 bhead.nr = bhead4.nr;
19 return bhead;
20}
21
22static const void *old_ptr_from_uint64_ptr(const uint64_t ptr)
23{
24 if constexpr (sizeof(void *) == 8) {
25 return reinterpret_cast<const void *>(ptr);
26 }
27 else {
28 return reinterpret_cast<const void *>(uintptr_t(uint32_from_uint64_ptr(ptr)));
29 }
30}
31
32static BHead bhead_from_small_bhead8(const SmallBHead8 &small_bhead8)
33{
34 BHead bhead;
35 bhead.code = small_bhead8.code;
36 bhead.len = small_bhead8.len;
37 bhead.old = old_ptr_from_uint64_ptr(small_bhead8.old);
38 bhead.SDNAnr = small_bhead8.SDNAnr;
39 bhead.nr = small_bhead8.nr;
40 return bhead;
41}
42
43static BHead bhead_from_large_bhead8(const LargeBHead8 &large_bhead8)
44{
45 BHead bhead;
46 bhead.code = large_bhead8.code;
47 bhead.len = large_bhead8.len;
48 bhead.old = old_ptr_from_uint64_ptr(large_bhead8.old);
49 bhead.SDNAnr = large_bhead8.SDNAnr;
50 bhead.nr = large_bhead8.nr;
51 return bhead;
52}
53
54std::optional<BHead> BLO_readfile_read_bhead(FileReader *file, const BHeadType type)
55{
56 /* NOTE: this is endianness-sensitive. */
57 /* The various BHead data below would need to be switched if the read blend-file was saved from a
58 * system using a different endianness. */
59 switch (type) {
60 case BHeadType::BHead4: {
61 BHead4 bhead4{};
62 bhead4.code = BLO_CODE_DATA;
63 const int64_t readsize = file->read(file, &bhead4, sizeof(bhead4));
64 if (readsize == sizeof(bhead4) || bhead4.code == BLO_CODE_ENDB) {
65 return bhead_from_bhead4(bhead4);
66 }
67 break;
68 }
70 SmallBHead8 small_bhead8{};
71 small_bhead8.code = BLO_CODE_DATA;
72 const int64_t readsize = file->read(file, &small_bhead8, sizeof(small_bhead8));
73 if (readsize == sizeof(small_bhead8) || small_bhead8.code == BLO_CODE_ENDB) {
74 return bhead_from_small_bhead8(small_bhead8);
75 }
76 break;
77 }
79 LargeBHead8 large_bhead8{};
80 large_bhead8.code = BLO_CODE_DATA;
81 const int64_t readsize = file->read(file, &large_bhead8, sizeof(large_bhead8));
82 if (readsize == sizeof(large_bhead8) || large_bhead8.code == BLO_CODE_ENDB) {
83 return bhead_from_large_bhead8(large_bhead8);
84 }
85 break;
86 }
87 }
88 return std::nullopt;
89}
90
91} // namespace blender
Wrapper for reading from various sources (e.g. raw files, compressed files, memory....
BMesh Iterators.
unsigned __int64 uint64_t
Definition BKE_text.h:91
static const void * old_ptr_from_uint64_ptr(const uint64_t ptr)
__int64 int64_t
Definition BKE_text.h:90
std::optional< BHead > BLO_readfile_read_bhead(FileReader *file, BHeadType type)
static BHead bhead_from_small_bhead8(const SmallBHead8 &small_bhead8)
uint32_t uint32_from_uint64_ptr(uint64_t ptr)
static BHead bhead_from_bhead4(const BHead4 &bhead4)
_W64 unsigned int uintptr_t
Definition BKE_text.h:120
static BHead bhead_from_large_bhead8(const LargeBHead8 &large_bhead8)
const void * old
FileReaderReadFn read