Exemple #1
0
long FileReader::FileSize(int Mode_)
{
  long StartPos_, CurPos_, Diff_;

  if ((Mode_ & ios::out) || (Mode_ & ios::app))
  {
    CurPos_ = tellp();
    seekp(0);
    StartPos_ = tellp();
    seekp(0, ios::end);
    Diff_ = tellp() - StartPos_;
    seekp(CurPos_);
  }
  else if (Mode_ & ios::in)
  {
    CurPos_ = tellg();
    seekg(0);
    StartPos_ = tellg();
    seekg(0, ios::end);
    Diff_ = tellg() - StartPos_;
    seekg(CurPos_);
  }

  return Diff_;
}
Exemple #2
0
size_t c_disk_file::size() {
    auto s = dynamic_cast<std::ifstream *>(m_stream);
    size_t pos = s->tellg();
    s->seekg (0, s->end);
    size_t length = s->tellg();
    s->seekg (pos, s->beg);
    return length;
}
/* Member function ibitstream::size
 * ------------------------------
 * Seek to file end and use tell to retrieve position.
 * In order to not disrupt reading, we also record cur streampos and
 * re-seek to there before returning.
 */
long ibitstream::size() {
    if (!is_open()) {
        error("Cannot get size of stream which is not open.");
    }
    clear();					// clear any error state
    streampos cur = tellg();	// save current streampos
    seekg(0, ios::end);			// seek to end
    streampos end = tellg();	// get offset
    seekg(cur);					// seek back to original pos
    return long(end);
}
Exemple #4
0
std::shared_ptr<VersionManifest> ArchiveReader::read_manifest(){
	auto stream = this->get_stream();
	if (this->manifest_offset < 0){
		const int uint64_length = sizeof(std::uint64_t);
		std::int64_t start = -uint64_length - sha256_digest_length;
		stream->seekg(start, std::ios::end);
		char temp[uint64_length];
		stream->read(temp, uint64_length);
		if (stream->gcount() != uint64_length)
			throw ArchiveReadException("Invalid data: File is too small to possibly be valid");
		deserialize_fixed_le_int(this->manifest_size, temp);
		start -= this->manifest_size;
		stream->seekg(start, std::ios::end);
		this->manifest_offset = stream->tellg();
	}else
		stream->seekg(this->manifest_offset);
	{
		boost::iostreams::stream<BoundedInputFilter> bounded(*stream, this->manifest_size);
		boost::iostreams::stream<LzmaInputFilter> lzma(bounded);

		ImplementedDeserializerStream ds(lzma);
		this->version_manifest.reset(ds.begin_deserialization<VersionManifest>(config::include_typehashes));
		if (!this->version_manifest)
			throw ArchiveReadException("Invalid data: Error during manifest deserialization");
	}

	this->base_objects_offset = this->manifest_offset - this->version_manifest->archive_metadata.entries_size_in_archive;
	this->stream_ids = this->version_manifest->archive_metadata.stream_ids;
	this->stream_sizes = this->version_manifest->archive_metadata.stream_sizes;

	return this->version_manifest;
}
Exemple #5
0
std::streampos BIStream::maxg() const
{
  std::streampos cur = tellg();
  std::streampos max = rdbuf()->pubseekoff(0, std::ios_base::end,
                                           std::ios_base::in);
  rdbuf()->pubseekpos(cur, std::ios_base::in);
  return max;
}
Exemple #6
0
 // Offset of the first character in rawData in the file
 std::streampos getRawPos()
 {
    std::streampos t = tellg();
    if (static_cast<long>(t)==-1)
       return -1;
    else
       return t - static_cast<std::streampos>(rawData.length());
 }
Exemple #7
0
    bool NSFSoundStream::open(const std::string& fileName) {
        sf::Lock lock(mutex);

        if(!FileSystem::exists(fileName)) {
            return false;
        }

        int length = 0;
        auto fs = FileSystem::openFileRead(fileName);

        fs->seekg (0, std::ios::end);
        length = static_cast<int>(fs->tellg());
        fs->seekg (0, std::ios::beg);

        std::unique_ptr<char[]> nsfFileBuffer(new char[length]);

        fs->read(nsfFileBuffer.get(), length);

        // To honor the contract of returning false on failure,
        // catch these exceptions and return false instead? Good/bad?
        try {
            gme_type_t file_type = gme_identify_extension(fileName.c_str());

            if(!file_type) {
                return false;
            }

            for(std::size_t i = 0; i < samplerCount; ++i) {
                auto sampleEmu = std::shared_ptr<Music_Emu>(file_type->new_emu());

                if(!sampleEmu) {
                    return false;
                }

                // Must set sample rate before loading data
                handleError(sampleEmu->set_sample_rate(SAMPLE_RATE));
                handleError(gme_load_data(sampleEmu.get(), nsfFileBuffer.get(), length));

                sampleEmu->start_track(-1);
                sampleEmu->ignore_silence(false);

                auto sampleBuffer = std::make_shared<std::vector<short>>(masterBufferSize);
                std::fill(std::begin(*sampleBuffer), std::end(*sampleBuffer), 0);
                availableSamplers.push(std::make_pair(sampleEmu, sampleBuffer));
                // sampleEmus.push_back(std::move(sampleEmu));
            }

            trackInfo.reset(new track_info_t());
        } catch(std::runtime_error& ex) {
            HIKARI_LOG(debug) << ex.what();
            return false;
        }

        initialize(2, SAMPLE_RATE);
        //setCurrentTrack(0);

        return true;
    }
