vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / matroska / libMatroskaParser / StreamIO.cpp
blobfafd8b2fb0f206d94303556cdd29832272ef04ac
1 /*
2 * Copyright (c) 2004, Marcus Overhagen
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <stdio.h>
26 #include <malloc.h>
28 #include "StreamIO.h"
30 struct StreamIO {
31 InputStream filecache;
32 BPositionIO *source;
35 static int
36 stream_read(struct InputStream *cc, ulonglong pos, void *buffer, int count)
38 return reinterpret_cast<StreamIO *>(cc)->source->ReadAt(pos, buffer, count);
41 // Only needed if resync required.
42 static longlong
43 stream_scan(struct InputStream *cc, ulonglong start, unsigned signature)
45 return -1;
48 //static void
49 //stream_close(struct InputStream *cc)
50 //{
51 //}
53 // Size of a buffer
54 // we might want to test against bytes remaining in file.
55 static unsigned
56 stream_getsize(struct InputStream *cc)
58 return 524288;
61 static const char *
62 stream_geterror(struct InputStream *cc)
64 return "";
67 /* memory allocation */
68 static void *
69 stream_memalloc(struct InputStream *cc, size_t size)
71 return malloc(size);
74 static void *
75 stream_memrealloc(struct InputStream *cc,void *mem,size_t newsize)
77 return realloc(mem,newsize);
80 static void
81 stream_memfree(struct InputStream *cc, void *mem)
83 free(mem);
86 // zero return causes parser to abort open
87 static int
88 stream_progress(struct InputStream *cc, ulonglong cur, ulonglong max)
90 // no idea what this function is supposed to do.
91 return max-cur;
94 InputStream *
95 CreateFileCache(BDataIO *dataio)
97 BPositionIO *posio;
98 posio = dynamic_cast<BPositionIO *>(dataio);
99 if (!posio)
100 return NULL;
102 StreamIO *io;
103 io = (StreamIO *)malloc(sizeof(StreamIO));
104 if (!io)
105 return NULL;
107 io->filecache.read = &stream_read;
108 io->filecache.scan = &stream_scan;
109 // io->filecache.close = &stream_close;
110 io->filecache.getcachesize = &stream_getsize;
111 io->filecache.geterror = &stream_geterror;
112 io->filecache.memalloc = &stream_memalloc;
113 io->filecache.memrealloc = &stream_memrealloc;
114 io->filecache.memfree = &stream_memfree;
115 io->filecache.progress = &stream_progress;
116 io->source = posio;
118 return reinterpret_cast<InputStream *>(io);