Debian Codegraph @

terps/bocfel/blorb.cpp

raw symbols SCIP
1 // Copyright 2010-2023 Chris Spiegel.
2 //
3 // SPDX-License-Identifier: MIT
4
5 #include <memory>
6 #include <stdexcept>
7 #include <unordered_map>
8 #include <utility>
9
10 #include "blorb.h"
11 #include "iff.h"
12 #include "io.h"
13 #include "types.h"
14
15 static constexpr uint32_t PICT = 0x50696374;
16 static constexpr uint32_t SND = 0x536e6420;
17 static constexpr uint32_t EXEC = 0x45786563;
18 static constexpr uint32_t DATA = 0x44617461;
19
20 Blorb::Blorb(const std::shared_ptr<IO> &io)
21 {
22 uint32_t size;
23 std::unique_ptr<IFF> iff;
24
25 try {
26 iff = std::make_unique<IFF>(io, IFF::TypeID("IFRS"));
27 } catch (const IFF::InvalidFile &) {
28 throw InvalidFile();
29 }
30
31 if (!iff->find(IFF::TypeID("RIdx"), size)) {
32 throw InvalidFile();
33 }
34
35 try {
36 uint32_t nresources = iff->io()->read32();
37
38 if ((nresources * 12) + 4 != size) {
39 throw InvalidFile();
40 }
41
42 for (uint32_t i = 0; i < nresources; i++) {
43 Usage usage;
44
45 uint32_t usage_ = iff->io()->read32();
46 uint32_t number = iff->io()->read32();
47 uint32_t start = iff->io()->read32();
48
49 try {
50 static const std::unordered_map<uint32_t, Usage> usages = {
51 {PICT, Usage::Pict},
52 {SND, Usage::Snd},
53 {EXEC, Usage::Exec},
54 {DATA, Usage::Data},
55 };
56
57 usage = usages.at(usage_);
58 } catch (const std::out_of_range &) {
59 continue;
60 }
61
62 long saved = iff->io()->tell();
63 iff->io()->seek(start, IO::SeekFrom::Start);
64 IFF::TypeID type(iff->io()->read32());
65 size = iff->io()->read32();
66 iff->io()->seek(saved, IO::SeekFrom::Start);
67
68 if (type == IFF::TypeID("FORM")) {
69 start -= 8;
70 size += 8;
71 }
72
73 Chunk chunk(type, start + 8, size);
74 m_chunks.insert({{usage, number}, chunk});
75 }
76 } catch (const IO::IOError &) {
77 throw InvalidFile();
78 }
79 }
80
81 const Blorb::Chunk *Blorb::find(Blorb::Usage usage, uint32_t number)
82 {
83 try {
84 return &m_chunks.at({usage, number});
85 } catch (const std::out_of_range &) {
86 return nullptr;
87 }
88 }