tools/iriver: fix resource management in encode/decode functions
Change-Id: Ib0b38378c66d5902694d9af9fd0271d616101660
This commit is contained in:
parent
a20755e9ef
commit
da45b37fac
1 changed files with 44 additions and 45 deletions
|
@ -99,6 +99,7 @@ static FILE * openoutfile( const char * filename )
|
||||||
int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify,
|
int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify,
|
||||||
enum striptype stripmode )
|
enum striptype stripmode )
|
||||||
{
|
{
|
||||||
|
int rv = 0;
|
||||||
FILE * infile = NULL;
|
FILE * infile = NULL;
|
||||||
FILE * outfile = NULL;
|
FILE * outfile = NULL;
|
||||||
int i = -1;
|
int i = -1;
|
||||||
|
@ -110,7 +111,7 @@ int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
unsigned char newmunge;
|
unsigned char newmunge;
|
||||||
signed long lenread;
|
signed long lenread;
|
||||||
int s = 0;
|
int s = 0;
|
||||||
unsigned char * pChecksums, * ppChecksums = 0;
|
unsigned char * pChecksums = NULL, * ppChecksums = NULL;
|
||||||
unsigned char ck;
|
unsigned char ck;
|
||||||
|
|
||||||
infile = openinfile(infile_name);
|
infile = openinfile(infile_name);
|
||||||
|
@ -121,9 +122,8 @@ int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid encrypted iHP "
|
fprintf( stderr, "This doesn't look like a valid encrypted iHP "
|
||||||
"firmware - reason: header length\n" );
|
"firmware - reason: header length\n" );
|
||||||
fclose(infile);
|
rv = -1;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
i = testheader( headerdata );
|
i = testheader( headerdata );
|
||||||
|
@ -131,9 +131,8 @@ int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This firmware is for an unknown model, or is not"
|
fprintf( stderr, "This firmware is for an unknown model, or is not"
|
||||||
" a valid encrypted iHP firmware\n" );
|
" a valid encrypted iHP firmware\n" );
|
||||||
fclose(infile);
|
rv = -2;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -2;
|
|
||||||
};
|
};
|
||||||
fprintf( stderr, "Model %s\n", models[ i ] );
|
fprintf( stderr, "Model %s\n", models[ i ] );
|
||||||
|
|
||||||
|
@ -151,9 +150,8 @@ int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid encrypted "
|
fprintf( stderr, "This doesn't look like a valid encrypted "
|
||||||
"iHP firmware - reason: file 'length' data\n" );
|
"iHP firmware - reason: file 'length' data\n" );
|
||||||
fclose(infile);
|
rv = -3;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -3;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
minsize = firmware_minsize[i];
|
minsize = firmware_minsize[i];
|
||||||
|
@ -226,18 +224,16 @@ int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid encrypted "
|
fprintf( stderr, "This doesn't look like a valid encrypted "
|
||||||
"iHP firmware - reason: ESTFBINR 'length' data\n" );
|
"iHP firmware - reason: ESTFBINR 'length' data\n" );
|
||||||
fclose(infile);
|
rv = -4;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -4;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if( fp != dwLength2 )
|
if( fp != dwLength2 )
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid encrypted "
|
fprintf( stderr, "This doesn't look like a valid encrypted "
|
||||||
"iHP firmware - reason: 'length2' mismatch\n" );
|
"iHP firmware - reason: 'length2' mismatch\n" );
|
||||||
fclose(infile);
|
rv = -5;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -5;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fp = 0;
|
fp = 0;
|
||||||
|
@ -252,9 +248,8 @@ int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid encrypted "
|
fprintf( stderr, "This doesn't look like a valid encrypted "
|
||||||
"iHP firmware - reason: Checksum mismatch!" );
|
"iHP firmware - reason: Checksum mismatch!" );
|
||||||
fclose(infile);
|
rv = -6;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -6;
|
|
||||||
};
|
};
|
||||||
ppChecksums += lenread;
|
ppChecksums += lenread;
|
||||||
};
|
};
|
||||||
|
@ -263,9 +258,8 @@ int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid encrypted "
|
fprintf( stderr, "This doesn't look like a valid encrypted "
|
||||||
"iHP firmware - reason: 'length3' mismatch\n" );
|
"iHP firmware - reason: 'length3' mismatch\n" );
|
||||||
fclose(infile);
|
rv = -7;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -7;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -287,11 +281,18 @@ int iriver_decode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
|
|
||||||
return 0;
|
bail:
|
||||||
|
if (infile != NULL)
|
||||||
|
fclose(infile);
|
||||||
|
if (outfile != NULL)
|
||||||
|
fclose(outfile);
|
||||||
|
free(pChecksums);
|
||||||
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify )
|
int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify )
|
||||||
{
|
{
|
||||||
|
int rv = 0;
|
||||||
FILE * infile = NULL;
|
FILE * infile = NULL;
|
||||||
FILE * outfile = NULL;
|
FILE * outfile = NULL;
|
||||||
int i = -1;
|
int i = -1;
|
||||||
|
@ -303,7 +304,7 @@ int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
unsigned char newmunge;
|
unsigned char newmunge;
|
||||||
signed long lenread;
|
signed long lenread;
|
||||||
int s = 0;
|
int s = 0;
|
||||||
unsigned char * pChecksums, * ppChecksums;
|
unsigned char * pChecksums = NULL, * ppChecksums = NULL;
|
||||||
unsigned char ck;
|
unsigned char ck;
|
||||||
|
|
||||||
infile = openinfile(infile_name);
|
infile = openinfile(infile_name);
|
||||||
|
@ -314,9 +315,8 @@ int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid decoded "
|
fprintf( stderr, "This doesn't look like a valid decoded "
|
||||||
"iHP firmware - reason: header length\n" );
|
"iHP firmware - reason: header length\n" );
|
||||||
fclose(infile);
|
rv = -1;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if( modify )
|
if( modify )
|
||||||
|
@ -329,9 +329,8 @@ int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This firmware is for an unknown model, or is not"
|
fprintf( stderr, "This firmware is for an unknown model, or is not"
|
||||||
" a valid decoded iHP firmware\n" );
|
" a valid decoded iHP firmware\n" );
|
||||||
fclose(infile);
|
rv = -2;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -2;
|
|
||||||
};
|
};
|
||||||
fprintf( stderr, "Model %s\n", models[ i ] );
|
fprintf( stderr, "Model %s\n", models[ i ] );
|
||||||
|
|
||||||
|
@ -348,9 +347,8 @@ int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid decoded iHP"
|
fprintf( stderr, "This doesn't look like a valid decoded iHP"
|
||||||
" firmware - reason: file 'length' data\n" );
|
" firmware - reason: file 'length' data\n" );
|
||||||
fclose(infile);
|
rv = -3;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -3;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
minsize = firmware_minsize[i];
|
minsize = firmware_minsize[i];
|
||||||
|
@ -401,18 +399,16 @@ int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid decoded iHP"
|
fprintf( stderr, "This doesn't look like a valid decoded iHP"
|
||||||
" firmware - reason: ESTFBINR 'length' data\n" );
|
" firmware - reason: ESTFBINR 'length' data\n" );
|
||||||
fclose(infile);
|
rv = -4;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -4;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if( fp != dwLength2 )
|
if( fp != dwLength2 )
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid decoded "
|
fprintf( stderr, "This doesn't look like a valid decoded "
|
||||||
"iHP firmware - reason: 'length1' mismatch\n" );
|
"iHP firmware - reason: 'length1' mismatch\n" );
|
||||||
fclose(infile);
|
rv = -5;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -5;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* write out remainder w/out applying descrambler */
|
/* write out remainder w/out applying descrambler */
|
||||||
|
@ -431,14 +427,17 @@ int iriver_encode(const char *infile_name, const char *outfile_name, BOOL modify
|
||||||
{
|
{
|
||||||
fprintf( stderr, "This doesn't look like a valid decoded "
|
fprintf( stderr, "This doesn't look like a valid decoded "
|
||||||
"iHP firmware - reason: 'length2' mismatch\n" );
|
"iHP firmware - reason: 'length2' mismatch\n" );
|
||||||
fclose(infile);
|
rv = -6;
|
||||||
fclose(outfile);
|
goto bail;
|
||||||
return -6;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fprintf( stderr, "File encoded successfully and checksum table built!\n" );
|
fprintf( stderr, "File encoded successfully and checksum table built!\n" );
|
||||||
|
|
||||||
fclose(infile);
|
bail:
|
||||||
fclose(outfile);
|
if (infile != NULL)
|
||||||
return 0;
|
fclose(infile);
|
||||||
|
if (outfile != NULL)
|
||||||
|
fclose(outfile);
|
||||||
|
free(pChecksums);
|
||||||
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue