Write a packet to the stream.

        :param int pos:     The packet number to write in the sequence of the stream. May be None to append to the stream.
        :param data:        The data to write, as a string or bitvector.
        :param size:        The optional size to write. May be symbolic; must be constrained to at most the size of data.
        :return:            The next packet to use after this

def write(self, pos, data, size=None, events=True, **kwargs):
        """
        Write a packet to the stream.

        :param int pos:     The packet number to write in the sequence of the stream. May be None to append to the stream.
        :param data:        The data to write, as a string or bitvector.
        :param size:        The optional size to write. May be symbolic; must be constrained to at most the size of data.
        :return:            The next packet to use after this
        """
        if events:
            self.state.history.add_event('fs_write', filename=self.name, data=data, size=size, pos=pos)

        # sanity check on read/write modes
        if self.write_mode is None:
            self.write_mode = True
        elif self.write_mode is False:
            raise SimFileError("Cannot read and write to the same SimPackets")

        data = _deps_unpack(data)[0]
        if type(data) is bytes:
            data = claripy.BVV(data)
        if size is None:
            size = len(data) // self.state.arch.byte_width if isinstance(data, claripy.Bits) else len(data)
        if type(size) is int:
            size = self.state.solver.BVV(size, self.state.arch.bits)

        # sanity check on packet number and determine if data is already present
        if pos < 0:
            raise SimFileError("SimPacket.write(%d): Negative packet number?" % pos)
        elif pos > len(self.content):
            raise SimFileError("SimPacket.write(%d): Packet number is past frontier of %d?" % (pos, len(self.content)))
        elif pos != len(self.content):
            realdata, realsize = self.content[pos]
            maxlen = max(len(realdata), len(data))
            self.state.solver.add(realdata[maxlen-1:0] == data[maxlen-1:0])
            self.state.solver.add(size == realsize)
            if not self.state.solver.satisfiable():
                raise SimFileError("Packet write equality constraints made state unsatisfiable???")
            return pos+1

        # write it out!
        self.content.append((_deps_unpack(data)[0], size))
        return pos+1