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