/* Member function ibitstream::readBit
 * ---------------------------------
 * If bits remain in curByte, retrieve next and increment pos
 * Else if end of curByte (or some other read happened), then read next byte
 * and start reading from bit position 0 of that byte.
 * If read byte from file at EOF, return EOF.
 */
int ibitstream::readBit() {
    if (!is_open()) {
        error("Cannot read a bit from a stream that is not open.");
    }

    // if just finished bits from curByte or if data read from stream after last readBit()
    if (lastTell != tellg() || pos == NUM_BITS_IN_BYTE) {
        if ((curByte = get()) == EOF) {
            // read next single byte from file
            return EOF;
        }
        pos = 0; // start reading from first bit of new byte
        lastTell = tellg();
    }
    int result = GetNthBit(pos, curByte);
    pos++;   // advance bit position for next call to readBit
    return result;
}
Exemple #9
0
void RealFile::open()
{
    _file.open(_path.c_str(), std::ios::in|std::ios::binary);
    if(!good()) {
        Log::error("Unable to open file", _path);
    } else {
        FileAnchor fileAnchor(*this);
        _file.seekg(0, std::ios::end);
        _size = tellg();
    }
}
    void ChunkManifestIngestor::IngestChunk(size_t index) const
    {
        if (index >= m_filePaths.size())
        {
            FatalError error("ChunkManifestIngestor: chunk index out of range.");
            throw error;
        }

        // TODO: this library method should not print to std::cout.
        std::cout << "  " << m_filePaths[index] << std::endl;

        auto input = m_fileSystem.OpenForRead(m_filePaths[index].c_str(),
                                              std::ios::binary);

        if (input->fail())
        {
            std::stringstream message;
            message << "Failed to open chunk file '"
                    << m_filePaths[index]
                    << "'";
            throw FatalError(message.str());
        }

        input->seekg(0, input->end);
        auto length = input->tellg();
        input->seekg(0, input->beg);

        std::vector<char> chunkData;
        chunkData.reserve(static_cast<size_t>(length) + 1ull);
        chunkData.insert(chunkData.begin(),
                         (std::istreambuf_iterator<char>(*input)),
                          std::istreambuf_iterator<char>());

        {
            // Block scopes IChunkWriter.
            // IChunkWriter's destructor zero-terminates its output and closes its stream.
            std::unique_ptr<IChunkWriter> chunkWriter;
            if (m_chunkWriterFactory != nullptr) {
                chunkWriter = m_chunkWriterFactory->CreateChunkWriter(index);
            }

            ChunkIngestor processor(m_configuration,
                                    m_ingestor,
                                    m_cacheDocuments,
                                    m_filter,
                                    chunkWriter.get());     // TODO: consider std::move chunkwriter to processor.

            ChunkReader(&chunkData[0],
                        &chunkData[0] + chunkData.size(),
                        processor);
        }
    }
Exemple #11
0
    Stream::Stream(const std::string& fileName)
    {
        auto s = std::make_unique<std::ifstream>();
        s->open(fileName, std::ios::in | std::ios::binary | std::ios::ate);
        if (!*s)
        {
            LOG_ERROR("Lvl file not found %s", fileName.c_str());
            throw Exception("File not found");
        }

        mSize = static_cast<size_t>(s->tellg());
        s->seekg(std::ios::beg);

        mStream.reset(s.release());
    }
