demux: ts: fix potential null deref (cid #1412981)
[vlc.git] / src / input / stream_memory.c
blobf6b220c946631c6aaa4b41a2697e83b291b2a5f2
1 /*****************************************************************************
2 * stream_memory.c: stream_t wrapper around memory buffer
3 *****************************************************************************
4 * Copyright (C) 1999-2008 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include "stream.h"
30 struct stream_sys_t
32 size_t i_pos; /* Current reading offset */
33 size_t i_size;
34 uint8_t *p_buffer;
37 static ssize_t Read( stream_t *, void *p_read, size_t i_read );
38 static int Seek( stream_t *, uint64_t );
39 static int Control( stream_t *, int i_query, va_list );
41 static void stream_MemoryPreserveDelete(stream_t *s)
43 free(s->p_sys);
46 static void stream_MemoryDelete(stream_t *s)
48 stream_sys_t *sys = s->p_sys;
50 free(sys->p_buffer);
51 stream_MemoryPreserveDelete(s);
54 stream_t *(vlc_stream_MemoryNew)(vlc_object_t *p_this, uint8_t *p_buffer,
55 size_t i_size, bool preserve)
57 stream_t *s = vlc_stream_CommonNew( p_this,
58 preserve ? stream_MemoryPreserveDelete
59 : stream_MemoryDelete );
60 stream_sys_t *p_sys;
62 if( !s )
63 return NULL;
65 s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
66 if( !s->p_sys )
68 stream_CommonDelete( s );
69 return NULL;
71 p_sys->i_pos = 0;
72 p_sys->i_size = i_size;
73 p_sys->p_buffer = p_buffer;
75 s->pf_read = Read;
76 s->pf_seek = Seek;
77 s->pf_control = Control;
79 return s;
82 /****************************************************************************
83 * AStreamControl:
84 ****************************************************************************/
85 static int Control( stream_t *s, int i_query, va_list args )
87 stream_sys_t *p_sys = s->p_sys;
89 uint64_t *pi_64;
91 switch( i_query )
93 case STREAM_GET_SIZE:
94 pi_64 = va_arg( args, uint64_t * );
95 *pi_64 = p_sys->i_size;
96 break;
98 case STREAM_CAN_SEEK:
99 case STREAM_CAN_FASTSEEK:
100 case STREAM_CAN_PAUSE:
101 case STREAM_CAN_CONTROL_PACE:
102 *va_arg( args, bool * ) = true;
103 break;
105 case STREAM_GET_PTS_DELAY:
106 *va_arg( args, int64_t * ) = 0;
107 break;
109 case STREAM_GET_TITLE_INFO:
110 case STREAM_GET_TITLE:
111 case STREAM_GET_SEEKPOINT:
112 case STREAM_GET_META:
113 case STREAM_GET_CONTENT_TYPE:
114 case STREAM_GET_SIGNAL:
115 case STREAM_SET_TITLE:
116 case STREAM_SET_SEEKPOINT:
117 return VLC_EGENERIC;
119 case STREAM_SET_PAUSE_STATE:
120 break; /* nothing to do */
122 case STREAM_SET_PRIVATE_ID_STATE:
123 case STREAM_SET_PRIVATE_ID_CA:
124 case STREAM_GET_PRIVATE_ID_STATE:
125 msg_Err( s, "Hey, what are you thinking? "
126 "DO NOT USE PRIVATE STREAM CONTROLS!!!" );
127 return VLC_EGENERIC;
129 default:
130 msg_Err( s, "invalid vlc_stream_vaControl query=0x%x", i_query );
131 return VLC_EGENERIC;
133 return VLC_SUCCESS;
136 static ssize_t Read( stream_t *s, void *p_read, size_t i_read )
138 stream_sys_t *p_sys = s->p_sys;
140 if( i_read > p_sys->i_size - p_sys->i_pos )
141 i_read = p_sys->i_size - p_sys->i_pos;
142 if ( p_read )
143 memcpy( p_read, p_sys->p_buffer + p_sys->i_pos, i_read );
144 p_sys->i_pos += i_read;
145 return i_read;
148 static int Seek( stream_t *s, uint64_t offset )
150 stream_sys_t *p_sys = s->p_sys;
152 if( offset > p_sys->i_size )
153 offset = p_sys->i_size;
155 p_sys->i_pos = offset;
156 return VLC_SUCCESS;