Пример #1
0
Boolean PosixBaseStorageObject::canSeek(int fd)
{
  struct stat sb;
  if (fstat(fd, &sb) < 0 || !S_ISREG(sb.st_mode)
      || (startOffset_ = lseek(fd, off_t(0), SEEK_CUR)) < 0)
    return 0;
  else
    return 1;
}
Пример #2
0
hostptr_t Memory::shadow(hostptr_t addr, size_t count)
{
	TRACE(GLOBAL, "Getting shadow mapping for %p ("FMT_SIZE" bytes)", addr, count);
	FileMapEntry entry = Files.find(addr);
	if(entry.handle() == NULL) return NULL;
	off_t offset = off_t(addr - entry.address());
	hostptr_t ret = (hostptr_t)MapViewOfFile(entry.handle(), FILE_MAP_WRITE, 0, (DWORD)offset, (DWORD)count);
	return ret;
}
Пример #3
0
inline bool truncate_file (file_handle_t hnd, std::size_t size)
{
   typedef boost::make_unsigned<off_t>::type uoff_t;
   if(uoff_t((std::numeric_limits<off_t>::max)()) < size){
      errno = EINVAL;
      return false;
   }
   return 0 == ::ftruncate(hnd, off_t(size));
}
Пример #4
0
bool cRecorder::NextFile(void)
{
  if (recordFile && frameDetector->IndependentFrame()) { // every file shall start with an independent frame
     if (fileSize > MEGABYTE(off_t(Setup.MaxVideoFileSize)) || RunningLowOnDiskSpace()) {
        recordFile = fileName->NextFile();
        fileSize = 0;
        }
     }
  return recordFile != NULL;
}
Пример #5
0
bool cLiveRecorder::NextFile(void) {
	if (recordFile && frameDetector->IndependentFrame()) { // every file shall start with an independent frame
		if(RunningLowOnDiskSpace() && index)
			((cLiveIndex *)index)->DropFile();
		if (fileSize > MEGABYTE(off_t(MaxFileSize)) || RunningLowOnDiskSpace()) {
			recordFile = fileName->NextFile();
			fileSize = 0;
		} // if
	} // if
	return recordFile != NULL;
} // cLiveRecorder::NextFile
Пример #6
0
std::string CaptureFD::readIncremental() {
  std::string filename = file_.path().string();
  // Yes, I know that I could just keep the file open instead. So sue me.
  folly::File f(openNoInt(filename.c_str(), O_RDONLY), true);
  auto size = size_t(lseek(f.fd(), 0, SEEK_END) - readOffset_);
  std::unique_ptr<char[]> buf(new char[size]);
  auto bytes_read = folly::preadFull(f.fd(), buf.get(), size, readOffset_);
  PCHECK(ssize_t(size) == bytes_read);
  readOffset_ += off_t(size);
  chunkCob_(StringPiece(buf.get(), buf.get() + size));
  return std::string(buf.get(), size);
}
Пример #7
0
static int wrapPositional(F f, int fd, off_t offset, Args... args) {
  off_t origLoc = lseek(fd, 0, SEEK_CUR);
  if (origLoc == off_t(-1)) {
    return -1;
  }
  if (lseek(fd, offset, SEEK_SET) == off_t(-1)) {
    return -1;
  }

  int res = (int)f(fd, args...);

  int curErrNo = errno;
  if (lseek(fd, origLoc, SEEK_SET) == off_t(-1)) {
    if (res == -1) {
      errno = curErrNo;
    }
    return -1;
  }
  errno = curErrNo;

  return res;
}
Пример #8
0
	static int Seek(void* context, ogg_int64_t offset, int whence)
	{
		VorbisFileAdapter* adapter = (VorbisFileAdapter*)context;

		off_t origin = 0;
		switch(whence)
		{
		case SEEK_SET:
			origin = 0;
			break;
		case SEEK_CUR:
			origin = adapter->offset;
			break;
		case SEEK_END:
			origin = adapter->size+1;
			break;
			NODEFAULT;
		}

		adapter->offset = Clamp(off_t(origin+offset), off_t(0), adapter->size);
		return 0;
	}
