]> git.draconx.ca Git - upkg.git/blob - src/engine/sound.gob
Stop using gnulib's flexmember module.
[upkg.git] / src / engine / sound.gob
1 %alltop{
2 /*
3  * upkg: tool for manipulating Unreal Tournament packages.
4  * Copyright © 2012, 2022 Nick Bowler
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 %}
20
21 %ctop{
22 #include <config.h>
23 %}
24
25 %{
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <strings.h>
29 #include <uobject/exportable.h>
30 #include "pack.h"
31 %}
32
33 %h{
34 #include <uobject/uobject.h>
35 %}
36
37 class Engine:Sound from U:Object (dynamic)
38         (interface U:Object:Exportable)
39 {
40         interface U:Object:Exportable
41         private int export(U:Object *uo, FILE *outf)
42         {
43                 struct upkg_file *f = uo->pkg_file;
44                 unsigned char buf[1024];
45                 size_t buflen;
46
47                 if (upkg_export_seek(f, 0, SEEK_SET) != 0)
48                         return -1;
49
50                 do {
51                         buflen = upkg_export_read(f, buf, sizeof buf);
52                         if (fwrite(buf, buflen, 1, outf) != 1)
53                                 return -1;
54                 } while (buflen == sizeof buf);
55
56                 if (!f->eof)
57                         return -1;
58                 return 0;
59         }
60
61         interface U:Object:Exportable
62         private int export_name(U:Object *uo, char *buf, size_t n)
63         {
64                 return snprintf(buf, n, "%s.wav", uo->pkg_file->name);
65         }
66
67         override (U:Object) int deserialize(U:Object *uo)
68         {
69                 struct upkg_file *f = uo->pkg_file;
70                 size_t rc, pos = 0, buflen;
71                 unsigned char buf[32];
72                 unsigned long end_offset;
73                 const char *fmt_name;
74                 long size, fmt;
75
76                 PARENT_HANDLER(uo);
77
78                 buflen = upkg_export_read(f, buf, sizeof buf);
79
80                 /* This seems to be the name of the format.  The only format
81                  * that actually gets used is WAV, so we can check that. */
82                 rc = upkg_decode_index(&fmt, buf+pos, buflen-pos);
83                 if (rc == 0)
84                         return -1;
85                 pos += rc;
86
87                 fmt_name = upkg_get_name(f->pkg, fmt);
88                 if (!fmt_name || strcasecmp(fmt_name, "wav") != 0) {
89                         u_err(uo, "format name is not WAV");
90                         return -1;
91                 }
92
93                 /* Like textures, there's an (apparently useless) offset to
94                  * the end of the sound data which is implied by the next
95                  * field.  Just validate it. */
96                 if (f->pkg->version >= 63) {
97                         if (pos + 4 > buflen)
98                                 return -1;
99                         end_offset = unpack_32_le(buf+pos);
100                         pos += 4;
101                 }
102
103                 rc = upkg_decode_index(&size, buf+pos, buflen-pos);
104                 if (rc == 0 || size < 0 || size > f->len - pos)
105                         return -1;
106                 pos += rc;
107
108                 f->base += pos;
109                 f->len   = size;
110                 upkg_export_seek(f, 0, SEEK_SET);
111
112                 if (f->pkg->version >= 63) {
113                         if (end_offset != f->base + f->len) {
114                                 u_err(uo, "end offset does not match size");
115                                 return -1;
116                         }
117                 }
118
119                 return 0;
120         }
121 }