libopenraw
rawcontainer.cpp
1 /*
2  * libopenraw - rawcontainer.cpp
3  *
4  * Copyright (C) 2006-2007 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 <iostream>
22 
23 #include <libopenraw/types.h>
24 
25 #include "trace.h"
26 #include "endianutils.h"
27 #include "io/file.h"
28 #include "rawcontainer.h"
29 
30 
31 
32 using namespace Debug;
33 
34 namespace OpenRaw {
35  namespace Internals {
36 
37 
38  RawContainer::RawContainer(IO::Stream *_file, off_t offset)
39  : m_file(_file),
40  m_offset(offset),
41  m_endian(ENDIAN_NULL)
42  {
43  m_file->open();
44  m_file->seek(offset, SEEK_SET);
45  }
46 
47 
49  {
50  m_file->close();
51  }
52 
53 
54  bool RawContainer::readInt8(IO::Stream *f, int8_t & v)
55  {
56  unsigned char buf;
57  int s = f->read(&buf, 1);
58  if (s != 1) {
59  return false;
60  }
61  v = buf;
62  return true;
63  }
64 
65  bool RawContainer::readUInt8(IO::Stream *f, uint8_t & v)
66  {
67  unsigned char buf;
68  int s = f->read(&buf, 1);
69  if (s != 1) {
70  return false;
71  }
72  v = buf;
73  return true;
74  }
75 
76  bool
78  {
79  if (m_endian == ENDIAN_NULL) {
80 
81  Trace(ERROR) << "null endian\n";
82 
83  return false;
84  }
85  unsigned char buf[2];
86  int s = f->read(buf, 2);
87  if (s != 2) {
88  return false;
89  }
90  if (m_endian == ENDIAN_LITTLE) {
91  v = EL16(buf);
92  }
93  else {
94  v = BE16(buf);
95  }
96  return true;
97  }
98 
99 
100  bool
102  {
103  if (m_endian == ENDIAN_NULL) {
104 
105  Trace(ERROR) << "null endian\n";
106 
107  return false;
108  }
109  unsigned char buf[4];
110  int s = f->read(buf, 4);
111  if (s != 4) {
112  Trace(ERROR) << "read " << s << " bytes\n";
113  return false;
114  }
115 
116  if (m_endian == ENDIAN_LITTLE) {
117  v = EL32(buf);
118  }
119  else {
120  v = BE32(buf);
121  }
122 
123  return true;
124  }
125 
126 
127  bool
129  {
130  if (m_endian == ENDIAN_NULL) {
131 
132  Trace(ERROR) << "null endian\n";
133 
134  return false;
135  }
136  unsigned char buf[2];
137  int s = f->read(buf, 2);
138  if (s != 2) {
139  return false;
140  }
141  if (m_endian == ENDIAN_LITTLE) {
142  v = EL16(buf);
143  }
144  else {
145  v = BE16(buf);
146  }
147  return true;
148  }
149 
150 
151  bool
153  {
154  if (m_endian == ENDIAN_NULL) {
155 
156  Trace(ERROR) << "null endian\n";
157 
158  return false;
159  }
160  unsigned char buf[4];
161  int s = f->read(buf, 4);
162  if (s != 4) {
163  return false;
164  }
165 
166  if (m_endian == ENDIAN_LITTLE) {
167  v = EL32(buf);
168  }
169  else {
170  v = BE32(buf);
171  }
172 
173  return true;
174  }
175 
176 
177  size_t
178  RawContainer::fetchData(void *buf, const off_t offset,
179  const size_t buf_size)
180  {
181  size_t s;
182  m_file->seek(offset, SEEK_SET);
183  s = m_file->read(buf, buf_size);
184  return s;
185  }
186 
187 
188  }
189 }