Пример #9
0
OOBase::uint64_t OOBase::File::tell() const
{
#if defined(_WIN32)
	LARGE_INTEGER off,pos;
	off.QuadPart = 0;
	pos.QuadPart = 0;
	if (!::SetFilePointerEx(m_fd,off,&pos,FILE_CURRENT))
		return uint64_t(-1);
	return pos.QuadPart;
#else
	off_t s = ::lseek(m_fd,0,SEEK_CUR);
	if (s == off_t(-1))
		return uint64_t(-1);
	return s;
#endif
}
TEST_F(LineBufferTest, single_line_no_newline) {
  std::string line_data;
  line_data += "Single line with no newline.";
  ASSERT_TRUE(TEMP_FAILURE_RETRY(
      write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
  ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));

  char buffer[100];
  LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));

  char* line;
  size_t line_len;
  ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  ASSERT_STREQ("Single line with no newline.", line);
  ASSERT_EQ(sizeof("Single line with no newline.") - 1, line_len);

  ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
}
Пример #11
0
OOBase::uint64_t OOBase::File::seek(int64_t offset, enum seek_direction dir)
{
#if defined(_WIN32)
	DWORD d = FILE_CURRENT;
	switch (dir)
	{
	case seek_begin:
		d = FILE_BEGIN;
		break;

	case seek_current:
		break;

	case seek_end:
		d = FILE_END;
		break;
	}
	LARGE_INTEGER off,pos;
	off.QuadPart = offset;
	pos.QuadPart = 0;
	if (!::SetFilePointerEx(m_fd,off,&pos,d))
		return uint64_t(-1);
	return pos.QuadPart;
#else
	int d = SEEK_CUR;
	switch (dir)
	{
	case seek_begin:
		d = SEEK_SET;
		break;

	case seek_current:
		break;

	case seek_end:
		d = SEEK_END;
		break;
	}
	off_t s = ::lseek(m_fd,offset,d);
	if (s == off_t(-1))
		return uint64_t(-1);
	return s;
#endif
}
Пример #12
0
SYSAPI int _Chela_IO_Seek(FILE_HANDLE stream, size_t offset, int origin)
{
    int whence = 0;
    switch(origin)
    {
    case SO_Begin:
        whence = SEEK_SET;
        break;
    case SO_Current:
        whence = SEEK_CUR;
        break;
    case SO_End:
        whence = SEEK_END;
        break;
    default:
        _Chela_Sys_Fatal("Invalid seek origin value.");
        break;
    }
    return lseek((size_t)stream, offset, whence) != off_t(-1);
}
Пример #13
0
/*
 * Find the zip Central Directory and memory-map it.
 *
 * On success, returns 0 after populating fields from the EOCD area:
 *   mDirectoryOffset
 *   mDirectoryMap
 *   mNumEntries
 */
static int mapCentralDirectory(int fd, const char* debugFileName,
    ZipArchive* pArchive)
{
    /*
     * Get and test file length.
     */
    off_t fileLength = lseek(fd, 0, SEEK_END);
    if (fileLength < kEOCDLen) {
        LOGV("Zip: length %ld is too small to be zip", (long) fileLength);
        return -1;
    }

    /*
     * Perform the traditional EOCD snipe hunt.
     *
     * We're searching for the End of Central Directory magic number,
     * which appears at the start of the EOCD block.  It's followed by
     * 18 bytes of EOCD stuff and up to 64KB of archive comment.  We
     * need to read the last part of the file into a buffer, dig through
     * it to find the magic number, parse some values out, and use those
     * to determine the extent of the CD.
     *
     * We start by pulling in the last part of the file.
     */
    size_t readAmount = kMaxEOCDSearch;
    if (fileLength < off_t(readAmount))
        readAmount = fileLength;

    u1* scanBuf = (u1*) malloc(readAmount);
    if (scanBuf == NULL) {
        return -1;
    }

    int result = mapCentralDirectory0(fd, debugFileName, pArchive,
            fileLength, readAmount, scanBuf);

    free(scanBuf);
    return result;
}
TEST_F(LineBufferTest, line_larger_than_buffer) {
  std::string line_data;
  line_data += "The first line.\n";
  line_data += "Second line here.\n";
  line_data += "This is a really, really, really, kind of long.\n";
  line_data += "The fourth line.\n";
  ASSERT_TRUE(TEMP_FAILURE_RETRY(
      write(tmp_file_->fd, line_data.c_str(), line_data.size())) != -1);
  ASSERT_TRUE(lseek(tmp_file_->fd, 0, SEEK_SET) != off_t(-1));

  char buffer[25];
  LineBuffer line_buf(tmp_file_->fd, buffer, sizeof(buffer));

  char* line;
  size_t line_len;
  ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  ASSERT_STREQ("The first line.", line);
  ASSERT_EQ(sizeof("The first line.") - 1, line_len);

  ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  ASSERT_STREQ("Second line here.", line);
  ASSERT_EQ(sizeof("Second line here.") - 1, line_len);

  ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  ASSERT_STREQ("This is a really, really", line);
  ASSERT_EQ(sizeof(buffer) - 1, line_len);
  ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  ASSERT_STREQ(", really, kind of long.", line);
  ASSERT_EQ(sizeof(", really, kind of long.") - 1, line_len);

  line_data += "The fourth line.\n";
  ASSERT_TRUE(line_buf.GetLine(&line, &line_len));
  ASSERT_STREQ("The fourth line.", line);
  ASSERT_EQ(sizeof("The fourth line.") - 1, line_len);

  ASSERT_FALSE(line_buf.GetLine(&line, &line_len));
}
Пример #15
0
bool
TripleIndBlockNode::rb_traverse(Context & i_ctxt,
                                FileNode & i_fn,
                                unsigned int i_flags,
                                off_t i_base,
                                off_t i_rngoff,
                                size_t i_rngsize,
                                BlockTraverseFunc & i_trav)
{
    static off_t const refspan = NUMREF * NUMREF * BLKSZ;

    off_t startoff = max(i_rngoff, i_base);

    // Are we beyond the target range?
    if (i_base > i_rngoff + off_t(i_rngsize))
        goto done;

    // Figure out which index we start with.
    for (off_t ndx = (startoff - i_base) / refspan; ndx < off_t(NUMREF); ++ndx)
    {
        off_t off = i_base + (ndx * refspan);

        // If we are beyond the traversal region we're done.
        if (off >= i_rngoff + off_t(i_rngsize))
            goto done;

        // Find the block object to use.
        DoubleIndBlockNodeHandle nh;

        // Do we have one in the cache already?
        if (m_blkobj_X[ndx])
        {
            // Yep, use it.
            nh = dynamic_cast<DoubleIndBlockNode *>(&*m_blkobj_X[ndx]);
        }
        else
        {
            // Nope, does it have a digest yet?
            if (m_blkref[ndx])
            {
                // Does the clean cache have it?
                BlockNodeHandle bnh = i_ctxt.m_bncachep->lookup(m_blkref[ndx]);
                if (bnh)
                {
                    // Yes, better be a DoubleIndBlockNode ...
                    nh = dynamic_cast<DoubleIndBlockNode *>(&*bnh);
                }
                else
                {
                    // Nope, read it from the blockstore.
                    nh = new DoubleIndBlockNode(i_ctxt, m_blkref[ndx]);

                    // Insert it in the clean cache.
                    if (!(i_flags & RB_NOCACHE))
                        i_ctxt.m_bncachep->insert(nh);
                }
            }
            else if (i_flags & RB_MODIFY_X)
            {
                // Nope, create new block.
                nh = new DoubleIndBlockNode();

                // Keep it in the dirty cache.
                m_blkobj_X[ndx] = nh;

                // Increment the block count.
                i_fn.blocks(i_fn.blocks() + 1);
            }
            else
            {
                // Use the zero singleton.
                nh = i_ctxt.m_zdinobj;

                // And *don't* keep it in the cache!
            }
        }

        // Recursively traverse ...
        if (nh->rb_traverse(i_ctxt, i_fn, i_flags, off,
                            i_rngoff, i_rngsize, i_trav))
        {
            if (i_flags & RB_MODIFY_X)
            {
                // The node is already marked dirty ...

                // Remove it from the clean cache.
                i_ctxt.m_bncachep->remove(nh->bn_blkref());

                // Insert it in the dirty collection.
                m_blkobj_X[ndx] = nh;

                // We're dirty too.
                bn_isdirty(true);
            }
        }
    }

 done:
    // Return our dirty state.
    return bn_isdirty();
}
Пример #16
0
/* --- @bin_2_iso@ --- *
 *
 * Arguments:	@file_ptrs *fptrs@ = pointer struct of source and destination file
 * 		@image_struct *img_struct@ = struct of image pregap
 *
 * Returns:	Zeor on success, @-1@ on error.
 *
 * Use:		Return the detection of the image.
 */
