1 /*****************************************************************************
2 * buffer.c: MMS access plug-in
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_charset.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;
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
)
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
);
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;
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( var_buffer_t
*p_buf
, const char *p_str
)
115 #ifdef WORDS_BIGENDIAN
116 p_out
= ToCharset( "UTF-16BE", p_str
, &i_out
);
118 p_out
= ToCharset( "UTF-16LE", p_str
, &i_out
);
126 for( size_t i
= 0; i
< i_out
; i
++ )
127 var_buffer_add16( p_buf
, p_out
[i
] );
130 var_buffer_add16( p_buf
, 0 );
133 void var_buffer_free( var_buffer_t
*p_buf
)
135 free( p_buf
->p_data
);
140 void var_buffer_initread( var_buffer_t
*p_buf
, void *p_data
, int i_data
)
142 p_buf
->i_size
= i_data
;
144 p_buf
->p_data
= p_data
;
147 uint8_t var_buffer_get8 ( var_buffer_t
*p_buf
)
150 if( p_buf
->i_data
>= p_buf
->i_size
)
154 i_byte
= p_buf
->p_data
[p_buf
->i_data
];
159 uint16_t var_buffer_get16( var_buffer_t
*p_buf
)
163 i_b1
= var_buffer_get8( p_buf
);
164 i_b2
= var_buffer_get8( p_buf
);
166 return( i_b1
+ ( i_b2
<< 8 ) );
170 uint32_t var_buffer_get32( var_buffer_t
*p_buf
)
174 i_w1
= var_buffer_get16( p_buf
);
175 i_w2
= var_buffer_get16( p_buf
);
177 return( i_w1
+ ( i_w2
<< 16 ) );
180 uint64_t var_buffer_get64( var_buffer_t
*p_buf
)
182 uint64_t i_dw1
, i_dw2
;
184 i_dw1
= var_buffer_get32( p_buf
);
185 i_dw2
= var_buffer_get32( p_buf
);
187 return( i_dw1
+ ( i_dw2
<< 32 ) );
190 int var_buffer_getmemory ( var_buffer_t
*p_buf
, void *p_mem
, int64_t i_mem
)
194 i_copy
= __MIN( i_mem
, p_buf
->i_size
- p_buf
->i_data
);
195 if( i_copy
> 0 && p_mem
!= NULL
)
197 memcpy( p_mem
, p_buf
+ p_buf
->i_data
, i_copy
);
203 p_buf
->i_data
+= i_copy
;
207 int var_buffer_readempty( var_buffer_t
*p_buf
)
209 return( ( p_buf
->i_data
>= p_buf
->i_size
) ? 1 : 0 );
212 void var_buffer_getguid( var_buffer_t
*p_buf
, guid_t
*p_guid
)
216 p_guid
->Data1
= var_buffer_get32( p_buf
);
217 p_guid
->Data2
= var_buffer_get16( p_buf
);
218 p_guid
->Data3
= var_buffer_get16( p_buf
);
220 for( i
= 0; i
< 8; i
++ )
222 p_guid
->Data4
[i
] = var_buffer_get8( p_buf
);