Beispiel #1
0
HRESULT OutFileStream::Close( void )
{
  return ConvertBoolToHRESULT(file_.Close());
}
STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
{
  #ifdef USE_WIN_FILE
  
  #ifdef SUPPORT_DEVICE_FILE
  if (processedSize != NULL)
    *processedSize = 0;
  if (size == 0)
    return S_OK;
  if (File.IsDeviceFile)
  {
    if (File.LengthDefined)
    {
      if (VirtPos >= File.Length)
        return VirtPos == File.Length ? S_OK : E_FAIL;
      UInt64 rem = File.Length - VirtPos;
      if (size > rem)
        size = (UInt32)rem;
    }
    for (;;)
    {
      const UInt32 mask = kClusterSize - 1;
      UInt64 mask2 = ~(UInt64)mask;
      UInt64 alignedPos = VirtPos & mask2;
      if (BufferSize > 0 && BufferStartPos == alignedPos)
      {
        UInt32 pos = (UInt32)VirtPos & mask;
        if (pos >= BufferSize)
          return S_OK;
        UInt32 rem = MyMin(BufferSize - pos, size);
        memcpy(data, Buffer + pos, rem);
        VirtPos += rem;
        if (processedSize != NULL)
          *processedSize += rem;
        return S_OK;
      }
      
      bool useBuffer = false;
      if ((VirtPos & mask) != 0 || ((ptrdiff_t)data & mask) != 0 )
        useBuffer = true;
      else
      {
        UInt64 end = VirtPos + size;
        if ((end & mask) != 0)
        {
          end &= mask2;
          if (end <= VirtPos)
            useBuffer = true;
          else
            size = (UInt32)(end - VirtPos);
        }
      }
      if (!useBuffer)
        break;
      if (alignedPos != PhyPos)
      {
        UInt64 realNewPosition;
        bool result = File.Seek(alignedPos, FILE_BEGIN, realNewPosition);
        if (!result)
          return ConvertBoolToHRESULT(result);
        PhyPos = realNewPosition;
      }

      BufferStartPos = alignedPos;
      UInt32 readSize = kClusterSize;
      if (File.LengthDefined)
        readSize = (UInt32)MyMin(File.Length - PhyPos, (UInt64)kClusterSize);

      if (Buffer == 0)
      {
        Buffer = (Byte *)MidAlloc(kClusterSize);
        if (Buffer == 0)
          return E_OUTOFMEMORY;
      }
      bool result = File.Read1(Buffer, readSize, BufferSize);
      if (!result)
        return ConvertBoolToHRESULT(result);

      if (BufferSize == 0)
        return S_OK;
      PhyPos += BufferSize;
    }

    if (VirtPos != PhyPos)
    {
      UInt64 realNewPosition;
      bool result = File.Seek(VirtPos, FILE_BEGIN, realNewPosition);
      if (!result)
        return ConvertBoolToHRESULT(result);
      PhyPos = VirtPos = realNewPosition;
    }
  }
  #endif

  UInt32 realProcessedSize;
  bool result = File.ReadPart(data, size, realProcessedSize);
  if (processedSize != NULL)
    *processedSize = realProcessedSize;
  #ifdef SUPPORT_DEVICE_FILE
  VirtPos += realProcessedSize;
  PhyPos += realProcessedSize;
  #endif
  return ConvertBoolToHRESULT(result);
  
  #else
  
  if (processedSize != NULL)
    *processedSize = 0;
  ssize_t res = File.Read(data, (size_t)size);
  if (res == -1)
    return E_FAIL;
  if (processedSize != NULL)
    *processedSize = (UInt32)res;
  return S_OK;

  #endif
}
Beispiel #3
0
STDMETHODIMP CInFileStream::GetSize(UInt64 *size)
{
  return ConvertBoolToHRESULT(File.GetLength(*size));
}
HRESULT COutFileStream::Close()
{
  return ConvertBoolToHRESULT(File.Close());
}