robinvdvleuten / php-nntp

Client for communicating with servers throught the Network News Transfer Protocol (NNTP) protocol.
MIT License
39 stars 12 forks source link

Add support for yenc encoded overviews. #10

Open robinvdvleuten opened 10 years ago

robinvdvleuten commented 10 years ago

Add support for yenc encoded overviews

kissifrot commented 8 years ago

The following (basic) code works:

$yEncodedString = 'ybegin**** (yencoded string ******)';

$encoded = array();

// Extract the yEnc string itself.
preg_match("/^(=ybegin.*=yend[^$]*)$/ims", $yEncodedString, $encoded);
if (!isset($encoded[1])) {
    return false;
}

$encoded = $encoded[1];
// Remove the header and trailer from the string before parsing it.
$encoded = preg_replace("/(^=ybegin.*\\r\\n)/im", "", $encoded, 1);
$encoded = preg_replace("/(^=ypart.*\\r\\n)/im", "", $encoded, 1);
$encoded = preg_replace("/(^=yend.*)/im", "", $encoded, 1);

// Remove linebreaks from the string.
$encoded = trim(str_replace("\r\n", "", $encoded));
// Decode
$decoded = '';
for( $i = 0; $i < strlen($encoded); $i++)
{
    if ($encoded{$i} == "=")
    {
        $i++;
        $decoded .= chr((ord($encoded{$i}) - 64) - 42);
    }
    else
    {
        $decoded .= chr(ord($encoded{$i}) - 42);
    }
}

// Do something with the $decoded
robinvdvleuten commented 8 years ago

@kissifrot can you create a PR for this functionality?