2 * upkg: tool for manipulating Unreal Tournament packages.
3 * Copyright © 2009-2011, 2022 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 #include "music-module.h"
30 unsigned char header[64];
34 int music_mod_init(void)
39 void music_mod_exit(void)
43 struct music_mod *music_mod_open(struct upkg_file *f)
47 m = malloc(sizeof *m);
53 m->hdrlen = upkg_export_read(f, m->header, sizeof m->header);
57 void music_mod_close(struct music_mod *m)
62 int music_mod_dump(struct music_mod *m, FILE *of)
64 unsigned char buf[1024];
67 if (upkg_export_seek(m->f, 0, SEEK_SET) != 0) {
72 rc = upkg_export_read(m->f, buf, sizeof buf);
79 if (fwrite(buf, rc, 1, of) != 1) {
81 fprintf(stderr, "unexpected end-of-file.\n");
88 if (rc < sizeof buf) {
96 int is_xm(unsigned char *buf, unsigned long len)
98 static const char head[15] =
99 /* ASCII encoding of "Extended Module" */
100 "\x45\x78\x74\x65\x6e\x64\x65\x64\x20\x4d\x6f\x64\x75\x6c\x65";
102 if (len >= sizeof head && memcmp(head, buf, sizeof head) == 0)
107 int is_it(unsigned char *buf, unsigned long len)
109 static const char head[4] =
110 /* ASCII encoding of "IMPM" */
113 if (len >= sizeof head && memcmp(head, buf, sizeof head) == 0)
118 int is_s3m(unsigned char *buf, unsigned long len)
120 static const char head[4] =
121 /* ASCII encoding of "SCRM" */
124 if (len < 0x2c + sizeof head)
126 if (memcmp(head, buf+0x2c, sizeof head) == 0)
131 const char *music_mod_type(struct music_mod *mod)
133 if (is_xm(mod->header, mod->hdrlen))
135 if (is_it(mod->header, mod->hdrlen))
137 if (is_s3m(mod->header, mod->hdrlen))