vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / matroska / libebml / MemIOCallback.cpp
bloba23afda0b566ae14172673550106a35784573144
1 /****************************************************************************
2 ** libebml : parse EBML files, see http://embl.sourceforge.net/
3 **
4 ** <file/class description>
5 **
6 ** Copyright (C) 2003 Jory Stone. All rights reserved.
7 **
8 ** This library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Lesser General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2.1 of the License, or (at your option) any later version.
12 **
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Lesser General Public License for more details.
17 **
18 ** You should have received a copy of the GNU Lesser General Public
19 ** License along with this library; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 ** See http://www.matroska.org/license/lgpl/ for LGPL licensing information.
24 ** Contact license@matroska.org if any conditions of this licensing are
25 ** not clear to you.
27 **********************************************************************/
29 /*!
30 \file
31 \version \$Id: MemIOCallback.cpp 693 2004-07-31 08:56:28Z robux4 $
32 \author Jory Stone <jcsston @ toughguy.net>
35 #include "ebml/MemIOCallback.h"
36 #include "ebml/Debug.h"
37 #include "ebml/EbmlConfig.h"
39 START_LIBEBML_NAMESPACE
41 MemIOCallback::MemIOCallback(uint64 DefaultSize)
43 //The default size of the buffer is 128 bytes
44 dataBuffer = (binary *)malloc(DefaultSize);
45 if (dataBuffer == NULL) {
46 mOk = false;
47 std::stringstream Msg;
48 Msg << "Failed to alloc memory block of size ";
49 // not working with VC6 Msg << DefaultSize;
50 mLastErrorStr = Msg.str();
51 return;
54 dataBufferMemorySize = DefaultSize;
55 dataBufferPos = 0;
56 dataBufferTotalSize = 0;
57 mOk = true;
60 MemIOCallback::~MemIOCallback()
62 if (dataBuffer != NULL)
63 free(dataBuffer);
66 uint32 MemIOCallback::read(void *Buffer, size_t Size)
68 if (Buffer == NULL || Size < 1)
69 return 0;
70 //If the size is larger than than the amount left in the buffer
71 if (Size + dataBufferPos > dataBufferTotalSize)
73 //We will only return the remaining data
74 memcpy(Buffer, dataBuffer + dataBufferPos, dataBufferTotalSize - dataBufferPos);
75 dataBufferPos = dataBufferTotalSize;
76 return dataBufferTotalSize - dataBufferPos;
79 //Well... We made it here, so do a quick and simple copy
80 memcpy(Buffer, dataBuffer+dataBufferPos, Size);
81 dataBufferPos += Size;
83 return Size;
86 void MemIOCallback::setFilePointer(int64 Offset, seek_mode Mode)
88 if (Mode == seek_beginning)
89 dataBufferPos = Offset;
90 else if (Mode == seek_current)
91 dataBufferPos = dataBufferPos + Offset;
92 else if (Mode == seek_end)
93 dataBufferPos = dataBufferTotalSize + Offset;
96 size_t MemIOCallback::write(const void *Buffer, size_t Size)
98 if (dataBufferMemorySize < dataBufferPos + Size)
100 //We need more memory!
101 dataBuffer = (binary *)realloc((void *)dataBuffer, dataBufferPos + Size);
103 memcpy(dataBuffer+dataBufferPos, Buffer, Size);
104 dataBufferPos += Size;
105 if (dataBufferPos > dataBufferTotalSize)
106 dataBufferTotalSize = dataBufferPos;
108 return Size;
111 uint32 MemIOCallback::write(IOCallback & IOToRead, size_t Size)
113 if (dataBufferMemorySize < dataBufferPos + Size)
115 //We need more memory!
116 dataBuffer = (binary *)realloc((void *)dataBuffer, dataBufferPos + Size);
118 IOToRead.readFully(&dataBuffer[dataBufferPos], Size);
119 dataBufferTotalSize = Size;
120 return Size;
123 END_LIBEBML_NAMESPACE