]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/picomodel/pm_mdc.c
Merge remote-tracking branch 'ttimo/master'
[xonotic/netradiant.git] / libs / picomodel / pm_mdc.c
index 2e4849fbd6aa13d3e46213f9d420c0a5a3d08af9..7e898ff1e3887f2c6d980831959c1d3c313268e1 100644 (file)
@@ -416,22 +416,19 @@ mdc_t;
  */
 
 static int _mdc_canload( PM_PARAMS_CANLOAD ){
-       mdc_t   *mdc;
+       const mdc_t *mdc;
 
 
-       /* to keep the compiler happy */
-       *fileName = *fileName;
-
        /* sanity check */
-       if ( bufSize < ( sizeof( *mdc ) * 2 ) ) {
+       if ( (size_t) bufSize < ( sizeof( *mdc ) * 2 ) ) {
                return PICO_PMV_ERROR_SIZE;
        }
 
        /* set as mdc */
-       mdc = (mdc_t*) buffer;
+       mdc = (const mdc_t*) buffer;
 
        /* check mdc magic */
-       if ( *( (int*) mdc->magic ) != *( (int*) MDC_MAGIC ) ) {
+       if ( *( (const int*) mdc->magic ) != *( (const int*) MDC_MAGIC ) ) {
                return PICO_PMV_ERROR_IDENT;
        }
 
@@ -453,7 +450,7 @@ static int _mdc_canload( PM_PARAMS_CANLOAD ){
 
 static picoModel_t *_mdc_load( PM_PARAMS_LOAD ){
        int i, j;
-       picoByte_t          *bb;
+       picoByte_t          *bb, *bb0;
        mdc_t               *mdc;
        mdcSurface_t        *surface;
        mdcShader_t         *shader;
@@ -461,8 +458,8 @@ static picoModel_t *_mdc_load( PM_PARAMS_LOAD ){
        mdcFrame_t          *frame;
        mdcTriangle_t       *triangle;
        mdcVertex_t         *vertex;
-       mdcXyzCompressed_t  *vertexComp;
-       short               *mdcShort, *mdcCompVert;
+       mdcXyzCompressed_t  *vertexComp = NULL;
+       short               *mdcShort, *mdcCompVert = NULL;
        double lat, lng;
 
        picoModel_t         *picoModel;
@@ -479,12 +476,14 @@ static picoModel_t *_mdc_load( PM_PARAMS_LOAD ){
 
 
        /* set as mdc */
-       bb = (picoByte_t*) buffer;
-       mdc = (mdc_t*) buffer;
+       bb0 = bb = (picoByte_t*) _pico_alloc( bufSize );
+       memcpy( bb, buffer, bufSize );
+       mdc = (mdc_t*) bb;
 
        /* check ident and version */
        if ( *( (int*) mdc->magic ) != *( (int*) MDC_MAGIC ) || _pico_little_long( mdc->version ) != MDC_VERSION ) {
                /* not an mdc file (todo: set error) */
+               _pico_free( bb0 );
                return NULL;
        }
 
@@ -503,11 +502,13 @@ static picoModel_t *_mdc_load( PM_PARAMS_LOAD ){
        /* do frame check */
        if ( mdc->numFrames < 1 ) {
                _pico_printf( PICO_ERROR, "MDC with 0 frames" );
+               _pico_free( bb0 );
                return NULL;
        }
 
        if ( frameNum < 0 || frameNum >= mdc->numFrames ) {
                _pico_printf( PICO_ERROR, "Invalid or out-of-range MDC frame specified" );
+               _pico_free( bb0 );
                return NULL;
        }
 
@@ -605,6 +606,7 @@ static picoModel_t *_mdc_load( PM_PARAMS_LOAD ){
        picoModel = PicoNewModel();
        if ( picoModel == NULL ) {
                _pico_printf( PICO_ERROR, "Unable to allocate a new model" );
+               _pico_free( bb0 );
                return NULL;
        }
 
@@ -625,6 +627,7 @@ static picoModel_t *_mdc_load( PM_PARAMS_LOAD ){
                if ( picoSurface == NULL ) {
                        _pico_printf( PICO_ERROR, "Unable to allocate a new model surface" );
                        PicoFreeModel( picoModel ); /* sea */
+                       _pico_free( bb0 );
                        return NULL;
                }
 
@@ -639,6 +642,7 @@ static picoModel_t *_mdc_load( PM_PARAMS_LOAD ){
                if ( picoShader == NULL ) {
                        _pico_printf( PICO_ERROR, "Unable to allocate a new model shader" );
                        PicoFreeModel( picoModel );
+                       _pico_free( bb0 );
                        return NULL;
                }
 
@@ -722,6 +726,7 @@ static picoModel_t *_mdc_load( PM_PARAMS_LOAD ){
        }
 
        /* return the new pico model */
+       _pico_free( bb0 );
        return picoModel;
 }