def bytes_leading(raw_bytes, needle=b'\x00'):
    '\n    Finds the number of prefixed byte occurrences in the haystack.\n\n    Useful when you want to deal with padding.\n\n    :param raw_bytes:\n        Raw bytes.\n    :param needle:\n        The byte to count. Default \x00.\n    :returns:\n        The number of leading needle bytes.\n    '
    leading = 0
    _byte = needle[0]
    for x in raw_bytes:
        if x == _byte:
            leading += 1
        else:
            break
    return leading
