bool XMLReader::Read() {
	    if (mWhitespaceHandling == kWhitespaceHandlingNone) {
	        while (ReadInternal()) {
	            if (mNodeType != kWhitespace) {
	                return true;
	            }
	        }
	        return false;
	    }
	    else {
	        return ReadInternal();
	    }
	}
	virtual bool Read(uint8* Destination, int64 BytesToRead) override
	{
#if MANAGE_FILE_HANDLES
		if( IsManaged() )
		{
			ActivateSlot();
			int64 BytesRead = ReadInternal(Destination, BytesToRead);
			FileOffset += BytesRead;
			return BytesRead == BytesToRead;
		}
		else
#endif
		{
			return ReadInternal(Destination, BytesToRead) == BytesToRead;
		}
	}
Example #3
0
bool DirectoryBlobReader::Read(u64 offset, u64 length, u8* buffer)
{
  // TODO: We don't handle raw access to the encrypted area of Wii discs correctly.

  const std::set<DiscContent>& contents =
      m_is_wii ? m_nonpartition_contents : m_gamecube_pseudopartition.GetContents();
  return ReadInternal(offset, length, buffer, contents);
}
Example #4
0
nsresult
SourceBufferResource::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes)
{
  SBR_DEBUGV("Read(aBuffer=%p, aCount=%u, aBytes=%p)",
             aBuffer, aCount, aBytes);
  ReentrantMonitorAutoEnter mon(mMonitor);

  return ReadInternal(aBuffer, aCount, aBytes, /* aMayBlock = */ true);
}
byte idZeroRunLengthCompressor::ReadByte() {
	// See if we need to possibly read more data
	if ( zeroCount == 0 ) {
		int value = ReadInternal();
		if ( value == -1 ) {
			assert( 0 );
		}
		if ( value != 0 ) {
			return (byte)value;	// Return non zero values immediately
		}
		// Read the number of zeroes
		zeroCount = ReadInternal();
	}
	
	assert( zeroCount > 0 );
	
	zeroCount--;
	return 0;
}
Example #6
0
bool DirectoryBlobReader::ReadWiiDecrypted(u64 offset, u64 size, u8* buffer, u64 partition_offset)
{
  if (!m_is_wii)
    return false;

  auto it = m_partitions.find(partition_offset);
  if (it == m_partitions.end())
    return false;

  return ReadInternal(offset, size, buffer, it->second.GetContents());
}
nsresult
SourceBufferResource::ReadAtInternal(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes,
                                     bool aMayBlock)
{
  mMonitor.AssertCurrentThreadIn();
  nsresult rv = SeekInternal(aOffset);
  if (NS_FAILED(rv)) {
    return rv;
  }

  return ReadInternal(aBuffer, aCount, aBytes, aMayBlock);
}
// Sync methods
int64_t StdStreamAdapter::Read(uint8_t *pbBuffer,
                               int64_t  cbBuffer) {
  if (m_iBackingStream.get() == nullptr) {
    // unavailable
    throw exceptions::RMSCryptoIOException(
            exceptions::RMSCryptoIOException::OperationUnavailable,
            "Operation unavailable!");
  }

  lock_guard<mutex> locker(*m_locker);
  return ReadInternal(pbBuffer, cbBuffer);
}
int RageFileObjInflate::SeekInternal( int iPos )
{
	/* Optimization: if offset is the end of the file, it's a lseek(0,SEEK_END).  Don't
	 * decode anything. */
	if( iPos >= m_iUncompressedSize )
	{
		m_iFilePos = m_iUncompressedSize;
		m_pFile->Seek( m_pFile->GetFileSize() );
		decomp_buf_ptr = decomp_buf;
		decomp_buf_avail = 0;
		inflateReset( m_pInflate );
		return m_iUncompressedSize;
	}

	if( iPos < m_iFilePos )
	{
		inflateReset( m_pInflate );
		decomp_buf_ptr = decomp_buf;
		decomp_buf_avail = 0;

		m_pFile->Seek( 0 );
		m_iFilePos = 0;
	}

	int iOffset = iPos - m_iFilePos;

	/* Can this be optimized? */
	char buf[1024*4];
	while( iOffset )
	{
		int got = ReadInternal( buf, min( (int) sizeof(buf), iOffset ) );
		if( got == -1 )
			return -1;

		if( got == 0 )
			break;
		iOffset -= got;
	}

	return m_iFilePos;
}
FileInputStream::SizeType FileInputStream::read(void* destBuffer,
                                                SizeType maxBytesToRead) {
  if (!OpenedOk()) {
    LOG(Warning) << "Trying to read from an invalid stream.";
    NOTREACHED() << "Do not read from an invalid stream.";
    return 0;
  }
  DCHECK(destBuffer != NULL && maxBytesToRead >= 0);

  if (m_needToSeek) {
    if (detail::setFileInputStreamPosition(m_handle, m_currentPosition) < 0)
      return 0;

    m_needToSeek = false;
  }

  size_t num = ReadInternal(destBuffer, static_cast<size_t>(maxBytesToRead));
  m_currentPosition += num;

  return static_cast<SizeType>(num);
}
Example #11
0
uint8 ReadStream::ReadByte()
{
	uint8 val;
	ReadInternal(&val, sizeof(val));
	return val;
}
Example #12
0
ssize_t
TextStream::Read (char *buf, size_t n)
{
	size_t inleft = buflen;
	char *inbuf = bufptr;
	char *outbuf = buf;
	size_t outleft = n;
	ssize_t nread;
	size_t r;
	
	do {
		if (cd != (GIConv) -1) {
			if (g_iconv (cd, &inbuf, &inleft, &outbuf, &outleft) == (size_t) -1) {
				switch (errno) {
				case E2BIG:
					// not enough space available in the output buffer
					goto out;
				case EINVAL:
					// incomplete multibyte character sequence
					goto out;
				case EILSEQ:
					// illegal multibyte sequence
					return -1;
				default:
					// unknown error, fail
					return -1;
				}
			}
		} else {
			r = MIN (inleft, outleft);
			memcpy (outbuf, inbuf, r);
			outleft -= r;
			outbuf += r;
			inleft -= r;
			inbuf += r;
		}
		
		if (outleft == 0 || eof)
			break;
		
		// buffer more data
		if (inleft > 0)
			memmove (buffer, inbuf, inleft);
		
		inbuf = buffer + inleft;
		if ((nread = ReadInternal (inbuf, sizeof (buffer) - inleft)) <= 0) {
			eof = true;
			break;
		}
		
		inleft += nread;
		inbuf = buffer;
	} while (true);
	
	if (eof && cd != (GIConv) -1)
		g_iconv (cd, NULL, NULL, &outbuf, &outleft);
	
out:
	
	buflen = inleft;
	bufptr = inbuf;
	
	return (outbuf - buf);
}
Example #13
0
bool
TextStream::ReadBOM (bool force)
{
	Encoding encoding = UNKNOWN;
	gunichar2 bom;
	ssize_t nread;
	
	// prefetch the first chunk of data in order to determine encoding
	if ((nread = ReadInternal (buffer, sizeof (buffer))) == -1) {
		Close ();
		
		return false;
	}
	
	bufptr = buffer;
	buflen = nread;
	
	if (nread >= 2) {
		memcpy (&bom, buffer, 2);
		switch (bom) {
		case ANTIBOM:
			encoding = UTF16_BE;
			buflen -= 2;
			bufptr += 2;
			break;
		case BOM:
			encoding = UTF16_LE;
			buflen -= 2;
			bufptr += 2;
			break;
		case 0:
			if (nread >= 4) {
				memcpy (&bom, buffer + 2, 2);
				if (bom == ANTIBOM) {
					encoding = UTF32_BE;
					buflen -= 4;
					bufptr += 4;
				} else if (bom == BOM) {
					encoding = UTF32_LE;
					buflen -= 4;
					bufptr += 4;
				}
			}
			break;
		default:
			encoding = UTF8;
			break;
		}
	} else {
		// assume utf-8
		encoding = UTF8;
	}
	
	if (encoding == UNKNOWN) {
		if (!force) {
			Close ();
			
			return false;
		}
		
		encoding = UTF8;
	}
	
	if (encoding != UTF8 && (cd = g_iconv_open ("UTF-8", encoding_names[encoding])) == (GIConv) -1) {
		Close ();
		
		return false;
	}
	
	eof = false;
	
	return true;
}
Example #14
0
unsigned File::Read(void* dest, unsigned size)
{
    if (!IsOpen())
    {
        // If file not open, do not log the error further here to prevent spamming the stderr stream
        return 0;
    }

    if (mode_ == FILE_WRITE)
    {
        ATOMIC_LOGERROR("File not opened for reading");
        return 0;
    }

    if (size + position_ > size_)
        size = size_ - position_;
    if (!size)
        return 0;

#ifdef __ANDROID__
    if (assetHandle_ && !compressed_)
    {
        // If not using a compressed package file, buffer file reads on Android for better performance
        if (!readBuffer_)
        {
            readBuffer_ = new unsigned char[READ_BUFFER_SIZE];
            readBufferOffset_ = 0;
            readBufferSize_ = 0;
        }

        unsigned sizeLeft = size;
        unsigned char* destPtr = (unsigned char*)dest;

        while (sizeLeft)
        {
            if (readBufferOffset_ >= readBufferSize_)
            {
                readBufferSize_ = Min(size_ - position_, READ_BUFFER_SIZE);
                readBufferOffset_ = 0;
                ReadInternal(readBuffer_.Get(), readBufferSize_);
            }

            unsigned copySize = Min((readBufferSize_ - readBufferOffset_), sizeLeft);
            memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
            destPtr += copySize;
            sizeLeft -= copySize;
            readBufferOffset_ += copySize;
            position_ += copySize;
        }

        return size;
    }
#endif

    if (compressed_)
    {
        unsigned sizeLeft = size;
        unsigned char* destPtr = (unsigned char*)dest;

        while (sizeLeft)
        {
            if (!readBuffer_ || readBufferOffset_ >= readBufferSize_)
            {
                unsigned char blockHeaderBytes[4];
                ReadInternal(blockHeaderBytes, sizeof blockHeaderBytes);

                MemoryBuffer blockHeader(&blockHeaderBytes[0], sizeof blockHeaderBytes);
                unsigned unpackedSize = blockHeader.ReadUShort();
                unsigned packedSize = blockHeader.ReadUShort();

                if (!readBuffer_)
                {
                    readBuffer_ = new unsigned char[unpackedSize];
                    inputBuffer_ = new unsigned char[LZ4_compressBound(unpackedSize)];
                }

                /// \todo Handle errors
                ReadInternal(inputBuffer_.Get(), packedSize);
                LZ4_decompress_fast((const char*)inputBuffer_.Get(), (char*)readBuffer_.Get(), unpackedSize);

                readBufferSize_ = unpackedSize;
                readBufferOffset_ = 0;
            }

            unsigned copySize = Min((readBufferSize_ - readBufferOffset_), sizeLeft);
            memcpy(destPtr, readBuffer_.Get() + readBufferOffset_, copySize);
            destPtr += copySize;
            sizeLeft -= copySize;
            readBufferOffset_ += copySize;
            position_ += copySize;
        }

        return size;
    }

    // Need to reassign the position due to internal buffering when transitioning from writing to reading
    if (readSyncNeeded_)
    {
        SeekInternal(position_ + offset_);
        readSyncNeeded_ = false;
    }

    if (!ReadInternal(dest, size))
    {
        // Return to the position where the read began
        SeekInternal(position_ + offset_);
        ATOMIC_LOGERROR("Error while reading from file " + GetName());
        return 0;
    }

    writeSyncNeeded_ = true;
    position_ += size;
    return size;
}
Example #15
0
SISPLUGIN_EXPORT long SISPluginReadCard(
	unsigned long   ulVersion,              /**< In:     SW-version of the middleware */
	void            *pPCSCfunctions,        /**< In:     pointer to a tPCSCfunctions struct */
	const char      *csReaderName,          /**< In:     the reader name as returned by SCardListReaders() */
	SCARDHANDLE     *phCard,                /**< In:     handle to the card if it was succesful or 0 when it failed */
	unsigned char   *pucData,               /**< Out:    returns the 404 bytes of the SIS-card */
	unsigned long   ulReserved,             /**< In:     reserved for future extensions */
	void            *pReserved              /**< In/Out: reserved for future extensions */
	)             
{
	eIDMW::MWLOG(eIDMW::LEV_DEBUG, eIDMW::MOD_SIS, L"SISPluginReadCard(%d) called", *phCard);

	SCARD_IO_REQUEST ioSendPci, ioRecvPci;
	tPCSCfunctions *pPCSCfuncs = (tPCSCfunctions *) pPCSCfunctions;
	DWORD ulProto = SCARD_PROTOCOL_T0;
	long lRet = -11111;

	if (ulVersion/100 != 1)
		return lRet;         //only version 1.xx supported

	memset(pucData, 0, 404);

#ifdef __APPLE__
	if (0 != *phCard)
	{
		pPCSCfuncs->pCardDisconnect(*phCard, SCARD_RESET_CARD);
		*phCard = 0;
	}
#endif

	if (0 == *phCard)
	{
		lRet = ConnectInSyncMode(pPCSCfuncs, csReaderName, phCard, &ulProto);
		eIDMW::MWLOG(eIDMW::LEV_DEBUG, eIDMW::MOD_SIS, L"    ConnectInSyncMode return = 0x%0x",lRet);
		if (SCARD_S_SUCCESS != lRet)
			return lRet;
	}

	ioSendPci.cbPciLength = sizeof(SCARD_IO_REQUEST);
	ioRecvPci.cbPciLength = sizeof(SCARD_IO_REQUEST);
	ioSendPci.dwProtocol = ulProto;
	ioRecvPci.dwProtocol = ulProto;

	lRet = pPCSCfuncs->pCardBeginTransaction(*phCard);
	if (SCARD_S_SUCCESS == lRet)
	{
		lRet = ReadInternal(pPCSCfuncs, *phCard, &ioSendPci, &ioRecvPci, 0, 404, pucData);
		eIDMW::MWLOG(eIDMW::LEV_DEBUG, eIDMW::MOD_SIS, L"    ReadInternal return = 0x%0x",lRet);
		BackToAsyncMode(pPCSCfuncs, csReaderName, phCard);
		pPCSCfuncs->pCardEndTransaction(*phCard, SCARD_LEAVE_CARD);
	}

	// Check if it's a SIS card
	if (0 == lRet && (0xA0 != pucData[21] || 0x00 != pucData[22] || 0x00 != pucData[23] ||
		0x00 != pucData[24] || 0x33 != pucData[25]))
	{
		lRet = SCARD_E_CARD_UNSUPPORTED;
	}

	return lRet;
}