FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/gopher.c
Date: 2026-08-26 22:09:26
Exec Total Coverage
Lines: 0 50 0.0%
Functions: 0 5 0.0%
Branches: 0 18 0.0%

Line Branch Exec Source
1 /*
2 * Gopher protocol
3 *
4 * Copyright (c) 2009 Toshimitsu Kimura
5 * Copyright (c) 2021 parazyd <parazyd@dyne.org>
6 *
7 * based on libavformat/http.c, Copyright (c) 2000, 2001 Fabrice Bellard
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "config.h"
27 #include "config_components.h"
28
29 #include "libavutil/avstring.h"
30 #include "avformat.h"
31 #include "internal.h"
32 #include "network.h"
33 #include "url.h"
34
35 typedef struct GopherContext {
36 URLContext *hd;
37 } GopherContext;
38
39 static int gopher_write(URLContext *h, const uint8_t *buf, int size)
40 {
41 GopherContext *s = h->priv_data;
42 return ffurl_write(s->hd, buf, size);
43 }
44
45 static int gopher_connect(URLContext *h, const char *path)
46 {
47 char buffer[1024];
48
49 if (!*path) return AVERROR(EINVAL);
50 switch (*++path) {
51 case ';':
52 case '<':
53 case '5':
54 case '9':
55 case 's':
56 path = strchr(path, '/');
57 if (!path) return AVERROR(EINVAL);
58 break;
59 default:
60 av_log(h, AV_LOG_WARNING,
61 "Gopher protocol type '%c' not supported yet!\n",
62 *path);
63 return AVERROR(EINVAL);
64 }
65
66 if (strpbrk(path, "\r\n")) {
67 av_log(h, AV_LOG_ERROR, "Gopher path contains CRLF characters\n");
68 return AVERROR(EINVAL);
69 }
70
71 /* send gopher sector */
72 snprintf(buffer, sizeof(buffer), "%s\r\n", path);
73
74 if (gopher_write(h, buffer, strlen(buffer)) < 0)
75 return AVERROR(EIO);
76
77 return 0;
78 }
79
80 static int gopher_close(URLContext *h)
81 {
82 GopherContext *s = h->priv_data;
83 ffurl_closep(&s->hd);
84 return 0;
85 }
86
87 static int gopher_open(URLContext *h, const char *uri, int flags)
88 {
89 GopherContext *s = h->priv_data;
90 char proto[10], hostname[1024], auth[1024], path[1024], buf[1024];
91 int port, err;
92 const char *lower_proto = "tcp";
93
94 h->is_streamed = 1;
95
96 /* needed in any case to build the host string */
97 av_url_split(proto, sizeof(proto), auth, sizeof(auth),
98 hostname, sizeof(hostname), &port, path, sizeof(path), uri);
99
100 if (port < 0)
101 port = 70;
102
103 if (!strcmp(proto, "gophers"))
104 lower_proto = "tls";
105
106 ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
107
108 s->hd = NULL;
109 err = ffurl_open_whitelist(&s->hd, buf, AVIO_FLAG_READ_WRITE,
110 &h->interrupt_callback, NULL, h->protocol_whitelist, h->protocol_blacklist, h);
111 if (err < 0)
112 goto fail;
113
114 if ((err = gopher_connect(h, path)) < 0)
115 goto fail;
116 return 0;
117 fail:
118 gopher_close(h);
119 return err;
120 }
121
122 static int gopher_read(URLContext *h, uint8_t *buf, int size)
123 {
124 GopherContext *s = h->priv_data;
125 int len = ffurl_read(s->hd, buf, size);
126 return len;
127 }
128
129 #if CONFIG_GOPHER_PROTOCOL
130 const URLProtocol ff_gopher_protocol = {
131 .name = "gopher",
132 .url_open = gopher_open,
133 .url_read = gopher_read,
134 .url_write = gopher_write,
135 .url_close = gopher_close,
136 .priv_data_size = sizeof(GopherContext),
137 .flags = URL_PROTOCOL_FLAG_NETWORK,
138 .default_whitelist = "gopher,tcp"
139 };
140 #endif /* CONFIG_GOPHER_PROTOCOL */
141
142 #if CONFIG_GOPHERS_PROTOCOL
143 const URLProtocol ff_gophers_protocol = {
144 .name = "gophers",
145 .url_open = gopher_open,
146 .url_read = gopher_read,
147 .url_write = gopher_write,
148 .url_close = gopher_close,
149 .priv_data_size = sizeof(GopherContext),
150 .flags = URL_PROTOCOL_FLAG_NETWORK,
151 .default_whitelist = "gopher,gophers,tcp,tls"
152 };
153 #endif /* CONFIG_GOPHERS_PROTOCOL */
154