Added checksum

This commit is contained in:
Netkas 2023-09-29 23:42:56 -04:00
parent e1f813ab60
commit 976518439f
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
7 changed files with 104 additions and 2 deletions

View file

@ -52,6 +52,19 @@
return 1;
}
if(isset($args['info']))
{
try
{
return self::displayInfo($args);
}
catch(Exception $e)
{
Console::outException(sprintf('Failed to display info: %s', $e->getMessage()), $e, 1);
return 1;
}
}
if(isset($args['headers']))
{
try
@ -149,6 +162,31 @@
return 0;
}
/**
* Prints out basic information about the package
*
* @param array $args
* @return int
*/
private static function displayInfo(array $args): int
{
$package_reader = new PackageReader($args['path'] ?? $args['p']);
Console::out(sprintf('File Version: %s', $package_reader->getFileVersion()));
Console::out(sprintf('crc32: %s', $package_reader->getChecksum()));
Console::out(sprintf('sha256: %s', $package_reader->getChecksum('sha256')));
Console::out(sprintf('Flags: %s', implode(', ', $package_reader->getFlags())));
Console::out(sprintf('Package Offset: %s', $package_reader->getPackageOffset()));
Console::out(sprintf('Package Length: %s', $package_reader->getPackageLength()));
Console::out(sprintf('Header Offset: %s', $package_reader->getHeaderOffset()));
Console::out(sprintf('Header Length: %s', $package_reader->getHeaderLength()));
Console::out(sprintf('Data Offset: %s', $package_reader->getDataOffset()));
Console::out(sprintf('Data Length: %s', $package_reader->getDataLength()));
Console::out(sprintf('Directory Size: %d items', count($package_reader->getDirectory())));
return 0;
}
/**
* Displays the assembly section
*
@ -311,6 +349,7 @@
new CliHelpSection(['--path', '-p'], 'Required. Specifies the path to the binary package to inspect'),
new CliHelpSection(['--json'], 'Prints out the information of the package in JSON format'),
new CliHelpSection(['--pretty-json'], 'Prints out the information of the package in pretty JSON format'),
new CliHelpSection(['info'], 'Prints out basic information about the binary package'),
new CliHelpSection(['headers'], 'Prints out the headers of the package'),
new CliHelpSection(['metadata'], 'Prints out the metadata of the package'),
new CliHelpSection(['assembly'], 'Prints out the assembly information of the package'),

View file

@ -37,6 +37,7 @@
use ncc\Exceptions\NotSupportedException;
use ncc\Exceptions\PathNotFoundException;
use ncc\Interfaces\CompilerInterface;
use ncc\Managers\PackageManager;
use ncc\Managers\ProjectManager;
use ncc\Objects\Package\Component;
use ncc\Objects\Package\Metadata;
@ -192,9 +193,18 @@
$this->processResource($package_writer, $resource);
}
$package_manager = new PackageManager();
// Add the project dependencies
foreach($this->project_manager->getProjectConfiguration()->getBuild()->getDependencies() as $dependency)
{
if(isset($configuration->getOptions()[BuildConfigurationOptions::STATIC_DEPENDENCIES]))
{
$package_entry = $package_manager->getPackageLock()->getEntry($dependency->getName());
$shadow_package = $package_entry->getShadowPackagePath($dependency->getVersion());
// TODO: Add support for static dependencies
}
$package_writer->addDependencyConfiguration($dependency);
}

View file

@ -654,6 +654,36 @@
return $this->data_length;
}
/**
* Returns the checksum of the package
*
* @param string $hash
* @param bool $binary
* @return string
*/
public function getChecksum(string $hash='crc32b', bool $binary=false): string
{
$checksum = hash($hash, '', $binary);
fseek($this->package_file, $this->package_offset);
$bytes_left = $this->package_length;
while ($bytes_left > 0)
{
$buffer = fread($this->package_file, min(1024, $bytes_left));
$buffer_length = strlen($buffer);
$bytes_left -= $buffer_length;
$checksum = hash($hash, ($checksum . $buffer), $binary);
if ($buffer_length === 0)
{
break;
}
}
return $checksum;
}
/**
* @param string $path
* @return void

View file

@ -31,4 +31,6 @@
public const NCC_CONFIGURATION = 'ncc_configuration';
public const OUTPUT_FILE = 'output_file';
public const STATIC_DEPENDENCIES = 'static';
}

View file

@ -518,8 +518,6 @@
$package_reader->getComponent($component_name)->getData([ComponentDecodeOptions::AS_FILE]), 0755
);
Console::inlineProgressBar(++$current_step, $total_steps);
}

View file

@ -101,6 +101,17 @@
return $this->getVersion($version)->getPath($this->name);
}
/**
* Returns the path to where the shadow package is located
*
* @param string $version
* @return string
*/
public function getShadowPackagePath(string $version): string
{
return $this->getVersion($version)->getShadowPackagePath($this->name);
}
/**
* Adds a new version entry to the package, if overwrite is true then
* the entry will be overwritten if it exists, otherwise it will return

View file

@ -25,6 +25,7 @@
namespace ncc\Objects\PackageLock;
use InvalidArgumentException;
use ncc\Enums\FileDescriptor;
use ncc\Exceptions\ConfigurationException;
use ncc\Interfaces\BytecodeObjectInterface;
use ncc\Objects\ProjectConfiguration\Dependency;
@ -222,6 +223,17 @@
return PathFinder::getPackagesPath() . DIRECTORY_SEPARATOR . sprintf('%s=%s', $package_name, $this->getVersion());
}
/**
* Returns the path where the shadow package is located
*
* @param string $package_name
* @return string
*/
public function getShadowPackagePath(string $package_name): string
{
return $this->getPath($package_name) . DIRECTORY_SEPARATOR . FileDescriptor::SHADOW_PACKAGE;
}
/**
* Returns an array representation of the object
*