1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
//! # blend_info
//!
//! Print some information about a Blender scene file.
//! ```shell
//! $ ./target/release/blend_info -h
//! blend_info 0.2.9
//! Print some information about a Blender scene file
//!
//! USAGE:
//!     blend_info [FLAGS] [OPTIONS] <path>
//!
//! FLAGS:
//!         --dna         Print information about DNA of Blender
//!     -h, --help        Prints help information
//!         --pointers    Print code (e.g. OB, CA, LA, MA, DATA) and pointers
//!     -V, --version     Prints version information
//!
//! OPTIONS:
//!     -n, --struct_name <struct-name>    Print information about a particular struct
//!
//! ARGS:
//!     <path>    The path to the file to read
//! ```
//!
//! ## Usage as a crate (used in your own code)
//!
//! ```rust
//! use blend_info::{print_pointer, read_dna, use_dna, DnaStrC};
//! // std
//! use std::collections::HashMap;
//! use std::path::PathBuf;
//! 
//! fn main() -> std::io::Result<()> {
//!     println!("use_dna");
//!     // .blend file
//!     let path: PathBuf = PathBuf::from(r"blend/factory_v279.blend");
//!     println!("Try to read {:?} file ...", path);
//!     // read DNA
//!     let mut dna_types_hm: HashMap<String, u16> = HashMap::new();
//!     let mut dna_structs_hm: HashMap<String, DnaStrC> = HashMap::new();
//!     let mut dna_pointers_hm: HashMap<usize, usize> = HashMap::new();
//!     let mut dna_2_type_id: Vec<u16> = Vec::new();
//!     let mut types: Vec<String> = Vec::new();
//!     let mut num_bytes_read: usize = 0;
//!     let print_dna: bool = false;
//!     let print_pointers: bool = true;
//!     read_dna(
//!         print_dna,
//!         print_pointers,
//!         &path,
//!         &mut dna_types_hm,
//!         &mut dna_structs_hm,
//!         &mut dna_pointers_hm,
//!         &mut dna_2_type_id,
//!         &mut types,
//!         &mut num_bytes_read,
//!     )?;
//!     println!("{} bytes read by read_dna() ...", num_bytes_read);
//!     // use DNA
//!     println!(
//!         "Try to read {:?} file again (camera/lamp centric this time) ...",
//!         path
//!     );
//!     let print_dna: bool = true;
//!     let names: Vec<String> = vec!["Camera".to_string(), "Lamp".to_string()];
//!     let mut bytes_read: Vec<u8> = Vec::with_capacity(num_bytes_read);
//!     let mut structs_read: Vec<String> = Vec::with_capacity(names.len());
//!     let mut data_read: Vec<u32> = Vec::with_capacity(names.len());
//!     let mut pointers_read: Vec<(usize, u32)> = Vec::with_capacity(names.len());
//!     use_dna(
//!         print_dna,
//!         &path,
//!         &dna_types_hm,
//!         &dna_structs_hm,
//!         &names,
//!         &dna_2_type_id,
//!         &types,
//!         &mut bytes_read,
//!         &mut structs_read,
//!         &mut data_read,
//!         &mut pointers_read,
//!     )?;
//!     println!("bytes_read: {:?}", bytes_read);
//!     println!("structs_read: {:?}", structs_read);
//!     println!("data_read: {:?}", data_read);
//!     println!("pointers_read: {:?}", pointers_read);
//!     print!("SDNAnr = {}: ", pointers_read[0].1);
//!     print_pointer(pointers_read[0].0, &names[0], &dna_pointers_hm);
//!     print!("SDNAnr = {}: ", pointers_read[1].1);
//!     print_pointer(pointers_read[1].0, &names[1], &dna_pointers_hm);
//!     Ok(())
//! }
//! ```
//!
//! ## Examples (used as a standalone executable to query .blend files)
//!
//! ### DNA
//!
//! Find Blender version and read all bytes:
//!
//! ```shell
//! $ ./target/release/blend_info --dna blend/factory_v279.blend | less
//! BLENDER-v279
//! ...
//! 459792 bytes read
//! ```
//!
//! ```shell
//! $ ./target/release/blend_info --dna blend/factory_v300.blend | less
//! BLENDER-v300
//! ...
//! 806388 bytes read
//! ```
//!
//! Get an idea what structs might be useful and which names are defined:
//!
//! ```shell
//! $ ./target/release/blend_info --dna blend/factory_v279.blend | grep "\[SDNAnr =" -A 1
//!   [SDNAnr = 0]
//!   Link (len=16) {
//! --
//!   [SDNAnr = 1]
//!   LinkData (len=24) {
//! ...
//! --
//!   [SDNAnr = 620]
//!   CacheFile (len=1200) {
//! ```
//!
//! ### Structs and their contained data
//!
//! ```shell
//! $ ./target/release/blend_info -n Camera blend/factory_v279.blend
//! Camera 248
//! struct Camera { // SDNAnr = 25
//!     ID id; // 120
//!     AnimData *adt; // 8
//!     char type; // 1
//!     char dtx; // 1
//!     short flag; // 2
//!     float passepartalpha; // 4
//!     float clipsta; // 4
//!     float clipend; // 4
//!     float lens; // 4
//!     float ortho_scale; // 4
//!     float drawsize; // 4
//!     float sensor_x; // 4
//!     float sensor_y; // 4
//!     float shiftx; // 4
//!     float shifty; // 4
//!     float YF_dofdist; // 4
//!     Ipo *ipo; // 8
//!     Object *dof_ob; // 8
//!     GPUDOFSettings gpu_dof; // 24
//!     char sensor_fit; // 1
//!     char pad[7]; // 7
//!     CameraStereoSettings stereo; // 24
//! }; // 248
//! ```
//! ```shell
//! $ ./target/release/blend_info -n ID blend/factory_v279.blend
//! ID 120
//! struct ID { // SDNAnr = 10
//!     void *next; // 8
//!     void *prev; // 8
//!     ID *newid; // 8
//!     Library *lib; // 8
//!     char name[66]; // 66
//!     short flag; // 2
//!     short tag; // 2
//!     short pad_s1; // 2
//!     int us; // 4
//!     int icon_id; // 4
//!     IDProperty *properties; // 8
//! }; // 120
//! ```
//! ```shell
//! $ ./target/release/blend_info -n Object.id.name blend/factory_v279.blend
//! Object.id.name = "OBCamera"
//! Object.id.name = "OBCube"
//! Object.id.name = "OBLamp"
//! ```
//! ```shell
//! $ ./target/release/blend_info -n Camera.id.name blend/factory_v279.blend
//! Camera.id.name = "CACamera"
//! ```
//! ```shell
//! $ ./target/release/blend_info -n Lamp.id.name blend/factory_v279.blend
//! Lamp.id.name = "LALamp"
//! ```
//! ```shell
//! $ ./target/release/blend_info -n Mesh.id.name blend/factory_v279.blend
//! Mesh.id.name = "MECube"
//! ```
//! ```shell
//! $ ./target/release/blend_info -n Camera.lens blend/factory_v279.blend
//!Camera.lens = 35_f32
//! ```
//! ```shell
//! $ ./target/release/blend_info -n Object.obmat blend/factory_v279.blend
//! Object.obmat = [
//!     0.68592066,
//!     0.72767633,
//!     0.0,
//!     0.0,
//!     -0.32401347,
//!     0.30542085,
//!     0.89539564,
//!     0.0,
//!     0.6515582,
//!     -0.6141704,
//!     0.4452714,
//!     0.0,
//!     7.4811316,
//!     -6.50764,
//!     5.343665,
//!     1.0,
//! ]
//! Object.obmat = [
//!     1.0,
//!     0.0,
//!     0.0,
//!     0.0,
//!     0.0,
//!     1.0,
//!     0.0,
//!     0.0,
//!     0.0,
//!     0.0,
//!     1.0,
//!     0.0,
//!     0.0,
//!     0.0,
//!     0.0,
//!     1.0,
//! ]
//! Object.obmat = [
//!     -0.29086465,
//!     0.95517117,
//!     -0.05518906,
//!     0.0,
//!     -0.7711008,
//!     -0.19988336,
//!     0.60452473,
//!     0.0,
//!     0.5663932,
//!     0.2183912,
//!     0.79467225,
//!     0.0,
//!     4.0762453,
//!     1.005454,
//!     5.903862,
//!     1.0,
//! ]
//! ```

