read_big_endian

The timeline below displays how wordpress function read_big_endian has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

// Value was not yet parsed.
/**
 * Reads an unsigned integer with most significant bits first.
 *
 * @param binary string $input     Must be at least $num_bytes-long.
 * @param int           $num_bytes Number of parsed bytes.
 * @return int                     Value.
 */
function read_big_endian($input, $num_bytes)
{
    if ($num_bytes == 1) {
        return unpack('C', $input)[1];
    } else if ($num_bytes == 2) {
        return unpack('n', $input)[1];
    } else if ($num_bytes == 3) {
        $bytes = unpack('C3', $input);
        return $bytes[1] << 16 | $bytes[2] << 8 | $bytes[3];
    } else {
        // $num_bytes is 4
        // This might fail to read unsigned values >= 2^31 on 32-bit systems.
        // See https://www.php.net/manual/en/function.unpack.php#106041
        return unpack('N', $input)[1];
    }
}