int bin_2_iso ( file_ptrs* fptrs,  image_struct*  img_struct )
{
	off_t	n_loop = 0;
	off_t	n_loop_incrementer = 0;
	off_t	n_img_size = 0;
	int	n_return_value = ERROR;
	int	n_mode = 0;
	long 	n_iso_block_no = 0;

	unsigned const char synch_pattern [ 12 ] = { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };

	sync_header_block	header; /* Sync Header Block */

	off_t (* mode [ 5 ] ) ( file_ptrs* fptrs, off_t n_fptr_pos, off_t n_iso_block_no )
		= { &mode_0, &mode_1, &mode_2, &mode_2_form_1_headerless, &mode_2_form_2_headerless };

	if ( NULL== fptrs -> fsource ) return ( n_return_value ); /* The source file pointer is empty */
	if ( ( n_img_size = get_file_size ( fptrs -> fsource ) ) < 1 ) return ( n_return_value ); /* The image file is empty */

	for ( n_loop = ( off_t ) img_struct -> pregap; n_loop < n_img_size; ) {

		progress_bar ( ( int ) ( ( ( n_loop + 1 ) * 100 ) / n_img_size ) );

		/* Initalize for the loop */
		memset ( &header, 0, sizeof ( sync_header_block ) );
		
		/* Initialize the file pointer to the start point */
		set_file_pointer ( fptrs -> fsource, n_loop );

		fread ( &header, 1, sizeof ( sync_header_block ), fptrs -> fsource );

		/*printf ("Image size :%ld\n", n_img_size );
		printf ( "LOOP NO :%d\n", n_loop );
		print ( ( void* ) &header, 16 );*/

		/* Revert back the file pointer as it has moved the
		 * file pointer till the header (read 16 bytes of data) */
		set_file_pointer ( fptrs -> fsource, n_loop );

		if ( !memcmp ( &synch_pattern, &header, 12 ) ) {
			n_iso_block_no++;

			/* Image has 12 BYTE SYNC HEADER */
			n_mode = ( (char) ( *( header.msf_block.mode ) ) );
			if ( ( n_mode > -1 ) && ( n_mode < 3 ) )
				/* Mode { 0, 1, 2 } */
				n_loop_incrementer = ( *( mode [ n_mode ] ) ) ( fptrs, n_loop, n_iso_block_no ); /* Increment to the next block */
			else printf ( "Weird Mode \n" );
			if ( 0 < n_loop_incrementer ) {
				if ( ( 2448 == img_struct -> block ) || ( 2368 == img_struct -> block ) ) {
					n_loop += n_loop_incrementer;
					n_loop += ( ( off_t ) img_struct -> block - n_loop_incrementer );
					n_loop_incrementer = 0;
				} else n_loop += n_loop_incrementer;
			}
		} else if ( ( !memcmp ( &header, ( ( (unsigned char*) (&header) ) + 4 ), 4 ) ) && ( 2336 == img_struct -> block ) ) {
			int n_mode_no_header = 0;
			n_iso_block_no++;

			/*print ( ( void* ) &header, 8 );*/
			n_mode_no_header =  ( (int) *( ( (unsigned char*) (&header) ) + 2 ) ) & 0x20;
			n_mode_no_header = ( n_mode_no_header  == 32 ) ? 1 : 0;

			n_loop += ( *( mode [ 3 + n_mode_no_header ] ) ) ( fptrs, n_loop, n_iso_block_no ); /* Increment to the next block */
		} else {
			/* Image does not have any standard header */
			/*print ( ( void* ) &header, 16 );*/
			n_loop++; /* Increment to the next location */
			/*n_loop += img_struct -> block;*/ /* Increment to the next block */
		}
	}

	set_file_pointer ( fptrs -> fsource, ( off_t ) 0 );

	n_return_value = AOK;
	progress_bar ( 100 );

	printf ( "\n" );

	return ( n_return_value );
}
Пример #17
0
void _mapFuncNames(ThreadContext *context, FileSys::SeekableDescription *fdesc, VAddr addr, size_t len, off_t off){
  typedef typename ElfDefs<mode>::Elf_Ehdr Elf_Ehdr;
  typedef typename ElfDefs<mode>::Elf_Shdr Elf_Shdr;
  typedef typename ElfDefs<mode>::Elf_Sym  Elf_Sym;
  // Read in the ELF header
  Elf_Ehdr ehdr;
  ssize_t ehdrSiz=fdesc->pread(&ehdr,sizeof(Elf_Ehdr),0);
  I(ehdrSiz==sizeof(Elf_Ehdr));
  cvtEndianEhdr<mode>(ehdr);
  // Read in section headers
  Elf_Shdr shdrs[ehdr.e_shnum];
  ssize_t shdrsSiz=fdesc->pread(shdrs,sizeof(Elf_Shdr)*ehdr.e_shnum,ehdr.e_shoff);
  I(shdrsSiz==(ssize_t)(sizeof(Elf_Shdr)*ehdr.e_shnum));
  I(shdrsSiz==(ssize_t)(sizeof(shdrs)));
  for(size_t sec=0;sec<ehdr.e_shnum;sec++)
    cvtEndianShdr<mode>(shdrs[sec]);
  // Read in section name strings
  I((ehdr.e_shstrndx>0)&&(ehdr.e_shstrndx<ehdr.e_shnum));
  char secStrTab[shdrs[ehdr.e_shstrndx].sh_size];
  ssize_t secStrTabSiz=fdesc->pread(secStrTab,shdrs[ehdr.e_shstrndx].sh_size,shdrs[ehdr.e_shstrndx].sh_offset);
  I(secStrTabSiz==(ssize_t)(shdrs[ehdr.e_shstrndx].sh_size));
  // Iterate over all sections
  for(size_t sec=0;sec<ehdr.e_shnum;sec++){
    switch(shdrs[sec].sh_type){
    case SHT_PROGBITS: {
      if(!(shdrs[sec].sh_flags&SHF_EXECINSTR))
        break;
      ssize_t loadBias=(addr-shdrs[sec].sh_addr)+(shdrs[sec].sh_offset-off);
      char  *secNam=secStrTab+shdrs[sec].sh_name;
      size_t secNamLen=strlen(secNam);
      if((off_t(shdrs[sec].sh_offset)>=off)&&(shdrs[sec].sh_offset<off+len)){
        char  *begNam="SecBeg";
        size_t begNamLen=strlen(begNam);
        char symNam[secNamLen+begNamLen+1];
        strcpy(symNam,begNam);
        strcpy(symNam+begNamLen,secNam);
        VAddr symAddr=shdrs[sec].sh_addr+loadBias;
        context->getAddressSpace()->addFuncName(symAddr,symNam,fdesc->getName());
      }
      if((off_t(shdrs[sec].sh_offset+shdrs[sec].sh_size)>off)&&
         (shdrs[sec].sh_offset+shdrs[sec].sh_size<=off+len)){
        char  *endNam="SecEnd";
        size_t endNamLen=strlen(endNam);
        char symNam[secNamLen+endNamLen+1];
        strcpy(symNam,endNam);
        strcpy(symNam+endNamLen,secNam);
        VAddr symAddr=shdrs[sec].sh_addr+shdrs[sec].sh_size+loadBias;
        context->getAddressSpace()->addFuncName(symAddr,symNam,fdesc->getName());
      }
    } break;
    case SHT_SYMTAB:
    case SHT_DYNSYM: {
      I(shdrs[sec].sh_entsize==sizeof(Elf_Sym));
      // Read in the symbols
      size_t symnum=shdrs[sec].sh_size/sizeof(Elf_Sym);
      Elf_Sym syms[symnum];
      ssize_t symsSiz=fdesc->pread(syms,shdrs[sec].sh_size,shdrs[sec].sh_offset);
      I(symsSiz==(ssize_t)(sizeof(Elf_Sym)*symnum));
      I(symsSiz==(ssize_t)(sizeof(syms)));
      for(size_t sym=0;sym<symnum;sym++)
        cvtEndianSym<mode>(syms[sym]);
      // Read in the symbol name strings
      char strTab[shdrs[shdrs[sec].sh_link].sh_size];
      ssize_t strTabSiz=fdesc->pread(strTab,shdrs[shdrs[sec].sh_link].sh_size,shdrs[shdrs[sec].sh_link].sh_offset);
      I(strTabSiz==(ssize_t)(shdrs[shdrs[sec].sh_link].sh_size));
      for(size_t sym=0;sym<symnum;sym++){
        I(ELF32_ST_TYPE(syms[sym].st_info)==ELF64_ST_TYPE(syms[sym].st_info));
        switch(ELF64_ST_TYPE(syms[sym].st_info)){
        case STT_FUNC: {
          if(!syms[sym].st_shndx)
            break;
          ssize_t loadBias=(addr-shdrs[syms[sym].st_shndx].sh_addr)+
                           (shdrs[syms[sym].st_shndx].sh_offset-off);
//          printf("sh_type %s st_name %s st_value 0x%08x st_size 0x%08x st_shndx 0x%08x\n",
//                 shdrs[sec].sh_type==SHT_SYMTAB?"SYMTAB":"DYNSYM",
//                 strTab+syms[sym].st_name,
//                 (int)(syms[sym].st_value),(int)(syms[sym].st_size),(int)(syms[sym].st_shndx));
          char *symNam=strTab+syms[sym].st_name;
          VAddr symAddr=syms[sym].st_value+loadBias;
          if((symAddr<addr)||(symAddr>=addr+len))
            break;
	  context->getAddressSpace()->addFuncName(symAddr,symNam,fdesc->getName());
        }  break;
        }
      }
    }  break;
    }
  }
}
Пример #18
0
/*
 * Class:       sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal
 * Method:      readBytesFromProcess0
 * Signature:   (JJ)[B
 * Description: read bytes from debuggee process/core
 */
