/
BEncoding.m
293 lines (228 loc) · 9.37 KB
/
BEncoding.m
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// BEncoding.m
//
// This file is part of the BEncoding framework.
//
// This framework 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 framework 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 framework. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright © Nathan Ollerenshaw 2008.
//
#import "BEncoding.h"
typedef struct {
NSUInteger length;
NSUInteger offset;
const char *bytes;
} BEncodingData;
// Private methods
//
// They're not REALLY private, but there is no point exposing them
// in the header.
@interface BEncoding (Private)
+(NSNumber *)numberFromEncodedData:(BEncodingData *)data;
+(NSData *)dataFromEncodedData:(BEncodingData *)data;
+(NSString *)stringFromEncodedData:(BEncodingData *)data;
+(NSArray *)arrayFromEncodedData:(BEncodingData *)data;
+(NSDictionary *)dictionaryFromEncodedData:(BEncodingData *)data;
+(id)objectFromData:(BEncodingData *)data;
@end
@implementation BEncoding
// +(NSData *)encodeDataFromObject:(id)object
//
// This method to returns an NSData object that contains the bencoded
// representation of the object that you send. You can send complex structures
// such as an NSDictionary that contains NSArrays, NSNumbers and NSStrings, and
// the encoder will correctly serialise the data in the expected way.
//
// Supports NSData, NSString, NSNumber, NSArray and NSDictionary objects.
//
// NSStrings are encoded as NSData objects as there is no way to differentiate
// between the two when decoding.
//
// NSNumbers are encoded and decoded with their longLongValue.
//
// NSDictionary keys must be NSStrings.
+(NSData *)encodedDataFromObject:(id)object
{
NSMutableData *data = [[NSMutableData alloc] init];
char buffer[32]; // Small buffer to hold length strings. Needs to hold a 64bit number.
memset(buffer, 0, sizeof(buffer)); // Ensure the buffer is zeroed
if ([object isKindOfClass:[NSData class]])
{
// Encode a chunk of bytes from an NSData.
snprintf(buffer, 32, "%lu:", [object length]);
[data appendBytes:buffer length:strlen(buffer)];
[data appendData:object];
[object autorelease];
return [data autorelease];
}
if ([object isKindOfClass:[NSString class]])
{
// Encode an NSString
NSData *stringData = [object dataUsingEncoding:NSUTF8StringEncoding];
snprintf(buffer, 32, "%lu:", [stringData length]);
[data appendBytes:buffer length:strlen(buffer)];
[data appendData:stringData];
return [data autorelease];
}
else if ([object isKindOfClass:[NSNumber class]])
{
// Encode an NSNumber
snprintf(buffer, 32, "i%llue", [object longLongValue]);
[data appendBytes:buffer length:strlen(buffer)];
return [data autorelease];
}
else if ([object isKindOfClass:[NSArray class]])
{
// Encode an NSArray
[data appendBytes:"l" length:1];
for (id item in object) {
[data appendData:[BEncoding encodedDataFromObject:item]];
}
[data appendBytes:"e" length:1];
return [data autorelease];
}
else if ([object isKindOfClass:[NSDictionary class]])
{
// Encode an NSDictionary
[data appendBytes:"d" length:1];
for (id key in object) {
// Assert that the key is a string. It is expected that you'll check before
// passing this class an array of objects as keys that are not NSStrings.
assert([key isKindOfClass:[NSString class]]);
NSData *stringData = [key dataUsingEncoding:NSUTF8StringEncoding];
snprintf(buffer, 32, "%lu:", [stringData length]);
[data appendBytes:buffer length:strlen(buffer)];
[data appendData:stringData];
[data appendData:[BEncoding encodedDataFromObject:[object objectForKey:key]]];
}
[data appendBytes:"e" length:1];
return [data autorelease];
}
[data autorelease];
return nil;
}
+(NSNumber *)numberFromEncodedData:(BEncodingData *)data
{
NSMutableString *numberString = [[NSMutableString alloc] init];
long long int number;
assert(data->bytes[data->offset] == 'i');
data->offset++; // We start on the i so we need to move by one.
while (data->offset < data->length && data->bytes[data->offset] != 'e') {
[numberString appendFormat:@"%c", data->bytes[data->offset++]];
}
if (![[NSScanner scannerWithString:numberString] scanLongLong:&number]){
[numberString autorelease];
return nil;
}
data->offset++; // Always move the offset off the end of the encoded item.
[numberString autorelease];
return [NSNumber numberWithLongLong:number];
}
+(NSData *)dataFromEncodedData:(BEncodingData *)data
{
NSMutableString *dataLength = [[NSMutableString alloc] init];
NSMutableData *decodedData = [[NSMutableData alloc] init];
if (data->bytes[data->offset] < '0' | data->bytes[data->offset] > '9'){
[decodedData autorelease];
[dataLength autorelease];
return nil; // Needed because we must fail to create a dictionary if it isn't a string.
}
// strings are special; they start with a number so we don't move by one.
while (data->offset < data->length && data->bytes[data->offset] != ':') {
[dataLength appendFormat:@"%c", data->bytes[data->offset++]];
}
if (data->bytes[data->offset] != ':'){
[decodedData autorelease];
[dataLength autorelease];
return nil; // We must have overrun the end of the bencoded string.
}
data->offset++;
[decodedData appendBytes:data->bytes+data->offset length:[dataLength integerValue]];
data->offset += [dataLength integerValue]; // Always move the offset off the end of the encoded item.
[decodedData autorelease];
[dataLength autorelease];
return decodedData;
}
+(NSString *)stringFromEncodedData:(BEncodingData *)data
{
/* A string is just bencoded data */
NSData *decodedData = [self dataFromEncodedData:data];
if (decodedData == nil)
return nil;
return [[[NSString alloc] initWithBytes:[decodedData bytes] length:[decodedData length] encoding:NSUTF8StringEncoding] autorelease];
}
+(NSArray *)arrayFromEncodedData:(BEncodingData *)data
{
NSMutableArray *array = [[NSMutableArray alloc] init];
assert(data->bytes[data->offset] == 'l');
data->offset++; // Move off the l so we point to the first encoded item.
while (data->bytes[data->offset] != 'e') {
[array addObject:[BEncoding objectFromData:data]];
}
data->offset++; // Always move off the end of the encoded item.
return [array autorelease];
}
+(NSDictionary *)dictionaryFromEncodedData:(BEncodingData *)data
{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
NSString *key = nil;
id value = nil;
assert(data->bytes[data->offset] == 'd');
data->offset++; // Move off the d so we point to the string key.
while (data->bytes[data->offset] != 'e') {
if (data->bytes[data->offset] >= '0' && data->bytes[data->offset] <= '9') {
// Dictionaries are a bencoded string with a bencoded value.
key = [BEncoding stringFromEncodedData:data];
value = [BEncoding objectFromData:data];
if (key != nil && value != nil)
[dictionary setValue:value forKey:key];
}
}
data->offset++; // Move off the e so we point to the next encoded item.
return [dictionary autorelease];
}
+(id)objectFromData:(BEncodingData *)data
{
/* Each of the decoders expect that the offset points to the first character
* of the encoded entity, for example the i in the bencoded integer "i18e" */
switch (data->bytes[data->offset]) {
case 'l':
return [BEncoding arrayFromEncodedData:data];
break;
case 'd':
return [BEncoding dictionaryFromEncodedData:data];
break;
case 'i':
return [BEncoding numberFromEncodedData:data];
break;
default:
if (data->bytes[data->offset] >= '0' && data->bytes[data->offset] <= '9')
return [BEncoding dataFromEncodedData:data];
break;
}
// If we reach here, it doesn't appear that this is bencoded data. So, we'll
// just return nil and advance to the next byte in the hopes we'll decode
// something useful. Not sure if this is a good strategy.
data->offset++;
return nil;
}
+(id)objectFromEncodedData:(NSData *)sourceData
{
BEncodingData data;
data.bytes = [sourceData bytes];
data.length = [sourceData length];
data.offset = 0;
return [BEncoding objectFromData:&data];
}
@end