FFmpeg
Loading...
Searching...
No Matches
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
37static 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
80char *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
99int 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}
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
error code definitions
#define AVERROR(e)
Definition error.h:45
static av_const int av_tolower(int c)
Locale-independent conversion of ASCII characters to lowercase.
Definition avstring.h:237
static av_const int av_isxdigit(int c)
Locale-independent conversion of ASCII isxdigit.
Definition avstring.h:247
Utility Preprocessor macros.
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
static const uint64_t c2
Definition murmur3.c:53
#define av_malloc(s)
Definition ops_static.c:52
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
static size_t urldecode(char *dest, const char *url, size_t url_len, int decode_plus_sign)
Definition urldecode.c:37
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
static double c[64]