Exemple #1
0
function getDiff($X, $Y)
{
    preg_match_all("/(<.*?>\\s*|\\s+)([^\\s<]*)/", " " . $X, $Xmatch);
    preg_match_all("/(<.*?>\\s*|\\s+)([^\\s<]*)/", " " . $Y, $Ymatch);
    //Determine common beginning
    $sHTMLStart = "";
    while (count($Xmatch[0]) && count($Ymatch[0]) && trim($Xmatch[2][0]) == trim($Ymatch[2][0])) {
        $sHTMLStart .= $Xmatch[0][0];
        array_shift($Xmatch[0]);
        array_shift($Xmatch[1]);
        array_shift($Xmatch[2]);
        array_shift($Ymatch[0]);
        array_shift($Ymatch[1]);
        array_shift($Ymatch[2]);
    }
    //Find common ending
    $X_end = count($Xmatch[0]) - 1;
    $Y_end = count($Ymatch[0]) - 1;
    $sHTMLEnd = "";
    while ($X_end >= 0 && $Y_end >= 0 && trim($Xmatch[2][$X_end]) == trim($Ymatch[2][$Y_end])) {
        $sHTMLEnd = $Xmatch[0][$X_end] . $sHTMLEnd;
        unset($Xmatch[0][$X_end]);
        unset($Xmatch[1][$X_end]);
        unset($Xmatch[2][$X_end]);
        unset($Ymatch[0][$Y_end]);
        unset($Ymatch[1][$Y_end]);
        unset($Ymatch[2][$Y_end]);
        $X_end--;
        $Y_end--;
    }
    //What will actually diff
    $Xmatch_trimmed = array();
    foreach ($Xmatch[2] as $i => $match) {
        $Xmatch_trimmed[] = trim($match);
    }
    $Ymatch_trimmed = array();
    foreach ($Ymatch[2] as $i => $match) {
        $Ymatch_trimmed[] = trim($match);
    }
    ob_start();
    printDiff(LongestCommonSubsequence($Xmatch_trimmed, $Ymatch_trimmed), $Xmatch, $Ymatch, $Xmatch_trimmed, $Ymatch_trimmed, count($Xmatch_trimmed) - 1, count($Ymatch_trimmed) - 1);
    $sHTML = ob_get_contents();
    ob_end_clean();
    $sHTML = preg_replace('#</b >(\\s*)<b style="color:green">#', '\\1', $sHTML);
    $sHTML = preg_replace('#<b style="color:green">(\\s*)</b >#', '\\1', $sHTML);
    $sHTML = preg_replace('#</s >(\\s*)<s style="color:red">#', '\\1', $sHTML);
    $sHTML = preg_replace('#<s style="color:red">(\\s*)</s >#', '\\1', $sHTML);
    return $sHTMLStart . $sHTML . $sHTMLEnd;
}
Exemple #2
0
    }
    printf("\n");
    printf("diff:\n");
    for ($i = 0; $i < count($a); $i++) {
        if ($a[$i] ^ $b[$i]) {
            printf("%02x,", $a[$i] ^ $b[$i]);
        } else {
            printf("  ,");
        }
    }
    printf("\n\n");
}
$key = FieldElement::fromHex($testVectors[0]);
$input = FieldElement::fromHex($testVectors[1]);
$nonce = FieldElement::fromHex($testVectors[2]);
$ad = FieldElement::fromHex($testVectors[3]);
$expected = FieldElement::fromHex($testVectors[4]);
$ciphertext = Salt::encrypt($input, $ad, $nonce, $key);
if (!Salt::equal($expected, $ciphertext)) {
    echo "encryption error:\n";
    printDiff($expected, $ciphertext);
} else {
    echo "encryption OK\n";
}
$plaintext = Salt::decrypt($ciphertext, $ad, $nonce, $key);
if (!Salt::equal($input, $plaintext)) {
    echo "decryption error:\n";
    printDiff($input, $plaintext);
} else {
    echo "decryption OK\n";
}
Exemple #3
0
    for ($i = 0; $i < count($b); $i++) {
        printf("%02x,", $b[$i]);
    }
    printf("\n");
    printf("diff:\n");
    for ($i = 0; $i < count($a); $i++) {
        if ($a[$i] ^ $b[$i]) {
            printf("%02x,", $a[$i] ^ $b[$i]);
        } else {
            printf("  ,");
        }
    }
    printf("\n\n");
}
for ($i = 0; $i < count($testVectors); $i++) {
    $key = FieldElement::fromHex($testVectors[$i][0]);
    $nonce = FieldElement::fromHex($testVectors[$i][1]);
    $expected = FieldElement::fromHex($testVectors[$i][2]);
    $len = count($expected);
    $out = new SplFixedArray($len);
    $ctx = new Chacha20();
    $ctx->keysetup($key);
    $ctx->ivsetup($nonce);
    $ctx->keystream($out, $len);
    if (!Salt::equal($expected, $out)) {
        echo "error: " . $i . "\n";
        printDiff($expected, $out);
    } else {
        echo $i . " OK\n";
    }
}
Exemple #4
0
/**
 * @brief Obtain ISSN for a journal
 *
 * @param journal Journal name
 * @param threshold Threshold for matching name (default = 0.75)
 *
 * If exact match not found we use approximate string matching to find the best match. The
 * journal name is stripped of short words ("of", "the") and punctuation, then a MySQL LIKE
 * query finds a candidate list. From this list we take title with the best Dice score.
 * 
 * @return ISSN, if it exists, otherwise an empty string
 *
 */