PacketStreamReader::FrameInfo PacketStreamReader::Stream::peekFrameHeader(const PacketStreamReader& p)
{
    if (_frame)
        return _frame;

    _frame.frame_streampos = tellg();

    if (peekTag() == TAG_SRC_JSON)
    {
        readTag(TAG_SRC_JSON);
        _frame.src = readUINT();
        json::parse(_frame.meta, *this);
    }

    _frame.packet_streampos = tellg();

    readTag(TAG_SRC_PACKET);
    _frame.time = readTimestamp();

    if (_frame)
    {
        if (readUINT() != _frame.src)
            throw std::runtime_error("Frame preceded by metadata for a mismatched source. Stream may be corrupt.");
    }
    else
        _frame.src = readUINT();

    _frame.size = p.Sources()[_frame.src].data_size_bytes;
    if (!_frame.size)
        _frame.size = readUINT();
    _frame.sequence_num = p.GetPacketIndex(_frame.src);

    _tag = TAG_SRC_PACKET;

    return _frame;
}
Exemple #13
0
void
readFont(FontData &dst, const char *src)
{
   auto file = std::ifstream { src, std::ifstream::in | std::ifstream::binary };

   if (file.is_open()) {
      file.seekg(0, std::ifstream::end);
      dst.size = gsl::narrow_cast<uint32_t>(file.tellg());
      dst.data = reinterpret_cast<uint8_t *>(gSharedHeap->alloc(dst.size));
      file.seekg(0, std::ifstream::beg);
      file.read(reinterpret_cast<char*>(dst.data.get()), dst.size);
   } else {
      dst.size = 0;
      dst.data = nullptr;
   }
}
void Server::SendFile(std::pair<std::mutex*, UDPMetadata*>* _pair) {
	//auto mutex = &_pair->first;
	auto local_buffer = new char[UDP_BUFFER_SIZE];
	auto metadata = _pair->second;
	auto file = metadata->file;
	while (!metadata->file->eof() && metadata->returnAllPackages || metadata->missedPackages.size() > 0) 
	{
		{
			std::unique_lock<std::mutex> lock(*_pair->first);

			if (--metadata->currentDelay > 0) continue;
			metadata->currentDelay = metadata->delay;

			if (--metadata->packagesTillDrop <= 0) {
				//RemoveUDPClient(client);
				std::cout << "UDP client disconnected." << std::endl;
				break;
				//if (client == udpClients.end()) break;
			}
			
			/*if (metadata->missedPackages.size() > 0 )
				std::cout << metadata->missedPackages.size() << std::endl;*/
			if (metadata->missedPackages.size() > 0) {
				file->seekg(metadata->missedPackages[0] * UDP_BUFFER_SIZE);
				metadata->missedPackages.erase(metadata->missedPackages.begin());
			}
			else {
				//file->seekg(metadata->progress);
				file->seekg(metadata->progress);
				metadata->progress += UDP_BUFFER_SIZE;
			}
		}

		auto packageNumber = file->tellg() / UDP_BUFFER_SIZE;
		std::cout << packageNumber << std::endl;
		file->read(local_buffer, UDP_BUFFER_SIZE);
		auto dataSize = file->gcount();

		AddNumberToDatagram(local_buffer, dataSize, packageNumber);

		{
			std::unique_lock<std::mutex> lock(udpMutex);
			SendRawDataTo(_udp_socket, local_buffer, dataSize + UDP_NUMBER_SIZE, metadata->addr);
		}
	}
	std::cout << "UDP sending finished." << std::endl;
}
//--------------------------------------------------------------------
// @mfunc	Obtain the offsets into the file for each row
//			
//			
//
// @rdesc BOOL
//      @flag TRUE | Got the offsets, 
//      @flag FALSE | Could not obtain all the necessary info
//
BOOL CParseInitFile::ParseDataInfo()
{
	HRESULT hr = S_OK;
	m_cRows = 0;
	
	TRACE_CALL(L"PRIVLIB: CParseInitFile::ParseDataInfo.\n");

	//Skip over any lines, until the [DATA] section is reached...
	if(FAILED(hr = FindSection("[DATA]")))
		return FALSE;
    
	while(hr==S_OK)
    {
        //Check if we are provided with more rows than 
		//our current array can hold...
		if(m_cRows >= m_cRowOffsets || m_rgRowOffsets == NULL)
		{
			//Realloc the array
			m_cRowOffsets += MAX_ROW;
			m_rgRowOffsets = (DBLENGTH*)PROVIDER_REALLOC(m_rgRowOffsets, m_cRowOffsets*sizeof(DBLENGTH));
			if(m_rgRowOffsets == NULL)
				return FALSE;
		}
		
		//Save the Current Row Offset, so we can get this row data again
		//(ignoring null data)
        if(m_pvInput[0])
			m_rgRowOffsets[m_cRows] = tellg();
						
		//Get the NextLine of input
		//Note we allow S_FALSE to be returned for end of file...
		if(FAILED(hr = GetNextLine(m_pvInput, MAX_INPUT_BUFFER)))
		{
			odtLog << "ERROR:  Unable to find RowData for Row " << m_cRows+1 << " in INI <File:" << m_pszFileName << ">" << ENDL;
			odtLog << "ERROR:  Make sure your using a correctly generated INI File from TableDump.exe" << ENDL;
			return FALSE;
		}

		//We successfully retrieved this row...
		if(hr == S_OK)
			m_cRows++;
    }

	return TRUE;
}
Exemple #16
0
    std::vector<char> FileSystem::readFileAsCharBuffer(const std::string & fileName) {
        std::vector<char> buffer;

        if(exists(fileName)) {
            int length = 0;
            auto fs = openFileRead(fileName);

            fs->seekg (0, std::ios::end);
            length = static_cast<int>(fs->tellg());
            fs->seekg (0, std::ios::beg);

            buffer.reserve(length);

            fs->read(&buffer[0], length);
        }

        return buffer;
    }
