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