Only special-case absolute DOS paths on systems that support them.
[libav.git] / libavformat / avio.c
CommitLineData
de6d9b64
FB
1/*
2 * Unbuffered io for ffmpeg system
19720f15 3 * Copyright (c) 2001 Fabrice Bellard
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
245976da
DB
21
22#include "libavutil/avstring.h"
23#include "libavcodec/opt.h"
bc574408 24#include "os_support.h"
de6d9b64 25#include "avformat.h"
5acef35f
BA
26
27#if LIBAVFORMAT_VERSION_MAJOR >= 53
28/** @name Logging context. */
29/*@{*/
30static const char *urlcontext_to_name(void *ptr)
31{
32 URLContext *h = (URLContext *)ptr;
33 if(h->prot) return h->prot->name;
34 else return "NULL";
35}
36static const AVOption options[] = {{NULL}};
37static const AVClass urlcontext_class =
38 { "URLContext", urlcontext_to_name, options };
39/*@}*/
40#endif
de6d9b64 41
019ac05a
FB
42static int default_interrupt_cb(void);
43
de6d9b64 44URLProtocol *first_protocol = NULL;
019ac05a 45URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
de6d9b64 46
84be6e72
MN
47URLProtocol *av_protocol_next(URLProtocol *p)
48{
49 if(p) return p->next;
50 else return first_protocol;
51}
52
de6d9b64
FB
53int register_protocol(URLProtocol *protocol)
54{
55 URLProtocol **p;
56 p = &first_protocol;
57 while (*p != NULL) p = &(*p)->next;
58 *p = protocol;
59 protocol->next = NULL;
60 return 0;
61}
62
ba99cfc2
RB
63int url_open_protocol (URLContext **puc, struct URLProtocol *up,
64 const char *filename, int flags)
de6d9b64
FB
65{
66 URLContext *uc;
de6d9b64
FB
67 int err;
68
62181004 69 uc = av_malloc(sizeof(URLContext) + strlen(filename) + 1);
8a9488b5 70 if (!uc) {
8fa36ae0 71 err = AVERROR(ENOMEM);
8a9488b5
FB
72 goto fail;
73 }
5acef35f
BA
74#if LIBAVFORMAT_VERSION_MAJOR >= 53
75 uc->av_class = &urlcontext_class;
76#endif
62181004 77 uc->filename = (char *) &uc[1];
f746a046 78 strcpy(uc->filename, filename);
de6d9b64
FB
79 uc->prot = up;
80 uc->flags = flags;
81 uc->is_streamed = 0; /* default = not streamed */
8a9488b5 82 uc->max_packet_size = 0; /* default: stream file */
de6d9b64
FB
83 err = up->url_open(uc, filename, flags);
84 if (err < 0) {
1ea4f593 85 av_free(uc);
de6d9b64
FB
86 *puc = NULL;
87 return err;
88 }
abbae514
MN
89
90 //We must be carefull here as url_seek() could be slow, for example for http
91 if( (flags & (URL_WRONLY | URL_RDWR))
ba99cfc2 92 || !strcmp(up->name, "file"))
abbae514
MN
93 if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
94 uc->is_streamed= 1;
de6d9b64
FB
95 *puc = uc;
96 return 0;
8a9488b5
FB
97 fail:
98 *puc = NULL;
99 return err;
de6d9b64
FB
100}
101
ba99cfc2
RB
102int url_open(URLContext **puc, const char *filename, int flags)
103{
104 URLProtocol *up;
105 const char *p;
106 char proto_str[128], *q;
107
108 p = filename;
109 q = proto_str;
110 while (*p != '\0' && *p != ':') {
111 /* protocols can only contain alphabetic chars */
112 if (!isalpha(*p))
113 goto file_proto;
114 if ((q - proto_str) < sizeof(proto_str) - 1)
115 *q++ = *p;
116 p++;
117 }
118 /* if the protocol has length 1, we consider it is a dos drive */
bc574408 119 if (*p == '\0' || is_dos_path(filename)) {
ba99cfc2
RB
120 file_proto:
121 strcpy(proto_str, "file");
122 } else {
123 *q = '\0';
124 }
125
126 up = first_protocol;
127 while (up != NULL) {
128 if (!strcmp(proto_str, up->name))
129 return url_open_protocol (puc, up, filename, flags);
130 up = up->next;
131 }
132 *puc = NULL;
133 return AVERROR(ENOENT);
134}
135
de6d9b64
FB
136int url_read(URLContext *h, unsigned char *buf, int size)
137{
138 int ret;
139 if (h->flags & URL_WRONLY)
6f3e0b21 140 return AVERROR(EIO);
de6d9b64
FB
141 ret = h->prot->url_read(h, buf, size);
142 return ret;
143}
144
145int url_write(URLContext *h, unsigned char *buf, int size)
146{
147 int ret;
8a9488b5 148 if (!(h->flags & (URL_WRONLY | URL_RDWR)))
6f3e0b21 149 return AVERROR(EIO);
8a9488b5
FB
150 /* avoid sending too big packets */
151 if (h->max_packet_size && size > h->max_packet_size)
6f3e0b21 152 return AVERROR(EIO);
de6d9b64
FB
153 ret = h->prot->url_write(h, buf, size);
154 return ret;
155}
156
bc5c918e 157int64_t url_seek(URLContext *h, int64_t pos, int whence)
de6d9b64 158{
bc5c918e 159 int64_t ret;
de6d9b64
FB
160
161 if (!h->prot->url_seek)
8fa36ae0 162 return AVERROR(EPIPE);
de6d9b64
FB
163 ret = h->prot->url_seek(h, pos, whence);
164 return ret;
165}
166
de6d9b64
FB
167int url_close(URLContext *h)
168{
7a813b36 169 int ret = 0;
346db900 170 if (!h) return 0; /* can happen when url_open fails */
de6d9b64 171
7a813b36
AB
172 if (h->prot->url_close)
173 ret = h->prot->url_close(h);
1ea4f593 174 av_free(h);
de6d9b64
FB
175 return ret;
176}
177
178int url_exist(const char *filename)
179{
180 URLContext *h;
181 if (url_open(&h, filename, URL_RDONLY) < 0)
182 return 0;
183 url_close(h);
184 return 1;
185}
186
bc5c918e 187int64_t url_filesize(URLContext *h)
de6d9b64 188{
bc5c918e 189 int64_t pos, size;
115329f1 190
8e287af0
MN
191 size= url_seek(h, 0, AVSEEK_SIZE);
192 if(size<0){
1ae2c5f2 193 pos = url_seek(h, 0, SEEK_CUR);
eabbae73
RB
194 if ((size = url_seek(h, -1, SEEK_END)) < 0)
195 return size;
196 size++;
1ae2c5f2 197 url_seek(h, pos, SEEK_SET);
8e287af0 198 }
de6d9b64
FB
199 return size;
200}
8a9488b5 201
8a9488b5
FB
202int url_get_max_packet_size(URLContext *h)
203{
204 return h->max_packet_size;
205}
f746a046
FB
206
207void url_get_filename(URLContext *h, char *buf, int buf_size)
208{
75e61b0e 209 av_strlcpy(buf, h->filename, buf_size);
f746a046 210}
019ac05a
FB
211
212
213static int default_interrupt_cb(void)
214{
215 return 0;
216}
217
019ac05a
FB
218void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
219{
220 if (!interrupt_cb)
221 interrupt_cb = default_interrupt_cb;
222 url_interrupt_cb = interrupt_cb;
223}
536333a0 224
502bdf68 225int av_url_read_pause(URLContext *h, int pause)
536333a0
BA
226{
227 if (!h->prot->url_read_pause)
228 return AVERROR(ENOSYS);
502bdf68 229 return h->prot->url_read_pause(h, pause);
536333a0
BA
230}
231
bc5c918e 232int64_t av_url_read_seek(URLContext *h,
536333a0
BA
233 int stream_index, int64_t timestamp, int flags)
234{
235 if (!h->prot->url_read_seek)
236 return AVERROR(ENOSYS);
237 return h->prot->url_read_seek(h, stream_index, timestamp, flags);
238}