Ejemplo n.º 1
0
void testEncrypt(Slice key, Slice iv, Slice entity, Slice input, Slice cipher, int chunk) {
  SCOPED_TRACE(testing::Message() << "Encrypt(chunk = " << chunk << ")");

  std::stringbuf target(std::ios_base::out);
  std::ostream targetStream(&target);

  Encrypt encrypt(CryptoConfig::CONFIG_256(), key, iv, entity);
  EncryptStream stream(&targetStream, std::move(encrypt), 10);
  char* source = reinterpret_cast<char*>(&input[0]);
  int remaining = input.length();
  while (remaining > 0) {
    chunk = std::min(chunk, remaining);
    stream.write(source, chunk);
    source += chunk;
    remaining -= chunk;
  }
  stream.flush();
  std::string resultBeforeEnd = target.str();
  stream.end();
  std::string result = target.str();

  EXPECT_EQ(cipher.length() + 14, resultBeforeEnd.size());
  EXPECT_EQ(cipher.length() + 14 + 16, result.size());
  // check format bytes
  EXPECT_EQ((uint8_t) 1, result[0]);
  EXPECT_EQ((uint8_t) 2, result[1]);
  // check IV
  for (int i=0; i<iv.length(); i++) {
    EXPECT_EQ(iv[i], (uint8_t) result[i+2]);
  }
  // check cipher
  for (int i=0; i<cipher.length(); i++) {
    EXPECT_EQ(cipher[i], (uint8_t) result[i+14]);
  }
}
Ejemplo n.º 2
0
int BingoApp::generateCard(QTextStream& sourceSvgCard, size_t cardNumber)
{
	const QString& targetFilename = generateTargetCardFilename(cardNumber);
	QFile targetFile(targetFilename);
	if ( ! targetFile.open(QIODevice::Text | QIODevice::WriteOnly) )
		{ cerr << "bingo: could not create " << targetFilename << endl; return 22; }

	QTextStream targetStream(&targetFile);
	Card card(cardNumber, symbols);
	return card.fillSvg(sourceSvgCard, targetStream);
}
Ejemplo n.º 3
0
void StratumClient::updateJob(const QVariantMap& _newJobMap) {
  QString jobId = _newJobMap.value(STRATUM_JOB_PARAM_NAME_JOB_ID).toString();
  if (jobId.isEmpty()) {
    qDebug() << "Job didn't changed";
  } else {
    QWriteLocker lock(&m_jobLock);
    QByteArray blob = QByteArray::fromHex(_newJobMap.value(STRATUM_JOB_PARAM_NAME_JOB_BLOB).toByteArray());
    QByteArray targetArr = QByteArray::fromHex(_newJobMap.value(STRATUM_JOB_PARAM_NAME_JOB_TARGET).toByteArray());
    quint32 target;
    QDataStream targetStream(targetArr);
    targetStream.setByteOrder(QDataStream::LittleEndian);
    targetStream >> target;
    m_currentJob.jobId = jobId;
    m_currentJob.blob = QByteArray::fromHex(_newJobMap.value(STRATUM_JOB_PARAM_NAME_JOB_BLOB).toByteArray());;
    m_currentJob.target = target;
    m_nonce = 0;
  }
}
void DataInterface::writeEdgeList(
		   const QString sourceSelection, const QString targetSelection, const bool directedRelationships,
		   const QString relationsType, const std::string filename, const std::string sepOne, const std::string sepTwo
		   ) {
  // We point to the InputTable and we get all the stuff out that we need from the input table and the function arguments.
  std::string sourceString = sourceSelection.toStdString();
  std::string targetString = targetSelection.toStdString();
  std::string relationshipType = "Undirected";
  if (directedRelationships) {
    relationshipType = "Directed";
  }
  std::string relationshipLabel = relationsType.toStdString();
  std::istringstream convertSepOne(sepOne.c_str());
  char sepOneChar;
  convertSepOne >> sepOneChar;
  std::istringstream convertSepTwo(sepTwo.c_str());
  char sepTwoChar;
  convertSepTwo >> sepTwoChar;
    
  // We need to know in which part of the data vector the source and target node reside.
  // We create and set an index to achieve that.
  int sourceIndex = 0;
  int targetIndex = 0;
  for (std::vector<std::string>::size_type i = 0; i != header.size(); i++) {
    std::string currentString = header[i];
    if (currentString == sourceString) {
      sourceIndex = i;
    }
    if (currentString == targetString) {
      targetIndex = i;
    }
  }

  // Then we prepare the file for writing.
  std::ofstream fileOut(filename.c_str());
  if (!fileOut.is_open()) {
    QPointer<QMessageBox> errorBox = new QMessageBox;
    errorBox->setText(tr("<b>ERROR: Could not write file</b>"));
    errorBox->setInformativeText("The program had problems trying to open the file in which to write data");
    errorBox->exec();
    return;
  }
  
  // First we write the header of the file.
  if (relationshipLabel.length() > 0) {
    fileOut << "Source" << sepOne << "Target" << sepOne << "Weight" << sepOne << "Type" << sepOne << "Type_User_Defined";
  } else {
    fileOut << "Source" << sepOne << "Target" << sepOne << "Weight" << sepOne << "Type";
  }
  fileOut << '\n';

  // We make a vector to store our lines for the output file.
  std::vector <std::string> fileVector;
  
  // Then we iterate through the data vector, find the appropriate entries, and write them to our file vector.
  std::vector <std::vector <std::string> >::iterator itData;
  for (itData = rowData.begin(); itData != rowData.end(); itData++) {
    std::vector <std::string> currentData = *itData;
    std::string currentSource = currentData[sourceIndex];
    std::string currentTarget = currentData[targetIndex];
    std::vector <std::string> sepSources;
    std::vector <std::string> sepTargets;

    // Some of the entries may have multiple values in one string. We need to separate these now.
    std::istringstream sourceStream(currentSource);
    while (sourceStream) {
      std::string s;
      if (!getline(sourceStream, s, sepTwoChar)) break;
      while (s[0] == ' ') {
	s = s.substr(1, s.length());
      }
      sepSources.push_back(s);
    }
    std::istringstream targetStream(currentTarget);
    while (targetStream) {
      std::string s;
      if (!getline(targetStream, s, sepTwoChar)) break;
      while (s[0] == ' ') {
	s = s.substr(1, s.length());
      }
      sepTargets.push_back(s);
    }
    std::vector <std::string>::iterator itSepSources;
    std::vector <std::string>::iterator itSepTargets;

    // After making sures that the values have been separated where necessary, we can write the bulk of our file.
    for (itSepSources = sepSources.begin(); itSepSources != sepSources.end(); itSepSources++) {
      std::string currentSepSource = *itSepSources;
      for (itSepTargets = sepTargets.begin(); itSepTargets != sepTargets.end(); itSepTargets++) {
	std::string currentSepTarget = *itSepTargets;
	if (currentSepSource != currentSepTarget) {
	  std::vector <std::string> tempVector;
	  tempVector.push_back(currentSepSource);
	  tempVector.push_back(currentSepTarget);
	  if (sourceSelection == targetSelection) {
	    std::sort(tempVector.begin(), tempVector.end());
	  }
	  std::string sourceTarget = tempVector[0] + sepOne + tempVector[1];
	  if (relationshipLabel.size() > 0) {
	    fileVector.push_back(sourceTarget + sepOne + "1" + sepOne + relationshipType + sepOne +  relationshipLabel + '\n');
	  } else {
	    fileVector.push_back(sourceTarget + sepOne + "1" + sepOne + relationshipType + '\n');
	  }
	}
      }
    }
  }
  if (fileVector.empty()) {
    QPointer<QMessageBox> errorBox =  new QMessageBox;
    errorBox->setText("<b>WARNING</b>");
    errorBox->setInformativeText("The Edge list is empty. Check your settings.");
    errorBox->exec();
  }
  // We remove all doubles from the edge list here.
  std::sort(fileVector.begin(), fileVector.end());
  fileVector.erase(std::unique(fileVector.begin(), fileVector.end()), fileVector.end());

  // Then we iterate through our file vector and write everything to our file.
  std::vector <std::string>::iterator lineReader;
  for (lineReader = fileVector.begin(); lineReader != fileVector.end(); lineReader++) {
    fileOut << *lineReader;
  }
  
  // And after that we can close the file and end the function.
  fileOut.close();
  return;
}
Ejemplo n.º 5
0
int ExtractGHKM::Main(int argc, char *argv[])
{
  // Process command-line options.
  Options options;
  ProcessOptions(argc, argv, options);

  // Open input files.
  InputFileStream targetStream(options.targetFile);
  InputFileStream sourceStream(options.sourceFile);
  InputFileStream alignmentStream(options.alignmentFile);

  // Open output files.
  OutputFileStream fwdExtractStream;
  OutputFileStream invExtractStream;
  std::ofstream glueGrammarStream;
  std::ofstream unknownWordStream;
  std::string fwdFileName = options.extractFile;
  std::string invFileName = options.extractFile + std::string(".inv");
  if (options.gzOutput) {
    fwdFileName += ".gz";
    invFileName += ".gz";
  }
  OpenOutputFileOrDie(fwdFileName, fwdExtractStream);
  OpenOutputFileOrDie(invFileName, invExtractStream);
  if (!options.glueGrammarFile.empty()) {
    OpenOutputFileOrDie(options.glueGrammarFile, glueGrammarStream);
  }
  if (!options.unknownWordFile.empty()) {
    OpenOutputFileOrDie(options.unknownWordFile, unknownWordStream);
  }

  // Target label sets for producing glue grammar.
  std::set<std::string> labelSet;
  std::map<std::string, int> topLabelSet;

  // Word count statistics for producing unknown word labels.
  std::map<std::string, int> wordCount;
  std::map<std::string, std::string> wordLabel;

  std::string targetLine;
  std::string sourceLine;
  std::string alignmentLine;
  XmlTreeParser xmlTreeParser(labelSet, topLabelSet);
  ScfgRuleWriter writer(fwdExtractStream, invExtractStream, options);
  size_t lineNum = options.sentenceOffset;
  while (true) {
    std::getline(targetStream, targetLine);
    std::getline(sourceStream, sourceLine);
    std::getline(alignmentStream, alignmentLine);

    if (targetStream.eof() && sourceStream.eof() && alignmentStream.eof()) {
      break;
    }

    if (targetStream.eof() || sourceStream.eof() || alignmentStream.eof()) {
      Error("Files must contain same number of lines");
    }

    ++lineNum;

    // Parse target tree.
    if (targetLine.size() == 0) {
      std::cerr << "skipping line " << lineNum << " with empty target tree\n";
      continue;
    }
    std::auto_ptr<ParseTree> t;
    try {
      t = xmlTreeParser.Parse(targetLine);
      assert(t.get());
    } catch (const Exception &e) {
      std::ostringstream s;
      s << "Failed to parse XML tree at line " << lineNum;
      if (!e.GetMsg().empty()) {
        s << ": " << e.GetMsg();
      }
      Error(s.str());
    }

    // Read source tokens.
    std::vector<std::string> sourceTokens(ReadTokens(sourceLine));

    // Read word alignments.
    Alignment alignment;
    try {
      alignment = ReadAlignment(alignmentLine);
    } catch (const Exception &e) {
      std::ostringstream s;
      s << "Failed to read alignment at line " << lineNum << ": ";
      s << e.GetMsg();
      Error(s.str());
    }
    if (alignment.size() == 0) {
      std::cerr << "skipping line " << lineNum << " without alignment points\n";
      continue;
    }

    // Record word counts.
    if (!options.unknownWordFile.empty()) {
      CollectWordLabelCounts(*t, options, wordCount, wordLabel);
    }

    // Form an alignment graph from the target tree, source words, and
    // alignment.
    AlignmentGraph graph(t.get(), sourceTokens, alignment);

    // Extract minimal rules, adding each rule to its root node's rule set.
    graph.ExtractMinimalRules(options);

    // Extract composed rules.
    if (!options.minimal) {
      graph.ExtractComposedRules(options);
    }

    // Write the rules, subject to scope pruning.
    const std::vector<Node *> &targetNodes = graph.GetTargetNodes();
    for (std::vector<Node *>::const_iterator p = targetNodes.begin();
         p != targetNodes.end(); ++p) {
      const std::vector<const Subgraph *> &rules = (*p)->GetRules();
      for (std::vector<const Subgraph *>::const_iterator q = rules.begin();
           q != rules.end(); ++q) {
        ScfgRule r(**q);
        // TODO Can scope pruning be done earlier?
        if (r.Scope() <= options.maxScope) {
          writer.Write(r);
        }
      }
    }
  }

  if (!options.glueGrammarFile.empty()) {
    WriteGlueGrammar(labelSet, topLabelSet, glueGrammarStream);
  }

  if (!options.unknownWordFile.empty()) {
    WriteUnknownWordLabel(wordCount, wordLabel, options, unknownWordStream);
  }

  return 0;
}