Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

buffer.c

00001 /*****************************************************************************
00002  * buffer.c: MMS access plug-in
00003  *****************************************************************************
00004  * Copyright (C) 2001-2004 the VideoLAN team
00005  * $Id: buffer.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>
00028 
00029 #include <vlc/vlc.h>
00030 
00031 #include "asf.h"
00032 #include "buffer.h"
00033 
00034 /*****************************************************************************
00035  * Buffer management functions
00036  *****************************************************************************/
00037 int var_buffer_initwrite( var_buffer_t *p_buf, int i_default_size )
00038 {
00039     p_buf->i_size =  ( i_default_size > 0 ) ? i_default_size : 2048;
00040     p_buf->i_data = 0;
00041     if( !( p_buf->p_data = malloc( p_buf->i_size ) ) )
00042     {
00043         return( -1 );
00044     }
00045     return( 0 );
00046 }
00047 
00048 int var_buffer_reinitwrite( var_buffer_t *p_buf, int i_default_size )
00049 {
00050     p_buf->i_data = 0;
00051     if( p_buf->i_size < i_default_size )
00052     {
00053         p_buf->i_size = i_default_size;
00054         if( p_buf->p_data )
00055         {
00056             free( p_buf->p_data );
00057         }
00058         p_buf->p_data = malloc( p_buf->i_size );
00059     }
00060     if( !p_buf->p_data )
00061     {
00062         p_buf->i_size =  ( i_default_size > 0 ) ? i_default_size : 2048;
00063         p_buf->p_data = malloc( p_buf->i_size );
00064     }
00065     if( !p_buf->p_data )
00066     {
00067         return( -1 );
00068     }
00069     return( 0 );
00070 }
00071 
00072 void var_buffer_add8 ( var_buffer_t *p_buf, uint8_t  i_byte )
00073 {
00074     /* check if there is enough data */
00075     if( p_buf->i_data >= p_buf->i_size )
00076     {
00077         p_buf->i_size += 1024;
00078         p_buf->p_data = realloc( p_buf->p_data, p_buf->i_size );
00079     }
00080     p_buf->p_data[p_buf->i_data] = i_byte&0xff;
00081     p_buf->i_data++;
00082 }
00083 
00084 void var_buffer_add16( var_buffer_t *p_buf, uint16_t i_word )
00085 {
00086     var_buffer_add8( p_buf, i_word&0xff );
00087     var_buffer_add8( p_buf, ( i_word >> 8 )&0xff );
00088 }
00089 
00090 void var_buffer_add32( var_buffer_t *p_buf, uint32_t i_dword )
00091 {
00092     var_buffer_add16( p_buf, i_dword&0xffff );
00093     var_buffer_add16( p_buf, ( i_dword >> 16 )&0xffff );
00094 }
00095 
00096 void var_buffer_add64( var_buffer_t *p_buf, uint64_t i_long )
00097 {
00098     var_buffer_add32( p_buf, i_long&0xffffffff );
00099     var_buffer_add32( p_buf, ( i_long >> 32 )&0xffffffff );
00100 }
00101 
00102 
00103 void var_buffer_addmemory( var_buffer_t *p_buf, void *p_mem, int i_mem )
00104 {
00105     /* check if there is enough data */
00106     if( p_buf->i_data + i_mem >= p_buf->i_size )
00107     {
00108         p_buf->i_size += i_mem + 1024;
00109         p_buf->p_data = realloc( p_buf->p_data, p_buf->i_size );
00110     }
00111 
00112     memcpy( p_buf->p_data + p_buf->i_data,
00113             p_mem,
00114             i_mem );
00115     p_buf->i_data += i_mem;
00116 }
00117 
00118 void var_buffer_addUTF16( var_buffer_t *p_buf, char *p_str )
00119 {
00120     unsigned int i;
00121     if( !p_str )
00122     {
00123         var_buffer_add16( p_buf, 0 );
00124     }
00125     else
00126     {
00127         for( i = 0; i < strlen( p_str ) + 1; i++ ) // and 0
00128         {
00129             var_buffer_add16( p_buf, p_str[i] );
00130         }
00131     }
00132 }
00133 
00134 void var_buffer_free( var_buffer_t *p_buf )
00135 {
00136     if( p_buf->p_data )
00137     {
00138         free( p_buf->p_data );
00139     }
00140     p_buf->i_data = 0;
00141     p_buf->i_size = 0;
00142 }
00143 
00144 void var_buffer_initread( var_buffer_t *p_buf, void *p_data, int i_data )
00145 {
00146     p_buf->i_size = i_data;
00147     p_buf->i_data = 0;
00148     p_buf->p_data = p_data;
00149 }
00150 
00151 uint8_t var_buffer_get8 ( var_buffer_t *p_buf )
00152 {
00153     uint8_t  i_byte;
00154     if( p_buf->i_data >= p_buf->i_size )
00155     {
00156         return( 0 );
00157     }
00158     i_byte = p_buf->p_data[p_buf->i_data];
00159     p_buf->i_data++;
00160     return( i_byte );
00161 }
00162 
00163 
00164 uint16_t var_buffer_get16( var_buffer_t *p_buf )
00165 {
00166     uint16_t i_b1, i_b2;
00167 
00168     i_b1 = var_buffer_get8( p_buf );
00169     i_b2 = var_buffer_get8( p_buf );
00170 
00171     return( i_b1 + ( i_b2 << 8 ) );
00172 
00173 }
00174 
00175 uint32_t var_buffer_get32( var_buffer_t *p_buf )
00176 {
00177     uint32_t i_w1, i_w2;
00178 
00179     i_w1 = var_buffer_get16( p_buf );
00180     i_w2 = var_buffer_get16( p_buf );
00181 
00182     return( i_w1 + ( i_w2 << 16 ) );
00183 }
00184 
00185 uint64_t var_buffer_get64( var_buffer_t *p_buf )
00186 {
00187     uint64_t i_dw1, i_dw2;
00188 
00189     i_dw1 = var_buffer_get32( p_buf );
00190     i_dw2 = var_buffer_get32( p_buf );
00191 
00192     return( i_dw1 + ( i_dw2 << 32 ) );
00193 }
00194 
00195 int var_buffer_getmemory ( var_buffer_t *p_buf, void *p_mem, int64_t i_mem )
00196 {
00197     int i_copy;
00198 
00199     i_copy = __MIN( i_mem, p_buf->i_size - p_buf->i_data );
00200     if( i_copy > 0 && p_mem != NULL)
00201     {
00202         memcpy( p_mem, p_buf + p_buf->i_data, i_copy );
00203     }
00204     if( i_copy < 0 )
00205     {
00206 //        fprintf( stderr, "\n**************arrrrrrggggg\n" );
00207         i_copy = 0;
00208     }
00209     p_buf->i_data += i_copy;
00210     return( i_copy );
00211 }
00212 
00213 int var_buffer_readempty( var_buffer_t *p_buf )
00214 {
00215     return( ( p_buf->i_data >= p_buf->i_size ) ? 1 : 0 );
00216 }
00217 
00218 void var_buffer_getguid( var_buffer_t *p_buf, guid_t *p_guid )
00219 {
00220     int i;
00221 
00222     p_guid->v1 = var_buffer_get32( p_buf );
00223     p_guid->v2 = var_buffer_get16( p_buf );
00224     p_guid->v3 = var_buffer_get16( p_buf );
00225 
00226     for( i = 0; i < 8; i++ )
00227     {
00228         p_guid->v4[i] = var_buffer_get8( p_buf );
00229     }
00230 }
00231 

Generated on Tue Dec 20 10:14:26 2005 for vlc-0.8.4a by  doxygen 1.4.2