use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::mem;

/// Each Blender C struct can have member entries, with a type and a name.
#[derive(Debug, Clone)]
pub struct DnaStrMember {
    /// Type of struct member, e.g. "float"
    pub mem_type: String,
    /// Name of struct member, e.g. "lens"
    pub mem_name: String,
}

impl DnaStrMember {
    pub fn new(mem_type: String, mem_name: String) -> Self {
        DnaStrMember {
            mem_type: mem_type,
            mem_name: mem_name,
        }
    }
}

/// Each Blender versions stores a number of DnaStrC blocks, which
/// represent C structs in the original C code.
///
/// e.g.
/// ```shell
/// $ ./target/release/blend_info --dna blend/factory_v279.blend | grep " Camera (" -B 1 -A 23
///   [SDNAnr = 25]
///   Camera (len=248) {
///     ID id;
///     AnimData *adt;
///     char type;
///     char dtx;
///     short flag;
///     float passepartalpha;
///     float clipsta;
///     float clipend;
///     float lens;
///     float ortho_scale;
///     float drawsize;
///     float sensor_x;
///     float sensor_y;
///     float shiftx;
///     float shifty;
///     float YF_dofdist;
///     Ipo *ipo;
///     Object *dof_ob;
///     GPUDOFSettings gpu_dof;
///     char sensor_fit;
///     char pad[7];
///     CameraStereoSettings stereo;
///   }
/// ```
#[derive(Debug)]
pub struct DnaStrC {
    /// SDNAnr: ID within Blender for struct names
    pub sdna_nr: u32,
    /// A vector of struct members (type and name)
    pub members: Vec<DnaStrMember>,
}

impl DnaStrC {
    pub fn new(sdna_nr: u32, members: Vec<DnaStrMember>) -> Self {
        DnaStrC {
            sdna_nr: sdna_nr,
            members: members,
        }
    }
}

fn decode_blender_header(print_dna: bool, header: &[u8], version: &mut u32) -> bool {
    // BLENDER
    match header[0] as char {
        'B' => {
            if print_dna {
                print!("B")
            }
        }
        _ => return false,
    }
    match header[1] as char {
        'L' => {
            if print_dna {
                print!("L")
            }
        }
        _ => return false,
    }
    match header[2] as char {
        'E' => {
            if print_dna {
                print!("E")
            }
        }
        _ => return false,
    }
    match header[3] as char {
        'N' => {
            if print_dna {
                print!("N")
            }
        }
        _ => return false,
    }
    match header[4] as char {
        'D' => {
            if print_dna {
                print!("D")
            }
        }
        _ => return false,
    }
    match header[5] as char {
        'E' => {
            if print_dna {
                print!("E")
            }
        }
        _ => return false,
    }
    match header[6] as char {
        'R' => {
            if print_dna {
                print!("R")
            }
        }
        _ => return false,
    }
    // [_|-]
    match header[7] as char {
        '_' => {
            if print_dna {
                print!("_")
            }
        }
        '-' => {
            if print_dna {
                print!("-")
            }
        }
        _ => return false,
    }
    // [v|V]
    match header[8] as char {
        'v' => {
            if print_dna {
                print!("v")
            }
        }
        'V' => {
            if print_dna {
                print!("V")
            }
        }
        _ => return false,
    }
    for i in 9..12 {
        if header[i].is_ascii_digit() {
            if print_dna {
                print!("{:?}", (header[i] as char).to_digit(10).unwrap())
            };
        } else {
            return false;
        }
    }
    if print_dna {
        print!("\n");
    }
    // get the version number (last 3 chars)
    let last3c = vec![header[9], header[10], header[11]];
    let version_str = String::from_utf8(last3c).unwrap();
    // convert to u32 and return
    *version = version_str.parse::<u32>().unwrap();
    true
}

fn make_id(code: &[u8]) -> String {
    let mut id = String::with_capacity(4);
    for i in 0..4 {
        if (code[i] as char).is_ascii_alphanumeric() {
            id.push(code[i] as char);
        }
    }
    id
}

fn do_print(code: &String) -> bool {
    if *code != String::from("BR")
        && *code != String::from("DATA")
        && *code != String::from("DNA1")
        && *code != String::from("GLOB")
        && *code != String::from("GR")
        && *code != String::from("LS")
        && *code != String::from("PL")
        && *code != String::from("REND")
        && *code != String::from("SC")
        && *code != String::from("SN")
        && *code != String::from("TEST")
        && *code != String::from("WM")
        && *code != String::from("WS")
    {
        true
    } else {
        false
    }
}

fn read_names(
    print_dna: bool,
    f: &mut File,
    nr_names: usize,
    names: &mut Vec<String>,
    byte_counter: &mut usize,
) -> std::io::Result<()> {
    let mut name_counter: usize = 0;
    let mut buffer = [0; 1];
    loop {
        if names.len() == nr_names {
            break;
        } else {
            let mut name = String::new();
            loop {
                // read only one char/byte
                f.read(&mut buffer)?;
                *byte_counter += 1;
                if buffer[0] == 0 {
                    break;
                } else {
                    name.push(buffer[0] as char);
                }
            }
            // println!("  {:?}", name);
            names.push(name);
            name_counter += 1;
        }
    }
    if print_dna {
        println!("  {} names found in {} bytes", name_counter, byte_counter);
    }
    Ok(())
}

