Skip to content

Commit

Permalink
Dev 240211 (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondrake authored Feb 11, 2024
1 parent 7312137 commit 25e5a52
Show file tree
Hide file tree
Showing 27 changed files with 260 additions and 171 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
fail-fast: false
matrix:
php-version:
# - "8.1"
- "8.1"
- "8.2"
# - "8.3"
- "8.3"

steps:
- name: Install PHP
Expand Down
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
"symfony/var-dumper": "^6 | ^7",
"symfony/yaml": "^6 | ^7",
"bramus/monolog-colored-line-formatter": "^3",
"phpstan/phpstan": "^1.10"
"phpstan/phpstan": "^1.10",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1"
},
"autoload": {
"psr-4": {
Expand All @@ -41,5 +43,10 @@
"FileEye\\MediaProbe\\Test\\": "tests/"
}
},
"bin": ["bin/fileeye-mediaprobe"]
"bin": ["bin/fileeye-mediaprobe"],
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}
2 changes: 2 additions & 0 deletions specs/Jpeg/Exif.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
collection: Jpeg\Exif
title: 'JPEG Exif data'
class: FileEye\MediaProbe\Block\Jpeg\Exif
parser: FileEye\MediaProbe\Parser\Jpeg\Exif
writer: FileEye\MediaProbe\Writer\Jpeg\Exif
DOMNode: exif
items:
Tiff:
Expand Down
2 changes: 2 additions & 0 deletions specs/Jpeg/Segment.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
collection: Jpeg\Segment
title: Generic JPEG data segment
class: FileEye\MediaProbe\Block\Jpeg\Segment
parser: FileEye\MediaProbe\Parser\Jpeg\Segment
writer: FileEye\MediaProbe\Writer\Jpeg\Segment
DOMNode: jpegSegment
items: {}
2 changes: 2 additions & 0 deletions specs/Jpeg/SegmentApp1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name: APP1
title: 'JPEG Application segment 1'
payload: variable
class: FileEye\MediaProbe\Block\Jpeg\SegmentApp1
parser: FileEye\MediaProbe\Parser\Jpeg\SegmentApp1
writer: FileEye\MediaProbe\Writer\Jpeg\SegmentApp1
DOMNode: jpegSegment
items:
Exif:
Expand Down
2 changes: 2 additions & 0 deletions specs/Jpeg/SegmentCom.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ name: COM
title: 'JPEG Comment'
payload: variable
class: FileEye\MediaProbe\Block\Jpeg\SegmentCom
parser: FileEye\MediaProbe\Parser\Jpeg\SegmentCom
writer: FileEye\MediaProbe\Writer\Jpeg\SegmentCom
DOMNode: jpegSegment
items: {}
2 changes: 2 additions & 0 deletions specs/Jpeg/SegmentSos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ name: SOS
title: 'JPEG Start of scan'
payload: scan
class: FileEye\MediaProbe\Block\Jpeg\SegmentSos
parser: FileEye\MediaProbe\Parser\Jpeg\SegmentSos
writer: FileEye\MediaProbe\Writer\Jpeg\SegmentSos
DOMNode: jpegSegment
items: {}
16 changes: 9 additions & 7 deletions src/Block/Exif/Vendor/Canon/FilterInfoIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ protected function doParseData(DataElement $data): void
$filter_size = $data->getLong($offset + 4);
$this
->addBlock(
new ItemDefinition(CollectionFactory::get('ExifMakerNotes\Canon\Filter'),
DataFormat::BYTE,
$filter_size,
$offset,
0,
$i
))
new ItemDefinition(
CollectionFactory::get('ExifMakerNotes\Canon\Filter'),
DataFormat::BYTE,
$filter_size,
$offset,
0,
$i
)
)
->parseData(new DataWindow($data, $offset, $filter_size + 4));
$offset += 4 + $filter_size;
}
Expand Down
46 changes: 0 additions & 46 deletions src/Block/Jpeg/Exif.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
namespace FileEye\MediaProbe\Block\Jpeg;

use FileEye\MediaProbe\Model\BlockBase;
use FileEye\MediaProbe\Collection\CollectionFactory;
use FileEye\MediaProbe\Data\DataElement;
use FileEye\MediaProbe\Data\DataWindow;
use FileEye\MediaProbe\Entry\Core\Undefined;
use FileEye\MediaProbe\ItemDefinition;
use FileEye\MediaProbe\MediaProbe;
use FileEye\MediaProbe\Utility\ConvertBytes;

