Guest User

PHP NBT Class

a guest
Oct 16th, 2010
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1. <?php
  2. class nbt {
  3.     public $root;
  4.    
  5.     public $verbose = false;
  6.    
  7.     const TAG_END = 0;
  8.     const TAG_BYTE = 1;
  9.     const TAG_SHORT = 2;
  10.     const TAG_INT = 3;
  11.     const TAG_LONG = 4;
  12.     const TAG_FLOAT = 5;
  13.     const TAG_DOUBLE = 6;
  14.     const TAG_BYTE_ARRAY = 7;
  15.     const TAG_STRING = 8;
  16.     const TAG_LIST = 9;
  17.     const TAG_COMPOUND = 10;
  18.    
  19.     public function loadFile($filename) {
  20.         $fp = fopen("compress.zlib://{$filename}", "r");
  21.         $this->root = null;
  22.         $this->traverseTag($fp, &$this->root);
  23.     }
  24.    
  25.     private function traverseTag($fp, $tree) {
  26.         if(feof($fp)) return false;
  27.         $tagType = $this->readType($fp, self::TAG_BYTE); // Read type byte.
  28.         if($tagType == self::TAG_END) {
  29.             return false;
  30.         } else {
  31.             if($this->verbose) $position = ftell($fp);
  32.             $tagName = $this->readType($fp, self::TAG_STRING);
  33.             $tagData = $this->readType($fp, $tagType);
  34.             if($this->verbose) echo "{$position}; {$tagName}: ".PHP_EOL.var_export($tagData, true).PHP_EOL;
  35.             $tree[] = array("type"=>$tagType, "name"=>$tagName, "value"=>$tagData);
  36.             return true;
  37.         }
  38.     }
  39.    
  40.     private function readType($fp, $tagType) {
  41.         switch($tagType) {
  42.             case self::TAG_BYTE: // Signed byte (8 bit)
  43.                 $unpacked = unpack("c", fread($fp, 1));
  44.                 return $unpacked[1];
  45.             case self::TAG_SHORT: // Signed short (16 bit, big endian)
  46.                 $unpacked = unpack("n", $binary = fread($fp, 2));
  47.                 return $unpacked[1];
  48.             case self::TAG_INT: // Signed integer (32 bit, big endian)
  49.                 $unpacked = unpack("i", fread($fp, 4));
  50.                 return $unpacked[1];
  51.             case self::TAG_LONG: // Signed long (64 bit, big endian)
  52.                 $unpacked = unpack("l", fread($fp, 8));
  53.                 return $unpacked[1];
  54.             case self::TAG_FLOAT: // Floating point value (32 bit, big endian, IEEE 754-2008)
  55.                 $unpacked = unpack("f", fread($fp, 4));
  56.                 return $unpacked[1];
  57.             case self::TAG_DOUBLE: // Double value (64 bit, big endian, IEEE 754-2008)
  58.                 $unpacked = unpack("d", fread($fp, 8));
  59.                 return $unpacked[1];
  60.             case self::TAG_BYTE_ARRAY: // Byte array
  61.                 $arrayLength = fread($fp, $this->readType($fp, self::TAG_INT));
  62.                 $array = array();
  63.                 for($i = 0; $i < $arrayLength; $i++) $array[] = fread($fp, 1);
  64.                 return $array;
  65.             case self::TAG_STRING: // String
  66.                 if(!$stringLength = $this->readType($fp, self::TAG_SHORT)) return "";
  67.                 var_dump($stringLength);
  68.                 $string = utf8_decode(fread($fp, $stringLength)); // Read in number of bytes specified by string length, and decode from utf8.
  69.                 return $string;
  70.             case self::TAG_LIST: // List
  71.                 $tagID = $this->readType($fp, self::TAG_BYTE);
  72.                 $listLength = $this->readType($fp, self::TAG_INT);
  73.                 $list = array();
  74.                 for($i = 0; $i < $listLength; $i++) {
  75.                     if(feof($fp)) break;
  76.                     $array[] = $this->readType($fp, $tagID);
  77.                 }
  78.                 return $list;
  79.             case self::TAG_COMPOUND: // Compound
  80.                 $tree = array();
  81.                 while($this->traverseTag($fp, &$tree));
  82.                 return $tree;
  83.         }
  84.     }
  85. }
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment