import struct


def read_openssh_string(bin_fp):
    """
    Read a length-prefixed ("OpenSSH-style Pascal") string from a file.

    This format is described in https://tools.ietf.org/html/rfc4251
    (look for "Arbitrary length binary string").

    :param bin_fp: Binary file-like object
    :return: bytes
    """
    len_buf = bin_fp.read(4)
    if not len_buf:
        raise EOFError()
    length, = struct.unpack('!I', len_buf)
    buf = bin_fp.read(length)
    if len(buf) != length:
        raise ValueError(
            'short read for string (expected %d bytes, read %d)'
            % (length, len(buf))
        )
    return buf