fn read_type_names(
    print_dna: bool,
    f: &mut File,
    nr_types: usize,
    type_names: &mut Vec<String>,
    byte_counter: &mut usize,
) -> std::io::Result<()> {
    let mut name_counter: usize = 0;
    let mut buffer = [0; 1];
    loop {
        if type_names.len() == nr_types {
            break;
        } else {
            let mut name = String::new();
            loop {
                // read only one char/byte
                f.read(&mut buffer)?;
                *byte_counter += 1;
                if buffer[0] == 0 {
                    break;
                } else {
                    name.push(buffer[0] as char);
                }
            }
            // println!("  {:?}", name);
            type_names.push(name);
            name_counter += 1;
        }
    }
    if print_dna {
        println!(
            "  {} type names found in {} bytes",
            name_counter, byte_counter
        );
    }
    Ok(())
}

/// Each Blender C struct can have member entries and this function
/// calculates a size for each of these entries.
///
/// e.g.
/// ```shell
/// $ ./target/release/blend_info -n Camera blend/factory_v279.blend
/// Camera 248
/// struct Camera { // SDNAnr = 25
///   ID id; // 120
///   AnimData *adt; // 8
///   char type; // 1
///   char dtx; // 1
///   short flag; // 2
///   float passepartalpha; // 4
///   float clipsta; // 4
///   float clipend; // 4
///   float lens; // 4
///   float ortho_scale; // 4
///   float drawsize; // 4
///   float sensor_x; // 4
///   float sensor_y; // 4
///   float shiftx; // 4
///   float shifty; // 4
///   float YF_dofdist; // 4
///   Ipo *ipo; // 8
///   Object *dof_ob; // 8
///   GPUDOFSettings gpu_dof; // 24
///   char sensor_fit; // 1
///   char pad[7]; // 7
///   CameraStereoSettings stereo; // 24
/// }; // 248
/// ```
///
/// Simple members, like "char", "short", or "float" consist e.g. of
/// one, two, or 4 bytes, whereas ID is a C struct itself (and the
/// number of bytes might change over time between Blender versions).
/// Pointers to a C struct start with an "*" in it's name
/// (e.g. "*ipo"), and always have the same length. independent of the
/// length of the C struct they are pointing to. Arrays (like
/// "pad\[7\]") multiply the length of the array (in this case 7) by the
/// number of bytes used for the type (in this case one byte for a
/// "char").
pub fn calc_mem_tlen(member: &DnaStrMember, type_found: u16) -> u16 {
    let mut mem_tlen: u16 = 0;
    // check first char for '*' (pointer)
    let mut chars = member.mem_name.chars();
    if let Some(c) = chars.next() {
        if c == '*' {
            // pointers
            if let Some(cl) = chars.last() {
                // array of pointers?
                if cl == ']' {
                    // find number between square brackets ('some[number]')
                    let mut chars2 = member.mem_name.chars();
                    let radix: u32 = 10;
                    let mut number_index: usize = 0;
                    let mut numbers: Vec<u32> = Vec::with_capacity(2 as usize);
                    let mut start: bool = false;
                    while let Some(c2) = chars2.next() {
                        if !start && c2 == '[' {
                            numbers.push(0);
                            start = true;
                        } else if start && c2 == '[' {
                            number_index += 1;
                            numbers.push(0);
                        } else if start && c2.is_digit(radix) {
                            numbers[number_index] *= radix;
                            numbers[number_index] += c2.to_digit(radix).unwrap();
                        }
                    }
                    if numbers.len() == 1 {
                        mem_tlen = (numbers[0] as u16) * mem::size_of::<usize>() as u16;
                    } else if numbers.len() == 3 {
                        mem_tlen = (numbers[0] as u16)
                            * (numbers[1] as u16)
                            * (numbers[2] as u16)
                            * mem::size_of::<usize>() as u16;
                    } else {
                        mem_tlen = (numbers[0] as u16)
                            * (numbers[1] as u16)
                            * mem::size_of::<usize>() as u16;
                    }
                } else {
                    // simple pointer
                    mem_tlen = mem::size_of::<usize>() as u16;
                }
            }
        } else if c == '(' {
            // functions?
            if let Some(c2) = chars.next() {
                if c2 == '*' {
                    // function pointer
                    mem_tlen = mem::size_of::<usize>() as u16;
                } else {
                    println!("TODO: {:?}", chars);
                }
            } else {
                println!("TODO: {:?}", chars);
            }
        } else {
            if let Some(cl) = chars.last() {
                // arrays?
                if cl == ']' {
                    // find number between square brackets ('some[number]')
                    let mut chars2 = member.mem_name.chars();
                    let radix: u32 = 10;
                    let mut number_index: usize = 0;
                    let mut numbers: Vec<u32> = Vec::with_capacity(2 as usize);
                    let mut start: bool = false;
                    while let Some(c2) = chars2.next() {
                        if !start && c2 == '[' {
                            numbers.push(0);
                            start = true;
                        } else if start && c2 == '[' {
                            number_index += 1;
                            numbers.push(0);
                        } else if start && c2.is_digit(radix) {
                            numbers[number_index] *= radix;
                            numbers[number_index] += c2.to_digit(radix).unwrap();
                        }
                    }
                    if numbers.len() == 1 {
                        mem_tlen = (numbers[0] as u16) * type_found;
                    } else {
                        mem_tlen = (numbers[0] as u16) * (numbers[1] as u16) * type_found;
                    }
                } else {
                    // simple type or struct
                    mem_tlen = type_found;
                }
            } else {
                // single letter name
                mem_tlen = type_found;
            }
        }
    }
    mem_tlen
}

/// Try to extract a String from a C struct with a "\*name\*" member.
pub fn get_id_name(
    member: &DnaStrMember,
    bytes_read: &[u8],
    byte_index: usize,
    dna_structs_hm: &HashMap<String, DnaStrC>,
    dna_types_hm: &HashMap<String, u16>,
) -> String {
    let mut return_str: String = String::new();
    if let Some(struct_found2) = dna_structs_hm.get(member.mem_type.as_str()) {
        let mut byte_index2: usize = 0;
        for member2 in &struct_found2.members {
            if let Some(type_found2) = dna_types_hm.get(&member2.mem_type) {
                let mem_tlen2: u16 = calc_mem_tlen(member2, *type_found2);
                if member2.mem_name.contains("name") {
                    let mut id = String::with_capacity(mem_tlen2 as usize);
                    for i in 0..mem_tlen2 as usize {
                        if bytes_read[byte_index + byte_index2 + i] == 0 {
                            break;
                        }
                        id.push(bytes_read[byte_index + byte_index2 + i] as char);
                    }
                    // this will be returned
                    return_str = id;
                    byte_index2 += mem_tlen2 as usize;
                } else {
                    byte_index2 += mem_tlen2 as usize;
                }
            }
        }
    }
    return_str
}

