예제 #1
0
int
process_rings (int netin, int netout, int netex, int ttyin, int ttyout,
	       int poll)
	/* If poll == 0, then block until something to do */
{
  int c;
  /* One wants to be a bit careful about setting returnValue
   * to one, since a one implies we did some useful work,
   * and therefore probably won't be called to block next
   * time (TN3270 mode only).
   */
  int returnValue = 0;
  static struct timeval TimeValue = { 0, 0 };
  int maxfd = -1;
  int tmp;

  if ((netout || netin || netex) && net > maxfd)
    maxfd = net;
  if (ttyout && tout > maxfd)
    maxfd = tout;
  if (ttyin && tin > maxfd)
    maxfd = tin;
  tmp = howmany (maxfd + 1, NFDBITS) * sizeof (fd_mask);
  if (tmp > fdsn)
    {
      if (ibitsp)
	free (ibitsp);
      if (obitsp)
	free (obitsp);
      if (xbitsp)
	free (xbitsp);
      fdsn = tmp;
      if ((ibitsp = (fd_set *) malloc (fdsn)) == NULL)
	err (1, "malloc");
      if ((obitsp = (fd_set *) malloc (fdsn)) == NULL)
	err (1, "malloc");
      if ((xbitsp = (fd_set *) malloc (fdsn)) == NULL)
	err (1, "malloc");
      memset (ibitsp, 0, fdsn);
      memset (obitsp, 0, fdsn);
      memset (xbitsp, 0, fdsn);
    }

  if (netout)
    FD_SET (net, obitsp);
  if (ttyout)
    FD_SET (tout, obitsp);
  if (ttyin)
    FD_SET (tin, ibitsp);
  if (netin)
    FD_SET (net, ibitsp);
  if (netex)
    FD_SET (net, xbitsp);

  if ((c = select (maxfd + 1, ibitsp, obitsp, xbitsp,
		   (poll == 0) ? (struct timeval *) 0 : &TimeValue)) < 0)
    {
      if (c == -1)
	{
	  /*
	   * we can get EINTR if we are in line mode,
	   * and the user does an escape (TSTP), or
	   * some other signal generator.
	   */
	  if (errno == EINTR)
	    {
	      return 0;
	    }
#	    if defined(TN3270)
	  /*
	   * we can get EBADF if we were in transparent
	   * mode, and the transcom process died.
	   */
	  if (errno == EBADF)
	    {
	      /*
	       * zero the bits (even though kernel does it)
	       * to make sure we are selecting on the right
	       * ones.
	       */
	      memset (ibitsp, 0, fdsn);
	      memset (obitsp, 0, fdsn);
	      memset (xbitsp, 0, fdsn);
	      return 0;
	    }
#	    endif /* defined(TN3270) */
	  /* I don't like this, does it ever happen? */
	  printf ("sleep(5) from telnet, after select\r\n");
	  sleep (5);
	}
      return 0;
    }

  /*
   * Any urgent data?
   */
  if (FD_ISSET (net, xbitsp))
    {
      FD_CLR (net, xbitsp);
      SYNCHing = 1;
      (void) ttyflush (1);	/* flush already enqueued data */
    }

  /*
   * Something to read from the network...
   */
  if (FD_ISSET (net, ibitsp))
    {
      int canread;

      FD_CLR (net, ibitsp);
      canread = ring_empty_consecutive (&netiring);
#if	!defined(SO_OOBINLINE)
      /*
       * In 4.2 (and some early 4.3) systems, the
       * OOB indication and data handling in the kernel
       * is such that if two separate TCP Urgent requests
       * come in, one byte of TCP data will be overlaid.
       * This is fatal for Telnet, but we try to live
       * with it.
       *
       * In addition, in 4.2 (and...), a special protocol
       * is needed to pick up the TCP Urgent data in
       * the correct sequence.
       *
       * What we do is:  if we think we are in urgent
       * mode, we look to see if we are "at the mark".
       * If we are, we do an OOB receive.  If we run
       * this twice, we will do the OOB receive twice,
       * but the second will fail, since the second
       * time we were "at the mark", but there wasn't
       * any data there (the kernel doesn't reset
       * "at the mark" until we do a normal read).
       * Once we've read the OOB data, we go ahead
       * and do normal reads.
       *
       * There is also another problem, which is that
       * since the OOB byte we read doesn't put us
       * out of OOB state, and since that byte is most
       * likely the TELNET DM (data mark), we would
       * stay in the TELNET SYNCH (SYNCHing) state.
       * So, clocks to the rescue.  If we've "just"
       * received a DM, then we test for the
       * presence of OOB data when the receive OOB
       * fails (and AFTER we did the normal mode read
       * to clear "at the mark").
       */
      if (SYNCHing)
	{
	  int atmark;
	  static int bogus_oob = 0, first = 1;

	  ioctl (net, SIOCATMARK, (char *) &atmark);
	  if (atmark)
	    {
	      c = recv (net, netiring.supply, canread, MSG_OOB);
	      if ((c == -1) && (errno == EINVAL))
		{
		  c = recv (net, netiring.supply, canread, 0);
		  if (clocks.didnetreceive < clocks.gotDM)
		    {
		      SYNCHing = stilloob (net);
		    }
		}
	      else if (first && c > 0)
		{
		  /*
		   * Bogosity check.  Systems based on 4.2BSD
		   * do not return an error if you do a second
		   * recv(MSG_OOB).  So, we do one.  If it
		   * succeeds and returns exactly the same
		   * data, then assume that we are running
		   * on a broken system and set the bogus_oob
		   * flag.  (If the data was different, then
		   * we probably got some valid new data, so
		   * increment the count...)
		   */
		  int i;
		  i = recv (net, netiring.supply + c, canread - c, MSG_OOB);
		  if (i == c &&
		      memcmp (netiring.supply, netiring.supply + c, i) == 0)
		    {
		      bogus_oob = 1;
		      first = 0;
		    }
		  else if (i < 0)
		    {
		      bogus_oob = 0;
		      first = 0;
		    }
		  else
		    c += i;
		}
	      if (bogus_oob && c > 0)
		{
		  int i;
		  /*
		   * Bogosity.  We have to do the read
		   * to clear the atmark to get out of
		   * an infinate loop.
		   */
		  i = read (net, netiring.supply + c, canread - c);
		  if (i > 0)
		    c += i;
		}
	    }
	  else
	    {
	      c = recv (net, netiring.supply, canread, 0);
	    }
	}
      else
	{
	  c = recv (net, netiring.supply, canread, 0);
	}
      settimer (didnetreceive);
#else /* !defined(SO_OOBINLINE) */
      c = recv (net, (char *) netiring.supply, canread, 0);
#endif /* !defined(SO_OOBINLINE) */
      if (c < 0 && errno == EWOULDBLOCK)
	{
	  c = 0;
	}
      else if (c <= 0)
	{
	  return -1;
	}
      if (netdata)
	{
	  Dump ('<', netiring.supply, c);
	}
      if (c)
	ring_supplied (&netiring, c);
      returnValue = 1;
    }

  /*
   * Something to read from the tty...
   */
  if (FD_ISSET (tin, ibitsp))
    {
      FD_CLR (tin, ibitsp);
      c = TerminalRead (ttyiring.supply, ring_empty_consecutive (&ttyiring));
      if (c < 0 && errno == EIO)
	c = 0;
      if (c < 0 && errno == EWOULDBLOCK)
	{
	  c = 0;
	}
      else
	{
	  /* EOF detection for line mode!!!! */
	  if ((c == 0) && MODE_LOCAL_CHARS (globalmode) && isatty (tin))
	    {
	      /* must be an EOF... */
	      *ttyiring.supply = termEofChar;
	      c = 1;
	    }
	  if (c <= 0)
	    {
	      return -1;
	    }
	  if (termdata)
	    {
	      Dump ('<', ttyiring.supply, c);
	    }
	  ring_supplied (&ttyiring, c);
	}
      returnValue = 1;		/* did something useful */
    }

  if (FD_ISSET (net, obitsp))
    {
      FD_CLR (net, obitsp);
      returnValue |= netflush ();
    }
  if (FD_ISSET (tout, obitsp))
    {
      FD_CLR (tout, obitsp);
      returnValue |= (ttyflush (SYNCHing | flushout) > 0);
    }

  return returnValue;
}
예제 #2
0
	bool OLE::writeToDevice (void)
	{
	CHECK_DEVICE;

	#ifdef DEBUG_OBJECT
		m_device->debug ("\n>>>> OLE::writeToDevice <<<<\n");
	#endif

	#ifdef DEBUG_OBJECT
		Dump (zero);

		switch (m_objectType)
		{
		case OLEType::Static:
			m_device->debug ("\tobjectType: 1 - static\n");
			break;
		case OLEType::Embedded:
			m_device->debug ("\tobjectType: 2 - embedded\n");
			break;
		case OLEType::Link:
			m_device->debug ("\tobjectType: 3 - link\n");
			break;
		}

		Dump (indent);
		Dump (width);
		Dump (height);
		Dump (zero2);
		Dump (numDataBytes);
		Dump (zero3);
		Dump (objectName);
		Dump (zero4);
		Dump (numHeaderBytes);
		Dump (zero5);
		Dump (widthScaledRel1000);
		Dump (heightScaledRel1000);
	#endif

		// write header
		if (!OLEGenerated::writeToDevice ())
			return false;

		// write data
		if (!m_device->writeInternal (m_externalObject, m_externalObjectSize))
			return false;

		return true;
	}
예제 #3
0
	bool PageLayout::readFromDevice (void)
	{
	CHECK_DEVICE;

	#ifdef DEBUG_PAGELAYOUT
		m_device->debug ("\n<<<< PageLayout::readFromDevice >>>>\n");
	#endif

		int numPageLayoutPages = m_header->getNumPageSectionProperty ();

	#ifdef DEBUG_PAGELAYOUT
		m_device->debug ("num pageLayoutPages: ", numPageLayoutPages);
	#endif

		// no PageLayout
		if (numPageLayoutPages == 0)
			return true;
		else if (numPageLayoutPages > 1)
			ErrorAndQuit (Error::InvalidFormat, "invalid #pageLayoutPages\n");

		// seek to the PageLayout in the file
		if (!m_device->seekInternal (m_header->getPageSectionProperty () * 128, SEEK_SET))
			ErrorAndQuit (Error::FileError, "could not seek to pageLayout\n");

		if (!PageLayoutGenerated::readFromDevice ())
			return false;

	#ifdef DEBUG_PAGELAYOUT
		Dump (magic102);
		Dump (magic512);

		Dump (pageHeight);
		Dump (pageWidth);
		Dump (pageNumberStart);
		Dump (topMargin);
		Dump (textHeight);
		Dump (leftMargin);
		Dump (textWidth);

		Dump (magic256);

		Dump (headerFromTop);
		Dump (footerFromTop);

		Dump (magic720);
		Dump (zero);
		Dump (magic1080);
		Dump (unknown);
		Dump (zero2);
	#endif

		#define UpdateModifiedCount(variable) if (m_##variable != variable##Default) m_numModified++
		UpdateModifiedCount (magic102);
		UpdateModifiedCount (magic512);
		UpdateModifiedCount (pageHeight);
		UpdateModifiedCount (pageWidth);
		UpdateModifiedCount (pageNumberStart);
		UpdateModifiedCount (topMargin);
		UpdateModifiedCount (textHeight);
		UpdateModifiedCount (leftMargin);
		UpdateModifiedCount (textWidth);
		UpdateModifiedCount (magic256);
		UpdateModifiedCount (headerFromTop);
		UpdateModifiedCount (footerFromTop);
		UpdateModifiedCount (magic720);
		UpdateModifiedCount (zero);
		UpdateModifiedCount (magic1080);
		//UpdateModifiedCount (unknown);	// no reliable default for unknown
		UpdateModifiedCount (zero2);
		#undef UpdateModifiedCount

		return true;
	}
예제 #4
0
파일: VM.cpp 프로젝트: tsmigiel/compiler
std::ostream &
Register::DumpTrace(std::ostream &o, VM &vm, const int bb, const int pc)
{
	return Dump(o) << "=" << Get(vm) << " ";
}
예제 #5
0
void LogCompStr::AssertValid() {
  if (dwCursorPos > GetCompCharCount()) {
    Dump();
    DebugPrintA("dwCursorPos: %u\n", dwCursorPos);
    DebugPrintA("GetCompCharCount(): %u\n", GetCompCharCount());
    assert(0);
  }
  if (comp_attr.size()) {
    if (comp_attr.size() != comp_str.size()) {
      Dump();
      DebugPrintA("comp_attr.size(): %u\n", (int)comp_attr.size());
      DebugPrintA("comp_str.size(): %u\n", (int)comp_str.size());
      assert(0);
    }
  }
  if (comp_clause.size()) {
    if (comp_clause[0] != 0) {
      Dump();
      assert(0);
    }
    if (comp_clause[comp_clause.size() - 1] != GetCompCharCount()) {
      Dump();
      assert(0);
    }
    if (extra.iClause > (DWORD)comp_clause.size()) {
      Dump();
      DebugPrintA("extra.iClause: %u\n", extra.iClause);
      DebugPrintA("comp_clause.size(): %u\n", (int)comp_clause.size());
      assert(0);
    }
    for (size_t i = 1; i < comp_clause.size(); ++i) {
      if (comp_clause[i] > GetCompCharCount()) {
        Dump();
        assert(0);
      }
      if (comp_clause[i - 1] > comp_clause[i]) {
        Dump();
        assert(0);
      }
    }
  }
  if (result_read_clause.size()) {
    if (result_read_clause[0] != 0) {
      Dump();
      assert(0);
    }
    for (size_t i = 1; i < result_read_clause.size(); ++i) {
      if (result_read_clause[i] > (DWORD)result_read_str.size()) {
        Dump();
        assert(0);
      }
      if (result_read_clause[i - 1] > result_read_clause[i]) {
        Dump();
        assert(0);
      }
    }
  }
  if (result_clause.size()) {
    if (result_clause[0] != 0) {
      Dump();
      assert(0);
    }
    for (size_t i = 1; i < result_clause.size(); ++i) {
      if (result_clause[i] > (DWORD)result_str.size()) {
        Dump();
        assert(0);
      }
      if (result_clause[i - 1] > result_clause[i]) {
        Dump();
        assert(0);
      }
    }
  }
  if (extra.hiragana_clauses.size() != extra.typing_clauses.size()) {
    Dump();
    assert(0);
  }
} // LogCompStr::AssertValid
예제 #6
0
static FBLinearPtr
localAllocateOffscreenLinear(
    ScreenPtr pScreen, 
    int length,
    int gran,
    MoveLinearCallbackProcPtr moveCB,
    RemoveLinearCallbackProcPtr removeCB,
    pointer privData
){
   FBManagerPtr offman;
   FBLinearLinkPtr link;
   FBAreaPtr area;
   FBLinearPtr linear = NULL;
   BoxPtr extents;
   int w, h, pitch;

   offman = pScreen->devPrivates[xf86FBScreenIndex].ptr;

   /* Try to allocate from linear memory first...... */
#ifdef DEBUG
   ErrorF("ALLOCATING LINEAR\n");
#endif
   if ((linear = AllocateLinear(offman, length, gran, privData)))
   	return linear;

#ifdef DEBUG
   ErrorF("NOPE, ALLOCATING AREA\n");
#endif

   if(!(link = xalloc(sizeof(FBLinearLink))))
     return NULL;

   /* No linear available, so try and pinch some from the XY areas */
   extents = REGION_EXTENTS(pScreen, offman->InitialBoxes);
   pitch = extents->x2 - extents->x1;

   if(gran && ((gran > pitch) || (pitch % gran))) {
	/* we can't match the specified alignment with XY allocations */
	xfree(link);
	return NULL;
   }

   if(length < pitch) { /* special case */
	w = length;
	h = 1;
   } else {
	w = pitch;
	h = (length + pitch - 1) / pitch;
   }

   if((area = localAllocateOffscreenArea(pScreen, w, h, gran, 
			moveCB   ? LinearMoveCBWrapper   : NULL, 
			removeCB ? LinearRemoveCBWrapper : NULL, 
			privData))) 
   {
	link->area = area;
	link->free = 0;
	link->next = offman->LinearAreas;
	offman->LinearAreas = link;
	linear = &(link->linear);
	linear->pScreen = pScreen;
	linear->size = h * w;
	linear->offset = (pitch * area->box.y1) + area->box.x1;
	linear->granularity = gran;
	linear->MoveLinearCallback = moveCB;
	linear->RemoveLinearCallback = removeCB;
	linear->devPrivate.ptr = privData;
   } else 
	xfree(link);

#ifdef DEBUG
   Dump(offman->LinearAreas);
#endif

   return linear;
}
예제 #7
0
파일: rtdmp.c 프로젝트: conioh/os-design
void
Dump(
    HANDLE  Handle
    )
{
    NTSTATUS    status;
    PKEY_BASIC_INFORMATION KeyInformation;
    OBJECT_ATTRIBUTES ObjectAttributes;
    ULONG   NamePos;
    ULONG   index;
    STRING  enumname;
    HANDLE  WorkHandle;
    ULONG   ResultLength;
    static  char buffer[WORK_SIZE];
    PUCHAR  p;

    KeyInformation = (PKEY_BASIC_INFORMATION)buffer;
    NamePos = WorkName.Length;

    //
    // Print name of node we are about to dump out
    //
    print(&WorkName);
    printf("::\n\n");

    //
    // Print out node's values
    //
    DumpValues(Handle);

    //
    // Enumerate node's children and apply ourselves to each one
    //

    for (index = 0; TRUE; index++) {

        RtlZeroMemory(KeyInformation, WORK_SIZE);
        status = NtEnumerateKey(
                    Handle,
                    index,
                    KeyBasicInformation,
                    KeyInformation,
                    WORK_SIZE,
                    &ResultLength
                    );

        if (status == STATUS_NO_MORE_ENTRIES) {

            WorkName.Length = NamePos;
            return;

        } else if (!NT_SUCCESS(status)) {

            printf("rtdmp: dump1: status = %08lx\n", status);
            exit(1);

        }

        enumname.Buffer = &(KeyInformation->Name[0]);
        enumname.Length = KeyInformation->NameLength;
        enumname.MaximumLength = KeyInformation->NameLength;

        p = WorkName.Buffer;
        p += WorkName.Length;
        *p = '\\';
        p++;
        *p = '\0';
        WorkName.Length += 2;

        RtlAppendStringToString((PSTRING)&WorkName, (PSTRING)&enumname);

        InitializeObjectAttributes(
            &ObjectAttributes,
            &enumname,
            0,
            Handle,
            NULL
            );
        ObjectAttributes.Attributes |= OBJ_CASE_INSENSITIVE;

        status = NtOpenKey(
                    &WorkHandle,
                    MAXIMUM_ALLOWED,
                    &ObjectAttributes
                    );
        if (!NT_SUCCESS(status)) {
            printf("rtdmp: dump2: %08lx\n", status);
            exit(1);
        }

        Dump(WorkHandle);
        NtClose(WorkHandle);
        WorkName.Length = NamePos;
    }
}
예제 #8
0
/**
    \fn open

*/
uint8_t    OpenDMLHeader::open(const char *name)
{
uint8_t badAvi=0;
uint32_t rd;

	printf("** opening OpenDML files **");	
        
	_fd=ADM_fopen(name,"rb");
	if(!_fd)
	{
		printf("\n cannot open %s \n",name);
		return 0;
	}
        myName=ADM_strdup(name);
#define CLR(x)              memset(& x,0,sizeof(  x));

          CLR( _videostream);
          CLR( _mainaviheader);
	      _isvideopresent=1;
	      _isaudiopresent=0;    	     	      	 	      
	      
		_nbTrack=0;
		riffParser *parser=new riffParser(name);
		
		if(MKFCC('R','I','F','F')!=(rd=parser->read32()))
			{
				printf("Not riff\n");badAvi=1;
				printf("%x != %x\n",rd,MKFCC('R','I','F','F'));
			}
		parser->read32();
		if(MKFCC('A','V','I',' ')!=parser->read32())
			{
				printf("Not Avi\n");badAvi=1;
			}
		
		if(!badAvi)
			{
				walk(parser);	
			
			}					
		delete parser;
		aprintf("Found %d tracks\n:-----------\n",_nbTrack);
		// check if it looks like a correct avi
		if(!_nbTrack) badAvi=1;
		
		// if we are up to here -> good avi :)
		if(badAvi)
		{
			printf("FAIL\n");
			return 0;
		}
		// now read up each parts...
		//____________________________
		                
#define DUMP_TRACK(i) aprintf(" at %"PRIu64" (%"PRIx64") size : %"PRIu64" (%"PRIx64")\n", \
				_Tracks[i].strh.offset,\
				_Tracks[i].strh.offset,\
				_Tracks[i].strh.size,\
				_Tracks[i].strh.size);

		for(uint32_t i=0;i<_nbTrack;i++)
		{
			DUMP_TRACK(i);		
		}		
		
		uint32_t vidTrack=0xff;
		// search wich track is the video one
		// and load it to _videoheader
		
		for(uint32_t i=0;i<_nbTrack;i++)
		{
			fseeko(_fd,_Tracks[i].strh.offset,SEEK_SET);
			if(_Tracks[i].strh.size!=sizeof(_videostream))
			{
				printf("[AVI]Mmm(1) we have a bogey here, size mismatch : %"PRIu64"\n",_Tracks[i].strh.size);
				printf("[AVI]expected %d\n",(int)sizeof(_videostream));
				if(_Tracks[i].strh.size<sizeof(_videostream)-8) // RECT is not mandatory
				{
                                  GUI_Error_HIG(QT_TR_NOOP("Malformed header"), NULL);
					return 0;
				}		
				printf("[AVI]Trying to continue anyway\n");			
			}
			fread(&_videostream,sizeof(_videostream),1,_fd);
#ifdef ADM_BIG_ENDIAN
				Endian_AviStreamHeader(&_videostream);
#endif
			if(_videostream.fccType==MKFCC('v','i','d','s'))
				{
					vidTrack=i;
					printf("Video track is %u\n",i);
					break;
				}		
		}
		if(0xff==vidTrack)
		{
			printf("Could not identify video track!");
			return 0;
		}
		
		// then bih stuff
		int32_t extra;
