wppaste
WordPress

Box::parse( Avifinfo\stream $handle, int $num_parsed_boxes, int $num_remaining_bytes = MAX_SIZE ): Avifinfo\Status

Source
wp-includes/class-avif-info.php:231
Reads the box header.

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$handleAvifinfo\stream
The resource the header will be parsed from.
$num_parsed_boxesint
The total number of parsed boxes. Prevents timeouts.
$num_remaining_bytesintoptional
The number of bytes that should be available from the resource.Default: MAX_SIZE

Return value

Avifinfo\Status
FOUND on success or an error on failure.

Performance profile

How much work a call to Box::parse() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
44–135

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 186.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost Box::parse() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$num_remaining_bytes && !$header_size44–72read(), read_big_endian()
!$num_remaining_bytes && !$header_size && !$num_parsed_boxes && !$data63–76read(), read_big_endian(), read()
!$num_remaining_bytes && !$header_size && !$num_parsed_boxes && $data85–135read(), read_big_endian(), read(), read_big_endian(), read_big_endian()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev18644–13534
8.518644–13534
8.418644–135349 fewer instructions than PHP 8.3
8.319547–14134
8.219547–14134
8.119547–14134
7.419547–14134

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

  • read()Reads bytes and advances the stream position by the same count.
  • read_big_endian()Reads an unsigned integer with most significant bits first.

Source code

  public function parse( $handle, &$num_parsed_boxes, $num_remaining_bytes = MAX_SIZE ) {    // See ISO/IEC 14496-12:2012(E) 4.2    $header_size = 8; // box 32b size + 32b type (at least)    if ( $header_size > $num_remaining_bytes ) {      return INVALID;    }    if ( !( $data = read( $handle, 8 ) ) ) {      return TRUNCATED;    }    $this->size = read_big_endian( $data, 4 );    $this->type = substr( $data, 4, 4 );    // 'box->size==1' means 64-bit size should be read after the box type.    // 'box->size==0' means this box extends to all remaining bytes.    if ( $this->size == 1 ) {      $header_size += 8;      if ( $header_size > $num_remaining_bytes ) {        return INVALID;      }      if ( !( $data = read( $handle, 8 ) ) ) {        return TRUNCATED;      }      // Stop the parsing if any box has a size greater than 4GB.      if ( read_big_endian( $data, 4 ) != 0 ) {        return ABORTED;      }      // Read the 32 least-significant bits.      $this->size = read_big_endian( substr( $data, 4, 4 ), 4 );    } else if ( $this->size == 0 ) {      // ISO/IEC 14496-12 4.2.2:      //   if size is 0, then this box shall be in a top-level box      //   (i.e. not contained in another box)      // Unfortunately the presence of a parent box is unknown here.      $this->size = $num_remaining_bytes;    }    if ( $this->size < $header_size ) {      return INVALID;    }    if ( $this->size > $num_remaining_bytes ) {      return INVALID;    }     // 16 bytes of usertype should be read here if the box type is 'uuid'.    // 'uuid' boxes are skipped so usertype is part of the skipped body.     $has_fullbox_header = $this->type == 'meta' || $this->type == 'pitm' ||                          $this->type == 'ipma' || $this->type == 'ispe' ||                          $this->type == 'pixi' || $this->type == 'iref' ||                          $this->type == 'auxC';    if ( $has_fullbox_header ) {      $header_size += 4;    }    if ( $this->size < $header_size ) {      return INVALID;    }    $this->content_size = $this->size - $header_size;    // Avoid timeouts. The maximum number of parsed boxes is arbitrary.    ++$num_parsed_boxes;    if ( $num_parsed_boxes >= MAX_NUM_BOXES ) {      return ABORTED;    }     $this->version = 0;    $this->flags   = 0;    if ( $has_fullbox_header ) {      if ( !( $data = read( $handle, 4 ) ) ) {        return TRUNCATED;      }      $this->version = read_big_endian( $data, 1 );      $this->flags   = read_big_endian( substr( $data, 1, 3 ), 3 );      // See AV1 Image File Format (AVIF) 8.1      // at https://aomediacodec.github.io/av1-avif/#avif-boxes (available when      // https://github.com/AOMediaCodec/av1-avif/pull/170 is merged).      $is_parsable = ( $this->type == 'meta' && $this->version <= 0 ) ||                     ( $this->type == 'pitm' && $this->version <= 1 ) ||                     ( $this->type == 'ipma' && $this->version <= 1 ) ||                     ( $this->type == 'ispe' && $this->version <= 0 ) ||                     ( $this->type == 'pixi' && $this->version <= 0 ) ||                     ( $this->type == 'iref' && $this->version <= 1 ) ||                     ( $this->type == 'auxC' && $this->version <= 0 );      // Instead of considering this file as invalid, skip unparsable boxes.

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-avif-info.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.