/// Extract byte by index, if member type matches "char", otherwise return 0_u8.
pub fn get_char(member: &DnaStrMember, bytes_read: &[u8], byte_index: usize) -> u8 {
    let mut char_value: u8 = 0;
    if member.mem_type.as_str() == "char" {
        char_value = bytes_read[byte_index];
    }
    char_value
}

/// Extract 4 bytes in a row and interpret those as a "float".
///
/// Print a WARNING in case the member type is **not** a "float" (and return 0.0_f32).
pub fn get_float(member: &DnaStrMember, bytes_read: &[u8], byte_index: usize) -> f32 {
    let mut float_value: f32 = 0.0;
    if member.mem_type.as_str() == "float" {
        let mut float_buf: [u8; 4] = [0_u8; 4];
        for i in 0..4 as usize {
            float_buf[i] = bytes_read[byte_index + i];
        }
        float_value = unsafe { mem::transmute(float_buf) };
    } else {
        println!("WARNING: \"float\" expected, {:?} found", member.mem_type);
    }
    float_value
}

/// Try to extract an array of 2 floats.
///
/// Print a WARNING in case the member type is **not** a "float" (and return array of two 0.0_f32).
pub fn get_float2(member: &DnaStrMember, bytes_read: &[u8], byte_index: usize) -> [f32; 2] {
    let mut float_values: [f32; 2] = [0.0; 2];
    if member.mem_type.as_str() == "float" {
        for i in 0..2 {
            let mut float_buf: [u8; 4] = [0_u8; 4];
            for b in 0..4 as usize {
                float_buf[b] = bytes_read[byte_index + i * 4 + b];
            }
            float_values[i] = unsafe { mem::transmute(float_buf) };
        }
    } else {
        println!("WARNING: \"float\" expected, {:?} found", member.mem_type);
    }
    float_values
}

/// Try to extract an array of 3 floats.
///
/// Print a WARNING in case the member type is **not** a "float" (and return array of three 0.0_f32).
pub fn get_float3(member: &DnaStrMember, bytes_read: &[u8], byte_index: usize) -> [f32; 3] {
    let mut float_values: [f32; 3] = [0.0; 3];
    if member.mem_type.as_str() == "float" {
        for i in 0..3 {
            let mut float_buf: [u8; 4] = [0_u8; 4];
            for b in 0..4 as usize {
                float_buf[b] = bytes_read[byte_index + i * 4 + b];
            }
            float_values[i] = unsafe { mem::transmute(float_buf) };
        }
    } else {
        println!("WARNING: \"float\" expected, {:?} found", member.mem_type);
    }
    float_values
}

/// Try to extract an array of 4 floats.
///
/// Print a WARNING in case the member type is **not** a "float" (and return array of four 0.0_f32).
pub fn get_float4(member: &DnaStrMember, bytes_read: &[u8], byte_index: usize) -> [f32; 4] {
    let mut float_values: [f32; 4] = [0.0; 4];
    if member.mem_type.as_str() == "float" {
        for i in 0..4 {
            let mut float_buf: [u8; 4] = [0_u8; 4];
            for b in 0..4 as usize {
                float_buf[b] = bytes_read[byte_index + i * 4 + b];
            }
            float_values[i] = unsafe { mem::transmute(float_buf) };
        }
    } else {
        println!("WARNING: \"float\" expected, {:?} found", member.mem_type);
    }
    float_values
}

/// Extract 4 bytes in a row and interpret those as a "int".
///
/// Print a WARNING in case the member type is **not** an "int" (and return 0_i32).
pub fn get_int(member: &DnaStrMember, bytes_read: &[u8], byte_index: usize) -> i32 {
    let mut int_value: i32 = 0;
    if member.mem_type.as_str() == "int" {
        int_value += (bytes_read[byte_index] as i32) << 0;
        int_value += (bytes_read[byte_index + 1] as i32) << 8;
        int_value += (bytes_read[byte_index + 2] as i32) << 16;
        int_value += (bytes_read[byte_index + 3] as i32) << 24;
    } else {
        println!("WARNING: \"int\" expected, {:?} found", member.mem_type);
    }
    int_value
}

/// Try to extract 16 "float" values and return them as an array of f32 values.
///
/// Print a WARNING in case the member type is **not** a "float" (and return array of 16 0.0_f32).
pub fn get_matrix(member: &DnaStrMember, bytes_read: &[u8], byte_index: usize) -> [f32; 16] {
    let mut mat_values: [f32; 16] = [0.0_f32; 16];
    if member.mem_type.as_str() == "float" {
        let mut skip_bytes: usize = 0;
        for i in 0..4 {
            for j in 0..4 {
                let mut mat_buf: [u8; 4] = [0_u8; 4];
                for b in 0..4 as usize {
                    mat_buf[b] = bytes_read[byte_index + skip_bytes + b];
                }
                let mat: f32 = unsafe { mem::transmute(mat_buf) };
                mat_values[i * 4 + j] = mat;
                skip_bytes += 4;
            }
        }
    } else {
        println!("WARNING: \"float\" expected, {:?} found", member.mem_type);
    }
    mat_values
}

/// Try to extract 8 bytes in a row and interpret those as a pointer (in memory).
pub fn get_pointer(_member: &DnaStrMember, bytes_read: &[u8], byte_index: usize) -> usize {
    let mut pointer_value: usize = 0;
    pointer_value += (bytes_read[byte_index] as usize) << 0;
    pointer_value += (bytes_read[byte_index + 1] as usize) << 8;
    pointer_value += (bytes_read[byte_index + 2] as usize) << 16;
    pointer_value += (bytes_read[byte_index + 3] as usize) << 24;
    pointer_value += (bytes_read[byte_index + 4] as usize) << 32;
    pointer_value += (bytes_read[byte_index + 5] as usize) << 40;
    pointer_value += (bytes_read[byte_index + 6] as usize) << 48;
    pointer_value += (bytes_read[byte_index + 7] as usize) << 56;
    pointer_value
}

/// Extract 2 bytes in a row and interpret those as a "short".
///
/// Print a WARNING in case the member type is **not** an "short" (and return 0_i16).
pub fn get_short(member: &DnaStrMember, bytes_read: &[u8], byte_index: usize) -> i16 {
    let mut short_value: i16 = 0;
    if member.mem_type.as_str() == "short" {
        short_value += (bytes_read[byte_index] as i16) << 0;
        short_value += (bytes_read[byte_index + 1] as i16) << 8;
    } else {
        println!("WARNING: \"short\" expected, {:?} found", member.mem_type);
    }
    short_value
}

