66#include <stdio.h>
77#include <stdlib.h>
88#include <string.h>
9+ #include <unistd.h>
10+ #include <limits.h>
911
1012#include "platform.h"
1113#include "romimg.h"
1214#include "SonyRX.h"
1315
16+ #define IMAGE_COMMENT_BASESIZE 31
17+
1418struct ROMImgStat
1519{
1620 void * image ;
@@ -139,12 +143,7 @@ static int GetExtInfoStat(const struct ROMImgStat *ImageStat, struct RomDirFileF
139143int CreateBlankROMImg (const char * filename , ROMIMG * ROMImg )
140144{
141145 unsigned int CommentLength ;
142- char LocalhostName [32 ], cwd [128 ];
143- #if defined(_WIN32 ) || defined(WIN32 )
144- char UserName [32 ] = "" ;
145- #else
146- char * UserName ;
147- #endif
146+ char LocalhostName [32 ] = {0 }, cwd [PATH_MAX ] = {0 }, UserName [32 ] = {0 };
148147 struct FileEntry * ResetFile ;
149148 struct ExtInfoFieldEntry * ExtInfoEntry ;
150149
@@ -154,14 +153,15 @@ int CreateBlankROMImg(const char *filename, ROMIMG *ROMImg)
154153#if defined(_WIN32 ) || defined(WIN32 )
155154 GetUsername (UserName , sizeof (UserName ));
156155#else
157- UserName = getenv ( "USER" );
156+ getlogin_r ( UserName , sizeof ( UserName ) );
158157#endif
159158 GetLocalhostName (LocalhostName , sizeof (LocalhostName ));
160159 GetCurrentWorkingDirectory (cwd , sizeof (cwd ));
161160 /* Comment format: YYYYMMDD-XXXYYY,conffile,<filename>,<user>@<localhost>/<image path> */
162- CommentLength = 31 + strlen (filename ) + strlen (UserName ) + strlen (LocalhostName ) + strlen (cwd );
163- ROMImg -> comment = (char * )malloc (CommentLength );
164- sprintf (ROMImg -> comment , "%08x,conffile,%s,%s@%s/%s" , ROMImg -> date , filename , (UserName [0 ] == '\0' )?"" :UserName , LocalhostName , cwd );
161+ CommentLength = IMAGE_COMMENT_BASESIZE + strlen (filename ) + sizeof (LocalhostName ) + sizeof (UserName ) + sizeof (cwd );
162+ ROMImg -> comment = (char * )malloc ( CommentLength + 1 );
163+ if (!ROMImg -> comment ) return ENOMEM ;
164+ snprintf (ROMImg -> comment , CommentLength , "%08x,conffile,%s,%s@%s/%s" , ROMImg -> date , filename , UserName , LocalhostName , cwd );
165165
166166 // Create a blank RESET file.
167167 ROMImg -> NumFiles = 1 ;
@@ -452,15 +452,26 @@ static int AddExtInfoStat(struct FileEntry *file, unsigned char type, void *data
452452 return result ;
453453}
454454
455- int AddFile (ROMIMG * ROMImg , const char * path )
455+ int AddFile (ROMIMG * ROMImg , const char * path , int upperconv )
456456{
457+ char tbuf [9 ] = "\0" ; // we dont need a large buf, this is for filling in the filename on ROMFS
457458 FILE * InputFile ;
458459 int result ;
459460 unsigned int FileDateStamp ;
460461 unsigned short FileVersion ;
461462 if ((InputFile = fopen (path , "rb" )) != NULL ) {
462463 const char * fname = strrchr (path , PATHSEP );
463464 if (fname == NULL ) fname = path ; else fname ++ ;
465+ if (upperconv ) {
466+ strncpy (tbuf , fname , sizeof (tbuf ));
467+ tbuf [sizeof (tbuf ) - 1 ] = '\0' ;
468+ if (tbuf [0 ] != '\0' ) {
469+ upperbuff (tbuf );
470+ fname = tbuf ;
471+ char * T = strrchr (fname , '.' );
472+ if (T != NULL ) * T = '\0' ; //null terminate extension
473+ }
474+ }
464475 int size ;
465476 fseek (InputFile , 0 , SEEK_END );
466477 size = ftell (InputFile );
@@ -476,7 +487,7 @@ int AddFile(ROMIMG *ROMImg, const char *path)
476487 file = & ROMImg -> files [ROMImg -> NumFiles - 1 ];
477488 memset (& ROMImg -> files [ROMImg -> NumFiles - 1 ], 0 , sizeof (struct FileEntry ));
478489
479- strncpy (file -> RomDir .name , fname , sizeof (file -> RomDir .name ) - 1 );
490+ strncpy (file -> RomDir .name , fname , sizeof (file -> RomDir .name ));
480491 file -> RomDir .name [sizeof (file -> RomDir .name ) - 1 ] = '\0' ;
481492 file -> RomDir .ExtInfoEntrySize = 0 ;
482493
0 commit comments