예제 #1
0
function read_netstring($handle, $hasher = FALSE)
{
    //reads a netstring as specified by http://cr.yp.to/proto/netstrings.txt
    //short version: "length:string,"
    $length = "";
    while (true) {
        $c = fgetc($handle);
        if ($c === FALSE) {
            throw new Exception("Expected digit or ':', got EOF");
        }
        if ($hasher) {
            hash_update($hasher, $c);
        }
        if (is_numeric($c)) {
            $length .= $c;
        } else {
            if (!is_whitespace($c)) {
                break;
            }
        }
    }
    if ($c != ":") {
        throw new Exception("Expected ':', got '{$c}'");
    }
    $length = (int) $length;
    $string = "";
    $read = 0;
    while (!feof($handle) && $read < $length) {
        $string .= fread($handle, $length - $read);
        //lets hope strlen is O(1)
        $read = strlen($string);
    }
    if ($hasher) {
        hash_update($hasher, $string);
    }
    if (strlen($string) != $length) {
        throw new Exception("String too short!");
    }
    if (feof($handle)) {
        throw new Exception("EOF before ','");
    }
    $c = fgetc($handle);
    if ($c != ',') {
        throw new Exception("Expected ',', got '{$c}'");
    }
    if ($hasher) {
        hash_update($hasher, $c);
    }
    return $string;
}
예제 #2
0
 function php_strip_whitespace($file)
 {
     if (!@is_readable($file)) {
         return '';
     }
     $contents = join('', @file($file));
     $out = '';
     $state = 0;
     for ($i = 0; $i < strlen($contents); $i++) {
         if (!$state && is_whitespace($contents[$i])) {
             continue;
         }
         if (!$state && ($c_close = is_commentopen($contents, $i))) {
             $c_open_len = $contents[$i] == '/' ? 2 : 1;
             $i = strpos($contents, $c_close, $i + $c_open_len) + strlen($c_close) - 1;
             continue;
         }
         $out .= $contents[$i];
         if (is_quote($contents[$i])) {
             if ($state == $contents[$i] && !is_escaped($contents, $i)) {
                 $state = 0;
                 continue;
             }
             if (!$state) {
                 $state = $contents[$i];
                 continue;
             }
         }
     }
     return $out;
 }
예제 #3
0
 function remove_last_whitespace()
 {
     if (count($this->content) == 0) {
         return;
     }
     $i = count($this->content) - 1;
     $last = $this->content[$i];
     while ($i >= 0 && is_whitespace($this->content[$i])) {
         $this->remove($this->content[$i]);
         $i--;
         if ($i >= 0) {
             $last = $this->content[$i];
         }
     }
     if ($i >= 0) {
         if (is_container($this->content[$i])) {
             $this->content[$i]->remove_last_whitespace();
         }
     }
 }