-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #516 from sneakywumpus/dev
clang 19 static analyzer run & clean-up
- Loading branch information
Showing
24 changed files
with
318 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,161 +1,172 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <ctype.h> | ||
#include <unistd.h> | ||
#include <sys/stat.h> | ||
|
||
void help(char *name) | ||
{ | ||
printf("%s - BINARY to Intel HEX file convertor version 1.00\n"\ | ||
"(c)BCL Vysoke Myto 2001 (benedikt@lphard.cz)\n\n",name); | ||
printf("Usage: %s [-option] binfile hexfile\n"\ | ||
" -l Bytes to read from binary file\n"\ | ||
" -i Binary file starting offset\n"\ | ||
" -o Output file offset (where HEX data starts)\n"\ | ||
" -t Exclude EOF record\n"\ | ||
" -a Append to end of existing HEX file\n"\ | ||
" -q Quiet mode (no statistics are printed)\n", name); | ||
printf("%s - BINARY to Intel HEX file convertor version 1.00\n" | ||
"(c)BCL Vysoke Myto 2001 (benedikt@lphard.cz)\n\n", name); | ||
printf("Usage: %s [-option] binfile hexfile\n" | ||
" -l Bytes to read from binary file\n" | ||
" -i Binary file starting offset\n" | ||
" -o Output file offset (where HEX data starts)\n" | ||
" -t Exclude EOF record\n" | ||
" -a Append to end of existing HEX file\n" | ||
" -q Quiet mode (no statistics are printed)\n", name); | ||
} | ||
|
||
int main(int argc,char *argv[])/*Main routine*/ | ||
int main(int argc, char *argv[]) /* Main routine */ | ||
{ | ||
char *ifile = NULL; | ||
char *ofile = NULL; | ||
int c; | ||
FILE *inp, *outp; | ||
int ch,csum; | ||
int ofsa = 0; | ||
int cnt = 0; | ||
struct stat statbuf; | ||
long int foffset = 0; | ||
long int fsize = 0; | ||
long int fsub; | ||
long int fpoint = 0; | ||
long int adrs = 0; | ||
unsigned char quiet = 0; | ||
unsigned char eofrec = 0; | ||
unsigned char append = 0; | ||
char *ifile = NULL; | ||
char *ofile = NULL; | ||
int c; | ||
FILE *inp, *outp; | ||
int ch, csum; | ||
int ofsa = 0; | ||
int cnt = 0; | ||
struct stat statbuf; | ||
long foffset = 0; | ||
long fsize = 0; | ||
long fsub; | ||
long fpoint = 0; | ||
long adrs = 0; | ||
unsigned char quiet = 0; | ||
unsigned char eofrec = 0; | ||
unsigned char append = 0; | ||
|
||
opterr = 0; //print error message if unknown option | ||
opterr = 0; //print error message if unknown option | ||
|
||
while ((c = getopt (argc, argv, "l:i:o:taqv")) != -1) | ||
switch (c) { | ||
case 'l': | ||
fsize = atol(optarg); | ||
break; | ||
case 'i': | ||
foffset = atol(optarg); | ||
break; | ||
case 'o': | ||
adrs = atol(optarg); | ||
break; | ||
case 't': | ||
eofrec = 1; | ||
break; | ||
case 'a': | ||
append = 1; | ||
break; | ||
case 'q': | ||
quiet = 1; | ||
break; | ||
case 'v': | ||
printf("%s - BINARY to Intel HEX file convertor version 1.00\n"\ | ||
"(c)BCL Vysoke Myto 2001 (benedikt@lphard.cz)\n",argv[0]); | ||
return EXIT_SUCCESS; | ||
case '?': | ||
help (argv[0]); | ||
return EXIT_FAILURE; | ||
} | ||
while ((c = getopt(argc, argv, "l:i:o:taqv")) != -1) | ||
switch (c) { | ||
case 'l': | ||
fsize = atol(optarg); | ||
break; | ||
case 'i': | ||
foffset = atol(optarg); | ||
break; | ||
case 'o': | ||
adrs = atol(optarg); | ||
break; | ||
case 't': | ||
eofrec = 1; | ||
break; | ||
case 'a': | ||
append = 1; | ||
break; | ||
case 'q': | ||
quiet = 1; | ||
break; | ||
case 'v': | ||
printf("%s - BINARY to Intel HEX file convertor version 1.00\n" | ||
"(c)BCL Vysoke Myto 2001 (benedikt@lphard.cz)\n", argv[0]); | ||
return EXIT_SUCCESS; | ||
case '?': | ||
help(argv[0]); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
if ((argc - optind) != 2) { | ||
printf("ERROR: Missing input/output file.\n"); | ||
help(argv[0]); | ||
return EXIT_FAILURE; | ||
} | ||
ifile = argv[optind]; | ||
ofile = argv[optind+1]; | ||
if ((argc - optind) != 2) { | ||
printf("ERROR: Missing input/output file.\n"); | ||
help(argv[0]); | ||
return EXIT_FAILURE; | ||
} | ||
ifile = argv[optind]; | ||
ofile = argv[optind + 1]; | ||
|
||
/*Open file check*/ | ||
if((inp = fopen(ifile, "rb")) == NULL){ | ||
printf("ERROR: Cannot open input file.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
fseek (inp, foffset, SEEK_SET); | ||
/* Open file check */ | ||
if ((inp = fopen(ifile, "rb")) == NULL) { | ||
printf("ERROR: Cannot open input file.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
fseek(inp, foffset, SEEK_SET); | ||
|
||
if (append == 0) { | ||
if((outp = fopen(ofile, "wt")) == NULL){ | ||
printf("ERROR: Cannot open output file.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
} else { | ||
if((outp = fopen(ofile, "at")) == NULL){ | ||
printf("ERROR: Cannot re-open output file.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
fseek (outp, 0, SEEK_END); | ||
} | ||
if (append == 0) { | ||
if ((outp = fopen(ofile, "wt")) == NULL) { | ||
fclose(inp); | ||
printf("ERROR: Cannot open output file.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
} else { | ||
if ((outp = fopen(ofile, "at")) == NULL) { | ||
fclose(inp); | ||
printf("ERROR: Cannot re-open output file.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
fseek(outp, 0, SEEK_END); | ||
} | ||
|
||
fstat(fileno(inp), &statbuf); | ||
if (quiet == 0) printf("Input file size=%ld\n",(long)statbuf.st_size); | ||
if (foffset > statbuf.st_size) { | ||
printf("ERROR: Input offset > input file length\n"); | ||
} | ||
if ((fsize == 0) || (fsize > (statbuf.st_size - foffset))) | ||
fsize = statbuf.st_size - foffset; | ||
fstat(fileno(inp), &statbuf); | ||
if (quiet == 0) | ||
printf("Input file size=%ld\n", (long) statbuf.st_size); | ||
if (foffset > statbuf.st_size) | ||
printf("ERROR: Input offset > input file length\n"); | ||
if (fsize == 0 || fsize > (statbuf.st_size - foffset)) | ||
fsize = statbuf.st_size - foffset; | ||
|
||
// fprintf(outp,":020000020000FC\n");/*Start Header*/ | ||
fsub = fsize - fpoint; | ||
if (fsub > 0x20) { | ||
fprintf(outp,":20%04X00",(unsigned int) adrs); /*Hex line Header*/ | ||
csum = 0x20 + (adrs>>8) + (adrs & 0xFF); | ||
adrs += 0x20; | ||
} | ||
else { | ||
fprintf(outp, ":%02X%04X00", (unsigned int) fsub, (unsigned int) adrs);/*Hex line Header*/ | ||
csum = fsub + (adrs>>8) + (adrs & 0xFF); | ||
adrs += fsub; | ||
} | ||
while (fsub > 0){ | ||
ch = fgetc(inp); | ||
fprintf(outp,"%02X",ch); /*Put data*/ | ||
cnt++; fpoint++; | ||
fsub = fsize - fpoint; | ||
csum = ch + csum; | ||
if((fsub == 0)||(cnt == 0x20)){ | ||
cnt = 0; csum = 0xFF & (~csum + 1); | ||
fprintf(outp,"%02X\n",csum); /*Put checksum*/ | ||
if(fsub == 0) break; | ||
if(adrs > 0xFFFF){ | ||
ofsa = 0x1000 + ofsa; | ||
adrs = 0; | ||
fprintf(outp,":02000002%04X",ofsa); /*Change offset address*/ | ||
csum = 0x02 + 0x02 + (ofsa>>8) + (ofsa & 0xFF); | ||
csum = 0xFF & (~csum + 1); | ||
fprintf(outp,"%02X\n", csum); | ||
} | ||
adrs = 0xFFFF & adrs; | ||
if (fsub > 0x20) { | ||
fprintf(outp,":20%04X00", (unsigned int) adrs); /*Next Hex line Header*/ | ||
csum = 0x20 + (adrs>>8) + (adrs & 0xFF); | ||
adrs += 0x20; | ||
} | ||
else { | ||
if(fsub > 0){ | ||
fprintf(outp, ":%02X%04X00", (unsigned int) fsub, (unsigned int) adrs); /*Next Hex line Header*/ | ||
csum = fsub + (adrs>>8) + (adrs & 0xFF); | ||
adrs += fsub; | ||
} | ||
} | ||
} | ||
} | ||
if (eofrec == 0) fprintf(outp,":00000001FF\n"); /*End footer*/ | ||
fflush (outp); | ||
// fprintf(outp, ":020000020000FC\n"); /* Start Header */ | ||
fsub = fsize - fpoint; | ||
if (fsub > 0x20) { | ||
fprintf(outp, ":20%04X00", (unsigned) adrs); /* Hex line Header */ | ||
csum = 0x20 + (adrs >> 8) + (adrs & 0xFF); | ||
adrs += 0x20; | ||
} else { | ||
fprintf(outp, ":%02X%04X00", (unsigned) fsub, | ||
(unsigned) adrs); /* Hex line Header */ | ||
csum = fsub + (adrs >> 8) + (adrs & 0xFF); | ||
adrs += fsub; | ||
} | ||
while (fsub > 0) { | ||
if ((ch = fgetc(inp)) == EOF) { | ||
fclose(inp); | ||
fclose(outp); | ||
printf("ERROR: Cannot read from input file.\n"); | ||
return EXIT_FAILURE; | ||
} | ||
fprintf(outp, "%02X", ch); /* Put data */ | ||
cnt++; | ||
fpoint++; | ||
fsub = fsize - fpoint; | ||
csum = ch + csum; | ||
if (fsub == 0 || cnt == 0x20) { | ||
cnt = 0; | ||
csum = 0xFF & (~csum + 1); | ||
fprintf(outp, "%02X\n", csum); /* Put checksum */ | ||
if (fsub == 0) | ||
break; | ||
if (adrs > 0xFFFF) { | ||
ofsa = 0x1000 + ofsa; | ||
adrs = 0; | ||
fprintf(outp, ":02000002%04X", ofsa); /* Change offset address */ | ||
csum = 0x02 + 0x02 + (ofsa >> 8) + (ofsa & 0xFF); | ||
csum = 0xFF & (~csum + 1); | ||
fprintf(outp, "%02X\n", csum); | ||
} | ||
adrs = 0xFFFF & adrs; | ||
if (fsub > 0x20) { | ||
fprintf(outp, ":20%04X00", | ||
(unsigned) adrs); /* Next Hex line Header */ | ||
csum = 0x20 + (adrs >> 8) + (adrs & 0xFF); | ||
adrs += 0x20; | ||
} else { | ||
if (fsub > 0) { | ||
fprintf(outp, ":%02X%04X00", (unsigned) fsub, | ||
(unsigned) adrs); /* Next Hex line Header */ | ||
csum = fsub + (adrs >> 8) + (adrs & 0xFF); | ||
adrs += fsub; | ||
} | ||
} | ||
} | ||
} | ||
if (eofrec == 0) | ||
fprintf(outp, ":00000001FF\n"); /* End footer */ | ||
fflush(outp); | ||
|
||
fstat(fileno(outp), &statbuf); | ||
if (quiet == 0) printf("Output file size=%ld\n",(long)statbuf.st_size); | ||
fstat(fileno(outp), &statbuf); | ||
if (quiet == 0) | ||
printf("Output file size=%ld\n", (long) statbuf.st_size); | ||
|
||
fclose(inp); | ||
fclose(outp); | ||
return EXIT_SUCCESS; | ||
fclose(inp); | ||
fclose(outp); | ||
return EXIT_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.