PHP Binary Class

    PHP Binary Class

    Hallööle..
    Ich hab mir mal just4fun ne Binary Class in PHP gemacht
    Hier der code:

    PHP-Quellcode

    1. <?php
    2. class BinaryReader
    3. {
    4. protected $arrBytes = array();
    5. public function getBytes()
    6. {
    7. return $this->arrBytes;
    8. }
    9. protected $intIndex = 0;
    10. public function getIndex()
    11. {
    12. return $this->intIndex;
    13. }
    14. public function seekIndex($intSeek, $boolBackward = false)
    15. {
    16. $intMixReturn = false;
    17. if(is_numeric($intSeek) && $intSeek >= 0)
    18. {
    19. if(!$boolBackward && ($this->intIndex + $intSeek) <= count($this->arrBytes))
    20. {
    21. $this->intIndex += $intSeek;
    22. $intMixReturn = $this->intIndex;
    23. }
    24. elseif($boolBackward && ($this->intIndex - $intSeek) >= 0)
    25. {
    26. $this->intIndex -= $intSeek;
    27. $intMixReturn = $this->intIndex;
    28. }
    29. }
    30. return $intMixReturn;
    31. }
    32. function __construct($mixInput)
    33. {
    34. $this->intIndex = 0;
    35. $this->arrBytes = array();
    36. if(is_array($mixInput))
    37. {
    38. $this->arrBytes = $mixInput;
    39. }
    40. elseif(is_string($mixInput))
    41. {
    42. for($intIndex = 0; $intIndex < strlen($mixInput); $intIndex ++)
    43. {
    44. $this->arrBytes[] = ord($mixInput[$intIndex]);
    45. }
    46. }
    47. else
    48. {
    49. return false;
    50. }
    51. }
    52. public function canRead($intUnit = 1)
    53. {
    54. if(($this->intIndex + $intUnit) <= count($this->arrBytes))
    55. {
    56. return true;
    57. }
    58. else
    59. {
    60. return false;
    61. }
    62. }
    63. public function readByte()
    64. {
    65. if($this->canRead(1))
    66. {
    67. $intTempByte = $this->arrBytes[$this->intIndex];
    68. $this->intIndex += 1;
    69. return $intTempByte;
    70. }
    71. else
    72. {
    73. return false;
    74. }
    75. }
    76. public function readShort()
    77. {
    78. if($this->canRead(2))
    79. {
    80. $intTempShort = BinaryHelper::GetShortFromBytes(array($this->arrBytes[$this->intIndex], $this->arrBytes[$this->intIndex + 1]));
    81. $this->intIndex += 2;
    82. return $intTempShort;
    83. }
    84. else
    85. {
    86. return false;
    87. }
    88. }
    89. public function readInteger()
    90. {
    91. if($this->canRead(4))
    92. {
    93. $intTempShort = BinaryHelper::GetIntegerFromBytes(array($this->arrBytes[$this->intIndex], $this->arrBytes[$this->intIndex + 1], $this->arrBytes[$this->intIndex + 2], $this->arrBytes[$this->intIndex + 3]));
    94. $this->intIndex += 4;
    95. return $intTempShort;
    96. }
    97. else
    98. {
    99. return false;
    100. }
    101. }
    102. public function readString()
    103. {
    104. $intLength = $this->readInteger();
    105. if($intLength !== false)
    106. {
    107. if($this->canRead($intLength))
    108. {
    109. $strTempString = "";
    110. for($intIndex = 0; $intIndex < $intLength; $intIndex ++)
    111. {
    112. $strTempString .= chr($this->readByte());
    113. }
    114. return $strTempString;
    115. }
    116. else
    117. {
    118. return false;
    119. }
    120. }
    121. else
    122. {
    123. return false;
    124. }
    125. }
    126. }
    127. class BinaryWriter
    128. {
    129. protected $arrBytes = array();
    130. public function getBytes()
    131. {
    132. return $this->arrBytes;
    133. }
    134. public function getBytesString()
    135. {
    136. $strFinalString = "";
    137. for($intIndex = 0; $intIndex < count($this->arrBytes); $intIndex ++)
    138. {
    139. $strFinalString .= chr($this->arrBytes[$intIndex]);
    140. }
    141. return $strFinalString;
    142. }
    143. public function getBytesHex()
    144. {
    145. $strFinalString = "";
    146. for($intIndex = 0; $intIndex < count($this->arrBytes); $intIndex ++)
    147. {
    148. $strTempString = strtoupper(dechex($this->arrBytes[$intIndex]));
    149. $strTempString = substr("00", 0, 2 - strlen($strTempString)).$strTempString;
    150. $strFinalString .= $strTempString." ";
    151. }
    152. return $strFinalString;
    153. }
    154. function __construct()
    155. {
    156. $this->arrBytes = array();
    157. }
    158. public function writeByte($intByte)
    159. {
    160. $intBytes = BinaryHelper::GetByteFromByte($intByte);
    161. $this->arrBytes[] = $intBytes[0];
    162. }
    163. public function writeString($strWrite)
    164. {
    165. $intStringLengthBytes = BinaryHelper::GetBytesFromInteger(strlen($strWrite));
    166. $this->arrBytes[] = $intStringLengthBytes[0];
    167. $this->arrBytes[] = $intStringLengthBytes[1];
    168. $this->arrBytes[] = $intStringLengthBytes[2];
    169. $this->arrBytes[] = $intStringLengthBytes[3];
    170. for($intIndex = 0; $intIndex < strlen($strWrite); $intIndex ++)
    171. {
    172. $this->arrBytes[] = ord($strWrite[$intIndex]);
    173. }
    174. }
    175. public function writeInteger($intWrite)
    176. {
    177. $intIntegerBytes = BinaryHelper::GetBytesFromInteger($intWrite);
    178. foreach($intIntegerBytes as $intIntegerByte)
    179. {
    180. $this->arrBytes[] = $intIntegerByte;
    181. }
    182. }
    183. public function writeShort($intWrite)
    184. {
    185. $intShortBytes = BinaryHelper::GetBytesFromShort($intWrite);
    186. foreach($intShortBytes as $intShortByte)
    187. {
    188. $this->arrBytes[] = $intShortByte;
    189. }
    190. }
    191. /*public function writeLong($intWrite)
    192. {
    193. $intLongBytes = BinaryHelper::GetBytesFromLong($intWrite);
    194. foreach($intLongBytes as $intLongByte)
    195. {
    196. $this->arrBytes[] = $intLongByte;
    197. }
    198. }*/
    199. }
    200. class BinaryHelper
    201. {
    202. public static function GetBytesFromInteger($intInteger)
    203. {
    204. $BitSequence = decbin($intInteger);
    205. $BitSequence = substr("00000000000000000000000000000000", 0, 32 - strlen($BitSequence)).$BitSequence;
    206. $BitSequence = substr($BitSequence, 0, 32);
    207. $arrBytes = array();
    208. $arrBytes[] = bindec(substr($BitSequence, 0, 8));
    209. $arrBytes[] = bindec(substr($BitSequence, 8, 8));
    210. $arrBytes[] = bindec(substr($BitSequence, 16, 8));
    211. $arrBytes[] = bindec(substr($BitSequence, 24, 8));
    212. return $arrBytes;
    213. }
    214. public static function GetByteFromByte($intByte)
    215. {
    216. $BitSequence = decbin($intByte);
    217. $BitSequence = substr("00000000", 0, 8 - strlen($BitSequence)).$BitSequence;
    218. $BitSequence = substr($BitSequence, 0, 8);
    219. $arrBytes = array();
    220. $arrBytes[] = bindec(substr($BitSequence, 0, 8));
    221. return $arrBytes;
    222. }
    223. public static function GetBytesFromShort($intShort)
    224. {
    225. $BitSequence = decbin($intShort);
    226. $BitSequence = substr("0000000000000000", 0, 16 - strlen($BitSequence)).$BitSequence;
    227. $BitSequence = substr($BitSequence, 0, 16);
    228. $arrBytes = array();
    229. $arrBytes[] = bindec(substr($BitSequence, 0, 8));
    230. $arrBytes[] = bindec(substr($BitSequence, 8, 8));
    231. return $arrBytes;
    232. }
    233. public static function GetBytesFromLong($intLong)
    234. {
    235. $BitSequence = decbin($intLong);
    236. $BitSequence = substr("0000000000000000000000000000000000000000000000000000000000000000", 0, 64 - strlen($BitSequence)).$BitSequence;
    237. $BitSequence = substr($BitSequence, 0, 64);
    238. $arrBytes = array();
    239. $arrBytes[] = bindec(substr($BitSequence, 0, 8));
    240. $arrBytes[] = bindec(substr($BitSequence, 8, 8));
    241. $arrBytes[] = bindec(substr($BitSequence, 16, 8));
    242. $arrBytes[] = bindec(substr($BitSequence, 24, 8));
    243. $arrBytes[] = bindec(substr($BitSequence, 32, 8));
    244. $arrBytes[] = bindec(substr($BitSequence, 40, 8));
    245. $arrBytes[] = bindec(substr($BitSequence, 48, 8));
    246. $arrBytes[] = bindec(substr($BitSequence, 56, 8));
    247. return $arrBytes;
    248. }
    249. public static function GetLongFromBytes($arrBytes)
    250. {
    251. if(count($arrBytes) != 8) { return false; }
    252. $BitSequence = "";
    253. for($intIndex = 0; $intIndex < 8; $intIndex ++)
    254. {
    255. $BitSequenceTemp = decbin($arrBytes[$intIndex]);
    256. $BitSequence .= substr("00000000", 0, 8 - strlen($BitSequenceTemp)).$BitSequenceTemp;
    257. }
    258. return bindec($BitSequence);
    259. }
    260. public static function GetIntegerFromBytes($arrBytes)
    261. {
    262. if(count($arrBytes) != 4) { return false; }
    263. $BitSequence = "";
    264. for($intIndex = 0; $intIndex < 4; $intIndex ++)
    265. {
    266. $BitSequenceTemp = decbin($arrBytes[$intIndex]);
    267. $BitSequence .= substr("00000000", 0, 8 - strlen($BitSequenceTemp)).$BitSequenceTemp;
    268. }
    269. return bindec($BitSequence);
    270. }
    271. public static function GetShortFromBytes($arrBytes)
    272. {
    273. if(count($arrBytes) != 2) { return false; }
    274. $BitSequence = "";
    275. for($intIndex = 0; $intIndex < 2; $intIndex ++)
    276. {
    277. $BitSequenceTemp = decbin($arrBytes[$intIndex]);
    278. $BitSequence .= substr("00000000", 0, 8 - strlen($BitSequenceTemp)).$BitSequenceTemp;
    279. }
    280. return bindec($BitSequence);
    281. }
    282. }
    283. ?>


    Viel spaß!