contrib: cargo: use cargo/vendored-openssl if needed
[vlc.git] / modules / access / mms / buffer.c
blobe5d3c4843b97b39c4dbc07d43ae8a866ab4f4383
1 /*****************************************************************************
2 * buffer.c: MMS access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program 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
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_access.h>
33 #include <vlc_charset.h>
35 #include "asf.h"
36 #include "buffer.h"
38 /*****************************************************************************
39 * Buffer management functions
40 *****************************************************************************/
41 int var_buffer_initwrite( var_buffer_t *p_buf, int i_default_size )
43 p_buf->i_size = ( i_default_size > 0 ) ? i_default_size : 2048;
44 p_buf->i_data = 0;
45 p_buf->p_data = malloc( p_buf->i_size );
46 return p_buf->p_data ? 0 : -1;
49 int var_buffer_reinitwrite( var_buffer_t *p_buf, int i_default_size )
51 p_buf->i_data = 0;
52 if( p_buf->i_size < i_default_size )
54 p_buf->i_size = i_default_size;
55 free( p_buf->p_data );
56 p_buf->p_data = malloc( p_buf->i_size );
58 if( !p_buf->p_data )
60 p_buf->i_size = ( i_default_size > 0 ) ? i_default_size : 2048;
61 p_buf->p_data = malloc( p_buf->i_size );
63 return p_buf->p_data ? 0 : -1;
66 void var_buffer_add8 ( var_buffer_t *p_buf, uint8_t i_byte )
68 /* check if there is enough data */
69 if( p_buf->i_data >= p_buf->i_size )
71 p_buf->i_size += 1024;
72 p_buf->p_data = xrealloc( p_buf->p_data, p_buf->i_size );
74 p_buf->p_data[p_buf->i_data] = i_byte&0xff;
75 p_buf->i_data++;
78 void var_buffer_add16( var_buffer_t *p_buf, uint16_t i_word )
80 var_buffer_add8( p_buf, i_word&0xff );
81 var_buffer_add8( p_buf, ( i_word >> 8 )&0xff );
84 void var_buffer_add32( var_buffer_t *p_buf, uint32_t i_dword )
86 var_buffer_add16( p_buf, i_dword&0xffff );
87 var_buffer_add16( p_buf, ( i_dword >> 16 )&0xffff );
90 void var_buffer_add64( var_buffer_t *p_buf, uint64_t i_long )
92 var_buffer_add32( p_buf, i_long&0xffffffff );
93 var_buffer_add32( p_buf, ( i_long >> 32 )&0xffffffff );
96 void var_buffer_addmemory( var_buffer_t *p_buf, void *p_mem, int i_mem )
98 /* check if there is enough data */
99 if( p_buf->i_data + i_mem >= p_buf->i_size )
101 p_buf->i_size += i_mem + 1024;
102 p_buf->p_data = xrealloc( p_buf->p_data, p_buf->i_size );
105 memcpy( p_buf->p_data + p_buf->i_data, p_mem, i_mem );
106 p_buf->i_data += i_mem;
109 void var_buffer_addUTF16( stream_t *p_access, var_buffer_t *p_buf, const char *p_str )
111 uint16_t *p_out;
112 size_t i_out;
114 if( p_str != NULL )
115 #ifdef WORDS_BIGENDIAN
116 p_out = ToCharset( "UTF-16BE", p_str, &i_out );
117 #else
118 p_out = ToCharset( "UTF-16LE", p_str, &i_out );
119 #endif
120 else
121 p_out = NULL;
122 if( p_out == NULL )
124 msg_Err( p_access, "UTF-16 conversion failed" );
125 i_out = 0;
128 i_out /= 2;
129 for( size_t i = 0; i < i_out; i ++ )
130 var_buffer_add16( p_buf, p_out[i] );
131 free( p_out );
133 var_buffer_add16( p_buf, 0 );
136 void var_buffer_free( var_buffer_t *p_buf )
138 free( p_buf->p_data );
139 p_buf->i_data = 0;
140 p_buf->i_size = 0;
143 void var_buffer_initread( var_buffer_t *p_buf, void *p_data, int i_data )
145 p_buf->i_size = i_data;
146 p_buf->i_data = 0;
147 p_buf->p_data = p_data;
150 uint8_t var_buffer_get8 ( var_buffer_t *p_buf )
152 uint8_t i_byte;
153 if( p_buf->i_data >= p_buf->i_size )
155 return( 0 );
157 i_byte = p_buf->p_data[p_buf->i_data];
158 p_buf->i_data++;
159 return( i_byte );
162 uint16_t var_buffer_get16( var_buffer_t *p_buf )
164 uint16_t i_b1, i_b2;
166 i_b1 = var_buffer_get8( p_buf );
167 i_b2 = var_buffer_get8( p_buf );
169 return( i_b1 + ( i_b2 << 8 ) );
173 uint32_t var_buffer_get32( var_buffer_t *p_buf )
175 uint32_t i_w1, i_w2;
177 i_w1 = var_buffer_get16( p_buf );
178 i_w2 = var_buffer_get16( p_buf );
180 return( i_w1 + ( i_w2 << 16 ) );
183 uint64_t var_buffer_get64( var_buffer_t *p_buf )
185 uint64_t i_dw1, i_dw2;
187 i_dw1 = var_buffer_get32( p_buf );
188 i_dw2 = var_buffer_get32( p_buf );
190 return( i_dw1 + ( i_dw2 << 32 ) );
193 int var_buffer_getmemory ( var_buffer_t *p_buf, void *p_mem, int64_t i_mem )
195 int i_copy;
197 i_copy = __MIN( i_mem, p_buf->i_size - p_buf->i_data );
198 if( i_copy > 0 && p_mem != NULL)
200 memcpy( p_mem, p_buf->p_data + p_buf->i_data , i_copy );
202 if( i_copy < 0 )
204 i_copy = 0;
206 p_buf->i_data += i_copy;
207 return( i_copy );
210 int var_buffer_readempty( var_buffer_t *p_buf )
212 return( ( p_buf->i_data >= p_buf->i_size ) ? 1 : 0 );
215 void var_buffer_getguid( var_buffer_t *p_buf, vlc_guid_t *p_guid )
217 p_guid->Data1 = var_buffer_get32( p_buf );
218 p_guid->Data2 = var_buffer_get16( p_buf );
219 p_guid->Data3 = var_buffer_get16( p_buf );
221 for( int i = 0; i < 8; i++ )
222 p_guid->Data4[i] = var_buffer_get8( p_buf );