WP_Translation_File_MO::detect_endian_and_validate_file()protectedWP 6.5.0

Detects endian and validates file.

Method of the class: WP_Translation_File_MO{}

No Hooks.

Return

false|'V'|'N'. V for little endian, N for big endian, or false on failure.

Usage

// protected - for code of main (parent) or child class
$result = $this->detect_endian_and_validate_file( $header );
$header(string) (required)
File contents.

Changelog

Since 6.5.0 Introduced.

WP_Translation_File_MO::detect_endian_and_validate_file() code WP 6.7.1

protected function detect_endian_and_validate_file( string $header ) {
	$big = unpack( 'N', $header );

	if ( false === $big ) {
		return false;
	}

	$big = reset( $big );

	if ( false === $big ) {
		return false;
	}

	$little = unpack( 'V', $header );

	if ( false === $little ) {
		return false;
	}

	$little = reset( $little );

	if ( false === $little ) {
		return false;
	}

	// Force cast to an integer as it can be a float on x86 systems. See https://core.trac.wordpress.org/ticket/60678.
	if ( (int) self::MAGIC_MARKER === $big ) {
		return 'N';
	}

	// Force cast to an integer as it can be a float on x86 systems. See https://core.trac.wordpress.org/ticket/60678.
	if ( (int) self::MAGIC_MARKER === $little ) {
		return 'V';
	}

	$this->error = 'Magic marker does not exist';
	return false;
}