Blender V4.5
dna_genfile.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
14
15#include <algorithm>
16#include <climits>
17#include <cstdio>
18#include <cstdlib>
19#include <cstring>
20#include <iostream>
21
22#include <fmt/format.h>
23
24#include "MEM_guardedalloc.h" /* for MEM_freeN MEM_mallocN MEM_callocN */
25
26#include "BLI_endian_switch.h"
27#include "BLI_ghash.h"
28#include "BLI_index_range.hh"
30#include "BLI_memarena.h"
31#include "BLI_set.hh"
32#include "BLI_utildefines.h"
33
34#include "DNA_genfile.h"
35#include "DNA_print.hh"
36#include "DNA_sdna_types.h" /* for SDNA ;-) */
37
119
120#ifdef __BIG_ENDIAN__
121/* Big Endian */
122# define MAKE_ID(a, b, c, d) (int(a) << 24 | int(b) << 16 | (c) << 8 | (d))
123#else
124/* Little Endian */
125# define MAKE_ID(a, b, c, d) (int(d) << 24 | int(c) << 16 | (b) << 8 | (a))
126#endif
127
128/* ************************* DIV ********************** */
129
131{
132 if (sdna->data_alloc) {
133 MEM_freeN(sdna->data);
134 }
135
136 MEM_SAFE_FREE(sdna->members);
138 MEM_SAFE_FREE(sdna->types);
139 MEM_SAFE_FREE(sdna->structs);
141
142#ifdef WITH_DNA_GHASH
143 if (sdna->types_to_structs_map) {
144 BLI_ghash_free(sdna->types_to_structs_map, nullptr, nullptr);
145 }
146#endif
147
148 if (sdna->mem_arena) {
150 }
151
154#ifdef WITH_DNA_GHASH
155 if (sdna->alias.types_to_structs_map) {
156 BLI_ghash_free(sdna->alias.types_to_structs_map, nullptr, nullptr);
157 }
158#endif
159
160 MEM_freeN(sdna);
161}
162
163int DNA_struct_size(const SDNA *sdna, int struct_index)
164{
165 return sdna->types_size[sdna->structs[struct_index]->type_index];
166}
167
171static bool ispointer(const char *name)
172{
173 /* check if pointer or function pointer */
174 return (name[0] == '*' || (name[0] == '(' && name[1] == '*'));
175}
176
177int DNA_struct_member_size(const SDNA *sdna, short type, short member_index)
178{
179 const char *cp = sdna->members[member_index];
180 int len = 0;
181
182 /* is it a pointer or function pointer? */
183 if (ispointer(cp)) {
184 /* has the name an extra length? (array) */
185 len = sdna->pointer_size * sdna->members_array_num[member_index];
186 }
187 else if (sdna->types_size[type]) {
188 /* has the name an extra length? (array) */
189 len = int(sdna->types_size[type]) * sdna->members_array_num[member_index];
190 }
191
192 return len;
193}
194
195#if 0
196static void printstruct(SDNA *sdna, short struct_index)
197{
198 /* is for debug */
199
200 SDNA_Struct *struct_info = sdna->structs[struct_index];
201 printf("struct %s\n", sdna->types[struct_info->type]);
202
203 for (int b = 0; b < struct_info->members_len; b++) {
204 SDNA_StructMember *struct_member = &struct_info->members[b];
205 printf(" %s %s\n", sdna->types[struct_member->type], sdna->names[struct_member->name]);
206 }
207}
208#endif
209
214 /* From SDNA struct. */
215 const char **types,
216 const int /*types_num*/,
217 SDNA_Struct **const structs,
218 const int structs_num,
219#ifdef WITH_DNA_GHASH
220 GHash *structs_map,
221#endif
222 /* Regular args. */
223 const char *str,
224 uint *struct_index_last)
225{
226 if (*struct_index_last < structs_num) {
227 const SDNA_Struct *struct_info = structs[*struct_index_last];
228 if (STREQ(types[struct_info->type_index], str)) {
229 return *struct_index_last;
230 }
231 }
232
233#ifdef WITH_DNA_GHASH
234 {
235 void **struct_index_p = BLI_ghash_lookup_p(structs_map, str);
236 if (struct_index_p) {
237 const int struct_index = POINTER_AS_INT(*struct_index_p);
238 *struct_index_last = struct_index;
239 return struct_index;
240 }
241 }
242#else
243 {
244 for (int struct_index = 0; struct_index < types_num; struct_index++) {
245 const SDNA_Struct *struct_info = structs[struct_index];
246 if (STREQ(types[struct_info->type], str)) {
247 *struct_index_last = struct_index;
248 return struct_index;
249 }
250 }
251 }
252#endif
253 return -1;
254}
255
257 const char *str,
258 uint *struct_index_last)
259{
260#ifdef WITH_DNA_GHASH
261 BLI_assert(sdna->types_to_structs_map != nullptr);
262#endif
264 /* Expand SDNA. */
265 sdna->types,
266 sdna->types_num,
267 sdna->structs,
268 sdna->structs_num,
269#ifdef WITH_DNA_GHASH
271#endif
272 /* Regular args. */
273 str,
274 struct_index_last);
275}
276
277int DNA_struct_find_index_with_alias_ex(const SDNA *sdna, const char *str, uint *struct_index_last)
278{
279#ifdef WITH_DNA_GHASH
280 BLI_assert(sdna->alias.types_to_structs_map != nullptr);
281#endif
283 /* Expand SDNA. */
284 sdna->alias.types,
285 sdna->types_num,
286 sdna->structs,
287 sdna->structs_num,
288#ifdef WITH_DNA_GHASH
290#endif
291 /* Regular args. */
292 str,
293 struct_index_last);
294}
295
296int DNA_struct_find_index_without_alias(const SDNA *sdna, const char *str)
297{
298 uint index_last_dummy = UINT_MAX;
299 return DNA_struct_find_index_without_alias_ex(sdna, str, &index_last_dummy);
300}
301
302int DNA_struct_find_with_alias(const SDNA *sdna, const char *str)
303{
304 uint index_last_dummy = UINT_MAX;
305 return DNA_struct_find_index_with_alias_ex(sdna, str, &index_last_dummy);
306}
307
308bool DNA_struct_exists_with_alias(const SDNA *sdna, const char *str)
309{
310 return DNA_struct_find_with_alias(sdna, str) != -1;
311}
312
313/* ************************* END DIV ********************** */
314
315/* ************************* READ DNA ********************** */
316
317BLI_INLINE const char *pad_up_4(const char *ptr)
318{
319 return (const char *)((uintptr_t(ptr) + 3) & ~3);
320}
321
322template<typename T>
324 const int64_t num,
325 const void *const data_pointer_end,
326 const char **r_error_message)
327{
328 BLI_STATIC_ASSERT(std::is_pointer_v<T> && std::is_const_v<std::remove_pointer_t<T>>,
329 "Data type for this util must be a pointer to const data")
330 BLI_assert_msg(num >= 0, "Caller should ensure read size is always >= 0");
331 if ((data + num) > data_pointer_end) [[unlikely]] {
332 *r_error_message = "Invalid data in SDNA file";
333 return nullptr;
334 }
335 T ret = data;
336 data += num;
337 return ret;
338}
339
340template<typename T>
342 T data_pointer_current,
343 const void *const data_pointer_end,
344 const char **r_error_message)
345{
346 BLI_STATIC_ASSERT(std::is_pointer_v<T> && std::is_const_v<std::remove_pointer_t<T>>,
347 "Data type for this util must be a pointer to const data")
348 if (size < 0 || size > (static_cast<T>(data_pointer_end) - data_pointer_current)) [[unlikely]] {
349 *r_error_message = "Invalid array size data in SDNA file";
350 return false;
351 }
352 return true;
353}
354
355static const char *sdna_data_pointer_read_string(const char *&data,
356 const void *const data_pointer_end,
357 const char **r_error_message)
358{
359 BLI_assert(static_cast<const char *>(data_pointer_end) >= data);
360 const char *str_end = static_cast<const char *>(
361 memchr(data, '\0', size_t(static_cast<const char *>(data_pointer_end) - data)));
362 if (!str_end) [[unlikely]] {
363 *r_error_message = "Invalid string data in SDNA file";
364 return nullptr;
365 }
366 const int64_t string_size = str_end - data;
367 BLI_assert(string_size >= 0);
368 /* Non-empty string is a hard requirement by all use-cases currently. */
369 if (!string_size) [[unlikely]] {
370 *r_error_message = "Invalid string data in SDNA file";
371 return nullptr;
372 }
373 const char *ret = data;
374 data += (string_size + 1); /* String size + null terminator. */
375 return ret;
376}
377
391static bool sdna_struct_size_check(const SDNA *sdna,
392 const SDNA_Struct *struct_info,
393 const int64_t voided_members_size,
394 const char **r_error_message)
395{
396 int64_t members_size = voided_members_size;
397 for (const SDNA_StructMember &member :
398 blender::Span(struct_info->members, struct_info->members_num))
399 {
400 const char *member_name = sdna->members[member.member_index];
401 const int64_t array_num = sdna->members_array_num[member.member_index];
402 const int64_t member_size = ispointer(member_name) ? sdna->pointer_size :
403 sdna->types_size[member.type_index];
404 if (member_size < 0) [[unlikely]] {
405 *r_error_message = "Invalid struct member size in SDNA file";
406 return false;
407 }
408 members_size += member_size * array_num;
409 }
410 if (members_size != sdna->types_size[struct_info->type_index]) [[unlikely]] {
411 *r_error_message = "Invalid struct size in SDNA file";
412 return false;
413 }
414 return true;
415}
416
420static bool init_structDNA(SDNA *sdna, bool do_endian_swap, const char **r_error_message)
421{
422 /* The various pointers iterating over the SDNA::data buffer in this function should never reach
423 * this address. */
424 const void *const data_pointer_end = sdna->data + sdna->data_size;
425
426 int member_index_gravity_fix = -1;
427 const SDNA_Struct *gravity_fix_struct = nullptr;
428
429 const int *int_buf = reinterpret_cast<const int *>(sdna->data);
430 const int *int_data;
431 const short *short_buf;
432 const short *short_data;
433 const char *char_buf;
434
435 /* Clear pointers in case of error. */
436 sdna->types = nullptr;
437 sdna->types_size = nullptr;
438 sdna->types_alignment = nullptr;
439 sdna->structs = nullptr;
440#ifdef WITH_DNA_GHASH
441 sdna->types_to_structs_map = nullptr;
442#endif
443
444 sdna->members = nullptr;
445 sdna->members_array_num = nullptr;
446
447 sdna->mem_arena = nullptr;
448
449 /* Lazy initialize. */
450 memset(&sdna->alias, 0, sizeof(sdna->alias));
451
452 /* Struct DNA ('SDNA') */
453 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
454 return false;
455 }
456 if (*int_data != MAKE_ID('S', 'D', 'N', 'A')) {
457 *r_error_message = "SDNA error in SDNA file";
458 return false;
459 }
460
461 /* Names array ('NAME') */
462 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
463 return false;
464 }
465 if (*int_data == MAKE_ID('N', 'A', 'M', 'E')) {
466 /* NOTE: this is endianness-sensitive. */
467 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
468 return false;
469 }
470 sdna->members_num = *int_data;
471 if (do_endian_swap) {
473 }
475 sdna->members_num, int_buf, data_pointer_end, r_error_message))
476 {
477 return false;
478 }
479 sdna->members_num_alloc = sdna->members_num;
480 sdna->members = MEM_calloc_arrayN<const char *>(sdna->members_num, "sdnanames");
481 }
482 if (!sdna->members) {
483 *r_error_message = "NAME error in SDNA file";
484 return false;
485 }
486
487 char_buf = reinterpret_cast<const char *>(int_buf);
488 for (int member_index = 0; member_index < sdna->members_num; member_index++) {
489 const char *orig_member_name = sdna_data_pointer_read_string(
490 char_buf, data_pointer_end, r_error_message);
491 if (orig_member_name == nullptr) {
492 return false;
493 }
494 sdna->members[member_index] = orig_member_name;
495
496 /* "float gravity [3]" was parsed wrong giving both "gravity" and
497 * "[3]" members. we rename "[3]", and later set the type of
498 * "gravity" to "void" so the offsets work out correct. See #20330. */
499 if (STREQ(orig_member_name, "[3]")) {
500 if (member_index && STREQ(sdna->members[member_index - 1], "Cvi")) {
501 sdna->members[member_index] = "gravity[3]";
502 member_index_gravity_fix = member_index;
503 }
504 }
505 }
506 char_buf = pad_up_4(char_buf);
507
508 /* Type names array ('TYPE') */
509 int_buf = reinterpret_cast<const int *>(char_buf);
510 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
511 return false;
512 }
513 if (*int_data == MAKE_ID('T', 'Y', 'P', 'E')) {
514 /* NOTE: this is endianness-sensitive. */
515 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
516 return false;
517 }
518 sdna->types_num = *int_data;
519 if (do_endian_swap) {
521 }
523 sdna->types_num, int_buf, data_pointer_end, r_error_message))
524 {
525 return false;
526 }
527 sdna->types = MEM_calloc_arrayN<const char *>(sdna->types_num, "sdnatypes");
528 }
529 if (!sdna->types) {
530 *r_error_message = "TYPE error in SDNA file";
531 return false;
532 }
533
534 char_buf = reinterpret_cast<const char *>(int_buf);
535 for (int type_index = 0; type_index < sdna->types_num; type_index++) {
536 const char *orig_type_name = sdna_data_pointer_read_string(
537 char_buf, data_pointer_end, r_error_message);
538 if (orig_type_name == nullptr) {
539 return false;
540 }
541 /* WARNING! See: DNA_struct_rename_legacy_hack_static_from_alias docs. */
542 sdna->types[type_index] = DNA_struct_rename_legacy_hack_static_from_alias(orig_type_name);
543 }
544 char_buf = pad_up_4(char_buf);
545
546 /* Type lengths array ('TLEN') */
547 int_buf = reinterpret_cast<const int *>(char_buf);
548 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
549 return false;
550 }
551 short_buf = reinterpret_cast<const short *>(int_buf);
552 if (*int_data == MAKE_ID('T', 'L', 'E', 'N')) {
553 /* NOTE: this is endianness-sensitive. */
554 if (!(short_data = sdna_data_pointer_read(
555 short_buf, sdna->types_num, data_pointer_end, r_error_message)))
556 {
557 return false;
558 }
559 sdna->types_size = const_cast<short *>(short_data);
560
561 if (do_endian_swap) {
563 }
564 }
565 if (!sdna->types_size) {
566 *r_error_message = "TLEN error in SDNA file";
567 return false;
568 }
569 /* prevent BUS error */
570 if (sdna->types_num & 1) {
571 if (!sdna_data_pointer_read(short_buf, 1, data_pointer_end, r_error_message)) {
572 return false;
573 }
574 }
575
576 /* Struct array ('STRC') */
577 int_buf = reinterpret_cast<const int *>(short_buf);
578 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
579 return false;
580 }
581 if (*int_data == MAKE_ID('S', 'T', 'R', 'C')) {
582 /* NOTE: this is endianness-sensitive. */
583 if (!(int_data = sdna_data_pointer_read(int_buf, 1, data_pointer_end, r_error_message))) {
584 return false;
585 }
586 sdna->structs_num = *int_data;
587 if (do_endian_swap) {
589 }
591 sdna->structs_num, int_buf, data_pointer_end, r_error_message))
592 {
593 return false;
594 }
595 sdna->structs = MEM_calloc_arrayN<SDNA_Struct *>(sdna->structs_num, "sdnastrcs");
596 }
597 if (!sdna->structs) {
598 *r_error_message = "STRC error in SDNA file";
599 return false;
600 }
601
602 /* Safety check, to ensure that there is no multiple usages of a same struct index. */
603 blender::Set<short> struct_type_indices;
604 struct_type_indices.reserve(sdna->structs_num);
605 short_buf = reinterpret_cast<const short *>(int_buf);
606 for (int struct_index = 0; struct_index < sdna->structs_num; struct_index++) {
607 /* First read the always-valid part of SDNA_struct (its first two short members). */
608 /* NOTE: this is endianness-sensitive. */
609 if (!(short_data = sdna_data_pointer_read(short_buf, 2, data_pointer_end, r_error_message))) {
610 return false;
611 }
612 SDNA_Struct *struct_info = reinterpret_cast<SDNA_Struct *>(const_cast<short *>(short_data));
613 if (do_endian_swap) {
614 BLI_endian_switch_int16(&struct_info->type_index);
616 }
617
618 /* Now validate the size of the SDNA_StructMember array and consume it in the buffer too. */
620 constexpr int64_t struct_member_short_size = (sizeof(SDNA_StructMember) / sizeof(short));
621 const int64_t struct_members_size = struct_member_short_size * struct_info->members_num;
623 struct_members_size, short_buf, data_pointer_end, r_error_message) ||
624 !sdna_data_pointer_read(short_buf, struct_members_size, data_pointer_end, r_error_message))
625 {
626 return false;
627 }
628 sdna->structs[struct_index] = struct_info;
629
630 if (do_endian_swap) {
631 for (short a = 0; a < struct_info->members_num; a++) {
632 SDNA_StructMember *member = &struct_info->members[a];
635 }
636 }
637
638 if (struct_info->type_index < 0 || struct_info->type_index >= sdna->types_num) {
639 *r_error_message = "Invalid struct type index in SDNA file";
640 return false;
641 }
642 if (!struct_type_indices.add(struct_info->type_index)) {
643 *r_error_message = "Invalid duplicate struct type index in SDNA file";
644 return false;
645 }
646 for (int64_t struct_member_index : blender::IndexRange(struct_info->members_num)) {
647 const SDNA_StructMember &struct_member = struct_info->members[struct_member_index];
648 if (struct_member.type_index < 0 || struct_member.type_index >= sdna->types_num) {
649 *r_error_message = "Invalid struct member's type index in SDNA file";
650 return false;
651 }
652 if (struct_member.member_index < 0 || struct_member.member_index >= sdna->members_num) {
653 *r_error_message = "Invalid struct member's member index in SDNA file";
654 return false;
655 }
656 }
657
658 /* Second part of gravity problem, setting "gravity" type from float (7) to void (9).
659 * See #20330.
660 *
661 * NOTE: The gravity fix only applies to pre-2011 blend-files, which had `void` at
662 * type index 9 in their SDNA (`int64_t` was inserted before `void` in the `types`
663 * array later, shifting `void` to a later index in current Blender's SDNA, but the
664 * file's SDNA loaded here still has `void` at 9). */
665 if (member_index_gravity_fix > -1) {
666 constexpr int float_type_index = 7;
667 constexpr int void_type_index = 9;
668 constexpr int gravity_member_index = 4;
669 if (STREQ(sdna->types[struct_info->type_index], "ClothSimSettings")) {
670 if (struct_info->members_num < gravity_member_index + 2 ||
671 struct_info->members[gravity_member_index].type_index != float_type_index ||
672 !STREQ(sdna->members[struct_info->members[gravity_member_index].member_index],
673 "gravity") ||
674 !STREQ(sdna->members[struct_info->members[gravity_member_index + 1].member_index],
675 "gravity[3]") ||
676 float_type_index >= sdna->types_num ||
677 !STREQ(sdna->types[float_type_index], "float") || void_type_index >= sdna->types_num ||
678 !STREQ(sdna->types[void_type_index], "void"))
679 {
680 *r_error_message = "Invalid data in SDNA file";
681 return false;
682 }
683 struct_info->members[gravity_member_index].type_index = void_type_index;
684 gravity_fix_struct = struct_info;
685 }
686 }
687 }
688
689 /* The whole SDNA data should have been processed and consumed, so the current pointer should be
690 * at the expected 'end' value now. */
691 char_buf = reinterpret_cast<const char *>(short_buf);
692 if (char_buf != data_pointer_end) {
693 *r_error_message = "Invalid data in SDNA file";
694 return false;
695 }
696
697#ifdef WITH_DNA_GHASH
698 {
699 /* create a ghash lookup to speed up */
700 sdna->types_to_structs_map = BLI_ghash_str_new_ex("init_structDNA gh", sdna->structs_num);
701
702 for (intptr_t struct_index = 0; struct_index < sdna->structs_num; struct_index++) {
703 SDNA_Struct *struct_info = sdna->structs[struct_index];
705 (void *)sdna->types[struct_info->type_index],
706 POINTER_FROM_INT(struct_index));
707 }
708 }
709#endif
710
711 /* Calculate 'sdna->pointer_size'.
712 *
713 * NOTE: Cannot just do `sizeof(void *)` here, since the current DNA may come from a blend-file
714 * saved on a different system, using a different pointer size. So instead, use half the size of
715 * the #ListBase struct (only made of two pointers).
716 */
717 {
718 const int struct_index = DNA_struct_find_index_without_alias(sdna, "ListBase");
719
720 /* Should never happen, only with corrupt file for example. */
721 if (UNLIKELY(struct_index == -1)) {
722 *r_error_message = "ListBase struct error! Not found.";
723 return false;
724 }
725
726 const SDNA_Struct *struct_info = sdna->structs[struct_index];
727 sdna->pointer_size = sdna->types_size[struct_info->type_index] / 2;
728
729 /* Should never fail, double-check that #ListBase struct is still what is should be
730 * (a couple of pointers and nothing else). */
731 if (UNLIKELY(struct_info->members_num != 2 || !ELEM(sdna->pointer_size, 4, 8))) {
732 *r_error_message = "ListBase struct error: invalid computed pointer-size.";
733 return false;
734 }
735 }
736
737 /* Cache name size. */
738 {
739 short *members_array_num = MEM_malloc_arrayN<short>(size_t(sdna->members_num), __func__);
740 sdna->members_array_num = members_array_num;
741 for (int member_index = 0; member_index < sdna->members_num; member_index++) {
742 const int array_num = DNA_member_array_num(sdna->members[member_index]);
743 if (array_num < 0 || array_num > SHRT_MAX) [[unlikely]] {
744 *r_error_message = "Invalid struct member array size in SDNA file";
745 return false;
746 }
747 members_array_num[member_index] = short(array_num);
748 }
749 }
750
751 /* Reject internally inconsistent DNA before it reaches struct reconstruction. */
752 for (const SDNA_Struct *struct_info : blender::Span(sdna->structs, sdna->structs_num)) {
753 /* The old makesdna counted the wrongly parsed "gravity" float in the struct size. */
754 const int64_t voided_members_size = (struct_info == gravity_fix_struct) ? sizeof(float) : 0;
755 if (!sdna_struct_size_check(sdna, struct_info, voided_members_size, r_error_message)) {
756 return false;
757 }
758 }
759
760 sdna->types_alignment = MEM_malloc_arrayN<int>(size_t(sdna->types_num), __func__);
761 for (int type_index = 0; type_index < sdna->types_num; type_index++) {
762 sdna->types_alignment[type_index] = int(__STDCPP_DEFAULT_NEW_ALIGNMENT__);
763 }
764 {
765 /* TODO: This should be generalized at some point. We should be able to specify `overaligned`
766 * types directly in the DNA struct definitions. */
767 uint dummy_index = 0;
768 const int mat4x4f_struct_index = DNA_struct_find_index_without_alias_ex(
769 sdna, "mat4x4f", &dummy_index);
770 if (mat4x4f_struct_index > 0) {
771 const SDNA_Struct *struct_info = sdna->structs[mat4x4f_struct_index];
772 const int mat4x4f_type_index = struct_info->type_index;
773 sdna->types_alignment[mat4x4f_type_index] = alignof(blender::float4x4);
774 }
775 }
776
777 return true;
778}
779
781 const int data_len,
782 bool do_endian_swap,
783 bool data_alloc,
784 const bool do_alias,
785 const char **r_error_message)
786{
787 SDNA *sdna = MEM_mallocN<SDNA>("sdna");
788 const char *error_message = nullptr;
789
790 sdna->data_size = data_len;
791 if (data_alloc) {
792 char *data_copy = MEM_malloc_arrayN<char>(size_t(data_len), "sdna_data");
793 memcpy(data_copy, data, data_len);
794 sdna->data = data_copy;
795 }
796 else {
797 sdna->data = static_cast<const char *>(data);
798 }
799 sdna->data_alloc = data_alloc;
800
801 if (init_structDNA(sdna, do_endian_swap, &error_message)) {
802 if (do_alias) {
804 }
805 return sdna;
806 }
807
808 if (r_error_message == nullptr) {
809 fprintf(stderr, "Error decoding blend file SDNA: %s\n", error_message);
810 }
811 else {
812 *r_error_message = error_message;
813 }
814 DNA_sdna_free(sdna);
815 return nullptr;
816}
817
824static SDNA *g_sdna = nullptr;
825
827{
828 g_sdna = DNA_sdna_from_data(DNAstr, DNAlen, false, false, true, nullptr);
829}
830
832{
833 BLI_assert(g_sdna != nullptr);
834 return g_sdna;
835}
836
838{
840 g_sdna = nullptr;
841}
842
843/* ******************** END READ DNA ********************** */
844
845/* ******************* HANDLE DNA ***************** */
846
851static void set_compare_flags_for_struct(const SDNA *oldsdna,
852 const SDNA *newsdna,
853 char *compare_flags,
854 const int old_struct_index)
855{
856 if (compare_flags[old_struct_index] != SDNA_CMP_UNKNOWN) {
857 /* This flag has been initialized already. */
858 return;
859 }
860
861 SDNA_Struct *old_struct = oldsdna->structs[old_struct_index];
862 const char *struct_name = oldsdna->types[old_struct->type_index];
863
864 const int new_struct_index = DNA_struct_find_index_without_alias(newsdna, struct_name);
865 if (new_struct_index == -1) {
866 /* Didn't find a matching new struct, so it has been removed. */
867 compare_flags[old_struct_index] = SDNA_CMP_REMOVED;
868 return;
869 }
870
871 SDNA_Struct *new_struct = newsdna->structs[new_struct_index];
872 if (old_struct->members_num != new_struct->members_num) {
873 /* Structs with a different amount of members are not equal. */
874 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
875 return;
876 }
877 if (oldsdna->types_size[old_struct->type_index] != newsdna->types_size[new_struct->type_index]) {
878 /* Structs that don't have the same size are not equal. */
879 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
880 return;
881 }
882
883 /* Compare each member individually. */
884 for (int member_index = 0; member_index < old_struct->members_num; member_index++) {
885 const SDNA_StructMember *old_member = &old_struct->members[member_index];
886 const SDNA_StructMember *new_member = &new_struct->members[member_index];
887
888 const char *old_type_name = oldsdna->types[old_member->type_index];
889 const char *new_type_name = newsdna->types[new_member->type_index];
890 if (!STREQ(old_type_name, new_type_name)) {
891 /* If two members have a different type in the same place, the structs are not equal. */
892 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
893 return;
894 }
895
896 const char *old_member_name = oldsdna->members[old_member->member_index];
897 const char *new_member_name = newsdna->members[new_member->member_index];
898 if (!STREQ(old_member_name, new_member_name)) {
899 /* If two members have a different name in the same place, the structs are not equal. */
900 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
901 return;
902 }
903
904 if (ispointer(old_member_name)) {
905 if (oldsdna->pointer_size != newsdna->pointer_size) {
906 /* When the struct contains a pointer, and the pointer sizes differ, the structs are not
907 * equal. */
908 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
909 return;
910 }
911 }
912 else {
913 const int old_member_struct_index = DNA_struct_find_index_without_alias(oldsdna,
914 old_type_name);
915 if (old_member_struct_index >= 0) {
916 set_compare_flags_for_struct(oldsdna, newsdna, compare_flags, old_member_struct_index);
917 if (compare_flags[old_member_struct_index] != SDNA_CMP_EQUAL) {
918 /* If an embedded struct is not equal, the parent struct cannot be equal either. */
919 compare_flags[old_struct_index] = SDNA_CMP_NOT_EQUAL;
920 return;
921 }
922 }
923 }
924 }
925
926 compare_flags[old_struct_index] = SDNA_CMP_EQUAL;
927}
928
929const char *DNA_struct_get_compareflags(const SDNA *oldsdna, const SDNA *newsdna)
930{
931 if (oldsdna->structs_num == 0) {
932 printf("error: file without SDNA\n");
933 return nullptr;
934 }
935
936 char *compare_flags = MEM_malloc_arrayN<char>(size_t(oldsdna->structs_num), "compare flags");
937 memset(compare_flags, SDNA_CMP_UNKNOWN, oldsdna->structs_num);
938
939 /* Set correct flag for every struct. */
940 for (int old_struct_index = 0; old_struct_index < oldsdna->structs_num; old_struct_index++) {
941 set_compare_flags_for_struct(oldsdna, newsdna, compare_flags, old_struct_index);
942 BLI_assert(compare_flags[old_struct_index] != SDNA_CMP_UNKNOWN);
943 }
944
945 /* First struct is the fake 'raw data' one (see the #SDNA_TYPE_RAW_DATA 'basic type' definition
946 * and its usages). By definition, it is always 'equal'.
947 *
948 * NOTE: Bugs History (pre-4.3).
949 *
950 * It used to be `struct Link`, it was skipped in compare_flags (at index `0`). This was a bug,
951 * and was dirty-patched by setting `compare_flags[0]` to `SDNA_CMP_EQUAL` unconditionally.
952 *
953 * Then the `0` struct became `struct DrawDataList`, which was never actually written in
954 * blend-files.
955 *
956 * Write and read blend-file code also has had implicit assumptions that a `0` value in the
957 * #BHead.SDNAnr (aka DNA struct index) meant 'raw data', and therefore was not representing any
958 * real DNA struct. This assumption has been false for years. By luck, this bug seems to have
959 * been fully harmless, for at least the following reasons:
960 * - Read code always ignored DNA struct info in BHead blocks with a `0` value.
961 * - `DrawDataList` data was never actually written in blend-files.
962 * - `struct Link` never needed DNA-versioning.
963 *
964 * NOTE: This may have been broken in BE/LE conversion cases, however this endianness handling
965 * code have likely been dead/never used in practice for many years now.
966 */
967 BLI_STATIC_ASSERT(SDNA_RAW_DATA_STRUCT_INDEX == 0, "'raw data' SDNA struct index should be 0")
969
970/* This code can be enabled to see which structs have changed. */
971#if 0
972 for (int a = 0; a < oldsdna->structs_len; a++) {
973 if (compare_flags[a] == SDNA_CMP_NOT_EQUAL) {
974 SDNA_Struct *struct_info = oldsdna->structs[a];
975 printf("changed: %s\n", oldsdna->types[struct_info->type]);
976 }
977 }
978#endif
979
980 return compare_flags;
981}
982
995static void cast_primitive_type(const eSDNA_Type old_type,
996 const eSDNA_Type new_type,
997 const int array_len,
998 const char *old_data,
999 char *new_data)
1000{
1001 /* define lengths */
1002 const int oldlen = DNA_elem_type_size(old_type);
1003 const int curlen = DNA_elem_type_size(new_type);
1004
1005 double old_value_f = 0.0;
1006 /* Intentionally overflow signed values into an unsigned type.
1007 * Casting back to a signed value preserves the sign (when the new value is signed). */
1008 uint64_t old_value_i = 0;
1009
1010 for (int a = 0; a < array_len; a++) {
1011 switch (old_type) {
1012 case SDNA_TYPE_CHAR: {
1013 const char value = *old_data;
1014 old_value_i = value;
1015 old_value_f = double(value);
1016 break;
1017 }
1018 case SDNA_TYPE_UCHAR: {
1019 const uchar value = *reinterpret_cast<const uchar *>(old_data);
1020 old_value_i = value;
1021 old_value_f = double(value);
1022 break;
1023 }
1024 case SDNA_TYPE_SHORT: {
1025 const short value = *reinterpret_cast<const short *>(old_data);
1026 old_value_i = value;
1027 old_value_f = double(value);
1028 break;
1029 }
1030 case SDNA_TYPE_USHORT: {
1031 const ushort value = *reinterpret_cast<const ushort *>(old_data);
1032 old_value_i = value;
1033 old_value_f = double(value);
1034 break;
1035 }
1036 case SDNA_TYPE_INT: {
1037 const int value = *reinterpret_cast<const int *>(old_data);
1038 old_value_i = value;
1039 old_value_f = double(value);
1040 break;
1041 }
1042 case SDNA_TYPE_FLOAT: {
1043 const float value = *reinterpret_cast<const float *>(old_data);
1044 /* `int64_t` range stored in a `uint64_t`. */
1045 old_value_i = uint64_t(int64_t(value));
1046 old_value_f = value;
1047 break;
1048 }
1049 case SDNA_TYPE_DOUBLE: {
1050 const double value = *reinterpret_cast<const double *>(old_data);
1051 /* `int64_t` range stored in a `uint64_t`. */
1052 old_value_i = uint64_t(int64_t(value));
1053 old_value_f = value;
1054 break;
1055 }
1056 case SDNA_TYPE_INT64: {
1057 const int64_t value = *reinterpret_cast<const int64_t *>(old_data);
1058 old_value_i = uint64_t(value);
1059 old_value_f = double(value);
1060 break;
1061 }
1062 case SDNA_TYPE_UINT64: {
1063 const uint64_t value = *reinterpret_cast<const uint64_t *>(old_data);
1064 old_value_i = value;
1065 old_value_f = double(value);
1066 break;
1067 }
1068 case SDNA_TYPE_INT8: {
1069 const int8_t value = *reinterpret_cast<const int8_t *>(old_data);
1070 old_value_i = uint64_t(value);
1071 old_value_f = double(value);
1072 break;
1073 }
1074 case SDNA_TYPE_RAW_DATA:
1075 BLI_assert_msg(false, "Conversion from SDNA_TYPE_RAW_DATA is not supported");
1076 break;
1077 }
1078
1079 switch (new_type) {
1080 case SDNA_TYPE_CHAR:
1081 *new_data = char(old_value_i);
1082 break;
1083 case SDNA_TYPE_UCHAR:
1084 *reinterpret_cast<uchar *>(new_data) = uchar(old_value_i);
1085 break;
1086 case SDNA_TYPE_SHORT:
1087 *reinterpret_cast<short *>(new_data) = short(old_value_i);
1088 break;
1089 case SDNA_TYPE_USHORT:
1090 *reinterpret_cast<ushort *>(new_data) = ushort(old_value_i);
1091 break;
1092 case SDNA_TYPE_INT:
1093 *reinterpret_cast<int *>(new_data) = int(old_value_i);
1094 break;
1095 case SDNA_TYPE_FLOAT:
1096 if (old_type < 2) {
1097 old_value_f /= 255.0;
1098 }
1099 *reinterpret_cast<float *>(new_data) = old_value_f;
1100 break;
1101 case SDNA_TYPE_DOUBLE:
1102 if (old_type < 2) {
1103 old_value_f /= 255.0;
1104 }
1105 *reinterpret_cast<double *>(new_data) = old_value_f;
1106 break;
1107 case SDNA_TYPE_INT64:
1108 *reinterpret_cast<int64_t *>(new_data) = int64_t(old_value_i);
1109 break;
1110 case SDNA_TYPE_UINT64:
1111 *reinterpret_cast<uint64_t *>(new_data) = old_value_i;
1112 break;
1113 case SDNA_TYPE_INT8:
1114 *reinterpret_cast<int8_t *>(new_data) = int8_t(old_value_i);
1115 break;
1116 case SDNA_TYPE_RAW_DATA:
1117 BLI_assert_msg(false, "Conversion to SDNA_TYPE_RAW_DATA is not supported");
1118 break;
1119 }
1120
1121 old_data += oldlen;
1122 new_data += curlen;
1123 }
1124}
1125
1126static void cast_pointer_32_to_64(const int array_len,
1127 const uint32_t *old_data,
1128 uint64_t *new_data)
1129{
1130 for (int a = 0; a < array_len; a++) {
1131 new_data[a] = old_data[a];
1132 }
1133}
1134
1135static void cast_pointer_64_to_32(const int array_len,
1136 const uint64_t *old_data,
1137 uint32_t *new_data)
1138{
1139 /* WARNING: 32-bit Blender trying to load file saved by 64-bit Blender,
1140 * pointers may lose uniqueness on truncation! (Hopefully this won't
1141 * happen unless/until we ever get to multi-gigabyte .blend files...) */
1142 for (int a = 0; a < array_len; a++) {
1143 new_data[a] = old_data[a] >> 3;
1144 }
1145}
1146
1150static bool elem_streq(const char *name, const char *oname)
1151{
1152 int a = 0;
1153
1154 while (true) {
1155 if (name[a] != oname[a]) {
1156 return false;
1157 }
1158 if (name[a] == '[' || oname[a] == '[') {
1159 break;
1160 }
1161 if (name[a] == 0 || oname[a] == 0) {
1162 break;
1163 }
1164 a++;
1165 }
1166 return true;
1167}
1168
1179 /* Expand SDNA. */
1180 const char **types,
1181 const char **names,
1182 /* Regular args. */
1183 const char *type,
1184 const char *name,
1185 const SDNA_Struct *old)
1186{
1187 /* in old is the old struct */
1188 for (int a = 0; a < old->members_num; a++) {
1189 const SDNA_StructMember *member = &old->members[a];
1190 const char *otype = types[member->type_index];
1191 const char *oname = names[member->member_index];
1192
1193 if (elem_streq(name, oname)) { /* name equal */
1194 return STREQ(type, otype); /* type equal */
1195 }
1196 }
1197 return false;
1198}
1199
1203static bool elem_exists_without_alias(const SDNA *sdna,
1204 const char *type,
1205 const char *name,
1206 const SDNA_Struct *old)
1207{
1208 return elem_exists_impl(
1209 /* Expand SDNA. */
1210 sdna->types,
1211 sdna->members,
1212 /* Regular args. */
1213 type,
1214 name,
1215 old);
1216}
1217
1218static bool elem_exists_with_alias(const SDNA *sdna,
1219 const char *type,
1220 const char *name,
1221 const SDNA_Struct *old)
1222{
1223 return elem_exists_impl(
1224 /* Expand SDNA. */
1225 sdna->alias.types,
1226 sdna->alias.members,
1227 /* Regular args. */
1228 type,
1229 name,
1230 old);
1231}
1232
1233static int elem_offset_impl(const SDNA *sdna,
1234 const char **types,
1235 const char **names,
1236 const char *type,
1237 const char *name,
1238 const SDNA_Struct *old)
1239{
1240 /* Without array-part, so names can differ: return old `namenr` and type. */
1241
1242 /* in old is the old struct */
1243 int offset = 0;
1244 for (int a = 0; a < old->members_num; a++) {
1245 const SDNA_StructMember *member = &old->members[a];
1246 const char *otype = types[member->type_index];
1247 const char *oname = names[member->member_index];
1248 if (elem_streq(name, oname)) { /* name equal */
1249 if (STREQ(type, otype)) { /* type equal */
1250 return offset;
1251 }
1252 break; /* Fail below. */
1253 }
1254 offset += DNA_struct_member_size(sdna, member->type_index, member->member_index);
1255 }
1256 return -1;
1257}
1258
1274static int elem_offset_without_alias(const SDNA *sdna,
1275 const char *type,
1276 const char *name,
1277 const SDNA_Struct *old)
1278{
1279 return elem_offset_impl(sdna, sdna->types, sdna->members, type, name, old);
1280}
1281
1285static int elem_offset_with_alias(const SDNA *sdna,
1286 const char *type,
1287 const char *name,
1288 const SDNA_Struct *old)
1289{
1290 return elem_offset_impl(sdna, sdna->alias.types, sdna->alias.members, type, name, old);
1291}
1292
1293/* Each struct member belongs to one of the categories below. */
1299
1301 const SDNA_StructMember *member)
1302{
1303 const char *member_name = sdna->members[member->member_index];
1304 if (ispointer(member_name)) {
1306 }
1307 const char *member_type_name = sdna->types[member->type_index];
1308 if (DNA_struct_exists_without_alias(sdna, member_type_name)) {
1310 }
1312}
1313
1314static int get_member_size_in_bytes(const SDNA *sdna, const SDNA_StructMember *member)
1315{
1316 const char *name = sdna->members[member->member_index];
1317 const int array_length = sdna->members_array_num[member->member_index];
1318 if (ispointer(name)) {
1319 return sdna->pointer_size * array_length;
1320 }
1321 const int type_size = sdna->types_size[member->type_index];
1322 return type_size * array_length;
1323}
1324
1325void DNA_struct_switch_endian(const SDNA *sdna, int struct_index, char *data)
1326{
1327 if (struct_index == -1) {
1328 return;
1329 }
1330
1331 const SDNA_Struct *struct_info = sdna->structs[struct_index];
1332
1333 int offset_in_bytes = 0;
1334 for (int member_index = 0; member_index < struct_info->members_num; member_index++) {
1335 const SDNA_StructMember *member = &struct_info->members[member_index];
1336 const eStructMemberCategory member_category = get_struct_member_category(sdna, member);
1337 char *member_data = data + offset_in_bytes;
1338 const char *member_type_name = sdna->types[member->type_index];
1339 const int member_array_length = sdna->members_array_num[member->member_index];
1340
1341 switch (member_category) {
1343 const int substruct_size = sdna->types_size[member->type_index];
1344 const int substruct_index = DNA_struct_find_index_without_alias(sdna, member_type_name);
1345 BLI_assert(substruct_index != -1);
1346 for (int a = 0; a < member_array_length; a++) {
1347 DNA_struct_switch_endian(sdna, substruct_index, member_data + a * substruct_size);
1348 }
1349 break;
1350 }
1352 switch (member->type_index) {
1353 case SDNA_TYPE_SHORT:
1354 case SDNA_TYPE_USHORT: {
1355 BLI_endian_switch_int16_array((int16_t *)member_data, member_array_length);
1356 break;
1357 }
1358 case SDNA_TYPE_INT:
1359 case SDNA_TYPE_FLOAT: {
1360 /* NOTE: intentionally ignore `long/ulong`, because these could be 4 or 8 bytes.
1361 * Fortunately, we only use these types for runtime variables and only once for a
1362 * struct type that is no longer used. */
1363 BLI_endian_switch_int32_array((int32_t *)member_data, member_array_length);
1364 break;
1365 }
1366 case SDNA_TYPE_INT64:
1367 case SDNA_TYPE_UINT64:
1368 case SDNA_TYPE_DOUBLE: {
1369 BLI_endian_switch_int64_array((int64_t *)member_data, member_array_length);
1370 break;
1371 }
1372 default: {
1373 break;
1374 }
1375 }
1376 break;
1377 }
1379 /* See `readfile.cc` (#uint32_from_uint64_ptr swap endian argument),
1380 * this is only done when reducing the size of a pointer from 4 to 8. */
1381 if (sizeof(void *) < 8) {
1382 if (sdna->pointer_size == 8) {
1383 BLI_endian_switch_uint64_array((uint64_t *)member_data, member_array_length);
1384 }
1385 }
1386 break;
1387 }
1388 }
1389 offset_in_bytes += get_member_size_in_bytes(sdna, member);
1390 }
1391}
1392
1401
1404 union {
1405 struct {
1406 int old_offset;
1407 int new_offset;
1408 int size;
1409 } memcpy;
1410 struct {
1411 int old_offset;
1412 int new_offset;
1413 int array_len;
1417 struct {
1418 int old_offset;
1419 int new_offset;
1420 int array_len;
1421 } cast_pointer;
1422 struct {
1423 int old_offset;
1424 int new_offset;
1425 int array_len;
1426 short old_struct_index;
1427 short new_struct_index;
1428 } substruct;
1430};
1431
1440
1441static void reconstruct_structs(const DNA_ReconstructInfo *reconstruct_info,
1442 const int blocks,
1443 const int old_struct_index,
1444 const int new_struct_index,
1445 const char *old_blocks,
1446 char *new_blocks);
1447
1457static void reconstruct_struct(const DNA_ReconstructInfo *reconstruct_info,
1458 const int new_struct_index,
1459 const char *old_block,
1460 char *new_block)
1461{
1462 const ReconstructStep *steps = reconstruct_info->steps[new_struct_index];
1463 const int step_count = reconstruct_info->step_counts[new_struct_index];
1464
1465 /* Execute all preprocessed steps. */
1466 for (int a = 0; a < step_count; a++) {
1467 const ReconstructStep *step = &steps[a];
1468 switch (step->type) {
1470 memcpy(new_block + step->data.memcpy.new_offset,
1471 old_block + step->data.memcpy.old_offset,
1472 step->data.memcpy.size);
1473 break;
1475 cast_primitive_type(step->data.cast_primitive.old_type,
1476 step->data.cast_primitive.new_type,
1477 step->data.cast_primitive.array_len,
1478 old_block + step->data.cast_primitive.old_offset,
1479 new_block + step->data.cast_primitive.new_offset);
1480 break;
1482 cast_pointer_64_to_32(step->data.cast_pointer.array_len,
1483 (const uint64_t *)(old_block + step->data.cast_pointer.old_offset),
1484 (uint32_t *)(new_block + step->data.cast_pointer.new_offset));
1485 break;
1487 cast_pointer_32_to_64(step->data.cast_pointer.array_len,
1488 (const uint32_t *)(old_block + step->data.cast_pointer.old_offset),
1489 (uint64_t *)(new_block + step->data.cast_pointer.new_offset));
1490 break;
1492 reconstruct_structs(reconstruct_info,
1493 step->data.substruct.array_len,
1494 step->data.substruct.old_struct_index,
1495 step->data.substruct.new_struct_index,
1496 old_block + step->data.substruct.old_offset,
1497 new_block + step->data.substruct.new_offset);
1498 break;
1500 /* Do nothing, because the memory block are zeroed (from #MEM_callocN).
1501 *
1502 * Note that the struct could be initialized with the default struct,
1503 * however this complicates versioning, especially with flags, see: D4500. */
1504 break;
1505 }
1506 }
1507}
1508
1510static void reconstruct_structs(const DNA_ReconstructInfo *reconstruct_info,
1511 const int blocks,
1512 const int old_struct_index,
1513 const int new_struct_index,
1514 const char *old_blocks,
1515 char *new_blocks)
1516{
1517 const SDNA_Struct *old_struct = reconstruct_info->oldsdna->structs[old_struct_index];
1518 const SDNA_Struct *new_struct = reconstruct_info->newsdna->structs[new_struct_index];
1519
1520 const int old_block_size = reconstruct_info->oldsdna->types_size[old_struct->type_index];
1521 const int new_block_size = reconstruct_info->newsdna->types_size[new_struct->type_index];
1522
1523 for (int a = 0; a < blocks; a++) {
1524 const char *old_block = old_blocks + a * old_block_size;
1525 char *new_block = new_blocks + a * new_block_size;
1526 reconstruct_struct(reconstruct_info, new_struct_index, old_block, new_block);
1527 }
1528}
1529
1530void *DNA_struct_reconstruct(const DNA_ReconstructInfo *reconstruct_info,
1531 int old_struct_index,
1532 int blocks,
1533 const void *old_blocks,
1534 const char *alloc_name)
1535{
1536 const SDNA *oldsdna = reconstruct_info->oldsdna;
1537 const SDNA *newsdna = reconstruct_info->newsdna;
1538
1539 const SDNA_Struct *old_struct = oldsdna->structs[old_struct_index];
1540 const char *type_name = oldsdna->types[old_struct->type_index];
1541 const int new_struct_index = DNA_struct_find_index_without_alias(newsdna, type_name);
1542
1543 if (new_struct_index == -1) {
1544 return nullptr;
1545 }
1546
1547 const SDNA_Struct *new_struct = newsdna->structs[new_struct_index];
1548 const int new_block_size = newsdna->types_size[new_struct->type_index];
1549
1550 const int alignment = DNA_struct_alignment(newsdna, new_struct_index);
1551 char *new_blocks = static_cast<char *>(
1552 MEM_calloc_arrayN_aligned(new_block_size, blocks, alignment, alloc_name));
1553 reconstruct_structs(reconstruct_info,
1554 blocks,
1555 old_struct_index,
1556 new_struct_index,
1557 static_cast<const char *>(old_blocks),
1558 new_blocks);
1559 return new_blocks;
1560}
1561
1564 const SDNA_Struct *struct_info,
1565 const char *name,
1566 int *r_offset)
1567{
1568 int offset = 0;
1569 for (int a = 0; a < struct_info->members_num; a++) {
1570 const SDNA_StructMember *member = &struct_info->members[a];
1571 const char *member_name = sdna->members[member->member_index];
1572 if (elem_streq(name, member_name)) {
1573 *r_offset = offset;
1574 return member;
1575 }
1576 offset += get_member_size_in_bytes(sdna, member);
1577 }
1578 return nullptr;
1579}
1580
1582static void init_reconstruct_step_for_member(const SDNA *oldsdna,
1583 const SDNA *newsdna,
1584 const char *compare_flags,
1585 const SDNA_Struct *old_struct,
1586 const SDNA_StructMember *new_member,
1587 const int new_member_offset,
1588 ReconstructStep *r_step)
1589{
1590
1591 /* Find the matching old member. */
1592 int old_member_offset;
1593 const char *new_name = newsdna->members[new_member->member_index];
1595 oldsdna, old_struct, new_name, &old_member_offset);
1596
1597 if (old_member == nullptr) {
1598 /* No matching member has been found in the old struct. */
1600 return;
1601 }
1602
1603 /* Determine the member category of the old an new members. */
1604 const eStructMemberCategory new_category = get_struct_member_category(newsdna, new_member);
1605 const eStructMemberCategory old_category = get_struct_member_category(oldsdna, old_member);
1606
1607 if (new_category != old_category) {
1608 /* Can only reconstruct the new member based on the old member, when the belong to the same
1609 * category. */
1611 return;
1612 }
1613
1614 const int new_array_length = newsdna->members_array_num[new_member->member_index];
1615 const int old_array_length = oldsdna->members_array_num[old_member->member_index];
1616 const int shared_array_length = std::min(new_array_length, old_array_length);
1617
1618 const char *new_type_name = newsdna->types[new_member->type_index];
1619 const char *old_type_name = oldsdna->types[old_member->type_index];
1620
1621 switch (new_category) {
1623 if (STREQ(new_type_name, old_type_name)) {
1624 const int old_struct_index = DNA_struct_find_index_without_alias(oldsdna, old_type_name);
1625 BLI_assert(old_struct_index != -1);
1626 enum eSDNA_StructCompare compare_flag = eSDNA_StructCompare(
1627 compare_flags[old_struct_index]);
1628 BLI_assert(compare_flag != SDNA_CMP_REMOVED);
1629 if (compare_flag == SDNA_CMP_EQUAL) {
1630 /* The old and new members are identical, just do a #memcpy. */
1631 r_step->type = RECONSTRUCT_STEP_MEMCPY;
1632 r_step->data.memcpy.new_offset = new_member_offset;
1633 r_step->data.memcpy.old_offset = old_member_offset;
1634 r_step->data.memcpy.size = newsdna->types_size[new_member->type_index] *
1635 shared_array_length;
1636 }
1637 else {
1638 const int new_struct_index = DNA_struct_find_index_without_alias(newsdna, new_type_name);
1639 BLI_assert(new_struct_index != -1);
1640
1641 /* The old and new members are different, use recursion to reconstruct the
1642 * nested struct. */
1643 BLI_assert(compare_flag == SDNA_CMP_NOT_EQUAL);
1645 r_step->data.substruct.new_offset = new_member_offset;
1646 r_step->data.substruct.old_offset = old_member_offset;
1647 r_step->data.substruct.array_len = shared_array_length;
1648 r_step->data.substruct.new_struct_index = new_struct_index;
1649 r_step->data.substruct.old_struct_index = old_struct_index;
1650 }
1651 }
1652 else {
1653 /* Cannot match structs that have different names. */
1655 }
1656 break;
1657 }
1659 if (STREQ(new_type_name, old_type_name)) {
1660 /* Primitives with the same name cannot be different, so just do a #memcpy. */
1661 r_step->type = RECONSTRUCT_STEP_MEMCPY;
1662 r_step->data.memcpy.new_offset = new_member_offset;
1663 r_step->data.memcpy.old_offset = old_member_offset;
1664 r_step->data.memcpy.size = newsdna->types_size[new_member->type_index] *
1665 shared_array_length;
1666 }
1667 else {
1668 /* The old and new primitive types are different, cast from the old to new type. */
1670 r_step->data.cast_primitive.array_len = shared_array_length;
1671 r_step->data.cast_primitive.new_offset = new_member_offset;
1672 r_step->data.cast_primitive.old_offset = old_member_offset;
1673 r_step->data.cast_primitive.new_type = eSDNA_Type(new_member->type_index);
1674 r_step->data.cast_primitive.old_type = eSDNA_Type(old_member->type_index);
1675 }
1676 break;
1677 }
1679 if (newsdna->pointer_size == oldsdna->pointer_size) {
1680 /* The pointer size is the same, so just do a #memcpy. */
1681 r_step->type = RECONSTRUCT_STEP_MEMCPY;
1682 r_step->data.memcpy.new_offset = new_member_offset;
1683 r_step->data.memcpy.old_offset = old_member_offset;
1684 r_step->data.memcpy.size = newsdna->pointer_size * shared_array_length;
1685 }
1686 else if (newsdna->pointer_size == 8 && oldsdna->pointer_size == 4) {
1687 /* Need to convert from 32 bit to 64 bit pointers. */
1689 r_step->data.cast_pointer.new_offset = new_member_offset;
1690 r_step->data.cast_pointer.old_offset = old_member_offset;
1691 r_step->data.cast_pointer.array_len = shared_array_length;
1692 }
1693 else if (newsdna->pointer_size == 4 && oldsdna->pointer_size == 8) {
1694 /* Need to convert from 64 bit to 32 bit pointers. */
1696 r_step->data.cast_pointer.new_offset = new_member_offset;
1697 r_step->data.cast_pointer.old_offset = old_member_offset;
1698 r_step->data.cast_pointer.array_len = shared_array_length;
1699 }
1700 else {
1701 BLI_assert_msg(0, "invalid pointer size");
1703 }
1704 break;
1705 }
1706 }
1707}
1708
1710[[maybe_unused]] static void print_reconstruct_step(const ReconstructStep *step,
1711 const SDNA *oldsdna,
1712 const SDNA *newsdna)
1713{
1714 switch (step->type) {
1716 printf("initialize zero");
1717 break;
1718 }
1720 printf("memcpy, size: %d, old offset: %d, new offset: %d",
1721 step->data.memcpy.size,
1722 step->data.memcpy.old_offset,
1723 step->data.memcpy.new_offset);
1724 break;
1725 }
1727 printf(
1728 "cast element, old type: %d ('%s'), new type: %d ('%s'), old offset: %d, new offset: "
1729 "%d, length: %d",
1730 int(step->data.cast_primitive.old_type),
1731 oldsdna->types[step->data.cast_primitive.old_type],
1732 int(step->data.cast_primitive.new_type),
1733 newsdna->types[step->data.cast_primitive.new_type],
1734 step->data.cast_primitive.old_offset,
1735 step->data.cast_primitive.new_offset,
1736 step->data.cast_primitive.array_len);
1737 break;
1738 }
1740 printf("pointer to 32, old offset: %d, new offset: %d, length: %d",
1741 step->data.cast_pointer.old_offset,
1742 step->data.cast_pointer.new_offset,
1743 step->data.cast_pointer.array_len);
1744 break;
1745 }
1747 printf("pointer to 64, old offset: %d, new offset: %d, length: %d",
1748 step->data.cast_pointer.old_offset,
1749 step->data.cast_pointer.new_offset,
1750 step->data.cast_pointer.array_len);
1751 break;
1752 }
1754 printf(
1755 "substruct, old offset: %d, new offset: %d, new struct: %d ('%s', size per struct: %d), "
1756 "length: %d",
1757 step->data.substruct.old_offset,
1758 step->data.substruct.new_offset,
1759 step->data.substruct.new_struct_index,
1760 newsdna->types[newsdna->structs[step->data.substruct.new_struct_index]->type_index],
1761 newsdna->types_size[newsdna->structs[step->data.substruct.new_struct_index]->type_index],
1762 step->data.substruct.array_len);
1763 break;
1764 }
1765 }
1766}
1767
1773 const SDNA *newsdna,
1774 const char *compare_flags,
1775 const SDNA_Struct *old_struct,
1776 const SDNA_Struct *new_struct)
1777{
1778 ReconstructStep *steps = MEM_calloc_arrayN<ReconstructStep>(new_struct->members_num, __func__);
1779
1780 int new_member_offset = 0;
1781 for (int new_member_index = 0; new_member_index < new_struct->members_num; new_member_index++) {
1782 const SDNA_StructMember *new_member = &new_struct->members[new_member_index];
1784 newsdna,
1785 compare_flags,
1786 old_struct,
1787 new_member,
1788 new_member_offset,
1789 &steps[new_member_index]);
1790 new_member_offset += get_member_size_in_bytes(newsdna, new_member);
1791 }
1792
1793 return steps;
1794}
1795
1797static int compress_reconstruct_steps(ReconstructStep *steps, const int old_step_count)
1798{
1799 int new_step_count = 0;
1800 for (int a = 0; a < old_step_count; a++) {
1801 ReconstructStep *step = &steps[a];
1802 switch (step->type) {
1804 /* These steps are simply removed. */
1805 break;
1807 if (new_step_count > 0) {
1808 /* Try to merge this memcpy step with the previous one. */
1809 ReconstructStep *prev_step = &steps[new_step_count - 1];
1810 if (prev_step->type == RECONSTRUCT_STEP_MEMCPY) {
1811 /* Check if there are no bytes between the blocks to copy. */
1812 if (prev_step->data.memcpy.old_offset + prev_step->data.memcpy.size ==
1813 step->data.memcpy.old_offset &&
1814 prev_step->data.memcpy.new_offset + prev_step->data.memcpy.size ==
1815 step->data.memcpy.new_offset)
1816 {
1817 prev_step->data.memcpy.size += step->data.memcpy.size;
1818 break;
1819 }
1820 }
1821 }
1822 steps[new_step_count] = *step;
1823 new_step_count++;
1824 break;
1829 /* These steps are not changed at all for now. It should be possible to merge consecutive
1830 * steps of the same type, but it is not really worth it. */
1831 steps[new_step_count] = *step;
1832 new_step_count++;
1833 break;
1834 }
1835 }
1836 return new_step_count;
1837}
1838
1840 const SDNA *newsdna,
1841 const char *compare_flags)
1842{
1843 DNA_ReconstructInfo *reconstruct_info = MEM_callocN<DNA_ReconstructInfo>(__func__);
1844 reconstruct_info->oldsdna = oldsdna;
1845 reconstruct_info->newsdna = newsdna;
1846 reconstruct_info->compare_flags = compare_flags;
1847 reconstruct_info->step_counts = MEM_malloc_arrayN<int>(size_t(newsdna->structs_num), __func__);
1848 reconstruct_info->steps = MEM_malloc_arrayN<ReconstructStep *>(size_t(newsdna->structs_num),
1849 __func__);
1850
1851 /* Generate reconstruct steps for all structs. */
1852 for (int new_struct_index = 0; new_struct_index < newsdna->structs_num; new_struct_index++) {
1853 const SDNA_Struct *new_struct = newsdna->structs[new_struct_index];
1854 const char *new_struct_name = newsdna->types[new_struct->type_index];
1855 const int old_struct_index = DNA_struct_find_index_without_alias(oldsdna, new_struct_name);
1856 if (old_struct_index < 0) {
1857 reconstruct_info->steps[new_struct_index] = nullptr;
1858 reconstruct_info->step_counts[new_struct_index] = 0;
1859 continue;
1860 }
1861 const SDNA_Struct *old_struct = oldsdna->structs[old_struct_index];
1863 oldsdna, newsdna, compare_flags, old_struct, new_struct);
1864
1865 /* Comment the line below to skip the compression for debugging purposes. */
1866 const int steps_len = compress_reconstruct_steps(steps, new_struct->members_num);
1867
1868 reconstruct_info->steps[new_struct_index] = steps;
1869 reconstruct_info->step_counts[new_struct_index] = steps_len;
1870
1871/* This is useful when debugging the reconstruct steps. */
1872#if 0
1873 printf("%s: \n", new_struct_name);
1874 for (int a = 0; a < steps_len; a++) {
1875 printf(" ");
1876 print_reconstruct_step(&steps[a], oldsdna, newsdna);
1877 printf("\n");
1878 }
1879#endif
1880 }
1881
1882 return reconstruct_info;
1883}
1884
1886{
1887 for (int new_struct_index = 0; new_struct_index < reconstruct_info->newsdna->structs_num;
1888 new_struct_index++)
1889 {
1890 if (reconstruct_info->steps[new_struct_index] != nullptr) {
1891 MEM_freeN(reconstruct_info->steps[new_struct_index]);
1892 }
1893 }
1894 MEM_freeN(reconstruct_info->steps);
1895 MEM_freeN(reconstruct_info->step_counts);
1896 MEM_freeN(reconstruct_info);
1897}
1898
1900 const char *stype,
1901 const char *vartype,
1902 const char *name)
1903{
1904 const int struct_index = DNA_struct_find_index_without_alias(sdna, stype);
1905 BLI_assert(struct_index != -1);
1906 const SDNA_Struct *const struct_info = sdna->structs[struct_index];
1907 return elem_offset_without_alias(sdna, vartype, name, struct_info);
1908}
1909
1911 const char *stype,
1912 const char *vartype,
1913 const char *name)
1914{
1915 const int struct_index = DNA_struct_find_with_alias(sdna, stype);
1916 BLI_assert(struct_index != -1);
1917 const SDNA_Struct *const struct_info = sdna->structs[struct_index];
1918 return elem_offset_with_alias(sdna, vartype, name, struct_info);
1919}
1920
1921bool DNA_struct_exists_without_alias(const SDNA *sdna, const char *stype)
1922{
1923 return DNA_struct_find_index_without_alias(sdna, stype) != -1;
1924}
1925
1927 const char *stype,
1928 const char *vartype,
1929 const char *name)
1930{
1931 const int struct_index = DNA_struct_find_index_without_alias(sdna, stype);
1932
1933 if (struct_index != -1) {
1934 const SDNA_Struct *const struct_info = sdna->structs[struct_index];
1935 const bool found = elem_exists_without_alias(sdna, vartype, name, struct_info);
1936
1937 if (found) {
1938 return true;
1939 }
1940 }
1941 return false;
1942}
1943
1945 const char *stype,
1946 const char *vartype,
1947 const char *name)
1948{
1949 const int SDNAnr = DNA_struct_find_with_alias(sdna, stype);
1950
1951 if (SDNAnr != -1) {
1952 const SDNA_Struct *const spo = sdna->structs[SDNAnr];
1953 const bool found = elem_exists_with_alias(sdna, vartype, name, spo);
1954
1955 if (found) {
1956 return true;
1957 }
1958 }
1959 return false;
1960}
1961
1963{
1964 /* should contain all enum types */
1965 switch (elem_nr) {
1966 case SDNA_TYPE_CHAR:
1967 case SDNA_TYPE_UCHAR:
1968 case SDNA_TYPE_INT8:
1969 return 1;
1970 case SDNA_TYPE_SHORT:
1971 case SDNA_TYPE_USHORT:
1972 return 2;
1973 case SDNA_TYPE_INT:
1974 case SDNA_TYPE_FLOAT:
1975 return 4;
1976 case SDNA_TYPE_DOUBLE:
1977 case SDNA_TYPE_INT64:
1978 case SDNA_TYPE_UINT64:
1979 return 8;
1980 case SDNA_TYPE_RAW_DATA:
1981 BLI_assert_msg(false, "Operations on the size of SDNA_TYPE_RAW_DATA is not supported");
1982 return 0;
1983 }
1984
1985 /* weak */
1986 return 8;
1987}
1988
1989int DNA_struct_alignment(const SDNA *sdna, const int struct_index)
1990{
1991 return sdna->types_alignment[struct_index];
1992}
1993
1994const char *DNA_struct_identifier(SDNA *sdna, const int struct_index)
1995{
1997 const SDNA_Struct *struct_info = sdna->structs[struct_index];
1998 return sdna->alias.types[struct_info->type_index];
1999}
2000
2001/* -------------------------------------------------------------------- */
2004
2005static bool DNA_sdna_patch_struct(SDNA *sdna, const int struct_index, const char *new_type_name)
2006{
2008 const SDNA_Struct *struct_info = sdna->structs[struct_index];
2009#ifdef WITH_DNA_GHASH
2011 sdna->types_to_structs_map, (void *)sdna->types[struct_info->type_index], nullptr, nullptr);
2013 sdna->types_to_structs_map, (void *)new_type_name, POINTER_FROM_INT(struct_index));
2014#endif
2015 sdna->types[struct_info->type_index] = new_type_name;
2016 return true;
2017}
2019 const char *old_type_name,
2020 const char *new_type_name)
2021{
2022 const int struct_index = DNA_struct_find_index_without_alias(sdna, old_type_name);
2023 if (struct_index != -1) {
2024 return DNA_sdna_patch_struct(sdna, struct_index, new_type_name);
2025 }
2026 return false;
2027}
2028
2029/* Make public if called often with same struct (avoid duplicate lookups). */
2031 const int struct_index,
2032 const char *old_member_name,
2033 const char *new_member_name)
2034{
2035 /* These names aren't handled here (it's not used).
2036 * Ensure they are never used or we get out of sync arrays. */
2037 BLI_assert(sdna->alias.members == nullptr);
2038 const int old_member_name_len = strlen(old_member_name);
2039 const int new_member_name_len = strlen(new_member_name);
2040 BLI_assert(new_member_name != nullptr);
2041 SDNA_Struct *struct_info = sdna->structs[struct_index];
2042 for (int struct_member_index = struct_info->members_num; struct_member_index > 0;
2043 struct_member_index--)
2044 {
2045 SDNA_StructMember *member_info = &struct_info->members[struct_member_index];
2046 const char *old_member_name_full = sdna->members[member_info->member_index];
2047 /* Start & end offsets in #old_member_full. */
2048 uint old_member_name_full_offset_start;
2049 if (DNA_member_id_match(old_member_name,
2050 old_member_name_len,
2051 old_member_name_full,
2052 &old_member_name_full_offset_start))
2053 {
2054 if (sdna->mem_arena == nullptr) {
2056 }
2057 const char *new_member_name_full = DNA_member_id_rename(sdna->mem_arena,
2058 old_member_name,
2059 old_member_name_len,
2060 new_member_name,
2061 new_member_name_len,
2062 old_member_name_full,
2063 strlen(old_member_name_full),
2064 old_member_name_full_offset_start);
2065
2066 if (sdna->members_num == sdna->members_num_alloc) {
2067 sdna->members_num_alloc += 64;
2068 sdna->members = static_cast<const char **>(MEM_recallocN(
2069 (void *)sdna->members, sizeof(*sdna->members) * sdna->members_num_alloc));
2070 sdna->members_array_num = static_cast<short int *>(
2071 MEM_recallocN((void *)sdna->members_array_num,
2072 sizeof(*sdna->members_array_num) * sdna->members_num_alloc));
2073 }
2074 const short old_member_index = member_info->member_index;
2075 member_info->member_index = sdna->members_num++;
2076 sdna->members[member_info->member_index] = new_member_name_full;
2077 sdna->members_array_num[member_info->member_index] =
2078 sdna->members_array_num[old_member_index];
2079
2080 return true;
2081 }
2082 }
2083 return false;
2084}
2086 const char *type_name,
2087 const char *old_member_name,
2088 const char *new_member_name)
2089{
2090 const int struct_index = DNA_struct_find_index_without_alias(sdna, type_name);
2091 if (struct_index != -1) {
2092 return DNA_sdna_patch_struct_member(sdna, struct_index, old_member_name, new_member_name);
2093 }
2094 return false;
2095}
2096
2098
2099/* -------------------------------------------------------------------- */
2104
2111static void sdna_expand_names(SDNA *sdna)
2112{
2113 int names_expand_len = 0;
2114 for (int struct_index = 0; struct_index < sdna->structs_num; struct_index++) {
2115 const SDNA_Struct *struct_old = sdna->structs[struct_index];
2116 names_expand_len += struct_old->members_num;
2117 }
2118 const char **names_expand = MEM_malloc_arrayN<const char *>(size_t(names_expand_len), __func__);
2119 short *names_array_len_expand = MEM_malloc_arrayN<short>(size_t(names_expand_len), __func__);
2120
2121 int names_expand_index = 0;
2122 for (int struct_index = 0; struct_index < sdna->structs_num; struct_index++) {
2123 /* We can't edit this memory 'sdna->structs' points to (read-only `datatoc` file). */
2124 const SDNA_Struct *struct_old = sdna->structs[struct_index];
2125
2126 const int array_size = sizeof(short) * 2 + sizeof(SDNA_StructMember) * struct_old->members_num;
2127 SDNA_Struct *struct_new = static_cast<SDNA_Struct *>(
2128 BLI_memarena_alloc(sdna->mem_arena, array_size));
2129 memcpy(struct_new, struct_old, array_size);
2130 sdna->structs[struct_index] = struct_new;
2131
2132 for (int i = 0; i < struct_old->members_num; i++) {
2133 const SDNA_StructMember *member_old = &struct_old->members[i];
2134 SDNA_StructMember *member_new = &struct_new->members[i];
2135
2136 names_expand[names_expand_index] = sdna->members[member_old->member_index];
2137 names_array_len_expand[names_expand_index] =
2138 sdna->members_array_num[member_old->member_index];
2139
2140 BLI_assert(names_expand_index < SHRT_MAX);
2141 member_new->member_index = names_expand_index;
2142 names_expand_index++;
2143 }
2144 }
2145 MEM_freeN(sdna->members);
2146 sdna->members = names_expand;
2147
2149 sdna->members_array_num = names_array_len_expand;
2150
2151 sdna->members_num = names_expand_len;
2152}
2153
2155 GHash *elem_map_alias_from_static,
2156 const char *struct_name_static,
2157 const char *elem_static_full)
2158{
2159 const int elem_static_full_len = strlen(elem_static_full);
2160 char *elem_static = static_cast<char *>(alloca(elem_static_full_len + 1));
2161 const int elem_static_len = DNA_member_id_strip_copy(elem_static, elem_static_full);
2162 const char *str_pair[2] = {struct_name_static, elem_static};
2163 const char *elem_alias = static_cast<const char *>(
2164 BLI_ghash_lookup(elem_map_alias_from_static, str_pair));
2165 if (elem_alias) {
2166 return DNA_member_id_rename(sdna->mem_arena,
2167 elem_static,
2168 elem_static_len,
2169 elem_alias,
2170 strlen(elem_alias),
2171 elem_static_full,
2172 elem_static_full_len,
2173 DNA_member_id_offset_start(elem_static_full));
2174 }
2175 return nullptr;
2176}
2177
2179{
2180 if (sdna->alias.members && sdna->alias.types) {
2181 return;
2182 }
2183
2184 /* We may want this to be optional later. */
2185 const bool use_legacy_hack = true;
2186
2187 if (sdna->mem_arena == nullptr) {
2189 }
2190
2191 GHash *type_map_alias_from_static;
2192 GHash *member_map_alias_from_static;
2193
2195 DNA_RENAME_ALIAS_FROM_STATIC, &type_map_alias_from_static, &member_map_alias_from_static);
2196
2197 if (sdna->alias.types == nullptr) {
2198 sdna->alias.types = MEM_malloc_arrayN<const char *>(size_t(sdna->types_num), __func__);
2199 for (int type_index = 0; type_index < sdna->types_num; type_index++) {
2200 const char *type_name_static = sdna->types[type_index];
2201
2202 if (use_legacy_hack) {
2203 type_name_static = DNA_struct_rename_legacy_hack_alias_from_static(type_name_static);
2204 }
2205
2206 sdna->alias.types[type_index] = static_cast<const char *>(BLI_ghash_lookup_default(
2207 type_map_alias_from_static, type_name_static, (void *)type_name_static));
2208 }
2209 }
2210
2211 if (sdna->alias.members == nullptr) {
2212 sdna_expand_names(sdna);
2213 sdna->alias.members = MEM_malloc_arrayN<const char *>(size_t(sdna->members_num), __func__);
2214 for (int struct_index = 0; struct_index < sdna->structs_num; struct_index++) {
2215 const SDNA_Struct *struct_info = sdna->structs[struct_index];
2216 const char *struct_name_static = sdna->types[struct_info->type_index];
2217
2218 if (use_legacy_hack) {
2219 struct_name_static = DNA_struct_rename_legacy_hack_alias_from_static(struct_name_static);
2220 }
2221
2222 for (int a = 0; a < struct_info->members_num; a++) {
2223 const SDNA_StructMember *member = &struct_info->members[a];
2224 const char *member_alias_full = dna_sdna_alias_from_static_elem_full(
2225 sdna,
2226 member_map_alias_from_static,
2227 struct_name_static,
2228 sdna->members[member->member_index]);
2229 if (member_alias_full != nullptr) {
2230 sdna->alias.members[member->member_index] = member_alias_full;
2231 }
2232 else {
2233 sdna->alias.members[member->member_index] = sdna->members[member->member_index];
2234 }
2235 }
2236 }
2237 }
2238 BLI_ghash_free(type_map_alias_from_static, nullptr, nullptr);
2239 BLI_ghash_free(member_map_alias_from_static, MEM_freeN, nullptr);
2240}
2241
2243{
2244 if (sdna->alias.types_to_structs_map) {
2245 return;
2246 }
2247
2249#ifdef WITH_DNA_GHASH
2250 /* create a ghash lookup to speed up */
2251 GHash *type_to_struct_index_map = BLI_ghash_str_new_ex(__func__, sdna->structs_num);
2252 for (intptr_t struct_index = 0; struct_index < sdna->structs_num; struct_index++) {
2253 const SDNA_Struct *struct_info = sdna->structs[struct_index];
2254 BLI_ghash_insert(type_to_struct_index_map,
2255 (void *)sdna->alias.types[struct_info->type_index],
2256 POINTER_FROM_INT(struct_index));
2257 }
2258 sdna->alias.types_to_structs_map = type_to_struct_index_map;
2259#else
2260 UNUSED_VARS(sdna);
2261#endif
2262}
2263
2265
2266/* -------------------------------------------------------------------- */
2270
2271namespace blender::dna {
2272
2273static void print_struct_array_recursive(const SDNA &sdna,
2274 const SDNA_Struct &sdna_struct,
2275 const void *initial_data,
2276 const int64_t element_num,
2277 const int indent,
2278 fmt::appender &dst);
2279static void print_single_struct_recursive(const SDNA &sdna,
2280 const SDNA_Struct &sdna_struct,
2281 const void *initial_data,
2282 const int indent,
2283 fmt::appender &dst);
2284
2288static bool char_array_startswith_simple_name(const char *data, const int array_len)
2289{
2290 const int string_length = strnlen(data, array_len);
2291 if (string_length == array_len) {
2292 return false;
2293 }
2294 for (const int i : IndexRange(string_length)) {
2295 const uchar c = data[i];
2296 /* This is only a very simple check and does not cover more complex cases with multi-byte UTF8
2297 * characters. It's only a heuristic anyway, making a wrong decision here just means that the
2298 * data will be printed differently. */
2299 if (!std::isprint(c)) {
2300 return false;
2301 }
2302 }
2303 return true;
2304}
2305
2306static void print_struct_array_recursive(const SDNA &sdna,
2307 const SDNA_Struct &sdna_struct,
2308 const void *data,
2309 const int64_t element_num,
2310 const int indent,
2311 fmt::appender &dst)
2312{
2313 if (element_num == 1) {
2314 print_single_struct_recursive(sdna, sdna_struct, data, indent, dst);
2315 return;
2316 }
2317
2318 const char *struct_name = sdna.types[sdna_struct.type_index];
2319 const int64_t struct_size = sdna.types_size[sdna_struct.type_index];
2320 for (const int64_t i : IndexRange(element_num)) {
2321 const void *element_data = POINTER_OFFSET(data, i * struct_size);
2322 fmt::format_to(dst, "{:{}}{}: <{}>\n", "", indent, i, struct_name);
2323 print_single_struct_recursive(sdna, sdna_struct, element_data, indent + 2, dst);
2324 }
2325}
2326
2327static void print_single_struct_recursive(const SDNA &sdna,
2328 const SDNA_Struct &sdna_struct,
2329 const void *initial_data,
2330 const int indent,
2331 fmt::appender &dst)
2332{
2333 using namespace blender;
2334 const void *data = initial_data;
2335
2336 for (const int member_i : IndexRange(sdna_struct.members_num)) {
2337 const SDNA_StructMember &member = sdna_struct.members[member_i];
2338 const char *member_type_name = sdna.types[member.type_index];
2339 const char *member_name = sdna.members[member.member_index];
2340 const eStructMemberCategory member_category = get_struct_member_category(&sdna, &member);
2341 const int member_array_len = sdna.members_array_num[member.member_index];
2342
2343 fmt::format_to(dst, "{:{}}{} {}:", "", indent, member_type_name, member_name);
2344
2345 if (member_category == STRUCT_MEMBER_CATEGORY_PRIMITIVE &&
2346 member.type_index == SDNA_TYPE_CHAR && member_array_len > 1)
2347 {
2348 const char *str_data = static_cast<const char *>(data);
2349 fmt::format_to(dst, " ");
2350 if (char_array_startswith_simple_name(str_data, member_array_len)) {
2351 fmt::format_to(dst, "'{}'", str_data);
2352 }
2353 else {
2354 for (const int i : IndexRange(member_array_len)) {
2355 fmt::format_to(dst, "{} ", int(str_data[i]));
2356 }
2357 }
2358 fmt::format_to(dst, "\n");
2359 }
2360 else {
2361 switch (member_category) {
2363 fmt::format_to(dst, "\n");
2364 const int substruct_i = DNA_struct_find_index_without_alias(&sdna, member_type_name);
2365 const SDNA_Struct &sub_sdna_struct = *sdna.structs[substruct_i];
2367 sdna, sub_sdna_struct, data, member_array_len, indent + 2, dst);
2368 break;
2369 }
2371 fmt::format_to(dst, " ");
2372 const int type_size = sdna.types_size[member.type_index];
2373 const eSDNA_Type type = eSDNA_Type(member.type_index);
2374 for ([[maybe_unused]] const int elem_i : IndexRange(member_array_len)) {
2375 const void *current_data = POINTER_OFFSET(data, elem_i * type_size);
2376 switch (type) {
2377 case SDNA_TYPE_CHAR: {
2378 const char value = *reinterpret_cast<const char *>(current_data);
2379 fmt::format_to(dst, "{}", int(value));
2380 break;
2381 }
2382 case SDNA_TYPE_UCHAR: {
2383 const uchar value = *reinterpret_cast<const uchar *>(current_data);
2384 fmt::format_to(dst, "{}", int(value));
2385 break;
2386 }
2387 case SDNA_TYPE_INT8: {
2388 fmt::format_to(dst, "{}", *reinterpret_cast<const int8_t *>(current_data));
2389 break;
2390 }
2391 case SDNA_TYPE_SHORT: {
2392 fmt::format_to(dst, "{}", *reinterpret_cast<const short *>(current_data));
2393 break;
2394 }
2395 case SDNA_TYPE_USHORT: {
2396 fmt::format_to(dst, "{}", *reinterpret_cast<const ushort *>(current_data));
2397 break;
2398 }
2399 case SDNA_TYPE_INT: {
2400 fmt::format_to(dst, "{}", *reinterpret_cast<const int *>(current_data));
2401 break;
2402 }
2403 case SDNA_TYPE_FLOAT: {
2404 fmt::format_to(dst, "{}", *reinterpret_cast<const float *>(current_data));
2405 break;
2406 }
2407 case SDNA_TYPE_DOUBLE: {
2408 fmt::format_to(dst, "{}", *reinterpret_cast<const double *>(current_data));
2409 break;
2410 }
2411 case SDNA_TYPE_INT64: {
2412 fmt::format_to(dst, "{}", *reinterpret_cast<const int64_t *>(current_data));
2413 break;
2414 }
2415 case SDNA_TYPE_UINT64: {
2416 fmt::format_to(dst, "{}", *reinterpret_cast<const uint64_t *>(current_data));
2417 break;
2418 }
2419 case SDNA_TYPE_RAW_DATA: {
2421 break;
2422 }
2423 }
2424 fmt::format_to(dst, " ");
2425 }
2426 fmt::format_to(dst, "\n");
2427 break;
2428 }
2430 for ([[maybe_unused]] const int elem_i : IndexRange(member_array_len)) {
2431 const void *current_data = POINTER_OFFSET(data, sdna.pointer_size * elem_i);
2432 fmt::format_to(dst, " {}", *reinterpret_cast<const void *const *>(current_data));
2433 }
2434 fmt::format_to(dst, "\n");
2435 break;
2436 }
2437 }
2438 }
2439 const int member_size = get_member_size_in_bytes(&sdna, &member);
2440 data = POINTER_OFFSET(data, member_size);
2441 }
2442}
2443
2445 const int struct_id,
2446 const void *initial_data,
2447 const void *address,
2448 const int64_t element_num,
2449 std::ostream &stream)
2450{
2451 const SDNA_Struct &sdna_struct = *sdna.structs[struct_id];
2452
2453 fmt::memory_buffer buf;
2454 fmt::appender dst{buf};
2455
2456 const char *struct_name = sdna.types[sdna_struct.type_index];
2457 fmt::format_to(dst, "<{}> {}x at {}\n", struct_name, element_num, address);
2458
2459 print_struct_array_recursive(sdna, sdna_struct, initial_data, element_num, 2, dst);
2460 stream << fmt::to_string(buf);
2461}
2462
2463void print_struct_by_id(const int struct_id, const void *data)
2464{
2465 const SDNA &sdna = *DNA_sdna_current_get();
2466 print_structs_at_address(sdna, struct_id, data, data, 1, std::cout);
2467}
2468
2469} // namespace blender::dna
2470
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_STATIC_ASSERT(a, msg)
Definition BLI_assert.h:83
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define BLI_STATIC_ASSERT_ALIGN(st, align)
Definition BLI_assert.h:86
#define BLI_INLINE
BLI_INLINE void BLI_endian_switch_int32(int *val) ATTR_NONNULL(1)
void BLI_endian_switch_int32_array(int *val, int size) ATTR_NONNULL(1)
void BLI_endian_switch_uint64_array(uint64_t *val, int size) ATTR_NONNULL(1)
void BLI_endian_switch_int64_array(int64_t *val, int size) ATTR_NONNULL(1)
void BLI_endian_switch_int16_array(short *val, int size) ATTR_NONNULL(1)
BLI_INLINE void BLI_endian_switch_int16(short *val) ATTR_NONNULL(1)
void * BLI_ghash_lookup_default(const GHash *gh, const void *key, void *val_default) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:738
void ** BLI_ghash_lookup_p(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:745
GHash * BLI_ghash_str_new_ex(const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
bool BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:787
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:731
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:707
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:860
#define BLI_MEMARENA_STD_BUFSIZE
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
ATTR_WARN_UNUSED_RESULT const size_t num
unsigned char uchar
unsigned int uint
unsigned short ushort
#define UNUSED_VARS(...)
#define POINTER_FROM_INT(i)
#define POINTER_AS_INT(i)
#define UNLIKELY(x)
#define ELEM(...)
#define POINTER_OFFSET(v, ofs)
#define STREQ(a, b)
blenloader genfile private function prototypes
const char * DNA_struct_get_compareflags(const struct SDNA *oldsdna, const struct SDNA *newsdna)
void DNA_sdna_free(struct SDNA *sdna)
eSDNA_Type
Definition DNA_genfile.h:32
@ SDNA_TYPE_CHAR
Definition DNA_genfile.h:33
@ SDNA_TYPE_RAW_DATA
Definition DNA_genfile.h:54
@ SDNA_TYPE_INT
Definition DNA_genfile.h:37
@ SDNA_TYPE_UINT64
Definition DNA_genfile.h:46
@ SDNA_TYPE_DOUBLE
Definition DNA_genfile.h:41
@ SDNA_TYPE_SHORT
Definition DNA_genfile.h:35
@ SDNA_TYPE_UCHAR
Definition DNA_genfile.h:34
@ SDNA_TYPE_INT8
Definition DNA_genfile.h:47
@ SDNA_TYPE_INT64
Definition DNA_genfile.h:45
@ SDNA_TYPE_FLOAT
Definition DNA_genfile.h:40
@ SDNA_TYPE_USHORT
Definition DNA_genfile.h:36
const struct SDNA * DNA_sdna_current_get(void)
int DNA_struct_find_with_alias(const struct SDNA *sdna, const char *str)
const unsigned char DNAstr[]
int DNA_elem_type_size(eSDNA_Type elem_nr)
int DNA_struct_member_offset_by_name_with_alias(const struct SDNA *sdna, const char *stype, const char *vartype, const char *name)
int DNA_struct_size(const struct SDNA *sdna, int struct_index)
const char * DNA_struct_identifier(struct SDNA *sdna, int struct_index)
void DNA_sdna_current_free(void)
bool DNA_sdna_patch_struct_by_name(struct SDNA *sdna, const char *old_type_name, const char *new_type_name)
void DNA_sdna_current_init(void)
void * DNA_struct_reconstruct(const struct DNA_ReconstructInfo *reconstruct_info, int old_struct_index, int blocks, const void *old_blocks, const char *alloc_name)
bool DNA_struct_exists_without_alias(const struct SDNA *sdna, const char *stype)
struct DNA_ReconstructInfo * DNA_reconstruct_info_create(const struct SDNA *oldsdna, const struct SDNA *newsdna, const char *compare_flags)
void DNA_sdna_alias_data_ensure(struct SDNA *sdna)
bool DNA_struct_member_exists_with_alias(const struct SDNA *sdna, const char *stype, const char *vartype, const char *name)
struct SDNA * DNA_sdna_from_data(const void *data, int data_len, bool do_endian_swap, bool data_alloc, bool do_alias, const char **r_error_message)
int DNA_struct_member_size(const struct SDNA *sdna, short type, short member_index)
int DNA_struct_find_index_with_alias_ex(const struct SDNA *sdna, const char *str, unsigned int *struct_index_last)
void DNA_struct_switch_endian(const struct SDNA *sdna, int struct_index, char *data)
bool DNA_struct_member_exists_without_alias(const struct SDNA *sdna, const char *stype, const char *vartype, const char *name)
const int DNAlen
int DNA_struct_find_index_without_alias(const struct SDNA *sdna, const char *str)
void DNA_reconstruct_info_free(struct DNA_ReconstructInfo *reconstruct_info)
bool DNA_sdna_patch_struct_member_by_name(struct SDNA *sdna, const char *type_name, const char *old_member_name, const char *new_member_name)
int DNA_struct_alignment(const struct SDNA *sdna, int struct_index)
int DNA_struct_find_index_without_alias_ex(const struct SDNA *sdna, const char *str, unsigned int *struct_index_last)
eSDNA_StructCompare
Definition DNA_genfile.h:60
@ SDNA_CMP_EQUAL
Definition DNA_genfile.h:66
@ SDNA_CMP_REMOVED
Definition DNA_genfile.h:63
@ SDNA_CMP_UNKNOWN
Definition DNA_genfile.h:71
@ SDNA_CMP_NOT_EQUAL
Definition DNA_genfile.h:69
void DNA_sdna_alias_data_ensure_structs_map(struct SDNA *sdna)
bool DNA_struct_exists_with_alias(const struct SDNA *sdna, const char *str)
int DNA_struct_member_offset_by_name_without_alias(const struct SDNA *sdna, const char *stype, const char *vartype, const char *name)
struct SDNA SDNA
#define SDNA_RAW_DATA_STRUCT_INDEX
struct SDNA_Struct SDNA_Struct
Read Guarded memory(de)allocation.
#define MAKE_ID(a, b, c, d)
Definition blendthumb.hh:53
BMesh const char void * data
long long int int64_t
unsigned long long int uint64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
constexpr IndexRange()=default
void reserve(const int64_t n)
Definition BLI_set.hh:637
bool add(const Key &key)
Definition BLI_set.hh:248
static bool init_structDNA(SDNA *sdna, bool do_endian_swap, const char **r_error_message)
static void print_reconstruct_step(const ReconstructStep *step, const SDNA *oldsdna, const SDNA *newsdna)
static void reconstruct_struct(const DNA_ReconstructInfo *reconstruct_info, const int new_struct_index, const char *old_block, char *new_block)
static void reconstruct_structs(const DNA_ReconstructInfo *reconstruct_info, const int blocks, const int old_struct_index, const int new_struct_index, const char *old_blocks, char *new_blocks)
static bool sdna_struct_size_check(const SDNA *sdna, const SDNA_Struct *struct_info, const int64_t voided_members_size, const char **r_error_message)
eReconstructStepType
@ RECONSTRUCT_STEP_CAST_POINTER_TO_32
@ RECONSTRUCT_STEP_CAST_POINTER_TO_64
@ RECONSTRUCT_STEP_INIT_ZERO
@ RECONSTRUCT_STEP_SUBSTRUCT
@ RECONSTRUCT_STEP_CAST_PRIMITIVE
@ RECONSTRUCT_STEP_MEMCPY
static bool ispointer(const char *name)
static bool DNA_sdna_patch_struct_member(SDNA *sdna, const int struct_index, const char *old_member_name, const char *new_member_name)
static bool elem_exists_impl(const char **types, const char **names, const char *type, const char *name, const SDNA_Struct *old)
static bool DNA_sdna_patch_struct(SDNA *sdna, const int struct_index, const char *new_type_name)
static int elem_offset_with_alias(const SDNA *sdna, const char *type, const char *name, const SDNA_Struct *old)
static void cast_primitive_type(const eSDNA_Type old_type, const eSDNA_Type new_type, const int array_len, const char *old_data, char *new_data)
BLI_INLINE const char * pad_up_4(const char *ptr)
static void init_reconstruct_step_for_member(const SDNA *oldsdna, const SDNA *newsdna, const char *compare_flags, const SDNA_Struct *old_struct, const SDNA_StructMember *new_member, const int new_member_offset, ReconstructStep *r_step)
static void cast_pointer_64_to_32(const int array_len, const uint64_t *old_data, uint32_t *new_data)
static void sdna_expand_names(SDNA *sdna)
static int get_member_size_in_bytes(const SDNA *sdna, const SDNA_StructMember *member)
static int dna_struct_find_index_ex_impl(const char **types, const int, SDNA_Struct **const structs, const int structs_num, const char *str, uint *struct_index_last)
static const char * dna_sdna_alias_from_static_elem_full(SDNA *sdna, GHash *elem_map_alias_from_static, const char *struct_name_static, const char *elem_static_full)
static bool elem_exists_without_alias(const SDNA *sdna, const char *type, const char *name, const SDNA_Struct *old)
static eStructMemberCategory get_struct_member_category(const SDNA *sdna, const SDNA_StructMember *member)
static bool elem_exists_with_alias(const SDNA *sdna, const char *type, const char *name, const SDNA_Struct *old)
static void set_compare_flags_for_struct(const SDNA *oldsdna, const SDNA *newsdna, char *compare_flags, const int old_struct_index)
static const SDNA_StructMember * find_member_with_matching_name(const SDNA *sdna, const SDNA_Struct *struct_info, const char *name, int *r_offset)
static ReconstructStep * create_reconstruct_steps_for_struct(const SDNA *oldsdna, const SDNA *newsdna, const char *compare_flags, const SDNA_Struct *old_struct, const SDNA_Struct *new_struct)
static int elem_offset_without_alias(const SDNA *sdna, const char *type, const char *name, const SDNA_Struct *old)
static int elem_offset_impl(const SDNA *sdna, const char **types, const char **names, const char *type, const char *name, const SDNA_Struct *old)
eStructMemberCategory
@ STRUCT_MEMBER_CATEGORY_STRUCT
@ STRUCT_MEMBER_CATEGORY_POINTER
@ STRUCT_MEMBER_CATEGORY_PRIMITIVE
static void cast_pointer_32_to_64(const int array_len, const uint32_t *old_data, uint64_t *new_data)
static bool elem_streq(const char *name, const char *oname)
static const char * sdna_data_pointer_read_string(const char *&data, const void *const data_pointer_end, const char **r_error_message)
static SDNA * g_sdna
static T sdna_data_pointer_read(T &data, const int64_t num, const void *const data_pointer_end, const char **r_error_message)
static int compress_reconstruct_steps(ReconstructStep *steps, const int old_step_count)
static bool sdna_data_validate_array_size(const int64_t size, T data_pointer_current, const void *const data_pointer_end, const char **r_error_message)
const char * DNA_struct_rename_legacy_hack_static_from_alias(const char *name)
Definition dna_utils.cc:292
const char * DNA_struct_rename_legacy_hack_alias_from_static(const char *name)
Definition dna_utils.cc:308
int DNA_member_array_num(const char *str)
Definition dna_utils.cc:29
bool DNA_member_id_match(const char *member_id, const int member_id_len, const char *member_full, uint *r_member_full_offset)
Definition dna_utils.cc:116
uint DNA_member_id_offset_start(const char *member_full)
Definition dna_utils.cc:74
uint DNA_member_id_strip_copy(char *member_id_dst, const char *member_full_src)
Definition dna_utils.cc:96
char * DNA_member_id_rename(MemArena *mem_arena, const char *member_id_src, const int member_id_src_len, const char *member_id_dst, const int member_id_dst_len, const char *member_full_src, const int member_full_src_len, const uint member_full_src_offset_len)
Definition dna_utils.cc:134
@ DNA_RENAME_ALIAS_FROM_STATIC
Definition dna_utils.h:91
void DNA_alias_maps(enum eDNA_RenameDir version_dir, struct GHash **r_type_map, struct GHash **r_member_map)
#define str(s)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define printf(...)
#define MEM_recallocN(vmemh, len)
#define MEM_SAFE_FREE(v)
#define UINT_MAX
Definition hash_md5.cc:44
void * MEM_mallocN(size_t len, const char *str)
Definition mallocn.cc:128
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void *(*) MEM_calloc_arrayN_aligned(size_t len, size_t size, size_t alignment, const char *str)
Definition mallocn.cc:59
ccl_device_inline float len(const float2 a)
#define T
static void print_struct_array_recursive(const SDNA &sdna, const SDNA_Struct &sdna_struct, const void *initial_data, const int64_t element_num, const int indent, fmt::appender &dst)
static bool char_array_startswith_simple_name(const char *data, const int array_len)
static void print_single_struct_recursive(const SDNA &sdna, const SDNA_Struct &sdna_struct, const void *initial_data, const int indent, fmt::appender &dst)
void print_struct_by_id(int struct_id, const void *data)
void print_structs_at_address(const SDNA &sdna, int struct_id, const void *data, const void *address, int64_t element_num, std::ostream &stream)
MatBase< float, 4, 4 > float4x4
return ret
ReconstructStep ** steps
const char * compare_flags
eReconstructStepType type
union ReconstructStep::@071354107217270120033057222027047172212277333201 data
SDNA_StructMember members[]
struct GHash * types_to_structs_map
const char ** members
int types_num
int structs_num
short * types_size
SDNA_Struct ** structs
int * types_alignment
struct MemArena * mem_arena
int members_num
int members_num_alloc
const char ** types
int pointer_size
const char * data
bool data_alloc
struct SDNA::@202212146013366211022056013367272213111007374231 alias
short * members_array_num
int data_size
i
Definition text_draw.cc:230
struct ReconstructStep::@071354107217270120033057222027047172212277333201::@044123166360150113074235220132123363066272115022 cast_primitive
struct ReconstructStep::@071354107217270120033057222027047172212277333201::@024265075232010367206107235241134100000152136357 cast_pointer
struct ReconstructStep::@071354107217270120033057222027047172212277333201::@064020044043160367014130207226267156041225015165 substruct
struct ReconstructStep::@071354107217270120033057222027047172212277333201::@317035355061027230024155212135006366012152201143 memcpy