Beispiel #1
0
/**
 * Load image from a file
 */
bool Image::load_file(std::string infile) {
  // Identify the image format
  char header[5];
  std::ifstream istream;
  istream.open(infile, std::ios::binary|std::ios::in);
  istream.read((char *)&header, 4);
  istream.close();

  if (identify_format(header, this->file_format, this->file_extension) < 0) {
    std::cout << "Cannot identify image format" << std::endl;
    return false;
  }

  // Open and resize the image with OpenCV
  src = cv::imread(infile);
  if (!src.data) {
    std::cout << "Unable to read in the image" << std::endl;
    return false;
  }

  return true;
}
Beispiel #2
0
//
// Loads a file of known hashes.
// First identifies the file type, then reads the file.
 //
hashlist::loadstatus_t 
hashlist::load_hash_file(display *ocb,const std::string &fn)
{
  loadstatus_t status = loadstatus_ok;
  hashfile_format type;

  FILE *hl_handle = fopen(fn.c_str(),"rb");
  if (NULL == hl_handle)
  {
    if (ocb) 
      ocb->error("%s: %s", fn.c_str(), strerror(errno));
    return status_file_error;
  }
  
  type = identify_format(ocb,fn,hl_handle);
  if (file_unknown == type)
  {
    if (ocb) 
      ocb->error("%s: Unable to identify file format", fn.c_str());
    fclose(hl_handle);
    hl_handle = 0;
    return status_unknown_filetype;
  }

  bool contains_bad_lines = false;
  bool record_valid;

  // We start our counter at line number two for the two lines
  // of header we've already read
  uint64_t line_number = 2;

  // TODO: Read the line directly into a std::string
  char line[MAX_STRING_LENGTH];	
  while (fgets(line,MAX_STRING_LENGTH,hl_handle)) 
  {
    line_number++;			
    
    // Lines starting with a pound sign are comments and can be ignored
    if ('#' == line[0])
      continue;

    // C++ typically fails with a bad_alloc, but you can make it return null
    // http://www.cplusplus.com/reference/std/new/bad_alloc/
    // http://www.cplusplus.com/reference/std/new/nothrow/
    file_data_t *t = new (std::nothrow) file_data_t(); 
    if (NULL == t)
    {
      ocb->fatal_error("%s: Out of memory in line %"PRIu64,
		       fn.c_str(), line_number);
    }
    
    chop_line(line);
    record_valid = true;

    // Convert the input line to a string for easier manipulations
    std::string line_as_string(line);
    std::vector<std::string> fields = split(line_as_string,',');

    size_t column_number;
    for (column_number=0 ; column_number<fields.size() ; column_number++)
    {
      std::string word = fields[column_number];

      // The first column should always be the file size
      if (0 == column_number)
      {
	t->file_bytes = (uint64_t)strtoll(word.c_str(),NULL,10);
	continue;
      }

      if (column_number == filename_column)
      {
	// If the filename contained commas, it was split 
	// incorrectly by the 'split' statememt above. The filename
	// will be split across more than one column.
	// As such we need to 'find' everything
	// in the string starting with the current location.
	// The result should be closer to the end of the string than
	// the start, so we can use rfind. (This also avoids a problem
	// when the filename is the same as one of the hashes, which
	// happens now and again.)
	size_t start = line_as_string.rfind(word);
	t->file_name = line_as_string.substr(start,std::string::npos);

	// This should be the last column, so we break out now.
	break;
      }

      // All other columns should contain a valid hash in hex
      if ( !algorithm_t::valid_hash(hash_column[column_number],word))
      {
	if (ocb) 
	  ocb->error("%s: Invalid %s hash in line %"PRIu64,
		     fn.c_str(), 
		     hashes[hash_column[column_number]].name.c_str(),
		     line_number);
	contains_bad_lines = true;
	record_valid = false;
	// Break out (done = true) and then process the next line
	break;
      }
      
      // Convert the hash to a std::string and save it
      lowercase(word);
      t->hash_hex[hash_column[column_number]] = word;
    }

    if (record_valid) 
      add_fdt(t);
  }

  fclose(hl_handle);
  hl_handle = 0;

  if (contains_bad_lines)
    return status_contains_bad_hashes;
    
  return status;
}