Files
metin2-client/tools/foxfs/foxfs.py
T

90 lines
4.1 KiB
Python

"""Read-only AVA2 FoxFS reader. Layout verified against bundled FoxFS.lib Archive.obj:
load, generateFilenameIndex and get. No Windows program is executed.
"""
import struct
import zlib
from pathlib import Path
MAGIC=0x000FED0C
MASK=0xFFFFFFFF
def xxh32(data,seed=MAGIC):
p1,p2,p3,p4,p5=2654435761,2246822519,3266489917,668265263,374761393
def rol(n,bits):
n &= MASK
return ((n<<bits)|(n>>(32-bits))) & MASK
def step(acc,word):return rol(acc+word*p2,13)*p1 & MASK
at,length=0,len(data)
if length>=16:
v=[(seed+p1+p2)&MASK,(seed+p2)&MASK,seed&MASK,(seed-p1)&MASK]
while at<=length-16:
v=[step(a,w) for a,w in zip(v,struct.unpack_from('<4I',data,at))];at+=16
result=sum(rol(a,b) for a,b in zip(v,(1,7,12,18))) & MASK
else:result=(seed+p5)&MASK
result=(result+length)&MASK
while at+4<=length:
result=rol(result+struct.unpack_from('<I',data,at)[0]*p3,17)*p4 & MASK;at+=4
for byte in data[at:]:result=rol(result+byte*p5,11)*p1 & MASK
result ^= result>>15;result=result*p2 & MASK
result ^= result>>13;result=result*p3 & MASK
return (result^(result>>16)) & MASK
def decompress_lz4(data,expected):
"""Bounded raw block decoder; overlapping matches retain LZ4 semantics."""
output=bytearray();at=0
def length(value):
nonlocal at
if value==15:
while True:
if at>=len(data):raise ValueError('truncated LZ4 length')
byte=data[at];at+=1;value+=byte
if byte!=255:break
return value
while at<len(data):
token=data[at];at+=1;literals=length(token>>4)
if at+literals>len(data) or len(output)+literals>expected:raise ValueError('invalid LZ4 literals')
output+=data[at:at+literals];at+=literals
if at==len(data):break
if at+2>len(data):raise ValueError('truncated LZ4 match')
distance=data[at]|data[at+1]<<8;at+=2;match=length(token&15)+4
if not 0<distance<=len(output) or len(output)+match>expected:raise ValueError('invalid LZ4 match')
for _ in range(match):output.append(output[-distance])
if len(output)!=expected:raise ValueError('LZ4 decoded size mismatch')
return bytes(output)
def filename_crc(name):return zlib.crc32(name.lower().replace('\\','/').encode('latin1'))
class Archive:
def __init__(self,path):
self.path=Path(path);self.rows={}
with self.path.open('rb') as source:
header=source.read(68)
if len(header)!=68 or struct.unpack_from('<I',header)[0]!=MAGIC:
raise ValueError('not an AVA2 FoxFS archive: '+str(path))
self.key,self.iv=header[4:36],header[36:52]
at,size=68,self.path.stat().st_size
while at<size:
source.seek(at);raw=source.read(20)
if len(raw)!=20:raise ValueError('truncated FoxFS entry')
row=struct.unpack('<5I',raw);original,checksum,offset,stored,crc=row
if offset!=at+20 or offset+stored>size:raise ValueError('invalid FoxFS entry extent')
# Original load() retains the earliest entry on a CRC collision.
self.rows.setdefault(crc,row);at=offset+stored
def get(self,name,max_bytes=64*1024*1024):return self.get_crc(filename_crc(name),max_bytes)
def get_crc(self,crc,max_bytes=64*1024*1024):
row=self.rows.get(crc)
if row is None:return None
original,checksum,offset,stored,_=row
if max(original,stored)>max_bytes:raise ValueError('FoxFS resource exceeds requested size limit')
from cryptography.hazmat.primitives.ciphers import Cipher,algorithms,modes
with self.path.open('rb') as source:
source.seek(offset);encrypted=source.read(stored)
if len(encrypted)!=stored:raise ValueError('FoxFS archive changed during read')
decoder=Cipher(algorithms.AES(self.key),modes.CFB(self.iv)).decryptor()
raw=decoder.update(encrypted)+decoder.finalize()
if original!=stored:raw=decompress_lz4(raw,original)
if xxh32(raw)!=checksum:raise ValueError('FoxFS content checksum mismatch: '+str(self.path))
return raw