Location: PHPKode > scripts > uuencode/uudecode > uuencode-uudecode.php
<?php

/* UUCoder by David Sklar <hide@address.com> 13 Jul 1999
   Converted to PHP from the C in the WWW common library (distributed
   with Lynx) which says:
** ACKNOWLEDGEMENT:
**
This code is taken from rpem distribution, and was originally
**
written by Mark Riordan.
**
** AUTHORS:
**
MR
Mark Riordan	hide@address.com
**
AL
Ari Luotonen	hide@address.com
**
** HISTORY:
**
Added as part of the WWW library and edited to conform
**
with the WWW project coding standards by:	AL  5 Aug 1993
**
Originally written by:				MR 12 Aug 1990
**
Original header text:
*/

class UUCoder {
    /* encoded -> printable mapping */
    var $six2pr= \'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\';
    var $pr2six;

    function UUCoder() {
	/* set up printable -> encoded mapping */
	for ($i = 0, $j = strlen($this->six2pr); $i < $j; $i++) {
	    $this->pr2six[$this->six2pr[$i]] = $i;
	}
    }

    function safe_substr($buf,$pos,$len) {
	if ($pos < $len) {
	    return ord($buf[$pos]);
	} else {
	    return 0;
	}
    }

    function decode($in) {
	/* Strip leading whitespace. */
	$in = ltrim($in);
	
	$out = \'\';  $i = 0; $j = strlen($in) - 3;
	while ($i < $j) {
	    $out .= chr($this->pr2six[$in[$i]] << 2 |
			$this->pr2six[$in[$i+1]] >> 4);
	    $out .= chr($this->pr2six[$in[$i+1]] << 4 |
			$this->pr2six[$in[$i+2]] >>2);
	    $out .= chr($this->pr2six[$in[$i+2]] << 6 |
			$this->pr2six[$in[$i+3]]);
	    $i += 4;
	}
	return $out;
    }

    function encode($in) {
	$out = \'\';
	
	for ($i=0, $j = strlen($in); $i < $j; $i += 3) {
	    $out .= $this->six2pr[$this->safe_substr($in,$i,$j)>>2];
	    $out .= $this->six2pr[(($this->safe_substr($in,$i,$j) << 4) &060)|
				  (($this->safe_substr($in,$i+1,$j) >>4)&017)];
	    $out .= $this->six2pr[(($this->safe_substr($in,$i+1,$j)<<2)&074) |
				  (($this->safe_substr($in,$i+2,$j) >> 6)&03)];
	    $out .= $this->six2pr[$this->safe_substr($in,$i+2,$j) & 077];
	}
	
	/* Adjust if the # of characters we should have encoded isn\'t
	 * a multiple of 3 */
	if ($i == $j+1) {
	    /* There were only 2 bytes in that last group */
	    $out[strlen($out)-1] = \'=\';
	} elseif ($i == $j+2) {
	    /* There was only 1 byte in that last group */
	    $k = strlen($out);
	    $out[$k-1] = $out[$k-2] = \'=\';
	}
	
	return $out;
    }
}
?>
Return current item: uuencode/uudecode