/// Try to extract an array of 3 shorts.
///
/// Print a WARNING in case the member type is **not** a "short" (and return array of three 0_i16).
pub fn get_short3(member: &DnaStrMember, bytes_read: &[u8], byte_index: usize) -> [i16; 3] {
    let mut short_values: [i16; 3] = [0; 3];
    if member.mem_type.as_str() == "short" {
        for i in 0..3 {
            let mut short_value: i16 = 0;
            short_value += (bytes_read[byte_index] as i16) << 0;
            short_value += (bytes_read[byte_index + 1] as i16) << 8;
            short_values[i] = short_value;
        }
    } else {
        println!("WARNING: \"short\" expected, {:?} found", member.mem_type);
    }
    short_values
}

/// Print information about a pointer (byte position in binary .blend file vs. memory address).
///
/// e.g.
/// ```shell
/// $ ./target/release/blend_info -n Object.*data blend/factory_v279.blend
/// Object.*data = 0x000567dc (0x00007fa32df90a08)
/// Object.*data = 0x00056d3c (0x00007fa31ef6b608)
/// Object.*data = 0x000568ec (0x00007fa3334c6608)
/// ```
pub fn print_pointer(
    pointer: usize,
    struct_name: &String,
    dna_pointers_hm: &HashMap<usize, usize>,
) {
    if pointer != 0_usize {
        if let Some(pointer_found) = dna_pointers_hm.get(&pointer) {
            println!(
                "{} = {:#010x} ({:#018x})",
                struct_name, pointer_found, pointer
            );
        }
    } else {
        println!("{} = NULL", struct_name);
    }
}

