I am trying to build a function which decodes a CS:GO match sharing code. I have seen enough examples but everyhting is in JS or C# but nothing in PHP.
I took the akiver demo manager as an example and i tried to replicate it in PHP. I am going bit blind because i have no idea what is the output on a certain points so i can only hope that the result will be what i expect it to be. I think i am on the right path, the problem comes when the bytes have to be created/interpeted/converted to the desire outcome.
The code that should be decoded is the following: 'CSGO-oPRbA-uTQuR-UFkiC-hYWMB-syBcO' ($getNextGame variable)
The result should be 3418217537907720662
My code so far:
/**
* #param $getNextGame
* #return array
*/
public function decodeDemoCode(string $getNextGame): array
{
$shareCodePattern = "/CSGO(-?[\w]{5}){5}$/";
if (preg_match($shareCodePattern, $getNextGame) === 1) {
$result = [];
$bigNumber = 0;
$matchIdBytes = $outcomeIdBytes = $tvPortIdBytes = [];
$dictionary = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789";
$dictionaryLength = strlen($dictionary);
$changedNextGame = str_replace(array("CSGO", "-"), "", $getNextGame);
$chars = array_reverse(str_split($changedNextGame));
foreach ($chars as $char) {
$bigNumber = ($bigNumber * $dictionaryLength) + strpos($dictionary, $char);
}
}
}
This brings me back something like that:
1.86423701402E+43 (double)
Then i have the following:
$packed = unpack("C*", $bigNumber);
$reversedPacked = array_reverse($packed);
and this brings the following back:
array(17 items)
0 => 51 (integer)
1 => 52 (integer)
2 => 43 (integer)
3 => 69 (integer)
4 => 50 (integer)
5 => 48 (integer)
6 => 52 (integer)
7 => 49 (integer)
8 => 48 (integer)
9 => 55 (integer)
10 => 51 (integer)
11 => 50 (integer)
12 => 52 (integer)
13 => 54 (integer)
14 => 56 (integer)
15 => 46 (integer)
16 => 49 (integer)
Now here i am not really sure what to do because i do not completely understand C# and i have never worked with bytes in PHP before.
Generally the return type should be an array and would look something like that:
$result = [
matchId => 3418217537907720662,
reservationId => 3418217537907720662,
tvPort => 55788
];
Thanks in advance. Any help is deeply appreciated
I have created a PHP class which makes that possible:
CS:GO ShareCode Decoder PHP
The first problem you have to solve is the returned double value. PHP has limitation when it comes to big integers. More to that here What is the maximum value for an integer in PHP.
Because of this limitation you are losing precision leading to inaccurate results. In order to solve this problem you will have to use one of these libraries GMB, BC Math. What these libraries do, is to give you back the result as a string which solves the double value you got.
So your code has to look something like that:
foreach ($chars as $char) {
$bigNumber = gmp_add(
gmp_mul($bigNumber,$dictionaryLength),
strpos($dictionary,$char)
);
}
json_encode($bigNumber);
$result = json_decode($bigNumber, true, 512, JSON_BIGINT_AS_STRING);
This will give you back the following "18642370140230194654275126136176397505221000"
You do not really need the PHP pack and unpack functions since the results can be generated without them. The next step is to convert your number to hexadecimal. You can do that with the following:
$toHex = gmp_strval(gmp_init($number, 10), 16);
Again you need to use the gmp library in order to get the desired value. What you do, is to make sure that the result is a string and then you convert your number's base from 10 to 16 which is the equivalent of hexadecimal. The results is the following:
"d6010080bdf26f2fbf0100007cf76f2f5188"
The next step is to convert the hex value to an array of byte integers. It looks like this:
$bytes = [];
$byteArray= str_split($toHex, 2);
foreach ($byteArray as $byte) {
$bytes[] = (int)base_convert($byte, 16, 10);
}
What you do here is to split the array to every two chars. The $byteArray variable looks like this (before it enters the foreach loop)
array(18 items)
0 => 'd6' (2 chars) 1 => '01' (2 chars) 2 => '00' (2 chars) 3 => '80' (2 chars)
4 => 'bd' (2 chars) 5 => 'f2' (2 chars) 6 => '6f' (2 chars) 7 => '2f' (2 chars)
8 => 'bf' (2 chars) 9 => '01' (2 chars) 10 => '00' (2 chars) 11 => '00' (2 chars)
12 => '7c' (2 chars) 13 => 'f7' (2 chars) 14 => '6f' (2 chars) 15 => '2f' (2 chars)
16 => '51' (2 chars) 17 => '88' (2 chars)
Now you will have to convert each entry into integer. Since the results are not that big anymore you can change the base of your values with the base_convert function. The base is 16 (hex) and you will have to change it back to 10. The results $bytes after the foreach loop looks like this:
array(18 items)
0 => 214 (integer) 1 => 1 (integer) 2 => 0 (integer) 3 => 128 (integer)
4 => 189 (integer) 5 => 242 (integer) 6 => 111 (integer) 7 => 47 (integer)
8 => 191 (integer) 9 => 1 (integer) 10 => 0 (integer) 11 => 0 (integer)
12 => 124 (integer) 13 => 247 (integer) 14 => 111 (integer) 15 => 47 (integer)
16 => 81 (integer) 17 => 136 (integer)
Now you have to define which bytes are responsible for each result.
$matchIdBytes = array_reverse(array_slice($bytes, 0, 8));
$reservationIdBytes = array_reverse(array_slice($bytes, 8, 8));
$portBytes = array_reverse(array_slice($bytes, 16, 2));
For the match id you will have to get the first 8 entries and the reverse the array
For the reservation id you will have to get the next 8 entries starting from the 8th entry and reverse the array
For the port you will have to get the last 2 entries and reverse the array
Now you will have to return the value
return [
'matchId' => $this->getResultFromBytes($matchIdBytes),
'reservationId' => $this->getResultFromBytes($reservationIdBytes),
'tvPort' => $this->getResultFromBytes($portBytes)
];
The getResultFromBytes() function:
**
* #param array $bytes
* #return string
*/
public function getResultFromBytes(array $bytes): string
{
$chars = array_map("chr", $bytes);
$bin = implode($chars);
$hex = bin2hex($bin);
return gmp_strval($this->gmp_hexDec($hex));
}
/**
* #param $n
* #return string
*/
public function gmp_hexDec($n): string
{
$gmp = gmp_init(0);
$multi = gmp_init(1);
for ($i=strlen($n)-1;$i>=0;$i--,$multi=gmp_mul($multi, 16)) {
$gmp = gmp_add($gmp, gmp_mul($multi, hexdec($n[$i])));
}
return $gmp;
}
Best regards
Related
Im doing a Grib2 decoder in PHP, and started with a half written library that I found. Everything is working fine except the values I get from the data are incorrect after converting Int Values to real values. I think I am converting everything right, and even when I test with cloud data it looks correct when I check it in Panoply. I think its with this formula that is all over the internet. Below im using 10 m above ground GFS from https://nomads.ncep.noaa.gov
Y*10^D = R+(X1+X2)*2^E
Im not sure I'm plugging in the values correctly but again it works with cloud cover percentages.
So.... The "Data Representation Values" I get from Grib Section 5
'Reference value (R)' => 886.25067138671875,
'Binary Scale Factor (E)' => 0,
'Decimal Scale Factor (D)' => 2,
'Number of bits used for each packed value' => 11,
'exp' => pow(2, $E), //(Equals 1) (The Library used these as the 2^E)
'base' => pow(10, $D), //(Equals 100) (And the 10^D)
'template' => 0,
As you can see below the numbers definitely have a connection to the Reference Value. The Number closest to 886(R) is 892 and its actual value should be 0.05 as shown below (EX.) The numbers Higher are than 892 are positive and the ones lower than 892 are negative. But when I user the formula (886 + 892 * 1) / 100 it give me 17.78, not 0.05. I seem to be missing something pretty obvious, am I misunderstanding the formula/equation where Y is the value I want...
X1 = 0 (documentation says)
X2 = 892 (documentation says is scaled value, the value in the Grib from bits?)
2^0 = 1
10^2 = 100
R = 886.25067138671875
Y * 10^D = R + (X1 + X2) * 2^E
Y * 100 = R + (X1 + X2) * 1
886 + (0 + 892) * 1 ) / 100
(886 + 892 * 1) / 100
= 17.78
Int Values of wind from Grib (After converting from Bits)
0 => 695,
1 => 639,
2 => 631,
3 => 0,
4 => 436,
5 => 513,
6 => 690,
7 => 570,
8 => 625,
9 => 805,
10 => 892,<-----------(EX.)
11 => 1044,
12 => 952,
13 => 1081,
14 => 1414,
15 => 997,
16 => 1106,
17 => 974,
18 => 1135,
19 => 1069,
20 => 912,
Actual decoded wind values shown in Panoply (Well known Grib App)
-1.9125067
-2.4725068
-2.5525067
-8.862507
-4.5025067
-3.7325068
-1.9625068
-3.1625068
-2.6125066
-0.81250674
0.057493284 <-----------(EX.)
1.5774933
0.6574933
1.9474933
5.2774935
1.1074933
2.1974933
0.87749326
2.4874933
1.8274933
0.2574933
y = 0.01 * (x - 886.25067138671875) seems to work for all points
so 0.01 * (892 - 886.25067138671875) = 0.0574
I'm beginner in PHP scripting. As per my knowledge, array means collection of same types of elements but PHP violating that rule.
Here is my code:
<!DOCTYPE html>
<html>
<body>
<?php
$a = 10;
echo $a;
$arr = array();
for($i = 0; $i < 10; $i++){
$arr[$i] = $i * $i; // Integer values
}
$arr[10]="Rohit"; // string value
$arr[11] = 10.15; // float value
$arr[12] = true; // boolean value
for($i = 0; $i < COUNT($arr); $i++){
echo "<br>".$arr[$i];
}
echo "<br>Length of array: ".COUNT($arr);
echo "var_dump output: ".var_dump($arr);
?>
</body>
</html>
and here is what I got result:
10
0
1
4
9
16
25
36
49
64
81
Rohit
10.15
1
Length of array: 13
C:\wamp\www\phpModules\check16_6_18_study.php:28:
array (size=13)
0 => int 0
1 => int 1
2 => int 4
3 => int 9
4 => int 16
5 => int 25
6 => int 36
7 => int 49
8 => int 64
9 => int 81
10 => string 'Rohit' (length=5)
11 => float 10.15
12 => boolean true
var_dump output:
You can notice first 10 elements are integers, 10th element is string, 11th element is float and 12th element is boolean.
Please anyone explain me why PHP has such confusing behavior?
PHP is a language with a dynamic type system. Like most dynamic languages such as Python, Javascript, Ruby don't check types for anything by default. It's really just emergent behavior from how everything else works.
I want to convert the byte array from a webservice to image. The webservice response array looks like the following but I could not figure out how to render this array back to an image. Please help me
'MemberImage' =>
array (size=151745)
0 => int 255
1 => int 216
2 => int 255
3 => int 224
4 => int 0
5 => int 16
6 => int 74
7 => int 70
8 => int 73
9 => int 70
10 => int 0
11 => int 1
12 => int 1
13 => int 1
14 => int 0
15 => int 72
16 => int 0
17 => int 72
18 => int 0
19 => int 0
20 => int 255 ...
Use pack to convert data into binary string, es:
$data = implode('', array_map(function($e) {
return pack("C*", $e);
}, $MemberImage));
// header here
// ...
// body
echo $data;
If you want to convert that array to an actual byte array (i.e. a binary string in PHP) you could use the following function...
function arrayToBinaryString(Array $arr) {
$str = "";
foreach($arr as $elm) {
$str .= chr((int) $elm);
}
return $str;
}
You could also use pack instead of the above function to do the same thing like so...
call_user_func_array('pack', array_merge(['C*'], $arr));
or in PHP 5.6+
pack('C*', ...$arr);
With that you could then - in theory - use the binary string as an image. So, for example, assuming you want to output the raw image data, and that the image is say a PNG, you would do something like the following, in conjunction with the above code...
header('Content-type: image/png');
echo arrayToBinaryString($myArray);
Just be sure to edit the Content-type header with whatever type the actual image is. If you don't know you could use something like getimagesize on the binary string to extract the MIME type from the image data.
$tmpFile = tempnam("/tmp");
$image = arrayToBinaryString($myArray);
file_put_conetnts($tmpFile, $image);
$imageData = getimagesize($tmpFile);
header("Content-type: {$imageData['mime']}");
echo $image;
unlink($tmpFile);
I'm trying to convert a binary string to a byte array of a specific format.
Sample binary data:
ê≤ÚEZêK
The hex version of the binary string looks like this:
00151b000000000190b2f20304455a000003900000004b0000
The Python script uses struct package and unpacks the above string (in binary) using this code:
data = unpack(">hBiiiiih",binarydata)
The desired byte array looks like this. This is also the output of the data array is:
(21, 27, 0, 26260210, 50611546, 912, 75, 0)
How can I unpack the same binary string using PHP's unpack() function and get the same output? That is, what's the >hBiiiiih equivalent in PHP?
So far my PHP code
$hex = "00151b000000000190b2f20304455a000003900000004b0000";
$bin = pack("H*",$hex);
print_r(unpack("x/c*"));
Which gives:
Array ( [*1] => 21 [*2] => 27 [*3] => 0 [*4] => 0 [*5] => 0 [*6] => 0 [*7] => 1 [*8] => -112 [*9] => -78 [*10] => -14 [*11] => 3 [*12] => 4 [*13] => 69 [*14] => 90 [*15] => 0 [*16] => 0 [*17] => 3 [*18] => -112 [*19] => 0 [*20] => 0 [*21] => 0 [*22] => 75 [*23] => 0 [*24] => 0 )
Would also appreciate links to a PHP tutorial on working with pack/unpack.
This produces the same result as does Python, but it treats signed values as unsigned because unpack() does not have format codes for signed values with endianness. Also note that the integers are converted using long, but this is OK because both have the same size.
$hex = "00151b000000000190b2f20304455a000003900000004b0000";
$bin = pack("H*", $hex);
$x = unpack("nbe_unsigned_1/Cunsigned_char/N5be_unsigned_long/nbe_unsigned_2", $bin);
print_r($x);
Array
(
[be_unsigned_1] => 21
[unsigned_char] => 27
[be_unsigned_long1] => 0
[be_unsigned_long2] => 26260210
[be_unsigned_long3] => 50611546
[be_unsigned_long4] => 912
[be_unsigned_long5] => 75
[be_unsigned_2] => 0
)
Because this data is treated as unsigned, you will need to detect whether the original data was negative, which can be done for 2 byte shorts with something similar to this:
if $x["be_unsigned_1"] >= pow(2, 15)
$x["be_unsigned_1"] = $x["be_unsigned_1"] - pow(2, 16);
and for longs using
if $x["be_unsigned_long2"] >= pow(2, 31)
$x["be_unsigned_long2"] = $x["be_unsigned_long2"] - pow(2, 32);
I'm new to php so please take it easy.
I have created an array of integers. 1-100. What I want to do is shuffle the array and remove random numbers from it leaving only lets say 15 numbers.
This is what I have done so far, can't figure out how to remove the random numbers. I know I could possibly use unset function but I'm unsure how could I use it in my situation.
// Create an Array using range() function
$element = range(1, 100);
// Shuffling $element array randomly
shuffle($element);
// Set amount of number to get rid of from array
$numbersOut = 85;
// Remove unnecessary items from the array
var_dump($element);
Just try with:
$element = range(1, 100);
shuffle($element);
$output = array_slice($element, 0, 15);
var_dump($output);
Output:
array (size=15)
0 => int 78
1 => int 40
2 => int 10
3 => int 94
4 => int 82
5 => int 16
6 => int 15
7 => int 57
8 => int 79
9 => int 83
10 => int 32
11 => int 13
12 => int 96
13 => int 48
14 => int 62
Or if you want to use $numbersOut variable:
$numbersOut = 85;
$output = array_slice($element, $numbersOut);
It will slice an array from 85 to the end. Remember - if you will have 90 elements in the array, this method will return just 5 elements.