/**
Expand All @@ -27,51 +21,11 @@ class Exif extends BlockBase
// @todo xxx the trailing bytes may not be zeros
const EXIF_HEADER = "Exif\0\0";

/**
* {@inheritdoc}
*/
protected function doParseData(DataElement $data): void
{
assert($this->debugInfo(['dataElement' => $data]));

$tiff = new ItemDefinition(
collection: CollectionFactory::get('Tiff\Tiff'),
);
$tiffParser = $tiff->collection->getPropertyValue('parser');

if ($tiffParser::getTiffSegmentByteOrder($data, strlen(self::EXIF_HEADER)) !== null) {
$this->addBlock($tiff)->parseData($data, strlen(self::EXIF_HEADER), $data->getSize() - strlen(self::EXIF_HEADER));
} else {
// We store the data as normal JPEG content if it could not be
// parsed as Tiff data.
$entry = new Undefined($this, [$data->getBytes()]);
$this->error("TIFF header not found. Parsed {text}", ['text' => $entry->toString()]);
}
}

/**
* {@inheritdoc}
*/
public function toBytes(int $byte_order = ConvertBytes::LITTLE_ENDIAN, int $offset = 0): string
{
return self::EXIF_HEADER . $this->getElement('*')->toBytes();
}

/**
* Determines if the data is an EXIF segment.
*/
public static function isExifSegment(DataElement $dataElement, $offset = 0): bool
{
// There must be at least 6 bytes for the Exif header.
if ($dataElement->getSize() - $offset < strlen(self::EXIF_HEADER)) {
return false;
}

// Verify the Exif header.
if ($dataElement->getBytes($offset, strlen(self::EXIF_HEADER)) === self::EXIF_HEADER) {
return true;
}

return false;
}
}
12 changes: 0 additions & 12 deletions src/Block/Jpeg/Segment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,11 @@

namespace FileEye\MediaProbe\Block\Jpeg;

use FileEye\MediaProbe\Data\DataElement;
use FileEye\MediaProbe\Entry\Core\Undefined;

/**
* Class representing a generic JPEG data segment.
*
* This is the default segment processor in case no specific class are defined.
*/
class Segment extends SegmentBase
{
/**
* {@inheritdoc}
*/
protected function doParseData(DataElement $data): void
{
assert($this->debugInfo(['dataElement' => $data]));
// Adds the segment data as an Undefined entry.
new Undefined($this, $data);
}
}
23 changes: 0 additions & 23 deletions src/Block/Jpeg/SegmentApp1.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,13 @@

namespace FileEye\MediaProbe\Block\Jpeg;

use FileEye\MediaProbe\Block\Jpeg\Exif;
use FileEye\MediaProbe\Collection\CollectionFactory;
use FileEye\MediaProbe\Data\DataElement;
use FileEye\MediaProbe\Entry\Core\Undefined;
use FileEye\MediaProbe\ItemDefinition;
use FileEye\MediaProbe\Utility\ConvertBytes;

