Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Added version/compatibility level info
Browse files Browse the repository at this point in the history
  • Loading branch information
adriweb committed Sep 30, 2015
1 parent 289540a commit fc4f60f
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/TIVarFile.php
Expand Up @@ -30,7 +30,10 @@ class TIVarFile extends BinaryFile
'data_length2' => null,
'data' => null
];
/** @var TIVarType */
private $type = null;
/** @var TIVersion */
private $version = null;
private $computedChecksum = null;
private $inFileChecksum = null;
private $isFromFile = null;
Expand Down Expand Up @@ -75,7 +78,7 @@ public static function loadFromFile($filePath = '')
}
}

public static function createNew(TIVarType $type = null, $name = '')
public static function createNew(TIVarType $type = null, $name = '', TIVersion $version = null)
{
if ($type !== null)
{
Expand All @@ -92,8 +95,9 @@ public static function createNew(TIVarType $type = null, $name = '')

$instance = new self();
$instance->type = $type;
$instance->version = ($instance->version !== null) ? $version : TIVersion::createFromName('84+'); // default
$instance->header = [
'signature' => "**TI83F*",
'signature' => $instance->version->getSig(),
'sig_extra' => [ 0x1A, 0x0A, 0x00 ],
'comment' => str_pad("Created by tivars_lib on " . date("M j, Y"), 42, "\0"),
'entries_len' => 0 // will have to be overwritten later
Expand Down Expand Up @@ -125,19 +129,21 @@ private function makeHeaderFromFile()
$this->header['sig_extra'] = $this->get_raw_bytes(3);
$this->header['comment'] = $this->get_string_bytes(42);
$this->header['entries_len'] = $this->get_raw_bytes(1)[0] + ($this->get_raw_bytes(1)[0] << 8);
$this->version = TIVersion::createFromSignature($this->header['signature']);
}

private function makeVarEntryFromFile()
{
$fileLevel = $this->version->getLevel();
$dataSectionOffset = (8+3+42+2); // after header
fseek($this->file, $dataSectionOffset);
$this->varEntry = [];
$this->varEntry['constBytes'] = $this->get_raw_bytes(2);
$this->varEntry['data_length'] = $this->get_raw_bytes(1)[0] + ($this->get_raw_bytes(1)[0] << 8);
$this->varEntry['typeID'] = $this->get_raw_bytes(1)[0];
$this->varEntry['varname'] = $this->get_string_bytes(8);
$this->varEntry['version'] = $this->get_raw_bytes(1)[0];
$this->varEntry['archivedFlag'] = $this->get_raw_bytes(1)[0];
$this->varEntry['version'] = ($fileLevel >= TIVersions::hasVersionField) ? $this->get_raw_bytes(1)[0] : null;
$this->varEntry['archivedFlag'] = ($fileLevel >= TIVersions::hasFlash) ? $this->get_raw_bytes(1)[0] : null;
$this->varEntry['data_length2'] = $this->get_raw_bytes(1)[0] + ($this->get_raw_bytes(1)[0] << 8);
$this->varEntry['data'] = $this->get_raw_bytes($this->varEntry['data_length']);
}
Expand Down
1 change: 1 addition & 0 deletions src/TIVarTypes.php
Expand Up @@ -29,6 +29,7 @@ private static function insertType($name, $id, array $exts)
}
}

