/**
  This function gets the file system information from an open file descriptor,
  and stores it in a buffer allocated from pool.

  @param[in] FHand       The file handle.

  @return                A pointer to a buffer with file information.
  @retval                NULL is returned if failed to get Vaolume Label Info.

**/
EFI_FILE_SYSTEM_VOLUME_LABEL *
FileSystemVolumeLabelInfo (
  IN EFI_FILE_HANDLE      FHand
  )
{
  EFI_STATUS                        Status;
  EFI_FILE_SYSTEM_VOLUME_LABEL      *Buffer;
  UINTN                             BufferSize;
  //
  // Initialize for GrowBuffer loop
  //
  Buffer      = NULL;
  BufferSize  = SIZE_OF_EFI_FILE_SYSTEM_VOLUME_LABEL + 200;

  //
  // Call the real function
  //
  while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {
    Status = FHand->GetInfo (
                      FHand,
                      &gEfiFileSystemVolumeLabelInfoIdGuid,
                      &BufferSize,
                      Buffer
                      );
  }

  return Buffer;
}
int SimpleStreamBuf::overflow (int c)
{
    auto endOfFile = std::char_traits< char >::eof();
    if(c == endOfFile)
    {
        return endOfFile;
    }

    char* old_begin = m_buffer;

    char *old_pptr = pptr();
    char *old_gptr = gptr();
    char *old_egptr = egptr();

    size_t currentWritePosition = m_bufferSize;

    if(!GrowBuffer())
    {
        return endOfFile;
    }

    char* new_begin = m_buffer;
    char* new_end = new_begin + m_bufferSize;

    setp(new_begin + (old_pptr - old_begin) + 1, new_end);
    setg(new_begin, new_begin + (old_gptr - old_begin), new_begin + (old_egptr - old_begin));

    auto val = std::char_traits< char >::to_char_type(c);
    *(new_begin + currentWritePosition) = val;

    return c;
}
MemoryOutStream::MemoryOutStream(int const size)
    : m_capacity (0)
    , m_buffer (0)

