<commit_before><?php
/**
 * CFPropertyList
 * {@link http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/plist.5.html Property Lists}
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @package plist
 * @version $Id$
 */

/**
 * Facility for reading and writing binary PropertyLists. Ported from {@link http://www.opensource.apple.com/source/CF/CF-476.15/CFBinaryPList.c CFBinaryPList.c}.
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @package plist
 * @example example-read-02.php Read a Binary PropertyList
 * @example example-read-03.php Read a PropertyList without knowing the type
 */
abstract class CFBinaryPropertyList {
  /**
   * Table containing uniqued objects
   * @var array
   */
  protected $uniqueTable = Array();

  /**
   * Number of objects in file
   * @var integer
   */
  protected $countObjects = 0;

  /**
   * The length of all strings in the file (byte length, not character length)
   * @var integer
   */
  protected $stringSize = 0;

  /**
   * The length of all ints in file (byte length)
   * @var integer
   */
  protected $intSize = 0;

  /**
   * The length of misc objects (i.e. not integer and not string) in file
   * @var integer
   */
  protected $miscSize = 0;

  /**
   * Number of object references in file (needed to calculate reference byte length)
   * @var integer
   */
  protected $objectRefs = 0;

  /**
   * Number of objects written during save phase; needed to calculate the size of the object table
   * @var integer
   */
  protected $writtenObjectCount = 0;

  /**
   * Table containing all objects in the file
   */
  protected $objectTable = Array();

  /**
   * The size of object references
   */
  protected $objectRefSize = 0;

  /**
   * The „offsets” (i.e. the different entries) in the file
   */
  protected $offsets = Array();

  /**
   * Read a „null type” (filler byte, true, false, 0 byte)
   * @param $length The byte itself
   * @return the byte value (e.g. CFBoolean(true), CFBoolean(false), 0 or 15)
   * @throws PListException on encountering an unknown null type
   */
  protected function readBinaryNullType($length) {
    switch($length) {
      case 0: return 0; // null type
      case 8: return new CFBoolean(false);
      case 9: return new CFBoolean(true);
      case 15: return 15; // fill type
    }

    throw new PListException("unknown null type: $length");
  }

  /**
   * Read an integer value
   * @param string $fname The filename of the file we're reading from
   * @param ressource $fd The file descriptor we're reading from
   * @param integer $length The length (in bytes) of the integer value, coded as „set bit $length to 1”
   * @return CFNumber The integer value
   * @throws PListException if integer val is invalid
   * @throws IOException if read error occurs
   */
  protected function readBinaryInt($fname,$fd,$length) {
    if($length > 3) throw new PListException("Integer greater than 8 bytes: $length");

    $nbytes = 1 << $length;

    $val = null;
    if(strlen($buff = fread($fd, $nbytes)) != $nbytes) throw IOException::readError($fname);
    switch($length) {
      case 0:
        $val = unpack("C", $buff);
        $val = $val[1];
        break;
      case 1:
        $val = unpack("n", $buff);
        $val = $val[1];
        break;
      case 2:
        $val = unpack("N", $buff);
        $val = $val[1];
        break;
      case 3:
        $words = unpack("Nhighword/Nlowword",$buff);
        $val = $words['highword'] << 32 | $words['lowword'];
        if($val > pow(2,63)) $va -= pow(2,64);
        break;
    }

    return new CFNumber($val);
  }

