bool isValidIP(char* ip_address)
{
        int seperator = 0;
        if(ip_address == NULL){
                return false;
        }
        else{
                //printf("here\n");
                char *eachByte;
                eachByte = strtok(ip_address,".");
                //printf("Eachbyte is\n");
                if(eachByte == NULL){
                        return false;
                }
                while(eachByte)
                {       
                        //Ayyo devre idakondu function bari beka :P
                        // pthuu python! :D
                        if(!valid_byte(eachByte)){
                                return false;
                        }
                        int value = atoi(eachByte);
                        if(value < 0 && value > 255){
                                return false;
                        }
                        printf("Here\n");
                        eachByte = strtok(NULL,".");
                        //printf("Here\n")
                        if(eachByte){
                                ++seperator;
                        }
                        
                }
               // printf("\n");
        }
       if(seperator != 3){
                return false;
       }
        return true;
}
Beispiel #2
0
/*
Corrupt the loaded file with the parameters in the PSPCorruptionInfo class
*/
void PSPCorruption::corrupt()
{
  if (info->step() > 0 && info->files().size() > 0)
  {
    //  Copy file only if step is valid and there are files to corrupt.
    boost::filesystem::copy_file(this->m_original_file, this->m_temp_file, boost::filesystem::copy_option::overwrite_if_exists);
  }
  else
  {
    std::cout << "Not corrupting: " << info->step() << "\t" << info->files().size() << std::endl;
    return; //  If no step or files to corrupt then return
  }

  //  For counting amount of corruptions
  uint32_t corruptions = 0;

  //  Random number distribution over the byte value range
  std::uniform_int_distribution<int> random(0x00, 0xFF);

  //  Open file for read/write in binary mode
  std::fstream img(this->m_temp_file, std::ios::in | std::ios::out | std::ios::binary);

  //  If image could not be read then throw an exception
  if (!img.good())
  {
    throw InvalidFileException("Could not open temp file: " + this->m_temp_file);
  }

  for (auto& file : info->files())
  {
    if (file == "")
    {
      continue;
    }

    //  Read the file into wad
    Entry entry = (*this->rom)[file];
    //  Get the raw data of the entry
    std::vector<uint8_t> data;

    try
    {
      data = entry.get(img, false);
    }
    catch (...)
    {
      //std::cout << "Could not find file '" << file << "'" << std::endl;
      continue;
    }

    //  If no data, then throw an exception
    if (data.empty())
    {
      //std::cout << "No data found in file '" << file << "'" << std::endl;
      continue;
    }

    for (uint32_t i = info->start(); (i < data.size()) && (i < info->end()); i += info->step())
    {
      if (info->type() == CorruptionType::Shift)
      {
        //  If it's okay to put the other byte in this position then change it
        if (i + info->value() < data.size() && valid_byte(data[i + info->value()], i))
        {
          corruptions++;
          data[i] = data[i + info->value()];
        }
      }
      else if (info->type() == CorruptionType::Swap)
      {

        if (i + info->value() < data.size() &&
          valid_byte(data[i + info->value()], i) &&  //  If it's okay to put the other byte in this position
          valid_byte(data[i], i + info->value()))    //  And it's okay to put this byte in the other position
        {
          corruptions++;
          uint8_t temp = data[i + info->value()];
          data[i] = temp;
          data[i + info->value()] = temp;
        }
      }
      else if (info->type() == CorruptionType::Add)
      {
        //  If the new byte value is valid then do the corruption
        if (valid_byte(data[i] + info->value(), i))
        {
          corruptions++;
          data[i] += info->value();
        }
      }
      else if (info->type() == CorruptionType::Set)
      {
        //  If the set value can be placed here then do it
        if (valid_byte(info->value(), i))
        {
          corruptions++;
          data[i] = info->value();
        }
      }
      else if (info->type() == CorruptionType::Random)
      {
        bool corrupted = false;

        //  Try up to 100 times to corrupt
        for (uint32_t retry = 0; !corrupted && retry < 100; retry++)
        {
          uint8_t rand = random(this->random);
          if (valid_byte(rand, i))
          {
            corruptions++;
            data[i] = rand;
            corrupted = true;
          }
        }
      }
      else if (info->type() == CorruptionType::RotateLeft)
      {
        uint8_t rotate = util::rol<uint8_t>(data[i], info->value());

        if (valid_byte(rotate, i))
        {
          data[i] = rotate;
          corruptions++;
        }
      }
      else if (info->type() == CorruptionType::RotateRight)
      {
        uint8_t rotate = util::ror<uint8_t>(data[i], info->value());

        if (valid_byte(rotate, i))
        {
          data[i] = rotate;
          corruptions++;
        }
      }
      else if (info->type() == CorruptionType::LogicalAnd)
      {
        if (valid_byte(data[i] & info->value(), i))
        {
          data[i] &= info->value();
          corruptions++;
        }
      }
      else if (info->type() == CorruptionType::LogicalOr)
      {
        if (valid_byte(data[i] | info->value(), i))
        {
          data[i] |= info->value();
          corruptions++;
        }
      }
      else if (info->type() == CorruptionType::LogicalXor)
      {
        if (valid_byte(data[i] ^ info->value(), i))
        {
          data[i] ^= info->value();
          corruptions++;
        }
      }
      else if (info->type() == CorruptionType::LogicalComplement)
      {
        if (valid_byte(~data[i], i))
        {
          data[i] = ~data[i];
          corruptions++;
        }
      }
      else
      {
        break;  //  No corruption selected, might as well quit.
      }
    }

    //  Write the modified data back to the file
    entry.write(img, data, false);
  }

  //  Close the file
  img.close();

  //  Tell the user how many bytes were corrupted
  std::cout << corruptions << " bytes corrupted." << std::endl;
}