Blender V5.3
blendthumb_extract.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#include <cctype>
13#include <cstring>
14
15#include "BLI_alloca.hh"
16#include "BLI_endian_defines.hh"
17#include "BLI_fileops.hh"
18#include "BLI_filereader.hh"
19#include "BLI_string.hh"
20
21#include "BLO_core_bhead.hh"
24
25#include "blendthumb.hh"
26
27namespace blender {
28
29BLI_STATIC_ASSERT(ENDIAN_ORDER == L_ENDIAN, "Blender only builds on little endian systems")
30
32{
33 uint32_t *rect = reinterpret_cast<uint32_t *>(thumb->data.data());
34 int x = thumb->width, y = thumb->height;
35 uint32_t *top = rect;
36 uint32_t *bottom = top + ((y - 1) * x);
37 uint32_t *line = static_cast<uint32_t *>(malloc(x * sizeof(uint32_t)));
38
39 y >>= 1;
40 for (; y > 0; y--) {
41 memcpy(line, top, x * sizeof(uint32_t));
42 memcpy(top, bottom, x * sizeof(uint32_t));
43 memcpy(bottom, line, x * sizeof(uint32_t));
44 bottom -= x;
45 top += x;
46 }
47 free(line);
48}
49
50static int32_t bytes_to_native_i32(const uint8_t bytes[4])
51{
53 memcpy(&data, bytes, 4);
54 /* NOTE: this is endianness-sensitive. */
55 /* PNG is always little-endian, and would require switching on a big-endian system. */
56 return data;
57}
58
59static bool file_read(FileReader *file, uint8_t *buf, size_t buf_len)
60{
61 return (file->read(file, buf, buf_len) == buf_len);
62}
63
64static bool file_seek(FileReader *file, size_t len)
65{
66 if (file->seek != nullptr) {
67 if (file->seek(file, len, SEEK_CUR) == -1) {
68 return false;
69 }
70 return true;
71 }
72
73 /* File doesn't support seeking (e.g. gzip), so read and discard in chunks. */
74 constexpr size_t dummy_data_size = 4096;
75 Array<char> dummy_data(dummy_data_size);
76 while (len > 0) {
77 const size_t len_chunk = std::min(len, dummy_data_size);
78 if (size_t(file->read(file, dummy_data.data(), len_chunk)) != len_chunk) {
79 return false;
80 }
81 len -= len_chunk;
82 }
83 return true;
84}
85
87 Thumbnail *thumb,
88 const BlenderHeader &header)
89{
90 BLI_assert(header.endian == L_ENDIAN);
91 /* Iterate over file blocks until we find the thumbnail or run out of data. */
92 while (true) {
93 /* Read next BHead. */
94 const std::optional<BHead> bhead = BLO_readfile_read_bhead(file, header.bhead_type());
95 if (!bhead.has_value()) {
96 /* File has ended. */
97 return BT_INVALID_THUMB;
98 }
99 if (bhead->len < 0) {
100 /* Avoid parsing bad data. */
101 return BT_INVALID_THUMB;
102 }
103 switch (bhead->code) {
104 case MAKE_ID('T', 'E', 'S', 'T'): {
105 uint8_t shape[8];
106 if (bhead->len < int64_t(sizeof(shape)) || !file_read(file, shape, sizeof(shape))) {
107 return BT_INVALID_THUMB;
108 }
109 thumb->width = bytes_to_native_i32(&shape[0]);
110 thumb->height = bytes_to_native_i32(&shape[4]);
111
112 /* Verify that image dimensions and data size make sense, to handle
113 * corrupt files. Max size is rather arbitrary, large enough to fit
114 * any thumbnail and small enough it fits on any system. */
115 constexpr int max_size = 4096;
116 const size_t data_size = size_t(bhead->len) - sizeof(shape);
117 const uint64_t expected_size = uint64_t(thumb->width) * uint64_t(thumb->height) * 4;
118 if (thumb->width <= 0 || thumb->height <= 0 || thumb->width > max_size ||
119 thumb->height > max_size || data_size != expected_size)
120 {
121 return BT_INVALID_THUMB;
122 }
123
124 thumb->data = Array<uint8_t>(data_size);
125 if (!file_read(file, thumb->data.data(), data_size)) {
126 return BT_INVALID_THUMB;
127 }
128 return BT_OK;
129 }
130 case MAKE_ID('R', 'E', 'N', 'D'): {
131 if (!file_seek(file, bhead->len)) {
132 return BT_INVALID_THUMB;
133 }
134 /* Check the next block. */
135 break;
136 }
137 default: {
138 /* Early exit if there are no `TEST` or `REND` blocks.
139 * This saves scanning the entire blend file which could be slow. */
140 return BT_INVALID_THUMB;
141 }
142 }
143 }
144 return BT_INVALID_THUMB;
145}
146
148{
150
151 if (file == nullptr) {
152 return BT_ERROR;
153 }
154
155 const BlenderHeaderVariant header_variant = BLO_readfile_blender_header_decode(file);
156 if (!std::holds_alternative<BlenderHeader>(header_variant)) {
157 file->close(file);
158 return BT_ERROR;
159 }
160 const BlenderHeader &header = std::get<BlenderHeader>(header_variant);
161
162 /* Check if the file is new enough to contain a thumbnail. */
163 if (header.file_version < 250) {
164 file->close(file);
165 return BT_EARLY_VERSION;
166 }
167
168 /* Check if the file was written from a big-endian build. */
169 if (header.endian != L_ENDIAN) {
170 file->close(file);
171 return BT_INVALID_FILE;
172 }
173 BLI_assert(header.endian == ENDIAN_ORDER);
174
175 /* Read the thumbnail. */
176 eThumbStatus err = blendthumb_extract_from_file_impl(file, thumb, header);
177 file->close(file);
178 if (err != BT_OK) {
179 return err;
180 }
181
183 return BT_OK;
184}
185
186} // namespace blender
#define L_ENDIAN
#define ENDIAN_ORDER
File and directory operations.
Wrapper for reading from various sources (e.g. raw files, compressed files, memory....
#define MAKE_ID(a, b, c, d)
Definition blendthumb.hh:50
const T * data() const
Definition BLI_array.hh:323
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void free(Subdiv *subdiv)
Definition subdiv.cc:192
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
eThumbStatus blendthumb_create_thumb_from_file(FileReader *rawfile, Thumbnail *thumb)
FileReader * BLO_file_reader_uncompressed(FileReader *rawfile)
static void thumb_data_vertical_flip(Thumbnail *thumb)
std::optional< BHead > BLO_readfile_read_bhead(FileReader *file, BHeadType type)
unsigned int uint32_t
Definition BKE_text.h:81
static int32_t bytes_to_native_i32(const uint8_t bytes[4])
static bool file_read(FileReader *file, uint8_t *buf, size_t buf_len)
BMesh const char void * data
std::variant< BlenderHeaderInvalid, BlenderHeaderUnknown, BlenderHeader > BlenderHeaderVariant
unsigned char uint8_t
Definition BKE_text.h:79
static bool file_seek(FileReader *file, size_t len)
static eThumbStatus blendthumb_extract_from_file_impl(FileReader *file, Thumbnail *thumb, const BlenderHeader &header)
signed int int32_t
Definition BKE_text.h:78
@ BT_INVALID_THUMB
Definition blendthumb.hh:37
@ BT_INVALID_FILE
Definition blendthumb.hh:35
@ BT_EARLY_VERSION
Definition blendthumb.hh:36
BlenderHeaderVariant BLO_readfile_blender_header_decode(FileReader *file)
FileReaderCloseFn close
FileReaderSeekFn seek
FileReaderReadFn read
Array< uint8_t > data
Definition blendthumb.hh:25