Files
DCCONSTRUCTIONS f32c7b400b feat(vesc): extend motor diagnostics, configuration and transport recovery
Add firmware and extension catalogues, configuration transactions, sample review and QML diagnostics. Preserve explicit authority, bounded retries and stop recovery in motor transport.
2026-10-29 09:09:59 +03:00

86 lines
4.6 KiB
Python

"""Bounded Qt RCC and VESC package containers. Never evaluates QML or Lisp."""
import hashlib
import re
import struct
import zlib
MAX_ARCHIVE=8*1024*1024
MAX_PACKAGE=1024*1024
MAX_EXPANDED=4*1024*1024
def digest(data):return hashlib.sha256(data).hexdigest()
def uncompress(data,limit=MAX_EXPANDED):
if len(data)<5:raise ValueError('Truncated Qt compressed data')
size=int.from_bytes(data[:4],'big')
if not 0<size<=limit:raise ValueError('Expanded package exceeds bound')
decoder=zlib.decompressobj()
try:out=decoder.decompress(data[4:],size+1)
except zlib.error as error:raise ValueError('Invalid compressed package stream') from error
if len(out)!=size or not decoder.eof or decoder.unused_data or decoder.unconsumed_tail:raise ValueError('Invalid compressed package size or stream')
return out
def fields(data):
if not 5<=len(data)<=MAX_PACKAGE:raise ValueError('Package exceeds bound')
raw=uncompress(data)
if not raw.startswith(b'VESC Packet\0'):raise ValueError('Not a VESC package')
at=12;out={}
while at<len(raw):
end=raw.find(b'\0',at,at+65)
if end<at+1 or end+5>len(raw):raise ValueError('Invalid package field')
name=raw[at:end].decode('ascii');size=struct.unpack_from('>i',raw,end+1)[0];at=end+5
if name in out or size<0 or size>len(raw)-at or len(out)>=32:raise ValueError('Duplicate, truncated or excessive package field')
out[name]=raw[at:at+size];at+=size
if not out.get('name') or len(out['name'])>256:raise ValueError('Package name required')
for k in ('name','description','description_md','qmlFile','pkgDescQml'):
if k in out and b'\0' in out[k]:raise ValueError('Invalid text field')
if k in out:out[k].decode('utf-8')
if out.get('qmlIsFullscreen',b'\0') not in (b'\0',b'\1'):raise ValueError('Invalid fullscreen flag')
return out
def metadata(data,path='import.vescpkg'):
f=fields(data)
return {'id':digest(data),'path':path,'name':f['name'].decode(),'library':path.split('/')[0].startswith('lib_'),
'bytes':len(data),'lisp_bytes':len(f.get('lispData',b'')),'qml_bytes':len(f.get('qmlFile',b'')),
'description':f.get('description_md',b'').decode()[:65536], 'has_compatibility':bool(f.get('pkgDescQml'))}
def archive(data):
if not 24<=len(data)<=MAX_ARCHIVE or data[:4]!=b'qres':raise ValueError('Invalid Qt package archive')
version,tree,blobs,names,features=struct.unpack_from('>5I',data,4)
if version not in (2,3) or features!=0 or not 24<=blobs<names<tree<len(data) or (len(data)-tree)%22:raise ValueError('Unsupported RCC layout')
count=(len(data)-tree)//22
if not 2<=count<=2048:raise ValueError('RCC node bound exceeded')
seen=set();out={};total=0
def visit(index,parent,depth):
nonlocal total
if index in seen or not 0<=index<count or depth>5:raise ValueError('Invalid RCC tree')
seen.add(index);at=tree+index*22;name_offset,flags=struct.unpack_from('>IH',data,at)
if flags&~3:raise ValueError('Unsupported RCC compression')
if index==0:path=''
else:
n=names+name_offset
if not names<=n<tree-6:raise ValueError('Invalid RCC name')
size=int.from_bytes(data[n:n+2],'big')
if not 0<size<=128 or n+6+size*2>tree:raise ValueError('RCC name exceeds bound')
name=data[n+6:n+6+size*2].decode('utf-16-be')
if not re.fullmatch(r'[A-Za-z0-9_. -]{1,128}',name) or name in ('.','..'):raise ValueError('Invalid RCC path')
path=parent+'/'+name if parent else name
if flags&2:
children,first=struct.unpack_from('>II',data,at+6)
if children<1 or first+children>count:raise ValueError('Invalid RCC children')
for i in range(first,first+children):visit(i,path,depth+1)
else:
offset=int.from_bytes(data[at+10:at+14],'big');pos=blobs+offset
if not blobs<=pos<names-4:raise ValueError('Invalid RCC data pointer')
size=int.from_bytes(data[pos:pos+4],'big')
if not 0<size<=MAX_PACKAGE or pos+4+size>names:raise ValueError('Invalid RCC file size')
blob=data[pos+4:pos+4+size]
if flags&1:blob=uncompress(blob,MAX_PACKAGE)
if not path.startswith('vesc_packages/') or not path.endswith('.vescpkg') or path in out:raise ValueError('Unexpected RCC member')
total+=len(blob)
if total>MAX_ARCHIVE or len(out)>=128:raise ValueError('Catalog exceeds bound')
fields(blob);out[path.removeprefix('vesc_packages/')]=blob
visit(0,'',0)
if len(seen)!=count or not out:raise ValueError('Incomplete RCC tree')
return out