//		_fd=fopen(name,"rb");
		
		fseeko(_fd,_Tracks[vidTrack].strf.offset,SEEK_SET);		
		extra=_Tracks[vidTrack].strf.size-sizeof(_video_bih);
		if(extra<0)
		{	
			printf("[AVI]bih is not big enough (%"PRIu64"/%d)!\n",_Tracks[vidTrack].strf.size,(int)sizeof(_video_bih));
			return 0;
		}
		fread(&_video_bih,sizeof(_video_bih),1,_fd);
#ifdef ADM_BIG_ENDIAN
		Endian_BitMapInfo(&_video_bih);
#endif
		if(extra>0)
		{				
			_videoExtraLen=extra;		
			_videoExtraData=new uint8_t [extra];
			fread(_videoExtraData,extra,1,_fd);
		}
		_isvideopresent=1;
		//--------------------------------------------------
		//	Read audio trak info, select if there is
		//	several
		//--------------------------------------------------
		// and audio track
		if(_mainaviheader.dwStreams>=2)
		{
			// which one is the audio track, is there several ?
			if(!(_nbAudioTracks=countAudioTrack()))
                        {
                                printf("Weird, there is no audio track, but more than one stream...\n");
                        }			
                        else
                        {
                          uint32_t run=0,audio=0;
                          odmlAudioTrack *track;

                          _audioTracks=new odmlAudioTrack[_nbAudioTracks]; 
                          _audioStreams=new ADM_audioStream *[_nbAudioTracks]; 
                          while(audio<_nbAudioTracks)
                          {
                                        ADM_assert(run<_nbTrack);

                                        track=&(_audioTracks[audio]);
                                        fseeko(_fd,_Tracks[run].strh.offset,SEEK_SET);
                                        if(_Tracks[run].strh.size != sizeof(_audiostream))
                                        {
                                                printf("[AVI]Mmm(2) we have a bogey here, size mismatch : %"PRIu64"\n",_Tracks[run].strh.size);
                                                printf("[AVI]expected %d\n",(int)sizeof(_audiostream));
                                                if(_Tracks[run].strh.size<sizeof(_audiostream)-8)
                                                {
                                                  GUI_Error_HIG(QT_TR_NOOP("Malformed header"), NULL);
                                                        return 0;
                                                }
                                                printf("[AVI]Trying to continue anyway\n");			
                                        }
                                        fread(track->avistream,sizeof(_audiostream),1,_fd);
#ifdef ADM_BIG_ENDIAN
                                        Endian_AviStreamHeader(track->avistream);
#endif
                                        if(track->avistream->fccType!=MKFCC('a','u','d','s'))
                                        {	
                                                printf("Not an audio track!\n");
                                                run++;
                                                continue;
                                        }
                                        // now read extra stuff
                                        fseeko(_fd,_Tracks[run].strf.offset,SEEK_SET);		
                                        extra=_Tracks[run].strf.size-sizeof(WAVHeader);
                                        if(extra<0)
                                        {	
                                                printf("[AVI]WavHeader is not big enough (%"PRIu64"/%d)!\n",
                                                _Tracks[run].strf.size,(int)sizeof(WAVHeader));
                                                return 0;
                                        }
                                        fread(track->wavHeader,sizeof(WAVHeader),1,_fd);				
#ifdef ADM_BIG_ENDIAN
                                        Endian_WavHeader(track->wavHeader);
#endif
                                        if(extra>2)
                                        {
                                                fgetc(_fd);fgetc(_fd);
                                                extra-=2;
                                                track->extraDataLen=extra;		
                                                track->extraData=new uint8_t [extra];
                                                fread(track->extraData,extra,1,_fd);
                                        }
                                        track->trackNum=run;
                                        audio++;
                                        run++;
                           }	
                        }
                }
		
		// now look at the index stuff
		// there could be 3 cases:
		// 1- It is a openDML index, meta index  + several smaller index
		// 2- It is a legacy index (type 1 , most common)
		// 3- It is a broken index or no index at all
		//
		// If it is a openDML index we will find a "indx" field in the Tracks
		// Else we will find it in _regularIndex Track
		// Since openDML often also have a regular index we will try open DML first
		
		uint8_t ret=0;
		Dump();
		
		// take the size of riff header and actual file size
		uint64_t riffSize;
		fseeko(_fd,0,SEEK_END);		
		_fileSize=ftello(_fd);
		fseeko(_fd,0,SEEK_SET);
		read32();
		riffSize=(uint64_t )read32();
				
		
		// 1st case, we have an avi < 4 Gb
		// potentially avi type 1	
#if 0	
		if((_fileSize<4*1024*1024*1024LL)&&
                	// if riff size is ~ fileSize try regular index
			 (abs(riffSize-_fileSize)<1024*1024))
#endif

#define HAS(x) if(x) printf(#x" : yes\n"); else printf(#x" : no\n");
                // If there is no openDML index
                HAS( _regularIndex.offset);
                HAS( _Tracks[vidTrack].indx.offset);
                if(!ret && _regularIndex.offset &&!_Tracks[vidTrack].indx.offset) 
        // try regular avi if a idx1 field is there (avi index)
                        ret=indexRegular(vidTrack);

                if (!ret && _Tracks[vidTrack].indx.offset)	// Try openDML if a index field is there (openDML)
                        ret=indexODML(vidTrack);
                if(!ret) 
                {
                        printf("Could not index it properly...\n");
                        return 0;

                }
                if(!_nbAudioTracks)
                {
                         _isaudiopresent=0;
                }
                else
                {
                        odmlAudioTrack *track;
                        // Check it is not a weird DV file
                        if(fourCC::check(_video_bih.biCompression,(uint8_t *)"dvsd"))
                        {
                             for(int i=0;i<_nbAudioTracks;i++)
                             {
                                    track=&(_audioTracks[i]);
                                    WAVHeader *hdr=  track->wavHeader;
                                    if(!hdr->frequency)
                                    {
                                            ADM_warning("Fixing audio track to be PCM\n");
                                            hdr->frequency=48000;
                                            //hdr->channels=2;
                                            hdr->byterate=48000*hdr->channels*2;
                                            hdr->blockalign=2*hdr->channels;
                                    }
                             }

                        }
                        // build audio stream
                        
                        for(int i=0;i<_nbAudioTracks;i++)
                        {
                                track=&(_audioTracks[i]);
                                ADM_aviAudioAccess *access=new ADM_aviAudioAccess(track->index,track->wavHeader,
                                            track->nbChunks,
                                            myName,
                                            track->extraDataLen,track->extraData);
                                _audioStreams[i]= ADM_audioCreateStream((track->wavHeader), access);
                        }
                }
                if(!_video_bih.biCompression && fourCC::check(_videostream.fccHandler,(uint8_t*)"DIB "))
                  {
                        _videostream.fccHandler=_video_bih.biCompression=fourCC::get((uint8_t*)"DIB ");
                  }
                else
                _videostream.fccHandler=_video_bih.biCompression;
                printf("\nOpenDML file successfully read..\n");
                if(ret==1) 
                {
                    computePtsDts();
                    removeEmptyFrames();
                }
                ADM_info("PtsAvailable : %d\n",(int)ptsAvailable);
                return ret;
}
예제 #9
0
Node *Canonical(Node *n, Miscellaneous *miscell, int *cnt, char *uform, int *tl_yychar)
{	Node *m, *p, *k1, *k2, *prev, *dflt = ZN;
	int tok;
	static Node	*can = ZN;


	if (!n) return n;

	tok = n->ntyp;
	if (tok != AND && tok != OR)
		return n;

	can = ZN;
	addcan(tok, n, miscell);
#if 1
	Debug("\nA0: "); Dump(can); 
	Debug("\nA1: "); Dump(n); Debug("\n");
#endif
	releasenode(1, n);

	/* mark redundant nodes */
	if (tok == AND)
	{	for (m = can; m; m = (m->ntyp == AND) ? m->rgt : ZN)
		{	k1 = (m->ntyp == AND) ? m->lft : m;
			if (k1->ntyp == TRUE)
			{	marknode(AND, m);
				dflt = True;
				continue;
			}
			if (k1->ntyp == FALSE)
			{	releasenode(1, can);
				can = False;
				goto out;
		}	}
		for (m = can; m; m = (m->ntyp == AND) ? m->rgt : ZN)
		for (p = can; p; p = (p->ntyp == AND) ? p->rgt : ZN)
		{	if (p == m
			||  p->ntyp == -1
			||  m->ntyp == -1)
				continue;
			k1 = (m->ntyp == AND) ? m->lft : m;
			k2 = (p->ntyp == AND) ? p->lft : p;

			if (isequal(k1, k2, cnt, uform, tl_yychar, miscell))
			{	marknode(AND, p);
				continue;
			}
			if (anywhere(OR, k1, k2, cnt, uform, tl_yychar, miscell))
			{	marknode(AND, p);
				continue;
			}
			if (k2->ntyp == U_OPER
			&&  anywhere(AND, k2->rgt, can, cnt, uform, tl_yychar, miscell))
			{	marknode(AND, p);
				continue;
			}	/* q && (p U q) = q */
	}	}
	if (tok == OR)
	{	for (m = can; m; m = (m->ntyp == OR) ? m->rgt : ZN)
		{	k1 = (m->ntyp == OR) ? m->lft : m;
			if (k1->ntyp == FALSE)
			{	marknode(OR, m);
				dflt = False;
				continue;
			}
			if (k1->ntyp == TRUE)
			{	releasenode(1, can);
				can = True;
				goto out;
		}	}
		for (m = can; m; m = (m->ntyp == OR) ? m->rgt : ZN)
		for (p = can; p; p = (p->ntyp == OR) ? p->rgt : ZN)
		{	if (p == m
			||  p->ntyp == -1
			||  m->ntyp == -1)
				continue;
			k1 = (m->ntyp == OR) ? m->lft : m;
			k2 = (p->ntyp == OR) ? p->lft : p;

			if (isequal(k1, k2, cnt, uform, tl_yychar, miscell))
			{	marknode(OR, p);
				continue;
			}
			if (anywhere(AND, k1, k2, cnt, uform, tl_yychar, miscell))
			{	marknode(OR, p);
				continue;
			}
			if (k2->ntyp == V_OPER
			&&  k2->lft->ntyp == FALSE
			&&  anywhere(AND, k2->rgt, can, cnt, uform, tl_yychar, miscell))
			{	marknode(OR, p);
				continue;
			}	/* p || (F V p) = p */
	}	}
	for (m = can, prev = ZN; m; )	/* remove marked nodes */
	{	if (m->ntyp == -1)
		{	k2 = m->rgt;
			releasenode(0, m);
			if (!prev)
			{	m = can = can->rgt;
			} else
			{	m = prev->rgt = k2;
				/* if deleted the last node in a chain */
				if (!prev->rgt && prev->lft
				&&  (prev->ntyp == AND || prev->ntyp == OR))
				{	k1 = prev->lft;
					prev->ntyp = prev->lft->ntyp;
					prev->sym = prev->lft->sym;
					prev->rgt = prev->lft->rgt;
					prev->lft = prev->lft->lft;
					releasenode(0, k1);
				}
			}
			continue;
		}
		prev = m;
		m = m->rgt;
	}
out:
#if 1
	Debug("A2: "); Dump(can); Debug("\n");
#endif
	if (!can)
	{	if (!dflt)
			fatal("cannot happen, Canonical", (char *) 0, cnt, uform, tl_yychar, miscell);
		return dflt;
	}

	return can;
}
예제 #10
0
 void DumpTotal(FILE* out) {
   mClassSize /= mAllStats.mCreates;
   Dump(-1, out, nsTraceRefcntImpl::ALL_STATS);
 }
