Source for file Encode.php
Documentation is available at Encode.php
// +----------------------------------------------------------------------+
// | Decode and Encode data in Bittorrent format |
// +----------------------------------------------------------------------+
// | Copyright (C) 2004-2005 Markus Tacker <m@tacker.org> |
// +----------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or |
// | modify it under the terms of the GNU Lesser General Public |
// | License as published by the Free Software Foundation; either |
// | version 2.1 of the License, or (at your option) any later version. |
// | This library 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 |
// | Lesser General Public License for more details. |
// | You should have received a copy of the GNU Lesser General Public |
// | License along with this library; if not, write to the |
// | Free Software Foundation, Inc. |
// | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
// +----------------------------------------------------------------------+
* Encode data in Bittorrent format
* Original Python implementation by Petru Paler <petru@paler.net>
* PHP translation by Gerard Krijgsman <webmaster@animesuki.com>
* Gerard's regular expressions removed by Carl Ritson <critson@perlfu.co.uk>
* BEncoding is a simple, easy to implement method of associating
* data types with information in a file. The values in a torrent
* There are 4 different data types that can be bEncoded:
* Integers, Strings, Lists and Dictionaries.
* [http://www.monduna.com/bt/faq.html]
* @package File_Bittorrent
* @author Markus Tacker <m@tacker.org>
* @version $Id: Encode.php 62 2006-09-04 08:53:26Z m $
* Include required classes
* Encode data in Bittorrent format
* Original Python implementation by Petru Paler <petru@paler.net>
* PHP translation by Gerard Krijgsman <webmaster@animesuki.com>
* Gerard's regular expressions removed by Carl Ritson <critson@perlfu.co.uk>
* BEncoding is a simple, easy to implement method of associating
* data types with information in a file. The values in a torrent
* There are 4 different data types that can be bEncoded:
* Integers, Strings, Lists and Dictionaries.
* [http://www.monduna.com/bt/faq.html]
* @package File_Bittorrent
* @author Markus Tacker <m@tacker.org>
* Encode a var in BEncode format
* @param mixed Variable to encode
PEAR ::raiseError ('File_Bittorrent_Encode::encode() - Unsupported type.', null , null , "Variable must be one of 'string', 'integer', 'double' or 'array'");
* Strings are prefixed with their length followed by a colon.
* For example, "Monduna" would bEncode to 7:Monduna and "BitTorrents"
* would bEncode to 11:BitTorrents.
return strlen($str) . ':' . $str;
* Integers are prefixed with an i and terminated by an e. For
* example, 123 would bEcode to i123e, -3272002 would bEncode to
* This code assumes arrays with purely integer indexes are lists,
* arrays which use string indexes assumed to be dictionaries.
* Dictionaries are prefixed with a d and terminated by an e. They
* are similar to list, except that items are in key value pairs. The
* dictionary {"key":"value", "Monduna":"com", "bit":"Torrents", "number":7}
* would bEncode to d3:key5:value7:Monduna3:com3:bit:8:Torrents6:numberi7ee
* Lists are prefixed with a l and terminated by an e. The list
* should contain a series of bEncoded elements. For example, the
* list of strings ["Monduna", "Bit", "Torrents"] would bEncode to
* l7:Monduna3:Bit8:Torrentse. The list [1, "Monduna", 3, ["Sub", "List"]]
* would bEncode to li1e7:Mondunai3el3:Sub4:Listee
// Check for strings in the keys
ksort($array, SORT_NUMERIC );
foreach ($array as $val) {
$return .= $this->encode($val);
ksort($array, SORT_STRING );
foreach ($array as $key => $val) {
$return .= $this->encode($val);
Documentation generated on Tue, 13 Mar 2007 10:00:11 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.
|