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 21** 22** See http://www.matroska.org/license/lgpl/ for LGPL licensing information. 23** 24** Contact license@matroska.org if any conditions of this licensing are 25** not clear to you. 26** 27**********************************************************************/ 28 29/*! 30 \file 31 \version \$Id: MemIOCallback.cpp 693 2004-07-31 08:56:28Z robux4 $ 32 \author Jory Stone <jcsston @ toughguy.net> 33*/ 34 35#include "ebml/MemIOCallback.h" 36#include "ebml/Debug.h" 37#include "ebml/EbmlConfig.h" 38 39START_LIBEBML_NAMESPACE 40 41MemIOCallback::MemIOCallback(uint64 DefaultSize) 42{ 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; 52 } 53 54 dataBufferMemorySize = DefaultSize; 55 dataBufferPos = 0; 56 dataBufferTotalSize = 0; 57 mOk = true; 58} 59 60MemIOCallback::~MemIOCallback() 61{ 62 if (dataBuffer != NULL) 63 free(dataBuffer); 64} 65 66uint32 MemIOCallback::read(void *Buffer, size_t Size) 67{ 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) 72 { 73 //We will only return the remaining data 74 memcpy(Buffer, dataBuffer + dataBufferPos, dataBufferTotalSize - dataBufferPos); 75 dataBufferPos = dataBufferTotalSize; 76 return dataBufferTotalSize - dataBufferPos; 77 } 78 79 //Well... We made it here, so do a quick and simple copy 80 memcpy(Buffer, dataBuffer+dataBufferPos, Size); 81 dataBufferPos += Size; 82 83 return Size; 84} 85 86void MemIOCallback::setFilePointer(int64 Offset, seek_mode Mode) 87{ 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; 94} 95 96size_t MemIOCallback::write(const void *Buffer, size_t Size) 97{ 98 if (dataBufferMemorySize < dataBufferPos + Size) 99 { 100 //We need more memory! 101 dataBuffer = (binary *)realloc((void *)dataBuffer, dataBufferPos + Size); 102 } 103 memcpy(dataBuffer+dataBufferPos, Buffer, Size); 104 dataBufferPos += Size; 105 if (dataBufferPos > dataBufferTotalSize) 106 dataBufferTotalSize = dataBufferPos; 107 108 return Size; 109} 110 111uint32 MemIOCallback::write(IOCallback & IOToRead, size_t Size) 112{ 113 if (dataBufferMemorySize < dataBufferPos + Size) 114 { 115 //We need more memory! 116 dataBuffer = (binary *)realloc((void *)dataBuffer, dataBufferPos + Size); 117 } 118 IOToRead.readFully(&dataBuffer[dataBufferPos], Size); 119 dataBufferTotalSize = Size; 120 return Size; 121} 122 123END_LIBEBML_NAMESPACE 124