fixed double free - fixed probe function
[libav.git] / libav / avio.c
CommitLineData
de6d9b64
FB
1/*
2 * Unbuffered io for ffmpeg system
3 * Copyright (c) 2001 Gerard Lantau
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
de6d9b64
FB
19#include "avformat.h"
20
21URLProtocol *first_protocol = NULL;
22
23int register_protocol(URLProtocol *protocol)
24{
25 URLProtocol **p;
26 p = &first_protocol;
27 while (*p != NULL) p = &(*p)->next;
28 *p = protocol;
29 protocol->next = NULL;
30 return 0;
31}
32
33int url_open(URLContext **puc, const char *filename, int flags)
34{
35 URLContext *uc;
36 URLProtocol *up;
37 const char *p;
38 char proto_str[128], *q;
39 int err;
40
41 p = filename;
42 q = proto_str;
43 while (*p != '\0' && *p != ':') {
44 if ((q - proto_str) < sizeof(proto_str) - 1)
45 *q++ = *p;
46 p++;
47 }
a74127c0
FB
48 /* if the protocol has length 1, we consider it is a dos drive */
49 if (*p == '\0' || (q - proto_str) <= 1) {
de6d9b64
FB
50 strcpy(proto_str, "file");
51 } else {
52 *q = '\0';
53 }
54
55 up = first_protocol;
56 while (up != NULL) {
a74127c0 57 if (!strcmp(proto_str, up->name))
de6d9b64
FB
58 goto found;
59 up = up->next;
60 }
61 return -ENOENT;
62 found:
1ea4f593 63 uc = av_malloc(sizeof(URLContext));
de6d9b64
FB
64 if (!uc)
65 return -ENOMEM;
66 uc->prot = up;
67 uc->flags = flags;
68 uc->is_streamed = 0; /* default = not streamed */
69 uc->packet_size = 1; /* default packet size */
70 err = up->url_open(uc, filename, flags);
71 if (err < 0) {
1ea4f593 72 av_free(uc);
de6d9b64
FB
73 *puc = NULL;
74 return err;
75 }
76 *puc = uc;
77 return 0;
78}
79
80int url_read(URLContext *h, unsigned char *buf, int size)
81{
82 int ret;
83 if (h->flags & URL_WRONLY)
84 return -EIO;
85 ret = h->prot->url_read(h, buf, size);
86 return ret;
87}
88
89int url_write(URLContext *h, unsigned char *buf, int size)
90{
91 int ret;
92 if (!(h->flags & URL_WRONLY))
93 return -EIO;
94 ret = h->prot->url_write(h, buf, size);
95 return ret;
96}
97
98offset_t url_seek(URLContext *h, offset_t pos, int whence)
99{
100 offset_t ret;
101
102 if (!h->prot->url_seek)
103 return -EPIPE;
104 ret = h->prot->url_seek(h, pos, whence);
105 return ret;
106}
107
de6d9b64
FB
108int url_close(URLContext *h)
109{
110 int ret;
111
112 ret = h->prot->url_close(h);
1ea4f593 113 av_free(h);
de6d9b64
FB
114 return ret;
115}
116
117int url_exist(const char *filename)
118{
119 URLContext *h;
120 if (url_open(&h, filename, URL_RDONLY) < 0)
121 return 0;
122 url_close(h);
123 return 1;
124}
125
126offset_t url_filesize(URLContext *h)
127{
128 offset_t pos, size;
129
130 pos = url_seek(h, 0, SEEK_CUR);
131 size = url_seek(h, 0, SEEK_END);
132 url_seek(h, pos, SEEK_SET);
133 return size;
134}