/// Read a `.blend` file to extract DNA information first.
///
/// Verbosity flags:
///
/// * `print_dna`
/// * `print_pointers`
///
/// Input
///
/// * `path` - a path to a folder/filename.blend
///
/// Return values (`&mut`):
/// * `dna_types_hm` - A HashMap with a type name (e.g. "float") and it's byte size as u16
/// * `dna_structs_hm` - A HashMap with a struct name (e.g. "Camera") and it's members (DnaStrC)
/// * `dna_pointers_hm` - A HashMap with 2 usize values (real memory
/// address stored in file, and byte position within .blend file)
/// * `dna_2_type_id` - Use `sdna_nr` to find `type_id`
/// * `types` - Vec of type names (e.g. "char", "short", "double")
/// * `bytes_read` - Should match the file size on disk
pub fn read_dna(
    print_dna: bool,
    print_pointers: bool,
    path: &std::path::PathBuf,
    dna_types_hm: &mut HashMap<String, u16>,
    dna_structs_hm: &mut HashMap<String, DnaStrC>,
    dna_pointers_hm: &mut HashMap<usize, usize>,
    dna_2_type_id: &mut Vec<u16>,
    types: &mut Vec<String>,
    bytes_read: &mut usize,
) -> std::io::Result<()> {
    let mut f = File::open(path)?;
    // read exactly 12 bytes
    let mut counter: usize = 0;
    let mut chunk_start: usize;
    let mut buffer = [0; 12];
    f.read(&mut buffer)?;
    counter += 12;
    let mut blender_version: u32 = 0;
    if !decode_blender_header(print_dna, &buffer, &mut blender_version) {
        println!("ERROR: Not a .blend file");
        println!("First 12 bytes:");
        println!("{:?}", buffer);
    } else {
        let mut current_code: String = String::new();
        let mut data_counter: u32 = 0;
        loop {
            chunk_start = counter;
            // check 4 chars
            let mut buffer = [0; 4];
            f.read(&mut buffer)?;
            counter += 4;
            let code = make_id(&buffer);
            let mut buffer = [0; 4];
            f.read(&mut buffer)?;
            counter += 4;
            let mut len: u32 = 0;
            len += (buffer[0] as u32) << 0;
            len += (buffer[1] as u32) << 8;
            len += (buffer[2] as u32) << 16;
            len += (buffer[3] as u32) << 24;
            if code != String::from("DATA") {
                if current_code != String::from("")
                // && data_counter != 0
                {
                    if do_print(&current_code) {
                        if print_dna {
                            println!("  {} has {} data blocks", current_code, data_counter);
                        }
                    }
                }
                current_code = code.clone();
            } else {
                data_counter += 1;
            }
            if do_print(&code) {
                if print_dna {
                    println!("{} ({})", code, len);
                }
            }
            if code != String::from("DATA") {
                // reset
                data_counter = 0;
            }
            // use the old entry for pointer translation
            let mut buffer = [0; 8];
            f.read(&mut buffer)?;
            counter += 8;
            let mut old: usize = 0;
            old += (buffer[0] as usize) << 0;
            old += (buffer[1] as usize) << 8;
            old += (buffer[2] as usize) << 16;
            old += (buffer[3] as usize) << 24;
            old += (buffer[4] as usize) << 32;
            old += (buffer[5] as usize) << 40;
            old += (buffer[6] as usize) << 48;
            old += (buffer[7] as usize) << 56;
            if print_pointers {
                println!("{:?} = {:#018x}", code, old);
            }
            dna_pointers_hm.insert(old, chunk_start);
            // get SDNAnr
            let mut buffer = [0; 4];
            f.read(&mut buffer)?;
            counter += 4;
            let mut sdna_nr: u32 = 0;
            sdna_nr += (buffer[0] as u32) << 0;
            sdna_nr += (buffer[1] as u32) << 8;
            sdna_nr += (buffer[2] as u32) << 16;
            sdna_nr += (buffer[3] as u32) << 24;
            if do_print(&code) {
                if print_dna {
                    println!("SDNAnr = {} ({})", sdna_nr, len);
                }
            }
            // for now ignore the nr entry
            let mut buffer = [0; 4];
            f.read(&mut buffer)?;
            counter += 4;
            if code != String::from("DATA") {
                // TODO
            }
            // are we done?
            if code == String::from("ENDB") {
                break;
            }
            if code == String::from("DNA1") {
                if print_dna {
                    println!("{} ({})", code, len);
                }
                // "SDNANAME" in first 8 bytes
                let mut buffer = [0; 8];
                f.read(&mut buffer)?;
                counter += 8;
                let mut sdna_name = String::with_capacity(8);
                for i in 0..8 {
                    if (buffer[i] as char).is_ascii_alphabetic() {
                        sdna_name.push(buffer[i] as char);
                    }
                }
                if sdna_name != String::from("SDNANAME") {
                    println!("WARNING: \"SDNANAME\" expected, {:?} found", sdna_name);
                    // read remaining bytes
                    let mut buffer = vec![0; (len - 8) as usize];
                    f.read(&mut buffer)?;
                    counter += (len - 8) as usize;
                } else {
                    if print_dna {
                        println!("  {}", sdna_name);
                    }
                    let mut buffer = [0; 4];
                    f.read(&mut buffer)?;
                    counter += 4;
                    let mut nr_names: u32 = 0;
                    nr_names += (buffer[0] as u32) << 0;
                    nr_names += (buffer[1] as u32) << 8;
                    nr_names += (buffer[2] as u32) << 16;
                    nr_names += (buffer[3] as u32) << 24;
                    if print_dna {
                        println!("  expect {} names", nr_names);
                    }
                    let mut names: Vec<String> = Vec::with_capacity(nr_names as usize);
                    let mut names_len: usize = 0;
                    read_names(
                        print_dna,
                        &mut f,
                        nr_names as usize,
                        &mut names,
                        &mut names_len,
                    )?;
                    counter += names_len;
                    let mut remaining_bytes: usize = (len - 12) as usize - names_len;
                    // skip pad bytes, read "TYPE" and nr_types
                    let mut buffer = [0; 1];
                    loop {
                        f.read(&mut buffer)?;
                        counter += 1;
                        if buffer[0] == 0 {
                            // skip pad byte
                            remaining_bytes -= 1;
                        } else if buffer[0] as char == 'T' {
                            remaining_bytes -= 1;
                            break;
                        }
                    }
                    // match 'YPE' ('T' was matched above)
                    let mut buffer = [0; 3];
                    f.read(&mut buffer)?;
                    counter += 3;
                    remaining_bytes -= 3;
                    if buffer[0] as char == 'Y'
                        && buffer[1] as char == 'P'
                        && buffer[2] as char == 'E'
                    {
                        // nr_types
                        let mut buffer = [0; 4];
                        f.read(&mut buffer)?;
                        counter += 4;
                        remaining_bytes -= 4;
                        let mut nr_types: u32 = 0;
                        nr_types += (buffer[0] as u32) << 0;
                        nr_types += (buffer[1] as u32) << 8;
                        nr_types += (buffer[2] as u32) << 16;
                        nr_types += (buffer[3] as u32) << 24;
                        if print_dna {
                            println!("  expect {} type names", nr_types);
                        }
                        let mut types_len: usize = 0;
                        read_type_names(
                            print_dna,
                            &mut f,
                            nr_types as usize,
                            types,
                            &mut types_len,
                        )?;
                        counter += types_len;
                        remaining_bytes -= types_len;
                        // store tlen (type len) here
                        let mut tlen: Vec<u16> = Vec::new();
                        // skip pad bytes, read "TLEN"
                        let mut buffer = [0; 1];
                        loop {
                            f.read(&mut buffer)?;
                            counter += 1;
                            if buffer[0] == 0 {
                                // skip pad byte
                                remaining_bytes -= 1;
                            } else if buffer[0] as char == 'T' {
                                remaining_bytes -= 1;
                                break;
                            }
                        }
                        // match 'LEN' ('T' was matched above)
                        let mut buffer = [0; 3];
                        f.read(&mut buffer)?;
                        counter += 3;
                        remaining_bytes -= 3;
                        if buffer[0] as char == 'L'
                            && buffer[1] as char == 'E'
                            && buffer[2] as char == 'N'
                        {
                            // read short (16 bits = 2 bytes) for each type
                            for i in 0..nr_types as usize {
                                let mut buffer = [0; 2];
                                f.read(&mut buffer)?;
                                counter += 2;
                                remaining_bytes -= 2;
                                let mut type_size: u16 = 0;
                                type_size += (buffer[0] as u16) << 0;
                                type_size += (buffer[1] as u16) << 8;
                                // println!("  {} needs {} bytes", types[i], type_size);
                                tlen.push(type_size);
                                // store data read from DNA
                                dna_types_hm.insert(types[i].clone(), type_size);
                            }
                            // skip pad bytes, read "STRC"
                            let mut buffer = [0; 1];
                            loop {
                                f.read(&mut buffer)?;
                                counter += 1;
                                if buffer[0] == 0 {
                                    // skip pad byte
                                    remaining_bytes -= 1;
                                } else if buffer[0] as char == 'S' {
                                    remaining_bytes -= 1;
                                    break;
                                }
                            }
                            // match 'TRC' ('S' was matched above)
                            let mut buffer = [0; 3];
                            f.read(&mut buffer)?;
                            counter += 3;
                            remaining_bytes -= 3;
                            if buffer[0] as char == 'T'
                                && buffer[1] as char == 'R'
                                && buffer[2] as char == 'C'
                            {
                                // nr_structs
                                let mut buffer = [0; 4];
                                f.read(&mut buffer)?;
                                counter += 4;
                                remaining_bytes -= 4;
                                let mut nr_structs: u32 = 0;
                                nr_structs += (buffer[0] as u32) << 0;
                                nr_structs += (buffer[1] as u32) << 8;
                                nr_structs += (buffer[2] as u32) << 16;
                                nr_structs += (buffer[3] as u32) << 24;
                                if print_dna {
                                    println!("  expect {} struct pointers", nr_structs);
                                }
                                for s in 0..nr_structs as usize {
                                    // read two short values
                                    let mut buffer = [0; 2];
                                    f.read(&mut buffer)?;
                                    counter += 2;
                                    remaining_bytes -= 2;
                                    let mut type_idx: u16 = 0;
                                    type_idx += (buffer[0] as u16) << 0;
                                    type_idx += (buffer[1] as u16) << 8;
                                    f.read(&mut buffer)?;
                                    counter += 2;
                                    remaining_bytes -= 2;
                                    let mut short2: u16 = 0;
                                    short2 += (buffer[0] as u16) << 0;
                                    short2 += (buffer[1] as u16) << 8;
                                    dna_2_type_id.push(type_idx);
                                    // println!("  ({}, {})", type_idx, short2);
                                    if print_dna {
                                        println!("  [SDNAnr = {}]", s);
                                        println!(
                                            "  {} (len={}) {{",
                                            types[type_idx as usize], tlen[type_idx as usize]
                                        );
                                    }
                                    let tuple_counter: usize = short2 as usize;
                                    let mut members: Vec<DnaStrMember> =
                                        Vec::with_capacity(tuple_counter as usize);
                                    for _t in 0..tuple_counter {
                                        // read two short values
                                        let mut buffer = [0; 2];
                                        f.read(&mut buffer)?;
                                        counter += 2;
                                        remaining_bytes -= 2;
                                        let mut type_idx: u16 = 0;
                                        type_idx += (buffer[0] as u16) << 0;
                                        type_idx += (buffer[1] as u16) << 8;
                                        f.read(&mut buffer)?;
                                        counter += 2;
                                        remaining_bytes -= 2;
                                        let mut name_idx: u16 = 0;
                                        name_idx += (buffer[0] as u16) << 0;
                                        name_idx += (buffer[1] as u16) << 8;
                                        if print_dna {
                                            println!(
                                                "    {} {};",
                                                types[type_idx as usize], names[name_idx as usize]
                                            );
                                        }
                                        let member = DnaStrMember::new(
                                            types[type_idx as usize].clone(),
                                            names[name_idx as usize].clone(),
                                        );
                                        members.push(member);
                                    }
                                    if print_dna {
                                        println!("  }}");
                                    }
                                    // store single DnaStrC
                                    let dna_str_c = DnaStrC::new(s as u32, members);
                                    dna_structs_hm
                                        .insert(types[type_idx as usize].clone(), dna_str_c);
                                }
                            } else {
                                println!("ERROR: \"STRC\" expected, \"S\"{:?} found", buffer)
                            }
                        } else {
                            println!("ERROR: \"TLEN\" expected, \"T\"{:?} found", buffer)
                        }
                    } else {
                        println!("ERROR: \"TYPE\" expected, \"T\"{:?} found", buffer)
                    }
                    // read remaining bytes
                    if print_dna {
                        println!("  remaining bytes: {}", remaining_bytes);
                    }
                    let mut buffer = vec![0; remaining_bytes];
                    f.read(&mut buffer)?;
                    counter += remaining_bytes;
                }
            } else {
                // read len bytes
                let mut buffer = vec![0; len as usize];
                f.read(&mut buffer)?;
                counter += len as usize;
            }
        }
        if print_dna {
            println!("{} bytes read", counter);
        }
        *bytes_read = counter;
    }
    Ok(())
}

