changeset 1780:5ea4713e9e0f

Change c64 format probing API to use DMGrowBuf.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 12 Jun 2018 20:37:53 +0300
parents 20bf4140eaa1
children e53dc89c0e56
files tools/64vw.c tools/lib64fmts.c tools/lib64gfx.c tools/lib64gfx.h
diffstat 4 files changed, 58 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/tools/64vw.c	Tue Jun 12 18:33:35 2018 +0300
+++ b/tools/64vw.c	Tue Jun 12 20:37:53 2018 +0300
@@ -195,10 +195,12 @@
     if ((ret = dmReadDataFile(NULL, filename, &dataBuf, &dataSize)) != DMERR_OK)
         goto exit;
 
+    dmGrowBufConstCreateFrom(&tmp, dataBuf, dataSize);
+
     if (optProbeOnly)
-        ret = dmC64ProbeBMP(dataBuf, dataSize, fmt) != DM_PROBE_SCORE_FALSE ? DMERR_OK : DMERR_NOT_SUPPORTED;
+        ret = dmC64ProbeBMP(&tmp, fmt) != DM_PROBE_SCORE_FALSE ? DMERR_OK : DMERR_NOT_SUPPORTED;
     else
-        ret = dmC64DecodeBMP(cimage, dmGrowBufConstCreateFrom(&tmp, dataBuf, dataSize), 0, 2, fmt, forced);
+        ret = dmC64DecodeBMP(cimage, &tmp, 0, 2, fmt, forced);
 
 exit:
     dmFree(dataBuf);
--- a/tools/lib64fmts.c	Tue Jun 12 18:33:35 2018 +0300
+++ b/tools/lib64fmts.c	Tue Jun 12 20:37:53 2018 +0300
@@ -10,12 +10,12 @@
 
 
 
