root/trunk/contrib/extractor/libmpq/wave.cpp @ 2

Revision 2, 5.6 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 *  wave.c -- this file contains decompression methods used by Storm.dll
3 *            to decompress wave files.
4 *
5 *  Copyright (C) 2003 Maik Broemme <mbroemme@plusserver.de>
6 *
7 *  This source was adepted from the C++ version of wave.cpp included
8 *  in stormlib. The C++ version belongs to the following authors,
9 *
10 *  Ladislav Zezula <ladik.zezula.net>
11 *  Tom Amigo <tomamigo@apexmail.com>
12 *
13 *  This program is free software; you can redistribute it and/or modify
14 *  it under the terms of the GNU General Public License as published by
15 *  the Free Software Foundation; either version 2 of the License, or
16 *  (at your option) any later version.
17 *
18 *  This program is distributed in the hope that it will be useful,
19 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 *  GNU General Public License for more details.
22 *
23 *  You should have received a copy of the GNU General Public License
24 *  along with this program; if not, write to the Free Software
25 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27
28#include "wave.h"
29
30/* Tables necessary dor decompression */
31static unsigned long wave_table_1503f120[] = {
32        0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000004, 0xFFFFFFFF, 0x00000002, 0xFFFFFFFF, 0x00000006,
33        0xFFFFFFFF, 0x00000001, 0xFFFFFFFF, 0x00000005, 0xFFFFFFFF, 0x00000003, 0xFFFFFFFF, 0x00000007,
34        0xFFFFFFFF, 0x00000001, 0xFFFFFFFF, 0x00000005, 0xFFFFFFFF, 0x00000003, 0xFFFFFFFF, 0x00000007, 
35        0xFFFFFFFF, 0x00000002, 0xFFFFFFFF, 0x00000004, 0xFFFFFFFF, 0x00000006, 0xFFFFFFFF, 0x00000008 
36};
37
38static unsigned long wave_table_1503f1a0[] = {
39        0x00000007, 0x00000008, 0x00000009, 0x0000000A, 0x0000000B, 0x0000000C, 0x0000000D, 0x0000000E,
40        0x00000010, 0x00000011, 0x00000013, 0x00000015, 0x00000017, 0x00000019, 0x0000001C, 0x0000001F,
41        0x00000022, 0x00000025, 0x00000029, 0x0000002D, 0x00000032, 0x00000037, 0x0000003C, 0x00000042,
42        0x00000049, 0x00000050, 0x00000058, 0x00000061, 0x0000006B, 0x00000076, 0x00000082, 0x0000008F,
43        0x0000009D, 0x000000AD, 0x000000BE, 0x000000D1, 0x000000E6, 0x000000FD, 0x00000117, 0x00000133,
44        0x00000151, 0x00000173, 0x00000198, 0x000001C1, 0x000001EE, 0x00000220, 0x00000256, 0x00000292,
45        0x000002D4, 0x0000031C, 0x0000036C, 0x000003C3, 0x00000424, 0x0000048E, 0x00000502, 0x00000583,
46        0x00000610, 0x000006AB, 0x00000756, 0x00000812, 0x000008E0, 0x000009C3, 0x00000ABD, 0x00000BD0,
47        0x00000CFF, 0x00000E4C, 0x00000FBA, 0x0000114C, 0x00001307, 0x000014EE, 0x00001706, 0x00001954,
48        0x00001BDC, 0x00001EA5, 0x000021B6, 0x00002515, 0x000028CA, 0x00002CDF, 0x0000315B, 0x0000364B,
49        0x00003BB9, 0x000041B2, 0x00004844, 0x00004F7E, 0x00005771, 0x0000602F, 0x000069CE, 0x00007462,
50        0x00007FFF
51};
52
53/*
54 *  Decompress a wave file, mono or stereo
55 *
56 *  Offset: 1500F230
57 */
58int libmpq_wave_decompress(unsigned char *out_buf, int out_length, unsigned char *in_buf, int in_length, int channels) {
59        byte_and_short out;
60        byte_and_short in;
61        unsigned char *in_end = in_buf + in_length;     /* End on input buffer */
62        unsigned long index;
63        long nr_array1[2];
64        long nr_array2[2];
65        int count = 0;
66
67        out.pb     = out_buf;
68        in.pb      = in_buf;
69        nr_array1[0] = 0x2C;
70        nr_array1[1] = 0x2C;
71        in.pw++;
72
73        /* 15007AD7 */
74        for (count = 0; count < channels; count++) {
75                long temp;
76                temp = *(short *)in.pw++;
77                nr_array2[count] = temp;
78                if (out_length < 2) {
79                        return out.pb - out_buf;
80                }
81                *out.pw++   = (unsigned short)temp;
82                out_length -= 2;
83        }
84        index = channels - 1;
85        while (in.pb < in_end) {
86                unsigned char one_byte = *in.pb++;
87                if (channels == 2) {
88                        index = (index == 0) ? 1 : 0;
89                }
90
91                /*
92                 * Get one byte from input buffer
93                 * 15007B25
94                 */
95                if (one_byte & 0x80) {
96                        /* 15007B32 */
97                        switch(one_byte & 0x7F) {
98                                case 0:                                 /* 15007B8E */
99                                        if (nr_array1[index] != 0) {
100                                                nr_array1[index]--;
101                                        }
102                                        if (out_length < 2) {
103                                                break;
104                                        }
105                                        *out.pw++ = (unsigned short)nr_array2[index];
106                                        out_length -= 2;
107                                        continue;
108                                case 1:                                 /* 15007B72 */
109                                        nr_array1[index] += 8;          /* EBX also */
110                                        if (nr_array1[index] > 0x58) {
111                                                nr_array1[index] = 0x58;
112                                        }
113                                        if (channels == 2) {
114                                                index = (index == 0) ? 1 : 0;
115                                        }
116                                        continue;
117                                case 2:
118                                        continue;
119                                default:
120                                        nr_array1[index] -= 8;
121                                        if (nr_array1[index] < 0) {
122                                                nr_array1[index] = 0;
123                                        }
124                                        if (channels != 2) {
125                                                continue;
126                                        }
127                                        index = (index == 0) ? 1 : 0;
128                                        continue;
129                        }
130                } else {
131                        unsigned long temp1 = wave_table_1503f1a0[nr_array1[index]];    /* EDI */
132                        unsigned long temp2 = temp1 >> in_buf[1];       /* ESI */
133                        long temp3 = nr_array2[index];                  /* ECX */
134                        if (one_byte & 0x01) {                          /* EBX = one_byte */
135                                temp2 += (temp1 >> 0);
136                        }
137                        if (one_byte & 0x02) {
138                                temp2 += (temp1 >> 1);
139                        }
140                        if (one_byte & 0x04) {
141                                temp2 += (temp1 >> 2);
142                        }
143                        if (one_byte & 0x08) {
144                                temp2 += (temp1 >> 3);
145                        }
146                        if (one_byte & 0x10) {
147                                temp2 += (temp1 >> 4);
148                        }
149                        if (one_byte & 0x20) {
150                                temp2 += (temp1 >> 5);
151                        }
152                        if(one_byte & 0x40) {
153                                temp3 -= temp2;
154                                if (temp3 <= (long)0xFFFF8000) {
155                                        temp3 = (long)0xFFFF8000;
156                                }
157                        } else {
158                                temp3 += temp2;
159                                if (temp3 >= 0x7FFF) {
160                                        temp3 = 0x7FFF;
161                                }
162                        }
163                        nr_array2[index] = temp3;
164                        if (out_length < 2) {
165                                break;
166                        }
167
168                        temp2 = nr_array1[index];
169                        one_byte &= 0x1F;
170                        *out.pw++ = (unsigned short)temp3;
171                        out_length -= 2;
172                        temp2 += wave_table_1503f120[one_byte];
173                        nr_array1[index] = temp2;
174
175                        if (nr_array1[index] < 0) {
176                                nr_array1[index] = 0;
177                        } else {
178                                if (nr_array1[index] > 0x58) {
179                                        nr_array1[index] = 0x58;
180                                }
181                        }
182                }
183        }
184        return (out.pb - out_buf);
185}
Note: See TracBrowser for help on using the browser.