def decode_extra_distance(bits, dist):
    """Decode extra bits for a match distance symbol."""
    assert dist <= 29
    if dist >= 4:
        extra = (dist - 2) / 2
        if extra:
            ebits = bits.read(extra)
            dist = 2**(extra+1) + ((dist % 2) * (2**extra)) + ebits
    dist += 1
    return dist