ntdll: Tweak the file mapping permission checks some more, with tests.
[wine] / dlls / winemp3.acm / interface.c
1 /*
2  * Copyright (c) Michael Hipp and other authors of the mpglib project.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/debug.h"
27 #include "mpg123.h"
28 #include "mpglib.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(mpeg3);
31
32 BOOL InitMP3(struct mpstr *mp)
33 {
34         static int init = 0;
35
36         memset(mp,0,sizeof(struct mpstr));
37
38         mp->framesize = 0;
39         mp->fsizeold = -1;
40         mp->bsize = 0;
41         mp->head = mp->tail = NULL;
42         mp->fr.single = -1;
43         mp->bsnum = 0;
44         mp->synth_bo = 1;
45         mp->fr.mp = mp;
46
47         if(!init) {
48                 init = 1;
49                 make_decode_tables(32767);
50                 init_layer2();
51                 init_layer3(SBLIMIT);
52         }
53
54         return !0;
55 }
56
57 void ClearMP3Buffer(struct mpstr *mp)
58 {
59         struct buf *b,*bn;
60
61         b = mp->tail;
62         while(b) {
63                 HeapFree(GetProcessHeap(), 0, b->pnt);
64                 bn = b->next;
65                 HeapFree(GetProcessHeap(), 0, b);
66                 b = bn;
67         }
68         mp->tail = NULL;
69         mp->head = NULL;
70         mp->bsize = 0;
71 }
72
73 static struct buf *addbuf(struct mpstr *mp,const unsigned char *buf,int size)
74 {
75         struct buf *nbuf;
76
77         nbuf = HeapAlloc(GetProcessHeap(), 0, sizeof(struct buf));
78         if(!nbuf) {
79                 WARN("Out of memory!\n");
80                 return NULL;
81         }
82         nbuf->pnt = HeapAlloc(GetProcessHeap(), 0, size);
83         if(!nbuf->pnt) {
84                 HeapFree(GetProcessHeap(), 0, nbuf);
85                 WARN("Out of memory!\n");
86                 return NULL;
87         }
88         nbuf->size = size;
89         memcpy(nbuf->pnt,buf,size);
90         nbuf->next = NULL;
91         nbuf->prev = mp->head;
92         nbuf->pos = 0;
93
94         if(!mp->tail) {
95                 mp->tail = nbuf;
96         }
97         else {
98           mp->head->next = nbuf;
99         }
100
101         mp->head = nbuf;
102         mp->bsize += size;
103
104         return nbuf;
105 }
106
107 static void remove_buf(struct mpstr *mp)
108 {
109   struct buf *buf = mp->tail;
110
111   mp->tail = buf->next;
112   if(mp->tail)
113     mp->tail->prev = NULL;
114   else {
115     mp->tail = mp->head = NULL;
116   }
117
118   HeapFree(GetProcessHeap(), 0, buf->pnt);
119   HeapFree(GetProcessHeap(), 0, buf);
120
121 }
122
123 static int read_buf_byte(struct mpstr *mp)
124 {
125         unsigned int b;
126
127         int pos;
128
129         pos = mp->tail->pos;
130         while(pos >= mp->tail->size) {
131                 remove_buf(mp);
132                 pos = mp->tail->pos;
133         }
134
135         b = mp->tail->pnt[pos];
136         mp->bsize--;
137         mp->tail->pos++;
138
139
140         return b;
141 }
142
143 static void read_head(struct mpstr *mp)
144 {
145         unsigned long head;
146
147         head = read_buf_byte(mp);
148         head <<= 8;
149         head |= read_buf_byte(mp);
150         head <<= 8;
151         head |= read_buf_byte(mp);
152         head <<= 8;
153         head |= read_buf_byte(mp);
154
155         mp->header = head;
156 }
157
158 int decodeMP3(struct mpstr *mp,const unsigned char *in,int isize,unsigned char *out,
159                 int osize,int *done)
160 {
161         int len;
162
163         if(osize < 4608) {
164                 ERR("Output buffer too small\n");
165                 return MP3_ERR;
166         }
167
168         if(in) {
169                 if(addbuf(mp,in,isize) == NULL) {
170                         return MP3_ERR;
171                 }
172         }
173
174         /* First decode header */
175         if(mp->framesize == 0) {
176                 int ret;
177                 if(mp->bsize < 4) {
178                         return MP3_NEED_MORE;
179                 }
180                 read_head(mp);
181                 while (!(ret = decode_header(&mp->fr,mp->header)) && mp->bsize)
182                 {
183                         mp->header = mp->header << 8;
184                         mp->header |= read_buf_byte(mp);
185                 }
186
187                 if (!ret) {
188                         return MP3_NEED_MORE;
189                 }
190                 mp->framesize = mp->fr.framesize;
191         }
192
193         if(mp->fr.framesize > mp->bsize)
194                 return MP3_NEED_MORE;
195
196         wordpointer = mp->bsspace[mp->bsnum] + 512;
197         mp->bsnum = (mp->bsnum + 1) & 0x1;
198         bitindex = 0;
199
200         len = 0;
201         while(len < mp->framesize) {
202                 int nlen;
203                 int blen = mp->tail->size - mp->tail->pos;
204                 if( (mp->framesize - len) <= blen) {
205                   nlen = mp->framesize-len;
206                 }
207                 else {
208                   nlen = blen;
209                 }
210                 memcpy(wordpointer+len,mp->tail->pnt+mp->tail->pos,nlen);
211                 len += nlen;
212                 mp->tail->pos += nlen;
213                 mp->bsize -= nlen;
214                 if(mp->tail->pos == mp->tail->size) {
215                    remove_buf(mp);
216                 }
217         }
218
219         *done = 0;
220         if(mp->fr.error_protection)
221            getbits(16);
222         switch(mp->fr.lay) {
223           case 1:
224             do_layer1(&mp->fr,out,done);
225             break;
226           case 2:
227             do_layer2(&mp->fr,out,done);
228             break;
229           case 3:
230             do_layer3(&mp->fr,out,done);
231             break;
232         }
233
234         mp->fsizeold = mp->framesize;
235         mp->framesize = 0;
236
237         return MP3_OK;
238 }
239
240 int set_pointer(struct mpstr *mp, long backstep)
241 {
242   unsigned char *bsbufold;
243   if(mp->fsizeold < 0 && backstep > 0) {
244     /* This is not a bug if we just did seeking, the first frame is dropped then */
245     WARN("Can't step back %ld!\n",backstep);
246     return MP3_ERR;
247   }
248   bsbufold = mp->bsspace[mp->bsnum] + 512;
249   wordpointer -= backstep;
250   if (backstep)
251     memcpy(wordpointer,bsbufold+mp->fsizeold-backstep,backstep);
252   bitindex = 0;
253   return MP3_OK;
254 }