Edit File by line
/home/barbar84/public_h.../wp-inclu.../ID3
File: getid3.lib.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/////////////////////////////////////////////////////////////////
[2] Fix | Delete
/// getID3() by James Heinrich <info@getid3.org> //
[3] Fix | Delete
// available at https://github.com/JamesHeinrich/getID3 //
[4] Fix | Delete
// or https://www.getid3.org //
[5] Fix | Delete
// or http://getid3.sourceforge.net //
[6] Fix | Delete
// //
[7] Fix | Delete
// getid3.lib.php - part of getID3() //
[8] Fix | Delete
// see readme.txt for more details //
[9] Fix | Delete
// ///
[10] Fix | Delete
/////////////////////////////////////////////////////////////////
[11] Fix | Delete
[12] Fix | Delete
[13] Fix | Delete
class getid3_lib
[14] Fix | Delete
{
[15] Fix | Delete
/**
[16] Fix | Delete
* @param string $string
[17] Fix | Delete
* @param bool $hex
[18] Fix | Delete
* @param bool $spaces
[19] Fix | Delete
* @param string|bool $htmlencoding
[20] Fix | Delete
*
[21] Fix | Delete
* @return string
[22] Fix | Delete
*/
[23] Fix | Delete
public static function PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8') {
[24] Fix | Delete
$returnstring = '';
[25] Fix | Delete
for ($i = 0; $i < strlen($string); $i++) {
[26] Fix | Delete
if ($hex) {
[27] Fix | Delete
$returnstring .= str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT);
[28] Fix | Delete
} else {
[29] Fix | Delete
$returnstring .= ' '.(preg_match("#[\x20-\x7E]#", $string[$i]) ? $string[$i] : 'ยค');
[30] Fix | Delete
}
[31] Fix | Delete
if ($spaces) {
[32] Fix | Delete
$returnstring .= ' ';
[33] Fix | Delete
}
[34] Fix | Delete
}
[35] Fix | Delete
if (!empty($htmlencoding)) {
[36] Fix | Delete
if ($htmlencoding === true) {
[37] Fix | Delete
$htmlencoding = 'UTF-8'; // prior to getID3 v1.9.0 the function's 4th parameter was boolean
[38] Fix | Delete
}
[39] Fix | Delete
$returnstring = htmlentities($returnstring, ENT_QUOTES, $htmlencoding);
[40] Fix | Delete
}
[41] Fix | Delete
return $returnstring;
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Truncates a floating-point number at the decimal point.
[46] Fix | Delete
*
[47] Fix | Delete
* @param float $floatnumber
[48] Fix | Delete
*
[49] Fix | Delete
* @return float|int returns int (if possible, otherwise float)
[50] Fix | Delete
*/
[51] Fix | Delete
public static function trunc($floatnumber) {
[52] Fix | Delete
if ($floatnumber >= 1) {
[53] Fix | Delete
$truncatednumber = floor($floatnumber);
[54] Fix | Delete
} elseif ($floatnumber <= -1) {
[55] Fix | Delete
$truncatednumber = ceil($floatnumber);
[56] Fix | Delete
} else {
[57] Fix | Delete
$truncatednumber = 0;
[58] Fix | Delete
}
[59] Fix | Delete
if (self::intValueSupported($truncatednumber)) {
[60] Fix | Delete
$truncatednumber = (int) $truncatednumber;
[61] Fix | Delete
}
[62] Fix | Delete
return $truncatednumber;
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
/**
[66] Fix | Delete
* @param int|null $variable
[67] Fix | Delete
* @param int $increment
[68] Fix | Delete
*
[69] Fix | Delete
* @return bool
[70] Fix | Delete
*/
[71] Fix | Delete
public static function safe_inc(&$variable, $increment=1) {
[72] Fix | Delete
if (isset($variable)) {
[73] Fix | Delete
$variable += $increment;
[74] Fix | Delete
} else {
[75] Fix | Delete
$variable = $increment;
[76] Fix | Delete
}
[77] Fix | Delete
return true;
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/**
[81] Fix | Delete
* @param int|float $floatnum
[82] Fix | Delete
*
[83] Fix | Delete
* @return int|float
[84] Fix | Delete
*/
[85] Fix | Delete
public static function CastAsInt($floatnum) {
[86] Fix | Delete
// convert to float if not already
[87] Fix | Delete
$floatnum = (float) $floatnum;
[88] Fix | Delete
[89] Fix | Delete
// convert a float to type int, only if possible
[90] Fix | Delete
if (self::trunc($floatnum) == $floatnum) {
[91] Fix | Delete
// it's not floating point
[92] Fix | Delete
if (self::intValueSupported($floatnum)) {
[93] Fix | Delete
// it's within int range
[94] Fix | Delete
$floatnum = (int) $floatnum;
[95] Fix | Delete
}
[96] Fix | Delete
}
[97] Fix | Delete
return $floatnum;
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
/**
[101] Fix | Delete
* @param int $num
[102] Fix | Delete
*
[103] Fix | Delete
* @return bool
[104] Fix | Delete
*/
[105] Fix | Delete
public static function intValueSupported($num) {
[106] Fix | Delete
// check if integers are 64-bit
[107] Fix | Delete
static $hasINT64 = null;
[108] Fix | Delete
if ($hasINT64 === null) { // 10x faster than is_null()
[109] Fix | Delete
$hasINT64 = is_int(pow(2, 31)); // 32-bit int are limited to (2^31)-1
[110] Fix | Delete
if (!$hasINT64 && !defined('PHP_INT_MIN')) {
[111] Fix | Delete
define('PHP_INT_MIN', ~PHP_INT_MAX);
[112] Fix | Delete
}
[113] Fix | Delete
}
[114] Fix | Delete
// if integers are 64-bit - no other check required
[115] Fix | Delete
if ($hasINT64 || (($num <= PHP_INT_MAX) && ($num >= PHP_INT_MIN))) { // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
[116] Fix | Delete
return true;
[117] Fix | Delete
}
[118] Fix | Delete
return false;
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
/**
[122] Fix | Delete
* @param string $fraction
[123] Fix | Delete
*
[124] Fix | Delete
* @return float
[125] Fix | Delete
*/
[126] Fix | Delete
public static function DecimalizeFraction($fraction) {
[127] Fix | Delete
list($numerator, $denominator) = explode('/', $fraction);
[128] Fix | Delete
return $numerator / ($denominator ? $denominator : 1);
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
/**
[132] Fix | Delete
* @param string $binarynumerator
[133] Fix | Delete
*
[134] Fix | Delete
* @return float
[135] Fix | Delete
*/
[136] Fix | Delete
public static function DecimalBinary2Float($binarynumerator) {
[137] Fix | Delete
$numerator = self::Bin2Dec($binarynumerator);
[138] Fix | Delete
$denominator = self::Bin2Dec('1'.str_repeat('0', strlen($binarynumerator)));
[139] Fix | Delete
return ($numerator / $denominator);
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
[144] Fix | Delete
*
[145] Fix | Delete
* @param string $binarypointnumber
[146] Fix | Delete
* @param int $maxbits
[147] Fix | Delete
*
[148] Fix | Delete
* @return array
[149] Fix | Delete
*/
[150] Fix | Delete
public static function NormalizeBinaryPoint($binarypointnumber, $maxbits=52) {
[151] Fix | Delete
if (strpos($binarypointnumber, '.') === false) {
[152] Fix | Delete
$binarypointnumber = '0.'.$binarypointnumber;
[153] Fix | Delete
} elseif ($binarypointnumber[0] == '.') {
[154] Fix | Delete
$binarypointnumber = '0'.$binarypointnumber;
[155] Fix | Delete
}
[156] Fix | Delete
$exponent = 0;
[157] Fix | Delete
while (($binarypointnumber[0] != '1') || (substr($binarypointnumber, 1, 1) != '.')) {
[158] Fix | Delete
if (substr($binarypointnumber, 1, 1) == '.') {
[159] Fix | Delete
$exponent--;
[160] Fix | Delete
$binarypointnumber = substr($binarypointnumber, 2, 1).'.'.substr($binarypointnumber, 3);
[161] Fix | Delete
} else {
[162] Fix | Delete
$pointpos = strpos($binarypointnumber, '.');
[163] Fix | Delete
$exponent += ($pointpos - 1);
[164] Fix | Delete
$binarypointnumber = str_replace('.', '', $binarypointnumber);
[165] Fix | Delete
$binarypointnumber = $binarypointnumber[0].'.'.substr($binarypointnumber, 1);
[166] Fix | Delete
}
[167] Fix | Delete
}
[168] Fix | Delete
$binarypointnumber = str_pad(substr($binarypointnumber, 0, $maxbits + 2), $maxbits + 2, '0', STR_PAD_RIGHT);
[169] Fix | Delete
return array('normalized'=>$binarypointnumber, 'exponent'=>(int) $exponent);
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/binary.html
[174] Fix | Delete
*
[175] Fix | Delete
* @param float $floatvalue
[176] Fix | Delete
*
[177] Fix | Delete
* @return string
[178] Fix | Delete
*/
[179] Fix | Delete
public static function Float2BinaryDecimal($floatvalue) {
[180] Fix | Delete
$maxbits = 128; // to how many bits of precision should the calculations be taken?
[181] Fix | Delete
$intpart = self::trunc($floatvalue);
[182] Fix | Delete
$floatpart = abs($floatvalue - $intpart);
[183] Fix | Delete
$pointbitstring = '';
[184] Fix | Delete
while (($floatpart != 0) && (strlen($pointbitstring) < $maxbits)) {
[185] Fix | Delete
$floatpart *= 2;
[186] Fix | Delete
$pointbitstring .= (string) self::trunc($floatpart);
[187] Fix | Delete
$floatpart -= self::trunc($floatpart);
[188] Fix | Delete
}
[189] Fix | Delete
$binarypointnumber = decbin($intpart).'.'.$pointbitstring;
[190] Fix | Delete
return $binarypointnumber;
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
/**
[194] Fix | Delete
* @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee-expl.html
[195] Fix | Delete
*
[196] Fix | Delete
* @param float $floatvalue
[197] Fix | Delete
* @param int $bits
[198] Fix | Delete
*
[199] Fix | Delete
* @return string|false
[200] Fix | Delete
*/
[201] Fix | Delete
public static function Float2String($floatvalue, $bits) {
[202] Fix | Delete
$exponentbits = 0;
[203] Fix | Delete
$fractionbits = 0;
[204] Fix | Delete
switch ($bits) {
[205] Fix | Delete
case 32:
[206] Fix | Delete
$exponentbits = 8;
[207] Fix | Delete
$fractionbits = 23;
[208] Fix | Delete
break;
[209] Fix | Delete
[210] Fix | Delete
case 64:
[211] Fix | Delete
$exponentbits = 11;
[212] Fix | Delete
$fractionbits = 52;
[213] Fix | Delete
break;
[214] Fix | Delete
[215] Fix | Delete
default:
[216] Fix | Delete
return false;
[217] Fix | Delete
}
[218] Fix | Delete
if ($floatvalue >= 0) {
[219] Fix | Delete
$signbit = '0';
[220] Fix | Delete
} else {
[221] Fix | Delete
$signbit = '1';
[222] Fix | Delete
}
[223] Fix | Delete
$normalizedbinary = self::NormalizeBinaryPoint(self::Float2BinaryDecimal($floatvalue), $fractionbits);
[224] Fix | Delete
$biasedexponent = pow(2, $exponentbits - 1) - 1 + $normalizedbinary['exponent']; // (127 or 1023) +/- exponent
[225] Fix | Delete
$exponentbitstring = str_pad(decbin($biasedexponent), $exponentbits, '0', STR_PAD_LEFT);
[226] Fix | Delete
$fractionbitstring = str_pad(substr($normalizedbinary['normalized'], 2), $fractionbits, '0', STR_PAD_RIGHT);
[227] Fix | Delete
[228] Fix | Delete
return self::BigEndian2String(self::Bin2Dec($signbit.$exponentbitstring.$fractionbitstring), $bits % 8, false);
[229] Fix | Delete
}
[230] Fix | Delete
[231] Fix | Delete
/**
[232] Fix | Delete
* @param string $byteword
[233] Fix | Delete
*
[234] Fix | Delete
* @return float|false
[235] Fix | Delete
*/
[236] Fix | Delete
public static function LittleEndian2Float($byteword) {
[237] Fix | Delete
return self::BigEndian2Float(strrev($byteword));
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
/**
[241] Fix | Delete
* ANSI/IEEE Standard 754-1985, Standard for Binary Floating Point Arithmetic
[242] Fix | Delete
*
[243] Fix | Delete
* @link http://www.psc.edu/general/software/packages/ieee/ieee.html
[244] Fix | Delete
* @link http://www.scri.fsu.edu/~jac/MAD3401/Backgrnd/ieee.html
[245] Fix | Delete
*
[246] Fix | Delete
* @param string $byteword
[247] Fix | Delete
*
[248] Fix | Delete
* @return float|false
[249] Fix | Delete
*/
[250] Fix | Delete
public static function BigEndian2Float($byteword) {
[251] Fix | Delete
$bitword = self::BigEndian2Bin($byteword);
[252] Fix | Delete
if (!$bitword) {
[253] Fix | Delete
return 0;
[254] Fix | Delete
}
[255] Fix | Delete
$signbit = $bitword[0];
[256] Fix | Delete
$floatvalue = 0;
[257] Fix | Delete
$exponentbits = 0;
[258] Fix | Delete
$fractionbits = 0;
[259] Fix | Delete
[260] Fix | Delete
switch (strlen($byteword) * 8) {
[261] Fix | Delete
case 32:
[262] Fix | Delete
$exponentbits = 8;
[263] Fix | Delete
$fractionbits = 23;
[264] Fix | Delete
break;
[265] Fix | Delete
[266] Fix | Delete
case 64:
[267] Fix | Delete
$exponentbits = 11;
[268] Fix | Delete
$fractionbits = 52;
[269] Fix | Delete
break;
[270] Fix | Delete
[271] Fix | Delete
case 80:
[272] Fix | Delete
// 80-bit Apple SANE format
[273] Fix | Delete
// http://www.mactech.com/articles/mactech/Vol.06/06.01/SANENormalized/
[274] Fix | Delete
$exponentstring = substr($bitword, 1, 15);
[275] Fix | Delete
$isnormalized = intval($bitword[16]);
[276] Fix | Delete
$fractionstring = substr($bitword, 17, 63);
[277] Fix | Delete
$exponent = pow(2, self::Bin2Dec($exponentstring) - 16383);
[278] Fix | Delete
$fraction = $isnormalized + self::DecimalBinary2Float($fractionstring);
[279] Fix | Delete
$floatvalue = $exponent * $fraction;
[280] Fix | Delete
if ($signbit == '1') {
[281] Fix | Delete
$floatvalue *= -1;
[282] Fix | Delete
}
[283] Fix | Delete
return $floatvalue;
[284] Fix | Delete
[285] Fix | Delete
default:
[286] Fix | Delete
return false;
[287] Fix | Delete
}
[288] Fix | Delete
$exponentstring = substr($bitword, 1, $exponentbits);
[289] Fix | Delete
$fractionstring = substr($bitword, $exponentbits + 1, $fractionbits);
[290] Fix | Delete
$exponent = self::Bin2Dec($exponentstring);
[291] Fix | Delete
$fraction = self::Bin2Dec($fractionstring);
[292] Fix | Delete
[293] Fix | Delete
if (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction != 0)) {
[294] Fix | Delete
// Not a Number
[295] Fix | Delete
$floatvalue = false;
[296] Fix | Delete
} elseif (($exponent == (pow(2, $exponentbits) - 1)) && ($fraction == 0)) {
[297] Fix | Delete
if ($signbit == '1') {
[298] Fix | Delete
$floatvalue = '-infinity';
[299] Fix | Delete
} else {
[300] Fix | Delete
$floatvalue = '+infinity';
[301] Fix | Delete
}
[302] Fix | Delete
} elseif (($exponent == 0) && ($fraction == 0)) {
[303] Fix | Delete
if ($signbit == '1') {
[304] Fix | Delete
$floatvalue = -0;
[305] Fix | Delete
} else {
[306] Fix | Delete
$floatvalue = 0;
[307] Fix | Delete
}
[308] Fix | Delete
$floatvalue = ($signbit ? 0 : -0);
[309] Fix | Delete
} elseif (($exponent == 0) && ($fraction != 0)) {
[310] Fix | Delete
// These are 'unnormalized' values
[311] Fix | Delete
$floatvalue = pow(2, (-1 * (pow(2, $exponentbits - 1) - 2))) * self::DecimalBinary2Float($fractionstring);
[312] Fix | Delete
if ($signbit == '1') {
[313] Fix | Delete
$floatvalue *= -1;
[314] Fix | Delete
}
[315] Fix | Delete
} elseif ($exponent != 0) {
[316] Fix | Delete
$floatvalue = pow(2, ($exponent - (pow(2, $exponentbits - 1) - 1))) * (1 + self::DecimalBinary2Float($fractionstring));
[317] Fix | Delete
if ($signbit == '1') {
[318] Fix | Delete
$floatvalue *= -1;
[319] Fix | Delete
}
[320] Fix | Delete
}
[321] Fix | Delete
return (float) $floatvalue;
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
/**
[325] Fix | Delete
* @param string $byteword
[326] Fix | Delete
* @param bool $synchsafe
[327] Fix | Delete
* @param bool $signed
[328] Fix | Delete
*
[329] Fix | Delete
* @return int|float|false
[330] Fix | Delete
* @throws Exception
[331] Fix | Delete
*/
[332] Fix | Delete
public static function BigEndian2Int($byteword, $synchsafe=false, $signed=false) {
[333] Fix | Delete
$intvalue = 0;
[334] Fix | Delete
$bytewordlen = strlen($byteword);
[335] Fix | Delete
if ($bytewordlen == 0) {
[336] Fix | Delete
return false;
[337] Fix | Delete
}
[338] Fix | Delete
for ($i = 0; $i < $bytewordlen; $i++) {
[339] Fix | Delete
if ($synchsafe) { // disregard MSB, effectively 7-bit bytes
[340] Fix | Delete
//$intvalue = $intvalue | (ord($byteword{$i}) & 0x7F) << (($bytewordlen - 1 - $i) * 7); // faster, but runs into problems past 2^31 on 32-bit systems
[341] Fix | Delete
$intvalue += (ord($byteword[$i]) & 0x7F) * pow(2, ($bytewordlen - 1 - $i) * 7);
[342] Fix | Delete
} else {
[343] Fix | Delete
$intvalue += ord($byteword[$i]) * pow(256, ($bytewordlen - 1 - $i));
[344] Fix | Delete
}
[345] Fix | Delete
}
[346] Fix | Delete
if ($signed && !$synchsafe) {
[347] Fix | Delete
// synchsafe ints are not allowed to be signed
[348] Fix | Delete
if ($bytewordlen <= PHP_INT_SIZE) {
[349] Fix | Delete
$signMaskBit = 0x80 << (8 * ($bytewordlen - 1));
[350] Fix | Delete
if ($intvalue & $signMaskBit) {
[351] Fix | Delete
$intvalue = 0 - ($intvalue & ($signMaskBit - 1));
[352] Fix | Delete
}
[353] Fix | Delete
} else {
[354] Fix | Delete
throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits ('.strlen($byteword).') in self::BigEndian2Int()');
[355] Fix | Delete
}
[356] Fix | Delete
}
[357] Fix | Delete
return self::CastAsInt($intvalue);
[358] Fix | Delete
}
[359] Fix | Delete
[360] Fix | Delete
/**
[361] Fix | Delete
* @param string $byteword
[362] Fix | Delete
* @param bool $signed
[363] Fix | Delete
*
[364] Fix | Delete
* @return int|float|false
[365] Fix | Delete
*/
[366] Fix | Delete
public static function LittleEndian2Int($byteword, $signed=false) {
[367] Fix | Delete
return self::BigEndian2Int(strrev($byteword), false, $signed);
[368] Fix | Delete
}
[369] Fix | Delete
[370] Fix | Delete
/**
[371] Fix | Delete
* @param string $byteword
[372] Fix | Delete
*
[373] Fix | Delete
* @return string
[374] Fix | Delete
*/
[375] Fix | Delete
public static function LittleEndian2Bin($byteword) {
[376] Fix | Delete
return self::BigEndian2Bin(strrev($byteword));
[377] Fix | Delete
}
[378] Fix | Delete
[379] Fix | Delete
/**
[380] Fix | Delete
* @param string $byteword
[381] Fix | Delete
*
[382] Fix | Delete
* @return string
[383] Fix | Delete
*/
[384] Fix | Delete
public static function BigEndian2Bin($byteword) {
[385] Fix | Delete
$binvalue = '';
[386] Fix | Delete
$bytewordlen = strlen($byteword);
[387] Fix | Delete
for ($i = 0; $i < $bytewordlen; $i++) {
[388] Fix | Delete
$binvalue .= str_pad(decbin(ord($byteword[$i])), 8, '0', STR_PAD_LEFT);
[389] Fix | Delete
}
[390] Fix | Delete
return $binvalue;
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
/**
[394] Fix | Delete
* @param int $number
[395] Fix | Delete
* @param int $minbytes
[396] Fix | Delete
* @param bool $synchsafe
[397] Fix | Delete
* @param bool $signed
[398] Fix | Delete
*
[399] Fix | Delete
* @return string
[400] Fix | Delete
* @throws Exception
[401] Fix | Delete
*/
[402] Fix | Delete
public static function BigEndian2String($number, $minbytes=1, $synchsafe=false, $signed=false) {
[403] Fix | Delete
if ($number < 0) {
[404] Fix | Delete
throw new Exception('ERROR: self::BigEndian2String() does not support negative numbers');
[405] Fix | Delete
}
[406] Fix | Delete
$maskbyte = (($synchsafe || $signed) ? 0x7F : 0xFF);
[407] Fix | Delete
$intstring = '';
[408] Fix | Delete
if ($signed) {
[409] Fix | Delete
if ($minbytes > PHP_INT_SIZE) {
[410] Fix | Delete
throw new Exception('ERROR: Cannot have signed integers larger than '.(8 * PHP_INT_SIZE).'-bits in self::BigEndian2String()');
[411] Fix | Delete
}
[412] Fix | Delete
$number = $number & (0x80 << (8 * ($minbytes - 1)));
[413] Fix | Delete
}
[414] Fix | Delete
while ($number != 0) {
[415] Fix | Delete
$quotient = ($number / ($maskbyte + 1));
[416] Fix | Delete
$intstring = chr(ceil(($quotient - floor($quotient)) * $maskbyte)).$intstring;
[417] Fix | Delete
$number = floor($quotient);
[418] Fix | Delete
}
[419] Fix | Delete
return str_pad($intstring, $minbytes, "\x00", STR_PAD_LEFT);
[420] Fix | Delete
}
[421] Fix | Delete
[422] Fix | Delete
/**
[423] Fix | Delete
* @param int $number
[424] Fix | Delete
*
[425] Fix | Delete
* @return string
[426] Fix | Delete
*/
[427] Fix | Delete
public static function Dec2Bin($number) {
[428] Fix | Delete
while ($number >= 256) {
[429] Fix | Delete
$bytes[] = (($number / 256) - (floor($number / 256))) * 256;
[430] Fix | Delete
$number = floor($number / 256);
[431] Fix | Delete
}
[432] Fix | Delete
$bytes[] = $number;
[433] Fix | Delete
$binstring = '';
[434] Fix | Delete
for ($i = 0; $i < count($bytes); $i++) {
[435] Fix | Delete
$binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, '0', STR_PAD_LEFT)).$binstring;
[436] Fix | Delete
}
[437] Fix | Delete
return $binstring;
[438] Fix | Delete
}
[439] Fix | Delete
[440] Fix | Delete
/**
[441] Fix | Delete
* @param string $binstring
[442] Fix | Delete
* @param bool $signed
[443] Fix | Delete
*
[444] Fix | Delete
* @return int|float
[445] Fix | Delete
*/
[446] Fix | Delete
public static function Bin2Dec($binstring, $signed=false) {
[447] Fix | Delete
$signmult = 1;
[448] Fix | Delete
if ($signed) {
[449] Fix | Delete
if ($binstring[0] == '1') {
[450] Fix | Delete
$signmult = -1;
[451] Fix | Delete
}
[452] Fix | Delete
$binstring = substr($binstring, 1);
[453] Fix | Delete
}
[454] Fix | Delete
$decvalue = 0;
[455] Fix | Delete
for ($i = 0; $i < strlen($binstring); $i++) {
[456] Fix | Delete
$decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
[457] Fix | Delete
}
[458] Fix | Delete
return self::CastAsInt($decvalue * $signmult);
[459] Fix | Delete
}
[460] Fix | Delete
[461] Fix | Delete
/**
[462] Fix | Delete
* @param string $binstring
[463] Fix | Delete
*
[464] Fix | Delete
* @return string
[465] Fix | Delete
*/
[466] Fix | Delete
public static function Bin2String($binstring) {
[467] Fix | Delete
// return 'hi' for input of '0110100001101001'
[468] Fix | Delete
$string = '';
[469] Fix | Delete
$binstringreversed = strrev($binstring);
[470] Fix | Delete
for ($i = 0; $i < strlen($binstringreversed); $i += 8) {
[471] Fix | Delete
$string = chr(self::Bin2Dec(strrev(substr($binstringreversed, $i, 8)))).$string;
[472] Fix | Delete
}
[473] Fix | Delete
return $string;
[474] Fix | Delete
}
[475] Fix | Delete
[476] Fix | Delete
/**
[477] Fix | Delete
* @param int $number
[478] Fix | Delete
* @param int $minbytes
[479] Fix | Delete
* @param bool $synchsafe
[480] Fix | Delete
*
[481] Fix | Delete
* @return string
[482] Fix | Delete
*/
[483] Fix | Delete
public static function LittleEndian2String($number, $minbytes=1, $synchsafe=false) {
[484] Fix | Delete
$intstring = '';
[485] Fix | Delete
while ($number > 0) {
[486] Fix | Delete
if ($synchsafe) {
[487] Fix | Delete
$intstring = $intstring.chr($number & 127);
[488] Fix | Delete
$number >>= 7;
[489] Fix | Delete
} else {
[490] Fix | Delete
$intstring = $intstring.chr($number & 255);
[491] Fix | Delete
$number >>= 8;
[492] Fix | Delete
}
[493] Fix | Delete
}
[494] Fix | Delete
return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
/**
[498] Fix | Delete
* @param mixed $array1
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function