]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/picomodel/pm_md2.c
q3map2 is now waring free
[xonotic/netradiant.git] / libs / picomodel / pm_md2.c
index bf78a99bfad8a7ea283e8583c8c9f59d9e95808b..d53f70295467081d1296d15a2b40b23cd33bf946 100644 (file)
@@ -300,20 +300,17 @@ float     md2_normals[ MD2_NUMVERTEXNORMALS ][ 3 ] =
 
 static int _md2_canload( PM_PARAMS_CANLOAD )
 {
-       md2_t   *md2;
-
-       /* to keep the compiler happy */
-       *fileName = *fileName;
+       const md2_t     *md2;
 
        /* sanity check */
-       if( bufSize < ( sizeof( *md2 ) * 2) )
+       if( (size_t) bufSize < ( sizeof( *md2 ) * 2) )
                return PICO_PMV_ERROR_SIZE;
        
        /* set as md2 */
-       md2 = (md2_t*) buffer;
+       md2 = (const md2_t*) buffer;
        
        /* check md2 magic */
-       if( *((int*) md2->magic) != *((int*) MD2_MAGIC) ) 
+       if( *((const int*) md2->magic) != *((const int*) MD2_MAGIC) ) 
                return PICO_PMV_ERROR_IDENT;
        
        /* check md2 version */
@@ -344,7 +341,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        md2Triangle_t   *triangle;
        md2XyzNormal_t  *vertex;
 
-       picoByte_t      *bb;
+       picoByte_t      *bb, *bb0;
        picoModel_t             *picoModel;
        picoSurface_t   *picoSurface;
        picoShader_t    *picoShader;
@@ -354,14 +351,16 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        
 
        /* set as md2 */
-       bb = (picoByte_t*) buffer;
-       md2     = (md2_t*) buffer;
+       bb0 = bb = (picoByte_t*) _pico_alloc(bufSize);
+       memcpy(bb, buffer, bufSize);
+       md2     = (md2_t*) bb;
 
        /* check ident and version */
-       if( *((int*) md2->magic) != *((int*) MD2_MAGIC) || _pico_little_long( md2->version ) != MD2_VERSION )
+       if( *((const int*) md2->magic) != *((const int*) MD2_MAGIC) || _pico_little_long( md2->version ) != MD2_VERSION )
        {
                /* not an md2 file (todo: set error) */
                _pico_printf( PICO_ERROR, "%s is not an MD2 File!", fileName );
+               _pico_free(bb0);
                return NULL;
        }
        
@@ -390,12 +389,14 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        if( md2->numFrames < 1 )
        {
                _pico_printf( PICO_ERROR, "%s has 0 frames!", fileName );
+               _pico_free(bb0);
                return NULL;
        }
        
        if( frameNum < 0 || frameNum >= md2->numFrames )
        {
                _pico_printf( PICO_ERROR, "Invalid or out-of-range MD2 frame specified" );
+               _pico_free(bb0);
                return NULL;
        }
 
@@ -429,7 +430,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        }
 
        // set Skin Name
-       strncpy(skinname, (bb + md2->ofsSkins), MD2_MAX_SKINNAME );
+       strncpy(skinname, (const char *) (bb + md2->ofsSkins), MD2_MAX_SKINNAME );
 
        // Print out md2 values
        _pico_printf(PICO_VERBOSE,"Skins: %d  Verts: %d  STs: %d  Triangles: %d  Frames: %d\nSkin Name \"%s\"\n", md2->numSkins, md2->numXYZ, md2->numST, md2->numTris, md2->numFrames, &skinname );
@@ -443,6 +444,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        if( picoModel == NULL )
        {
                _pico_printf( PICO_ERROR, "Unable to allocate a new model" );
+               _pico_free(bb0);
                return NULL;
        }
 
@@ -458,6 +460,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        {
                _pico_printf( PICO_ERROR, "Unable to allocate a new model surface" );
                PicoFreeModel( picoModel );
+               _pico_free(bb0);
                return NULL;
        }
 
@@ -469,6 +472,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        {
                _pico_printf( PICO_ERROR, "Unable to allocate a new model shader" );
                PicoFreeModel( picoModel );
+               _pico_free(bb0);
                return NULL;
        }
 
@@ -644,6 +648,7 @@ static picoModel_t *_md2_load( PM_PARAMS_LOAD )
        _pico_free(p_index_LUT_DUPS);
 
        /* return the new pico model */
+       _pico_free(bb0);
        return picoModel;
 
 }