This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
DataHandlerCallback.cpp
286 lines (236 loc) · 6.38 KB
/
DataHandlerCallback.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* DataHandlerCallback.cpp
*
* DataHandlerCallback.cpp - file I/O for libebml.
*
*
* Copyright (c) 2006 David Conrad
*
* This program 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;
* version 2.1 of the License.
*
* This program 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 program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "DataHandlerCallback.h"
#include <QuickTime/QuickTime.h>
#include <string>
#include "ebml/Debug.h"
#include "ebml/EbmlConfig.h"
#include "ebml/StdIOCallback.h"
using namespace std;
DataHBuffer::DataHBuffer(size_t bufferSize)
{
buffer = new uint8_t[bufferSize];
allocatedSize = bufferSize;
fileOffset = -1;
dataSize = 0;
}
DataHBuffer::~DataHBuffer()
{
if (buffer)
delete[] buffer;
}
void DataHBuffer::Realloc(size_t bufferSize)
{
if (buffer)
delete[] buffer;
buffer = new uint8_t[bufferSize];
allocatedSize = bufferSize;
fileOffset = -1;
dataSize = 0;
}
bool DataHBuffer::ContainsOffset(uint64_t offset)
{
return offset >= fileOffset && offset < fileOffset + dataSize;
}
uint8_t * DataHBuffer::GetBuffer(uint64_t offset, size_t size)
{
if (size > allocatedSize)
Realloc(size);
fileOffset = offset;
dataSize = size;
return buffer;
}
size_t DataHBuffer::Read(uint64_t offset, size_t size, uint8_t *store)
{
if (!ContainsOffset(offset))
return 0;
uint8_t *dataStart = buffer + (offset - fileOffset);
size_t amountToRead = MIN(fileOffset + dataSize - offset, size);
memcpy(store, dataStart, amountToRead);
return amountToRead;
}
DataHandlerCallback::DataHandlerCallback(ComponentInstance dataHandler, const open_mode aMode)
{
Initialize(aMode);
switch (aMode)
{
case MODE_READ:
case MODE_WRITE:
this->dataHandler = dataHandler;
break;
default:
throw 0;
break;
}
// figure out if we support wide offesets
getFileSize();
}
DataHandlerCallback::DataHandlerCallback(Handle dataRef, OSType dataRefType, const open_mode aMode)
{
ComponentResult err = noErr;
Component dataHComponent = NULL;
Initialize(aMode);
if (aMode == MODE_READ)
dataHComponent = GetDataHandler(dataRef, dataRefType, kDataHCanRead);
else if (aMode == MODE_WRITE)
dataHComponent = GetDataHandler(dataRef, dataRefType, kDataHCanWrite);
else
throw 0;
err = OpenAComponent(dataHComponent, &dataHandler);
if (err) {
throw CRTError("Error opening data handler component", err);
}
err = DataHSetDataRef(dataHandler, dataRef);
if (err) {
throw CRTError("Error setting data handler ref", err);
}
if (aMode == MODE_READ) {
err = DataHOpenForRead(dataHandler);
if (err) {
throw CRTError("Error opening data handler for read", err);
}
} else if (aMode == MODE_WRITE) {
err = DataHOpenForWrite(dataHandler);
if (err) {
throw CRTError("Error opening data handler for write", err);
}
} else {
throw 0;
}
closeHandler = true;
// figure out if we support wide offesets
getFileSize();
}
void DataHandlerCallback::Initialize(const open_mode aMode)
{
closeHandler = false;
supportsWideOffsets = true;
this->dataHandler = NULL;
mCurrentPosition = 0;
filesize = 0;
this->aMode = aMode;
}
DataHandlerCallback::~DataHandlerCallback() throw()
{
close();
}
uint32 DataHandlerCallback::read(void *buffer, size_t size)
{
ComponentResult err = noErr;
size_t amountRead = 0;
uint64_t oldPos = mCurrentPosition;
uint8_t *internalBuffer, *myBuffer = (uint8_t *)buffer;
if (mCurrentPosition + size > filesize)
getFileSize();
if (size < 1 || mCurrentPosition > filesize)
return 0;
if (mCurrentPosition + size > filesize)
size = filesize - mCurrentPosition;
while (size > 0) {
if (dataBuffer.ContainsOffset(mCurrentPosition)) {
amountRead = dataBuffer.Read(mCurrentPosition, size, myBuffer);
myBuffer += amountRead;
mCurrentPosition += amountRead;
size -= amountRead;
}
if (size <= 0)
break;
size_t readSize = MIN(READ_SIZE, filesize - mCurrentPosition);
internalBuffer = dataBuffer.GetBuffer(mCurrentPosition, readSize);
if (supportsWideOffsets) {
wide wideOffset = SInt64ToWide(mCurrentPosition);
err = DataHScheduleData64(dataHandler, (Ptr)internalBuffer, &wideOffset,
readSize, 0, NULL, NULL);
} else {
err = DataHScheduleData(dataHandler, (Ptr)internalBuffer, mCurrentPosition,
readSize, 0, NULL, NULL);
}
if (err) {
throw CRTError("Error reading data", err);
}
}
return mCurrentPosition - oldPos;
}
void DataHandlerCallback::setFilePointer(int64 Offset, seek_mode Mode)
{
switch ( Mode )
{
case SEEK_CUR:
mCurrentPosition += Offset;
break;
case SEEK_END:
// I think this is what seeking this way does (was ftell(File))
mCurrentPosition = getFileSize() + Offset;
break;
case SEEK_SET:
mCurrentPosition = Offset;
break;
}
}
size_t DataHandlerCallback::write(const void *Buffer, size_t Size)
{
ComponentResult err = noErr;
if (supportsWideOffsets) {
wide wideOffset = SInt64ToWide(mCurrentPosition);
err = DataHWrite64(dataHandler, (Ptr)Buffer, &wideOffset, Size, NULL, 0);
} else {
err = DataHWrite(dataHandler, (Ptr)Buffer, mCurrentPosition, Size, NULL, 0);
}
if (err) {
throw CRTError("Error writing data", err);
}
mCurrentPosition += Size;
// does QT tell us how much it writes?
return Size;
}
uint64 DataHandlerCallback::getFilePointer()
{
return mCurrentPosition;
}
void DataHandlerCallback::close()
{
if (closeHandler) {
if (aMode == MODE_READ)
DataHCloseForRead(dataHandler);
else if (aMode == MODE_WRITE)
DataHCloseForWrite(dataHandler);
dataHandler = NULL;
}
}
SInt64 DataHandlerCallback::getFileSize()
{
ComponentResult err = noErr;
wide wideFilesize;
err = DataHGetFileSize64(dataHandler, &wideFilesize);
if (err == noErr) {
supportsWideOffsets = true;
filesize = WideToSInt64(wideFilesize);
} else {
long size32;
supportsWideOffsets = false;
DataHGetFileSize(dataHandler, &size32);
filesize = size32;
}
return filesize;
}