From 152bab24521ea7cefd9e5d8cd1460c5871bff34c Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Mon, 1 Feb 2010 18:30:04 -0500 Subject: [PATCH] music: Add simple file type detection to the dummy module. The dummy module can be a bit smarter so that the file extension of exported data is correct. Add support for xm, it and s3m. --- src/engine/music-dummymod.c | 55 +++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/src/engine/music-dummymod.c b/src/engine/music-dummymod.c index db18fa4..a4c8666 100644 --- a/src/engine/music-dummymod.c +++ b/src/engine/music-dummymod.c @@ -18,12 +18,16 @@ #include #include +#include #include "music-module.h" #include "upkg.h" struct music_mod { struct upkg_file *f; + + unsigned char header[64]; + unsigned long hdrlen; }; int music_mod_init(void) @@ -37,12 +41,15 @@ void music_mod_exit(void) struct music_mod *music_mod_open(struct upkg_file *f) { - struct music_mod *m = malloc(sizeof *m); + struct music_mod *m; - if (m) { - m->f = f; + m = malloc(sizeof *m); + if (!m) { + return NULL; } + m->f = f; + m->hdrlen = upkg_export_read(f, m->header, sizeof m->header); return m; } @@ -85,7 +92,49 @@ int music_mod_dump(struct music_mod *m, FILE *of) } } +int is_xm(unsigned char *buf, unsigned long len) +{ + static const char head[15] = + /* ASCII encoding of "Extended Module" */ + "\x45\x78\x74\x65\x6e\x64\x65\x64\x20\x4d\x6f\x64\x75\x6c\x65"; + + if (len >= sizeof head && memcmp(head, buf, sizeof head) == 0) + return 1; + return 0; +} + +int is_it(unsigned char *buf, unsigned long len) +{ + static const char head[4] = + /* ASCII encoding of "IMPM" */ + "\x49\x4d\x50\x4d"; + + if (len >= sizeof head && memcmp(head, buf, sizeof head) == 0) + return 1; + return 0; +} + +int is_s3m(unsigned char *buf, unsigned long len) +{ + static const char head[4] = + /* ASCII encoding of "SCRM" */ + "\x53\x43\x52\x4d"; + + if (len < 0x2c + sizeof head) + return 0; + if (memcmp(head, buf+0x2c, sizeof head) == 0) + return 1; + return 0; +} + const char *music_mod_type(struct music_mod *mod) { + if (is_xm(mod->header, mod->hdrlen)) + return "xm"; + if (is_it(mod->header, mod->hdrlen)) + return "it"; + if (is_s3m(mod->header, mod->hdrlen)) + return "s3m"; + return "unknown"; } -- 2.43.0