// TODO : make the extensions arrays' content for each compatibility level (see TIVersions)
public static function initTIVarTypesArray()
{
self::insertType('Unknown', -1, [ ]);
Expand Down
101 changes: 101 additions & 0 deletions src/TIVersion.php
@@ -0,0 +1,101 @@
<?php
/*
* Part of tivars_lib
* (C) 2015 Adrien 'Adriweb' Bertrand
* https://github.com/adriweb/TIs_lib
* License: MIT
*/

namespace tivars;

class TIVersion
{
private $name = 'Unknown';
private $level = 99;
private $sig = '';

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return int
*/
public function getLevel()
{
return $this->level;
}

/**
* @return string
*/
public function getSig()
{
return $this->sig;
}


/*** "Constructors" ***/

/**
* @param int $level The version compatibliity level
* @return TIVersion
* @throws \Exception
*/
public static function createFromLevel($level = -1)
{
if (TIVersions::isValidLevel($level))
{
$instance = new self();
$instance->level = $level;
$instance->sig = TIVersions::getSignatureFromLevel($level);
$instance->name = TIVersions::getNameFromLevel($level);
return $instance;
} else {
throw new \Exception("Invalid version ID");
}
}

/**
* @param string $name The version name
* @return TIVersion
* @throws \Exception
*/
public static function createFromName($name = '')
{
if (TIVersions::isValidName($name))
{
$instance = new self();
$instance->name = $name;
$instance->level = TIVersions::getLevelFromName($name);
$instance->sig = TIVersions::getSignatureFromName($name);
return $instance;
} else {
throw new \Exception("Invalid version name");
}
}

/**
* @param int $level The version compatibliity level
* @return TIVersion
* @throws \Exception
*/
public static function createFromSignature($sig = '')
{
if (TIVersions::isValidSignature($sig))
{
$instance = new self();
$instance->sig = $sig;
$instance->level = TIVersions::getMinLevelFromSignature($sig);
$instance->name = TIVersions::getDefaultNameFromSignature($sig);
return $instance;
} else {
throw new \Exception("Invalid version signature");
}
}

}
154 changes: 154 additions & 0 deletions src/TIVersions.php
@@ -0,0 +1,154 @@
<?php
/*
* Part of tivars_lib
* (C) 2015 Adrien 'Adriweb' Bertrand
* https://github.com/adriweb/tivars_lib
* License: MIT
*/

namespace tivars;

abstract class TIVersions
{
const hasVersionField = 2;
const hasFlash = 2;
const hasColor = 4;

private static $versions = [];

/**
* Make and insert the associative arrays for the version.
*
* @param int $level The [compatibility] level of this version
* @param string $name The name of the calc using this version
* @param string $sig The signature (magic bytes) used for this version
*/
private static function insertVersion($level, $name, $sig)
{
if (!isset(self::$versions[$name]))
self::$versions[$name] = [ 'level' => $level, 'sig' => $sig ];

if (!isset(self::$versions[$level]))
self::$versions[$level] = [ 'name' => $name, 'sig' => $sig ];

if (!isset(self::$versions[$sig]))
self::$versions[$sig] = [ 'level' => $level, 'name' => $name ];
}

// TODO : Research actual compatibility level/"versions" from libtifiles, and maybe even TI ?
public static function initTIVersionsArray()
{
self::insertVersion(99, 'Unknown', '');

self::insertVersion(0, '82', '**TI82**');
self::insertVersion(1, '83', '**TI83**');
self::insertVersion(2, '83+', '**TI83F*');
self::insertVersion(2, '82+', '**TI83F*');
self::insertVersion(3, '84+', '**TI83F*');
self::insertVersion(3, '82A', '**TI83F*');
self::insertVersion(4, '84+CSE', '**TI83F*');
self::insertVersion(5, '84+CE', '**TI83F*');
self::insertVersion(6, '83PCE', '**TI83F*');
}

/**
* @param int $level The version level
* @return string The version name for that Level
*/
public static function getNameFromLevel($level = 99)
{
if ($level !== 99 && isset(self::$versions[$level]))
{
return self::$versions[$level]['name'];
} else {
return 'Unknown';
}
}

/**
* @param string $name The version name
* @return int The version level for that name
*/
public static function getLevelFromName($name = '')
{
if ($name !== '' && isset(self::$versions[$name]))
{
return self::$versions[$name]['level'];
} else {
return 99;
}
}

/**
* @param int $level The version level
* @return string The signature for that Level
*/
public static function getSignatureFromLevel($level = 99)
{
if ($level !== 99 && isset(self::$versions[$level]))
{
return self::$versions[$level]['sig'];
} else {
return '';
}
}

/**
* @param string $name
* @return string The signature for that name
*/
public static function getSignatureFromName($name = '')
{
if ($name !== '' && isset(self::$versions[$name]))
{
return self::$versions[$name]['sig'];
} else {
return '';
}
}

/**
* @param string $sig
* @return string The default calc name whose file formats use that signature
*/
public static function getDefaultNameFromSignature($sig = '')
{
if ($sig !== '' && isset(self::$versions[$sig]))
{
return self::$versions[$sig]['name'];
} else {
return '';
}
}

/**
* @param string $sig The signature
* @return string The minimum compatibility level for that signaure
*/
public static function getMinLevelFromSignature($sig = '')
{
if ($sig !== '' && isset(self::$versions[$sig]))
{
return self::$versions[$sig]['level'];
} else {
return 99;
}
}

public static function isValidLevel($level = 99)
{
return ($level >= 0 && is_int($level) && isset(self::$versions[$level]));
}

public static function isValidName($name = '')
{
return ($name !== '' && isset(self::$versions[$name]));
}

public static function isValidSignature($sig = '')
{
return ($sig !== '' && isset(self::$versions[$sig]));
}
}

TIVersions::initTIVersionsArray();

0 comments on commit fc4f60f

Please sign in to comment.