Files
mars_nwe_pcab/upstream-adapter/add-nds.py

33 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Fail closed if the upstream dispatcher layout is different."""
import pathlib,re,sys
p=pathlib.Path(sys.argv[1])/'src/nwconn.c'
s=p.read_text(encoding="latin-1")
anchor=r'if\s*\(ncp_type\s*==\s*0x2222\)\s*\{\s*switch\s*\(function\)\s*\{'
if len(re.findall(anchor,s))!=1 or not re.search(r'responsedata\s*=\s*\(\(uint8\s*\*\)\s*&ipxdata\)\s*\+\s*sizeof\(NCPRESPONSE\)',s) or not re.search(r'requestlen\s*=\s*data_len\s*-\s*sizeof\(NCPREQUEST\)',s) or 'mars_nds_request' in s:
raise SystemExit('Unsupported nwconn layout: NDS hook not installed')
start=re.search(anchor,s).end()
stub = '''case 0x68 : /* NDS NCP, NDS Fragger Protokoll ?? */
XDPRINTF((2,0, "NDS Fragger Protokoll not supportet"));
nw_debug=0;
completition = 0xfb; /* unknown request */
break;'''
known_stub = s.count(stub) == 1
if not known_stub and re.search(r'case\s+(?:0x68|104)\s*:',s[start:],re.I):
raise SystemExit('Existing NCP 104 handler: manual review required')
hook='''
case 0x68: { /* MARS experimental NDS read-only endpoint */
size_t nds_used = 0;
completition = mars_nds_request(ncprequest->task, ncprequest->sequence,
requestdata, requestlen < 0 ? 0 : (size_t)requestlen,
responsedata, sizeof(ipxdata)-sizeof(NCPRESPONSE), &nds_used);
data_len = (int)nds_used;
break;
}
'''
if known_stub:
s=s.replace(stub,hook,1)
else:
s=s[:start]+hook+s[start:]
p.write_text('#include "mars_nds_server.h"\n'+s, encoding="latin-1")