367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
28 lines
1.4 KiB
Python
28 lines
1.4 KiB
Python
"""Bounded classic query-cache framing shared by item and object readers."""
|
|
from pathlib import Path
|
|
import struct
|
|
|
|
|
|
def read_classic_query_cache(path, *, signature, min_payload=1):
|
|
path=Path(path)
|
|
if type(signature)is not bytes or len(signature)!=4 or not 1<=min_payload<=65536:
|
|
raise ValueError('Require explicit reviewed classic query-cache grammar')
|
|
if path.is_symlink() or not path.is_file() or not 20<=path.stat().st_size<=16777216:
|
|
raise ValueError('Require bounded regular classic query cache')
|
|
raw=path.read_bytes()
|
|
if len(raw)>16777216 or raw[:4]!=signature or struct.unpack_from('<I',raw,4)[0]!=5875:
|
|
raise ValueError('Unreviewed classic query-cache header/build')
|
|
pos=20; rows=[]; seen=set()
|
|
while pos<len(raw):
|
|
if len(raw)-pos in (4,8) and not any(raw[pos:]):
|
|
pos=len(raw); break
|
|
if pos+8>len(raw):
|
|
raise ValueError('Truncated classic query-cache envelope')
|
|
ident,size=struct.unpack_from('<II',raw,pos);pos+=8
|
|
if not 0<ident<0x80000000 or ident in seen or not min_payload<=size<=65536 or pos+size>len(raw):
|
|
raise ValueError('Invalid classic query-cache record')
|
|
rows.append((ident,raw[pos:pos+size]));seen.add(ident);pos+=size
|
|
if pos!=len(raw):
|
|
raise ValueError('Unexpected trailing classic query-cache bytes')
|
|
return raw,rows
|