X-Git-Url: http://gitweb.hugovil.com/?a=blobdiff_plain;f=src%2Fcommon%2Fhexfile.c;h=b9aff06117733fb044e4de7bf2e1bd2852e307b8;hb=121bcb38f29409f10de63a68d86620c4beb75c97;hp=9fef6937ba8f7cc50b72b6b048eba98e35bc42c1;hpb=00deadc94f868a45431b58e34e707d235cf02b47;p=emu8051.git diff --git a/src/common/hexfile.c b/src/common/hexfile.c index 9fef693..b9aff06 100644 --- a/src/common/hexfile.c +++ b/src/common/hexfile.c @@ -11,7 +11,6 @@ # include "config.h" #endif -#include #include #include #include @@ -30,49 +29,44 @@ static int asciihex2int_error; +int +asciihex2int_get_error(void) +{ + return asciihex2int_error; +} + /* Convert integer to ASCII hex string. */ void int2asciihex(int val, char *str, int width) { if (width == 1) - sprintf(str , "%.1X", (u_int8_t) val); + sprintf(str , "%.1X", (uint8_t) val); else if (width == 2) - sprintf(str , "%.2X", (u_int8_t) val); + sprintf(str , "%.2X", (uint8_t) val); else if (width == 4) - sprintf(str , "%.4X", (u_int16_t) val); + sprintf(str , "%.4X", (uint16_t) val); else sprintf(str , "Err"); } -/* Convert ASCII hex string to integer. */ -int -asciihex2int(char *str) -{ - int val; - int rc; - - rc = sscanf(str, "%X", &val); - - if (rc == 0) { - log_err("ASCII to hex conversion failure"); - asciihex2int_error = true; - } - - return val; -} - -/* Convert an ascii string to an hexadecimal number. */ +/* + * Convert ASCII string in hexadecimal notation to integer, up to length + * characters. + * The string contains only [0-9] and [a-f] characters (no "0x" prefix). + */ static unsigned int -asciihex2int_len(char *istring, int length) +asciihex2int_len(char *str, int length) { unsigned int result = 0; int i, ascii_code; - if (!length || (length > (int) strlen(istring))) - length = strlen(istring); + asciihex2int_error = false; + + if (!length || (length > (int) strlen(str))) + length = strlen(str); for (i = 0; i < length; i++) { - ascii_code = istring[i]; + ascii_code = str[i]; if (ascii_code > 0x39) ascii_code &= 0xDF; @@ -87,11 +81,23 @@ asciihex2int_len(char *istring, int length) } else { log_err("ASCII to hex conversion failure"); asciihex2int_error = true; + return 0; } } return result; } +/* + * Convert ASCII string in hexadecimal notation to integer, up to \0 end character. + * The string must not contain prefix "0x", only [0-9] and [a-f] characters. + */ +unsigned int +asciihex2int(char *str) +{ + /* Set length to zero to use full length of string. */ + return asciihex2int_len(str, 0); +} + /* * Return value: * true: success @@ -109,14 +115,18 @@ hexfile_load(const char *filename) log_debug("LoadHexFile"); - if (filename == NULL) - log_fail("No file specified"); + if (filename == NULL) { + log_err("No file specified"); + return false; + } /* Trying to open the file. */ fp = fopen(filename, "r"); - if (fp == NULL) - log_fail("Error opening hex file <%s>: %s", filename, - strerror(errno)); + if (fp == NULL) { + log_err("Error opening hex file <%s>: %s", filename, + strerror(errno)); + return false; + } /* Reading one line of data from the hex file. */ /* char *fgets(char *s, int size, FILE *stream); @@ -184,11 +194,12 @@ hexfile_load(const char *filename) close_file: status = fclose(fp); - if (status != EXIT_SUCCESS) - log_fail("Error closing hex file"); - - if (!valid) + if (status != EXIT_SUCCESS) { + log_err("Error closing hex file"); + valid = false; + } else if (!valid) { log_err("Error parsing hex file"); + } return valid; }