vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / matroska / libebml / ebml / IOCallback.h
blob50a7421634ac049ac723450095815502c9771329
1 /****************************************************************************
2 ** libebml : parse EBML files, see http://embl.sourceforge.net/
3 **
4 ** <file/class description>
5 **
6 ** Copyright (C) 2002-2004 Ingo Ralf Blum. 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: IOCallback.h 639 2004-07-09 20:59:14Z mosu $
33 #ifndef MATROSKA_IOCALLBACK_H
34 #define MATROSKA_IOCALLBACK_H
36 #include "EbmlTypes.h"
38 #include <exception>
39 #include <cstdio>
40 // #include <iostream>
43 START_LIBEBML_NAMESPACE
45 enum seek_mode
47 seek_beginning=SEEK_SET
48 ,seek_end=SEEK_END
49 ,seek_current=SEEK_CUR
52 class EBML_DLL_API IOCallback
54 public:
55 virtual ~IOCallback(){}
57 // The read callback works like most other read functions. You specify the
58 // file, the buffer and the size and the function returns the bytes read.
59 // If an error occurs or the file pointer points to the end of the file 0 is returned.
60 // Users are encouraged to throw a descriptive exception, when an error occurs.
61 virtual uint32 read(void*Buffer,size_t Size)=0;
63 // Seek to the specified position. The mode can have either SEEK_SET, SEEK_CUR
64 // or SEEK_END. The callback should return true(1) if the seek operation succeeded
65 // or false (0), when the seek fails.
66 virtual void setFilePointer(int64 Offset,seek_mode Mode=seek_beginning)=0;
68 // This callback just works like its read pendant. It returns the number of bytes written.
69 virtual size_t write(const void*Buffer,size_t Size)=0;
71 // Although the position is always positive, the return value of this callback is signed to
72 // easily allow negative values for returning errors. When an error occurs, the implementor
73 // should return -1 and the file pointer otherwise.
75 // If an error occurs, an exception should be thrown.
76 virtual uint64 getFilePointer()=0;
78 // The close callback flushes the file buffers to disk and closes the file. When using the stdio
79 // library, this is equivalent to calling fclose. When the close is not successful, an exception
80 // should be thrown.
81 virtual void close()=0;
84 // The readFully is made virtual to allow derived classes to use another
85 // implementation for this method, which e.g. does not read any data
86 // unlike this does
87 void readFully(void*Buffer,size_t Size);
89 template<class STRUCT> void readStruct(STRUCT&Struct){readFully(&Struct,sizeof(Struct));}
91 void writeFully(const void*Buffer,size_t Size);
93 template<class STRUCT> void writeStruct(const STRUCT&Struct){writeFully(&Struct,sizeof(Struct));}
96 /* cygwin incompatible
97 template<class TRAITS> std::basic_ostream<char,TRAITS>&operator<<(std::basic_ostream<char,TRAITS>&Stream,seek_mode Mode)
99 switch(Mode)
101 #define x(y) case seek_##y: Stream<<"seek_" #y; break
102 x(beginning);
103 x(current);
104 x(end);
105 #undef x
106 default:
107 assert(false);
110 return Stream;
114 END_LIBEBML_NAMESPACE
116 #endif // MATROSKA_IOCALLBACK_H