{
    GrowBuffer(size);
}
Example #4
0
/**
  This function gets the file information from an open file descriptor,
  and stores it in a buffer allocated from pool.

  @param[in] FHand           File Handle.

  @return    A pointer to a buffer with file information or NULL is returned.

**/
EFI_FILE_INFO *
FileInfo (
  IN EFI_FILE_HANDLE                        FHand
  )
{
  EFI_STATUS                           Status;
  EFI_FILE_INFO                        *Buffer;
  UINTN                                BufferSize;

  //
  // Initialize for GrowBuffer loop
  //
  Buffer      = NULL;
  BufferSize  = SIZE_OF_EFI_FILE_INFO + 200;

  //
  // Call the real function
  //
  while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {
    Status = FHand->GetInfo (
                      FHand,
                      &gEfiFileInfoGuid,
                      &BufferSize,
                      Buffer
                      );
  }

  return Buffer;
}
Example #5
0
void WriteByte(WriteBuffer* buf, int v)
{
    if (!buf->buffer)
        InitBuffer(buf);

    GrowBuffer(buf, 1);

    *(buf->buffer + buf->len) = v;
    buf->len++;
}
Example #6
0
void WriteBytes(WriteBuffer* buf, const char* s, int l)
{
    if (!buf->buffer)
        InitBuffer(buf);

    GrowBuffer(buf, l);

    memcpy(buf->buffer+buf->len, s, l);
    buf->len += l;
}
Example #7
0
int CFastStringArray::Add(LPCTSTR str)
{
	if(m_count>=m_limit_count) GrowIndex();
	int len = _tcslen(str) + 1;
	if(m_size + len > m_limit_size) GrowBuffer();
	m_index_list[m_count] = m_size;
	memcpy(m_buffer + m_size, str, sizeof(TCHAR)*len);
	m_count++;
	m_size += len;
	return m_count-1;
}
bool SharedBufferMLGPU::EnsureMappedBuffer(size_t aBytes) {
  if (!mBuffer || (mMaxSize - mCurrentPosition < aBytes)) {
    if (!GrowBuffer(aBytes)) {
      return false;
    }
  }
  if (!mMapped && !Map()) {
    return false;
  }
  return true;
}
nsresult nsByteArray::AppendBuffer(const char *buffer, PRUint32 length)
{
  nsresult ret = NS_OK;
  if (m_bufferPos + length > m_bufferSize)
    ret = GrowBuffer(m_bufferPos + length, 1024);
  if (ret == NS_OK)
  {
    memcpy(m_buffer + m_bufferPos, buffer, length);
    m_bufferPos += length;
  }
  return ret;
}
bool SharedBufferMLGPU::Init() {
  // If we can't use buffer offset binding, we never allocated shared buffers.
  if (!mCanUseOffsetAllocation) {
    return true;
  }

  // If we can use offset binding, allocate an initial shared buffer now.
  if (!GrowBuffer(mDefaultSize)) {
    return false;
  }
  return true;
}
Example #11
0
void InsertBytes(WriteBuffer* buf, int pos, const char* s, int l)
{
    if (!buf->buffer)
        InitBuffer(buf);

    assert(pos<=buf->len);

    GrowBuffer(buf, l);

    memmove(buf->buffer+pos+l, buf->buffer+pos, buf->len-pos);
    memcpy(buf->buffer+pos, s, l);
    buf->len += l;
}
Example #12
0
void OverWriteBytes(WriteBuffer* buf, int pos, const char* s, int l)
{
    if (!buf->buffer)
        InitBuffer(buf);

    assert(pos<=buf->len);

    int growsize = max(pos+l - buf->len, 0);
    GrowBuffer(buf, growsize);

    memcpy(buf->buffer+pos, s, l);
    buf->len += growsize;
}
MemoryOutStream& MemoryOutStream::operator << (char const* txt)
{
    int const bytesLeft = m_capacity - (int)std::strlen(m_buffer);
    int const bytesRequired = (int)std::strlen(txt) + 1;

    if (bytesRequired > bytesLeft)
    {
        int const requiredCapacity = bytesRequired + m_capacity - bytesLeft;
        GrowBuffer(requiredCapacity);
    }

    std::strcat(m_buffer, txt);
    return *this;
}
Example #14
0
BOOL CGrowBuffer::SetUsedSize(UINT Size)
{
	if(Size>m_BufferSize)
	{
		if(!GrowBuffer(m_UsedSize+Size))
			return FALSE;
	}
	if(Size<m_UsedSize)
	{
		m_UsedSize=Size;
		UINT NodePos=BufferPosToNodePos(Size);
		for(UINT i=m_CurBufferNodeIndex;i>=NodePos&&Size;i--)
		{
			if(Size>m_BufferNodes[i].UsedSize)
			{
				Size-=m_BufferNodes[i].UsedSize;
				m_BufferNodes[i].UsedSize=0;
			}
			else
			{
				m_BufferNodes[i].UsedSize-=Size;
				Size=0;
			}
		}
		m_CurBufferNodeIndex=NodePos;
	}
	else if(Size>m_UsedSize)
	{
		m_UsedSize=Size;
		UINT NodePos=BufferPosToNodePos(Size);
		for(UINT i=m_CurBufferNodeIndex;i<=NodePos&&Size;i++)
		{
			if(Size>m_BufferNodes[i].BufferSize-m_BufferNodes[i].UsedSize)
			{
				Size-=m_BufferNodes[i].BufferSize-m_BufferNodes[i].UsedSize;
				m_BufferNodes[i].UsedSize=m_BufferNodes[i].BufferSize;
			}
			else
			{
				m_BufferNodes[i].UsedSize+=Size;
				Size=0;
			}
		}
		m_CurBufferNodeIndex=NodePos;
	}
	return TRUE;
}
Example #15
0
BACKUPCONTEXT *
AllocContext(DWORD cbBuffer)
{
    BACKUPCONTEXT *pbuc;

    pbuc = BackupAlloc(sizeof(*pbuc));

    if (pbuc != NULL) {
	RtlZeroMemory(pbuc, sizeof(*pbuc));
	pbuc->fStreamStart = TRUE;

	if (cbBuffer != 0 && !GrowBuffer(pbuc, cbBuffer)) {
	    BackupFree(pbuc);
	    pbuc = NULL;
	}
    }
    if (pbuc == NULL) {
	SetLastError(ERROR_NOT_ENOUGH_MEMORY);
    }
    return(pbuc);
}
Example #16
0
void pelet::UCharBufferedFileClass::AppendToLexeme(int minToGet) {
	if (NULL != File) {
		//printf("getting extra %d chars\n", minToGet);
		
		// since Limit points to the last character of the string (not past), we do +1 
		int validContentsCount = (Limit - TokenStart + 1); 
		int charsToGet = 0;
		UChar* startOfFreeSpace = 0;
		if (TokenStart > Buffer) {
			RemoveLeadingSlackSpace();
			startOfFreeSpace = Buffer + validContentsCount; 
			charsToGet = BufferCapacity - validContentsCount;
		}
		
		// if, after we removed the slack; we are still close to the edge; grow the buffer
		// choosing 20 because the longest PHP keywords is about this long
		if ((Current - Limit) < 20) {
			int oldCapacity = BufferCapacity;
			GrowBuffer(2 * oldCapacity);
			startOfFreeSpace = Buffer + validContentsCount;
			charsToGet = oldCapacity + (oldCapacity - validContentsCount);
		}

		// should read charsToGet bytes from file; not charsToFill
		// we want to get as much from the file as possible without re-allocation
		if (charsToGet > 0) {
			int read = u_file_read(startOfFreeSpace, charsToGet, File);
			Limit = Buffer + BufferCapacity - 1;
			if (read < charsToGet) {
				HasReachedEof = true;
				
				// insert null character as the lexers will look for null characters as EOF
				startOfFreeSpace[read] = '\0';
				Eof = startOfFreeSpace + read;
				u_fclose(File);
				File = NULL;
			}
		}
	}
}
Example #17
0
VOID *
LibGetVariableAndSize (
    IN CHAR16               *Name,
    IN EFI_GUID             *VendorGuid,
    OUT UINTN               *VarSize
    )
{
    EFI_STATUS              Status;
    VOID                    *Buffer;
    UINTN                   BufferSize;

    //
    // Initialize for GrowBuffer loop
    //

    Buffer = NULL;
    BufferSize = 100;

    //
    // Call the real function
    //

    while (GrowBuffer (&Status, &Buffer, BufferSize)) {
        Status = uefi_call_wrapper(
		    RT->GetVariable,
			5,
                    Name,
                    VendorGuid,
                    NULL,
                    &BufferSize,
                    Buffer
                    );
    }
    if (Buffer) {
        *VarSize = BufferSize;
    } else {
        *VarSize = 0;
    }
    return Buffer;
}
Example #18
0
INT
BackupWriteBuffer(BACKUPCONTEXT *pbuc, BACKUPIOFRAME *pbif)
{
    DWORD cbrequest;

    // allocate a buffer for data storage

    if (pbuc->fStreamStart) {
	pbuc->fStreamStart = FALSE;

	if (pbuc->cbBuffer < pbuc->head.Size.LowPart &&
	    !GrowBuffer(pbuc, pbuc->head.Size.LowPart)) {

	    return(BRB_FAIL);
	}
    }

    // Copy the stream into our allocated buffer

    cbrequest = min(pbif->cbRequest,
		    pbuc->head.Size.LowPart -
			pbuc->liStreamOffset.LowPart + pbuc->cbHeader);

    RtlCopyMemory(
	pbuc->pBuffer + pbuc->liStreamOffset.LowPart - pbuc->cbHeader,
	pbif->pIoBuffer,
	cbrequest);

    ReportTransfer(pbuc, pbif, cbrequest);

    // Tell caller if the entire stream is in our buffer

    if (pbuc->liStreamOffset.LowPart ==
	pbuc->head.Size.LowPart + pbuc->cbHeader) {
	return(BRB_DONE);
    }
    return(BRB_MORE);
}
Example #19
0
EFI_MEMORY_DESCRIPTOR *
LibMemoryMap (
    OUT UINTN               *NoEntries,
    OUT UINTN               *MapKey,
    OUT UINTN               *DescriptorSize,
    OUT UINT32              *DescriptorVersion
    )
{
    EFI_STATUS              Status;
    EFI_MEMORY_DESCRIPTOR   *Buffer;
    UINTN                   BufferSize;

    //
    // Initialize for GrowBuffer loop
    //

    Buffer = NULL;
    BufferSize = sizeof(EFI_MEMORY_DESCRIPTOR);

    //
    // Call the real function
    //

    while (GrowBuffer (&Status, (VOID **) &Buffer, BufferSize)) {
        Status = uefi_call_wrapper(BS->GetMemoryMap, 5, &BufferSize, Buffer, MapKey, DescriptorSize, DescriptorVersion);
    }

    //
    // Convert buffer size to NoEntries
    //

    if (!EFI_ERROR(Status)) {
        *NoEntries = BufferSize / *DescriptorSize;
    }

    return Buffer;
}
Example #20
0
BOOL
BackupReadAlternateData(HANDLE hFile, BACKUPCONTEXT *pbuc, BACKUPIOFRAME *pbif)
{
    // ALT_DATA is Macintosh stream data & other data streams.

    if (pbuc->fStreamStart) {
	NTSTATUS Status;
	FILE_STREAM_INFORMATION *pfsi;
	IO_STATUS_BLOCK iosb;

	// allocate a buffer big enough to hold all the necessary data.

	if (!pbuc->fBufferReady) {
	    while (TRUE) {
		Status = NtQueryInformationFile(
			    hFile,
			    &iosb,
			    pbuc->pBuffer,
			    pbuc->cbBuffer,
			    FileStreamInformation);

		if (NT_SUCCESS(Status) && iosb.Information != 0) {
		    pbuc->iBuffer = 0;
		    pbuc->fBufferReady = TRUE;
		    break;
		}
		if (!BufferOverflow(Status)) {
		    return(TRUE);	// No alt. streams, do next stream type
		}
		if (!GrowBuffer(pbuc, pbuc->cbBuffer * 2)) {
		    return(FALSE);	// No memory
		}
		// else grow succeeded
	    }
	}

	pbuc->hAlternate = NULL;
	pbuc->fStreamStart = FALSE;
	pfsi = (FILE_STREAM_INFORMATION *) &pbuc->pBuffer[pbuc->iBuffer];

	// Check StreamName for default data stream and skip if found
	// Checking StreamNameLength for <= 1 character is OFS specific!
	// Checking StreamName[1] for a colon is NTFS specific!

	if (pfsi->StreamNameLength <= sizeof(WCHAR) ||
	    pfsi->StreamName[1] == ':') {
	    if (pfsi->NextEntryOffset == 0) {
		return(TRUE);		// No more, do next stream type
	    }
	    pbuc->iBuffer += pfsi->NextEntryOffset;
	}
	pbuc->head.Size.LowPart = 1;
    }
    else if (pbuc->hAlternate == NULL) {
	NTSTATUS Status;
	FILE_STREAM_INFORMATION *pfsi;
	UNICODE_STRING strName;
	OBJECT_ATTRIBUTES oa;
	IO_STATUS_BLOCK iosb;

	pbuc->head.Size.LowPart = 0;
	pbuc->head.Size.HighPart = 0;

	pfsi = (FILE_STREAM_INFORMATION *) &pbuc->pBuffer[pbuc->iBuffer];

	strName.Length = (USHORT) pfsi->StreamNameLength;
	strName.MaximumLength = strName.Length;
	strName.Buffer = pfsi->StreamName;

	InitializeObjectAttributes(
		 &oa,
		 &strName,
		 OBJ_CASE_INSENSITIVE,
		 hFile,
		 NULL);

	Status = NtOpenFile(
		    &pbuc->hAlternate,
		    FILE_GENERIC_READ,
		    &oa,
		    &iosb,
		    FILE_SHARE_READ|FILE_SHARE_WRITE,
		    FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT);

	if (!NT_SUCCESS(Status)) {
	    pbuc->iBuffer += pfsi->NextEntryOffset;
	    if (pfsi->NextEntryOffset != 0) {
		pbuc->head.Size.LowPart = 1;
		pbuc->fMultiStreamType = TRUE;	// more to come
	    }
	    return(TRUE);
	}

	pbuc->head.dwStreamId = mwStreamList[pbuc->StreamIndex];
	pbuc->head.dwStreamAttributes = STREAM_NORMAL_ATTRIBUTE;
	pbuc->head.dwStreamNameSize = pfsi->StreamNameLength;

	pbuc->cbHeader = CB_NAMELESSHEADER + pfsi->StreamNameLength;

	RtlCopyMemory(
	    pbuc->head.cStreamName,
	    pfsi->StreamName,
	    pfsi->StreamNameLength);

	pbuc->head.Size.LowPart = GetFileSize(
		pbuc->hAlternate,
		&pbuc->head.Size.HighPart);

	if (pfsi->NextEntryOffset != 0) {
	    pbuc->iBuffer += pfsi->NextEntryOffset;
	    pbuc->fMultiStreamType = TRUE;	// more to come after this one
	}
    }
    else if (pbuc->liStreamOffset.HighPart != 0 ||
	    pbuc->liStreamOffset.LowPart >= pbuc->cbHeader) {

	if (pbuc->liStreamOffset.LowPart == pbuc->cbHeader &&
	    pbuc->liStreamOffset.HighPart == 0) {

	    // if we can't lock all records, return an error
	    if (!LockFile(pbuc->hAlternate, 0, 0, 0xffffffff, 0xffffffff)) {
		return(FALSE);
	    }
	}
	return(BackupReadStream(pbuc->hAlternate, pbuc, pbif));
    }
    return(TRUE);
}
Example #21
0
PPARSERDATA
AllocParserData(
    VOID
    )