  /**
   * Read a real value
   * @param string $fname The filename of the file we're reading from
   * @param ressource $fd The file descriptor we're reading from
   * @param integer $length The length (in bytes) of the integer value, coded as „set bit $length to 1”
   * @return CFNumber The real value
   * @throws PListException if real val is invalid
   * @throws IOException if read error occurs
   */
  protected function readBinaryReal($fname,$fd,$length) {
    if($length > 3) throw new PListException("Real greater than 8 bytes: $length");

    $nbytes = 1 << $length;
    $val = null;
    if(strlen($buff = fread($fd, $nbytes)) != $nbytes) throw IOException::readError($fname);

    switch($length) {
      case 0: // 1 byte float? must be an error<commit_msg>fixed small typo (va => val)<commit_after><?php
/**
 * CFPropertyList
 * {@link http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/plist.5.html Property Lists}
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @package plist
 * @version $Id$
 */

/**
 * Facility for reading and writing binary PropertyLists. Ported from {@link http://www.opensource.apple.com/source/CF/CF-476.15/CFBinaryPList.c CFBinaryPList.c}.
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @package plist
 * @example example-read-02.php Read a Binary PropertyList
 * @example example-read-03.php Read a PropertyList without knowing the type
 */
abstract class CFBinaryPropertyList {
  /**
   * Table containing uniqued objects
   * @var array
   */
  protected $uniqueTable = Array();

  /**
   * Number of objects in file
   * @var integer
   */
  protected $countObjects = 0;

  /**
   * The length of all strings in the file (byte length, not character length)
   * @var integer
   */
  protected $stringSize = 0;

  /**
   * The length of all ints in file (byte length)
   * @var integer
   */
  protected $intSize = 0;

  /**
   * The length of misc objects (i.e. not integer and not string) in file
   * @var integer
   */
  protected $miscSize = 0;

  /**
   * Number of object references in file (needed to calculate reference byte length)
   * @var integer
   */
  protected $objectRefs = 0;

  /**
   * Number of objects written during save phase; needed to calculate the size of the object table
   * @var integer
   */
  protected $writtenObjectCount = 0;

  /**
   * Table containing all objects in the file
   */
  protected $objectTable = Array();

  /**
   * The size of object references
   */
  protected $objectRefSize = 0;

  /**
   * The „offsets” (i.e. the different entries) in the file
   */
  protected $offsets = Array();

  /**
   * Read a „null type” (filler byte, true, false, 0 byte)
   * @param $length The byte itself
   * @return the byte value (e.g. CFBoolean(true), CFBoolean(false), 0 or 15)
   * @throws PListException on encountering an unknown null type
   */
  protected function readBinaryNullType($length) {
    switch($length) {
      case 0: return 0; // null type
      case 8: return new CFBoolean(false);
      case 9: return new CFBoolean(true);
      case 15: return 15; // fill type
    }

    throw new PListException("unknown null type: $length");
  }

  /**
   * Read an integer value
   * @param string $fname The filename of the file we're reading from
   * @param ressource $fd The file descriptor we're reading from
   * @param integer $length The length (in bytes) of the integer value, coded as „set bit $length to 1”
   * @return CFNumber The integer value
   * @throws PListException if integer val is invalid
   * @throws IOException if read error occurs
   */
  protected function readBinaryInt($fname,$fd,$length) {
    if($length > 3) throw new PListException("Integer greater than 8 bytes: $length");

    $nbytes = 1 << $length;

    $val = null;
    if(strlen($buff = fread($fd, $nbytes)) != $nbytes) throw IOException::readError($fname);
    switch($length) {
      case 0:
        $val = unpack("C", $buff);
        $val = $val[1];
        break;
      case 1:
        $val = unpack("n", $buff);
        $val = $val[1];
        break;
      case 2:
        $val = unpack("N", $buff);
        $val = $val[1];
        break;
      case 3:
        $words = unpack("Nhighword/Nlowword",$buff);
        $val = $words['highword'] << 32 | $words['lowword'];
        if($val > pow(2,63)) $val -= pow(2,64);
        break;
    }

    return new CFNumber($val);
  }

  /**
   * Read a real value
   * @param string $fname The filename of the file we're reading from
   * @param ressource $fd The file descriptor we're reading from
   * @param integer $length The length (in bytes) of the integer value, coded as „set bit $length to 1”
   * @return CFNumber The real value
   * @throws PListException if real val is invalid
   * @throws IOException if read error occurs
   */
  protected function readBinaryReal($fname,$fd,$length) {
    if($length > 3) throw new PListException("Real greater than 8 bytes: $length");

    $nbytes = 1 << $length;
    $val = null;
    if(strlen($buff = fread($fd, $nbytes)) != $nbytes) throw IOException::readError($fname);

    switch($length) {
      case 0: // 1 byte float? must be an error
{'fraction_non_alphanumeric': 0.1279890685493054, 'fraction_numerical': 0.012525620587565475, 'mean_word_length': 2.886283185840708, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4857900', 'n_tokens_mistral': 2807, 'n_tokens_neox': 2647, 'n_words': 953}