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
|
/* 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');
}
}
@try {
ret = [OFString stringWithCString: retCString
length: i];
} @finally {
free(retCString);
}
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 == '+')
|
|
|
|
|
|
|
|
|
|
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
|
/* Reference for static linking */
int _OFString_URLEncoding_reference;
@implementation OFString (URLEncoding)
- (OFString*)stringByURLEncoding
{
const char *string = [self UTF8String];
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 UTF8StringLength] * 3) + 1)) == NULL)
@throw [OFOutOfMemoryException
newWithClass: isa
requestedSize: ([self UTF8StringLength] * 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');
}
}
@try {
ret = [OFString stringWithUTF8String: retCString
length: i];
} @finally {
free(retCString);
}
return ret;
}
- (OFString*)stringByURLDecoding
{
OFString *ret;
const char *string = [self UTF8String];
char *retCString;
char byte = 0;
int state = 0;
size_t i;
if ((retCString = malloc([self UTF8StringLength] + 1)) == NULL)
@throw [OFOutOfMemoryException
newWithClass: isa
requestedSize: [self UTF8StringLength] + 1];
for (i = 0; *string; string++) {
switch (state) {
case 0:
if (*string == '%')
state = 1;
else if (*string == '+')
|
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
if (state != 0) {
free(retCString);
@throw [OFInvalidEncodingException newWithClass: isa];
}
@try {
ret = [OFString stringWithCString: retCString];
} @finally {
free(retCString);
}
return ret;
}
@end
|
|
>
|
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
if (state != 0) {
free(retCString);
@throw [OFInvalidEncodingException newWithClass: isa];
}
@try {
ret = [OFString stringWithUTF8String: retCString
length: i];
} @finally {
free(retCString);
}
return ret;
}
@end
|