-static int fmtProbeKoalaPaintPacked(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeKoalaPaintPacked(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
     // Attempt to prevent misprobes of unpacked Koala and Run Paint
-    if (len > 30 &&
-        len != 10006 &&
-        len != 10003 &&
+    if (buf->len > 30 &&
+        buf->len != 10006 &&
+        buf->len != 10003 &&
         dmCompareAddr16(buf, 0, fmt->addr))
         return DM_PROBE_SCORE_GOOD;
 
@@ -70,10 +70,10 @@
 }
 
 
-static int fmtProbeDrazPaint20Packed(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeDrazPaint20Packed(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
-    const Uint8 *ident = buf + 2;
-    if (len > 22 &&
+    const Uint8 *ident = buf->data + 2;
+    if (buf->len > 22 &&
         dmCompareAddr16(buf, 0, fmt->addr) &&
         memcmp(ident, "DRAZPAINT ", 10) == 0 &&
         ident[11] == '.' && (
@@ -146,11 +146,11 @@
 }
 
 
-static int fmtProbeDrazLace10Packed(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeDrazLace10Packed(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
-    if (len > 22 &&
+    if (buf->len > 22 &&
         dmCompareAddr16(buf, 0, fmt->addr) &&
-        memcmp(buf + 2, "DRAZLACE! 1.0", 13) == 0)
+        memcmp(buf->data + 2, "DRAZLACE! 1.0", 13) == 0)
         return DM_PROBE_SCORE_MAX;
 
     return DM_PROBE_SCORE_FALSE;
@@ -177,11 +177,11 @@
 
 static const char *fmtBDP5MagicID = "BDP 5.00";
 
-static int fmtProbeBDP5Packed(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeBDP5Packed(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
-    if (len > 20 &&
+    if (buf->len > 20 &&
         dmCompareAddr16(buf, 0, fmt->addr) &&
-        memcmp(buf + 2, fmtBDP5MagicID, strlen(fmtBDP5MagicID)) == 0)
+        memcmp(buf->data + 2, fmtBDP5MagicID, strlen(fmtBDP5MagicID)) == 0)
         return DM_PROBE_SCORE_MAX;
 
     return DM_PROBE_SCORE_FALSE;
@@ -255,11 +255,11 @@
 #define fmtGunPaintMagicLen   (14)
 #define fmtGunPaintMagicOffs  (0x3e8)
 
-static int fmtProbeGunPaint(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeGunPaint(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
-    if (len > 0x400 &&
+    if (buf->len > 0x400 &&
         dmCompareAddr16(buf, 0, fmt->addr) &&
-        memcmp(buf + fmtGunPaintMagicOffs + 2, fmtGunPaintMagicID, fmtGunPaintMagicLen) == 0)
+        memcmp(buf->data + fmtGunPaintMagicOffs + 2, fmtGunPaintMagicID, fmtGunPaintMagicLen) == 0)
         return DM_PROBE_SCORE_MAX;
 
     return DM_PROBE_SCORE_FALSE;
@@ -277,20 +277,21 @@
 }
 
 
-static int fmtProbeAmicaPaintPacked(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeAmicaPaintPacked(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
     size_t i, n;
 
-    if (len < 256 || !dmCompareAddr16(buf, 0, fmt->addr))
+    if (buf->len < 256 || !dmCompareAddr16(buf, 0, fmt->addr))
         return DM_PROBE_SCORE_FALSE;
 
     // Interpaint Hi-Res gives a false positive
     // as do some GunPaint images ..
-    if (len == 9002 || fmtProbeGunPaint(buf, len, fmt) > DM_PROBE_SCORE_GOOD)
+    if (buf->len == 9002 ||
+        fmtProbeGunPaint(buf, fmt) > DM_PROBE_SCORE_GOOD)
         return DM_PROBE_SCORE_FALSE;
 
-    for (n = 0, i = 2; i < len; i++)
-        if (buf[i] == 0xC2) n++;
+    for (n = 0, i = 2; i < buf->len; i++)
+        if (buf->data[i] == 0xC2) n++;
 
     if (n > 50)
         return DM_PROBE_SCORE_GOOD;
@@ -361,9 +362,9 @@
 }
 
 
-static int fmtProbeSaracenPaint(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeSaracenPaint(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
-    if ((len == 10219 || len == 10220) &&
+    if ((buf->len == 10219 || buf->len == 10220) &&
         dmCompareAddr16(buf, 0, fmt->addr))
         return DM_PROBE_SCORE_GOOD;
 
@@ -371,9 +372,9 @@
 }
 
 
-static int fmtProbeFLIDesigner(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeFLIDesigner(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
-    if (len == fmt->size &&
+    if (buf->len == fmt->size &&
         (dmCompareAddr16(buf, 0, 0x3c00) || dmCompareAddr16(buf, 0, 0x3ff0)))
         return DM_PROBE_SCORE_GOOD;
 
@@ -402,7 +403,7 @@
 }
 
 
-static int fmtProbeTruePaintPacked(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeTruePaintPacked(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
     // The beginning/un-changing part of the BASIC bootstrap and
     // relocation of decompression code
@@ -414,8 +415,8 @@
         0xff, 0xc6, 0xfe
     };
 
-    if (len >= 512 && dmCompareAddr16(buf, 0, fmt->addr) &&
-        memcmp(buf + 2, magicID, sizeof(magicID)) == 0)
+    if (buf->len >= 512 && dmCompareAddr16(buf, 0, fmt->addr) &&
+        memcmp(buf->data + 2, magicID, sizeof(magicID)) == 0)
         return DM_PROBE_SCORE_MAX;
 
     return DM_PROBE_SCORE_FALSE;
@@ -538,9 +539,10 @@
 #define XX2_BSIZE      (XX2_SIZE * 8)
 
 
-static int fmtProbeFormatXX2(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeFormatXX2(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
-    if (len >= XX2_MIN_SIZE && len <= XX2_MIN_SIZE + 8 &&
+    if (buf->len >= XX2_MIN_SIZE &&
+        buf->len <= XX2_MIN_SIZE + 8 &&
         dmCompareAddr16(buf, 0, fmt->addr))
         return DM_PROBE_SCORE_MAYBE;
 
@@ -571,18 +573,18 @@
 static const char *fmtFunPaint2MagicID = "FUNPAINT (MT) ";
 
 
-static int fmtProbeFunPaint2(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeFunPaint2(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
-    if (len > 30 &&
+    if (buf->len > 30 &&
         dmCompareAddr16(buf, 0, fmt->addr) &&
-        memcmp(buf + 2, fmtFunPaint2MagicID, strlen(fmtFunPaint2MagicID)) == 0)
+        memcmp(buf->data + 2, fmtFunPaint2MagicID, strlen(fmtFunPaint2MagicID)) == 0)
     {
         // Unpacked variant
-        if (fmt->size != 0 && buf[14 + 2] == 0)
+        if (fmt->size != 0 && buf->data[14 + 2] == 0)
             return DM_PROBE_SCORE_MAX;
 
         // Packed variant
-        if (fmt->size == 0 && buf[14 + 2] != 0)
+        if (fmt->size == 0 && buf->data[14 + 2] != 0)
             return DM_PROBE_SCORE_MAX;
     }
 
@@ -811,20 +813,20 @@
 }
 
 
-static int fmtProbeECIPacked(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt)
+static int fmtProbeECIPacked(const DMGrowBuf *buf, const DMC64ImageFormat *fmt)
 {
     size_t i, n;
 
     // XXX TODO: Perhaps count statistics about used byte values
     // and compare to value in buf[2] which is the RLE marker
-    if (len < 128 ||
+    if (buf->len < 128 ||
         !dmCompareAddr16(buf, 0, fmt->addr) ||
         // Try to avoid misprobe of Crest Hires FLI Designer and Cosmos Design format
-        len == 16386 || len == 16385)
+        buf->len == 16386 || buf->len == 16385)
         return DM_PROBE_SCORE_FALSE;
 
-    for (n = 0, i = 3; i < len; i++)
-        if (buf[i] == buf[2]) n++;
+    for (n = 0, i = 3; i < buf->len; i++)
+        if (buf->data[i] == buf->data[2]) n++;
 
     if (n > 50)
         return DM_PROBE_SCORE_GOOD;
--- a/tools/lib64gfx.c	Tue Jun 12 18:33:35 2018 +0300
+++ b/tools/lib64gfx.c	Tue Jun 12 20:37:53 2018 +0300
@@ -138,10 +138,12 @@
 }
 
 
-BOOL dmCompareAddr16(const Uint8 *buf, const size_t offs, const Uint16 addr)
+BOOL dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr)
 {
-    return buf[offs    ] == DM_GET_ADDR_LO(addr) &&
-           buf[offs + 1] == DM_GET_ADDR_HI(addr);
+    return
+        offs + 1 < buf->len &&
+        buf->data[offs    ] == DM_GET_ADDR_LO(addr) &&
+        buf->data[offs + 1] == DM_GET_ADDR_HI(addr);
 }
 
 
@@ -1341,7 +1343,7 @@
             return DMERR_OUT_OF_DATA;
 
         dmGrowBufConstCopyOffs(&tmp, buf, probeOffs);
-        if (dmC64ProbeBMP(tmp.data, tmp.len, fmt) == DM_PROBE_SCORE_FALSE)
+        if (dmC64ProbeBMP(&tmp, fmt) == DM_PROBE_SCORE_FALSE)
             return DMERR_NOT_SUPPORTED;
     }
 
@@ -1441,7 +1443,7 @@
 // if it contains a supported "C64" image format. Returns the
 // "probe score", see libgfx.h for list of values. If a match
 // is found, pointer to format description is set to *pfmt.
-int dmC64ProbeBMP(const Uint8 *buf, const size_t len, const DMC64ImageFormat **pfmt)
+int dmC64ProbeBMP(const DMGrowBuf *buf, const DMC64ImageFormat **pfmt)
 {
     int scoreMax = DM_PROBE_SCORE_FALSE, scoreIndex = -1;
 
@@ -1452,12 +1454,12 @@
         if (fmt->probe == NULL && fmt->size > 0 && fmt->addr > 0)
         {
             // Generic probe just checks matching size and load address
-            if (len == fmt->size && dmCompareAddr16(buf, 0, fmt->addr))
+            if (buf->len == fmt->size && dmCompareAddr16(buf, 0, fmt->addr))
                 score = DM_PROBE_SCORE_GOOD;
         }
         else
         if (fmt->probe != NULL)
-            score = fmt->probe(buf, len, fmt);
+            score = fmt->probe(buf, fmt);
 
         if (score > scoreMax)
         {
--- a/tools/lib64gfx.h	Tue Jun 12 18:33:35 2018 +0300
+++ b/tools/lib64gfx.h	Tue Jun 12 20:37:53 2018 +0300
@@ -211,7 +211,7 @@
 
     int  flags;  // DM_FMT_* flags, see libgfx.h
 
-    int  (*probe)(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt);
+    int  (*probe)(const DMGrowBuf *buf, const DMC64ImageFormat *fmt);
 
     int  (*decode)(DMC64Image *img, const DMGrowBuf *buf, const DMC64ImageFormat *fmt);
     int  (*encode)(DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt);
@@ -287,7 +287,7 @@
 // Miscellaneous functions
 //
 void      dmC64InitializeFormats(void);
-int       dmC64ProbeBMP(const Uint8 *buf, const size_t len, const DMC64ImageFormat **fmt);
+int       dmC64ProbeBMP(const DMGrowBuf *buf, const DMC64ImageFormat **fmt);
 
 char *    dmC64GetImageTypeString(char *buf, const size_t len, const int type, const BOOL lng);
 void      dmC64ImageDump(FILE *fh, const DMC64Image *img, const DMC64ImageFormat *fmt);
@@ -295,7 +295,7 @@
 void      dmSetDefaultC64Palette(DMImage *img);
 BOOL      dmSetMixedColorC64Palette(DMImage *img);
 
-BOOL      dmCompareAddr16(const Uint8 *buf, const size_t offs, const Uint16 addr);
+BOOL      dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr);
 int       dmC64ImageGetNumBanks(const DMC64ImageFormat *fmt);
 
 // C64 bitmap image allocation/freeing