Buckets:
| """Repair a demonstrably invalid base64 decoder regex; preserve the pre-patch bytes.""" | |
| import os,json,re,hashlib,tempfile | |
| from pathlib import Path | |
| from huggingface_hub import HfApi | |
| api=HfApi(token=os.environ['HF_TOKEN']);bucket='smodusermc/offline-games' | |
| key='site/games/basketballstars/assets/main.min.js';root=Path('/home/user/game-archive/reports') | |
| with tempfile.TemporaryDirectory(prefix='game-js-fix-') as tmp: | |
| p=Path(tmp)/'main.js';api.download_bucket_files(bucket,[(key,p)],raise_on_missing_files=True);old=p.read_bytes();text=old.decode('utf-8') | |
| pattern=r're_btou=new RegExp\(\[.*?\]\.join\("\|"\),"g"\)' | |
| replacement=r're_btou=/[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g' | |
| new,n=re.subn(pattern,lambda _:replacement,text,count=1);assert n==1,'Expected invalid regex initializer not found' | |
| patched=new.encode('utf-8');newfile=Path(tmp)/'patched.js';newfile.write_bytes(patched) | |
| api.batch_bucket_files(bucket,add=[(p,'_originals/'+key),(newfile,key)]) | |
| info=list(api.get_bucket_paths_info(bucket,[key]))[0];assert info.size==len(patched) | |
| report={'game_id':56,'path':key,'backup':'_originals/'+key,'reason':'Source JS contains a mojibake-corrupted range in its UTF-8/base64 decoder and throws Invalid regular expression before the game loads. Replaced that one regex initializer with explicit byte-range escapes.','original_sha256':hashlib.sha256(old).hexdigest(),'patched_sha256':hashlib.sha256(patched).hexdigest(),'original_bytes':len(old),'patched_bytes':len(patched),'runtime_retest':'pending'} | |
| m=json.loads((root/'game-56.json').read_text()) | |
| for f in m['files']: | |
| if f['path']==key:f.update(bytes=len(patched),sha256=report['patched_sha256'],modification=report['reason'],original_sha256=report['original_sha256']) | |
| (root/'game-56.json').write_text(json.dumps(m,indent=2));(root/'basketball-encoding-repair.json').write_text(json.dumps(report,indent=2)) | |
| api.batch_bucket_files(bucket,add=[(root/'game-56.json','_reports/games/0056.json'),(root/'basketball-encoding-repair.json','_verification/basketball-encoding-repair.json')]) | |
| print('Patched and backed up',key) | |
Xet Storage Details
- Size:
- 2.12 kB
- Xet hash:
- 35d8a52eae175b92f110646013f09283cb2816756464a381ff6b56028c5b6066
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.