FFmpeg
urldecode.c
Go to the documentation of this file.
1 /*
2  * Simple URL decoding function
3  * Copyright (c) 2012 Antti Seppälä
4  *
5  * References:
6  * RFC 3986: Uniform Resource Identifier (URI): Generic Syntax
7  * T. Berners-Lee et al. The Internet Society, 2005
8  *
9  * based on http://www.icosaedro.it/apache/urldecode.c
10  * from Umberto Salsi (salsi@icosaedro.it)
11  *
12  * This file is part of FFmpeg.
13  *
14  * FFmpeg is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * FFmpeg is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with FFmpeg; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #include <string.h>
30 
31 #include "libavutil/error.h"
32 #include "libavutil/macros.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/avstring.h"
35 #include "urldecode.h"
36 
37 static size_t urldecode(char *dest, const char *url, size_t url_len, int decode_plus_sign)
38 {
39  size_t s = 0, d = 0;
40  char c;
41 
42  while (s < url_len) {
43  c = url[s++];
44 
45  if (c == '%' && s + 1 < url_len) {
46  char c2 = url[s++];
47  char c3 = url[s++];
48  if (av_isxdigit(c2) && av_isxdigit(c3)) {
49  c2 = av_tolower(c2);
50  c3 = av_tolower(c3);
51 
52  if (c2 <= '9')
53  c2 = c2 - '0';
54  else
55  c2 = c2 - 'a' + 10;
56 
57  if (c3 <= '9')
58  c3 = c3 - '0';
59  else
60  c3 = c3 - 'a' + 10;
61 
62  dest[d++] = 16 * c2 + c3;
63 
64  } else { /* %zz or something other invalid */
65  dest[d++] = c;
66  dest[d++] = c2;
67  dest[d++] = c3;
68  }
69  } else if (c == '+' && decode_plus_sign) {
70  dest[d++] = ' ';
71  } else {
72  dest[d++] = c;
73  }
74 
75  }
76 
77  return d;
78 }
79 
80 char *ff_urldecode(const char *url, int decode_plus_sign)
81 {
82  char *dest = NULL;
83  size_t url_len;
84 
85  if (!url)
86  return NULL;
87 
88  url_len = strlen(url) + 1;
89  dest = av_malloc(url_len);
90 
91  if (!dest)
92  return NULL;
93 
94  urldecode(dest, url, url_len, decode_plus_sign);
95 
96  return dest;
97 }
98 
99 int ff_urldecode_len(char *dest, size_t dest_len, const char *url, size_t url_max_len, int decode_plus_sign)
100 {
101  size_t written_bytes;
102  size_t url_len = strlen(url);
103 
104  url_len = FFMIN(url_len, url_max_len);
105 
106  if (dest_len <= url_len)
107  return AVERROR(EINVAL);
108 
109  written_bytes = urldecode(dest, url, url_len, decode_plus_sign);
110  dest[written_bytes] = '\0';
111 
112  return written_bytes;
113 }
av_isxdigit
static av_const int av_isxdigit(int c)
Locale-independent conversion of ASCII isxdigit.
Definition: avstring.h:247
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
ff_urldecode
char * ff_urldecode(const char *url, int decode_plus_sign)
Decodes an URL from its percent-encoded form back into normal representation.
Definition: urldecode.c:80
macros.h
urldecode
static size_t urldecode(char *dest, const char *url, size_t url_len, int decode_plus_sign)
Definition: urldecode.c:37
s
#define s(width, name)
Definition: cbs_vp9.c:198
NULL
#define NULL
Definition: coverity.c:32
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
error.h
ff_urldecode_len
int ff_urldecode_len(char *dest, size_t dest_len, const char *url, size_t url_max_len, int decode_plus_sign)
Decodes an URL from its percent-encoded form back into normal representation.
Definition: urldecode.c:99
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
c2
static const uint64_t c2
Definition: murmur3.c:53
urldecode.h
mem.h
avstring.h
av_tolower
static av_const int av_tolower(int c)
Locale-independent conversion of ASCII characters to lowercase.
Definition: avstring.h:237