예제 #11
0
파일: Ntvol.c 프로젝트: ChiefGyk/VeraCrypt
NTSTATUS TCOpenVolume (PDEVICE_OBJECT DeviceObject,
	       PEXTENSION Extension,
	       MOUNT_STRUCT *mount,
	       PWSTR pwszMountVolume,
	       BOOL bRawDevice)
{
	FILE_STANDARD_INFORMATION FileStandardInfo;
	FILE_BASIC_INFORMATION FileBasicInfo;
	OBJECT_ATTRIBUTES oaFileAttributes;
	UNICODE_STRING FullFileName;
	IO_STATUS_BLOCK IoStatusBlock;
	PCRYPTO_INFO cryptoInfoPtr = NULL;
	PCRYPTO_INFO tmpCryptoInfo = NULL;
	LARGE_INTEGER lDiskLength;
	__int64 partitionStartingOffset = 0;
	int volumeType;
	char *readBuffer = 0;
	NTSTATUS ntStatus = 0;
	BOOL forceAccessCheck = (!bRawDevice && !(OsMajorVersion == 5 &&OsMinorVersion == 0)); // Windows 2000 does not support OBJ_FORCE_ACCESS_CHECK attribute
	BOOL disableBuffering = TRUE;
	BOOL exclusiveAccess = mount->bExclusiveAccess;

	Extension->pfoDeviceFile = NULL;
	Extension->hDeviceFile = NULL;
	Extension->bTimeStampValid = FALSE;

	RtlInitUnicodeString (&FullFileName, pwszMountVolume);
	InitializeObjectAttributes (&oaFileAttributes, &FullFileName, OBJ_CASE_INSENSITIVE | (forceAccessCheck ? OBJ_FORCE_ACCESS_CHECK : 0) | OBJ_KERNEL_HANDLE, NULL, NULL);
	KeInitializeEvent (&Extension->keVolumeEvent, NotificationEvent, FALSE);

	if (Extension->SecurityClientContextValid)
	{
		ntStatus = SeImpersonateClientEx (&Extension->SecurityClientContext, NULL);
		if (!NT_SUCCESS (ntStatus))
			goto error;
	}

	mount->VolumeMountedReadOnlyAfterDeviceWriteProtected = FALSE;

	// If we are opening a device, query its size first
	if (bRawDevice)
	{
		PARTITION_INFORMATION pi;
		PARTITION_INFORMATION_EX pix;
		LARGE_INTEGER diskLengthInfo;
		DISK_GEOMETRY dg;
		STORAGE_PROPERTY_QUERY storagePropertyQuery = {0};
		STORAGE_ACCESS_ALIGNMENT_DESCRIPTOR storageDescriptor = {0};

		ntStatus = IoGetDeviceObjectPointer (&FullFileName,
			FILE_READ_DATA | FILE_READ_ATTRIBUTES,
			&Extension->pfoDeviceFile,
			&Extension->pFsdDevice);

		if (!NT_SUCCESS (ntStatus))
			goto error;

		ntStatus = TCSendHostDeviceIoControlRequest (DeviceObject, Extension, IOCTL_DISK_GET_DRIVE_GEOMETRY, (char *) &dg, sizeof (dg));
		if (!NT_SUCCESS (ntStatus))
			goto error;

		lDiskLength.QuadPart = dg.Cylinders.QuadPart * dg.SectorsPerTrack * dg.TracksPerCylinder * dg.BytesPerSector;
		Extension->HostBytesPerSector = dg.BytesPerSector;

		storagePropertyQuery.PropertyId = StorageAccessAlignmentProperty;
		storagePropertyQuery.QueryType = PropertyStandardQuery;

		/* IOCTL_STORAGE_QUERY_PROPERTY supported only on Vista and above */
		if (NT_SUCCESS (TCSendHostDeviceIoControlRequestEx (DeviceObject, Extension, IOCTL_STORAGE_QUERY_PROPERTY,
			(char*) &storagePropertyQuery, sizeof(storagePropertyQuery),
			(char *) &storageDescriptor, sizeof (storageDescriptor))))
		{
			Extension->HostBytesPerPhysicalSector = storageDescriptor.BytesPerPhysicalSector;
		}
		else
		{
			Extension->HostBytesPerPhysicalSector = dg.BytesPerSector;
		}

		// Drive geometry is used only when IOCTL_DISK_GET_PARTITION_INFO fails
		if (NT_SUCCESS (TCSendHostDeviceIoControlRequest (DeviceObject, Extension, IOCTL_DISK_GET_PARTITION_INFO_EX, (char *) &pix, sizeof (pix))))
		{
			lDiskLength.QuadPart = pix.PartitionLength.QuadPart;
			partitionStartingOffset = pix.StartingOffset.QuadPart;
		}
		// Windows 2000 does not support IOCTL_DISK_GET_PARTITION_INFO_EX
		else if (NT_SUCCESS (TCSendHostDeviceIoControlRequest (DeviceObject, Extension, IOCTL_DISK_GET_PARTITION_INFO, (char *) &pi, sizeof (pi))))
		{
			lDiskLength.QuadPart = pi.PartitionLength.QuadPart;
			partitionStartingOffset = pi.StartingOffset.QuadPart;
		}
		else if (NT_SUCCESS (TCSendHostDeviceIoControlRequest (DeviceObject, Extension, IOCTL_DISK_GET_LENGTH_INFO, &diskLengthInfo, sizeof (diskLengthInfo))))
		{
			lDiskLength = diskLengthInfo;
		}

		ProbingHostDeviceForWrite = TRUE;

		if (!mount->bMountReadOnly
			&& TCSendHostDeviceIoControlRequest (DeviceObject, Extension,
				IsHiddenSystemRunning() ? TC_IOCTL_DISK_IS_WRITABLE : IOCTL_DISK_IS_WRITABLE, NULL, 0) == STATUS_MEDIA_WRITE_PROTECTED)
		{
			mount->bMountReadOnly = TRUE;
			DeviceObject->Characteristics |= FILE_READ_ONLY_DEVICE;
			mount->VolumeMountedReadOnlyAfterDeviceWriteProtected = TRUE;
		}

		ProbingHostDeviceForWrite = FALSE;

		// Some Windows tools (e.g. diskmgmt, diskpart, vssadmin) fail or experience timeouts when there is a raw device
		// open for exclusive access. Therefore, exclusive access is used only for file-hosted volumes.
		// Applications requiring a consistent device image need to acquire exclusive write access first. This is prevented
		// when a device-hosted volume is mounted.

		exclusiveAccess = FALSE;
	}
	else
	{
		// Limit the maximum required buffer size
		if (mount->BytesPerSector > 128 * BYTES_PER_KB)
		{
			ntStatus = STATUS_INVALID_PARAMETER;
			goto error;
		}

		Extension->HostBytesPerSector = mount->BytesPerSector;
		Extension->HostBytesPerPhysicalSector = mount->BytesPerPhysicalSector;

		if (Extension->HostBytesPerSector != TC_SECTOR_SIZE_FILE_HOSTED_VOLUME)
			disableBuffering = FALSE;
	}

	// Open the volume hosting file/device
	if (!mount->bMountReadOnly)
	{
		ntStatus = ZwCreateFile (&Extension->hDeviceFile,
			GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE,
			&oaFileAttributes,
			&IoStatusBlock,
			NULL,
			FILE_ATTRIBUTE_NORMAL |
			FILE_ATTRIBUTE_SYSTEM,
			exclusiveAccess ? 0 : FILE_SHARE_READ | FILE_SHARE_WRITE,
			FILE_OPEN,
			FILE_RANDOM_ACCESS |
			FILE_WRITE_THROUGH |
			(disableBuffering ? FILE_NO_INTERMEDIATE_BUFFERING : 0) |
			FILE_SYNCHRONOUS_IO_NONALERT,
			NULL,
			0);
	}

	/* 26-4-99 NT for some partitions returns this code, it is really a	access denied */
	if (ntStatus == 0xc000001b)
		ntStatus = STATUS_ACCESS_DENIED;

	mount->VolumeMountedReadOnlyAfterAccessDenied = FALSE;

	if (mount->bMountReadOnly || ntStatus == STATUS_ACCESS_DENIED)
	{
		ntStatus = ZwCreateFile (&Extension->hDeviceFile,
			GENERIC_READ | SYNCHRONIZE,
			&oaFileAttributes,
			&IoStatusBlock,
			NULL,
			FILE_ATTRIBUTE_NORMAL |
			FILE_ATTRIBUTE_SYSTEM,
			exclusiveAccess ? FILE_SHARE_READ : FILE_SHARE_READ | FILE_SHARE_WRITE,
			FILE_OPEN,
			FILE_RANDOM_ACCESS |
			FILE_WRITE_THROUGH |
			(disableBuffering ? FILE_NO_INTERMEDIATE_BUFFERING : 0) |
			FILE_SYNCHRONOUS_IO_NONALERT,
			NULL,
			0);

		if (NT_SUCCESS (ntStatus) && !mount->bMountReadOnly)
			mount->VolumeMountedReadOnlyAfterAccessDenied = TRUE;

		Extension->bReadOnly = TRUE;
		DeviceObject->Characteristics |= FILE_READ_ONLY_DEVICE;
	}
	else
		Extension->bReadOnly = FALSE;

	/* 26-4-99 NT for some partitions returns this code, it is really a
	access denied */
	if (ntStatus == 0xc000001b)
	{
		/* Partitions which return this code can still be opened with
		FILE_SHARE_READ but this causes NT problems elsewhere in
		particular if you do FILE_SHARE_READ NT will die later if
		anyone even tries to open the partition (or file for that
		matter...)  */
		ntStatus = STATUS_SHARING_VIOLATION;
	}

	if (!NT_SUCCESS (ntStatus))
	{
		goto error;
	}

	// If we have opened a file, query its size now
	if (bRawDevice == FALSE)
	{
		ntStatus = ZwQueryInformationFile (Extension->hDeviceFile,
			&IoStatusBlock,
			&FileBasicInfo,
			sizeof (FileBasicInfo),
			FileBasicInformation);

		if (NT_SUCCESS (ntStatus))
		{
			if (mount->bPreserveTimestamp)
			{
				Extension->fileCreationTime = FileBasicInfo.CreationTime;
				Extension->fileLastAccessTime = FileBasicInfo.LastAccessTime;
				Extension->fileLastWriteTime = FileBasicInfo.LastWriteTime;
				Extension->fileLastChangeTime = FileBasicInfo.ChangeTime;
				Extension->bTimeStampValid = TRUE;
			}

			ntStatus = ZwQueryInformationFile (Extension->hDeviceFile,
				&IoStatusBlock,
				&FileStandardInfo,
				sizeof (FileStandardInfo),
				FileStandardInformation);
		}

		if (!NT_SUCCESS (ntStatus))
		{
			Dump ("ZwQueryInformationFile failed while opening file: NTSTATUS 0x%08x\n",
				ntStatus);
			goto error;
		}

		lDiskLength.QuadPart = FileStandardInfo.EndOfFile.QuadPart;

		if (FileBasicInfo.FileAttributes & FILE_ATTRIBUTE_COMPRESSED)
		{
			Dump ("File \"%ls\" is marked as compressed - not supported!\n", pwszMountVolume);
			mount->nReturnCode = ERR_COMPRESSION_NOT_SUPPORTED;
			ntStatus = STATUS_SUCCESS;
			goto error;
		}

		ntStatus = ObReferenceObjectByHandle (Extension->hDeviceFile,
			FILE_ALL_ACCESS,
			*IoFileObjectType,
			KernelMode,
			&Extension->pfoDeviceFile,
			0);

		if (!NT_SUCCESS (ntStatus))
		{
			goto error;
		}

		/* Get the FSD device for the file (probably either NTFS or	FAT) */
		Extension->pFsdDevice = IoGetRelatedDeviceObject (Extension->pfoDeviceFile);
	}
	else
	{
		// Try to gain "raw" access to the partition in case there is a live filesystem on it (otherwise,
		// the NTFS driver guards hidden sectors and prevents mounting using a backup header e.g. after the user
		// accidentally quick-formats a dismounted partition-hosted TrueCrypt volume as NTFS).

		PFILE_OBJECT pfoTmpDeviceFile = NULL;

		if (NT_SUCCESS (ObReferenceObjectByHandle (Extension->hDeviceFile, FILE_ALL_ACCESS, *IoFileObjectType, KernelMode, &pfoTmpDeviceFile, NULL))
			&& pfoTmpDeviceFile != NULL)
		{
			TCFsctlCall (pfoTmpDeviceFile, FSCTL_ALLOW_EXTENDED_DASD_IO, NULL, 0, NULL, 0);
			ObDereferenceObject (pfoTmpDeviceFile);
		}
	}

	// Check volume size
	if (lDiskLength.QuadPart < TC_MIN_VOLUME_SIZE_LEGACY || lDiskLength.QuadPart > TC_MAX_VOLUME_SIZE)
	{
		mount->nReturnCode = ERR_VOL_SIZE_WRONG;
		ntStatus = STATUS_SUCCESS;
		goto error;
	}

	Extension->DiskLength = lDiskLength.QuadPart;
	Extension->HostLength = lDiskLength.QuadPart;

	readBuffer = TCalloc (max (max (TC_VOLUME_HEADER_EFFECTIVE_SIZE, PAGE_SIZE), Extension->HostBytesPerSector));
	if (readBuffer == NULL)
	{
		ntStatus = STATUS_INSUFFICIENT_RESOURCES;
		goto error;
	}

	// Go through all volume types (e.g., normal, hidden)
	for (volumeType = TC_VOLUME_TYPE_NORMAL;
		volumeType < TC_VOLUME_TYPE_COUNT;
		volumeType++)
	{
		Dump ("Trying to open volume type %d\n", volumeType);

		/* Read the volume header */

		if (!mount->bPartitionInInactiveSysEncScope
			|| (mount->bPartitionInInactiveSysEncScope && volumeType == TC_VOLUME_TYPE_HIDDEN))
		{
			// Header of a volume that is not within the scope of system encryption, or
			// header of a system hidden volume (containing a hidden OS)

			LARGE_INTEGER headerOffset;

			if (mount->UseBackupHeader && lDiskLength.QuadPart <= TC_TOTAL_VOLUME_HEADERS_SIZE)
				continue;

			switch (volumeType)
			{
			case TC_VOLUME_TYPE_NORMAL:
				headerOffset.QuadPart = mount->UseBackupHeader ? lDiskLength.QuadPart - TC_VOLUME_HEADER_GROUP_SIZE : TC_VOLUME_HEADER_OFFSET;
				break;

			case TC_VOLUME_TYPE_HIDDEN:
				if (lDiskLength.QuadPart <= TC_VOLUME_HEADER_GROUP_SIZE)
					continue;

				headerOffset.QuadPart = mount->UseBackupHeader ? lDiskLength.QuadPart - TC_HIDDEN_VOLUME_HEADER_OFFSET : TC_HIDDEN_VOLUME_HEADER_OFFSET;
				break;
			}

			Dump ("Reading volume header at %I64d\n", headerOffset.QuadPart);

			ntStatus = ZwReadFile (Extension->hDeviceFile,
			NULL,
			NULL,
			NULL,
			&IoStatusBlock,
			readBuffer,
			bRawDevice ? max (TC_VOLUME_HEADER_EFFECTIVE_SIZE, Extension->HostBytesPerSector) : TC_VOLUME_HEADER_EFFECTIVE_SIZE,
			&headerOffset,
			NULL);
		}
		else
		{
			// Header of a partition that is within the scope of system encryption

			WCHAR parentDrivePath [47+1] = {0};
			HANDLE hParentDeviceFile = NULL;
			UNICODE_STRING FullParentPath;
			OBJECT_ATTRIBUTES oaParentFileAttributes;
			LARGE_INTEGER parentKeyDataOffset;

			RtlStringCbPrintfW (parentDrivePath,
				sizeof (parentDrivePath),
				WIDE ("\\Device\\Harddisk%d\\Partition0"),
				mount->nPartitionInInactiveSysEncScopeDriveNo);

			Dump ("Mounting partition within scope of system encryption (reading key data from: %ls)\n", parentDrivePath);

			RtlInitUnicodeString (&FullParentPath, parentDrivePath);
			InitializeObjectAttributes (&oaParentFileAttributes, &FullParentPath, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,	NULL, NULL);

			ntStatus = ZwCreateFile (&hParentDeviceFile,
				GENERIC_READ | SYNCHRONIZE,
				&oaParentFileAttributes,
				&IoStatusBlock,
				NULL,
				FILE_ATTRIBUTE_NORMAL |
				FILE_ATTRIBUTE_SYSTEM,
				FILE_SHARE_READ | FILE_SHARE_WRITE,
				FILE_OPEN,
				FILE_RANDOM_ACCESS |
				FILE_WRITE_THROUGH |
				FILE_NO_INTERMEDIATE_BUFFERING |
				FILE_SYNCHRONOUS_IO_NONALERT,
				NULL,
				0);

			if (!NT_SUCCESS (ntStatus))
			{
				if (hParentDeviceFile != NULL)
					ZwClose (hParentDeviceFile);

				Dump ("Cannot open %ls\n", parentDrivePath);

				goto error;
			}

			parentKeyDataOffset.QuadPart = TC_BOOT_VOLUME_HEADER_SECTOR_OFFSET;

			ntStatus = ZwReadFile (hParentDeviceFile,
				NULL,
				NULL,
				NULL,
				&IoStatusBlock,
				readBuffer,
				max (TC_VOLUME_HEADER_EFFECTIVE_SIZE, Extension->HostBytesPerSector),
				&parentKeyDataOffset,
				NULL);

			if (hParentDeviceFile != NULL)
				ZwClose (hParentDeviceFile);
		}

		if (!NT_SUCCESS (ntStatus) && ntStatus != STATUS_END_OF_FILE)
		{
			Dump ("Read failed: NTSTATUS 0x%08x\n", ntStatus);
			goto error;
		}

		if (ntStatus == STATUS_END_OF_FILE || IoStatusBlock.Information < TC_VOLUME_HEADER_EFFECTIVE_SIZE)
		{
			Dump ("Read didn't read enough data\n");

			// If FSCTL_ALLOW_EXTENDED_DASD_IO failed and there is a live filesystem on the partition, then the
			// filesystem driver may report EOF when we are reading hidden sectors (when the filesystem is
			// shorter than the partition). This can happen for example after the user quick-formats a dismounted
			// partition-hosted TrueCrypt volume and then tries to mount the volume using the embedded backup header.
			memset (readBuffer, 0, TC_VOLUME_HEADER_EFFECTIVE_SIZE);
		}

		/* Attempt to recognize the volume (decrypt the header) */

		ReadVolumeHeaderRecoveryMode = mount->RecoveryMode;

		if ((volumeType == TC_VOLUME_TYPE_HIDDEN) && mount->bProtectHiddenVolume)
		{
			mount->nReturnCode = ReadVolumeHeaderWCache (
				FALSE,
				mount->bCache,
				mount->bCachePim,
				readBuffer,
				&mount->ProtectedHidVolPassword,
				mount->ProtectedHidVolPkcs5Prf,
				mount->ProtectedHidVolPim,
				mount->bTrueCryptMode,
				&tmpCryptoInfo);
		}
		else
		{
			mount->nReturnCode = ReadVolumeHeaderWCache (
				mount->bPartitionInInactiveSysEncScope && volumeType == TC_VOLUME_TYPE_NORMAL,
				mount->bCache,
				mount->bCachePim,
				readBuffer,
				&mount->VolumePassword,
				mount->pkcs5_prf,
				mount->VolumePim,
				mount->bTrueCryptMode,
				&Extension->cryptoInfo);
		}

		ReadVolumeHeaderRecoveryMode = FALSE;

		if (mount->nReturnCode == 0 || mount->nReturnCode == ERR_CIPHER_INIT_WEAK_KEY)
		{
			/* Volume header successfully decrypted */

			if (!Extension->cryptoInfo)
			{
				/* should never happen */
				mount->nReturnCode = ERR_OUTOFMEMORY;
				ntStatus = STATUS_SUCCESS;
				goto error;
			}

			Dump ("Volume header decrypted\n");
			Dump ("Required program version = %x\n", (int) Extension->cryptoInfo->RequiredProgramVersion);
			Dump ("Legacy volume = %d\n", (int) Extension->cryptoInfo->LegacyVolume);

			if (IsHiddenSystemRunning() && !Extension->cryptoInfo->hiddenVolume)
			{
				Extension->bReadOnly = mount->bMountReadOnly = TRUE;
				HiddenSysLeakProtectionCount++;
			}

			Extension->cryptoInfo->bProtectHiddenVolume = FALSE;
			Extension->cryptoInfo->bHiddenVolProtectionAction = FALSE;

			Extension->cryptoInfo->bPartitionInInactiveSysEncScope = mount->bPartitionInInactiveSysEncScope;

			/* compute the ID of this volume: SHA-512 of the effective header */
			sha256 (Extension->volumeID, readBuffer, TC_VOLUME_HEADER_EFFECTIVE_SIZE);

			if (volumeType == TC_VOLUME_TYPE_NORMAL)
			{
				if (mount->bPartitionInInactiveSysEncScope)
				{
					if (Extension->cryptoInfo->EncryptedAreaStart.Value > (unsigned __int64) partitionStartingOffset
						|| Extension->cryptoInfo->EncryptedAreaStart.Value + Extension->cryptoInfo->VolumeSize.Value <= (unsigned __int64) partitionStartingOffset)
					{
						// The partition is not within the key scope of system encryption
						mount->nReturnCode = ERR_PASSWORD_WRONG;
						ntStatus = STATUS_SUCCESS;
						goto error;
					}

					if (Extension->cryptoInfo->EncryptedAreaLength.Value != Extension->cryptoInfo->VolumeSize.Value)
					{
						// Partial encryption is not supported for volumes mounted as regular
						mount->nReturnCode = ERR_ENCRYPTION_NOT_COMPLETED;
						ntStatus = STATUS_SUCCESS;
						goto error;
					}
				}
				else if (Extension->cryptoInfo->HeaderFlags & TC_HEADER_FLAG_NONSYS_INPLACE_ENC)
				{
					if (Extension->cryptoInfo->EncryptedAreaLength.Value != Extension->cryptoInfo->VolumeSize.Value)
					{
						// Non-system in-place encryption process has not been completed on this volume
						mount->nReturnCode = ERR_NONSYS_INPLACE_ENC_INCOMPLETE;
						ntStatus = STATUS_SUCCESS;
						goto error;
					}
				}
			}

			Extension->cryptoInfo->FirstDataUnitNo.Value = 0;

			if (Extension->cryptoInfo->hiddenVolume && IsHiddenSystemRunning())
			{
				// Prevent mount of a hidden system partition if the system hosted on it is currently running
				if (memcmp (Extension->cryptoInfo->master_keydata, GetSystemDriveCryptoInfo()->master_keydata, EAGetKeySize (Extension->cryptoInfo->ea)) == 0)
				{
					mount->nReturnCode = ERR_VOL_ALREADY_MOUNTED;
					ntStatus = STATUS_SUCCESS;
					goto error;
				}
			}

			switch (volumeType)
			{
			case TC_VOLUME_TYPE_NORMAL:

				Extension->cryptoInfo->hiddenVolume = FALSE;

				if (mount->bPartitionInInactiveSysEncScope)
				{
					Extension->cryptoInfo->volDataAreaOffset = 0;
					Extension->DiskLength = lDiskLength.QuadPart;
					Extension->cryptoInfo->FirstDataUnitNo.Value = partitionStartingOffset / ENCRYPTION_DATA_UNIT_SIZE;
				}
				else if (Extension->cryptoInfo->LegacyVolume)
				{
					Extension->cryptoInfo->volDataAreaOffset = TC_VOLUME_HEADER_SIZE_LEGACY;
					Extension->DiskLength = lDiskLength.QuadPart - TC_VOLUME_HEADER_SIZE_LEGACY;
				}
				else
				{
					Extension->cryptoInfo->volDataAreaOffset = Extension->cryptoInfo->EncryptedAreaStart.Value;
					Extension->DiskLength = Extension->cryptoInfo->VolumeSize.Value;
				}

				break;

			case TC_VOLUME_TYPE_HIDDEN:

				cryptoInfoPtr = mount->bProtectHiddenVolume ? tmpCryptoInfo : Extension->cryptoInfo;

				Extension->cryptoInfo->hiddenVolumeOffset = cryptoInfoPtr->EncryptedAreaStart.Value;

				Dump ("Hidden volume offset = %I64d\n", Extension->cryptoInfo->hiddenVolumeOffset);
				Dump ("Hidden volume size = %I64d\n", cryptoInfoPtr->hiddenVolumeSize);
				Dump ("Hidden volume end = %I64d\n", Extension->cryptoInfo->hiddenVolumeOffset + cryptoInfoPtr->hiddenVolumeSize - 1);

				// Validate the offset
				if (Extension->cryptoInfo->hiddenVolumeOffset % ENCRYPTION_DATA_UNIT_SIZE != 0)
				{
					mount->nReturnCode = ERR_VOL_SIZE_WRONG;
					ntStatus = STATUS_SUCCESS;
					goto error;
				}

				// If we are supposed to actually mount the hidden volume (not just to protect it)
				if (!mount->bProtectHiddenVolume)
				{
					Extension->DiskLength = cryptoInfoPtr->hiddenVolumeSize;
					Extension->cryptoInfo->hiddenVolume = TRUE;
					Extension->cryptoInfo->volDataAreaOffset = Extension->cryptoInfo->hiddenVolumeOffset;
				}
				else
				{
					// Hidden volume protection
					Extension->cryptoInfo->hiddenVolume = FALSE;
					Extension->cryptoInfo->bProtectHiddenVolume = TRUE;

					Extension->cryptoInfo->hiddenVolumeProtectedSize = tmpCryptoInfo->hiddenVolumeSize;

					Dump ("Hidden volume protection active: %I64d-%I64d (%I64d)\n", Extension->cryptoInfo->hiddenVolumeOffset, Extension->cryptoInfo->hiddenVolumeProtectedSize + Extension->cryptoInfo->hiddenVolumeOffset - 1, Extension->cryptoInfo->hiddenVolumeProtectedSize);
				}

				break;
			}

			Dump ("Volume data offset = %I64d\n", Extension->cryptoInfo->volDataAreaOffset);
			Dump ("Volume data size = %I64d\n", Extension->DiskLength);
			Dump ("Volume data end = %I64d\n", Extension->cryptoInfo->volDataAreaOffset + Extension->DiskLength - 1);

			if (Extension->DiskLength == 0)
			{
				Dump ("Incorrect volume size\n");
				continue;
			}

			// If this is a hidden volume, make sure we are supposed to actually
			// mount it (i.e. not just to protect it)
			if (volumeType == TC_VOLUME_TYPE_NORMAL || !mount->bProtectHiddenVolume)
			{
				// Validate sector size
				if (bRawDevice && Extension->cryptoInfo->SectorSize != Extension->HostBytesPerSector)
				{
					mount->nReturnCode = ERR_PARAMETER_INCORRECT;
					ntStatus = STATUS_SUCCESS;
					goto error;
				}

				// Calculate virtual volume geometry
				Extension->TracksPerCylinder = 1;
				Extension->SectorsPerTrack = 1;
				Extension->BytesPerSector = Extension->cryptoInfo->SectorSize;
				Extension->NumberOfCylinders = Extension->DiskLength / Extension->BytesPerSector;
				Extension->PartitionType = 0;

				Extension->bRawDevice = bRawDevice;

				memset (Extension->wszVolume, 0, sizeof (Extension->wszVolume));
				if (wcsstr (pwszMountVolume, WIDE ("\\??\\UNC\\")) == pwszMountVolume)
				{
					/* UNC path */
					RtlStringCbPrintfW (Extension->wszVolume,
						sizeof (Extension->wszVolume),
						WIDE ("\\??\\\\%s"),
						pwszMountVolume + 7);
				}
				else
				{
					RtlStringCbCopyW (Extension->wszVolume, sizeof(Extension->wszVolume),pwszMountVolume);
				}

				memset (Extension->wszLabel, 0, sizeof (Extension->wszLabel));
				RtlStringCbCopyW (Extension->wszLabel, sizeof(Extension->wszLabel), mount->wszLabel);
			}

			// If we are to protect a hidden volume we cannot exit yet, for we must also
			// decrypt the hidden volume header.
			if (!(volumeType == TC_VOLUME_TYPE_NORMAL && mount->bProtectHiddenVolume))
			{
				TCfree (readBuffer);

				if (tmpCryptoInfo != NULL)
				{
					crypto_close (tmpCryptoInfo);
					tmpCryptoInfo = NULL;
				}

				return STATUS_SUCCESS;
			}
		}
		else if ((mount->bProtectHiddenVolume && volumeType == TC_VOLUME_TYPE_NORMAL)
			  || mount->nReturnCode != ERR_PASSWORD_WRONG)
		{
			 /* If we are not supposed to protect a hidden volume, the only error that is
				tolerated is ERR_PASSWORD_WRONG (to allow mounting a possible hidden volume).

				If we _are_ supposed to protect a hidden volume, we do not tolerate any error
				(both volume headers must be successfully decrypted). */

			break;
		}
	}

	/* Failed due to some non-OS reason so we drop through and return NT
	   SUCCESS then nReturnCode is checked later in user-mode */

	if (mount->nReturnCode == ERR_OUTOFMEMORY)
		ntStatus = STATUS_INSUFFICIENT_RESOURCES;
	else
		ntStatus = STATUS_SUCCESS;

error:
	if (mount->nReturnCode == ERR_SUCCESS)
		mount->nReturnCode = ERR_PASSWORD_WRONG;

	if (tmpCryptoInfo != NULL)
	{
		crypto_close (tmpCryptoInfo);
		tmpCryptoInfo = NULL;
	}

	if (Extension->cryptoInfo)
	{
		crypto_close (Extension->cryptoInfo);
		Extension->cryptoInfo = NULL;
	}

	if (Extension->bTimeStampValid)
	{
		RestoreTimeStamp (Extension);
	}

	/* Close the hDeviceFile */
	if (Extension->hDeviceFile != NULL)
		ZwClose (Extension->hDeviceFile);

	/* The cryptoInfo pointer is deallocated if the readheader routines
	   fail so there is no need to deallocate here  */

	/* Dereference the user-mode file object */
	if (Extension->pfoDeviceFile != NULL)
		ObDereferenceObject (Extension->pfoDeviceFile);

	/* Free the tmp IO buffers */
	if (readBuffer != NULL)
		TCfree (readBuffer);

	return ntStatus;
}
예제 #12
0
NTSTATUS EncryptedIoQueueStart (EncryptedIoQueue *queue)
{
	NTSTATUS status;
	EncryptedIoQueueBuffer *buffer;
	int i;

	queue->StartPending = TRUE;
	queue->ThreadExitRequested = FALSE;

	queue->OutstandingIoCount = 0;
	queue->IoThreadPendingRequestCount = 0;

	queue->FirstPoolBuffer = NULL;
	KeInitializeMutex (&queue->BufferPoolMutex, 0);

	KeInitializeEvent (&queue->NoOutstandingIoEvent, SynchronizationEvent, FALSE);
	KeInitializeEvent (&queue->PoolBufferFreeEvent, SynchronizationEvent, FALSE);
	KeInitializeEvent (&queue->QueueResumedEvent, SynchronizationEvent, FALSE);

	queue->FragmentBufferA = TCalloc (TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE);
	if (!queue->FragmentBufferA)
		goto noMemory;

	queue->FragmentBufferB = TCalloc (TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE);
	if (!queue->FragmentBufferB)
		goto noMemory;

	KeInitializeEvent (&queue->FragmentBufferAFreeEvent, SynchronizationEvent, TRUE);
	KeInitializeEvent (&queue->FragmentBufferBFreeEvent, SynchronizationEvent, TRUE);

	queue->ReadAheadBufferValid = FALSE;
	queue->ReadAheadBuffer = TCalloc (TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE);
	if (!queue->ReadAheadBuffer)
		goto noMemory;

	// Preallocate buffers
	for (i = 0; i < TC_ENC_IO_QUEUE_PREALLOCATED_IO_REQUEST_COUNT; ++i)
	{
		if (i < TC_ENC_IO_QUEUE_PREALLOCATED_ITEM_COUNT && !GetPoolBuffer (queue, sizeof (EncryptedIoQueueItem)))
			goto noMemory;

		if (!GetPoolBuffer (queue, sizeof (EncryptedIoRequest)))
			goto noMemory;
	}

	for (buffer = queue->FirstPoolBuffer; buffer != NULL; buffer = buffer->NextBuffer)
	{
		buffer->InUse = FALSE;
	}

	// Main thread
	InitializeListHead (&queue->MainThreadQueue);
	KeInitializeSpinLock (&queue->MainThreadQueueLock);
	KeInitializeEvent (&queue->MainThreadQueueNotEmptyEvent, SynchronizationEvent, FALSE);

	status = TCStartThread (MainThreadProc, queue, &queue->MainThread);
	if (!NT_SUCCESS (status))
		goto err;

	// IO thread
	InitializeListHead (&queue->IoThreadQueue);
	KeInitializeSpinLock (&queue->IoThreadQueueLock);
	KeInitializeEvent (&queue->IoThreadQueueNotEmptyEvent, SynchronizationEvent, FALSE);

	status = TCStartThread (IoThreadProc, queue, &queue->IoThread);
	if (!NT_SUCCESS (status))
	{
		queue->ThreadExitRequested = TRUE;
		TCStopThread (queue->MainThread, &queue->MainThreadQueueNotEmptyEvent);
		goto err;
	}

	// Completion thread
	InitializeListHead (&queue->CompletionThreadQueue);
	KeInitializeSpinLock (&queue->CompletionThreadQueueLock);
	KeInitializeEvent (&queue->CompletionThreadQueueNotEmptyEvent, SynchronizationEvent, FALSE);

	status = TCStartThread (CompletionThreadProc, queue, &queue->CompletionThread);
	if (!NT_SUCCESS (status))
	{
		queue->ThreadExitRequested = TRUE;
		TCStopThread (queue->MainThread, &queue->MainThreadQueueNotEmptyEvent);
		TCStopThread (queue->IoThread, &queue->IoThreadQueueNotEmptyEvent);
		goto err;
	}

#ifdef TC_TRACE_IO_QUEUE
	GetElapsedTimeInit (&queue->LastPerformanceCounter);
#endif

	queue->StopPending = FALSE;
	queue->StartPending = FALSE;

	Dump ("Queue started\n");
	return STATUS_SUCCESS;

noMemory:
	status = STATUS_INSUFFICIENT_RESOURCES;

err:
	if (queue->FragmentBufferA)
		TCfree (queue->FragmentBufferA);
	if (queue->FragmentBufferB)
		TCfree (queue->FragmentBufferB);
	if (queue->ReadAheadBuffer)
		TCfree (queue->ReadAheadBuffer);

	FreePoolBuffers (queue);

	queue->StartPending = FALSE;
	return status;
}
예제 #13
0
static VOID MainThreadProc (PVOID threadArg)
{
	EncryptedIoQueue *queue = (EncryptedIoQueue *) threadArg;
	PLIST_ENTRY listEntry;
	EncryptedIoQueueItem *item;

	LARGE_INTEGER fragmentOffset;
	ULONG dataRemaining;
	PUCHAR activeFragmentBuffer = queue->FragmentBufferA;
	PUCHAR dataBuffer;
	EncryptedIoRequest *request;
	uint64 intersectStart;
	uint32 intersectLength;
	ULONGLONG addResult;
	HRESULT hResult;

	if (IsEncryptionThreadPoolRunning())
		KeSetPriorityThread (KeGetCurrentThread(), LOW_REALTIME_PRIORITY);

	while (!queue->ThreadExitRequested)
	{
		if (!NT_SUCCESS (KeWaitForSingleObject (&queue->MainThreadQueueNotEmptyEvent, Executive, KernelMode, FALSE, NULL)))
			continue;

		while ((listEntry = ExInterlockedRemoveHeadList (&queue->MainThreadQueue, &queue->MainThreadQueueLock)))
		{
			PIRP irp = CONTAINING_RECORD (listEntry, IRP, Tail.Overlay.ListEntry);
			PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation (irp);

			if (queue->Suspended)
				KeWaitForSingleObject (&queue->QueueResumedEvent, Executive, KernelMode, FALSE, NULL);

			item = GetPoolBuffer (queue, sizeof (EncryptedIoQueueItem));
			if (!item)
			{
				TCCompleteDiskIrp (irp, STATUS_INSUFFICIENT_RESOURCES, 0);
				DecrementOutstandingIoCount (queue);
				IoReleaseRemoveLock (&queue->RemoveLock, irp);

				continue;
			}

			item->Queue = queue;
			item->OriginalIrp = irp;
			item->Status = STATUS_SUCCESS;

			IoSetCancelRoutine (irp, NULL);
			if (irp->Cancel)
			{
				CompleteOriginalIrp (item, STATUS_CANCELLED, 0);
				continue;
			}

			switch (irpSp->MajorFunction)
			{
			case IRP_MJ_READ:
				item->Write = FALSE;
				item->OriginalOffset = irpSp->Parameters.Read.ByteOffset;
				item->OriginalLength = irpSp->Parameters.Read.Length;
				break;

			case IRP_MJ_WRITE:
				item->Write = TRUE;
				item->OriginalOffset = irpSp->Parameters.Write.ByteOffset;
				item->OriginalLength = irpSp->Parameters.Write.Length;
				break;

			default:
				CompleteOriginalIrp (item, STATUS_INVALID_PARAMETER, 0);
				continue;
			}

#ifdef TC_TRACE_IO_QUEUE
			item->OriginalIrpOffset = item->OriginalOffset;
#endif

			// Handle misaligned read operations to work around a bug in Windows System Assessment Tool which does not follow FILE_FLAG_NO_BUFFERING requirements when benchmarking disk devices
			if (queue->IsFilterDevice
				&& !item->Write
				&& item->OriginalLength > 0
				&& (item->OriginalLength & (ENCRYPTION_DATA_UNIT_SIZE - 1)) == 0
				&& (item->OriginalOffset.QuadPart & (ENCRYPTION_DATA_UNIT_SIZE - 1)) != 0)
			{
				byte *buffer;
				ULONG alignedLength;
				LARGE_INTEGER alignedOffset;
				hResult = ULongAdd(item->OriginalLength, ENCRYPTION_DATA_UNIT_SIZE, &alignedLength);
				if (hResult != S_OK)
				{
					CompleteOriginalIrp (item, STATUS_INVALID_PARAMETER, 0);
					continue;
				}

				alignedOffset.QuadPart = item->OriginalOffset.QuadPart & ~((LONGLONG) ENCRYPTION_DATA_UNIT_SIZE - 1);

				buffer = TCalloc (alignedLength);
				if (!buffer)
				{
					CompleteOriginalIrp (item, STATUS_INSUFFICIENT_RESOURCES, 0);
					continue;
				}

				item->Status = TCReadDevice (queue->LowerDeviceObject, buffer, alignedOffset, alignedLength);

				if (NT_SUCCESS (item->Status))
				{
					UINT64_STRUCT dataUnit;

					dataBuffer = (PUCHAR) MmGetSystemAddressForMdlSafe (irp->MdlAddress, HighPagePriority);
					if (!dataBuffer)
					{
						TCfree (buffer);
						CompleteOriginalIrp (item, STATUS_INSUFFICIENT_RESOURCES, 0);
						continue;
					}

					if (queue->EncryptedAreaStart != -1 && queue->EncryptedAreaEnd != -1)
					{
						GetIntersection (alignedOffset.QuadPart, alignedLength, queue->EncryptedAreaStart, queue->EncryptedAreaEnd, &intersectStart, &intersectLength);
						if (intersectLength > 0)
						{
							dataUnit.Value = intersectStart / ENCRYPTION_DATA_UNIT_SIZE;
							DecryptDataUnits (buffer + (intersectStart - alignedOffset.QuadPart), &dataUnit, intersectLength / ENCRYPTION_DATA_UNIT_SIZE, queue->CryptoInfo);
						}
					}

					memcpy (dataBuffer, buffer + (item->OriginalOffset.LowPart & (ENCRYPTION_DATA_UNIT_SIZE - 1)), item->OriginalLength);
				}

				TCfree (buffer);
				CompleteOriginalIrp (item, item->Status, NT_SUCCESS (item->Status) ? item->OriginalLength : 0);
				continue;
			}

			// Validate offset and length
			if (item->OriginalLength == 0
				|| (item->OriginalLength & (ENCRYPTION_DATA_UNIT_SIZE - 1)) != 0
				|| (item->OriginalOffset.QuadPart & (ENCRYPTION_DATA_UNIT_SIZE - 1)) != 0
				|| (	!queue->IsFilterDevice &&
						(	(S_OK != ULongLongAdd(item->OriginalOffset.QuadPart, item->OriginalLength, &addResult))
							||	(addResult > (ULONGLONG) queue->VirtualDeviceLength)
						)
					)
				)
			{
				CompleteOriginalIrp (item, STATUS_INVALID_PARAMETER, 0);
				continue;
			}

#ifdef TC_TRACE_IO_QUEUE
			Dump ("Q  %I64d [%I64d] %c len=%d\n", item->OriginalOffset.QuadPart, GetElapsedTime (&queue->LastPerformanceCounter), item->Write ? 'W' : 'R', item->OriginalLength);
#endif

			if (!queue->IsFilterDevice)
			{
				// Adjust the offset for host file or device
				if (queue->CryptoInfo->hiddenVolume)
					hResult = ULongLongAdd(item->OriginalOffset.QuadPart, queue->CryptoInfo->hiddenVolumeOffset, &addResult);
				else
					hResult = ULongLongAdd(item->OriginalOffset.QuadPart, queue->CryptoInfo->volDataAreaOffset, &addResult);

				if (hResult != S_OK)
				{
					CompleteOriginalIrp (item, STATUS_INVALID_PARAMETER, 0);
					continue;
				}
				else
					item->OriginalOffset.QuadPart = addResult;

				// Hidden volume protection
				if (item->Write && queue->CryptoInfo->bProtectHiddenVolume)
				{
					// If there has already been a write operation denied in order to protect the
					// hidden volume (since the volume mount time)
					if (queue->CryptoInfo->bHiddenVolProtectionAction)
					{
						// Do not allow writing to this volume anymore. This is to fake a complete volume
						// or system failure (otherwise certain kinds of inconsistency within the file
						// system could indicate that this volume has used hidden volume protection).
						CompleteOriginalIrp (item, STATUS_INVALID_PARAMETER, 0);
						continue;
					}

					// Verify that no byte is going to be written to the hidden volume area
					if (RegionsOverlap ((unsigned __int64) item->OriginalOffset.QuadPart,
						(unsigned __int64) item->OriginalOffset.QuadPart + item->OriginalLength - 1,
						queue->CryptoInfo->hiddenVolumeOffset,
						(unsigned __int64) queue->CryptoInfo->hiddenVolumeOffset + queue->CryptoInfo->hiddenVolumeProtectedSize - 1))
					{
						Dump ("Hidden volume protection triggered: write %I64d-%I64d (protected %I64d-%I64d)\n", item->OriginalOffset.QuadPart, item->OriginalOffset.QuadPart + item->OriginalLength - 1, queue->CryptoInfo->hiddenVolumeOffset, queue->CryptoInfo->hiddenVolumeOffset + queue->CryptoInfo->hiddenVolumeProtectedSize - 1);
						queue->CryptoInfo->bHiddenVolProtectionAction = TRUE;

						// Deny this write operation to prevent the hidden volume from being overwritten
						CompleteOriginalIrp (item, STATUS_INVALID_PARAMETER, 0);
						continue;
					}
				}
			}
			else if (item->Write
				&& RegionsOverlap (item->OriginalOffset.QuadPart, item->OriginalOffset.QuadPart + item->OriginalLength - 1, TC_BOOT_VOLUME_HEADER_SECTOR_OFFSET, TC_BOOT_VOLUME_HEADER_SECTOR_OFFSET + TC_BOOT_ENCRYPTION_VOLUME_HEADER_SIZE - 1))
			{
				// Prevent inappropriately designed software from damaging important data that may be out of sync with the backup on the Rescue Disk (such as the end of the encrypted area).
				Dump ("Preventing write to the system encryption key data area\n");
				CompleteOriginalIrp (item, STATUS_MEDIA_WRITE_PROTECTED, 0);
				continue;
			}
			else if (item->Write && IsHiddenSystemRunning()
				&& (RegionsOverlap (item->OriginalOffset.QuadPart, item->OriginalOffset.QuadPart + item->OriginalLength - 1, TC_SECTOR_SIZE_BIOS, TC_BOOT_LOADER_AREA_SECTOR_COUNT * TC_SECTOR_SIZE_BIOS - 1)
				 || RegionsOverlap (item->OriginalOffset.QuadPart, item->OriginalOffset.QuadPart + item->OriginalLength - 1, GetBootDriveLength(), _I64_MAX)))
			{
				Dump ("Preventing write to boot loader or host protected area\n");
				CompleteOriginalIrp (item, STATUS_MEDIA_WRITE_PROTECTED, 0);
				continue;
			}

			dataBuffer = (PUCHAR) MmGetSystemAddressForMdlSafe (irp->MdlAddress, HighPagePriority);

			if (dataBuffer == NULL)
			{
				CompleteOriginalIrp (item, STATUS_INSUFFICIENT_RESOURCES, 0);
				continue;
			}

			// Divide data block to fragments to enable efficient overlapping of encryption and IO operations

			dataRemaining = item->OriginalLength;
			fragmentOffset = item->OriginalOffset;

			while (dataRemaining > 0)
			{
				BOOL isLastFragment = dataRemaining <= TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE;

				ULONG dataFragmentLength = isLastFragment ? dataRemaining : TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE;
				activeFragmentBuffer = (activeFragmentBuffer == queue->FragmentBufferA ? queue->FragmentBufferB : queue->FragmentBufferA);

				InterlockedIncrement (&queue->IoThreadPendingRequestCount);

				// Create IO request
				request = GetPoolBuffer (queue, sizeof (EncryptedIoRequest));
				if (!request)
				{
					CompleteOriginalIrp (item, STATUS_INSUFFICIENT_RESOURCES, 0);
					break;
				}
				request->Item = item;
				request->CompleteOriginalIrp = isLastFragment;
				request->Offset = fragmentOffset;
				request->Data = activeFragmentBuffer;
				request->OrigDataBufferFragment = dataBuffer;
				request->Length = dataFragmentLength;

				if (queue->IsFilterDevice)
				{
					if (queue->EncryptedAreaStart == -1 || queue->EncryptedAreaEnd == -1)
					{
						request->EncryptedLength = 0;
					}
					else
					{
						// Get intersection of data fragment with encrypted area
						GetIntersection (fragmentOffset.QuadPart, dataFragmentLength, queue->EncryptedAreaStart, queue->EncryptedAreaEnd, &intersectStart, &intersectLength);

						request->EncryptedOffset = intersectStart - fragmentOffset.QuadPart;
						request->EncryptedLength = intersectLength;
					}
				}
				else
				{
					request->EncryptedOffset = 0;
					request->EncryptedLength = dataFragmentLength;
				}

				AcquireFragmentBuffer (queue, activeFragmentBuffer);

				if (item->Write)
				{
					// Encrypt data
					memcpy (activeFragmentBuffer, dataBuffer, dataFragmentLength);

					if (request->EncryptedLength > 0)
					{
						UINT64_STRUCT dataUnit;
						ASSERT (request->EncryptedOffset + request->EncryptedLength <= request->Offset.QuadPart + request->Length);

						dataUnit.Value = (request->Offset.QuadPart + request->EncryptedOffset) / ENCRYPTION_DATA_UNIT_SIZE;

						if (queue->CryptoInfo->bPartitionInInactiveSysEncScope)
							dataUnit.Value += queue->CryptoInfo->FirstDataUnitNo.Value;
						else if (queue->RemapEncryptedArea)
							dataUnit.Value += queue->RemappedAreaDataUnitOffset;

						EncryptDataUnits (activeFragmentBuffer + request->EncryptedOffset, &dataUnit, request->EncryptedLength / ENCRYPTION_DATA_UNIT_SIZE, queue->CryptoInfo);
					}
				}

				// Queue IO request
				ExInterlockedInsertTailList (&queue->IoThreadQueue, &request->ListEntry, &queue->IoThreadQueueLock);
				KeSetEvent (&queue->IoThreadQueueNotEmptyEvent, IO_DISK_INCREMENT, FALSE);

				if (isLastFragment)
					break;

				dataRemaining -= TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE;
				dataBuffer += TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE;
				fragmentOffset.QuadPart += TC_ENC_IO_QUEUE_MAX_FRAGMENT_SIZE;
			}
		}
	}

	PsTerminateSystemThread (STATUS_SUCCESS);
}
예제 #14
0
static VOID IoThreadProc (PVOID threadArg)
{
	EncryptedIoQueue *queue = (EncryptedIoQueue *) threadArg;
	PLIST_ENTRY listEntry;
	EncryptedIoRequest *request;

	KeSetPriorityThread (KeGetCurrentThread(), LOW_REALTIME_PRIORITY);

	if (!queue->IsFilterDevice && queue->SecurityClientContext)
	{
#ifdef DEBUG
		NTSTATUS status =
#endif
		SeImpersonateClientEx (queue->SecurityClientContext, NULL);
		ASSERT (NT_SUCCESS (status));
	}

	while (!queue->ThreadExitRequested)
	{
		if (!NT_SUCCESS (KeWaitForSingleObject (&queue->IoThreadQueueNotEmptyEvent, Executive, KernelMode, FALSE, NULL)))
			continue;

		if (queue->ThreadExitRequested)
			break;

		while ((listEntry = ExInterlockedRemoveHeadList (&queue->IoThreadQueue, &queue->IoThreadQueueLock)))
		{
			InterlockedDecrement (&queue->IoThreadPendingRequestCount);
			request = CONTAINING_RECORD (listEntry, EncryptedIoRequest, ListEntry);

#ifdef TC_TRACE_IO_QUEUE
			Dump ("%c   %I64d [%I64d] roff=%I64d rlen=%d\n", request->Item->Write ? 'W' : 'R', request->Item->OriginalIrpOffset.QuadPart, GetElapsedTime (&queue->LastPerformanceCounter), request->Offset.QuadPart, request->Length);
#endif

			// Perform IO request if no preceding request of the item failed
			if (NT_SUCCESS (request->Item->Status))
			{
				if (queue->IsFilterDevice)
				{
					if (queue->RemapEncryptedArea && request->EncryptedLength > 0)
					{
						if (request->EncryptedLength != request->Length)
						{
							// Up to three subfragments may be required to handle a partially remapped fragment
							int subFragment;
							byte *subFragmentData = request->Data;

							for (subFragment = 0 ; subFragment < 3; ++subFragment)
							{
								LARGE_INTEGER subFragmentOffset;
								ULONG subFragmentLength;
								subFragmentOffset.QuadPart = request->Offset.QuadPart;

								switch (subFragment)
								{
								case 0:
									subFragmentLength = (ULONG) request->EncryptedOffset;
									break;

								case 1:
									subFragmentOffset.QuadPart += request->EncryptedOffset + queue->RemappedAreaOffset;
									subFragmentLength = request->EncryptedLength;
									break;

								case 2:
									subFragmentOffset.QuadPart += request->EncryptedOffset + request->EncryptedLength;
									subFragmentLength = (ULONG) (request->Length - (request->EncryptedOffset + request->EncryptedLength));
									break;
								}

								if (subFragmentLength > 0)
								{
									if (request->Item->Write)
										request->Item->Status = TCWriteDevice (queue->LowerDeviceObject, subFragmentData, subFragmentOffset, subFragmentLength);
									else
										request->Item->Status = TCCachedRead (queue, NULL, subFragmentData, subFragmentOffset, subFragmentLength);

									subFragmentData += subFragmentLength;
								}
							}
						}
						else
						{
							// Remap the fragment
							LARGE_INTEGER remappedOffset;
							remappedOffset.QuadPart = request->Offset.QuadPart + queue->RemappedAreaOffset;

							if (request->Item->Write)
								request->Item->Status = TCWriteDevice (queue->LowerDeviceObject, request->Data, remappedOffset, request->Length);
							else
								request->Item->Status = TCCachedRead (queue, NULL, request->Data, remappedOffset, request->Length);
						}
					}
					else
					{
						if (request->Item->Write)
							request->Item->Status = TCWriteDevice (queue->LowerDeviceObject, request->Data, request->Offset, request->Length);
						else
							request->Item->Status = TCCachedRead (queue, NULL, request->Data, request->Offset, request->Length);
					}
				}
				else
				{
					IO_STATUS_BLOCK ioStatus;

					if (request->Item->Write)
						request->Item->Status = ZwWriteFile (queue->HostFileHandle, NULL, NULL, NULL, &ioStatus, request->Data, request->Length, &request->Offset, NULL);
					else
						request->Item->Status = TCCachedRead (queue, &ioStatus, request->Data, request->Offset, request->Length);

					if (NT_SUCCESS (request->Item->Status) && ioStatus.Information != request->Length)
						request->Item->Status = STATUS_END_OF_FILE;
				}
			}

			if (request->Item->Write)
			{
				queue->ReadAheadBufferValid = FALSE;

				ReleaseFragmentBuffer (queue, request->Data);

				if (request->CompleteOriginalIrp)
				{
					CompleteOriginalIrp (request->Item, request->Item->Status,
						NT_SUCCESS (request->Item->Status) ? request->Item->OriginalLength : 0);
				}

				ReleasePoolBuffer (queue, request);
			}
			else
			{
				BOOL readAhead = FALSE;

				if (NT_SUCCESS (request->Item->Status))
					memcpy (request->OrigDataBufferFragment, request->Data, request->Length);

				ReleaseFragmentBuffer (queue, request->Data);
				request->Data = request->OrigDataBufferFragment;

				if (request->CompleteOriginalIrp
					&& queue->LastReadLength > 0
					&& NT_SUCCESS (request->Item->Status)
					&& InterlockedExchangeAdd (&queue->IoThreadPendingRequestCount, 0) == 0)
				{
					readAhead = TRUE;
					InterlockedIncrement (&queue->OutstandingIoCount);
				}

				ExInterlockedInsertTailList (&queue->CompletionThreadQueue, &request->CompletionListEntry, &queue->CompletionThreadQueueLock);
				KeSetEvent (&queue->CompletionThreadQueueNotEmptyEvent, IO_DISK_INCREMENT, FALSE);

				if (readAhead)
				{
					queue->ReadAheadBufferValid = FALSE;
					queue->ReadAheadOffset.QuadPart = queue->LastReadOffset.QuadPart + queue->LastReadLength;
					queue->ReadAheadLength = queue->LastReadLength;

					if (queue->ReadAheadOffset.QuadPart + queue->ReadAheadLength <= queue->MaxReadAheadOffset.QuadPart)
					{
#ifdef TC_TRACE_IO_QUEUE
						Dump ("A   %I64d [%I64d] roff=%I64d rlen=%d\n", request->Item->OriginalIrpOffset.QuadPart, GetElapsedTime (&queue->LastPerformanceCounter), queue->ReadAheadOffset, queue->ReadAheadLength);
#endif
						if (queue->IsFilterDevice)
						{
							queue->ReadAheadBufferValid = NT_SUCCESS (TCReadDevice (queue->LowerDeviceObject, queue->ReadAheadBuffer, queue->ReadAheadOffset, queue->ReadAheadLength));
						}
						else
						{
							IO_STATUS_BLOCK ioStatus;
							queue->ReadAheadBufferValid = NT_SUCCESS (ZwReadFile (queue->HostFileHandle, NULL, NULL, NULL, &ioStatus, queue->ReadAheadBuffer, queue->ReadAheadLength, &queue->ReadAheadOffset, NULL));
							queue->ReadAheadLength = (ULONG) ioStatus.Information;
						}
					}

					DecrementOutstandingIoCount (queue);
				}
			}
		}
	}

	PsTerminateSystemThread (STATUS_SUCCESS);
}
예제 #15
0
파일: TestMain.cpp 프로젝트: darcyg/Bex
 TestMain()
 {
     Dump("Bex Test Start...");
     atexit(pause);
 }
