-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathutils.cpp
49 lines (46 loc) · 1.28 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
*
* Copyright (C) Max <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "utils.h"
#include <ctype.h>
static char hex_chars[]="0123456789ABCDEF";
std::size_t url_encode(char *buf, const char *from, std::size_t len, std::size_t buf_size)
{
std::size_t res = 0;
if(from)
{
while (*from != 0 && len > 0 && buf_size > 0)
{
res++;
buf_size--;
if(isalnum(*from) || *from == '-' || *from == '_' || *from == '.' || *from == '~')
*buf++ = *from;
else {
*buf++ = '%';
*buf++ = hex_chars[(*from >> 4) & 0x0f];
*buf++ = hex_chars[*from & 0x0f];
res += 2;
buf_size -= 2;
}
from++;
len--;
}
}
*buf = 0;
return res;
}