# `pwnlib.tubes.buffer` — buffer implementation for tubes

class pwnlib.tubes.buffer.Buffer(buffer_fill_size=None)

  List of strings with some helper routines.

  Example

      >>> b = Buffer()
      >>> b.add(b"A" * 10)
      >>> b.add(b"B" * 10)
      >>> len(b)
      20
      >>> b.get(1)
      b'A'
      >>> len(b)
      19
      >>> b.get(9999)
      b'AAAAAAAAABBBBBBBBBB'
      >>> len(b)
      0
      >>> b.get(1)
      b''

  Implementation Details:

  > Implemented as a list. Strings are added onto the end. The `0th` item in the buffer is the oldest item, and will be received first.

  __contains__(x)

        >>> b = Buffer()
        >>> b.add(b'asdf')
        >>> b'x' in b
        False
        >>> b.add(b'x')
        >>> b'x' in b
        True

  __init__(buffer_fill_size=None)
  __len__()

        >>> b = Buffer()
        >>> b.add(b'lol')
        >>> len(b) == 3
        True
        >>> b.add(b'foobar')
        >>> len(b) == 9
        True

  add(data)

    Adds data to the buffer.

    Parameters:

      data (str,Buffer) – Data to add

  get(want=inf)

    Retrieves bytes from the buffer.

    Parameters:

      want (int) – Maximum number of bytes to fetch

    Returns:

      Data as string

    Example

        >>> b = Buffer()
        >>> b.add(b'hello')
        >>> b.add(b'world')
        >>> b.get(1)
        b'h'
        >>> b.get()
        b'elloworld'

  get_fill_size(size=None)

    Retrieves the default fill size for this buffer class.

    Parameters:

      size (int) – (Optional) If set and not None, returns the size variable back.

    Returns:

      Fill size as integer if size is None, else size.

  index(x)

        >>> b = Buffer()
        >>> b.add(b'asdf')
        >>> b.add(b'qwert')
        >>> b.index(b't') == len(b) - 1
        True

  unget(data)

    Places data at the front of the buffer.

    Parameters:

      data (str,Buffer) – Data to place at the beginning of the buffer.

    Example

        >>> b = Buffer()
        >>> b.add(b"hello")
        >>> b.add(b"world")
        >>> b.get(5)
        b'hello'
        >>> b.unget(b"goodbye")
        >>> b.get()
        b'goodbyeworld'

  __weakref__

    list of weak references to the object