-
Notifications
You must be signed in to change notification settings - Fork 37
/
CGIDECODE的完整源代码ideone_Zf96q5.cpp
91 lines (81 loc) · 1.78 KB
/
CGIDECODE的完整源代码ideone_Zf96q5.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
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
// CgiDecode.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
using namespace std;
/*Get the hex value for a given character*/
int getHexValue(char c)
{
int hValue=-1;
if (c>='0' && c<='9')
{
hValue = c -'0';
}
else if (c>='A' && c<='F')
{
hValue = c-'A'+10;
}
else if (c>='a' && c<='f')
{
hValue = c-'a'+10;
}
return hValue;
}
/** Translate a string from the CGI encoding to plain ascii text.
* '+' becomes space, %xx becomes byte with hex value xx,
* other alphanumeric characters map to themselves.
* Returns 0 for success, positive for erroneous input
* 1 = bad hexadecimal digit
*/
int decode(char *encoded, char *decoded)
{
char *eptr = encoded;
char *dptr = decoded;
int ok=0;
while (*eptr)
{
char c;
c = *eptr;
if (c == '+')
{ /* Case 1: '+' maps to blank */
*dptr = ' ';
}
else if (c == '%')
{ /* Case 2: '%xx' is hex for character xx */
int digit_high = getHexValue(*(++eptr));
int digit_low = getHexValue(*(++eptr));
if ( digit_high == -1 || digit_low==-1) {
/* *dptr='?'; */
ok=1; /* Bad return code */
} else {
*dptr = 16* digit_high + digit_low;
}
} else {/* Case 3: All other characters map to themselves */
*dptr = *eptr;
}
++dptr;
++eptr;
}
*dptr = '\0'; /* Null terminator for string */
return ok;
}
int main()
{
int ok;
char *input="http://w...content-available-to-author-only...s.org/index.pl?node=Snippets%20Section";/*This is a test case*/
char *output=(char*) malloc(strlen(input)+1);
ok = decode(input, output);
if (ok==0)
{
cout<<"Decode success! output:"<<output<<endl;
}
else if (ok>0)
{
cout<<"erroneous input"<<endl;
if (ok==1)
{
cout<<"bad hexadecimal digit"<<endl;
}
}
return 0;
}