]> git.draconx.ca Git - upkg.git/blob - src/engine/music-dummymod.c
Stop using gnulib's flexmember module.
[upkg.git] / src / engine / music-dummymod.c
1 /*
2  * upkg: tool for manipulating Unreal Tournament packages.
3  * Copyright © 2009-2011, 2022 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "music-module.h"
25 #include "upkg.h"
26
27 struct music_mod {
28         struct upkg_file *f;
29
30         unsigned char header[64];
31         unsigned long hdrlen;
32 };
33
34 int music_mod_init(void)
35 {
36         return 0;
37 }
38
39 void music_mod_exit(void)
40 {
41 }
42
43 struct music_mod *music_mod_open(struct upkg_file *f)
44 {
45         struct music_mod *m;
46
47         m = malloc(sizeof *m);
48         if (!m) {
49                 return NULL;
50         }
51         m->f = f;
52
53         m->hdrlen = upkg_export_read(f, m->header, sizeof m->header);
54         return m;
55 }
56
57 void music_mod_close(struct music_mod *m)
58 {
59         free(m);
60 }
61
62 int music_mod_dump(struct music_mod *m, FILE *of)
63 {
64         unsigned char buf[1024];
65         size_t rc;
66
67         if (upkg_export_seek(m->f, 0, SEEK_SET) != 0) {
68                 return -1;
69         }
70
71         while (1) {
72                 rc = upkg_export_read(m->f, buf, sizeof buf);
73                 if (rc == 0) {
74                         if (!m->f->eof)
75                                 return -1;
76                         return 0;
77                 }
78
79                 if (fwrite(buf, rc, 1, of) != 1) {
80                         if (feof(of)) {
81                                 fprintf(stderr, "unexpected end-of-file.\n");
82                         } else {
83                                 perror("fwrite");
84                         }
85                         return -1;
86                 }
87
88                 if (rc < sizeof buf) {
89                         if (!m->f->eof)
90                                 return -1;
91                         return 0;
92                 }
93         }
94 }
95
96 int is_xm(unsigned char *buf, unsigned long len)
97 {
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";
101
102         if (len >= sizeof head && memcmp(head, buf, sizeof head) == 0)
103                 return 1;
104         return 0;
105 }
106
107 int is_it(unsigned char *buf, unsigned long len)
108 {
109         static const char head[4] =
110                 /* ASCII encoding of "IMPM" */
111                 "\x49\x4d\x50\x4d";
112
113         if (len >= sizeof head && memcmp(head, buf, sizeof head) == 0)
114                 return 1;
115         return 0;
116 }
117
118 int is_s3m(unsigned char *buf, unsigned long len)
119 {
120         static const char head[4] =
121                 /* ASCII encoding of "SCRM" */
122                 "\x53\x43\x52\x4d";
123
124         if (len < 0x2c + sizeof head)
125                 return 0;
126         if (memcmp(head, buf+0x2c, sizeof head) == 0)
127                 return 1;
128         return 0;
129 }
130
131 const char *music_mod_type(struct music_mod *mod)
132 {
133         if (is_xm(mod->header, mod->hdrlen))
134                 return "xm";
135         if (is_it(mod->header, mod->hdrlen))
136                 return "it";
137         if (is_s3m(mod->header, mod->hdrlen))
138                 return "s3m";
139
140         return "unknown";
141 }