File: rawcontainer.cpp

package info (click to toggle)
libopenraw 0.0.8-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,964 kB
  • ctags: 1,936
  • sloc: sh: 10,199; cpp: 9,279; ansic: 1,466; makefile: 544; xml: 465; perl: 42
file content (189 lines) | stat: -rw-r--r-- 3,363 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * libopenraw - rawcontainer.cpp
 *
 * Copyright (C) 2006-2007 Hubert Figuiere
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <iostream>

#include <libopenraw/types.h>

#include "debug.h"
#include "endianutils.h"
#include "io/file.h"
#include "rawcontainer.h"



using namespace Debug;

namespace OpenRaw {
	namespace Internals {
	
	
		RawContainer::RawContainer(IO::Stream *_file, off_t offset)
			: m_file(_file),
				m_offset(offset),
				m_endian(ENDIAN_NULL)
		{
			m_file->open();
			m_file->seek(offset, SEEK_SET);
		}

	
		RawContainer::~RawContainer()
		{
			m_file->close();
		}


		bool RawContainer::readInt8(IO::Stream *f, int8_t & v)
		{
			unsigned char buf;
			int s = f->read(&buf, 1);
			if (s != 1) {
				return false;
			}
			v = buf;
			return true;
		}

		bool RawContainer::readUInt8(IO::Stream *f, uint8_t & v)
		{
			unsigned char buf;
			int s = f->read(&buf, 1);
			if (s != 1) {
				return false;
			}
			v = buf;
			return true;
		}

		bool 
		RawContainer::readInt16(IO::Stream *f, int16_t & v)
		{
			if (m_endian == ENDIAN_NULL) {

				Trace(ERROR) << "null endian\n";

				return false;
			}
			unsigned char buf[2];
			int s = f->read(buf, 2);
			if (s != 2) {
				return false;
			}
			if (m_endian == ENDIAN_LITTLE) {
				v = EL16(buf);
			}
			else {
				v = BE16(buf);
			}
			return true;
		}


		bool 
		RawContainer::readInt32(IO::Stream *f, int32_t & v)
		{
			if (m_endian == ENDIAN_NULL) {

				Trace(ERROR) << "null endian\n";

				return false;
			}
			unsigned char buf[4];
			int s = f->read(buf, 4);
			if (s != 4) {
				Trace(ERROR) << "read " << s << " bytes\n";
				return false;
			}

			if (m_endian == ENDIAN_LITTLE) {
				v = EL32(buf);
			}
			else {
				v = BE32(buf);
			}

			return true;
		}


		bool 
		RawContainer::readUInt16(IO::Stream *f, uint16_t & v)
		{
			if (m_endian == ENDIAN_NULL) {

				Trace(ERROR) << "null endian\n";

				return false;
			}
			unsigned char buf[2];
			int s = f->read(buf, 2);
			if (s != 2) {
				return false;
			}
			if (m_endian == ENDIAN_LITTLE) {
				v = EL16(buf);
			}
			else {
				v = BE16(buf);
			}
			return true;
		}


		bool 
		RawContainer::readUInt32(IO::Stream *f, uint32_t & v)
		{
			if (m_endian == ENDIAN_NULL) {

				Trace(ERROR) << "null endian\n";

				return false;
			}
			unsigned char buf[4];
			int s = f->read(buf, 4);
			if (s != 4) {
				return false;
			}

			if (m_endian == ENDIAN_LITTLE) {
				v = EL32(buf);
			}
			else {
 				v = BE32(buf);
			}

			return true;
		}


		size_t 
		RawContainer::fetchData(void *buf, const off_t offset,
														const size_t buf_size)
		{
			size_t s;
			m_file->seek(offset, SEEK_SET);
			s = m_file->read(buf, buf_size);
			return s;
		}


	}
}