JNIEXPORT jbyteArray JNICALL Java_sun_jvm_hotspot_debugger_proc_ProcDebuggerLocal_readBytesFromProcess0
  (JNIEnv *env, jobject this_obj, jlong address, jlong numBytes) {

  jbyteArray array = env->NewByteArray(numBytes);
  CHECK_EXCEPTION_(0);
  jboolean isCopy;
  jbyte* bufPtr = env->GetByteArrayElements(array, &isCopy);
  CHECK_EXCEPTION_(0);

  jlong p_ps_prochandle = env->GetLongField(this_obj, p_ps_prochandle_ID);
  ps_err_e ret = ps_pread((struct ps_prochandle*) p_ps_prochandle,
                       (psaddr_t)address, bufPtr, (size_t)numBytes);

  if (ret != PS_OK) {
    // part of the class sharing workaround. try shared heap area
    int classes_jsa_fd = env->GetIntField(this_obj, classes_jsa_fd_ID);
    if (classes_jsa_fd != -1 && address != (jlong)0) {
      print_debug("read failed at 0x%lx, attempting shared heap area\n", (long) address);

      struct FileMapHeader* pheader = (struct FileMapHeader*) env->GetLongField(this_obj, p_file_map_header_ID);
      // walk through the shared mappings -- we just have 4 of them.
      // so, linear walking is okay.
      for (int m = 0; m < NUM_SHARED_MAPS; m++) {

        // We can skip the non-read-only maps. These are mapped as MAP_PRIVATE
        // and hence will be read by libproc. Besides, the file copy may be
        // stale because the process might have modified those pages.
        if (pheader->_space[m]._read_only) {
          jlong baseAddress = (jlong) (uintptr_t) pheader->_space[m]._base;
          size_t usedSize = pheader->_space[m]._used;
          if (address >= baseAddress && address < (baseAddress + usedSize)) {
            // the given address falls in this shared heap area
            print_debug("found shared map at 0x%lx\n", (long) baseAddress);


            // If more data is asked than actually mapped from file, we need to zero fill
            // till the end-of-page boundary. But, java array new does that for us. we just
            // need to read as much as data available.

#define MIN2(x, y) (((x) < (y))? (x) : (y))

            jlong diff = address - baseAddress;
            jlong bytesToRead = MIN2(numBytes, usedSize - diff);
            off_t offset = pheader->_space[m]._file_offset  + off_t(diff);
            ssize_t bytesRead = pread(classes_jsa_fd, bufPtr, bytesToRead, offset);
            if (bytesRead != bytesToRead) {
              env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT);
              print_debug("shared map read failed\n");
              return jbyteArray(0);
            } else {
              print_debug("shared map read succeeded\n");
              env->ReleaseByteArrayElements(array, bufPtr, 0);
              return array;
            }
          } // is in current map
        } // is read only map
      } // for shared maps
    } // classes_jsa_fd != -1
    env->ReleaseByteArrayElements(array, bufPtr, JNI_ABORT);
    return jbyteArray(0);
  } else {
    env->ReleaseByteArrayElements(array, bufPtr, 0);
    return array;
  }
}
Пример #19
0
bool CFileRegion::doesInclude(off_t offset)
{
	return offset >= offset_ && offset < offset_ + off_t(size_);
}
Пример #20
0
bool cRecorder::NextFile(void)
{
  if (recordFile && frameDetector->IndependentFrame()) { // every file shall start with an independent frame
#ifdef USE_HARDLINKCUTTER
     if (fileSize > fileName->MaxFileSize() || RunningLowOnDiskSpace()) {
#else
     if (fileSize > MEGABYTE(off_t(Setup.MaxVideoFileSize)) || RunningLowOnDiskSpace()) {
#endif /* HARDLINKCUTTER */
        recordFile = fileName->NextFile();
        fileSize = 0;
        }
     }
  return recordFile != NULL;
}

void cRecorder::Activate(bool On)
{
  if (On)
     Start();
  else
     Cancel(3);
}

void cRecorder::Receive(uchar *Data, int Length)
{
  if (Running()) {
     int p = ringBuffer->Put(Data, Length);
     if (p != Length && Running())
        ringBuffer->ReportOverflow(Length - p);
     }
}

void cRecorder::Action(void)
{
  time_t t = time(NULL);
  bool InfoWritten = false;
  bool FirstIframeSeen = false;
#ifdef USE_LIVEBUFFER
  double fps = DEFAULTFRAMESPERSECOND;
#endif /*USE_LIVEBUFFER*/
  while (Running()) {
        int r;
        uchar *b = ringBuffer->Get(r);
        if (b) {
           int Count = frameDetector->Analyze(b, r);
           if (Count) {
              if (!Running() && frameDetector->IndependentFrame()) // finish the recording before the next independent frame
                 break;
              if (frameDetector->Synced()) {
#ifdef USE_LIVEBUFFER
                 if(index && (frameDetector->FramesPerSecond() != fps)) {
                    fps = frameDetector->FramesPerSecond();
                    index->SetFramesPerSecond(fps);
                 } // if
#endif /*USE_LIVEBUFFER*/
                 if (!InfoWritten) {
                    cRecordingInfo RecordingInfo(recordingName);
                    if (RecordingInfo.Read()) {
                       if (frameDetector->FramesPerSecond() > 0 && DoubleEqual(RecordingInfo.FramesPerSecond(), DEFAULTFRAMESPERSECOND) && !DoubleEqual(RecordingInfo.FramesPerSecond(), frameDetector->FramesPerSecond())) {
                          RecordingInfo.SetFramesPerSecond(frameDetector->FramesPerSecond());
                          RecordingInfo.Write();
                          Recordings.UpdateByName(recordingName);
                          }
                       }
                    InfoWritten = true;
                    }
/*                 if (frameDetector->NewPayload()) { // We're at the first TS packet of a new payload...
                    if (Buffering)
                       esyslog("ERROR: encountered new payload while buffering - dropping some data!");
                    if (!frameDetector->NewFrame()) { // ...but the frame type is yet unknown, so we need to buffer packets until we see the frame type
                       if (!Buffer) {
                          dsyslog("frame type not in first packet of payload - buffering");
                          if (!(Buffer = MALLOC(uchar, BUFFERSIZE))) {
                             esyslog("ERROR: can't allocate frame type buffer");
                             break;
                             }
                          }
                       BufferIndex = 0;
                       Buffering = true;
                       }
                    }
                 else if (frameDetector->NewFrame()) // now we know the frame type, so stop buffering
                    Buffering = false;
                 if (Buffering) {
                    if (BufferIndex + Count <= BUFFERSIZE) {
                       memcpy(Buffer + BufferIndex, b, Count);
                       BufferIndex += Count;
                       }
                    else
                       esyslog("ERROR: too many bytes for frame type buffer (%d > %d) - dropped %d bytes", BufferIndex + Count, int(BUFFERSIZE), Count);
                    }
                 else if (FirstIframeSeen || frameDetector->IndependentFrame()) {
*/
#ifdef USE_LIVEBUFFER
                    if(!FirstIframeSeen) FillInitialData(b, r);
#endif /*USE_LIVEBUFFER*/
                 if (FirstIframeSeen || frameDetector->IndependentFrame()) {
                    FirstIframeSeen = true; // start recording with the first I-frame
                    if (!NextFile())
                       break;
                    if (index && frameDetector->NewFrame())
                       index->Write(frameDetector->IndependentFrame(), fileName->Number(), fileSize);
                    if (frameDetector->IndependentFrame()) {
                       recordFile->Write(patPmtGenerator.GetPat(), TS_SIZE);
                       fileSize += TS_SIZE;
                       int Index = 0;
                       while (uchar *pmt = patPmtGenerator.GetPmt(Index)) {
                             recordFile->Write(pmt, TS_SIZE);
                             fileSize += TS_SIZE;
                             }
                       }
                    if (recordFile->Write(b, Count) < 0) {
                       LOG_ERROR_STR(fileName->Name());
                       break;
                       }
                    fileSize += Count;
                    t = time(NULL);
                    }
                 }
              ringBuffer->Del(Count);
              }
           }
#ifdef USE_LIVEBUFFER
        if (handleError && (time(NULL) - t > MAXBROKENTIMEOUT)) {
#else
        if (time(NULL) - t > MAXBROKENTIMEOUT) {
#endif
#if REELVDR
           Skins.QueueMessage(mtError, tr("can't record - check your configuration"));
#else
           esyslog("ERROR: video data stream broken. Requesting Emergency Exit.");
           ShutdownHandler.RequestEmergencyExit();
#endif /*REELVDR*/
           t = time(NULL);
           }
        }
}
Пример #21
0
void
TopCanvas::Create(PixelSize new_size,
                  bool full_screen, bool resizable)
{
#ifdef USE_FB
  assert(fd < 0);

#ifdef USE_TTY
  InitialiseTTY();
#endif

  const char *path = "/dev/fb0";
  fd = open(path, O_RDWR | O_NOCTTY | O_CLOEXEC);
  if (fd < 0) {
    fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
    return;
  }

  struct fb_fix_screeninfo finfo;
  if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) < 0) {
    fprintf(stderr, "FBIOGET_FSCREENINFO failed: %s\n", strerror(errno));
    Destroy();
    return;
  }

  if (finfo.type != FB_TYPE_PACKED_PIXELS) {
    fprintf(stderr, "Unsupported console hardware\n");
    Destroy();
    return;
  }

  switch (finfo.visual) {
  case FB_VISUAL_TRUECOLOR:
  case FB_VISUAL_PSEUDOCOLOR:
  case FB_VISUAL_STATIC_PSEUDOCOLOR:
  case FB_VISUAL_DIRECTCOLOR:
    break;

  default:
    fprintf(stderr, "Unsupported console hardware\n");
    Destroy();
    return;
  }

  /* Memory map the device, compensating for buggy PPC mmap() */
  const off_t page_size = getpagesize();
  off_t offset = off_t(finfo.smem_start)
    - (off_t(finfo.smem_start) &~ (page_size - 1));
  off_t map_size = finfo.smem_len + offset;

  map = mmap(nullptr, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  if (map == (void *)-1) {
    fprintf(stderr, "Unable to memory map the video hardware: %s\n",
            strerror(errno));
    map = nullptr;
    Destroy();
    return;
  }

  /* Determine the current screen depth */
  struct fb_var_screeninfo vinfo;
  if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) < 0 ) {
    fprintf(stderr, "Couldn't get console pixel format: %s\n",
            strerror(errno));
    Destroy();
    return;
  }