Exemple #17
0
//------------------------------------------------------------------------------
void SlpFrame::readEdges()
{
  std::streampos cmd_table_pos = slp_file_pos_ + std::streampos(cmd_table_offset_);
  
  left_edges_.resize(height_);
  right_edges_.resize(height_);
  
  uint32_t row_cnt = 0;
  
  while (tellg() < cmd_table_pos)
  {
    left_edges_[row_cnt] = read<int16_t>();
    right_edges_[row_cnt] = read<int16_t>();
    
    row_cnt ++;
  }
 
}
Exemple #18
0
void CConn_MemoryStream::ToVector(vector<char>* vec)
{
    if (!vec) {
        NCBI_THROW(CIO_Exception, eInvalidArg,
                   "CConn_MemoryStream::ToVector(NULL) is not allowed");
    }
    CConn_Streambuf* sb = dynamic_cast<CConn_Streambuf*>(rdbuf());
    size_t size = sb  &&  good() ? (size_t)(tellp() - tellg()) : 0;
    vec->resize(size);
    if (sb) {
        size_t s = (size_t) sb->sgetn(&(*vec)[0], size);
#ifdef NCBI_COMPILER_WORKSHOP
        if (s < 0) {
            s = 0;  // WS6 weirdness to sometimes return -1 from sgetn() :-/
        } else
#endif //NCBI_COMPILER_WORKSHOP
        _ASSERT(s == size);
        vec->resize(s);  // NB: just in case, essentially NOOP when s == size
    }
}
Exemple #19
0
// GuessEOLType():  Attampts to determine the EOL type by
//  scanning the source file.  Returns the old eoltype, or
//  or -1 if the search failed.
// The search starts from the begining of the file.  This will
//  always work unless, for some reason, there is an LF or CR
//  in the middle of a line.  The file pointer is returned to
//  it's previous location once the scan is over.
// Note that this code is not general, and won't must be modified
//  to find new EOL types (although I doubt there will be any...)
int pifstream::GuessEOLType() {
  int oldeol = eol;
  int length = 0;  
  char c;

  // Store the file position and jump to the start
  streampos oldpos = tellg();
  seekg( 0, ios::beg );

  // Begin Search
  while( good() ) {
    get(c);

    // LF Test
    if( c == '\n' ) {
      eol = LF;
      break;
    }

    // CR/CR+LF Test
    if( c == '\r' ) {
      get(c);
      if( c == '\n' )
        eol = CRLF;
      else {
        eol = CR;
      }
      break;
    }
  };

  // Finish up
  seekg( oldpos, ios::beg );

  if( !good() )
    return( -1 );
  else
    return( oldeol );
}
Exemple #20
0
bool
AperyBook::open(const char * fname)
{
  file_name_ = "";

  if (is_open())
    close();

  std::ifstream::open(fname, std::ifstream::in | std::ifstream::binary | std::ios::ate);
  if (!is_open())
    return false;

  size_ = tellg() / sizeof(AperyBookEntry);

  if (!good())
  {
    std::cerr << "Failed to open book file " << fname << std::endl;
    return false;
  }

  file_name_ = fname;
  return true;
}
bool Book::OpenBook(const char* fName) {
	this->m_fileName_ = "";

	if (is_open()) {
		close();
	}

	std::ifstream::open(fName, std::ifstream::in | std::ifstream::binary | std::ios::ate);

	if (!is_open()) {
		return false;
	}

	this->m_size_ = tellg() / sizeof(BookEntry);

	if (!good()) {
		std::cerr << "Failed to open book file " << fName  << std::endl;
		exit(EXIT_FAILURE);
	}

	this->m_fileName_ = fName;
	return true;
}
void Server::SendBlock(CLIENT_INFO clientInfo)
{
	char buffer[BUFFER_SIZE];
	auto file = clientInfo->second;
	try {
		while (file)
		{
			file->read(buffer, BUFFER_SIZE);
			size_t read = file->gcount();
			size_t realySent = SendRawData(clientInfo->first, buffer, read);
			if (realySent != read)
			{
				fpos_t pos = file->tellg();
				file->seekg(pos - (read - realySent));
			}
		}
	} catch(std::runtime_error e) {
		
	}
	FD_CLR(clientInfo->first, &clientsSet);
	file->close();
	shutdown(clientInfo->first, SD_BOTH);
	closesocket(clientInfo->first);
}
Exemple #23
0
//------------------------------------------------------------------------------
void SlpFrame::load(std::istream &istr)
{
  setIStream(istr);
  
  image_pixel_indexes_ = new uint8_t[width_ * height_];
  std::fill_n(image_pixel_indexes_, width_ * height_, transparent_index_);
  
  readEdges();
  
  // Skipping command offsets. They are not needed now but
  // they can be used for checking file integrity.
  for (uint32_t i=0; i < height_; i++)
  {
    read<uint32_t>();
  }
  
  // Each row has it's commands, 0x0F signals the end of a rows commands.
  for (uint32_t row = 0; row < height_; row++)
  {
    //std::cout << row << ": " << std::hex << (int)(tellg() - file_pos_) << std::endl;
    uint8_t data = 0;
    uint32_t pix_pos = left_edges_[row]; //pos where to start putting pixels
    
    while (data != 0x0F)
    {
      data = read<uint8_t>();
     
      //if ( (tellg() - file_pos_) == 0x1630)
      //  std::cout << row << ": " << std::hex << (int)(tellg() - file_pos_) << " cmd: " << (int)data<< std::endl;
      if (data == 0x0F)
        break;
      
      uint8_t cmd = data & 0xF;
      
      uint32_t pix_cnt = 0;
      
      //uint32_t pix_pos = left_edges_[row];
      uint8_t color_index = 0;
      
      switch (cmd) //0x00
      {
        case 0: // Lesser block copy
        case 4:
        case 8:
        case 0xC:
          pix_cnt = data >> 2;
          readPixelsToImage(row, pix_pos, pix_cnt);
          break;
        
        case 1: // lesser skip (making pixels transparent)
        case 5:
        case 9:
        case 0xD:
          pix_cnt = (data & 0xFC) >> 2;
          pix_pos += pix_cnt;
          break;
          
        case 2: // greater block copy
          pix_cnt = ((data & 0xF0) << 4) + read<uint8_t>();
          
          readPixelsToImage(row, pix_pos, pix_cnt);
          break;
          
        case 3: // greater skip
          pix_cnt = ((data & 0xF0) << 4) + read<uint8_t>();
          pix_pos += pix_cnt;
          break;
          
        case 6: // copy and transform (player color)
          pix_cnt = getPixelCountFromData(data);
 
          // TODO: player color
          readPixelsToImage(row, pix_pos, pix_cnt, true);
          
          break;
          
        case 7: // Run of plain color
          pix_cnt = getPixelCountFromData(data);
          
          color_index = read<uint8_t>();
          setPixelsToColor(row, pix_pos, pix_cnt, 
                           color_index);
        break;
        
        case 0xA: // Transform block (player color)
          pix_cnt = getPixelCountFromData(data);
          
          // TODO: file_.readuint8_t() | player_color
          color_index = read<uint8_t>();
          setPixelsToColor(row, pix_pos, pix_cnt, 
                           color_index, true);
        break;
        
        case 0x0B: // Shadow pixels
          //TODO: incomplete
          pix_cnt = getPixelCountFromData(data);
          pix_pos += pix_cnt; //skip until implemented
          
        break;
        
        case 0x0E: // extended commands.. TODO
        
          switch (data)
          {
            /*case 0x0E: //xflip?? skip?? TODO
            case 0x1E:
              //row-= 1;   
            break;
            */
            
            case 0x4E: //Outline pixel TODO player color
            case 0x6E: 
              //setPixelsToColor(outline_, row, pix_pos, 1, sf::Color(255,100,0));
              pix_pos += 1;
            break;
            
            case 0x5E: //Outline run TODO player color
            case 0x7E: 
              pix_cnt = read<uint8_t>();
              pix_pos += pix_cnt;
              
              //setPixelsToColor(outline_, row, pix_pos, pix_cnt, 
              //                 sf::Color(255 ,100,0));
            break;
          }

        break;
        default:
          std::cerr << "SlpFrame: Unknown cmd at " << std::hex << 
                  (int)(tellg() - slp_file_pos_)<< ": " << (int) data << std::endl;
          break;
      }
      
    }
  }
  
}
Exemple #24
0
                {
                    DWORD dwSizeRead;
                    if (ReadFile(hFile, _pData, dwSize, &dwSizeRead, NULL) && (dwSizeRead == dwSize))
                    {
                        _cbSize = dwSize;
                        _iIndex = 0;
                        _cbSizeValid = dwSize;
                    }
                }
            }
        }
    }

    istream &istream::operator>> (WORD &w)
    {
        DWORD dwSave = tellg();
        if (!_ReadWord(&w))
        {
            seekg(dwSave);
            w = 0;
            _state = std::ios_base::eofbit | std::ios_base::failbit;
        }
        return *this;
    }
    istream &istream::operator>> (BYTE &b)
    {
        DWORD dwSave = tellg();
        if (!_ReadByte(&b))
        {
            seekg(dwSave);
            b = 0;
Exemple #25
0
static bool
loadTestElf(const std::string &path, TestFile &tests)
{
   // Open file
   auto file = std::ifstream { path, std::ifstream::binary };
   auto buffer = std::vector<char> {};

   if (!file.is_open()) {
      return false;
   }

   // Get size
   file.seekg(0, std::istream::end);
   auto size = (unsigned int)file.tellg();
   file.seekg(0, std::istream::beg);

   // Read whole file
   buffer.resize(size);
   file.read(buffer.data(), size);
   assert(file.gcount() == size);
   file.close();

   auto in = BigEndianView { buffer.data(), size };
   auto header = elf::Header {};
   auto info = elf::FileInfo {};
   auto sections = std::vector<elf::Section> {};

   // Read header
   if (!elf::readHeader(in, header)) {
      gLog->error("Failed elf::readHeader");
      return false;
   }

   // Read sections
   if (!elf::readSections(in, header, sections)) {
      gLog->error("Failed elf::readSections");
      return false;
   }

   // Find the code and symbol sections
   elf::Section *codeSection = nullptr;
   elf::Section *symbolSection = nullptr;

   for (auto &section : sections) {
      if (section.header.type == elf::SHT_PROGBITS && (section.header.flags & elf::SHF_EXECINSTR)) {
         codeSection = &section;
      }

      if (section.header.type == elf::SHT_SYMTAB) {
         symbolSection = &section;
      }
   }

   if (!codeSection) {
      gLog->error("Could not find code section");
      return false;
   }

   if (!symbolSection) {
      gLog->error("Could not find symbol section");
      return false;
   }

   // Find all test functions (symbols)
   auto symView = BigEndianView { symbolSection->data.data(), symbolSection->data.size() };
   auto symStrTab = sections[symbolSection->header.link].data.data();
   auto symCount = symbolSection->data.size() / sizeof(elf::Symbol);

   for (auto i = 0u; i < symCount; ++i) {
      elf::Symbol symbol;
      elf::readSymbol(symView, symbol);
      auto type = symbol.info & 0xf;
      auto name = symbol.name + symStrTab;

      if (type == elf::STT_SECTION) {
         continue;
      }

      if (symbol.value == 0 && symbol.name == 0 && symbol.shndx == 0) {
         continue;
      }

      tests.tests[name].offset = symbol.value;
   }

   tests.code = std::move(codeSection->data);
   return true;
}
//
// Use GetPair retrieve the next comment and corresponding value from the
// Iometer config file.  (They are returned by reference in key and value.)
//
// Assumes the file pointer is at the beginning of a comment line (followed
// by zero or more additional comment lines).  If the first comment line is
// an end marker (end of a config file section), GetPair sets key equal to
// the comment and value empty.  If the first comment line is NOT an end
// marker, the remaining comment lines (if any) are skipped and the next
// line containing data is stored in value.
//
// Return value of FALSE indicates an error.  The calling function
// should report a specific error.
//
BOOL ICF_ifstream::GetPair(CString& key, CString& value)
{
	streampos placeholder;
	CString tempstring;

	if (!is_open())
	{
		// This should never happen.  This indicates an Iometer bug.
		ErrorMessage("A call was made to ICF_ifstream::GetPair() "
			"with a closed file!  Please report this as an Iometer bug.");
		return FALSE;
	}

	// If EOF, let the caller report an error.
	if (rdstate())
		return FALSE;

	// If first line isn't a comment, let the caller report an error.
	if (peek() != '\'')
		return FALSE;

	key = GetNextLine();

	// If this is an END or VERSION tag, there will be no data
	// corresponding to the comment on the following lines, so don't
	// look for any.
	//
	// Note that strlen is used (instead of a constant) to make the
	// connection between the string and its length obvious to someone
	// making changes to the code.
	if ( key.Left((int)(strlen("'End"))).CompareNoCase("'End") == 0
		|| key.Left((int)(strlen("'Version"))).CompareNoCase("'Version") == 0)
	{
		value.Empty();
		return TRUE;
	}

	// Skip any extra comment lines before the data.
	while (peek() == '\'')
	{
		placeholder = tellg();
		tempstring = GetNextLine();

		if ( tempstring.Left((int)(strlen("'End"))).CompareNoCase("'End") == 0
			|| tempstring.Left((int)(strlen("'Version"))).CompareNoCase("'Version") == 0)
		{
			// This only happens when there is an empty section, like:
			//	'Worker
			//	'End worker
			// Worker name is normally expected after the "'Worker" header,
			// but in this case, it's only an "'End worker" header.  The
			// "'End..." header must be returned in the NEXT call to GetPair.

			seekg( placeholder );	// back up the file pointer
			value.Empty();
			return TRUE;			// NOT an error condition
		}
	}

	// If EOF, let the caller report an error.
	if (rdstate())
	{
		value.Empty();
		return FALSE;
	}

	// Get the data corresponding to the above key.
	value = GetNextLine();

	return TRUE;
}
Exemple #27
0
//------------------------------------------------------------------------------
void DatFile::serializeObject(void)
{
  compressor_.beginCompression();

  serialize(FileVersion, FILE_VERSION_SIZE);

  if (getGameVersion() >= genie::GV_SWGB)
  {
    serializeSize<uint16_t>(civ_countSW_, Civs.size());
    serialize<int32_t>(SUnknown2);
    serialize<int32_t>(SUnknown3);
    serialize<int32_t>(SUnknown4);
    serialize<int32_t>(SUnknown5);

    if (verbose_)
    {
      std::cout << "Unkown1: " << civ_countSW_ << std::endl;
      std::cout << "Unkown2: " << SUnknown2 << std::endl;
      std::cout << "Unkown3: " << SUnknown3 << std::endl;
      std::cout << "Unkown4: " << SUnknown4 << std::endl;
      std::cout << "Unkown5: " << SUnknown5 << std::endl;
    }
  }

  serializeSize<uint16_t>(terrain_restriction_count_, TerrainRestrictions.size());
  serialize<uint16_t>(TerrainsUsed1);

  if (verbose_)
  {
    std::cout << FileVersion;
    std::cout << std::endl << "TerRestrictionCount: " <<terrain_restriction_count_ << std::endl;
    std::cout << "TerCount: " << TerrainsUsed1 << std::endl;
  }

  serialize<int32_t>(TerrainRestrictionPointers1, terrain_restriction_count_);

  if (getGameVersion() >= genie::GV_AoKA)
    serialize<int32_t>(TerrainRestrictionPointers2, terrain_restriction_count_);

  TerrainRestriction::setTerrainCount(TerrainsUsed1);
  serializeSub<TerrainRestriction>(TerrainRestrictions, terrain_restriction_count_);

  serializeSize<uint16_t>(player_color_count_, PlayerColours.size());

  if (verbose_)
    std::cout << "PlayerColours: " << player_color_count_ << std::endl;

  serializeSub<PlayerColour>(PlayerColours, player_color_count_);

  serializeSize<uint16_t>(sound_count_, Sounds.size());

  if (verbose_)
    std::cout << "Sounds: " << sound_count_ << std::endl;

  serializeSub<Sound>(Sounds, sound_count_);

  serializeSize<uint16_t>(graphic_count_, GraphicPointers.size());
  if (getGameVersion() < genie::GV_AoE)
  {
    serializeSub<Graphic>(Graphics, graphic_count_);
  }
  else
  {
    serialize<int32_t>(GraphicPointers, graphic_count_);
    serializeSubWithPointers<Graphic>(Graphics, graphic_count_, GraphicPointers);
  }

  if (verbose_)
  {
    std::cout << "Graphics: " << Graphics.size() << std::endl;

    std::cout << "Terrain block " << "(0x" << std::hex << tellg();
  }
  serialize<ISerializable>(TerrainBlock);

  // This data seems to be needed only in AoE and RoR.
  // In later games it is removable.
  // It exists in Star Wars games too, but is not used.
  if (getGameVersion() >= genie::GV_AoEB) // Temp fix
  serialize<ISerializable>(RandomMaps);

  if (verbose_)
    std::cout << " to 0x" <<std::hex << tellg() << std::dec << ")" << std::endl;

  serializeSize<uint32_t>(techage_count_, Techages.size());

  if (verbose_)
    std::cout << "Techage: " << techage_count_ << std::endl;

  serializeSub<Techage>(Techages, techage_count_);

  if (getGameVersion() >= genie::GV_SWGB) //pos: 0x111936
  {
    serializeSize<uint16_t>(unit_line_count_, UnitLines.size());
    serializeSub<UnitLine>(UnitLines, unit_line_count_);
  }

  if (getGameVersion() >= genie::GV_AoK)
  {
    serializeSize<uint32_t>(unit_count_, UnitHeaders.size());

    if (verbose_)
      std::cout << "Unitcount: " << unit_count_ << std::endl;

    serializeSub<UnitHeader>(UnitHeaders, unit_count_);
  }

  serializeSize<uint16_t>(civ_count_, Civs.size());

  if (verbose_)
    std::cout << "Civcount: " << civ_count_ << std::endl;

  serializeSub<Civ>(Civs, civ_count_);

  if (getGameVersion() >= genie::GV_SWGB)
    serialize<int8_t>(SUnknown7);

  serializeSize<uint16_t>(research_count_, Researchs.size());

  if (verbose_)
    std::cout << "Researchcount: " << research_count_ << std::endl;

  serializeSub<Research>(Researchs, research_count_);

  if (verbose_)
    std::cout << "TechTrees (before eof) (0x" << std::hex << tellg();

  if (getGameVersion() >= genie::GV_SWGB)
    serialize<int8_t>(SUnknown8);

  if (getGameVersion() >= genie::GV_AoKA)
  {
    serialize<int32_t>(UnknownPreTechTree, 7);
    serialize<ISerializable>(TechTree);
  }

  if (verbose_)
    std::cout << "to 0x" <<std::hex << tellg() << std::dec << ")" << std::endl;

  compressor_.endCompression();
}
std::shared_ptr<Image_8> Bitmap_Handler::get_Image(const char * file_name)
{

	auto input = std::ifstream{ file_name, std::ios::binary };
	if (input.is_open()) {

		// get length of file:
		input.seekg(0, input.end);
		unsigned long length = input.tellg();
		input.seekg(0, input.beg);

		if (length > 50) {

			//Should be replaced by an import into a vector
			char *buffer = new char[length];
			input.read(buffer, length);

			auto data_offset = merge_bytes_unsigned(buffer, 10, 4);

			auto size_x = merge_bytes_unsigned(buffer, 18, 4);

			auto size_y = merge_bytes_unsigned(buffer, 22, 4);

			auto biBitCount = merge_bytes_unsigned(buffer, 28, 2);

			auto bCompression = merge_bytes_unsigned(buffer, 30, 24);

			auto output = std::make_shared<Image_8>(size_x, size_y);

			unsigned long index = data_offset;

			unsigned int row_ind = 0;

			auto pixels = output->get_pixels();

			unsigned int z = 0;

			for (auto it = pixels->begin(); it != pixels->end(); it++) {
				(*it).set_RGB((unsigned char)(buffer[index]),(unsigned char)(buffer[index + 1]),(unsigned char)(buffer[index + 2]));
				index += 3;
				row_ind += 3;
				z++;
				//Catch last Bytes in Row
				if (z == (size_x)) {
					while (row_ind % 4 != 0) { index++; row_ind++; }
					row_ind = 0;
					z = 0;
				}
			}

			input.close();

			delete[] buffer;

			return output;
		}
	}
	else {
		return std::make_shared<Image_8>(0, 0);
		std::cout << "Image not found" << std::endl;
	}
}
Exemple #29
0
//------------------------------------------------------------------------------
void DatFile::serializeObject(void )
{
  compressor_.beginCompression();

  serialize<char>(&file_version_, FILE_VERSION_LEN);

  if (getGameVersion() >= genie::GV_SWGB)
  {
    serializeSize<uint16_t>(civ_countSW_, Civs.size());
    serialize<int32_t>(SUnknown2);
    serialize<int32_t>(SUnknown3);
    serialize<int32_t>(SUnknown4);
    serialize<int32_t>(SUnknown5);

    if (verbose_)
    {
      std::cout << "Unkown1: " << civ_countSW_ << std::endl;
      std::cout << "Unkown2: " << SUnknown2 << std::endl;
      std::cout << "Unkown3: " << SUnknown3 << std::endl;
      std::cout << "Unkown4: " << SUnknown4 << std::endl;
      std::cout << "Unkown5: " << SUnknown5 << std::endl;
    }
  }

  serializeSize<uint16_t>(terrain_restriction_count_, TerrainRestrictions.size());
  serialize<uint16_t>(NumberOfTerrainsUsed);

  if (verbose_)
  {
    std::cout << file_version_ << std::endl;
    std::cout << "TerRestrictionCount: " <<terrain_restriction_count_ << std::endl;
    std::cout << "TerCount: " << NumberOfTerrainsUsed << std::endl;
  }

  serialize<int32_t>(TerrainRestrictionPointers1, terrain_restriction_count_);

  if (getGameVersion() >= genie::GV_AoK)
    serialize<int32_t>(TerrainRestrictionPointers2, terrain_restriction_count_);

  TerrainRestriction::setTerrainCount(NumberOfTerrainsUsed);
  serializeSub<TerrainRestriction>(TerrainRestrictions, terrain_restriction_count_);

  serializeSize<uint16_t>(player_color_count_, PlayerColours.size());

  if (verbose_)
    std::cout << "PlayerColours: " << player_color_count_ << std::endl;

  serializeSub<PlayerColour>(PlayerColours, player_color_count_);

  serializeSize<uint16_t>(sound_count_, Sounds.size());

  if (verbose_)
    std::cout << "Sounds: " << sound_count_ << std::endl;

  serializeSub<Sound>(Sounds, sound_count_);

  serializeSize<uint16_t>(graphic_count_, GraphicPointers.size());
  serialize<int32_t>(GraphicPointers, graphic_count_);
  serializeSubWithPointers<Graphic>(Graphics, graphic_count_, GraphicPointers);

  if (verbose_)
  {
    std::cout << "Graphics: " << Graphics.size() << std::endl;

    std::cout << "GraphicsRendering " << "(0x" << std::hex << tellg();
  }
  //TODO: AoE/RoR: maybe:
  // struct { short unknown[3]; } terrainheader[terrain_count_]
  serialize<int16_t>(&GraphicsRendering, TERRAIN_HEADER_SIZE);

  if (verbose_)
    std::cout << " to 0x" << std::hex << tellg() << std::dec << ")" << std::endl;

  switch (getGameVersion())
  {
    case genie::GV_AoE:
    case genie::GV_RoR:
    case genie::GV_AoK:  serializeSub<Terrain>(Terrains, 32); break;
    case genie::GV_TC:   serializeSub<Terrain>(Terrains, 42); break;
    case genie::GV_SWGB:
    case genie::GV_CC:   serializeSub<Terrain>(Terrains, 55); break;
    default: break;
  }

  if (verbose_)
    std::cout << "Unknown2 (empty) (0x" << std::hex << tellg() << std::dec;

  serialize<int16_t>(Unknown2);

  if (verbose_)
    std::cout << " to 0x" << std::hex << tellg() << std::dec << ")" << std::endl;


  // TerrainBorders seem to be unused (are empty) in GV > RoR
  serializeSub<TerrainBorder>(TerrainBorders, 16); //TODO: fixed size?

  switch (getGameVersion()) // Empty space.
  {
    case genie::GV_AoE:
    case genie::GV_RoR:  serialize<int32_t>(&ZeroSpace, 1); break;
    case genie::GV_AoK:
    case genie::GV_TC:
    case genie::GV_SWGB:
    case genie::GV_CC:   serialize<int32_t>(&ZeroSpace, 7); break;
    default: break;
  }

  serialize<uint16_t>(NumberOfTerrainsUsed2);

  if (verbose_)
    std::cout << "RenderingPlusSomething?? (0x" << std::hex << tellg() << std::dec;

  serialize<int16_t>(&Rendering, 19);

  switch (getGameVersion()) // Few pointers and small numbers.
  {
    case genie::GV_AoE:
    case genie::GV_RoR:  serialize<int32_t>(&Something, 6); break;
    case genie::GV_AoK:
    case genie::GV_TC:   serialize<int32_t>(&Something, 162); break;
    case genie::GV_SWGB:
    case genie::GV_CC:   serialize<int32_t>(&Something, 163); break;
    default: break;
  }

  // This data seems to be needed only in AoE and RoR.
  // In later games it is removable.
  // It exists in Star Wars games too, but is not used.
  serialize<ISerializable>(Unknown);

  if (verbose_)
    std::cout << " to 0x" <<std::hex << tellg() << std::dec << ")" << std::endl;

  serializeSize<uint32_t>(techage_count_, Techages.size());

  if (verbose_)
    std::cout << "Techage: " << techage_count_ << std::endl;

  serializeSub<Techage>(Techages, techage_count_);

  if (getGameVersion() >= genie::GV_SWGB) //pos: 0x111936
  {
    serializeSize<uint16_t>(unit_line_count_, UnitLines.size());
    serializeSub<UnitLine>(UnitLines, unit_line_count_);
  }

  if (getGameVersion() >= genie::GV_AoK)
  {
    serializeSize<uint32_t>(unit_count_, UnitHeaders.size());

    if (verbose_)
      std::cout << "Unitcount: " << unit_count_ << std::endl;

    serializeSub<UnitHeader>(UnitHeaders, unit_count_);
  }

  serializeSize<uint16_t>(civ_count_, Civs.size());

  if (verbose_)
    std::cout << "Civcount: " << civ_count_ << std::endl;

  serializeSub<Civ>(Civs, civ_count_);

  if (getGameVersion() >= genie::GV_SWGB)
    serialize<char>(SUnknown7);

  serializeSize<uint16_t>(research_count_, Researchs.size());

  if (verbose_)
    std::cout << "Researchcount: " << research_count_ << std::endl;

  serializeSub<Research>(Researchs, research_count_);

  if (verbose_)
    std::cout << "TechTrees (before eof) (0x" << std::hex << tellg();

  if (getGameVersion() >= genie::GV_SWGB)
    serialize<char>(SUnknown8);

  if (getGameVersion() >= genie::GV_AoK)
  {
    serialize<int32_t>(UnknownPreTechTree, 7);
    serialize<ISerializable>(TechTree);
  }

  if (verbose_)
    std::cout << "to 0x" <<std::hex << tellg() << std::dec << ")" << std::endl;

  compressor_.endCompression();
}