예제 #16
0
void
LayerManagerComposite::Render()
{
  PROFILER_LABEL("LayerManagerComposite", "Render",
    js::ProfileEntry::Category::GRAPHICS);

  if (mDestroyed) {
    NS_WARNING("Call on destroyed layer manager");
    return;
  }

  // At this time, it doesn't really matter if these preferences change
  // during the execution of the function; we should be safe in all
  // permutations. However, may as well just get the values onces and
  // then use them, just in case the consistency becomes important in
  // the future.
  bool invertVal = gfxPrefs::LayersEffectInvert();
  bool grayscaleVal = gfxPrefs::LayersEffectGrayscale();
  float contrastVal = gfxPrefs::LayersEffectContrast();
  bool haveLayerEffects = (invertVal || grayscaleVal || contrastVal != 0.0);

  // Set LayerScope begin/end frame
  LayerScopeAutoFrame frame(PR_Now());

  // Dump to console
  if (gfxPrefs::LayersDump()) {
    this->Dump();
  } else if (profiler_feature_active("layersdump")) {
    std::stringstream ss;
    Dump(ss);
    profiler_log(ss.str().c_str());
  }

  // Dump to LayerScope Viewer
  if (LayerScope::CheckSendable()) {
    // Create a LayersPacket, dump Layers into it and transfer the
    // packet('s ownership) to LayerScope.
    auto packet = MakeUnique<layerscope::Packet>();
    layerscope::LayersPacket* layersPacket = packet->mutable_layers();
    this->Dump(layersPacket);
    LayerScope::SendLayerDump(Move(packet));
  }

  /** Our more efficient but less powerful alter ego, if one is available. */
  nsRefPtr<Composer2D> composer2D;
  composer2D = mCompositor->GetWidget()->GetComposer2D();

  // We can't use composert2D if we have layer effects
  if (!mTarget && !haveLayerEffects &&
      gfxPrefs::Composer2DCompositionEnabled() &&
      composer2D && composer2D->HasHwc() && composer2D->TryRenderWithHwc(mRoot,
          mCompositor->GetWidget(), mGeometryChanged))
  {
    LayerScope::SetHWComposed();
    if (mFPS) {
      double fps = mFPS->mCompositionFps.AddFrameAndGetFps(TimeStamp::Now());
      if (gfxPrefs::LayersDrawFPS()) {
        printf_stderr("HWComposer: FPS is %g\n", fps);
      }
    }
    mCompositor->EndFrameForExternalComposition(Matrix());
    // Reset the invalid region as compositing is done
    mInvalidRegion.SetEmpty();
    mLastFrameMissedHWC = false;
    return;
  } else if (!mTarget && !haveLayerEffects) {
    mLastFrameMissedHWC = !!composer2D;
  }

  {
    PROFILER_LABEL("LayerManagerComposite", "PreRender",
      js::ProfileEntry::Category::GRAPHICS);

    if (!mCompositor->GetWidget()->PreRender(this)) {
      return;
    }
  }

  nsIntRegion invalid;
  if (mTarget) {
    invalid = mTargetBounds;
  } else {
    invalid = mInvalidRegion;
    // Reset the invalid region now that we've begun compositing.
    mInvalidRegion.SetEmpty();
  }

  ParentLayerIntRect clipRect;
  Rect bounds(mRenderBounds.x, mRenderBounds.y, mRenderBounds.width, mRenderBounds.height);
  Rect actualBounds;

  CompositorBench(mCompositor, bounds);

  if (mRoot->GetClipRect()) {
    clipRect = *mRoot->GetClipRect();
    Rect rect(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
    mCompositor->BeginFrame(invalid, &rect, bounds, nullptr, &actualBounds);
  } else {
    gfx::Rect rect;
    mCompositor->BeginFrame(invalid, nullptr, bounds, &rect, &actualBounds);
    clipRect = ParentLayerIntRect(rect.x, rect.y, rect.width, rect.height);
  }

  if (actualBounds.IsEmpty()) {
    mCompositor->GetWidget()->PostRender(this);
    return;
  }

  // Allow widget to render a custom background.
  mCompositor->GetWidget()->DrawWindowUnderlay(this, IntRect(actualBounds.x,
                                                               actualBounds.y,
                                                               actualBounds.width,
                                                               actualBounds.height));

  RefPtr<CompositingRenderTarget> previousTarget;
  if (haveLayerEffects) {
    previousTarget = PushGroupForLayerEffects();
  } else {
    mTwoPassTmpTarget = nullptr;
  }

  // Render our layers.
  RootLayer()->Prepare(ViewAs<RenderTargetPixel>(clipRect, PixelCastJustification::RenderTargetIsParentLayerForRoot));
  RootLayer()->RenderLayer(ParentLayerIntRect::ToUntyped(clipRect));

  if (!mRegionToClear.IsEmpty()) {
    nsIntRegionRectIterator iter(mRegionToClear);
    const IntRect *r;
    while ((r = iter.Next())) {
      mCompositor->ClearRect(Rect(r->x, r->y, r->width, r->height));
    }
  }

  if (mTwoPassTmpTarget) {
    MOZ_ASSERT(haveLayerEffects);
    PopGroupForLayerEffects(previousTarget, ParentLayerIntRect::ToUntyped(clipRect),
                            grayscaleVal, invertVal, contrastVal);
  }

  // Allow widget to render a custom foreground.
  mCompositor->GetWidget()->DrawWindowOverlay(this, IntRect(actualBounds.x,
                                                              actualBounds.y,
                                                              actualBounds.width,
                                                              actualBounds.height));

  // Debugging
  RenderDebugOverlay(actualBounds);

  {
    PROFILER_LABEL("LayerManagerComposite", "EndFrame",
      js::ProfileEntry::Category::GRAPHICS);

    mCompositor->EndFrame();
    mCompositor->SetDispAcquireFence(mRoot); // Call after EndFrame()
  }

  if (composer2D) {
    composer2D->Render(mCompositor->GetWidget());
  }

  mCompositor->GetWidget()->PostRender(this);

  RecordFrame();
}
예제 #17
0
static FBLinearPtr
AllocateLinear(
   FBManagerPtr offman,
   int size,
   int granularity,
   pointer privData
){
   ScreenPtr pScreen = offman->pScreen;
   FBLinearLinkPtr linear = NULL;
   FBLinearLinkPtr newlink = NULL;
   int offset = 0, end;

   if(size <= 0) return NULL;

   if (!offman->LinearAreas) return NULL;

   linear = offman->LinearAreas;
   while (linear) {
 	/* Make sure we get a free area that's not an XY fallback case */
      if (!linear->area && linear->free) {
	 offset = (linear->linear.offset + granularity) & ~granularity;
	 end = offset+size;
	 if (end <= (linear->linear.offset + linear->linear.size))
	    break;
      }
      linear = linear->next;
   }
   if (!linear)
      return NULL;

   /* break left */
   if (offset > linear->linear.offset) {
      newlink = xalloc(sizeof(FBLinearLink));
      if (!newlink)
	 return NULL;
      newlink->area = NULL;
      newlink->linear.offset = offset;
      newlink->linear.size = linear->linear.size - (offset - linear->linear.offset);
      newlink->free = 1;
      newlink->next = linear->next;
      linear->linear.size -= newlink->linear.size;
      linear->next = newlink;
      linear = newlink;
   }

   /* break right */
   if (size < linear->linear.size) {
      newlink = xalloc(sizeof(FBLinearLink));
      if (!newlink)
	 return NULL;
      newlink->area = NULL;
      newlink->linear.offset = offset + size;
      newlink->linear.size = linear->linear.size - size;
      newlink->free = 1;
      newlink->next = linear->next;
      linear->linear.size = size;
      linear->next = newlink;
   }

   /* p = middle block */
   linear->linear.granularity = granularity;
   linear->free = 0;
   linear->linear.pScreen = pScreen;
   linear->linear.MoveLinearCallback = NULL;
   linear->linear.RemoveLinearCallback = NULL;
   linear->linear.devPrivate.ptr = NULL;

#ifdef DEBUG
   Dump(offman->LinearAreas);
#endif

   return &(linear->linear);
}
예제 #18
0
void CLimnStream::LoadFeed()
  { 
  if (!m_bCalculate || m_pFeed==NULL)
    return;
 
  MVector Vec = Vector;

  if (0)
    {
    Dbg.PrintLn("Vector @ LoadFeed");
    MVector V=Vector;
    for (int i=0; i<V.Count(); i++)
      Dbg.PrintLn("M[%-25s] : %10.4f", gs_MVDefn[i].Symbol(), V.M[i]);
    Dbg.PrintLn("");
    }

  double OreMass=0;
  for (int iSG=0; iSG<gs_DWCfg.nSGs(); iSG++)
    OreMass += Vec.M[gs_DWCfg.OreSpIds(iSG)];

  CArray <double, double> OreMasses;
  OreMasses.SetSize(gs_DWCfg.OreBlockCount());
	OreMassFromDensimetricDistribution(&gs_DWCfg, 
											               &m_pFeed->m_Densimetrics[0], 
											               &m_pFeed->m_OreSizeFeed[0], 
											               OreMass,//double totalFlow, 
											               &OreMasses[0] ) ;

  CArray <double, double> DmdDeport;
  DmdDeport.SetSize(gs_DWCfg.DataBlockCount());//DiamondBlockCount());
	DiamondDeportmentFromSGDistribution(&gs_DWCfg,
                                      &m_pFeed->m_Densimetrics[0], 
                                      &m_pFeed->m_OreSizeFeed[0], 
                                      &m_pFeed->m_DmdSGFeed[0], 
                                      &m_pFeed->m_DmdSizeFeed[0], 
                                      Vec.M[gs_DWCfg.DiamondSpId()],
                                      &DmdDeport[0]) ;

  m_Data[gs_DWCfg.iWaterLimnStreamIndex()]  = Vec.M[gs_DWCfg.WaterSpId()];
  m_Data[gs_DWCfg.iFeSiLimnStreamIndex()]   = Vec.M[gs_DWCfg.FeSiSpId()];

  //for (int iSz= 0; iSz< gs_DWCfg.nOreSizes(); iSz++ )
  //  {
  //  Dbg.Print("Densimetrics:");
  //  for (int iSG= 0 ; iSG< gs_DWCfg.nSGs(); iSG++ )
  //    Dbg.Print(" %10.4f", m_pFeed->m_Densimetrics[xlIndex(iSz, iSG, gs_DWCfg.nSGs())]);
  //  Dbg.PrintLn("");
  //  }
  //Dbg.PrintLn("");
  //for (int i= 0 ; i< m_pFeed->m_OreSizeFeed.GetCount(); i++ )
  //  Dbg.PrintLn("m_OreSizeFeed[%3i] %10.4f", i, m_pFeed->m_OreSizeFeed[i]);
  //Dbg.PrintLn("");
  //for (int i= 0 ; i< m_pFeed->m_DmdSGFeed.GetCount(); i++ )
  //  Dbg.PrintLn("m_DmdSGFeed[%3i]   %10.4f", i, m_pFeed->m_DmdSGFeed[i]);
  //Dbg.PrintLn("");
  //for (int i= 0 ; i< m_pFeed->m_DmdSizeFeed.GetCount(); i++ )
  //  Dbg.PrintLn("m_DmdSizeFeed[%3i] %10.4f", i, m_pFeed->m_DmdSizeFeed[i]);
  //Dbg.PrintLn("");

  for (int iOSz = 0 ; iOSz < nOreSizes() ; iOSz++ )
    {
    for (int iSG = 0 ; iSG < nSGs(); iSG++ )
      {
      Ore(iOSz, iSG) = OreMasses[xlIndex( iOSz, iSG, nSGs() )] ;
      }
    }

  for (int iDSz = 0 ; iDSz< nDiamondSizes(); iDSz++ )
    {
    for (int iOSz = 0 ; iOSz < nOreSizes() ; iOSz++ )
      {
      for (int iSG = 0 ; iSG < nSGs(); iSG++ )
        {
        Diamond(iDSz, iOSz, iSG)=DmdDeport[gs_DWCfg.iDDIndex(iDSz, iOSz, iSG)];
        }
      }
    }

  m_bIsMassForm = true;

  if (DoDbg)
    Dump("LoadFeed", DoDbg);
  
  ConvertToFracForm(Vector, false);

  if (DoDbg)
    Dump("LoadFeed", DoDbg);
  }; 
예제 #19
0
static void
Dump(const NMEAInfo &basic)
{
  if (basic.date_time_utc.IsDatePlausible())
    printf("Date=%02u.%02u.%04u\n",
           basic.date_time_utc.day, basic.date_time_utc.month, basic.date_time_utc.year);

  if (basic.time_available)
    printf("Time=%02u:%02u:%02u\n",
           basic.date_time_utc.hour, basic.date_time_utc.minute, basic.date_time_utc.second);

  if (!basic.alive)
    printf("GPS not connected\n");
  else if (!basic.gps.satellites_used_available)
    printf("GPS connected\n");
  else
    printf("GPS connected, %d satellites\n", basic.gps.satellites_used);

  if (basic.location_available) {
    printf("Position=");
    Dump(basic.location);
    printf("\n");
  }

  if (basic.track_available)
    printf("TrackBearing=%d\n", (int)basic.track.Degrees());

  if (basic.ground_speed_available)
    printf("GroundSpeed=%d\n", (int)basic.ground_speed);

  if (basic.airspeed_available) {
    printf("TrueAirspeed=%d\n", (int)basic.true_airspeed);
    printf("IndicatedAirspeed=%d\n",
           (int)basic.indicated_airspeed);
  }

  if (basic.gps_altitude_available)
    printf("GPSAltitude=%d\n", (int)basic.gps_altitude);

  if (basic.static_pressure_available)
    printf("StaticPressure=%f hPa\n",
           (double)basic.static_pressure.GetHectoPascal());

  if (basic.pressure_altitude_available)
    printf("PressureAltitude=%d\n", (int)basic.pressure_altitude);

  if (basic.baro_altitude_available)
    printf("BaroAltitude=%d\n", (int)basic.baro_altitude);

  if (basic.total_energy_vario_available)
    printf("TotalEnergyVario=%.1f\n", (double)basic.total_energy_vario);

  if (basic.netto_vario_available)
    printf("NettoVario=%.1f\n", (double)basic.netto_vario);

  if (basic.external_wind_available)
    printf("Wind=%d/%d\n",
           (int)basic.external_wind.bearing.Degrees(),
           (int)basic.external_wind.norm);

  if (basic.temperature_available)
    printf("OutsideAirTemperature=%d\n", (int)basic.temperature.ToKelvin());

  if (basic.humidity_available)
    printf("RelativeHumidity=%d\n", (int)basic.humidity);

  const DeviceInfo &device = basic.device;
  if (!device.product.empty())
    printf("Device.Product=%s\n", device.product.c_str());
  if (!device.serial.empty())
    printf("Device.Serial=%s\n", device.serial.c_str());
  if (!device.hardware_version.empty())
    printf("Device.HardwareVersion=%s\n", device.hardware_version.c_str());
  if (!device.software_version.empty())
    printf("Device.SoftwareVersion=%s\n", device.software_version.c_str());

  const DeviceInfo &device2 = basic.secondary_device;
  if (!device2.product.empty())
    printf("SecondaryDevice.Product=%s\n", device2.product.c_str());
  if (!device2.serial.empty())
    printf("SecondaryDevice.Serial=%s\n", device2.serial.c_str());
  if (!device2.hardware_version.empty())
    printf("SecondaryDevice.HardwareVersion=%s\n",
           device2.hardware_version.c_str());
  if (!device2.software_version.empty())
    printf("SecondaryDevice.SoftwareVersion=%s\n",
           device2.software_version.c_str());

  const FlarmData &flarm = basic.flarm;
  if (flarm.status.available) {
    printf("FLARM rx=%u tx=%u\n", flarm.status.rx, flarm.status.tx);
    printf("FLARM gps=%u\n", (unsigned)flarm.status.gps);
    printf("FLARM alarm=%u\n", (unsigned)flarm.status.alarm_level);
    printf("FLARM traffic=%zu\n", flarm.traffic.list.size());
  }

  if (basic.engine_noise_level_available)
    printf("ENL=%u\n", basic.engine_noise_level);

  if (basic.voltage_available)
    printf("Battery=%fV\n", (double)basic.voltage);

  if (basic.battery_level_available)
    printf("Battery=%f%%\n", (double)basic.battery_level);

  Dump(basic.settings);
}
예제 #20
0
void ResourceCenter::SetFocus(const std::string& focus) {
    if (focus == m_focus)
        return;
    if (focus.empty()) {
        ClearFocus();
        return;
    }
    std::vector<std::string> avail_foci = AvailableFoci();
    if (std::find(avail_foci.begin(), avail_foci.end(), focus) != avail_foci.end()) {
        m_focus = focus;
        if (m_focus == m_focus_turn_initial)
            m_last_turn_focus_changed = m_last_turn_focus_changed_turn_initial;
        else
            m_last_turn_focus_changed = CurrentTurn();
        ResourceCenterChangedSignal();
        return;
    }
    ErrorLogger() << "ResourceCenter::SetFocus Exploiter!-- unavailable focus " << focus << " attempted to be set for object w/ dump string: " << Dump();
}
예제 #21
0
파일: target.cpp 프로젝트: MyOwnClone/logog
	LogBuffer::~LogBuffer()
	{
		Dump();
		Deallocate();
	}
예제 #22
0
HRESULT hrOEMDevMode(DWORD dwMode, POEMDMPARAM pOemDMParam)
{
    POEMDEV pOEMDevIn;
    POEMDEV pOEMDevOut;


    // Verify parameters.
    if( (NULL == pOemDMParam)
        ||
        ( (OEMDM_SIZE != dwMode)
          &&
          (OEMDM_DEFAULT != dwMode)
          &&
          (OEMDM_CONVERT != dwMode)
          &&
          (OEMDM_MERGE != dwMode)
        )
      )
    {
        ERR(ERRORTEXT("DevMode() ERROR_INVALID_PARAMETER.\r\n"));
        VERBOSE(DLLTEXT("\tdwMode = %d, pOemDMParam = %#lx.\r\n"), dwMode, pOemDMParam);

        SetLastError(ERROR_INVALID_PARAMETER);
        return E_FAIL;
    }

    // Cast generic (i.e. PVOID) to OEM private devomode pointer type.
    pOEMDevIn = (POEMDEV) pOemDMParam->pOEMDMIn;
    pOEMDevOut = (POEMDEV) pOemDMParam->pOEMDMOut;

    switch(dwMode)
    {
		//
		//The Method should return the size of the memory allocation needed to store the UI plugin Private DEVMODE.
		//
        case OEMDM_SIZE:
            pOemDMParam->cbBufSize = sizeof(OEMDEV);
            break;

		//
		//Should fill the Private DEVMODE with the default values.
		//
        case OEMDM_DEFAULT:
			//
			//OEM_DMEXTRAHEADER Members
			//
            pOEMDevOut->dmOEMExtra.dwSize       = sizeof(OEMDEV);
            pOEMDevOut->dmOEMExtra.dwSignature  = OEM_SIGNATURE;
            pOEMDevOut->dmOEMExtra.dwVersion    = OEM_VERSION;

			//
			//Private members
			//
            pOEMDevOut->dwDriverData            = 0;
            pOEMDevOut->dwAdvancedData          = 0;

            VERBOSE(DLLTEXT("pOEMDevOut after setting default values:\r\n"));
            Dump(pOEMDevOut);			
            break;
			
		//
		//The method should convert private DEVMODE members to the current version, if necessary.
		//
        case OEMDM_CONVERT:
            ConvertOEMDevmode(pOEMDevIn, pOEMDevOut);
            break;
		
		//
		//The method should validate the information contained in private DEVMODE members and merge validated values into a private DEVMODE structure containing default values
		//
        case OEMDM_MERGE:
            ConvertOEMDevmode(pOEMDevIn, pOEMDevOut);
            MakeOEMDevmodeValid(pOEMDevOut);
            break;
    }
    Dump(pOemDMParam);

    return S_OK;
}
예제 #23
0
파일: VM.cpp 프로젝트: tsmigiel/compiler
std::ostream &
VariableRef::DumpTrace(std::ostream &o, VM &vm, const int bb, const int pc)
{
	return Dump(o) << "=" << Get(vm) << " ";
}
예제 #24
0
파일: main.cpp 프로젝트: dcdolson/dnsmon
int main(int argc, char* const* argv)
{
    DnsOptions options(argc, argv);

    int s = socket(PF_INET, SOCK_DGRAM, 0);
    if (s < 0)
    {
        syslog(LOG_LOCAL0|LOG_ERR, "Failure to open server socket: %m");
        exit(1);
    }
    
    // Listen port from internal clients.
    struct sockaddr_in sa;
    sa.sin_family = AF_INET;
    sa.sin_port = htons(options.Port()); // DNS
    sa.sin_addr.s_addr = INADDR_ANY;
    if (0 != bind(s, (struct sockaddr*)&sa, sizeof(sa)))
    {
        syslog(LOG_LOCAL0|LOG_ERR, "Failure to bind socket: %m");
        exit(1);
    }

    int client_socket = socket(PF_INET, SOCK_DGRAM, 0);
    if (client_socket < 0)
    {
        syslog(LOG_LOCAL0|LOG_ERR, "Failure to open client socket: %m");
        exit(1);
    }

    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(53);
    if (1 != inet_pton(AF_INET, options.ServerAddr(), &server.sin_addr))
    {
        syslog(LOG_LOCAL0|LOG_ERR, "Cannot parse server address '%s' as IPv4", options.ServerAddr());
        std::cerr << "Problem parsing server address '" << options.ServerAddr() << "' as IPv4" << std::endl;
        exit(1);
    }
    if (0 != connect(client_socket, (struct sockaddr*)&server, sizeof(server)))
    {
        syslog(LOG_LOCAL0|LOG_ERR, "Failure connecting to server: %m");
        exit(1);
    }

    if(options.IsDaemon())
    {
        daemon(0, 0);
    }

    uint16_t g_txn = 1;
    using tmap = std::unordered_map<uint16_t, std::pair<struct sockaddr_in, uint16_t> >;
    tmap transaction_map;

    while(1)
    {
        fd_set readfds;
        FD_ZERO(&readfds);
        FD_SET(s, &readfds);
        FD_SET(client_socket, &readfds);

        int n = select(std::max(s,client_socket)+1, &readfds, /*writefds*/ nullptr, /*exceptfds*/nullptr, /*timeout*/nullptr);
        if(n < 0)
        {
            syslog(LOG_LOCAL0|LOG_ERR, "Select() error: %m");
            exit(1);
        }

        uint8_t message[8192];

        if (FD_ISSET(s, &readfds))
        {
            // from internal host
            struct sockaddr_in from;
            socklen_t fromlen = sizeof(from);
            memset(&from, 0, fromlen);
            ssize_t bytes = recvfrom(s, message, sizeof(message), /*flags*/ 0, (struct sockaddr*)&from, &fromlen);
            if(bytes > 0)
            {
                // PacketDump(std::cout, message, bytes);
                if (fromlen == sizeof(from))
                {
                    uint16_t txn = ntohs(*(uint16_t*)message);
                    // std::cout << "Received " << bytes << " bytes from " << to_string(from.sin_addr)
                    //           << " : " << ntohs(from.sin_port) << " txn: " << txn << std::endl;
                    Dump(from, message, bytes);

                    // new transaction that makes sense to us, as a key for response.
                    uint16_t new_txn = ++g_txn;
                    transaction_map[new_txn] = std::make_pair(from, txn);
                    *(uint16_t*)message = htons(new_txn);
                    if(bytes != send(client_socket, message, bytes, /*flags*/ 0))
                    {
                        syslog(LOG_LOCAL0|LOG_ERR, "Trouble sending to server: %m");
                    }
                }
            }
            else
            {
                syslog(LOG_LOCAL0|LOG_ERR, "Error on server socket recvfrom: %m");
                exit(1);
            }
        }

        if(FD_ISSET(client_socket, &readfds))
        {
            // response from server
            struct sockaddr_in from;
            socklen_t fromlen = sizeof(from);
            memset(&from, 0, fromlen);
            ssize_t bytes = recvfrom(client_socket, message, sizeof(message), /*flags*/ 0, (struct sockaddr*)&from, &fromlen);
            if(bytes > 2 && fromlen == sizeof(from))
            {
                // PacketDump(std::cout, message, bytes);
                uint16_t txn = ntohs(*(uint16_t*)message);
                // std::cout << "Received " << bytes << " bytes from " << to_string(from.sin_addr)
                //           << " : " << ntohs(from.sin_port) << " txn: " << txn << std::endl;
                tmap::iterator i = transaction_map.find(txn);
                if(i != transaction_map.end())
                {
                    *(uint16_t*)message = htons(i->second.second);
                    ssize_t sent = sendto(s, message, bytes, /*flags*/ 0,
                                          (struct sockaddr*)&i->second.first, sizeof(struct sockaddr_in));
                    if(sent < 0)
                    {
                        syslog(LOG_LOCAL0|LOG_ERR, "Error sending back to client: %m");
                    }
                    transaction_map.erase(i);
                }
                else
                {
                    syslog(LOG_LOCAL0|LOG_WARNING, "Response transaction not found");
                }
            }
            else
            {
                syslog(LOG_LOCAL0|LOG_ERR, "Error on client socket recvfrom: %m");
                exit(1);
            }
        }
    }

}
예제 #25
0
	bool OLE::readFromDevice (void)
	{
	CHECK_DEVICE;

	#ifdef DEBUG_OBJECT
		m_device->debug ("\n<<<< OLE::readFromDevice >>>>\n");
	#endif

		if (!OLEGenerated::readFromDevice ())
			return false;

	#ifdef DEBUG_OBJECT
		Dump (zero);

		switch (m_objectType)
		{
		case OLEType::Static:
			m_device->debug ("\tobjectType: 1 - static\n");
			break;
		case OLEType::Embedded:
			m_device->debug ("\tobjectType: 2 - embedded\n");
			break;
		case OLEType::Link:
			m_device->debug ("\tobjectType: 3 - link\n");
			break;
		}

		Dump (indent);
		Dump (width);
		Dump (height);
		Dump (zero2);
		Dump (numDataBytes);
		Dump (zero3);
		Dump (objectName);
		Dump (zero4);
		Dump (numHeaderBytes);
		Dump (zero5);
		Dump (widthScaledRel1000);
		Dump (heightScaledRel1000);
	#endif

		// OPT: TODO: this is dumb, we read it only to give it back to the parser who tells the generator to write it
		m_externalObject = new Byte [m_externalObjectSize = getNumDataBytes ()];
		if (!m_externalObject)
			ErrorAndQuit (Error::OutOfMemory, "could not allocate memory for external OLE object\n");

		if (!m_device->readInternal (m_externalObject, m_externalObjectSize))
			return false;

		return true;
	}
예제 #26
0
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, const uint8_t wIndex, void** const DescriptorAddress)
{
    const uint8_t  DescriptorType   = (wValue >> 8);
    const uint8_t  DescriptorNumber = (wValue & 0xFF);

    void*    Address = NULL;
    uint16_t Size    = NO_DESCRIPTOR;

    struct SDynamicHID *pHIDData = (struct SDynamicHID *)DynamicHIDData;

    switch (DescriptorType)
    {
	case DTYPE_Device:
	    Address = DynamicHIDData + pHIDData->nDeviceDescriptorOffset;
	    Size    = pHIDData->nDeviceDescriptorLength;
	    if ( nSerialDebugLevel > 10 )
	    {
            UART1_Send_P( PSTR("Dev ") );
            UART1_SendInt( pHIDData->nDeviceDescriptorOffset );
            UART1_Send_P( PSTR(" ") );
            UART1_SendInt( pHIDData->nDeviceDescriptorLength );
            UART1_SendCRLF();
	    }
	    break;

	case DTYPE_Configuration:
	    Address = DynamicHIDData + pHIDData->nConfigDescriptorOffset;
	    Size    = pHIDData->nConfigDescriptorLength;
	    if ( nSerialDebugLevel > 10 )
	    {
            UART1_Send_P( PSTR("Config ") );
            UART1_SendInt( pHIDData->nConfigDescriptorOffset );
            UART1_Send_P( PSTR(" ") );
            UART1_SendInt( pHIDData->nConfigDescriptorLength);
            UART1_SendCRLF();
	    }
	    break;

	case DTYPE_String:
	    // TODO - error check string table length
	    Address = DynamicHIDData + pHIDData->nStringIndex0Offset[DescriptorNumber];
	    Size    = *(byte *)(Address);
	    if ( nSerialDebugLevel > 10 )
	    {
            UART1_Send_P( PSTR("Str ") );
            UART1_SendInt( DescriptorNumber );
            UART1_Send_P( PSTR(" ") );
            UART1_SendInt( pHIDData->nStringIndex0Offset[DescriptorNumber] );
            UART1_Send_P( PSTR(" ") );
            UART1_SendInt( Size);
            UART1_SendCRLF();
	    }
	    break;

	case DTYPE_HID:
	    Address = DynamicHIDData + pHIDData->nHIDDescriptorOffset;
	    Size    = pHIDData->nHIDDescriptorLength;
	    if ( nSerialDebugLevel > 10 )
	    {
            UART1_Send_P( PSTR("HID ") );
            UART1_SendInt( pHIDData->nHIDDescriptorOffset );
            UART1_Send_P( PSTR(" ") );
            UART1_SendInt( pHIDData->nHIDDescriptorLength);
            UART1_SendCRLF();
	    }
	    break;

	case DTYPE_Report:
	    Address = DynamicHIDData + pHIDData->nReportDescriptorOffset;
	    Size    = pHIDData->nReportDescriptorLength;
	    if ( nSerialDebugLevel > 10 )
	    {
            UART1_Send_P( PSTR("Report ") );
            UART1_SendInt( pHIDData->nReportDescriptorOffset );
            UART1_Send_P( PSTR(" ") );
            UART1_SendInt( pHIDData->nReportDescriptorLength);
            UART1_SendCRLF();
	    }
	    break;
    }
    if ( nSerialDebugLevel > 20 && Size > 0 )
    {
        Dump( Address, Size );
    }

    *DescriptorAddress = Address;
    return Size;
}
예제 #27
0
	bool Image::readFromDevice (void)
	{
	CHECK_DEVICE;

	#ifdef DEBUG_IMAGE
		m_device->debug ("\n<<<< Image::readFromDevice >>>>\n");
	#endif

		if (!ImageGenerated::readFromDevice ())
			return false;

	#ifdef DEBUG_IMAGE
		Dump (mappingMode);
		Dump (MFP_width);
		Dump (MFP_height);
		Dump (MFP_unknown);

		Dump (indent);
		Dump (width);
		Dump (height);
		Dump (zero);

		Dump (numHeaderBytes);
		Dump (numDataBytes);
		Dump (horizontalScalingRel1000);
		Dump (verticalScalingRel1000);
	#endif

		if (getIsWMF ())
		{
			//
			// get image dimensions
			//

			if (m_bmh->getWidth () || m_bmh->getHeight ())
				m_device->error (Error::Warn, "m_bmh structure should be 0 for WMFs\n");

			m_originalWidth = Milli2Twip (double (m_MFP_width) / 100.0) * 4.0/3.0;
			m_originalHeight = Milli2Twip (double (m_MFP_height) / 100.0) * 4.0/3.0;

			m_displayedWidth = double (m_width);
			m_displayedHeight = double (m_height);

			if (m_horizontalScalingRel1000 != 1000)
				m_device->error (Error::Warn, "horizontal scaling should not be set for WMFs\n");
			if (m_verticalScalingRel1000 != 1000)
				m_device->error (Error::Warn, "vertical scaling should not be set for WMFs\n");


			//
			// read image
			//

			m_externalImage = new Byte [m_externalImageSize = getNumDataBytes ()];
			if (!m_externalImage)
				ErrorAndQuit (Error::OutOfMemory, "could not allocate memory for external WMF image\n");

			if (!m_device->readInternal (m_externalImage, m_externalImageSize))
				ErrorAndQuit (Error::FileError, "could not read internal WMF\n");

			// Header check
			WMFHeader wmfHeader;
			m_device->setCache (m_externalImage);
				wmfHeader.setDevice (m_device);
				if (!wmfHeader.readFromDevice ())
					return false;

				// TODO: fix this incorrect check (see wmf.cpp)
				//if (wmfHeader.getFileSize () * sizeof (Word) != m_numDataBytes)
				//	m_device->error (Error::Warn, "wmfHeader.fileSize != numDataBytes\n");
			m_device->setCache (NULL);
		}
		else	//	if (getIsBMP ())
		{
			//
			// get image dimensions
			//

			m_originalWidth = Point2Twip (DWord (m_bmh->getWidth ()));
			m_originalHeight = Point2Twip (DWord (m_bmh->getHeight ()));

			m_displayedWidth = m_originalWidth / 1.38889 * m_horizontalScalingRel1000 / 1000;
			m_displayedHeight = m_originalHeight / 1.38889 * m_verticalScalingRel1000 / 1000;

#define MSWrite_fabs(val) (((val)>=0)?(val):(-(val)))

			if (MSWrite_fabs (m_MFP_width / double (m_bmh->getWidth ()) - 2.64) > .3)
				m_device->error (Error::Warn, "m_MFP_width != m_bmh->getWidth() * 2.64\n");
			if (MSWrite_fabs (m_MFP_height / double (m_bmh->getHeight ()) - 2.64) > .3)
				m_device->error (Error::Warn, "m_MFP_height != m_bmh->getHeight() * 2.64\n");

#undef MSWrite_fabs

			if (m_width)
				m_device->error (Error::Warn, "m_width should not be set for BMPs\n");

			if (m_height)
				m_device->error (Error::Warn, "m_height should not be set for BMPs\n");


			//
			// read image
			//

			Byte *internalData = new Byte [getNumDataBytes ()];
			if (!internalData)
				ErrorAndQuit (Error::OutOfMemory, "could not allocate memory for internal BMP image\n");
			if (!m_device->readInternal (internalData, getNumDataBytes ()))
				ErrorAndQuit (Error::FileError, "could not read internal BMP\n");

			// infoHeader
			BMP_BitmapInfoHeader infoHeader;
			infoHeader.setWidth (m_bmh->getWidth ());
			infoHeader.setHeight (m_bmh->getHeight ());
			if (m_bmh->getNumPlanes () != 1)
				ErrorAndQuit (Error::InvalidFormat, "bmh.m_numPlanes != 1\n");
			infoHeader.setNumPlanes (m_bmh->getNumPlanes ());
			infoHeader.setBitsPerPixel (m_bmh->getBitsPerPixel ());
			infoHeader.setCompression (0);	// BI_RGB (uncompressed)
			infoHeader.setSizeImage (0);		// lazy
			infoHeader.setXPixelsPerMeter (0), infoHeader.setYPixelsPerMeter (0);
			infoHeader.setColorsUsed (1 << infoHeader.getBitsPerPixel ());
			infoHeader.setColorsImportant (infoHeader.getColorsUsed ());

			if (infoHeader.getColorsUsed () != 2)
				ErrorAndQuit (Error::InternalError, "color bitmap???  Please email this file to <*****@*****.**>\n");

			Word colorTableSize = infoHeader.getColorsUsed () * BMP_BitmapColorIndex::s_size;

			// fileHeader
			BMP_BitmapFileHeader fileHeader;
			DWord fileSize = BMP_BitmapFileHeader::s_size + BMP_BitmapInfoHeader::s_size
										+ colorTableSize
										+ (m_bmh->getHeight ()
											* getBytesPerScanLine (m_bmh->getWidth (), m_bmh->getBitsPerPixel (), 4));

			fileHeader.setTotalBytes (fileSize);
			fileHeader.setActualImageOffset (BMP_BitmapFileHeader::s_size + BMP_BitmapInfoHeader::s_size
														+ colorTableSize);

			// colorTable
			BMP_BitmapColorIndex *colorIndex = new BMP_BitmapColorIndex [infoHeader.getColorsUsed ()];
			if (!colorIndex)
				ErrorAndQuit (Error::OutOfMemory, "could not allocate memory for colorIndex[]\n");


			// black and white...
			colorIndex [0].setRed (0), colorIndex [0].setGreen (0), colorIndex [0].setBlue (0);
			colorIndex [1].setRed (0xFF), colorIndex [1].setGreen (0xFF), colorIndex [1].setBlue (0xFF);

			m_externalImage = new Byte [m_externalImageSize = fileSize];
			if (!m_externalImage)
				ErrorAndQuit (Error::OutOfMemory, "could not allocate memory for external BMP image\n");

			MemoryDevice device;
			device.setCache (m_externalImage);
			fileHeader.setDevice (&device);
			fileHeader.writeToDevice ();
			infoHeader.setDevice (&device);
			infoHeader.writeToDevice ();
			for (int i = 0; i < 2; i++)
			{
				colorIndex [i].setDevice (&device);
				colorIndex [i].writeToDevice ();
			}

			// (BMP padded to 4 bytes vs WRI input bitmap which is actually padded to 2)
			Word scanLineWRILength = getBytesPerScanLine (infoHeader.getWidth (), infoHeader.getBitsPerPixel (), 2);
			if (scanLineWRILength != m_bmh->getWidthBytes ())
				ErrorAndQuit (Error::InvalidFormat, "scanLineWRILength != m_bmh->getWidthBytes()\n");
			Word scanLineBMPLength = getBytesPerScanLine (infoHeader.getWidth (), infoHeader.getBitsPerPixel (), 4);

		#ifdef DEBUG_IMAGE
			m_device->debug ("in: scanLineWRILength: ", scanLineWRILength);
			m_device->debug ("out: scanLineBMPLength: ", scanLineBMPLength);
		#endif

			// sanity check
			DWord expectedSize = DWord (infoHeader.getHeight ()) * DWord (scanLineWRILength);
			if (expectedSize != getNumDataBytes ())
			{
				if (expectedSize > getNumDataBytes ())
				{
					// better quit instead of reading past end of internalData[]
					ErrorAndQuit (Error::InvalidFormat, "infoHeader.getHeight () * scanLineWRILength > numDataBytes\n");
				}
				else
					m_device->error (Error::Warn, "infoHeader.getHeight () * scanLineWRILength != numDataBytes\n");
			}

			Byte *padding = new Byte [scanLineBMPLength - scanLineWRILength];
			if (!padding)
				ErrorAndQuit (Error::OutOfMemory, "could not allocate memory for scanline\n");
			memset (padding, 0, scanLineBMPLength - scanLineWRILength);

			// the DIB is upside-down...
			for (int i = (int) infoHeader.getHeight () - 1; i >= 0; i--)
			{
				// write bitmap scanline
				device.writeInternal (internalData + i * scanLineWRILength, scanLineWRILength * sizeof (Byte));

				// write padding for scanline
				device.writeInternal (padding, (scanLineBMPLength - scanLineWRILength) * sizeof (Byte));
			}

			delete [] padding;

			device.setCache (NULL);

			delete [] colorIndex;
			delete [] internalData;
		}

		return true;
	}