#ifdef GREYSCALE
  /* switch the frame buffer to 8 bits per pixel greyscale */

  vinfo.bits_per_pixel = 8;
  vinfo.grayscale = true;

  if (ioctl(fd, FBIOPUT_VSCREENINFO, &vinfo) < 0) {
    fprintf(stderr, "Couldn't set greyscale pixel format: %s\n",
            strerror(errno));
    Destroy();
    return;
  }

  /* read new finfo */
  ioctl(fd, FBIOGET_FSCREENINFO, &finfo);

  map_bpp = 1;
#else
  map_bpp = vinfo.bits_per_pixel / 8;
  if (map_bpp != 2 && map_bpp != 4) {
    fprintf(stderr, "Unsupported console hardware\n");
    Destroy();
    return;
  }
#endif

  map_pitch = finfo.line_length;
  epd_update_marker = 0;

#ifdef KOBO
  int updateSheme = UPDATE_SCHEME_QUEUE_AND_MERGE;
  if(ioctl(fd, MXCFB_SET_UPDATE_SCHEME, &updateSheme) < 0) {
      fprintf(stderr, "Couldn't set update_scheme: %s\n",
            strerror(errno));
  }
  KoboModel model = DetectKoboModel();
  switch(model) {
      case KoboModel::UNKNOWN:
        frame_sync = true;
        break;
      case KoboModel::MINI:
      case KoboModel::TOUCH:
      case KoboModel::GLO:
        frame_sync = false;
        break;
      case KoboModel::GLOHD:
        frame_sync = true;
        break;
      default:
        frame_sync = true;
        break;
  };
  
  
