#!/usr/bin/env python3
"""
Parse Saleae Logic Analyzer capture to extract RFID communication
"""
import struct
import sys

def read_sal_file(filename):
    """Read and parse the .sal file"""
    try:
        with open(filename, 'rb') as f:
            data = f.read()
        
        print(f"[+] File size: {len(data)} bytes")
        print(f"[+] First 100 bytes (hex):")
        print(' '.join(f'{b:02x}' for b in data[:100]))
        print()
        
        # Look for common patterns
        print("[+] Searching for readable strings...")
        strings = []
        current_string = []
        
        for byte in data:
            if 32 <= byte <= 126:  # Printable ASCII
                current_string.append(chr(byte))
            else:
                if len(current_string) >= 4:
                    strings.append(''.join(current_string))
                current_string = []
        
        if strings:
            print("[+] Found strings:")
            for s in strings[:20]:  # Show first 20
                print(f"    {s}")
        
        # Look for hex patterns that might be UIDs, auth codes, etc.
        print("\n[+] Looking for potential RFID data patterns...")
        
        # Search for sector data (MIFARE Classic blocks are 16 bytes)
        for i in range(0, len(data) - 16, 1):
            block = data[i:i+16]
            # Check if it looks like structured data (not all zeros or all FFs)
            if not all(b == 0 for b in block) and not all(b == 0xff for b in block):
                # Check for patterns
                hex_str = ' '.join(f'{b:02x}' for b in block)
                if any(c.isalpha() for c in hex_str) or sum(block) > 256:
                    if i < 1000:  # Only show early patterns
                        print(f"    Offset {i:04x}: {hex_str}")
        
        return data
        
    except Exception as e:
        print(f"[-] Error reading file: {e}")
        return None

if __name__ == "__main__":
    filename = "access_reader_logic_data.sal"
    data = read_sal_file(filename)