예제 #28
0
void
LayerManagerComposite::Render(const nsIntRegion& aInvalidRegion, const nsIntRegion& aOpaqueRegion)
{
  PROFILER_LABEL("LayerManagerComposite", "Render",
    js::ProfileEntry::Category::GRAPHICS);

  if (mDestroyed || !mCompositor || mCompositor->IsDestroyed()) {
    NS_WARNING("Call on destroyed layer manager");
    return;
  }

  ClearLayerFlags(mRoot);

  // At this time, it doesn't really matter if these preferences change
  // during the execution of the function; we should be safe in all
  // permutations. However, may as well just get the values onces and
  // then use them, just in case the consistency becomes important in
  // the future.
  bool invertVal = gfxPrefs::LayersEffectInvert();
  bool grayscaleVal = gfxPrefs::LayersEffectGrayscale();
  float contrastVal = gfxPrefs::LayersEffectContrast();
  bool haveLayerEffects = (invertVal || grayscaleVal || contrastVal != 0.0);

  // Set LayerScope begin/end frame
  LayerScopeAutoFrame frame(PR_Now());

  // Dump to console
  if (gfxPrefs::LayersDump()) {
    this->Dump(/* aSorted= */true);
  } else if (profiler_feature_active("layersdump")) {
    std::stringstream ss;
    Dump(ss);
    profiler_log(ss.str().c_str());
  }

  // Dump to LayerScope Viewer
  if (LayerScope::CheckSendable()) {
    // Create a LayersPacket, dump Layers into it and transfer the
    // packet('s ownership) to LayerScope.
    auto packet = MakeUnique<layerscope::Packet>();
    layerscope::LayersPacket* layersPacket = packet->mutable_layers();
    this->Dump(layersPacket);
    LayerScope::SendLayerDump(Move(packet));
  }

  mozilla::widget::WidgetRenderingContext widgetContext;
#if defined(XP_MACOSX)
  widgetContext.mLayerManager = this;
#elif defined(MOZ_WIDGET_ANDROID)
  widgetContext.mCompositor = GetCompositor();
#endif

  {
    PROFILER_LABEL("LayerManagerComposite", "PreRender",
      js::ProfileEntry::Category::GRAPHICS);

    if (!mCompositor->GetWidget()->PreRender(&widgetContext)) {
      return;
    }
  }

  ParentLayerIntRect clipRect;
  IntRect bounds(mRenderBounds.x, mRenderBounds.y, mRenderBounds.width, mRenderBounds.height);
  IntRect actualBounds;

  CompositorBench(mCompositor, bounds);

  MOZ_ASSERT(mRoot->GetOpacity() == 1);
#if defined(MOZ_WIDGET_ANDROID)
  LayerMetricsWrapper wrapper = GetRootContentLayer();
  if (wrapper) {
    mCompositor->SetClearColor(wrapper.Metadata().GetBackgroundColor());
  } else {
    mCompositor->SetClearColorToDefault();
  }
#endif
  if (mRoot->GetClipRect()) {
    clipRect = *mRoot->GetClipRect();
    IntRect rect(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
    mCompositor->BeginFrame(aInvalidRegion, &rect, bounds, aOpaqueRegion, nullptr, &actualBounds);
  } else {
    gfx::IntRect rect;
    mCompositor->BeginFrame(aInvalidRegion, nullptr, bounds, aOpaqueRegion, &rect, &actualBounds);
    clipRect = ParentLayerIntRect(rect.x, rect.y, rect.width, rect.height);
  }

  if (actualBounds.IsEmpty()) {
    mCompositor->GetWidget()->PostRender(&widgetContext);
    return;
  }

  // Allow widget to render a custom background.
  mCompositor->GetWidget()->DrawWindowUnderlay(
    &widgetContext, LayoutDeviceIntRect::FromUnknownRect(actualBounds));

  RefPtr<CompositingRenderTarget> previousTarget;
  if (haveLayerEffects) {
    previousTarget = PushGroupForLayerEffects();
  } else {
    mTwoPassTmpTarget = nullptr;
  }

  // Render our layers.
  RootLayer()->Prepare(ViewAs<RenderTargetPixel>(clipRect, PixelCastJustification::RenderTargetIsParentLayerForRoot));
  RootLayer()->RenderLayer(clipRect.ToUnknownRect());

  if (!mRegionToClear.IsEmpty()) {
    for (auto iter = mRegionToClear.RectIter(); !iter.Done(); iter.Next()) {
      const IntRect& r = iter.Get();
      mCompositor->ClearRect(Rect(r.x, r.y, r.width, r.height));
    }
  }

  if (mTwoPassTmpTarget) {
    MOZ_ASSERT(haveLayerEffects);
    PopGroupForLayerEffects(previousTarget, clipRect.ToUnknownRect(),
                            grayscaleVal, invertVal, contrastVal);
  }

  // Allow widget to render a custom foreground.
  mCompositor->GetWidget()->DrawWindowOverlay(
    &widgetContext, LayoutDeviceIntRect::FromUnknownRect(actualBounds));

  // Debugging
  RenderDebugOverlay(actualBounds);

  {
    PROFILER_LABEL("LayerManagerComposite", "EndFrame",
      js::ProfileEntry::Category::GRAPHICS);

    mCompositor->EndFrame();

    // Call after EndFrame()
    mCompositor->SetDispAcquireFence(mRoot);
  }

  mCompositor->GetWidget()->PostRender(&widgetContext);

  RecordFrame();
}
예제 #29
0
	bool Image::writeToDevice (void)
	{
	CHECK_DEVICE;

	#ifdef DEBUG_IMAGE
		m_device->debug ("\n>>>> Image::writeToDevice <<<<\n");
	#endif

	#ifdef DEBUG_IMAGE
		Dump (mappingMode);
		//Dump (MFP_width);	// will change below
		//Dump (MFP_height);
		Dump (MFP_unknown);

		Dump (indent);
		//Dump (width);
		//Dump (height);
		Dump (zero);

		Dump (numHeaderBytes);
		//Dump (numDataBytes);
		//Dump (horizontalScalingRel1000);
		//Dump (verticalScalingRel1000);
	#endif

		// sanity checking
		if (!m_externalImage || m_externalImageSize <= 0 ||
				m_originalWidth <= 0 || m_originalHeight <= 0 ||
				m_displayedWidth <= 0 || m_displayedHeight <= 0)
		{
			ErrorAndQuit (Error::InternalError, "uninitialised or invalid Image\n");
		}

		//
		// write data
		//
		//
		if (getIsWMF ())
		{
			// Header check
			WMFHeader wmfHeader;
			m_device->setCache (m_externalImage);
				wmfHeader.setDevice (m_device);
				if (!wmfHeader.readFromDevice ()) return false;

				// TODO: fix this incorrect check (see wmf.cpp)
				//if (wmfHeader.getFileSize () * sizeof (Word) != m_externalImageSize)
				//	m_device->error (Error::Warn, "wmfHeader.fileSize != externalImageSize\n");
			m_device->setCache (NULL);


			//
			// set image dimensions
			//

			// entire BitmapHeader is unused with WMFs
			m_bmh->setWidth (0);
			m_bmh->setHeight (0);
			m_bmh->setWidthBytes (0);
			m_bmh->setNumPlanes (0);
			m_bmh->setBitsPerPixel (0);

			m_MFP_width = Word (Twip2Milli (m_originalWidth * 0.75) * 100.0);
			m_MFP_height = Word (Twip2Milli (m_originalHeight * 0.75) * 100.0);

			m_width = Word (m_displayedWidth);
			m_height = Word (m_displayedHeight);

			// not used by WMFs
			m_horizontalScalingRel1000 = m_verticalScalingRel1000 = 1000;


			// write header
			setNumDataBytes (m_externalImageSize);
			if (!ImageGenerated::writeToDevice ())
				return false;

			// external=internal with WMF (i.e. we really do write a WMF)
			if (!m_device->writeInternal (m_externalImage, m_externalImageSize)) return false;
		}
		else	//	if (getIsBMP ())
		{
			m_device->setCache (m_externalImage);

			BMP_BitmapFileHeader fileHeader;
			fileHeader.setDevice (m_device);
			if (!fileHeader.readFromDevice ()) return false;


			/*Word colorTableSize = (1 << m_bmh->getNumPlanes ()) * BMP_BitmapColorIndex::s_size;

			// fileHeader
			DWord fileSize = BMP_BitmapFileHeader::s_size + BMP_BitmapInfoHeader::s_size
												+ colorTableSize
												+ (m_bmh->getHeight ()
													* getBytesPerScanLine (m_bmh->getWidth (), m_bmh->getBitsPerPixel (), 4));

			fileHeader.setTotalBytes (fileSize);
			fileHeader.setActualImageOffset (BMP_BitmapFileHeader::s_size + BMP_BitmapInfoHeader::s_size
														+ colorTableSize);*/

			// infoHeader
			BMP_BitmapInfoHeader infoHeader;
			infoHeader.setDevice (m_device);
			if (!infoHeader.readFromDevice ()) return false;

			// write out each scanline
			// to .WRI (padded to 2) vs input BMP (padded to 4
			Word scanLineWRILength = getBytesPerScanLine (infoHeader.getWidth (), infoHeader.getBitsPerPixel (), 2);
			Word scanLineBMPLength = getBytesPerScanLine (infoHeader.getWidth (), infoHeader.getBitsPerPixel (), 4);

			if (infoHeader.getWidth () <= 0 || infoHeader.getHeight () <= 0)
				ErrorAndQuit (Error::InvalidFormat, "infoHeader invalid dimensions\n");

			// did the user lie about the dimensions of the BMP?
			if (infoHeader.getWidth () != Word (Twip2Point (m_originalWidth)))
				m_device->error (Error::Warn, "infoHeader width != m_originalWidth\n");
			if (infoHeader.getHeight () != Word (Twip2Point (m_originalHeight)))
				m_device->error (Error::Warn, "infoHeader.height != m_originalHeight\n");

			m_bmh->setWidth (infoHeader.getWidth ());
			m_bmh->setHeight (infoHeader.getHeight ());
			m_bmh->setWidthBytes (scanLineWRILength);
			if (infoHeader.getNumPlanes () != 1)
				ErrorAndQuit (Error::InvalidFormat, "infoHeader.getNumPlanes() != 1\n");
			m_bmh->setNumPlanes (infoHeader.getNumPlanes ());
			m_bmh->setBitsPerPixel (infoHeader.getBitsPerPixel ());
			if (infoHeader.getCompression () != 0)	// BI_RGB (uncompressed)
				ErrorAndQuit (Error::Unsupported, "compressed bitmaps unsupported\n");
			//infoHeader.setSizeImage (0);		// lazy
			//infoHeader.setXPixelsPerMeter (0), infoHeader.setYPixelsPerMeter (0);
			infoHeader.setColorsUsed (1 << infoHeader.getBitsPerPixel ());	// make life easier
			//infoHeader.setColorsImportant (infoHeader.getColorsUsed ());

			if (infoHeader.getColorsUsed () != 2)
				ErrorAndQuit (Error::Unsupported, "can't save color BMPs, use WMFs for that purpose\n");

			// colorTable
			BMP_BitmapColorIndex *colorIndex = new BMP_BitmapColorIndex [infoHeader.getColorsUsed ()];
			if (!colorIndex)
				ErrorAndQuit (Error::OutOfMemory, "could not allocate memory for colorIndex[]\n");
			colorIndex [0].setDevice (m_device);
			if (!colorIndex [0].readFromDevice ()) return false;
			if (colorIndex [0].getRed () != 0 || colorIndex [0].getGreen () != 0 || colorIndex [0].getBlue () != 0)
				m_device->error (Error::Warn, "black not black\n");
			colorIndex [1].setDevice (m_device);
			if (!colorIndex [1].readFromDevice ()) return false;
			if (colorIndex [1].getRed () != 0xFF || colorIndex [1].getGreen () != 0xFF || colorIndex [1].getBlue () != 0xFF)
				m_device->error (Error::Warn, "white not white\n");

			// finish reading from m_externalImage
			m_device->setCache (NULL);


			//
			// set image dimensions
			//

			m_MFP_width = Word (Twip2Point (m_originalWidth) * 2.64);
			m_MFP_height = Word (Twip2Point (m_originalHeight) * 2.64);

			// BMPs don't use
			m_width = 0, m_height = 0;

			m_horizontalScalingRel1000 = Word (m_displayedWidth * 1.38889 * 1000.0 / m_originalWidth);
			m_verticalScalingRel1000 = Word (m_displayedHeight * 1.38889 * 1000.0 / m_originalHeight);


			// write header
			setNumDataBytes (infoHeader.getHeight () * scanLineWRILength);
			if (!ImageGenerated::writeToDevice ())
				return false;

			// sanity check
			DWord expectedSize = DWord (infoHeader.getHeight ()) * DWord (scanLineBMPLength);
			DWord imageSize = m_externalImageSize - fileHeader.getActualImageOffset ();
			if (expectedSize != imageSize)
			{
				if (expectedSize > imageSize)
				{
					// better quit instead of reading past end of m_externalImage[]
					ErrorAndQuit (Error::InvalidFormat, "infoHeader.getHeight () * scanLineBMPLength > imageSize\n");
				}
				else
					m_device->error (Error::Warn, "infoHeader.getHeight () * scanLineBMPLength != imageSize\n");
			}

			// the DIB is upside-down...
			Byte *bmpData = m_externalImage + fileHeader.getActualImageOffset () + (infoHeader.getHeight () - 1) * scanLineBMPLength;
			for (int i = (int) infoHeader.getHeight () - 1; i >= 0; i--)
			{
				// write bitmap scanline (padded to 2)
				//if (!m_device->writeInternal (m_externalImage + fileHeader.getActualImageOffset () + i * scanLineBMPLength, scanLineWRILength))
				if (!m_device->writeInternal (bmpData, scanLineWRILength))
					return false;

				bmpData -= scanLineBMPLength;
			}

			delete [] colorIndex;
		}

		return true;
	}
예제 #30
0
void Coin::ReadBIO(BIO *bio)
    {
    UnsignedCoin::ReadBIO(bio);
    m_bnCoinSignature=ReadNumber(bio,"signature=");
    Dump();
    }