summaryrefslogtreecommitdiff
path: root/faad2/src/common/faad/filestream.c
blob: 0f82bdfcedda44311ab32526f8f05d2225fbe286 (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
**  
** 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.
**
** Any non-GPL usage of this software or parts of this software is strictly
** forbidden.
**
** Commercial non-GPL licensing of this software is possible.
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
**
** $Id: filestream.c,v 1.3 2003/07/29 08:20:11 menno Exp $
**/

/* Not very portable yet */

#include <winsock2.h> // Note: Must be *before* windows.h
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include "filestream.h"
#include "aacinfo.h"

/* TEMPROARY HACK */
#define CommonExit(A) MessageBox(NULL, A, "FAAD Plugin", MB_OK)

int winsock_init=0; // 0=winsock not initialized, 1=success
long m_local_buffer_size = 64;
long m_stream_buffer_size = 128;

FILE_STREAM *open_filestream(char *filename)
{
    FILE_STREAM *fs;

    if(StringComp(filename,"http://", 7) == 0)
    {
        fs = (FILE_STREAM *)LocalAlloc(LPTR, sizeof(FILE_STREAM) + m_stream_buffer_size * 1024);

        if(fs == NULL)
            return NULL;

        fs->data = (unsigned char *)&fs[1];

        if(http_file_open(filename, fs) < 0)
        {
            LocalFree(fs);
            return NULL;
        }

        fs->http = 1;
    }
    else
    {
        fs = (FILE_STREAM*)LocalAlloc(LPTR, sizeof(FILE_STREAM) + m_local_buffer_size * 1024);

        if(fs == NULL)
            return NULL;

        fs->data = (unsigned char *)&fs[1];

        fs->stream = CreateFile(filename, GENERIC_READ,
            FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
            OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0);
        if (fs->stream == INVALID_HANDLE_VALUE)
        {
            LocalFree(fs);
            return NULL;
        }

        fs->http = 0;
    }

    fs->buffer_length = 0;
    fs->buffer_offset = 0;
    fs->file_offset = 0;

    return fs;
}

int read_byte_filestream(FILE_STREAM *fs)
{
    if(fs->buffer_offset == fs->buffer_length)
    {
        fs->buffer_offset = 0;

        if(fs->http)
            fs->buffer_length = recv(fs->inetStream, fs->data, m_stream_buffer_size * 1024, 0);
        else
            ReadFile(fs->stream, fs->data, m_local_buffer_size * 1024, &fs->buffer_length, 0);

        if(fs->buffer_length <= 0)
        {
            if(fs->http)
            {
                int x;
                x = WSAGetLastError();

                if(x == 0)
                {
                    /* Equivalent of a successful EOF for HTTP */
                }
            }

            fs->buffer_length = 0;
            return -1;
        }
    }

    fs->file_offset++;

    return fs->data[fs->buffer_offset++];
}

int read_buffer_filestream(FILE_STREAM *fs, void *data, int length)
{
    int i, tmp;
    unsigned char *data2 = (unsigned char *)data;

    for(i=0; i < length; i++)
    {
        if((tmp = read_byte_filestream(fs)) < 0)
        {
            if(i)
			{
                break;
            }
			else
			{
                return -1;
            }
        }
        data2[i] = tmp;
    }

    return i;
}

unsigned long filelength_filestream(FILE_STREAM *fs)
{
    unsigned long fsize;

    if (fs->http)
    {
        fsize = fs->http_file_length;
    }
	else
	{
        fsize = GetFileSize(fs->stream, NULL);
    }

    return fsize;
}

void seek_filestream(FILE_STREAM *fs, unsigned long offset, int mode)
{
    if(fs->http)
    {
        return;
    }

	SetFilePointer(fs->stream, offset, NULL, mode);

	if(mode == FILE_CURRENT)
		fs->file_offset += offset;
	else if(mode == FILE_END)
		fs->file_offset = filelength_filestream(fs) + offset;
	else
		fs->file_offset = offset;

    fs->buffer_length = 0;
    fs->buffer_offset = 0;
}

unsigned long tell_filestream(FILE_STREAM *fs)
{
    return fs->file_offset;
}

void close_filestream(FILE_STREAM *fs)
{
    if(fs)
    {
        if (fs->http)
        {
            if (fs->inetStream)
            {
                /* The 'proper' way to close a TCP connection */
                if(fs->inetStream)
                {
                    CloseTCP(fs->inetStream);
                }
            }
        }
        else
        {
            if(fs->stream)
                CloseHandle(fs->stream);
        }

        LocalFree(fs);
        fs = NULL;
    }
}

int WinsockInit()
{
    /* Before using winsock, you must load the DLL... */
    WSADATA wsaData;

    /* Load version 2.0 */
    if (WSAStartup( MAKEWORD( 2, 0 ), &wsaData ))
    {
        /* Disable streaming */
        return -1;
    }

    winsock_init = 1;

    return 0;
}

void WinsockDeInit()
{
    /* Unload the DLL */

    if(winsock_init)
        WSACleanup();
}

int FindCRLF(char *str)
{
    int i;

    for(i=0; i != lstrlen(str) && str[i] != '\r'; i++);

    return i;
}

void CloseTCP(int s)
{
    char tempbuf[1024];

    /* Set the socket to ignore any new incoming data */
    shutdown(s, 1);

    /* Get any old remaining data */
    while(recv(s, tempbuf, 1024, 0) > 0);

    /* Deallocate the socket */
    closesocket(s);
}

int resolve_host(char *host, SOCKADDR_IN *sck_addr, unsigned short remote_port)
{
    HOSTENT *hp;

    if (isalpha(host[0]))
    {
        /* server address is a name */
        hp = gethostbyname(host);
    }
    else
    {
        unsigned long addr;
        /* Convert nnn.nnn address to a usable one */
        addr = inet_addr(host);
        hp = gethostbyaddr((char *)&addr, 4, AF_INET);
    }

    if (hp == NULL)
    {
        char tmp[128];
        wsprintf(tmp, "Error resolving host address [%s]!\n", host);
        CommonExit(tmp);
        return -1;
    }

    ZeroMemory(sck_addr, sizeof(SOCKADDR_IN));
    sck_addr->sin_family = AF_INET;
    sck_addr->sin_port = htons(remote_port);
    CopyMemory(&sck_addr->sin_addr, hp->h_addr, hp->h_length);

    return 0;
}

int http_file_open(char *url, FILE_STREAM *fs)
{
    SOCKET sck;
    SOCKADDR_IN host;
    char server[1024], file[1024], request[1024], *temp = NULL, *tmpfile = NULL;
    int i, j, port = 80, bytes_recv, http_code;

    /* No winsock, no streaming */
    if(!winsock_init)
    {
        return -1;
    }

    url += 7; // Skip over http://

    /* Extract data from the URL */
    for(i=0; url[i] != '/' && url[i] != ':' && url[i] != 0; i++);

    ZeroMemory(server, 1024);
    CopyMemory(server, url, i);

    if(url[i] == ':')
    {
        /* A certain port was specified */
        port = atol(url + (i + 1));
    }

    for(; url[i] != '/' && url[i] != 0; i++);

    ZeroMemory(file, 1024);

    CopyMemory(file, url + i, lstrlen(url));

    /* END OF URL PARSING */

    /* Create a TCP/IP socket */
    sck = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if(sck == INVALID_SOCKET)
    {
        CommonExit("Error creating TCP/IP new socket");
        return -1;
    }

    /* Resolve the host address (turn www.blah.com into an IP) */
    if(resolve_host(server, &host, (unsigned short)port))
    {
        CommonExit("Error resolving host address");
        CloseTCP(sck);
        return -1;
    }

    /* Connect to the server */
    if(connect(sck, (SOCKADDR *)&host, sizeof(SOCKADDR)) == SOCKET_ERROR)
    {
        CommonExit("Error connecting to remote server");
        CloseTCP(sck);
        return -1;
    }

	tmpfile = calloc(1, (strlen(file) * 3) + 1);

	/* Encode URL */
	for(i=0, j=0; i < (int)strlen(file); i++)
	{
		if((unsigned char)file[i] <= 31 || (unsigned char)file[i] >= 127)
		{
			/* encode ASCII-control characters */
			wsprintf(tmpfile + j, "%%%X", (unsigned char)file[i]);
			j += 3;
			continue;
		}
		else
		{
			switch(file[i])
			{
				/* encode characters that could confuse some servers */
				case ' ':
				case '"':
				case '>':
				case '<':
				case '#':
				case '%':
				case '{':
				case '}':
				case '|':
				case '\\':
				case '^':
				case '~':
				case '[':
				case ']':
				case '`':

				wsprintf(tmpfile + j, "%%%X", (unsigned char)file[i]);
				j += 3;
				continue;
			}
		}
		
		tmpfile[j] = file[i];
		j++;
	}

    wsprintf(request, "GET %s\r\n\r\n", tmpfile);

	free(tmpfile);

    /* Send the request */
    if(send(sck, request, lstrlen(request), 0) <= 0)
    {
        /* Error sending data */
        CloseTCP(sck);
        return -1;
    }

    ZeroMemory(request, 1024);

    /* Send the request */
    if((bytes_recv = recv(sck, request, 1024, 0)) <= 0)
    {
        /* Error sending data */
        CloseTCP(sck);
        return -1;
    }

    if(StringComp(request,"HTTP/1.", 7) != 0)
    {
        /* Invalid header */
        CloseTCP(sck);
        return -1;
    }

    http_code = atol(request + 9);

    if(http_code < 200 || http_code > 299)
    {
        /* HTTP error */
        CloseTCP(sck);
        return -1;
    }

	// Search for a length field
	fs->http_file_length = 0;

    /* Limit search to only 20 loops */
    if((temp = strstr(request, "Content-Length: ")) != NULL)
    {
		/* Has a content-length field, copy into structure */
		fs->http_file_length = atol(temp + 16);
	}

    /* Copy the handle data into the structure */
    fs->inetStream = sck;

    /* Copy any excess data beyond the header into the filestream buffers */
	temp = strstr(request, "\r\n\r\n");

	if(temp)
	{
		temp += 4;
	}

    if(temp - request < bytes_recv)
    {
        memcpy(fs->data, temp, (temp - request) - bytes_recv);
        fs->buffer_length = (temp - request) - bytes_recv;
        fs->buffer_offset = 0;
    }

    return 0;
}