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
|
/* Reference for static linking */
int _OFString_URLEncoding_reference;
@implementation OFString (URLEncoding)
- (OFString*)stringByURLEncoding
{
const char *string_ = string;
char *retCString;
size_t i;
OFString *ret;
/*
* Worst case: 3 times longer than before.
* Oh, and we can't use [self allocWithSize:] here as self might be a
* @"" literal.
*/
if ((retCString = malloc((length * 3) + 1)) == NULL)
@throw [OFOutOfMemoryException newWithClass: isa
requestedSize: (length * 3) + 1];
for (i = 0; *string_ != '\0'; string_++) {
if (isalnum((int)*string_) || *string_ == '-' ||
*string_ == '_' || *string_ == '.' || *string_ == '~')
retCString[i++] = *string_;
else {
uint8_t high, low;
high = *string_ >> 4;
low = *string_ & 0x0F;
retCString[i++] = '%';
retCString[i++] =
(high > 9 ? high - 10 + 'A' : high + '0');
retCString[i++] =
(low > 9 ? low - 10 + 'A' : low + '0');
}
|
|
|
|
>
|
|
|
|
|
|
|
|
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
|
/* Reference for static linking */
int _OFString_URLEncoding_reference;
@implementation OFString (URLEncoding)
- (OFString*)stringByURLEncoding
{
const char *string = [self cString];
char *retCString;
size_t i;
OFString *ret;
/*
* Worst case: 3 times longer than before.
* Oh, and we can't use [self allocWithSize:] here as self might be a
* @"" literal.
*/
if ((retCString = malloc(([self cStringLength] * 3) + 1)) == NULL)
@throw [OFOutOfMemoryException
newWithClass: isa
requestedSize: ([self cStringLength] * 3) + 1];
for (i = 0; *string != '\0'; string++) {
if (isalnum((int)*string) || *string == '-' || *string == '_' ||
*string == '.' || *string == '~')
retCString[i++] = *string;
else {
uint8_t high, low;
high = *string >> 4;
low = *string & 0x0F;
retCString[i++] = '%';
retCString[i++] =
(high > 9 ? high - 10 + 'A' : high + '0');
retCString[i++] =
(low > 9 ? low - 10 + 'A' : low + '0');
}
|
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
|
return ret;
}
- (OFString*)stringByURLDecoding
{
OFString *ret;
const char *string_ = string;
char *retCString;
char byte = 0;
int state = 0;
size_t i;
if ((retCString = malloc(length + 1)) == NULL)
@throw [OFOutOfMemoryException newWithClass: isa
requestedSize: length + 1];
for (i = 0; *string_; string_++) {
switch (state) {
case 0:
if (*string_ == '%')
state = 1;
else if (*string_ == '+')
retCString[i++] = ' ';
else
retCString[i++] = *string_;
break;
case 1:
case 2:;
uint8_t shift = (state == 1 ? 4 : 0);
if (*string_ >= '0' && *string_ <= '9')
byte += (*string_ - '0') << shift;
else if (*string_ >= 'A' && *string_ <= 'F')
byte += (*string_ - 'A' + 10) << shift;
else if (*string_ >= 'a' && *string_ <= 'f')
byte += (*string_ - 'a' + 10) << shift;
else {
free(retCString);
@throw [OFInvalidEncodingException
newWithClass: isa];
}
if (++state == 3) {
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
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
|
return ret;
}
- (OFString*)stringByURLDecoding
{
OFString *ret;
const char *string = [self cString];
char *retCString;
char byte = 0;
int state = 0;
size_t i;
if ((retCString = malloc([self cStringLength] + 1)) == NULL)
@throw [OFOutOfMemoryException
newWithClass: isa
requestedSize: [self cStringLength] + 1];
for (i = 0; *string; string++) {
switch (state) {
case 0:
if (*string == '%')
state = 1;
else if (*string == '+')
retCString[i++] = ' ';
else
retCString[i++] = *string;
break;
case 1:
case 2:;
uint8_t shift = (state == 1 ? 4 : 0);
if (*string >= '0' && *string <= '9')
byte += (*string - '0') << shift;
else if (*string >= 'A' && *string <= 'F')
byte += (*string - 'A' + 10) << shift;
else if (*string >= 'a' && *string <= 'f')
byte += (*string - 'a' + 10) << shift;
else {
free(retCString);
@throw [OFInvalidEncodingException
newWithClass: isa];
}
if (++state == 3) {
|