/*++

Routine Description:

    Allocate memory to hold parser data

Arguments:

    NONE

Return Value:

    Pointer to allocated parser data structure
    NULL if there is an error

--*/

{
    PPARSERDATA pParserData;
    HANDLE      hheap;

    //
    // Create a heap and allocate memory space from it
    //

    if (! (hheap = HeapCreate(0, 4096, 0)) ||
        ! (pParserData = HeapAlloc(hheap, HEAP_ZERO_MEMORY, sizeof(PARSERDATA))))
    {
        Error(("Memory allocation failed\n"));
        if (hheap) {
            HeapDestroy(hheap);
        }
        return NULL;
    }

    pParserData->hheap = hheap;
    pParserData->unit = UNIT_POINT;
    pParserData->allowHexStr = TRUE;
    pParserData->numPlanes = pParserData->bitsPerPlane = 1;
    pParserData->checksum = 0;

    //
    // Buffers used for storing keyword, option, translation, and value.
    //
    
    SetBuffer(&pParserData->keyword, pParserData->keywordBuffer, MAX_KEYWORD_LEN);
    SetBuffer(&pParserData->option,  pParserData->optionBuffer, MAX_OPTION_LEN);
    SetBuffer(&pParserData->xlation, pParserData->xlationBuffer, MAX_XLATION_LEN);
    
    if (GrowBuffer(&pParserData->value) != ERR_NONE) {

        FreeParserData(pParserData);
        return NULL;
    }

    return pParserData;
}
Example #22
0
BOOL
BackupReadSecurityData(HANDLE hFile, BACKUPCONTEXT *pbuc, BACKUPIOFRAME *pbif)
{
    if (!pbif->fProcessSecurity) {
	return(TRUE);
    }
    if (pbuc->fStreamStart) {
	while (TRUE) {
	    NTSTATUS Status;
	    DWORD cbSecurityInfo;

	    // First try to read all the security data

	    RtlZeroMemory(pbuc->pBuffer, pbuc->cbBuffer);

	    Status = NtQuerySecurityObject(
			hFile,
			OWNER_SECURITY_INFORMATION |
			    GROUP_SECURITY_INFORMATION |
			    DACL_SECURITY_INFORMATION |
			    SACL_SECURITY_INFORMATION,
			pbuc->pBuffer,
			pbuc->cbBuffer,
			&cbSecurityInfo);

	    if (!NT_SUCCESS(Status) && !BufferOverflow(Status)) {

		// Now just try everything but SACL

		Status = NtQuerySecurityObject(
			    hFile,
			    OWNER_SECURITY_INFORMATION |
				GROUP_SECURITY_INFORMATION |
				DACL_SECURITY_INFORMATION,
			    pbuc->pBuffer,
			    pbuc->cbBuffer,
			    &cbSecurityInfo);
	    }
#if 0
	    if (!NT_SUCCESS(Status) && !BufferOverflow(Status)) {

		// Now just try to read the DACL

		Status = NtQuerySecurityObject(
			    hFile,
			    OWNER_SECURITY_INFORMATION |
				GROUP_SECURITY_INFORMATION,
			    pbuc->pBuffer,
			    pbuc->cbBuffer,
			    &cbSecurityInfo);
	    }
#endif
	    if (NT_SUCCESS(Status)) {
		pbuc->fBufferReady = TRUE;
		break;
	    }
	    if (!BufferOverflow(Status)) {
		return(TRUE);	// No Security info, do next stream type
	    }
	    if (!GrowBuffer(pbuc, cbSecurityInfo)) {
		return(FALSE);	// No memory
	    }
	    // else grow succeeded
	}

	pbuc->head.dwStreamId = mwStreamList[pbuc->StreamIndex];
	pbuc->head.dwStreamAttributes = STREAM_CONTAINS_SECURITY;
	pbuc->head.dwStreamNameSize = 0;

	pbuc->cbHeader = CB_NAMELESSHEADER;

	pbuc->head.Size.LowPart = RtlLengthSecurityDescriptor(pbuc->pBuffer);
	pbuc->head.Size.HighPart = 0;

	pbuc->fStreamStart = FALSE;
    }
    else if (pbuc->liStreamOffset.HighPart != 0 ||
	     pbuc->liStreamOffset.LowPart >= pbuc->cbHeader) {
	BackupReadBuffer(pbuc, pbif);
    }
    return(TRUE);
}
char * nsMsgLineStreamBuffer::ReadNextLine(nsIInputStream * aInputStream, PRUint32 &aNumBytesInLine, bool &aPauseForMoreData, nsresult *prv, bool addLineTerminator)
{
  // try to extract a line from m_inputBuffer. If we don't have an entire line, 
  // then read more bytes out from the stream. If the stream is empty then wait
  // on the monitor for more data to come in.
  
  NS_PRECONDITION(m_dataBuffer && m_dataBufferSize > 0, "invalid input arguments for read next line from input");

  if (prv)
    *prv = NS_OK;
  // initialize out values
  aPauseForMoreData = PR_FALSE;
  aNumBytesInLine = 0;
  char * endOfLine = nsnull;
  char * startOfLine = m_dataBuffer+m_startPos;
  
  if (m_numBytesInBuffer > 0) // any data in our internal buffer?
    endOfLine = PL_strchr(startOfLine, m_lineToken); // see if we already have a line ending...
  
  // it's possible that we got here before the first time we receive data from the server
  // so aInputStream will be nsnull...
  if (!endOfLine && aInputStream) // get some more data from the server
  {
    nsresult rv;
    PRUint32 numBytesInStream = 0;
    PRUint32 numBytesCopied = 0;
    bool nonBlockingStream;
    aInputStream->IsNonBlocking(&nonBlockingStream);
    rv = aInputStream->Available(&numBytesInStream);
    if (NS_FAILED(rv))
    {
      if (prv)
        *prv = rv;
      aNumBytesInLine = -1;
      return nsnull;
    }
    if (!nonBlockingStream && numBytesInStream == 0) // if no data available,
      numBytesInStream = m_dataBufferSize / 2; // ask for half the data buffer size.

    // if the number of bytes we want to read from the stream, is greater than the number
    // of bytes left in our buffer, then we need to shift the start pos and its contents
    // down to the beginning of m_dataBuffer...
    PRUint32 numFreeBytesInBuffer = m_dataBufferSize - m_startPos - m_numBytesInBuffer;
    if (numBytesInStream >= numFreeBytesInBuffer)
    {
      if (m_startPos)
      {
        memmove(m_dataBuffer, startOfLine, m_numBytesInBuffer);
        // make sure the end of the buffer is terminated
        m_dataBuffer[m_numBytesInBuffer] = '\0';
        m_startPos = 0;
        startOfLine = m_dataBuffer;
        numFreeBytesInBuffer = m_dataBufferSize - m_numBytesInBuffer;
        //printf("moving data in read line around because buffer filling up\n");
      }
      // If we didn't make enough space (or any), grow the buffer
      if (numBytesInStream >= numFreeBytesInBuffer)
      {
        PRInt32 growBy = (numBytesInStream - numFreeBytesInBuffer) * 2 + 1;
        // try growing buffer by twice as much as we need.
        nsresult rv = GrowBuffer(m_dataBufferSize + growBy);
        // if we can't grow the buffer, we have to bail.
        if (NS_FAILED(rv))
          return nsnull;
        startOfLine = m_dataBuffer;
        numFreeBytesInBuffer += growBy;
      }
      NS_ASSERTION(m_startPos == 0, "m_startPos should be 0 .....\n");
    }
    
    PRUint32 numBytesToCopy = NS_MIN(numFreeBytesInBuffer - 1 /* leave one for a null terminator */, numBytesInStream);
    if (numBytesToCopy > 0)
    {
      // read the data into the end of our data buffer
      char *startOfNewData = startOfLine + m_numBytesInBuffer;
      rv = aInputStream->Read(startOfNewData, numBytesToCopy, &numBytesCopied);
      if (prv)
        *prv = rv;
      PRUint32 i;
      for (i = 0; i < numBytesCopied; i++)  // replace nulls with spaces
      {
        if (!startOfNewData[i])
          startOfNewData[i] = ' ';
      }
      m_numBytesInBuffer += numBytesCopied;
      m_dataBuffer[m_startPos + m_numBytesInBuffer] = '\0';

      // okay, now that we've tried to read in more data from the stream,
      // look for another end of line character in the new data
      endOfLine = PL_strchr(startOfNewData, m_lineToken);
    }
  }
  
  // okay, now check again for endOfLine.
  if (endOfLine)
  {
    if (!m_eatCRLFs)
      endOfLine += 1; // count for LF or CR
    
    aNumBytesInLine = endOfLine - startOfLine;
    
    if (m_eatCRLFs && aNumBytesInLine > 0 && startOfLine[aNumBytesInLine-1] == '\r') // Remove the CR in a CRLF sequence
      aNumBytesInLine--;
    
    // PR_CALLOC zeros out the allocated line
    char* newLine = (char*) PR_CALLOC(aNumBytesInLine + (addLineTerminator ? MSG_LINEBREAK_LEN : 0) + 1);
    if (!newLine)
    {
      aNumBytesInLine = 0;
      aPauseForMoreData = PR_TRUE;
      return nsnull;
    }
    
    memcpy(newLine, startOfLine, aNumBytesInLine); // copy the string into the new line buffer
    if (addLineTerminator)
    {
      memcpy(newLine + aNumBytesInLine, MSG_LINEBREAK, MSG_LINEBREAK_LEN);
      aNumBytesInLine += MSG_LINEBREAK_LEN;
    }
    
    if (m_eatCRLFs)
      endOfLine += 1; // advance past LF or CR if we haven't already done so...
    
    // now we need to update the data buffer to go past the line we just read out. 
    m_numBytesInBuffer -= (endOfLine - startOfLine);
    if (m_numBytesInBuffer)
      m_startPos = endOfLine - m_dataBuffer;
    else
      m_startPos = 0;
    
    return newLine;
  }
  
  aPauseForMoreData = PR_TRUE;
  return nsnull; // if we somehow got here. we don't have another line in the buffer yet...need to wait for more data...
}
PRInt32  nsMsgLineBuffer::BufferInput(const char *net_buffer, PRInt32 net_buffer_size)
{
    int status = 0;
    if (m_bufferPos > 0 && m_buffer && m_buffer[m_bufferPos - 1] == '\r' &&
        net_buffer_size > 0 && net_buffer[0] != '\n') {
        /* The last buffer ended with a CR.  The new buffer does not start
           with a LF.  This old buffer should be shipped out and discarded. */
        PR_ASSERT(m_bufferSize > m_bufferPos);
        if (m_bufferSize <= m_bufferPos) return -1;
        status = ConvertAndSendBuffer();
        if (status < 0) 
           return status;
        m_bufferPos = 0;
    }
    while (net_buffer_size > 0)
    {
        const char *net_buffer_end = net_buffer + net_buffer_size;
        const char *newline = 0;
        const char *s;

        for (s = net_buffer; s < net_buffer_end; s++)
        {
          if (m_lookingForCRLF) {
            /* Move forward in the buffer until the first newline.
               Stop when we see CRLF, CR, or LF, or the end of the buffer.
               *But*, if we see a lone CR at the *very end* of the buffer,
               treat this as if we had reached the end of the buffer without
               seeing a line terminator.  This is to catch the case of the
               buffers splitting a CRLF pair, as in "FOO\r\nBAR\r" "\nBAZ\r\n".
            */
            if (*s == '\r' || *s == '\n') {
              newline = s;
              if (newline[0] == '\r') {
                if (s == net_buffer_end - 1) {
                  /* CR at end - wait for the next character. */
                  newline = 0;
                  break;
                }
                else if (newline[1] == '\n') {
                  /* CRLF seen; swallow both. */
                  newline++;
                }
              }
              newline++;
              break;
            }
          }
          else {
            /* if not looking for a CRLF, stop at CR or LF.  (for example, when parsing the newsrc file).  this fixes #9896, where we'd lose the last line of anything we'd parse that used CR as the line break. */
            if (*s == '\r' || *s == '\n') {
              newline = s;
              newline++;
              break;
            }
          }
        }
        
        /* Ensure room in the net_buffer and append some or all of the current
           chunk of data to it. */
        {
            const char *end = (newline ? newline : net_buffer_end);
            PRUint32 desired_size = (end - net_buffer) + m_bufferPos + 1;
            
            if (desired_size >= m_bufferSize)
            {
                status = GrowBuffer (desired_size, 1024);
                if (status < 0) 
                    return status;
            }
            memcpy (m_buffer + m_bufferPos, net_buffer, (end - net_buffer));
            m_bufferPos += (end - net_buffer);
        }
        
        /* Now m_buffer contains either a complete line, or as complete
           a line as we have read so far.
           
           If we have a line, process it, and then remove it from `m_buffer'.
           Then go around the loop again, until we drain the incoming data.
           */
        if (!newline)
            return 0;
        
        status = ConvertAndSendBuffer();
        if (status < 0) return status;
        
        net_buffer_size -= (newline - net_buffer);
        net_buffer = newline;
        m_bufferPos = 0;
    }
    return 0;
}
Example #25
0
BOOL
BackupReadEaData(HANDLE hFile, BACKUPCONTEXT *pbuc, BACKUPIOFRAME *pbif)
{
    if (pbuc->fStreamStart) {
	IO_STATUS_BLOCK iosb;

	while (TRUE) {
	    NTSTATUS Status;
	    FILE_EA_INFORMATION fei;

	    Status = NtQueryEaFile(
			hFile,
			&iosb,
			pbuc->pBuffer,
			pbuc->cbBuffer,
			FALSE,
			NULL,
			0,
			0,
			(BOOLEAN) TRUE);
	    if (NT_SUCCESS(Status) && iosb.Information != 0) {
		pbuc->fBufferReady = TRUE;
		break;
	    }
	    if (!BufferOverflow(Status)) {
		return(TRUE);	// No Eas, do next stream type
	    }
	    Status = NtQueryInformationFile(
			hFile,
			&iosb,
			&fei,
			sizeof(fei),
			FileEaInformation);

	    if (!NT_SUCCESS(Status)) {
		return(TRUE);	// No Eas, do next stream type
	    }
	    if (!GrowBuffer(pbuc, (fei.EaSize * 5) / 4)) {
		pbuc->fAccessError = TRUE;
		return(FALSE);	// No memory
	    }
	    // else grow succeeded
	}

	pbuc->head.dwStreamId = mwStreamList[pbuc->StreamIndex];
	pbuc->head.dwStreamAttributes = STREAM_NORMAL_ATTRIBUTE;
	pbuc->head.dwStreamNameSize = 0;

	pbuc->cbHeader = CB_NAMELESSHEADER;

	pbuc->head.Size.HighPart = 0;
	pbuc->head.Size.LowPart = iosb.Information;

	pbuc->fStreamStart = FALSE;
    }
    else if (pbuc->liStreamOffset.HighPart != 0 ||
	     pbuc->liStreamOffset.LowPart >= pbuc->cbHeader) {
	BackupReadBuffer(pbuc, pbif);
    }
    return(TRUE);
}