/**
* Class representing a JPEG APP1 segment.
*/
class SegmentApp1 extends SegmentBase
{
/**
* {@inheritdoc}
*/
protected function doParseData(DataElement $data): void
{
assert($this->debugInfo(['dataElement' => $data]));
// If we have an Exif table, parse it.
if (Exif::isExifSegment($data, 4)) {
$exif = new ItemDefinition(CollectionFactory::get('Jpeg\Exif'));
$this->addBlock($exif)->parseData($data, 4, $data->getSize() - 4);
} else {
// We store the data as normal JPEG content if it could not be
// parsed as Exif data.
$entry = new Undefined($this, $data);
$entry->debug("Not an Exif segment. Parsed {text}", ['text' => $entry->toString()]);
}
}

/**
* {@inheritdoc}
*/
Expand Down
13 changes: 0 additions & 13 deletions src/Block/Jpeg/SegmentCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@

namespace FileEye\MediaProbe\Block\Jpeg;

use FileEye\MediaProbe\Data\DataElement;
use FileEye\MediaProbe\Data\DataWindow;
use FileEye\MediaProbe\Entry\Core\Char;
use FileEye\MediaProbe\Utility\ConvertBytes;

/**
* Class representing a JPEG comment segment.
*/
class SegmentCom extends SegmentBase
{
/**
* {@inheritdoc}
*/
protected function doParseData(DataElement $data): void
{
assert($this->debugInfo(['dataElement' => $data]));
// Adds the segment data as a Char string.
new Char($this, new DataWindow($data, 4));
}

/**
* {@inheritdoc}
*/
Expand Down
49 changes: 0 additions & 49 deletions src/Block/Jpeg/SegmentSos.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

namespace FileEye\MediaProbe\Block\Jpeg;

use FileEye\MediaProbe\Block\RawData;
use FileEye\MediaProbe\Collection\CollectionFactory;
use FileEye\MediaProbe\Data\DataElement;
use FileEye\MediaProbe\Data\DataWindow;
use FileEye\MediaProbe\Entry\Core\Undefined;
use FileEye\MediaProbe\ItemDefinition;
use FileEye\MediaProbe\Data\DataFormat;

/**
* Class representing a JPEG SOS segment.
*/
Expand All @@ -19,45 +11,4 @@ class SegmentSos extends SegmentBase
* JPEG EOI marker.
*/
const JPEG_EOI = 0xD9;

/**
* {@inheritdoc}
*/
protected function doParseData(DataElement $data): void
{
assert($this->debugInfo(['dataElement' => $data]));
// This segment is last before End Of Image, and its length needs to be
// determined by finding the EOI marker backwards from the end of data.
// Some images have some trailing (garbage?) following the EOI marker,
// which we store in a RawData object.
$scan_size = $data->getSize();
while ($data->getByte($scan_size - 2) !== Jpeg::JPEG_DELIMITER || $data->getByte($scan_size - 1) != self::JPEG_EOI) {
$scan_size --;
}
$scan_size -= 2;

// Load data in an Undefined entry.
$data_window = new DataWindow($data, 0, $scan_size);
new Undefined($this, $data_window);

// Append the EOI.
$end_offset = $scan_size;
$eoi = new ItemDefinition(
$this->getParentElement()->getCollection()->getItemCollection(self::JPEG_EOI)
);
$this->getParentElement()->addBlock($eoi)->parseData($data, $end_offset, 2);
$end_offset += 2;

// Now check to see if there are any trailing data.
if ($end_offset < $data->getSize()) {
$raw_size = $data->getSize() - $end_offset;
$this->warning('Found trailing content after EOI: {size} bytes', ['size' => $raw_size]);
// There is no JPEG marker for trailing garbage, so we just collect
// the data in a RawData object.
$trail_definition = new ItemDefinition(CollectionFactory::get('RawData'), DataFormat::BYTE, $raw_size);
$trail_data_window = new DataWindow($data, $end_offset, $raw_size);
$trail = new RawData($trail_definition, $this->getParentElement());
$trail->parseData($trail_data_window);
}
}
}
2 changes: 2 additions & 0 deletions src/Collection/Jpeg/Exif.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Exif extends CollectionBase {
protected static $map = array (
'title' => 'JPEG Exif data',
'class' => 'FileEye\\MediaProbe\\Block\\Jpeg\\Exif',
'parser' => 'FileEye\\MediaProbe\\Parser\\Jpeg\\Exif',
'writer' => 'FileEye\\MediaProbe\\Writer\\Jpeg\\Exif',
'DOMNode' => 'exif',
'id' => 'Jpeg\\Exif',
'items' =>
Expand Down
2 changes: 2 additions & 0 deletions src/Collection/Jpeg/Segment.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Segment extends CollectionBase {
protected static $map = array (
'title' => 'Generic JPEG data segment',
'class' => 'FileEye\\MediaProbe\\Block\\Jpeg\\Segment',
'parser' => 'FileEye\\MediaProbe\\Parser\\Jpeg\\Segment',
'writer' => 'FileEye\\MediaProbe\\Writer\\Jpeg\\Segment',
'DOMNode' => 'jpegSegment',
'id' => 'Jpeg\\Segment',
);
Expand Down
2 changes: 2 additions & 0 deletions src/Collection/Jpeg/SegmentApp1.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class SegmentApp1 extends CollectionBase {
'title' => 'JPEG Application segment 1',
'payload' => 'variable',
'class' => 'FileEye\\MediaProbe\\Block\\Jpeg\\SegmentApp1',
'parser' => 'FileEye\\MediaProbe\\Parser\\Jpeg\\SegmentApp1',
'writer' => 'FileEye\\MediaProbe\\Writer\\Jpeg\\SegmentApp1',
'DOMNode' => 'jpegSegment',
'id' => 'Jpeg\\SegmentApp1',
'items' =>
Expand Down
2 changes: 2 additions & 0 deletions src/Collection/Jpeg/SegmentCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class SegmentCom extends CollectionBase {
'title' => 'JPEG Comment',
'payload' => 'variable',
'class' => 'FileEye\\MediaProbe\\Block\\Jpeg\\SegmentCom',
'parser' => 'FileEye\\MediaProbe\\Parser\\Jpeg\\SegmentCom',
'writer' => 'FileEye\\MediaProbe\\Writer\\Jpeg\\SegmentCom',
'DOMNode' => 'jpegSegment',
'id' => 'Jpeg\\SegmentCom',
);
Expand Down
2 changes: 2 additions & 0 deletions src/Collection/Jpeg/SegmentSos.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class SegmentSos extends CollectionBase {
'title' => 'JPEG Start of scan',
'payload' => 'scan',
'class' => 'FileEye\\MediaProbe\\Block\\Jpeg\\SegmentSos',
'parser' => 'FileEye\\MediaProbe\\Parser\\Jpeg\\SegmentSos',
'writer' => 'FileEye\\MediaProbe\\Writer\\Jpeg\\SegmentSos',
'DOMNode' => 'jpegSegment',
'id' => 'Jpeg\\SegmentSos',
);
Expand Down
Loading

0 comments on commit 25e5a52

Please sign in to comment.