#endif

  const unsigned width = vinfo.xres, height = vinfo.yres;
#elif defined(USE_VFB)
  const unsigned width = new_size.cx, height = new_size.cy;
#else
#error No implementation
#endif

  buffer.Allocate(width, height);
}
Пример #22
0
bool
pcl::io::LZFImageReader::loadImageBlob (const std::string &filename,
                                        std::vector<char> &data,
                                        uint32_t &uncompressed_size)
{
  if (filename == "" || !boost::filesystem::exists (filename))
  {
    PCL_ERROR ("[pcl::io::LZFImageReader::loadImage] Could not find file '%s'.\n", filename.c_str ());
    return (false);
  }
  // Open for reading
  int fd = pcl_open (filename.c_str (), O_RDONLY);
  if (fd == -1)
  {
    PCL_ERROR ("[pcl::io::LZFImageReader::loadImage] Failure to open file %s\n", filename.c_str () );
    return (false);
  }

  // Seek to the end of file to get the filesize
  off_t data_size = pcl_lseek (fd, 0, SEEK_END);
  if (data_size < 0)
  {
    pcl_close (fd);
    PCL_ERROR ("[pcl::io::LZFImageReader::loadImage] lseek errno: %d strerror: %s\n", errno, strerror (errno));
    PCL_ERROR ("[pcl::io::LZFImageReader::loadImage] Error during lseek ()!\n");
    return (false);
  }
  pcl_lseek (fd, 0, SEEK_SET);

#ifdef _WIN32
  // As we don't know the real size of data (compressed or not), 
  // we set dwMaximumSizeHigh = dwMaximumSizeLow = 0 so as to map the whole file
  HANDLE fm = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL, PAGE_READONLY, 0, 0, NULL);
  // As we don't know the real size of data (compressed or not), 
  // we set dwNumberOfBytesToMap = 0 so as to map the whole file
  char *map = static_cast<char*>(MapViewOfFile (fm, FILE_MAP_READ, 0, 0, 0));
  if (map == NULL)
  {
    CloseHandle (fm);
    pcl_close (fd);
    PCL_ERROR ("[pcl::io::LZFImageReader::loadImage] Error mapping view of file, %s\n", filename.c_str ());
    return (false);
  }