function issn_from_journal_title($journal, $threshold = 0.75)
{
    global $db;
    global $left;
    global $right;
    global $debug;
    $issn = '';
    $journal = trim($journal);
    // First try and exact match
    $sql = 'SELECT * FROM issn WHERE (title = ' . $db->Quote($journal) . ')';
    $result = $db->Execute($sql);
    if ($result == false) {
        die("failed: " . $sql);
    }
    if ($result->NumRows() == 1) {
        $issn = $result->fields['issn'];
    } else {
        // No exact match, try an approximate match
        // Clean up
        $query = $journal;
        // short pronouns are likely to cause problems as people may get them wrong (ie., "of" and "for")
        $query = str_replace(' of ', ' ', $query);
        $query = str_replace(' for ', ' ', $query);
        $query = preg_replace('/^The /', '', $query);
        $query = str_replace('&', 'and', $query);
        $query = str_replace(',', '', $query);
        $query = str_replace(':', '', $query);
        $query = str_replace('\'', '', $query);
        $query = str_replace('.', '', $query);
        $query = str_replace(' ', '% ', $query);
        $query = '%' . $query;
        $query .= '%';
        $sql = 'SELECT * FROM issn WHERE (title LIKE ' . $db->Quote($query) . ')';
        //echo $sql;
        $result = $db->Execute($sql);
        if ($result == false) {
            die("failed: " . $sql);
        }
        // Build results list
        $hits = array();
        while (!$result->EOF) {
            $left = $right = '';
            $qStr = $journal;
            $qStr = str_replace('.', '', $qStr);
            $hStr = $result->fields['title'];
            $hStr = str_replace('.', '', $hStr);
            $C = LCSLength($qStr, $hStr);
            printDiff($C, $qStr, $hStr, strlen($qStr), strlen($hStr));
            $score = $C[strlen($qStr)][strlen($hStr)];
            $score = 1.0 - (double) (strlen($qStr) + strlen($hStr) - 2 * $score) / (double) (strlen($qStr) + strlen($hStr));
            //$score *= 100;
            $hit = array('hit' => $result->fields['title'], 'hitDisplay' => $right, 'score' => $score, 'issn' => $result->fields['issn']);
            array_push($hits, $hit);
            $result->MoveNext();
        }
        // sort
        $scores = array();
        foreach ($hits as $key => $row) {
            $scores[$key] = $row['score'];
        }
        array_multisort($scores, SORT_NUMERIC, SORT_DESC, $hits);
        if ($debug) {
            echo '<table border="1" cellpadding="2">';
            echo '<tr style="font-family:Arial;font-size:12px;"><th>Journal</th><th>Score</th><th>ISSN</th></tr>';
            foreach ($hits as $hit) {
                echo '<tr style="font-family:Arial;font-size:12px;">';
                echo '<td>';
                echo "<span style=\"background:white;color:black;\">", $hit['hitDisplay'], "</span>";
                echo '</td>';
                echo '<td>';
                echo $hit['score'];
                echo '</td>';
                echo '<td>';
                echo '<a href="http://journalseek.net/cgi-bin/journalseek/journalsearch.cgi?field=issn&query=' . $hit['issn'] . '" target="_blank">' . $hit['issn'] . '</a>';
                echo '</td>';
                echo '</tr>';
            }
            echo '</table>';
        }
        if (count($hits) > 0) {
            // Do we have a hit (above some threshhold)
            if ($hits[0]['score'] >= $threshold) {
                $issn = $hits[0]['issn'];
            }
        }
    }
    return $issn;
}
Exemple #5
0
    printf("got : ");
    for ($i = 0; $i < 32; $i++) {
        printf("%02x,", $b[$i]);
    }
    printf("\n");
    printf("diff: ");
    for ($i = 0; $i < 32; $i++) {
        if ($a[$i] ^ $b[$i]) {
            printf("%02x,", $a[$i] ^ $b[$i]);
        } else {
            printf("  ,");
        }
    }
    printf("\n\n");
}
$time = -microtime(true);
$alice = Salt::box_keypair();
$alice_sk = $alice[0];
$alice_pk = $alice[1];
$bob = Salt::box_keypair();
$bob_sk = $alice[0];
$bob_pk = $alice[1];
$alice_shared = Salt::scalarmult($alice_sk, $bob_pk);
$bob_shared = Salt::scalarmult($bob_sk, $alice_pk);
if (!Salt::equal($alice_shared, $bob_shared)) {
    printDiff($alice_shared, $bob_shared);
}
$time += microtime(true);
printf("done\n");
printf("microtime: %f\n", $time);
printf("memory peak: %s\n", memory_get_peak_usage(true));
        }
    }
}
$maxkey = $maxval1 = $maxval2 = 0;
countMaxLen($diff, $maxkey, $maxval1, $maxval2);
function printDiff($diff, $maxkey, $maxval1, $maxval2, $file1, $file2)
{
    printf("FILE1 = %s\n", $file1);
    printf("FILE2 = %s\n", $file2);
    printf("| %-'-{$maxkey}s | %-'-{$maxval1}s | %-'-{$maxval2}s |\n", "----", "----", "----");
    printf("| %-{$maxkey}s | %-{$maxval1}s | %-{$maxval2}s |\n", "[KEY]", "[FILE1]", "[FILE2]");
    printf("| %-'-{$maxkey}s | %-'-{$maxval1}s | %-'-{$maxval2}s |\n", "----", "----", "----");
    foreach ($diff as $key => $value) {
        $showDiff = false;
        if (!isset($value[1])) {
            $value[1] = "<NULL>";
            $showDiff = true;
        } elseif (!isset($value[2])) {
            $value[2] = "<NULL>";
            $showDiff = true;
        } elseif ($value[1] !== $value[2]) {
            $showDiff = true;
        }
        if ($showDiff) {
            printf("| %-{$maxkey}s | %-{$maxval1}s | %-{$maxval2}s |\n", $key, $value[1], $value[2]);
        }
    }
    printf("| %-'-{$maxkey}s | %-'-{$maxval1}s | %-'-{$maxval2}s |\n", "----", "----", "----");
}
printDiff($diff, $maxkey, $maxval1, $maxval2, $file1, $file2);