-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfo.c
239 lines (218 loc) · 5.59 KB
/
lfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* wvWare
* Copyright (C) Caolan McNamara, Dom Lachowicz, and others
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include "wv.h"
/*
Word writes out the pllfo first by writing out a PL of LFO structures.
It then enumerates through each LFO to figure out how many LFOLVLs each
one has (LFO.clfolvl), and writes out, in order, each LFOLVL structure
followed by its corresponding LVL structure (if LFOLVL.fFormatting is set).
*/
static int
multiplication_will_overflow(U32 a, U32 b)
{
if((a > 0) && (b > 0) && (G_MAXUINT / a) >= b) {
return 0;
}
return 1;
}
int
wvGetLFO_records (LFO ** lfo, LFOLVL ** lfolvl, LVL ** lvl, U32 * nolfo,
U32 * nooflvl, U32 offset, U32 len, wvStream * fd)
{
U32 i;
U32 end;
*nooflvl = 0;
wvTrace (("lfo begins at %x len %d\n", offset, len));
wvStream_offset_from_end (fd, 0);
end = wvStream_tell (fd);
wvGetLFO_PLF (lfo, nolfo, offset, len, fd);
for (i = 0; i < *nolfo; i++)
*nooflvl += (*lfo)[i].clfolvl;
wvTrace (("pos %x %d\n", wvStream_tell (fd), *nooflvl));
wvTrace (("nolfo is %d nooflvl is %d\n", *nolfo, *nooflvl));
if ((*nooflvl == 0) ||
multiplication_will_overflow(sizeof (LFOLVL), *nooflvl) ||
multiplication_will_overflow(sizeof (LVL), *nooflvl))
{
*lfolvl = NULL;
*lvl = NULL;
return (0);
}
*lfolvl = (LFOLVL *) wvMalloc (sizeof (LFOLVL) * *nooflvl);
*lvl = (LVL *) wvMalloc (sizeof (LVL) * *nooflvl);
i = 0;
while (i < *nooflvl)
{
wvInitLVL (&((*lvl)[i]));
wvTrace (("%d pos now %x %d\n", i, wvStream_tell (fd), *nooflvl));
if (wvStream_tell (fd) == end)
{
wvWarning
("LFOLVL off the end of the file, continuing anyway\n");
i++;
continue;
}
wvGetLFOLVL (&((*lfolvl)[i]), fd);
#if 0
if (wvInvalidLFOLVL (&((*lfolvl)[i])))
continue;
#endif
if ((*lfolvl)[i].fFormatting)
{
wvTrace (("formatting set\n"));
wvGetLVL (&((*lvl)[i]), fd);
}
i++;
}
return (0);
}
int
wvGetLFO_PLF (LFO ** lfo, U32 * nolfo, U32 offset, U32 len, wvStream * fd)
{
U32 i;
if (len == 0)
{
*lfo = NULL;
*nolfo = 0;
}
else
{
wvStream_goto (fd, offset);
*nolfo = read_32ubit (fd);
wvTrace (("%d\n", *nolfo));
/* check for integer overflow */
if (multiplication_will_overflow(*nolfo, sizeof(LFO))) {
wvError (("Malicious document!\n"));
*nolfo = 0;
return (1);
} else {
*lfo = (LFO *) wvMalloc (*nolfo * sizeof(LFO));
if (*lfo == NULL)
{
wvError (("NO MEM 1, failed to alloc %d bytes\n",
*nolfo * sizeof (LFO)));
return (1);
}
for (i = 0; i < *nolfo; i++)
wvGetLFO (&((*lfo)[i]), fd);
}
}
return (0);
}
void
wvGetLFO (LFO * item, wvStream * fd)
{
int i;
item->lsid = read_32ubit (fd);
item->reserved1 = read_32ubit (fd);
item->reserved2 = read_32ubit (fd);
item->clfolvl = read_8ubit (fd);
for (i = 0; i < 3; i++)
item->reserved3[i] = read_8ubit (fd);
}
void
wvInitLFO (LFO * item)
{
int i;
item->lsid = 0;
item->reserved1 = 0;
item->reserved2 = 0;
item->clfolvl = 0;
for (i = 0; i < 3; i++)
item->reserved3[i] = 0;
}
void
wvGetLFOLVL (LFOLVL * item, wvStream * fd)
{
U8 temp8;
#ifdef PURIFY
wvInitLFOLVL (item);
#endif
item->iStartAt = read_32ubit (fd);
while (wvInvalidLFOLVL (item))
{
wvTrace (("pos %x\n", wvStream_tell (fd)));
item->iStartAt = read_32ubit (fd);
}
temp8 = read_8ubit (fd);
item->ilvl = temp8 & 0x0F;
item->fStartAt = (temp8 & 0x10) >> 4;
item->fFormatting = (temp8 & 0x20) >> 5;
item->reserved1 = (temp8 & 0xC0) >> 6;
item->reserved2 = read_8ubit (fd);
item->reserved3 = read_8ubit (fd);
item->reserved4 = read_8ubit (fd);
}
void
wvInitLFOLVL (LFOLVL * item)
{
item->iStartAt = 0;
item->ilvl = 0;
item->fStartAt = 0;
item->fFormatting = 0;
item->reserved1 = 0;
item->reserved2 = 0;
item->reserved3 = 0;
item->reserved4 = 0;
}
int
wvInvalidLFOLVL (LFOLVL * item)
{
if (item->iStartAt != 0xffffffff)
return (0);
#if 0
/*
a bloody russian doc, from Sergey V. Udaltsov <svu@pop.convey.ru> caused
the removal of this section
*/
if (item->ilvl != 0xf)
return (0);
if (item->fStartAt != 1)
return (0);
if (item->fFormatting != 1)
return (0);
if (item->reserved1 != 0x3)
return (0);
if (item->reserved2 != 0xff)
return (0);
if (item->reserved3 != 0xff)
return (0);
if (item->reserved4 != 0xff)
return (0);
#endif
wvWarning (("invalid list entry, trucking along happily anyway\n"));
return (1);
}
int
wvReleaseLFO_records (LFO ** lfo, LFOLVL ** lfolvl, LVL ** lvl, U32 nooflvl)
{
U32 i;
wvTrace (("releasing %d lvl records\n", nooflvl));
wvFree (*lfo);
wvFree (*lfolvl);
for (i = 0; i < nooflvl; i++)
wvReleaseLVL (&((*lvl)[i]));
wvFree (*lvl);
return (0);
}