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/mem.h"
32 #include "libavutil/avstring.h"
33 #include "urldecode.h"
34 
35 char *ff_urldecode(const char *url, int decode_plus_sign)
36 {
37  int s = 0, d = 0, url_len = 0;
38  char c;
39  char *dest = NULL;
40 
41  if (!url)
42  return NULL;
43 
44  url_len = strlen(url) + 1;
45  dest = av_malloc(url_len);
46 
47  if (!dest)
48  return NULL;
49 
50  while (s < url_len) {
51  c = url[s++];
52 
53  if (c == '%' && s + 2 < url_len) {
54  char c2 = url[s++];
55  char c3 = url[s++];
56  if (av_isxdigit(c2) && av_isxdigit(c3)) {
57  c2 = av_tolower(c2);
58  c3 = av_tolower(c3);
59 
60  if (c2 <= '9')
61  c2 = c2 - '0';
62  else
63  c2 = c2 - 'a' + 10;
64 
65  if (c3 <= '9')
66  c3 = c3 - '0';
67  else
68  c3 = c3 - 'a' + 10;
69 
70  dest[d++] = 16 * c2 + c3;
71 
72  } else { /* %zz or something other invalid */
73  dest[d++] = c;
74  dest[d++] = c2;
75  dest[d++] = c3;
76  }
77  } else if (c == '+' && decode_plus_sign) {
78  dest[d++] = ' ';
79  } else {
80  dest[d++] = c;
81  }
82 
83  }
84 
85  return dest;
86 }
av_isxdigit
static av_const int av_isxdigit(int c)
Locale-independent conversion of ASCII isxdigit.
Definition: avstring.h:256
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:35
s
#define s(width, name)
Definition: cbs_vp9.c:257
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
c2
static const uint64_t c2
Definition: murmur3.c:52
urldecode.h
mem.h
d
d
Definition: ffmpeg_filter.c:153
avstring.h
av_tolower
static av_const int av_tolower(int c)
Locale-independent conversion of ASCII characters to lowercase.
Definition: avstring.h:246