S
S
SFY2021-04-21 11:45:01
PHP
SFY, 2021-04-21 11:45:01

Function ord,chr from php, how to implement?

Tell me please.
There is a package unpacking code written in php.
And the line:

$part[$i] = chr(ord($part[$i]) ^ ord($zBytes[$i]));

I moved the code to js, ​​but there is a moment.

In php, ord('Ф') will return 208, in js the result will be different - 1060.
In js , because of this, the result of unpacking the package will be incorrect. Why is this behavior happening and how to fix / solve it? all php source code:'Ф'.charCodeAt(0)


function UnMunge($packet) {
    $MungeTable2 = array(0x05, 0x61, 0x7A, 0xED, 0x1B, 0xCA, 0x0D, 0x9B, 0x4A, 0xF1, 0x64, 0xC7, 0xB5, 0x8E, 0xDF, 0xA0);
    $z = ord($packet[0]);
    $packet = substr($packet, 8);
    $length = strlen($packet);
    $res = "";
    $count = 0;
    if ($length < 1) {
        return null;
    }

    $notz = ~$z;
    $length = $length >> 2;
    $zBytes = pack("L", $z);
    $notzBytes = pack("L", $notz);
    $x = 0;
    while ($length--) {
        $part = substr($packet, $count * 4, 4);
        for ($i = 0; $i < 4; $i++) {
            $part[$i] = chr(ord($part[$i]) ^ ord($zBytes[$i]));
        }

        for ($i = 0; $i < 4; $i++) {
            $part[$i] = chr(ord($part[$i]) ^ ((($MungeTable2[($count + $i) & 0x0F] | ($i << $i)) | $i) | 0xA5));
        }

        $s = "0000";
        for ($i = 0, $i2 = 3; $i < 4; $i++, $i2--) {
            $s[$i] = chr(ord($part[$i2]) ^ ord($notzBytes[$i]));
        }
        $res .= $s;
        $count++;
    }
    return $res;
}
$a = '010000c0010000805a18010051001041032a40016001014124660f377e3933737948106f908fd021c1b8c0812ba8c0f3a5c8f4919b99d1d0f48083908120e091d1d881be8becc0b48a99d1bfcbb0c0c3d1ab90b3c2fdc0f4411a71ca';
$a = hex2bin($a);
var_dump(UnMunge($a));
string(84) "J:Asv_version* Права доступа предоставлены! "

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2021-04-21
@SFY

String.fromCharCode
String.prototype.charCodeAt

S
Stalker_RED, 2021-04-21
@Stalker_RED

You may have heard there is such a thing as text encodings , and the increasing use is UTF-8 , which is a multi-byte encoding.
And ord() is a function for working with single-byte.

function showOrd($char) {
  echo "\"$char\""
  . ' ord='.ord($char)
  . ' mb_ord='.mb_ord($char)
  . PHP_EOL;
}

showOrd('а'); // "а" ord=208 mb_ord=1072
showOrd('ф'); // "ф" ord=209 mb_ord=1092
showOrd('я'); // "я" ord=209 mb_ord=1103

The letter 'F' in utf-8 is represented by two bytes, not one, and if you use ord() on both bytes, you get this result:
$str = "Ф";
for ( $pos=0; $pos < strlen($str); $pos ++ ) {
 $byte = substr($str, $pos);
 echo 'Byte ' . $pos . ' of $str has value ' . ord($byte) . PHP_EOL;
}
// Byte 0 of $str has value 208
// Byte 1 of $str has value 164

Look at the description of the Unicode letter 'F', note that the binary representation is 11010000:10100100, the first byte is 208, the second is 164.
https://www.fileformat.info/info/unicode/char/424/...
And the character in general consists of four bytes at once.
You just need to learn how to read a file one byte at a time in binary form, and then neither ord() nor charCodeAt() will be needed, you will immediately have a binary representation of this byte.
you just need to parse the starting game packages.
Do you have them as a string or is it a binary? If a binary, then work with it as with a binary.
fReader.readAsArrayBuffer(file);
All the possibilities for this are in js.
https://developer.mozilla.org/en-US/docs/Web/JavaS...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question