vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / asf_reader / ASFIndex.cpp
blob8a5b032e14092f20dc627d58891152130540c7a0
1 /*
2 * Copyright (c) 2009, David McPaul
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.
26 #include "ASFIndex.h"
28 #include <stdio.h>
31 void
32 IndexEntry::Clear()
34 frameNo=0;
35 noPayloads=0;
36 dataSize=0;
37 pts=0;
38 keyFrame=false;
41 void
42 IndexEntry::AddPayload(uint32 pdataSize)
44 noPayloads++;
45 dataSize += pdataSize;
48 StreamEntry::StreamEntry()
50 lastID = 0;
51 frameCount = 0;
52 maxPTS = 0;
53 duration = 0;
56 StreamEntry::~StreamEntry()
58 index.clear();
61 void
62 StreamEntry::setDuration(bigtime_t pduration)
64 duration = pduration;
68 IndexEntry
69 StreamEntry::GetIndex(uint32 frameNo)
71 IndexEntry indexEntry;
73 for (std::vector<IndexEntry>::iterator itr = index.begin(); itr != index.end(); ++itr) {
74 if (itr->frameNo == frameNo) {
75 indexEntry = *itr;
76 break;
79 return indexEntry;
82 bool
83 StreamEntry::HasIndex(uint32 frameNo)
85 if (!index.empty()) {
86 for (std::vector<IndexEntry>::iterator itr = index.begin(); itr != index.end(); ++itr) {
87 if (itr->frameNo == frameNo) {
88 return true;
93 return false;
96 IndexEntry
97 StreamEntry::GetIndex(bigtime_t pts)
99 IndexEntry indexEntry;
101 for (std::vector<IndexEntry>::iterator itr = index.begin(); itr != index.end(); ++itr) {
102 if (pts <= itr->pts) {
103 indexEntry = *itr;
104 break;
107 return indexEntry;
110 bool
111 StreamEntry::HasIndex(bigtime_t pts)
113 if (!index.empty()) {
114 for (std::vector<IndexEntry>::iterator itr = index.begin(); itr != index.end(); ++itr) {
115 if (pts <= itr->pts) {
116 return true;
121 return false;
125 Combine payloads with the same id into a single IndexEntry
126 When isLast flag is set then no more payloads are available (ie add current entry to index)
129 void
130 StreamEntry::AddPayload(uint32 id, bool keyFrame, bigtime_t pts, uint32 dataSize, bool isLast)
132 if (isLast) {
133 maxPTS = indexEntry.pts;
134 if (frameCount > 0) {
135 index.push_back(indexEntry);
136 // printf("Stream %d added Index %ld PTS %Ld payloads %d\n",streamIndex, indexEntry.frameNo, indexEntry.pts, indexEntry.noPayloads);
137 printf("Stream Index Loaded for Stream %d Max Frame %ld Max PTS %Ld size %ld\n",streamIndex, frameCount-1, maxPTS, index.size());
139 } else {
140 if (id != lastID) {
141 if (frameCount != 0) {
142 // add indexEntry to Index
143 index.push_back(indexEntry);
144 // printf("Stream %d added Index %ld PTS %Ld payloads %d\n",streamIndex, indexEntry.frameNo, indexEntry.pts, indexEntry.noPayloads);
146 lastID = id;
147 indexEntry.Clear();
149 indexEntry.frameNo = frameCount++;
150 indexEntry.keyFrame = keyFrame;
151 indexEntry.pts = pts;
152 indexEntry.dataSize = dataSize;
153 indexEntry.noPayloads = 1;
154 indexEntry.id = id;
155 } else {
156 indexEntry.AddPayload(dataSize);