#else
  char *map = static_cast<char*> (mmap (0, data_size, PROT_READ, MAP_SHARED, fd, 0));
  if (map == reinterpret_cast<char*> (-1))    // MAP_FAILED
  {
    pcl_close (fd);
    PCL_ERROR ("[pcl::io::LZFImageReader::loadImage] Error preparing mmap for PCLZF file.\n");
    return (false);
  }
#endif

  // Check the header identifier here
  char header_string[5];
  memcpy (&header_string,    &map[0], 5);        // PCLZF
  if (std::string (header_string).substr (0, 5) != "PCLZF")
  {
    PCL_ERROR ("[pcl::io::LZFImageReader::loadImage] Wrong signature header! Should be 'P'C'L'Z'F'.\n");
#ifdef _WIN32
  UnmapViewOfFile (map);
  CloseHandle (fm);
#else
    munmap (map, data_size);
#endif
    return (false);
  }
  memcpy (&width_,            &map[5], sizeof (uint32_t));
  memcpy (&height_,           &map[9], sizeof (uint32_t));
  char imgtype_string[16];
  memcpy (&imgtype_string,    &map[13], 16);       // BAYER8, RGB24_, YUV422_, ...
  image_type_identifier_ = std::string (imgtype_string).substr (0, 15);
  image_type_identifier_.insert (image_type_identifier_.end (), 1, '\0');

  static const int header_size = LZF_HEADER_SIZE;
  uint32_t compressed_size;
  memcpy (&compressed_size,   &map[29], sizeof (uint32_t));

  if (compressed_size + header_size != data_size)
  {
    PCL_ERROR ("[pcl::io::LZFImageReader::loadImage] Number of bytes to decompress written in file (%u) differs from what it should be (%u)!\n", compressed_size, data_size - header_size);
#ifdef _WIN32
  UnmapViewOfFile (map);
  CloseHandle (fm);
#else
    munmap (map, data_size);
#endif
    return (false);
  }

  memcpy (&uncompressed_size, &map[33], sizeof (uint32_t));

  data.resize (compressed_size);
  memcpy (&data[0], &map[header_size], compressed_size);
 
