]> git.sesse.net Git - vlc/blob - src/input/mem_stream.c
Make stream_Peek take a const pointer as it should
[vlc] / src / input / mem_stream.c
1 /*****************************************************************************
2  * mem_stream.c: stream_t wrapper around memory buffer
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26
27 #include "input_internal.h"
28
29 struct stream_sys_t
30 {
31     vlc_bool_t  i_preserve_memory;
32     int64_t     i_pos;      /* Current reading offset */
33     int64_t     i_size;
34     uint8_t    *p_buffer;
35
36 };
37
38 static int  Read   ( stream_t *, void *p_read, int i_read );
39 static int  Peek   ( stream_t *, const uint8_t **pp_peek, int i_read );
40 static int  Control( stream_t *, int i_query, va_list );
41 static void Delete ( stream_t * );
42
43 /**
44  * Create a stream from a memory buffer
45  *
46  * \param p_this the calling vlc_object
47  * \param p_buffer the memory buffer for the stream
48  * \param i_buffer the size of the buffer
49  * \param i_preserve_memory if this is set to VLC_FALSE the memory buffer
50  *        pointed to by p_buffer is freed on stream_Destroy
51  */
52 stream_t *__stream_MemoryNew( vlc_object_t *p_this, uint8_t *p_buffer,
53                               int64_t i_size, vlc_bool_t i_preserve_memory )
54 {
55     stream_t *s = vlc_stream_create( p_this );
56     stream_sys_t *p_sys;
57
58     if( !s ) return NULL;
59
60     s->p_sys = p_sys = malloc( sizeof( stream_sys_t ) );
61     p_sys->i_pos = 0;
62     p_sys->i_size = i_size;
63     p_sys->p_buffer = p_buffer;
64     p_sys->i_preserve_memory = i_preserve_memory;
65
66     s->pf_block   = NULL;
67     s->pf_read    = Read;
68     s->pf_peek    = Peek;
69     s->pf_control = Control;
70     s->pf_destroy = Delete;
71
72     s->i_char_width = 1;
73     s->b_little_endian = VLC_FALSE;
74     vlc_object_attach( s, p_this );
75
76     return s;
77 }
78
79 static void Delete( stream_t *s )
80 {
81     if( !s->p_sys->i_preserve_memory ) free( s->p_sys->p_buffer );
82     free( s->p_sys );
83     vlc_object_detach( s );
84     vlc_object_destroy( s );
85 }
86
87 /****************************************************************************
88  * AStreamControl:
89  ****************************************************************************/
90 static int Control( stream_t *s, int i_query, va_list args )
91 {
92     stream_sys_t *p_sys = s->p_sys;
93
94     vlc_bool_t *p_bool;
95     int64_t    *pi_64, i_64;
96     int        i_int;
97
98     switch( i_query )
99     {
100         case STREAM_GET_SIZE:
101             pi_64 = (int64_t*)va_arg( args, int64_t * );
102             *pi_64 = p_sys->i_size;
103             break;
104
105         case STREAM_CAN_SEEK:
106             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
107             *p_bool = VLC_TRUE;
108             break;
109
110         case STREAM_CAN_FASTSEEK:
111             p_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
112             *p_bool = VLC_TRUE;
113             break;
114
115         case STREAM_GET_POSITION:
116             pi_64 = (int64_t*)va_arg( args, int64_t * );
117             *pi_64 = p_sys->i_pos;
118             break;
119
120         case STREAM_SET_POSITION:
121             i_64 = (int64_t)va_arg( args, int64_t );
122             i_64 = __MAX( i_64, 0 );
123             i_64 = __MIN( i_64, s->p_sys->i_size ); 
124             p_sys->i_pos = i_64;
125             break;
126
127         case STREAM_GET_MTU:
128             return VLC_EGENERIC;
129
130         case STREAM_CONTROL_ACCESS:
131             i_int = (int) va_arg( args, int );
132             msg_Err( s, "Hey, what are you thinking ?"
133                      "DON'T USE STREAM_CONTROL_ACCESS !!!" );
134             return VLC_EGENERIC;
135
136         default:
137             msg_Err( s, "invalid stream_vaControl query=0x%x", i_query );
138             return VLC_EGENERIC;
139     }
140     return VLC_SUCCESS;
141 }
142
143 static int Read( stream_t *s, void *p_read, int i_read )
144 {
145     stream_sys_t *p_sys = s->p_sys;
146     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
147     memcpy( p_read, p_sys->p_buffer + p_sys->i_pos, i_res );
148     p_sys->i_pos += i_res;
149     return i_res;
150 }
151
152 static int Peek( stream_t *s, const uint8_t **pp_peek, int i_read )
153 {
154     stream_sys_t *p_sys = s->p_sys;
155     int i_res = __MIN( i_read, p_sys->i_size - p_sys->i_pos );
156     *pp_peek = p_sys->p_buffer + p_sys->i_pos;
157     return i_res;
158 }