From 36442ffa719d4bc9a767925f38f18e4a4a79002a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor?= Date: Sat, 25 Jun 2022 11:56:03 +0200 Subject: [PATCH] Update xqxdecode.c Xqxdecode has a fundamental flaw that means it never actually ran properly. It uses `fgets()` to read the header lines, under the assumption that they are newline-terminated. However, the most important of them all, the one preceding the ',XQX' magic number is actually null-terminated, so the program never ever correctly identifies the magic number and gets completely lost in the file. By adding our own `fgetsn()` that looks for both newline and null makes it work. --- xqxdecode.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/xqxdecode.c b/xqxdecode.c index f0ece6c..c477c60 100644 --- a/xqxdecode.c +++ b/xqxdecode.c @@ -142,6 +142,27 @@ proff(int curOff) printf("%6x: ", curOff); } +char* +fgetsn(char* str, int n, FILE* stream) +{ + register int c; + register char ch; + register char* cs; + cs = str; + + while (--n > 0 && (c = getc(stream)) != EOF) { + ch = (*cs++ = c); + if (ch == '\n') { + *cs = '\0'; + break; + } + else if (ch == '\0') + break; + } + + return (c == EOF && cs == str) ? NULL : str; +} + void decode(FILE *fp) { @@ -178,7 +199,7 @@ decode(FILE *fp) { char buf[1024]; - while (fgets(buf, sizeof(buf), fp)) + while (fgetsn(buf, sizeof(buf), fp)) { proff(curOff); if (buf[0] == '\033')