research: verify native preset stream and prepare bounded trajectory reader
这个提交包含在:
1 父节点
2d162a8fc5
当前提交
606544820f
4 个文件已更改
+266
没有与搜索条件相匹配的文件
@@ -0,0 +1,208 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Read a native CF collection stream as data, without loading vendor classes.
|
||||
|
||||
Accepts the version 1/2/5 collection/preset/version payload established from the
|
||||
installed ColorFinaleUtils framework. The surrounding UI export file envelope
|
||||
is not yet established by a real fixture. Supply an extracted raw payload only;
|
||||
this reader never scans for headers or guesses offsets inside other files.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import plistlib
|
||||
import sys
|
||||
|
||||
from read_colorfinale_preset import MAX_BYTES, decode_archive, geometry, identifier, inflate
|
||||
|
||||
|
||||
class Stream:
|
||||
def __init__(self, data):
|
||||
if len(data) > MAX_BYTES:
|
||||
raise ValueError('Native payload exceeds input limit')
|
||||
self.data, self.offset = data, 0
|
||||
|
||||
def take(self, count):
|
||||
if count < 0 or count > len(self.data) - self.offset:
|
||||
raise ValueError('Truncated native stream at byte ' + str(self.offset))
|
||||
value = self.data[self.offset:self.offset + count]
|
||||
self.offset += count
|
||||
return value
|
||||
|
||||
def integer(self):
|
||||
first = self.take(1)[0]
|
||||
if first == 0:
|
||||
return int.from_bytes(self.take(8), 'little')
|
||||
extra = (first & -first).bit_length() - 1
|
||||
encoded = bytes([first]) + self.take(extra)
|
||||
return int.from_bytes(encoded, 'little') >> (extra + 1)
|
||||
|
||||
def count(self, maximum):
|
||||
value = self.integer()
|
||||
if value > maximum:
|
||||
raise ValueError('Native count exceeds limit')
|
||||
return value
|
||||
|
||||
def binary(self, maximum=64 * 1024 * 1024):
|
||||
return self.take(self.count(maximum))
|
||||
|
||||
def text(self):
|
||||
return self.binary(1024 * 1024).decode('utf-8', errors='strict')
|
||||
|
||||
def flag(self):
|
||||
value = self.integer()
|
||||
if value not in (0, 1):
|
||||
raise ValueError('Unsupported native flag')
|
||||
return bool(value)
|
||||
|
||||
def storage_data(self):
|
||||
if not self.flag():
|
||||
return None, None
|
||||
if not self.flag():
|
||||
return None, self.take(32).hex()
|
||||
return self.binary(), None
|
||||
|
||||
|
||||
def read_collection(data):
|
||||
stream = Stream(data)
|
||||
if stream.integer() != 1:
|
||||
raise ValueError('Expected native collection version 1')
|
||||
collection = {'name': stream.text(), 'id': stream.text(),
|
||||
'flags': stream.take(1)[0]}
|
||||
versions = []
|
||||
total_versions = 0
|
||||
archive_bytes = 0
|
||||
for _ in range(stream.count(1000)):
|
||||
if stream.integer() != 2:
|
||||
raise ValueError('Expected native preset version 2')
|
||||
preset = {'name': stream.text(), 'id': stream.text()}
|
||||
tags = [stream.text() for _ in range(stream.count(10000))]
|
||||
for _ in range(stream.count(1000)):
|
||||
total_versions += 1
|
||||
if total_versions > 10000:
|
||||
raise ValueError('Too many native versions')
|
||||
if stream.integer() != 5:
|
||||
raise ValueError('Expected native preset-version format 5')
|
||||
name, identifier, color = stream.text(), stream.text(), stream.text()
|
||||
active = stream.flag()
|
||||
stack, stack_reference = stream.storage_data()
|
||||
masks, masks_reference = stream.storage_data()
|
||||
if masks_reference is not None:
|
||||
raise ValueError('Masks are an external SHA-256 reference; '
|
||||
'an inline-data payload is required')
|
||||
if not masks:
|
||||
continue
|
||||
archive = inflate(masks)
|
||||
archive_bytes += len(archive)
|
||||
if archive_bytes > MAX_BYTES:
|
||||
raise ValueError('Combined native archive expansion exceeds limit')
|
||||
scene = decode_archive(archive)
|
||||
if not isinstance(scene, dict):
|
||||
raise ValueError('Expected native tracker-scene dictionary')
|
||||
versions.append({'index': len(versions), 'name': name, 'id': identifier,
|
||||
'preset': preset, 'tags': tags, 'active': active,
|
||||
'colorSpace': color,
|
||||
'stackInline': stack is not None,
|
||||
'stackReference': stack_reference,
|
||||
'archiveSHA256': hashlib.sha256(archive).hexdigest(),
|
||||
'sceneID': identifier(scene.get('identifier')),
|
||||
'aspectRatio': scene.get('aspectRatio'),
|
||||
'masks': geometry(scene)})
|
||||
if stream.offset != len(data):
|
||||
raise ValueError('Trailing bytes after native collection payload')
|
||||
if not versions:
|
||||
raise ValueError('Native collection contains no inline masks archive')
|
||||
return {'collection': collection, 'versions': versions,
|
||||
'maskCount': sum(len(v['masks']) for v in versions),
|
||||
'keyCount': sum(len(m[p]['keys']) for v in versions for m in v['masks']
|
||||
for p in ('position', 'rotation', 'scale')),
|
||||
'reader': 'bounded native stream + plistlib; no vendor class loading',
|
||||
'coordinateNote': 'Raw authored coordinates and radians; no implicit flip.',
|
||||
'scope': 'Raw collection payload only. UI file envelope and real exported '
|
||||
'fixture remain unverified.'}
|
||||
|
||||
|
||||
def self_test():
|
||||
# Fixed prefix examples derived from the observed byte format, including
|
||||
# both encoding branches; these are not outputs of the fixture writer.
|
||||
examples = [(b'\x01', 0), (b'\x03', 1), (b'\xff', 127),
|
||||
(b'\x02\x02', 128), (b'\xfe\xff', 16383),
|
||||
(b'\x04\x00\x02', 16384),
|
||||
(b'\x80' + b'\xff' * 7, (1 << 56) - 1),
|
||||
(b'\x00' + (1 << 56).to_bytes(8, 'little'), 1 << 56),
|
||||
(b'\x00' + b'\xff' * 8, (1 << 64) - 1)]
|
||||
for encoded, expected in examples:
|
||||
s = Stream(encoded)
|
||||
assert s.integer() == expected and s.offset == len(encoded)
|
||||
|
||||
def integer(value):
|
||||
if value >= 1 << 56:
|
||||
return b'\0' + value.to_bytes(8, 'little')
|
||||
size = max(1, (value.bit_length() + 6) // 7)
|
||||
return ((value << size) | (1 << (size - 1))).to_bytes(size, 'little')
|
||||
|
||||
def binary(value):
|
||||
return integer(len(value)) + value
|
||||
|
||||
def text(value):
|
||||
return binary(value.encode())
|
||||
|
||||
archive = plistlib.dumps({'$archiver': 'NSKeyedArchiver', '$version': 100000,
|
||||
'$objects': ['$null', {'nodes': [
|
||||
{'position': {'value': [.25, .75], 'keyFrames': []},
|
||||
'rotation': {'value': 0, 'keyFrames': []},
|
||||
'scale': {'value': [1, 1], 'keyFrames': []}}]}],
|
||||
'$top': {'root': plistlib.UID(1)}}, fmt=plistlib.FMT_BINARY)
|
||||
prefix = (integer(1) + text('QA') + text('collection-id') + b'\0' + integer(1)
|
||||
+ integer(2) + text('Known motion') + text('preset-id') + integer(0)
|
||||
+ integer(1) + integer(5) + text('Version 1') + text('version-id')
|
||||
+ text('Rec709') + integer(1) + integer(0))
|
||||
payload = prefix + integer(1) + integer(1) + binary(archive)
|
||||
result = read_collection(payload)
|
||||
assert result['maskCount'] == 1 and result['keyCount'] == 0
|
||||
assert result['versions'][0]['masks'][0]['position']['staticValue'] == [.25, .75]
|
||||
invalid = [payload[:-1], payload + b'x', b'\x05' + payload[1:],
|
||||
prefix + integer(1) + integer(0) + bytes(32)]
|
||||
for data in invalid:
|
||||
try:
|
||||
read_collection(data)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError('Invalid native payload was accepted')
|
||||
print(json.dumps({'pass': True, 'checks': ['nine-fixed-variable-prefix-values',
|
||||
'native-1-2-5-inline-mask-generated-fixture',
|
||||
'reject-truncation-trailing-version-and-external-reference'],
|
||||
'scope': 'Generated data only; no original exported fixture used.'}))
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('input', type=Path, nargs='?')
|
||||
parser.add_argument('--output', type=Path)
|
||||
parser.add_argument('--self-test', action='store_true')
|
||||
args = parser.parse_args()
|
||||
if args.self_test:
|
||||
self_test()
|
||||
return
|
||||
if not args.input:
|
||||
parser.error('an explicitly extracted raw native collection payload is required')
|
||||
if args.input.stat().st_size > MAX_BYTES:
|
||||
raise ValueError('Native payload exceeds input limit')
|
||||
data = args.input.read_bytes()
|
||||
result = read_collection(data)
|
||||
result.update(input=str(args.input.resolve()), inputSHA256=hashlib.sha256(data).hexdigest())
|
||||
output = json.dumps(result, ensure_ascii=False, indent=2, allow_nan=False) + '\n'
|
||||
if args.output:
|
||||
args.output.write_text(output)
|
||||
else:
|
||||
sys.stdout.write(output)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except (ValueError, TypeError, KeyError, IndexError, OSError,
|
||||
plistlib.InvalidFileException) as error:
|
||||
print('Cannot extract verified native tracks: ' + str(error), file=sys.stderr)
|
||||
sys.exit(1)
|
||||
在新工单中引用
屏蔽一个用户