-
Notifications
You must be signed in to change notification settings - Fork 4
/
php_bencode_doc.php
609 lines (541 loc) · 11.3 KB
/
php_bencode_doc.php
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
<?php
/**
* Created by PhpStorm.
* User: frederick
* Date: 9/22/15
* Time: 1:09 AM
*/
/**
* Class bitem
*
* The base class of all other types. You're not allowed to instantiate it.
* Actually, only parse(), load() and save() are useful at PHP level
*/
abstract class bitem
{
/**
* @param string $ben The string to parse
* @return bdict|blist|bstr|bint
*
* Generally, calling this function will handle the type adaption work for you
* But if you're really sure about the original data and want only a specific type,
* you can also call other functions named parse the same in other classes
*/
static function parse($ben)
{
}
/**
* @return string
*/
abstract function encode();
/**
* @param string $file The path of the file to be decoded
* @return bdict|blist|bstr|bint
*/
static function load($file)
{
}
/**
* @param string $file The path of the file to save the encoded content into
*/
function save($file)
{
}
}
/**
* Class bdict
*
* Dictionary of Bencode.
* The keys of a bdict is always bstr while values could be bdict, blist, bstr or bint.
* This is handled by std::map in C++
*/
class bdict extends bitem
{
/**
* @return string "bdict" is always returned
*/
function get_type()
{
}
/**
* @param string $key The key of the object to set
* @param bdict|blist|bstr|bint $value The object
* @return bool
*/
function set($key, $value)
{
}
/**
* @param string $key The key of the object to get
* @return bdict|blist|bstr|bint|bool $value The object
*
* This function will return a reference of the node
* If you need a copy instead, please use get_copy()
*/
function get($key)
{
}
/**
* @param string $key The key of the object to get
* @return bdict|blist|bstr|bint|bool $value The object
*/
function get_copy($key)
{
}
/**
* @param string $key The key of the object to check
* @return bool
*/
function has($key)
{
}
/**
* @param string $key The key of the object to delete
* @return bool Deleted, true; not found, false
*/
function del($key)
{
}
/**
* @param string $path The path of the object to set
* @param bdict|blist|bstr|bint $value The object
* @return bool
*
* the path is a series of slash split keys
* for example,
* set('key1/0', new bint(1))
* will do
* (bdict)[ "key1"(bstr) => (blist)[ 0 => 1(bint) ] ]
* and if you want to use a slash in key, use
* \/
* instead, this could be sometimes confusing, pay attention to it
*
* what's more, if any parent of the object in the given path does not exist,
* it will be automatically created
* the type of the created parent determines on the format of the key
* for example,
* (bdict)[ ]
* ⟱
* set_path(key1/0', new bint(1))
* ⟱
* (bdict)[ "key1"(bstr) => (blist)[ 0 => 1(bint) ] ]
*
* (bdict)[ ]
* ⟱
* set_path('key1/index0', new bint(1))
* ⟱
* (bdict)[ "key1"(bstr) => (bdict)[ "index0"(bstr) => 1(bint) ] ]
* so be careful if you want a bdict whose keys are integer-only strings
*/
function set_path($path, $value)
{
}
/**
* @param string $path The path of the object to get
* @return bdict|blist|bstr|bint|bool $value The object
*
* This function will return a reference of the node
* If you need a copy instead, please use get_path_copy()
*/
function get_path($path)
{
}
/**
* @param string $path The path of the object to get
* @return bdict|blist|bstr|bint|bool $value The object
*/
function get_path_copy($path)
{
}
/**
* @param string $path The key of the object to delete
* @return bool Deleted, true; not found, false
*/
function del_path($path)
{
}
/**
* @return int Return the length of encoded content
*/
function length()
{
}
/**
* @return int Return the number of elements
*/
function count()
{
}
/**
* @param string $ben
* @return bdict
* @see bitem::parse()
*/
static function parse($ben)
{
}
/**
* @return string
*/
function encode()
{
}
/**
* @param string|int $needle
* @param int $mode 0: search keys; 1: search values
* @return string[] paths of the matching entries
*/
function search($needle, $mode)
{
}
/**
* @return array Return the array without meta
*
* convert the Bencode to a simple PHP array
* notice that it may cause data loss,
* so DO NOT use this function if you're not pretty sure about the content
*
* @see bdict::to_meta_array()
*/
function to_array()
{
}
/**
* @return array Return the meta array
*
* in a meta array, each single element will be described as a array
* for example,
* bint(10) will be
* [ '_type' => 'bint', '_length' => 4, '_data' => 10 ]
* bdict will be
* [ '_type' => 'bdict', '_length' => ..., '_size' => ..., '_data' => [ 'key1' => ..., 'key2' => ..., ... ] ]
* this is very useful when the Bencode contains empty bdict or blist,
* with out the _type the only way to distinguish them is to call get_type()
*/
function to_meta_array()
{
}
/**
* @return string The encoded content
*
* this magic method will be called implicitly when being converted to string
*/
function __toString()
{
}
/**
* PHP constructor
*/
function __construct()
{
}
/**
* PHP destructor
*/
function __destruct()
{
}
}
/**
* Class blist
*
* List of Bencode
* This is handled by std::vector in C++
*/
class blist extends bitem
{
/**
* @return string "blist" is always returned
*/
function get_type()
{
}
/**
* @param string $key The key of the object to set
* @param bdict|blist|bstr|bint $value The object
*
* If the key of a blist is larger than the size of it,
* the value will be pushed to the end of the blist
*/
function set($key, $value)
{
}
/**
* @param string $key The key of the object to get
* @return bdict|blist|bstr|bint|null $value The object
*/
function get($key)
{
}
/**
* @param string $key The key of the object to get
* @return bdict|blist|bstr|bint|null $value The object
*/
function get_copy($key)
{
}
/**
* @param string $key The key of the object to check
* @return bool
*/
function has($key)
{
}
/**
* @param string $key The key of the object to delete
* @return bool Deleted, true; not found, false
*/
function del($key)
{
}
/**
* @param string $path The path of the object to set
* @param bdict|blist|bstr|bint $value The object
* @return bool
* @see bdict::set_path()
*/
function set_path($path, $value)
{
}
/**
* @param string $path The path of the object to get
* @return bdict|blist|bstr|bint|bool $value The object
*
* This function will return a reference of the node
* If you need a copy instead, please use get_path_copy()
*/
function get_path($path)
{
}
/**
* @param string $path The path of the object to get
* @return bdict|blist|bstr|bint|bool $value The object
*/
function get_path_copy($path)
{
}
/**
* @param string $path The key of the object to delete
* @return bool Deleted, true; not found, false
*/
function del_path($path)
{
}
/**
* @return int Return the length of encoded content
*/
function length()
{
}
/**
* @return int Return the number of elements
*/
function count()
{
}
/**
* @param string $ben
* @return blist
* @see bitem::parse()
*/
static function parse($ben)
{
}
/**
* @return string
*/
function encode()
{
}
/**
* @param string|int $needle
* @param int $mode
* @return string[]
* @see bdict::search()
*/
function search($needle, $mode)
{
}
/**
* @return array Return the array without meta
* @see bdict::to_array()
*/
function to_array()
{
}
/**
* @return array Return the meta array
* @see bdict::to_meta_array()
*/
function to_meta_array()
{
}
/**
* @return string The encoded content
*
* this magic method will be called implicitly when being converted to string
*/
function __toString()
{
}
/**
* PHP constructor
*/
function __construct()
{
}
/**
* PHP destructor
*/
function __destruct()
{
}
}
/**
* Class bstr
*
* String of Bencode.
*/
class bstr extends bitem
{
/**
* @return string "bstr" is always returned
*/
function get_type()
{
}
/**
* @return string
*/
function get()
{
}
/**
* @param string $value
*/
function set($value)
{
}
/**
* @return int
*/
function length()
{
}
/**
* @param string $ben
* @return bstr
* @see bitem::parse()
*/
static function parse($ben)
{
}
/**
* @return string
*/
function encode()
{
}
/**
* @return string This will simply return the contained value
*
* this is somehow useless to PHP developers
* it will be called mostly when recursively converting a bdict or blist to array
*/
function to_array()
{
}
/**
* @return array
* @see bdict::to_meta_array()
*/
function to_meta_array()
{
}
/**
* @return string
*/
function __toString()
{
}
/**
* @param string|null $value
*/
function __construct($value)
{
}
}
/**
* Class bint
*
* Integer of Bencode.
*/
class bint extends bitem
{
/**
* @return string "bint" is always returned
*/
function get_type()
{
}
/**
* @return int
*/
function get()
{
}
/**
* @param int $value
*/
function set($value)
{
}
/**
* @return int
*/
function length()
{
}
/**
* @param string $ben
* @return bint
* @see bitem::parse()
*/
static function parse($ben)
{
}
/**
* @return string
*/
function encode()
{
}
/**
* @return int This will simply return the contained value
*
* this is somehow useless to PHP developers
* it will be called mostly when recursively converting a bdict or blist to array
*/
function to_array()
{
}
/**
* @return array
* @see bdict::to_meta_array()
*/
function to_meta_array()
{
}
/**
* @return string
*/
function __toString()
{
}
/**
* @param int|null $value
*/
function __construct($value)
{
}
}