libopenraw
rawcontainer.cpp
1 /*
2  * libopenraw - rawcontainer.cpp
3  *
4  * Copyright (C) 2006-2016 Hubert Figuiere
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <fcntl.h>
22 #include <memory>
23 
24 #include <libopenraw/debug.h>
25 
26 #include "trace.hpp"
27 #include "endianutils.hpp"
28 #include "rawcontainer.hpp"
29 
30 using namespace Debug;
31 
32 namespace OpenRaw {
33 namespace Internals {
34 
35 
36 RawContainer::RawContainer(const IO::Stream::Ptr &_file, off_t _offset)
37  : m_file(_file),
38  m_offset(_offset),
39  m_endian(ENDIAN_NULL)
40 {
41  m_file->open();
42  m_file->seek(_offset, SEEK_SET);
43 }
44 
45 
47 {
48  m_file->close();
49 }
50 
51 
52 bool RawContainer::readInt8(const IO::Stream::Ptr &f, int8_t & v)
53 {
54  unsigned char buf;
55  int s = f->read(&buf, 1);
56  if (s != 1) {
57  return false;
58  }
59  v = buf;
60  return true;
61 }
62 
63 bool RawContainer::readUInt8(const IO::Stream::Ptr &f, uint8_t & v)
64 {
65  unsigned char buf;
66  int s = f->read(&buf, 1);
67  if (s != 1) {
68  return false;
69  }
70  v = buf;
71  return true;
72 }
73 
74 bool
75 RawContainer::readInt16(const IO::Stream::Ptr &f, int16_t & v)
76 {
77  if (m_endian == ENDIAN_NULL) {
78 
79  Trace(ERROR) << "null endian\n";
80 
81  return false;
82  }
83  unsigned char buf[2];
84  int s = f->read(buf, 2);
85  if (s != 2) {
86  return false;
87  }
88  if (m_endian == ENDIAN_LITTLE) {
89  v = EL16(buf);
90  }
91  else {
92  v = BE16(buf);
93  }
94  return true;
95 }
96 
97 
98 bool
99 RawContainer::readInt32(const IO::Stream::Ptr &f, int32_t & v)
100 {
101  if (m_endian == ENDIAN_NULL) {
102 
103  Trace(ERROR) << "null endian\n";
104 
105  return false;
106  }
107  unsigned char buf[4];
108  int s = f->read(buf, 4);
109  if (s != 4) {
110  Trace(ERROR) << "read " << s << " bytes\n";
111  return false;
112  }
113 
114  if (m_endian == ENDIAN_LITTLE) {
115  v = EL32(buf);
116  }
117  else {
118  v = BE32(buf);
119  }
120 
121  return true;
122 }
123 
124 
125 bool
126 RawContainer::readUInt16(const IO::Stream::Ptr &f, uint16_t & v)
127 {
128  if (m_endian == ENDIAN_NULL) {
129 
130  Trace(ERROR) << "null endian\n";
131 
132  return false;
133  }
134  unsigned char buf[2];
135  int s = f->read(buf, 2);
136  if (s != 2) {
137  return false;
138  }
139  if (m_endian == ENDIAN_LITTLE) {
140  v = EL16(buf);
141  }
142  else {
143  v = BE16(buf);
144  }
145  return true;
146 }
147 
148 
149 bool
150 RawContainer::readUInt32(const IO::Stream::Ptr &f, uint32_t & v)
151 {
152  if (m_endian == ENDIAN_NULL) {
153 
154  Trace(ERROR) << "null endian\n";
155 
156  return false;
157  }
158  unsigned char buf[4];
159  int s = f->read(buf, 4);
160  if (s != 4) {
161  return false;
162  }
163 
164  if (m_endian == ENDIAN_LITTLE) {
165  v = EL32(buf);
166  }
167  else {
168  v = BE32(buf);
169  }
170 
171  return true;
172 }
173 
174 
175 size_t
176 RawContainer::fetchData(void *buf, off_t _offset,
177  size_t buf_size)
178 {
179  size_t s;
180  m_file->seek(_offset, SEEK_SET);
181  s = m_file->read(buf, buf_size);
182  return s;
183 }
184 
185 
186 }
187 }
188 /*
189  Local Variables:
190  mode:c++
191  c-file-style:"stroustrup"
192  c-file-offsets:((innamespace . 0))
193  tab-width:2
194  c-basic-offset:2
195  indent-tabs-mode:nil
196  fill-column:80
197  End:
198 */
size_t fetchData(void *buf, off_t offset, size_t buf_size)
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard. I guess it failed.
Definition: arwfile.cpp:30
bool readUInt32(const IO::Stream::Ptr &f, uint32_t &v)
Definition: trace.cpp:30
bool readUInt16(const IO::Stream::Ptr &f, uint16_t &v)
bool readInt32(const IO::Stream::Ptr &f, int32_t &v)
bool readInt16(const IO::Stream::Ptr &f, int16_t &v)