From 168d3b1e0977ab8fd4ae1e81e5f864bd58e4775a Mon Sep 17 00:00:00 2001 From: "Thomas \"Cakeisalie5\" Touhey" Date: Mon, 27 Mar 2017 19:37:24 +0200 Subject: [PATCH] Corrected add-in encoding (size_t filesize = 0). --- include/libg1m/format/cas.h | 6 ++-- include/libg1m/internals/log.h | 2 +- src/core/log.c | 63 +++++++++++++++------------------- src/encode/addin.c | 2 +- src/manage/handle.c | 24 +++++++------ 5 files changed, 46 insertions(+), 51 deletions(-) diff --git a/include/libg1m/format/cas.h b/include/libg1m/format/cas.h index 5f1673c..32e7d17 100644 --- a/include/libg1m/format/cas.h +++ b/include/libg1m/format/cas.h @@ -138,13 +138,13 @@ struct _cas100 { /* driver number, in ASCII (0x30 + ) */ uint8_t id; - /* ExportDrive: 0x00000400, size? */ + /* ExportDrive: 0x400, size? */ uint32_t _size; - /* ExportDrive: 128, type? */ + /* ExportDrive: 0x80, type? */ uint32_t _type; - /* ExportDrive: 0x00020000, other size? */ + /* ExportDrive: 0x20000, other size? */ uint32_t _othersize; /* 0xFFs */ diff --git a/include/libg1m/internals/log.h b/include/libg1m/internals/log.h index 0bb0b82..b2ebd11 100644 --- a/include/libg1m/internals/log.h +++ b/include/libg1m/internals/log.h @@ -84,7 +84,7 @@ # endif /* Log memory (behind the `logm_*` macros) */ -extern void g1m_log_mem(const char *prefix, void *m, size_t n); +extern void g1m_log_mem(const char *prefix, const void *m, size_t n); /* Type strings */ extern const char *g1m_get_type_string(int main_id, int type); diff --git a/src/core/log.c b/src/core/log.c index 17ae664..c08ef51 100644 --- a/src/core/log.c +++ b/src/core/log.c @@ -42,14 +42,14 @@ static void putascii(unsigned char *p, unsigned int i, int n) { /* goto end of the field */ p += (n - 1); - /* for each digit */ while (n--) { - /* get end digit from this point */ - int j = i % 16; - /* put it in ASCII-hex */ + /* get end digit from this point, put in ASCII-hex */ + div_t dv = div(i, 16); + int j = dv.rem; *p-- = j >= 10 ? j - 10 + 'A' : j + '0'; - /* then go to digit that's left */ - i /= 16; + + /* go to the digit to the left */ + i = dv.quot; } } @@ -62,19 +62,21 @@ static void putascii(unsigned char *p, unsigned int i, int n) * @arg n the size of the memory zone */ -static void log_mem_hex(char *s, unsigned char *m, size_t n) +static void log_mem_hex(char *s, const unsigned char *m, size_t n) { - size_t l = 0; - while (l < 16) { - /* put the hex number */ - if (n) putascii((unsigned char*)s, *m++, 2), s += 2; - else *s++ = ' ', *s++ = ' '; + for (size_t l = 0; l < 8; l++) { + /* put the first hex number */ + if (n) putascii((unsigned char*)s, *m++, 2); + else { *s++ = ' '; *s-- = ' '; } + s += 2; n -= !!n; - /* decrement size of the memory zone to go */ - n -= !!n; + /* put the second hex number */ + if (n) putascii((unsigned char*)s, *m++, 2); + else { *s++ = ' '; *s-- = ' '; } + s += 2; n -= !!n; - /* go to next character if s is at the ending of a group */ - if (l++ % 2) s++; + /* put a space */ + *s++ = ' '; } } @@ -87,17 +89,12 @@ static void log_mem_hex(char *s, unsigned char *m, size_t n) * @arg n the size of the memory zone */ -static void log_mem_asc(char *s, unsigned char *m, size_t n) +static void log_mem_asc(char *s, const unsigned char *m, size_t n) { - size_t l = 0; - /* for each byte */ - while (n-- && l++ < 16) { - /* put the character (or a dot if non printable) */ - if (isprint(*m++)) *s++ = *((char*)m - 1); - else *s++ = '.'; - } - /* put the line ending */ - *s++ = '\n', *s = '\0'; + for (n = min(n, 16); n; n--, m++) + *s++ = isprint(*m) ? *m : '.'; + *s++ = '\n'; + *s = '\0'; } /** @@ -109,7 +106,7 @@ static void log_mem_asc(char *s, unsigned char *m, size_t n) * @arg n the size of the memory zone */ -void g1m_log_mem(const char *prefx, void *m, size_t n) +void g1m_log_mem(const char *prefx, const void *m, size_t n) { /* if nothing, print it directly */ if (!n) fprintf(stderr, "%s(nothing)\n", prefx); @@ -122,16 +119,12 @@ void g1m_log_mem(const char *prefx, void *m, size_t n) memcpy(&linebuf[lineoff], "0000 0000 0000 0000 0000 0000 0000 0000 ", 40); /* then loop-loop-loop-loop-loop */ - unsigned char *p = m; - while (n > 0) { - /* fill in ascii-hex part */ + for (const unsigned char *p = m; n > 0; p += 16, n -= min(16, n)) { + /* prepare the parts */ log_mem_hex(&linebuf[lineoff], p, n); - /* fill in ascii part */ log_mem_asc(&linebuf[lineoff + 40], p, n); - /* then print line */ + + /* put the line */ fputs(linebuf, stderr); - /* and increment pointer */ - p += 16; - n -= min(16, n); } } diff --git a/src/encode/addin.c b/src/encode/addin.c index e194e12..a5c7dd9 100644 --- a/src/encode/addin.c +++ b/src/encode/addin.c @@ -48,7 +48,7 @@ int g1m_encode_addin(g1m_handle_t *handle, g1m_buffer_t *buffer) size_t i; uint8_t *b; /* make the StandardHeader up */ - size_t filesize; g1m_announce_addin(handle, &filesize); + size_t filesize = 0; g1m_announce_addin(handle, &filesize); struct standard_header std = { .main_id = "USBPower", .subtype = "\xF3\x00\x10\x00\x10\x00", diff --git a/src/manage/handle.c b/src/manage/handle.c index 1bbd572..b8d9e2e 100644 --- a/src/manage/handle.c +++ b/src/manage/handle.c @@ -194,29 +194,31 @@ int g1m_make_addin(g1m_handle_t **h, g1m_platform_t platform, size_t size, /* check the platform */ unsigned int width, height; - int titlesize = 8; + int titlesize = 8, intsize = 8; switch (platform) { case g1m_platform_fx: - width = G1A_ICON_WIDTH; - height = G1A_ICON_HEIGHT; + width = G1A_ICON_WIDTH; height = G1A_ICON_HEIGHT; break; case g1m_platform_cp: - width = C1A_ICON_WIDTH; - height = C1A_ICON_HEIGHT; - titlesize = 16; - break; + width = C1A_ICON_WIDTH; height = C1A_ICON_HEIGHT; + titlesize = 16; break; default: /* case g1m_platform_cg: */ width = G3A_ICON_WIDTH; height = G3A_ICON_HEIGHT; - titlesize = 16; + titlesize = 16; break; } - /* copy the name and internal name */ - int i; for (i = 0; isupper(name[i]) && i < titlesize; i++) + /* copy the addin title */ + int i; for (i = 0; (isupper(name[i]) || isdigit(name[i])) + && i < titlesize; i++) handle->title[i] = name[i]; handle->title[i] = 0; + if (!handle->title[0]) strcpy(handle->title, "@ADDIN"); + + /* copy the internal name */ handle->intname[0] = '@'; - for (i = 1; isupper(internal[i]) && i < 8; i++) + for (i = 1; (isupper(internal[i]) || isdigit(internal[i])) + && i < intsize; i++) handle->intname[i] = internal[i]; handle->intname[i] = 0;