From c3437b783381753cf69f1cf8495f1244d260bdec Mon Sep 17 00:00:00 2001 From: Garux Date: Tue, 19 Jan 2021 18:58:18 +0300 Subject: [PATCH] use safe strings in shaderInfo_s turn ImageLoad() to expect extensionless path as input --- tools/quake3/q3map2/image.cpp | 3 +-- tools/quake3/q3map2/q3map2.h | 8 +++--- tools/quake3/q3map2/shaders.cpp | 44 ++++++++++++++++----------------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/tools/quake3/q3map2/image.cpp b/tools/quake3/q3map2/image.cpp index f97b397a..66dd27e0 100644 --- a/tools/quake3/q3map2/image.cpp +++ b/tools/quake3/q3map2/image.cpp @@ -312,6 +312,7 @@ image_t *ImageFind( const char *name ){ /* ImageLoad() loads an rgba image and returns a pointer to the image_t struct or NULL if not found + expects extensionless path as input */ image_t *ImageLoad( const char *filename ){ @@ -330,9 +331,7 @@ image_t *ImageLoad( const char *filename ){ return NULL; } - /* strip file extension off name */ strcpy( name, filename ); - StripExtension( name ); /* try to find existing image */ image = ImageFind( name ); diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 09ba928c..654710e1 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -767,12 +767,12 @@ typedef struct shaderInfo_s String64 skyParmsImageBase; /* ydnar: for skies */ - char editorImagePath[ MAX_QPATH ]; /* use this image to generate texture coordinates */ - char lightImagePath[ MAX_QPATH ]; /* use this image to generate color / averageColor */ - char normalImagePath[ MAX_QPATH ]; /* ydnar: normalmap image for bumpmapping */ + String64 editorImagePath; /* use this image to generate texture coordinates */ + String64 lightImagePath; /* use this image to generate color / averageColor */ + String64 normalImagePath; /* ydnar: normalmap image for bumpmapping */ implicitMap_t implicitMap; /* ydnar: enemy territory implicit shaders */ - char implicitImagePath[ MAX_QPATH ]; + String64 implicitImagePath; image_t *shaderImage; image_t *lightImage; diff --git a/tools/quake3/q3map2/shaders.cpp b/tools/quake3/q3map2/shaders.cpp index 96f35fb5..b8d037f5 100644 --- a/tools/quake3/q3map2/shaders.cpp +++ b/tools/quake3/q3map2/shaders.cpp @@ -448,7 +448,7 @@ shaderInfo_t *CustomShader( shaderInfo_t *si, const char *find, char *replace ){ "\t\trgbGen identity\n" "\t}\n" "}\n", - si->implicitImagePath ); + si->implicitImagePath.c_str() ); } /* et: implicitMask */ @@ -475,8 +475,8 @@ shaderInfo_t *CustomShader( shaderInfo_t *si, const char *find, char *replace ){ "\t\trgbGen identity\n" "\t}\n" "}\n", - si->implicitImagePath, - si->implicitImagePath ); + si->implicitImagePath.c_str(), + si->implicitImagePath.c_str() ); } /* et: implicitBlend */ @@ -496,7 +496,7 @@ shaderInfo_t *CustomShader( shaderInfo_t *si, const char *find, char *replace ){ "\t}\n" "\tq3map_styleMarker\n" "}\n", - si->implicitImagePath ); + si->implicitImagePath.c_str() ); } /* default shader text */ @@ -775,7 +775,7 @@ static void LoadShaderImages( shaderInfo_t *si ){ si->normalImage = ImageLoad( si->normalImagePath ); if ( si->normalImage != NULL ) { Sys_FPrintf( SYS_VRB, "Shader %s has\n" - " NM %s\n", si->shader.c_str(), si->normalImagePath ); + " NM %s\n", si->shader.c_str(), si->normalImagePath.c_str() ); } } @@ -1046,7 +1046,7 @@ static void ParseShaderFile( const char *filename ){ } /* only care about images if we don't have a editor/light image */ - if ( strEmpty( si->editorImagePath ) && strEmpty( si->lightImagePath ) && strEmpty( si->implicitImagePath ) ) { + if ( si->editorImagePath.empty() && si->lightImagePath.empty() && si->implicitImagePath.empty() ) { /* digest any images */ if ( striEqual( token, "map" ) || striEqual( token, "clampMap" ) || @@ -1062,8 +1062,8 @@ static void ParseShaderFile( const char *filename ){ /* get an image */ GetTokenAppend( shaderText, false ); if ( token[ 0 ] != '*' && token[ 0 ] != '$' ) { - strcpy( si->lightImagePath, token ); - DefaultExtension( si->lightImagePath, ".tga" ); + StripExtension( token ); + si->lightImagePath = token; /* debug code */ //% Sys_FPrintf( SYS_VRB, "Deduced shader image: %s\n", si->lightImagePath ); @@ -1178,10 +1178,10 @@ static void ParseShaderFile( const char *filename ){ si->implicitMap = IM_OPAQUE; GetTokenAppend( shaderText, false ); if ( strEqual( token, "-" ) ) { - sprintf( si->implicitImagePath, "%s.tga", si->shader.c_str() ); + si->implicitImagePath = si->shader; } else{ - strcpy( si->implicitImagePath, token ); + si->implicitImagePath = token; } } @@ -1189,10 +1189,10 @@ static void ParseShaderFile( const char *filename ){ si->implicitMap = IM_MASKED; GetTokenAppend( shaderText, false ); if ( strEqual( token, "-" ) ) { - sprintf( si->implicitImagePath, "%s.tga", si->shader.c_str() ); + si->implicitImagePath = si->shader; } else{ - strcpy( si->implicitImagePath, token ); + si->implicitImagePath = token; } } @@ -1200,10 +1200,10 @@ static void ParseShaderFile( const char *filename ){ si->implicitMap = IM_MASKED; GetTokenAppend( shaderText, false ); if ( strEqual( token, "-" ) ) { - sprintf( si->implicitImagePath, "%s.tga", si->shader.c_str() ); + si->implicitImagePath = si->shader; } else{ - strcpy( si->implicitImagePath, token ); + si->implicitImagePath = token; } } @@ -1215,22 +1215,22 @@ static void ParseShaderFile( const char *filename ){ /* qer_editorimage */ else if ( striEqual( token, "qer_editorImage" ) ) { GetTokenAppend( shaderText, false ); - strcpy( si->editorImagePath, token ); - DefaultExtension( si->editorImagePath, ".tga" ); + StripExtension( token ); + si->editorImagePath = token; } /* ydnar: q3map_normalimage (bumpmapping normal map) */ else if ( striEqual( token, "q3map_normalImage" ) ) { GetTokenAppend( shaderText, false ); - strcpy( si->normalImagePath, token ); - DefaultExtension( si->normalImagePath, ".tga" ); + StripExtension( token ); + si->normalImagePath = token; } /* q3map_lightimage */ else if ( striEqual( token, "q3map_lightImage" ) ) { GetTokenAppend( shaderText, false ); - strcpy( si->lightImagePath, token ); - DefaultExtension( si->lightImagePath, ".tga" ); + StripExtension( token ); + si->lightImagePath = token; } /* ydnar: skyparms */ @@ -1243,8 +1243,8 @@ static void ParseShaderFile( const char *filename ){ si->skyParmsImageBase = token; /* use top image as sky light image */ - if ( strEmpty( si->lightImagePath ) ) { - sprintf( si->lightImagePath, "%s_up.tga", si->skyParmsImageBase.c_str() ); + if ( si->lightImagePath.empty() ) { + si->lightImagePath( si->skyParmsImageBase, "_up" ); } }