libopenraw  0.3.7
heap.cpp
1 /* -*- Mode: C++; c-basic-offset:4; tab-width:4; indent-tabs-mode:nil; -*- */
2 /*
3  * libopenraw - ciff/heap.cpp
4  *
5  * Copyright (C) 2006-2020 Hubert Figuière
6  *
7  * This library is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "ciff/heap.hpp"
23 #include "ciffcontainer.hpp"
24 
25 namespace OpenRaw {
26 namespace Internals {
27 namespace CIFF {
28 
29 Heap::Heap(off_t start, off_t length, const CIFFContainer* _container)
30  : m_start(start),
31  m_length(length),
32  m_container(_container),
33  m_records()
34 {
35  LOGDBG2("Heap @ %lld length = %lld\n", (long long int)start, (long long int)m_length);
36 }
37 
39 {
40  if (m_records.size() == 0) {
41  _loadRecords();
42  }
43  return m_records;
44 }
45 
46 
47 bool Heap::_loadRecords()
48 {
49  auto file = m_container->file();
50  auto endian = m_container->endian();
51  file->seek(m_start + m_length - 4, SEEK_SET);
52 
53  auto result = m_container->readInt32(file, endian);
54 
55  if (result) {
56  int32_t record_offset = result.value();
57 
58  m_records.clear();
59  file->seek(m_start + record_offset, SEEK_SET);
60  auto result16 = m_container->readInt16(file, endian);
61  if (result16.empty()) {
62  LOGDBG1("read numRecords failed\n");
63  return false;
64  }
65  int16_t numRecords = result16.value();
66  LOGDBG2("numRecords %d\n", numRecords);
67 
68  for (int16_t i = 0; i < numRecords; i++) {
69  RecordEntry entry;
70  entry.readFrom(m_container);
71  m_records.insert(std::make_pair(TAGCODE(entry.typeCode), entry));
72  }
73  return true;
74  }
75  return false;
76 }
77 
79 {
81  bool ret = false;
82  auto file = container->file();
83  int s = file->read(byteOrder, 2);
84  if (s == 2) {
85  if((byteOrder[0] == 'I') && (byteOrder[1] == 'I')) {
87  }
88  else if((byteOrder[0] == 'M') && (byteOrder[1] == 'M')) {
90  }
91  container->setEndian(endian);
92  auto result32 = container->readUInt32(file, endian);
93  if (result32) {
94  headerLength = result32.value();
95  ret = true;
96  }
97  if (ret) {
98  ret = (file->read(type, 4) == 4);
99  }
100  if (ret) {
101  ret = (file->read(subType, 4) == 4);
102  }
103  if (ret) {
104  result32 = container->readUInt32(file, endian);
105  if (result32) {
106  version = result32.value();
107  ret = true;
108  }
109  }
110  }
111  return ret;
112 }
113 
114 }
115 }
116 }
bool readFrom(CIFFContainer *)
Read the header from the container.
Definition: heap.cpp:78
RawContainer::EndianType endian
Definition: heap.hpp:98
Heap(off_t start, off_t length, const CIFFContainer *container)
Construct a heap from a location in the container.
Definition: heap.cpp:29
RecordEntries & records()
Return the records from the heap. Load them as needed.
Definition: heap.cpp:38
A record entry from a CIFF Heap.
Definition: recordentry.hpp:69
bool readFrom(const CIFFContainer *container)
Definition: recordentry.cpp:46
Option< uint32_t > readUInt32(const IO::Stream::Ptr &f, EndianType endian) const
Read an uint32 following the m_endian set.
Option< int32_t > readInt32(const IO::Stream::Ptr &f, EndianType endian) const
Read an int32 following the m_endian set.
Option< int16_t > readInt16(const IO::Stream::Ptr &f, EndianType endian) const
Read an int16 following the m_endian set.
std::map< uint16_t, RecordEntry > RecordEntries
Definition: recordentry.hpp:65
Global namespace for libopenraw.
Definition: arwfile.cpp:29