import zlib


def read_zlib_chunks(read_some, dec_size, buffer_size=4096):
    """Read zlib data from a buffer.

    This function requires that the buffer have additional data following the
    compressed data, which is guaranteed to be the case for git pack files.

    :param read_some: Read function that returns at least one byte, but may
        return less than the requested size
    :param dec_size: Expected size of the decompressed buffer
    :param buffer_size: Size of the read buffer
    :return: Tuple with list of chunks, length of compressed data length and
        and unused read data.
    :raise zlib.error: if a decompression error occurred.
    """
    if dec_size <= -1:
        raise ValueError("non-negative zlib data stream size expected")
    obj = zlib.decompressobj()
    ret = []
    fed = 0
    size = 0
    while obj.unused_data == "":
        add = read_some(buffer_size)
        if not add:
            raise zlib.error("EOF before end of zlib stream")
        fed += len(add)
        decomp = obj.decompress(add)
        size += len(decomp)
        ret.append(decomp)
    if size != dec_size:
        raise zlib.error("decompressed data does not match expected size")
    comp_len = fed - len(obj.unused_data)
    return ret, comp_len, obj.unused_data