[CLEO] URL Encode * test

  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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{$CLEO}
wait 0
while not SAMP.Available()
wait 400
end
alloc 0@ 256
format 0@ = "testпривет 0123456789~!()"
call @urlEncode 1 str 0@ to 1@
chat "%s -> %s" -1 0@ 1@
chat "Pointer: %p" -1 1@
free 0@
1@ = 0
ect
:urlEncode
// call @urlEncode 1 str 0@
0C3A: 1@ = string "" pointer
alloc 5@ 6 // buf
memset 5@ 0 6
strlen 2@ 0@
2@-- // for cycle
for 3@ = 0 to 2@ step 1
0C1D: 4@ = array 0@ element 3@ el_size 1
if 4@ <> 0x0
jf break
0C2C: 6@ = strrchr 1@ char 0x0
if gosub @url_encode_cond
then
0C15: strcat destination 1@ source 4@v
else
0C15: strcat destination 1@ source "%"
0C1C: 5@ = itoa 4@ radix 16
0C15: strcat destination 1@ source 5@v
end
end
free 5@
ret 1 1@
:url_encode_cond
if and
4@ >= 0x30 // 0
4@ <= 0x39 // 9
then ret_true
else
if and
4@ >= 0x41 // a
4@ <= 0x5A // z
then ret_true
else
if and
4@ >= 0x61 // A
4@ <= 0x7A // Z
then ret_true
else
if or
4@ == 0x21 // !
4@ == 0x28 // (
4@ == 0x29 // )
4@ == 0x5C // \
4@ == 0x7E // ~
then ret_true
else ret_false
end
end
end
end
return
/*
// Source: https://github.com/azuremous/ofxTwitcurl/blob/master/libs/libtwitcurl/.svn/text-base/urlencode.cpp.svn-base
#include "urlencode.h"
std::string char2hex( char dec )
{
char dig1 = (dec&0xF0)>>4;
char dig2 = (dec&0x0F);
if ( 0<= dig1 && dig1<= 9) dig1+=48; //0,48 in ascii
if (10<= dig1 && dig1<=15) dig1+=65-10; //A,65 in ascii
if ( 0<= dig2 && dig2<= 9) dig2+=48;
if (10<= dig2 && dig2<=15) dig2+=65-10;
std::string r;
r.append( &dig1, 1);
r.append( &dig2, 1);
return r;
}
std::string urlencode( const std::string &c )
{
std::string escaped;
int max = c.length();
for(int i=0; i<max; i++)
{
if ( (48 <= c[i] && c[i] <= 57) ||//0-9
(65 <= c[i] && c[i] <= 90) ||//ABC...XYZ
(97 <= c[i] && c[i] <= 122) || //abc...xyz
(c[i]=='~' || c[i]=='-' || c[i]=='_' || c[i]=='.')
)
{
escaped.append( &c[i], 1);
}
else
{
escaped.append("%");
escaped.append( char2hex(c[i]) );//converts char 255 to string "FF"
}
}
return escaped;
}
*/