/// Use the DNA types and structs returned by read_dna().
///
/// Read the .blend file (again), but limit the returned data by
/// filtering out information provided as input by the names vector of
/// strings.
///
/// Verbosity flags:
///
/// * `print_dna`
///
/// Input
///
/// * `path` - a path to a folder/filename.blend
/// * `names` - filter out by list of interesting things
///
/// Input returned by previous read_dna(...) call
///
/// * `dna_types_hm` - A HashMap with a type name (e.g. "float") and it's byte size as u16
/// * `dna_structs_hm` - A HashMap with a struct name (e.g. "Camera") and it's members (DnaStrC)
/// * `dna_2_type_id` - Use `sdna_nr` to find `type_id`
/// * `types` - Vec of type names (e.g. "char", "short", "double")
///
/// Return values (`&mut`):
/// * `bytes_read` - All bytes read which relate to input names
/// * `structs_read` - All struct names read as a list in order of bytes read
/// * `data_read` - List of chunk sizes (same length as *structs_read*)
/// * `pointers_read` - List of tuples (pointer as usize and SDNAnr)
pub fn use_dna(
    print_dna: bool,
    path: &std::path::PathBuf,
    dna_types_hm: &HashMap<String, u16>,
    dna_structs_hm: &HashMap<String, DnaStrC>,
    names: &Vec<String>,
    dna_2_type_id: &Vec<u16>,
    types: &Vec<String>,
    bytes_read: &mut Vec<u8>,
    structs_read: &mut Vec<String>,
    data_read: &mut Vec<u32>,
    pointers_read: &mut Vec<(usize, u32)>,
) -> std::io::Result<()> {
    // find sdna_nr for each name entry
    let mut sdna_nrs: Vec<u32> = Vec::with_capacity(names.len());
    for name in names {
        if let Some(struct_found) = dna_structs_hm.get(name) {
            let sdna_nr: u32 = struct_found.sdna_nr;
            sdna_nrs.push(sdna_nr);
        }
    }
    // read Blender file
    let mut f = File::open(path)?;
    // read exactly 12 bytes
    let mut counter: usize = 0;
    let mut buffer = [0; 12];
    f.read(&mut buffer)?;
    counter += 12;
    let mut blender_version: u32 = 0;
    if !decode_blender_header(print_dna, &buffer, &mut blender_version) {
        println!("ERROR: Not a .blend file");
        println!("First 12 bytes:");
        println!("{:?}", buffer);
    } else {
        loop {
            // check 4 chars
            let mut buffer = [0; 4];
            f.read(&mut buffer)?;
            counter += 4;
            let code = make_id(&buffer);
            let mut buffer = [0; 4];
            f.read(&mut buffer)?;
            counter += 4;
            let mut len: u32 = 0;
            len += (buffer[0] as u32) << 0;
            len += (buffer[1] as u32) << 8;
            len += (buffer[2] as u32) << 16;
            len += (buffer[3] as u32) << 24;
            // use the old entry for pointer translation
            let mut buffer = [0; 8];
            f.read(&mut buffer)?;
            counter += 8;
            let mut old: usize = 0;
            old += (buffer[0] as usize) << 0;
            old += (buffer[1] as usize) << 8;
            old += (buffer[2] as usize) << 16;
            old += (buffer[3] as usize) << 24;
            old += (buffer[4] as usize) << 32;
            old += (buffer[5] as usize) << 40;
            old += (buffer[6] as usize) << 48;
            old += (buffer[7] as usize) << 56;
            // get SDNAnr
            let mut buffer = [0; 4];
            f.read(&mut buffer)?;
            counter += 4;
            let mut sdna_nr: u32 = 0;
            sdna_nr += (buffer[0] as u32) << 0;
            sdna_nr += (buffer[1] as u32) << 8;
            sdna_nr += (buffer[2] as u32) << 16;
            sdna_nr += (buffer[3] as u32) << 24;
            // for now ignore the nr entry
            let mut buffer = [0; 4];
            f.read(&mut buffer)?;
            counter += 4;
            // are we done?
            if code == String::from("ENDB") {
                break;
            }
            // read len bytes
            let mut buffer = vec![0; len as usize];
            f.read(&mut buffer)?;
            counter += len as usize;
            // return buffer?
            if code == String::from("DATA") {
                let type_id: usize = dna_2_type_id[sdna_nr as usize] as usize;
                for search_index in 0..sdna_nrs.len() {
                    let name = &names[search_index];
                    if *name == types[type_id] {
                        // get expected tlen from dna_types
                        let mut type_tlen: u16 = 0;
                        if let Some(tlen) = dna_types_hm.get(name) {
                            type_tlen = *tlen;
                        }
                        if print_dna {
                            println!(
                                "DATA ({} * {}={})",
                                len / type_tlen as u32,
                                types[type_id],
                                len
                            );
                            println!(
                                "{}[{}] (SDNAnr = {}) found in {:?}",
                                name, type_tlen, sdna_nr, sdna_nrs
                            );
                        }
                        bytes_read.append(&mut buffer);
                        structs_read.push(name.clone());
                        data_read.push(len);
                        pointers_read.push((old, sdna_nr));
                    }
                }
            } else {
                for search_index in 0..sdna_nrs.len() {
                    let search_sdna_nr = sdna_nrs[search_index];
                    if search_sdna_nr == sdna_nr {
                        let name = &names[search_index];
                        // get expected tlen from dna_types
                        let mut type_tlen: u16 = 0;
                        if let Some(tlen) = dna_types_hm.get(name) {
                            type_tlen = *tlen;
                        }
                        if print_dna {
                            println!(
                                "{}[{}] (SDNAnr = {}) found in {:?}",
                                name, type_tlen, sdna_nr, sdna_nrs
                            );
                        }
                        bytes_read.append(&mut buffer);
                        structs_read.push(name.clone());
                        data_read.push(len);
                        pointers_read.push((old, sdna_nr));
                    }
                }
            }
        }
        if print_dna {
            println!("{} bytes read", counter);
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn get_id_name_test_01() {
        let member: DnaStrMember = DnaStrMember {
            mem_type: "ID".to_string(),
            mem_name: "id".to_string(),
        };
        let bytes_read = [
            8_u8, 19, 168, 78, 145, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 67, 65, 99, 117, 114, 114, 101, 110, 116, 95, 99, 97, 109, 0, 48,
            48, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            1, 0, 0, 0, 0, 0, 0, 0, 136, 141, 162, 78, 145, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 36, 0, 0, 0, 0, 63, 205, 204, 204, 61, 0, 0, 200, 66, 132, 205, 3, 66, 0, 0, 192,
            64, 205, 204, 204, 61, 0, 0, 0, 66, 0, 0, 144, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 132, 205, 3,
            66, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 184, 30, 133, 61, 153,
            153, 249, 63, 0, 0, 0, 0, 0, 0, 0, 0, 146, 10, 134, 63, 54, 141, 167, 63, 8, 20, 168,
            78, 145, 127, 0, 0, 8, 18, 168, 78, 145, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 67, 65, 100, 111, 111, 114, 49, 95, 99, 97, 109, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
            0, 0, 200, 142, 162, 78, 145, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 0,
            63, 205, 204, 204, 61, 0, 0, 200, 66, 132, 205, 3, 66, 0, 0, 192, 64, 205, 204, 204,
            61, 0, 0, 0, 66, 0, 0, 144, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 132, 205, 3, 66, 0, 0, 0, 66, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 184, 30, 133, 61, 153, 153, 249, 63, 0, 0,
            0, 0, 0, 0, 0, 0, 146, 10, 134, 63, 54, 141, 167, 63, 8, 21, 168, 78, 145, 127, 0, 0,
            8, 19, 168, 78, 145, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 65,
            100, 111, 111, 114, 50, 121, 95, 99, 97, 109, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 8, 224, 163,
            78, 145, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 0, 63, 205, 204, 204,
            61, 0, 0, 200, 66, 132, 205, 3, 66, 0, 0, 192, 64, 205, 204, 204, 61, 0, 0, 0, 66, 0,
            0, 144, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 132, 205, 3, 66, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 184, 30, 133, 61, 153, 153, 249, 63, 0, 0, 0, 0, 0, 0, 0, 0,
            146, 10, 134, 63, 54, 141, 167, 63, 8, 22, 168, 78, 145, 127, 0, 0, 8, 20, 168, 78,
            145, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 65, 115, 104, 97,
            102, 116, 95, 99, 97, 109, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 72, 225, 163, 78, 145, 127,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 0, 0, 0, 0, 63, 205, 204, 204, 61, 0, 0, 200,
            66, 150, 252, 234, 65, 0, 0, 192, 64, 205, 204, 204, 61, 0, 0, 0, 66, 0, 0, 144, 65, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 67, 150, 252, 234, 65, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 184, 30, 133, 61, 153, 153, 249, 63, 0, 0, 0, 0, 0, 0, 0, 0, 146, 10, 134,
            63, 54, 141, 167, 63, 0, 0, 0, 0, 0, 0, 0, 0, 8, 21, 168, 78, 145, 127, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 65, 120, 89, 95, 99, 97, 109, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 1, 0, 0, 0, 0, 0, 0, 0, 136, 226, 163, 78, 145, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 36, 0, 0, 0, 0, 63, 205, 204, 204, 61, 0, 0, 200, 66, 132, 205, 3, 66, 0, 0, 192,
            64, 205, 204, 204, 61, 0, 0, 0, 66, 0, 0, 144, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 132, 205, 3,
            66, 0, 0, 0, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 184, 30, 133, 61, 153,
            153, 249, 63, 0, 0, 0, 0, 0, 0, 0, 0, 146, 10, 134, 63, 54, 141, 167, 63,
        ];
        let dna_str_c: DnaStrC = DnaStrC {
            sdna_nr: 10,
            members: [
                DnaStrMember {
                    mem_type: "void".to_string(),
                    mem_name: "*next".to_string(),
                },
                DnaStrMember {
                    mem_type: "void".to_string(),
                    mem_name: "*prev".to_string(),
                },
                DnaStrMember {
                    mem_type: "ID".to_string(),
                    mem_name: "*newid".to_string(),
                },
                DnaStrMember {
                    mem_type: "Library".to_string(),
                    mem_name: "*lib".to_string(),
                },
                DnaStrMember {
                    mem_type: "char".to_string(),
                    mem_name: "name[66]".to_string(),
                },
                DnaStrMember {
                    mem_type: "short".to_string(),
                    mem_name: "flag".to_string(),
                },
                DnaStrMember {
                    mem_type: "short".to_string(),
                    mem_name: "tag".to_string(),
                },
                DnaStrMember {
                    mem_type: "short".to_string(),
                    mem_name: "pad_s1".to_string(),
                },
                DnaStrMember {
                    mem_type: "int".to_string(),
                    mem_name: "us".to_string(),
                },
                DnaStrMember {
                    mem_type: "int".to_string(),
                    mem_name: "icon_id".to_string(),
                },
                DnaStrMember {
                    mem_type: "IDProperty".to_string(),
                    mem_name: "*properties".to_string(),
                },
            ]
            .to_vec(),
        };
        let byte_index: usize = 0;
        let mut dna_structs_hm: HashMap<String, DnaStrC> = HashMap::new();
        dna_structs_hm.insert("ID".to_string(), dna_str_c);
        let mut dna_types_hm: HashMap<String, u16> = HashMap::new();
        dna_types_hm.insert("void".to_string(), 0);
        dna_types_hm.insert("Library".to_string(), 2200);
        dna_types_hm.insert("char".to_string(), 1);
        dna_types_hm.insert("IDPropertyData".to_string(), 32);
        dna_types_hm.insert("int".to_string(), 4);
        dna_types_hm.insert("ID".to_string(), 120);
        dna_types_hm.insert("short".to_string(), 2);
        let id: String = get_id_name(
            &member,
            &bytes_read,
            byte_index,
            &dna_structs_hm,
            &dna_types_hm,
        );
        println!("  ID.name = {:?}", id);
        let base_name = id.clone()[2..].to_string();
        println!("{}", base_name);
        assert_eq!(base_name, "current_cam");
    }
}