#ifdef _WIN32
  UnmapViewOfFile (map);
  CloseHandle (fm);
#else
  if (munmap (map, data_size) == -1)
    PCL_ERROR ("[pcl::io::LZFImageReader::loadImage] Munmap failure\n");
#endif
  pcl_close (fd);

  data_size = off_t (compressed_size);      // We only care about this from here on
  return (true);
}
Пример #23
0
int TTask::setHttpHeader()
{
	hcp->ClearRequestHdr();
	//先写入上层传入的http头,如果该头和内部头重复,则会在后续处理中被覆盖
	if(NULL != httpHdrs)
	{
		THttpHdrList * httpHdrlist = httpHdrs->GetHdrs();
		
		THttpHdrList::iterator itr = httpHdrlist->begin();
		for( ; itr != httpHdrlist->end(); ++itr )
		{
			THttpHdr* pHdr = *itr;
			hcp->BuildCustomHdr(pHdr->GetKey().c_str(), pHdr->GetVal().c_str());
		}		
	}

	hcp->BuildAcceptHdr();

	if(TASK_UPLOAD == task_type || TASK_QUERY == task_type)
	{
//		hcp->BuildCustomHdr("Referer", url->get_encoded_path());
//		hcp->BuildCustomHdr("Accept-Language", "zh-cn");
		//Post 方法需要写入post数据大小
//			char contenttype[128] = {0};
//			snprintf(contenttype, sizeof(contenttype), 
//				"multipart/form-data; boundary=%s", __HEAD_BROUNDARY);
//			hcp->BuildContentType(contenttype);
		hcp->BuildContentType();

//		hcp->BuildCustomHdr("Accept-Encoding", "gzip, deflate");
		hcp->BuildUserAgentHdr();
		hcp->BuildHostHdr(url->get_host(), url->get_port());
		hcp->BuildContentLengthHdr(postDataSize);
		
	}
	else if(TASK_BINARY == task_type)
	{
		hcp->BuildUserAgentHdr();
		hcp->BuildHostHdr(url->get_host(), url->get_port());
		hcp->BuildContentLengthHdr(postDataSize);
	}
	else if(TASK_DOWNLOAD == task_type)
	{		
		hcp->BuildUserAgentHdr();
		hcp->BuildHostHdr(url->get_host(), url->get_port());
		if((startSize != 0) || (endSize != 0))
		{
			if(endSize == 0)
			{
				hcp->BuildRangeHdr(off_t(startSize), -1);
			}
			else
			{
				hcp->BuildRangeHdr(off_t(startSize), off_t(endSize));
			}
		}
	}
	else if(TASK_HEAD == task_type)
	{
		hcp->BuildUserAgentHdr();
		hcp->BuildHostHdr(url->get_host(), url->get_port());
	}
	
	
	if(hcp->IsUseProxy() && hcp->GetProxy().type == HTTPDOWN_PROXY_HTTP)
	{
		if(hcp->GetProxy().domain != "")
		{
			//It's ntlm proxy
			hcp->processNtlmAuth();
			hcp->BuildNtlmAuthHdr(hcp->getNtlmNegotiateStr().c_str());
			hcp->BuildCustomHdr("Proxy-Connection", "Close");
//				hcp->BuildCustomHdr("Connection", "Keep-Alive");
			hcp->BuildConnectionHdr();
		}
		else
		{
			hcp->BuildBasicAuthHdr(url->get_user(), url->get_password() );
			if(!hcp->GetProxy().user.empty())
			{
				hcp->BuildProxyAuthHdr(hcp->GetProxy().user.c_str(), hcp->GetProxy().pwd.c_str());
			}			
			hcp->BuildConnectionHdr();
		}
	}
	else
	{
	//hcp->BuildPragmaHdr();
		hcp->BuildConnectionHdr();
	}
	//hcp->BuildCustomHdr("Cache-Control", "no-cache");
	return RET_OK;
}
Пример #24
0
	virtual int Get(uint16_t FileNumber, off_t FileOffset) {
		for ( std::vector<tLiveIndex>::iterator item = idx.begin(); item != idx.end(); item++)
			if (item->number > FileNumber || ((item->number == FileNumber) && off_t(item->offset) >= FileOffset))
				return item->index;
		return lastPos;
	}; // Get