summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDarkvater <Darkvater@openttd.org>2007-01-13 13:47:57 +0000
committerDarkvater <Darkvater@openttd.org>2007-01-13 13:47:57 +0000
commit95024bb21c21c38fde3453c34e7029b6cf3626ea (patch)
treec5dd340a09a075d06a70fed0556e127c470cc125 /src
parent574ded3afdab4c28b892101e550dc89ffd170e71 (diff)
downloadopenttd-95024bb21c21c38fde3453c34e7029b6cf3626ea.tar.xz
(svn r8093) -Codechange: Add a function to get a string representation of an MD5SUM and use it.
Diffstat (limited to 'src')
-rw-r--r--src/newgrf_config.cpp5
-rw-r--r--src/newgrf_gui.cpp9
-rw-r--r--src/string.cpp18
-rw-r--r--src/string.h2
4 files changed, 23 insertions, 11 deletions
diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp
index 1c49bd207..3d711fcd5 100644
--- a/src/newgrf_config.cpp
+++ b/src/newgrf_config.cpp
@@ -205,12 +205,9 @@ bool IsGoodGRFConfigList(void)
const GRFConfig *f = FindGRFConfig(c->grfid, c->md5sum);
if (f == NULL) {
char buf[512], *p = buf;
- uint i;
p += snprintf(p, lastof(buf) - p, "Couldn't find NewGRF %08X (%s) checksum ", BSWAP32(c->grfid), c->filename);
- for (i = 0; i < lengthof(c->md5sum); i++) {
- p += snprintf(p, lastof(buf) - p, "%02X", c->md5sum[i]);
- }
+ md5sumToString(p, lastof(buf), c->md5sum);
ShowInfo(buf);
res = false;
diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp
index d5934a433..6884f043f 100644
--- a/src/newgrf_gui.cpp
+++ b/src/newgrf_gui.cpp
@@ -41,9 +41,7 @@ static int parse_intlist(const char *p, int *items, int maxitems)
static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint w, bool show_params)
{
- char buff[512];
- char *s;
- uint i;
+ char buff[256];
/* Draw filename or not if it is not known (GRF sent over internet) */
if (c->filename != NULL) {
@@ -57,10 +55,7 @@ static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint w, bool show
y += DrawStringMultiLine(x, y, STR_NEWGRF_GRF_ID, w);
/* Prepare and draw MD5 sum */
- s = buff;
- for (i = 0; i < lengthof(c->md5sum); i++) {
- s += snprintf(s, lastof(buff) - s, "%02X", c->md5sum[i]);
- }
+ md5sumToString(buff, lastof(buff), c->md5sum);
SetDParamStr(0, buff);
y += DrawStringMultiLine(x, y, STR_NEWGRF_MD5SUM, w);
diff --git a/src/string.cpp b/src/string.cpp
index 872b6e08f..d55d9a70e 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -177,6 +177,24 @@ int CDECL vsnprintf(char *str, size_t size, const char *format, va_list ap)
#endif /* WIN32 */
+/** Convert the md5sum to a hexadecimal string representation
+ * @param buf buffer to put the md5sum into
+ * @param last last character of buffer (usually lastof(buf))
+ * @param md5sum the md5sum itself
+ * @return a pointer to the next character after the md5sum */
+char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
+{
+ char *p = buf;
+
+ for (uint i = 0; i < 16; i++) {
+ p += snprintf(p, last + 1 - p, "%02X", md5sum[i]);
+ if (p >= last) break;
+ }
+
+ return p;
+}
+
+
/* UTF-8 handling routines */
diff --git a/src/string.h b/src/string.h
index 2dbc06eee..c3bc45280 100644
--- a/src/string.h
+++ b/src/string.h
@@ -55,6 +55,8 @@ static inline int ttd_strnlen(const char *str, int maxlen)
return t - str;
}
+/** Convert the md5sum number to a 'hexadecimal' string, return next pos in buffer */
+char *md5sumToString(char *buf, const char *last, const uint8 md5sum[16]);
typedef uint32 WChar;