def decode_syncsafe32(i):
    """given a 32 bit int, returns a 28 bit value
    with sync-safe bits removed

    may raise ValueError if the value is negative,
    larger than 32 bits or contains invalid sync-safe bits"""
    if i >= 2 ** 32:
        raise ValueError('value of {} is too large'.format(i))
    elif i < 0:
        raise ValueError('value cannot be negative')
    value = 0
    for x in range(4):
        if i & 128 == 0:
            value |= (i & 127) << x * 7
            i >>= 8
        else:
            raise ValueError('invalid sync-safe bit')
    return value
