Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/cortex/blob/main/tests/import/walfix.py # cortex aien-dev/cortex Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `cortex / tests / import / walfix.py` 79 lines ยท 3337 bytes Raw Blame History 1 #!/usr/bin/env python3 2 # walfix.py - verification-only WAL oracle/fixture tool (Python 3, not on the product path). 3 # Independent re-implementation of SQLite 3.45 walIndexRecover/walDecodeFrame/walChecksumBytes. 4 # walfix.py info WAL -> "frames=N valid=V committed=M dbpages=P commits=a,b,..." 5 # walfix.py flipck WAL K -> flip one byte of frame K's checksum-1 field (1-based) 6 # walfix.py be WAL -> re-encode the valid prefix with big-endian checksums 7 # (magic 0x377f0683), as a big-endian host would write it 8 # walfix.py version WAL N -> set the format version to N and re-seal the header checksum 9 import struct, sys 10 11 def cksum(be, data, s1, s2): 12 fmt = '>%dI' if be else '<%dI' 13 w = struct.unpack(fmt % (len(data) // 4), data) 14 for i in range(0, len(w), 2): 15 s1 = (s1 + w[i] + s2) & 0xFFFFFFFF 16 s2 = (s2 + w[i + 1] + s1) & 0xFFFFFFFF 17 return s1, s2 18 19 def scan(b): 20 if len(b) <= 32: 21 return dict(frames=0, valid=0, committed=0, dbpages=0, commits=[], ok=False) 22 magic, ver, ps, ck, salt1, salt2, c1, c2 = struct.unpack('>8I', b[:32]) 23 if (magic & 0xFFFFFFFE) != 0x377f0682 or ps & (ps - 1) or ps > 65536 or ps < 512: 24 return dict(frames=0, valid=0, committed=0, dbpages=0, commits=[], ok=False) 25 be = magic & 1 26 s = cksum(be, b[:24], 0, 0) 27 if s != (c1, c2): 28 return dict(frames=0, valid=0, committed=0, dbpages=0, commits=[], ok=False) 29 n = (len(b) - 32) // (ps + 24) 30 valid = mx = ndb = 0 31 commits = [] 32 for i in range(n): 33 o = 32 + i * (ps + 24) 34 pg, nt, fs1, fs2, k1, k2 = struct.unpack('>6I', b[o:o + 24]) 35 if (fs1, fs2) != (salt1, salt2) or pg == 0: 36 break 37 s = cksum(be, b[o:o + 8], *s) 38 s = cksum(be, b[o + 24:o + 24 + ps], *s) 39 if s != (k1, k2): 40 break 41 valid = i + 1 42 if nt: 43 mx, ndb = i + 1, nt 44 commits.append(i + 1) 45 return dict(frames=n, valid=valid, committed=mx, dbpages=ndb, commits=commits, ok=True, 46 ps=ps, ver=ver) 47 48 def main(): 49 cmd, path = sys.argv[1], sys.argv[2] 50 b = bytearray(open(path, 'rb').read()) 51 if cmd == 'info': 52 r = scan(bytes(b)) 53 print('frames=%d valid=%d committed=%d dbpages=%d commits=%s' % ( 54 r['frames'], r['valid'], r['committed'], r['dbpages'], 55 ','.join(map(str, r['commits'])) or '-')) 56 return 57 if cmd == 'flipck': 58 k = int(sys.argv[3]); ps = struct.unpack('>I', b[8:12])[0] 59 o = 32 + (k - 1) * (ps + 24) + 16 60 b[o] ^= 0x01 61 elif cmd == 'be': 62 r = scan(bytes(b)); ps = r['ps'] 63 struct.pack_into('>I', b, 0, 0x377f0683) 64 s = cksum(1, bytes(b[:24]), 0, 0) 65 struct.pack_into('>2I', b, 24, *s) 66 for i in range(r['valid']): 67 o = 32 + i * (ps + 24) 68 s = cksum(1, bytes(b[o:o + 8]), *s) 69 s = cksum(1, bytes(b[o + 24:o + 24 + ps]), *s) 70 struct.pack_into('>2I', b, o + 16, *s) 71 elif cmd == 'version': 72 struct.pack_into('>I', b, 4, int(sys.argv[3])) 73 be = struct.unpack('>I', b[:4])[0] & 1 74 struct.pack_into('>2I', b, 24, *cksum(be, bytes(b[:24]), 0, 0)) 75 else: 76 sys.exit('walfix: unknown command') 77 open(path, 'wb').write(b) 78 79 main()