libopenraw  0.3.7
recordentry.cpp
1 /* -*- Mode: C++; c-basic-offset:4; tab-width:4; indent-tabs-mode:nil; -*- */
2 /*
3  * libopenraw - ciff/recordentry.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 <stdint.h>
23 #include "ifd.hpp"
24 #include "ciff/recordentry.hpp"
25 #include "ciffcontainer.hpp"
26 
27 namespace OpenRaw {
28 namespace Internals {
29 namespace CIFF {
30 
32  : length(0), offset(0)
33 {
34 }
35 
36 RecordEntry::InHeap::InHeap(uint32_t _length, uint32_t _offset)
37  : length(_length), offset(_offset)
38 {
39 }
40 
41 RecordEntry::RecordEntry()
42  : typeCode(0)
43 {
44 }
45 
46 bool RecordEntry::readFrom(const CIFFContainer* container)
47 {
48  auto file = container->file();
49  auto endian = container->endian();
50  auto result_16 = container->readUInt16(file, endian);
51  if (result_16.empty()) {
52  return false;
53  }
54  typeCode = result_16.value();
55  if (inRecord()) {
56  InRec inrecord;
57  int pos = file->seek(0, SEEK_CUR);
58  container->fetchData(inrecord.c_array(), pos, 8);
59  data = inrecord;
60  } else {
61  auto result_32 = container->readUInt32(file, endian);
62  if (result_32.empty()) {
63  return false;
64  }
65  auto length = result_32.value();
66  result_32 = container->readUInt32(file, endian);
67  if (result_32.empty()) {
68  return false;
69  }
70  auto offset = result_32.value();
72  }
73  return true;
74 }
75 
76 size_t RecordEntry::fetchData(Heap* heap, void* buf, size_t size) const
77 {
78  return heap->container()->fetchData(buf,
79  offset() + heap->offset(), size);
80 }
81 
82 Heap RecordEntry::heap(Heap& h, const CIFFContainer* container) const
83 {
84  return Heap(offset() + h.offset(), length(), container);
85 }
86 
87 uint32_t RecordEntry::count() const
88 {
89  auto length = this->length();
90  switch (type()) {
91  case TYPE_BYTE:
92  return length;
93  case TYPE_ASCII:
94  return length;
95  case TYPE_WORD:
96  return length / 2;
97  case TYPE_DWORD:
98  return length / 4;
99  default:
100  return length;
101  }
102 }
103 
104 uint16_t RecordEntry::exifType() const
105 {
106  switch (type()) {
107  case TYPE_BYTE:
108  return IFD::EXIF_FORMAT_BYTE;
109  case TYPE_ASCII:
110  return IFD::EXIF_FORMAT_ASCII;
111  case TYPE_WORD:
112  return IFD::EXIF_FORMAT_SHORT;
113  case TYPE_DWORD:
114  return IFD::EXIF_FORMAT_LONG;
115  default:
116  return IFD::EXIF_FORMAT_INVALID;
117  }
118 }
119 
120 uint32_t RecordEntry::containerOffset(const Heap& heap) const
121 {
122  return heap.offset() + offset();
123 }
124 
125 std::string RecordEntry::getString(Heap& heap) const
126 {
127  LOGASSERT(type() == TYPE_ASCII);
128  char buf[256];
129  size_t sz = this->length();
130  if(sz > 256) {
131  sz = 256;
132  }
133  /*size_t sz2 = */fetchData(&heap, (void*)buf, sz);
134  return buf;
135 }
136 
137 }
138 }
139 }
const CIFFContainer * container() const
Return the containing container.
Definition: heap.hpp:63
off_t offset() const
Return the offset from the begining of the container.
Definition: heap.hpp:68
boost::array< uint8_t, 8 > InRec
boost::variant< InRec, InHeap > data
uint32_t containerOffset(const Heap &heap) const
bool readFrom(const CIFFContainer *container)
Definition: recordentry.cpp:46
size_t fetchData(Heap *heap, void *buf, size_t size) const
Definition: recordentry.cpp:76
std::string getString(Heap &heap) const
Heap heap(Heap &h, const CIFFContainer *container) const
Definition: recordentry.cpp:82
Option< uint32_t > readUInt32(const IO::Stream::Ptr &f, EndianType endian) const
Read an uint32 following the m_endian set.
Option< uint16_t > readUInt16(const IO::Stream::Ptr &f, EndianType endian) const
Read an uint16 following the m_endian set.
size_t fetchData(void *buf, off_t offset, size_t buf_size) const
Fetch the data chunk from the file.
Global namespace for libopenraw.
Definition: arwfile.cpp:29