Istream& ITstream::read(token& t)
{
    // Return the put back token if it exists
    if (Istream::getBack(t))
    {
        lineNumber_ = t.lineNumber();
        return *this;
    }

    if (tokenIndex_ < size())
    {
        t = operator[](tokenIndex_++);
        lineNumber_ = t.lineNumber();

        if (tokenIndex_ == size())
        {
            setEof();
        }
    }
    else
    {
        if (eof())
        {
            FatalIOErrorIn
            (
                "ITstream::read(token& t)",
                *this
            )   << "attempt to read beyond EOF"
                << exit(FatalIOError);

            setBad();
        }
        else
        {
            setEof();
        }

        if (size())
        {
            token::undefinedToken.lineNumber()
                = operator[](size() - 1).lineNumber();
        }
        else
        {
            token::undefinedToken.lineNumber() = lineNumber();
        }

        t = token::undefinedToken;
    }

    return *this;
}
Exemplo n.º 2
0
int64_t ZipFile::readImpl(char *buffer, int64_t length) {
  assert(m_gzFile);
  int64_t nread = gzread(m_gzFile, buffer, length);
  if (nread == 0 || gzeof(m_gzFile)) {
    setEof(true);
  } else {
    errno = 0;
    gzerror(m_gzFile, &errno);
    if (errno == 1) { // Z_STREAM_END = 1
      setEof(true);
    }
  }
  return (nread < 0) ? 0 : nread;
}
Exemplo n.º 3
0
ODevice::ODevice()
{
    _impl = new IODeviceImpl(*this);
    setEnabled(true);
    setAsync(true);
    setEof(false);
}
inline void IPstream::checkEof()
{
    if (bufPosition_ == messageSize_)
    {
        setEof();
    }
}
Exemplo n.º 5
0
bool ZipFile::seek(int64_t offset, int whence /* = SEEK_SET */) {
  assert(m_gzFile);

  if (whence == SEEK_CUR) {
    off_t result = gzseek(m_gzFile, 0, SEEK_CUR);
    if (result != (off_t)-1) {
      offset += result - (bufferedLen() + getPosition());
    }
    if (offset > 0 && offset < bufferedLen()) {
      setReadPosition(getReadPosition() + offset);
      setPosition(getPosition() + offset);
      return true;
    }
    offset += getPosition();
    whence = SEEK_SET;
  }

  // invalidate the current buffer
  setWritePosition(0);
  setReadPosition(0);
  setEof(false);
  flush();
  off_t result = gzseek(m_gzFile, offset, whence);
  setPosition(result);
  return result != (off_t)-1;
}
Exemplo n.º 6
0
inline void Foam::UIPstream::checkEof()
{
    if (externalBufPosition_ == messageSize_)
    {
        setEof();
    }
}
Exemplo n.º 7
0
bool PlainFile::seek(int64_t offset, int whence /* = SEEK_SET */) {
  assert(valid());

  if (whence == SEEK_CUR) {
    off_t result = lseek(getFd(), 0, SEEK_CUR);
    if (result != (off_t)-1) {
      offset += result - (bufferedLen() + getPosition());
    }
    if (offset > 0 && offset < bufferedLen()) {
      setReadPosition(getReadPosition() + offset);
      setPosition(getPosition() + offset);
      return true;
    }
    offset += getPosition();
    whence = SEEK_SET;
  }

  // invalidate the current buffer
  setWritePosition(0);
  setReadPosition(0);
  // clear the eof flag
  setEof(false);
  flush();
  // lseek instead of seek to be consistent with read
  off_t result = lseek(getFd(), offset, whence);
  setPosition(result);
  return result != (off_t)-1;
}
Exemplo n.º 8
0
StdinDevice::StdinDevice()
{
    _impl = new IODeviceImpl(*this);
    setEnabled(true);
    setAsync(true);
    setEof(false);
    _impl->open(STDIN_FILENO, true, true);
}
Exemplo n.º 9
0
void TcpSocket::accept(const TcpServer& server, unsigned flags)
{
    close();
    _impl->accept(server, flags);
    setEnabled(true);
    setAsync(true);
    setEof(false);
}
Exemplo n.º 10
0
void TcpSocket::connect(const AddrInfo& addrinfo)
{
    close();
    _impl->beginConnect(addrinfo);
    _impl->endConnect();
    setEnabled(true);
    setAsync(true);
    setEof(false);
}
Exemplo n.º 11
0
bool ZipFile::rewind() {
  assert(m_gzFile);
  seek(0);
  setWritePosition(0);
  setReadPosition(0);
  setPosition(0);
  setEof(false);
  gzrewind(m_gzFile);
  return true;
}
Exemplo n.º 12
0
bool BZ2File::closeImpl() {
  assert(m_bzFile);
  bool ret = true;
  BZ2_bzclose(m_bzFile);
  m_bzFile = nullptr;
  setIsClosed(true);
  m_innerFile->close();
  File::closeImpl();
  setEof(false);
  return ret;
}
Exemplo n.º 13
0
int64_t PlainFile::readImpl(char *buffer, int64_t length) {
  assert(valid());
  assert(length > 0);
  // use read instead of fread to handle EOL in stdin
  size_t ret = ::read(getFd(), buffer, length);
  if (ret == 0
   || (ret == (size_t)-1
    && errno != EWOULDBLOCK && errno != EINTR && errno != EBADF)) {
    setEof(true);
  }
  return ret == (size_t)-1 ? 0 : ret;
}
Exemplo n.º 14
0
bool TcpSocket::beginConnect(const AddrInfo& addrinfo)
{
    close();
    bool ret = _impl->beginConnect(addrinfo);
    setEnabled(true);
    setAsync(true);
    setEof(false);

    if (ret)
        connected(*this);
    return ret;
}
Exemplo n.º 15
0
int64_t BZ2File::readImpl(char * buf, int64_t length) {
  if (length == 0) {
    return 0;
  }
  assert(m_bzFile);
  int len = BZ2_bzread(m_bzFile, buf, length);
  /* Sometimes libbz2 will return fewer bytes than requested, and set bzerror
   * to BZ_STREAM_END, but it's not actually EOF, and you can keep reading from
   * the file - so, only set EOF after a failed read. This matches PHP5.
   */
  if (len == 0) {
    setEof(true);
  }
  return len;
}
bool ZLGzipAsynchronousInputStream::processInputInternal(Handler &handler) {
	if (myEndOfStream) {
		return false;
	}
	if (eof()) {
		handler.shutdown();
		myEndOfStream = true;
		return true;
	}
	while (myOffset < myDataLen && myStreamState) {
		if (!skipHeader(myData, myDataLen)) {
			return false;
		}
	}
	if (myOffset >= myDataLen) {
		myOffset -= myDataLen;
		return true;
	}
	myZStream->next_in = (Bytef*) myData + myOffset;
	myZStream->avail_in = myDataLen - myOffset;
	myOffset = 0;
	bool forcedCall = false;
	while (!myEndOfStream && (myZStream->avail_in > 0 || forcedCall)) {
		forcedCall = false;
		myZStream->avail_out = myOutBufferSize;
		myZStream->next_out = (Bytef*) myOutBuffer;
		int code = ::inflate(myZStream, Z_SYNC_FLUSH);
		if ((code != Z_OK) && (code != Z_STREAM_END)) {
			return false;
		}
		if (myOutBufferSize != myZStream->avail_out) {
			if (myZStream->avail_out == 0) {
				forcedCall = true;
			}
			if (!handler.handleBuffer(myOutBuffer, myOutBufferSize - myZStream->avail_out)) {
				return false;
			}
			if (code == Z_STREAM_END) {
				myEndOfStream = true;
				setEof();
				handler.shutdown();
			}
		}
	}
	return true;
}
Exemplo n.º 17
0
int64_t SSLSocket::readImpl(char *buffer, int64_t length) {
  int64_t nr_bytes = 0;
  if (m_data->m_ssl_active) {
    bool retry = true;
    do {
      if (m_data->m_is_blocked) {
        Socket::waitForData();
        if (timedOut()) {
          break;
        }
        // could get here and we only have parts of an SSL packet
      }
      nr_bytes = SSL_read(m_data->m_handle, buffer, length);
      if (nr_bytes > 0) break; /* we got the data */
      retry = handleError(nr_bytes, false);
      setEof(!retry && errno != EAGAIN && !SSL_pending(m_data->m_handle));
    } while (retry);
  } else {
    nr_bytes = Socket::readImpl(buffer, length);
  }
  return nr_bytes < 0 ? 0 : nr_bytes;
}
Exemplo n.º 18
0
ZipFile::ZipFile() : m_gzFile(nullptr) {
  setIsLocal(true);
  setEof(false);
}
Exemplo n.º 19
0
bool SSLSocket::handleError(int64_t nr_bytes, bool is_init) {
  char esbuf[512];
  std::string ebuf;
  unsigned long ecode;

  bool retry = true;
  int err = SSL_get_error(m_data->m_handle, nr_bytes);
  switch (err) {
  case SSL_ERROR_ZERO_RETURN:
    /* SSL terminated (but socket may still be active) */
    retry = false;
    break;
  case SSL_ERROR_WANT_READ:
  case SSL_ERROR_WANT_WRITE:
    /* re-negotiation, or perhaps the SSL layer needs more
     * packets: retry in next iteration */
    errno = EAGAIN;
    retry = (is_init || m_data->m_is_blocked);
    break;
  case SSL_ERROR_SYSCALL:
    if (ERR_peek_error() == 0) {
      if (nr_bytes == 0) {
        if (ERR_get_error()) {
          raise_warning("SSL: fatal protocol error");
        }
        SSL_set_shutdown(m_data->m_handle,
                         SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
        setEof(true);
        retry = false;
      } else {
        raise_warning("SSL: %s", folly::errnoStr(errno).c_str());
        retry = false;
      }
      break;
    }
    /* fall through */
  default:
    /* some other error */
    ecode = ERR_get_error();
    switch (ERR_GET_REASON(ecode)) {
    case SSL_R_NO_SHARED_CIPHER:
      raise_warning("SSL_R_NO_SHARED_CIPHER: no suitable shared cipher "
                    "could be used.  This could be because the server is "
                    "missing an SSL certificate (local_cert context "
                    "option)");
      retry = false;
      break;

    default:
      do {
        // NULL is automatically added
        ERR_error_string_n(ecode, esbuf, sizeof(esbuf));
        if (!ebuf.empty()) {
          ebuf += '\n';
        }
        ebuf += esbuf;
      } while ((ecode = ERR_get_error()) != 0);

      raise_warning("SSL operation failed with code %d. %s%s",
                    err, !ebuf.empty() ? "OpenSSL Error messages:\n" : "",
                    !ebuf.empty() ? ebuf.c_str() : "");
    }

    retry = false;
    errno = 0;
  }

  return retry;
}
Exemplo n.º 20
0
Foam::UIPstream::UIPstream
(
    const commsTypes commsType,
    const int fromProcNo,
    DynamicList<char>& externalBuf,
    label& externalBufPosition,
    const int tag,
    const label comm,
    const bool clearAtEnd,
    streamFormat format,
    versionNumber version
)
:
    UPstream(commsType),
    Istream(format, version),
    fromProcNo_(fromProcNo),
    externalBuf_(externalBuf),
    externalBufPosition_(externalBufPosition),
    tag_(tag),
    comm_(comm),
    clearAtEnd_(clearAtEnd),
    messageSize_(0)
{
    setOpened();
    setGood();

    if (commsType == UPstream::nonBlocking)
    {
        // Message is already received into externalBuf
    }
    else
    {
        MPI_Status status;

        label wantedSize = externalBuf_.capacity();

        if (debug)
        {
            Pout<< "UIPstream::UIPstream : read from:" << fromProcNo
                << " tag:" << tag << " comm:" << comm_
                << " wanted size:" << wantedSize
                << Foam::endl;
        }


        // If the buffer size is not specified, probe the incomming message
        // and set it
        if (!wantedSize)
        {
            MPI_Probe
            (
                fromProcNo_,
                tag_,
                PstreamGlobals::MPICommunicators_[comm_],
                &status
            );
            MPI_Get_count(&status, MPI_BYTE, &messageSize_);

            externalBuf_.setCapacity(messageSize_);
            wantedSize = messageSize_;

            if (debug)
            {
                Pout<< "UIPstream::UIPstream : probed size:" << wantedSize
                    << Foam::endl;
            }
        }

        messageSize_ = UIPstream::read
        (
            commsType,
            fromProcNo_,
            externalBuf_.begin(),
            wantedSize,
            tag_,
            comm_
        );

        // Set addressed size. Leave actual allocated memory intact.
        externalBuf_.setSize(messageSize_);

        if (!messageSize_)
        {
            setEof();
        }
    }
}
Exemplo n.º 21
0
Foam::UIPstream::UIPstream(const int fromProcNo, PstreamBuffers& buffers)
:
    UPstream(buffers.commsType_),
    Istream(buffers.format_, buffers.version_),
    fromProcNo_(fromProcNo),
    externalBuf_(buffers.recvBuf_[fromProcNo]),
    externalBufPosition_(buffers.recvBufPos_[fromProcNo]),
    tag_(buffers.tag_),
    comm_(buffers.comm_),
    clearAtEnd_(true),
    messageSize_(0)
{
    if (commsType() != UPstream::scheduled && !buffers.finishedSendsCalled_)
    {
        FatalErrorIn("UIPstream::UIPstream(const int, PstreamBuffers&)")
            << "PstreamBuffers::finishedSends() never called." << endl
            << "Please call PstreamBuffers::finishedSends() after doing"
            << " all your sends (using UOPstream) and before doing any"
            << " receives (using UIPstream)" << Foam::exit(FatalError);
    }

    setOpened();
    setGood();

    if (commsType() == UPstream::nonBlocking)
    {
        // Message is already received into externalBuf
        messageSize_ = buffers.recvBuf_[fromProcNo].size();

        if (debug)
        {
            Pout<< "UIPstream::UIPstream PstreamBuffers :"
                << " fromProcNo:" << fromProcNo
                << " tag:" << tag_ << " comm:" << comm_
                << " receive buffer size:" << messageSize_
                << Foam::endl;
        }
    }
    else
    {
        MPI_Status status;

        label wantedSize = externalBuf_.capacity();

        if (debug)
        {
            Pout<< "UIPstream::UIPstream PstreamBuffers :"
                << " read from:" << fromProcNo
                << " tag:" << tag_ << " comm:" << comm_
                << " wanted size:" << wantedSize
                << Foam::endl;
        }

        // If the buffer size is not specified, probe the incomming message
        // and set it
        if (!wantedSize)
        {
            MPI_Probe
            (
                fromProcNo_,
                tag_,
                PstreamGlobals::MPICommunicators_[comm_],
                &status
            );
            MPI_Get_count(&status, MPI_BYTE, &messageSize_);

            externalBuf_.setCapacity(messageSize_);
            wantedSize = messageSize_;

            if (debug)
            {
                Pout<< "UIPstream::UIPstream PstreamBuffers : probed size:"
                    << wantedSize << Foam::endl;
            }
        }

        messageSize_ = UIPstream::read
        (
            commsType(),
            fromProcNo_,
            externalBuf_.begin(),
            wantedSize,
            tag_,
            comm_
        );

        // Set addressed size. Leave actual allocated memory intact.
        externalBuf_.setSize(messageSize_);

        if (!messageSize_)
        {
            setEof();
        }
    }
}
Exemplo n.º 22
0
String PlainFile::read(int64_t length) {
  if (length) setEof(false);
  return File::read(length);
}