yDec.php
/* Module: yDec
Version: 0.9
Date: 25.Sep.2002
Author: Rasmus Schultz (mindplay@mindplay.dk)
Usage: See included example (test.php)
License: GPL - See http://www.gnu.org/copyleft/gpl.html
PLEASE NOTE:
CRC32 checks aren't implemented - I don't know how to do this.
The crc32() function in PHP does not seem useful in this case,
as it only allows you to calculate everything in one go - we
don't want to do that here, since doing so would make memory
usage unpredictable. If you figure out how to add the CRC32
calculation, please send me the code: mindplay@mindplay.dk
The ydecode function returns one of the following values:
0 = Decode successful
1 = Unable to open file
2 = Unable to locate start of yencoded data
3 = Invalid yenc header
4 = Invalid multipart header
5 = Unable to write file
6 = Encoded data is broken or incomplete (size mismatch)
*/
function getyencparam($line, $name) {
$value = NULL;
if ($pos = strpos($line, ' '.$name.'=')) {
$pos += 2+strlen($name);
if ($endpos = strpos($line, ' ', $pos+1)) {
$value = substr($line, $pos, $endpos-$pos);
}
}
return($value);
}
function ydecode($filename, $folder) {
$bufferSize = 4096;
$buffer = str_repeat(chr(0), $bufferSize);
$bufferLength = 0;
$written = 0;
if (!($fileIn = fopen($filename, 'rb'))) {
return(1);
}
while ($line = fgets($fileIn, 4096)) {
$line = rtrim($line, "\n\r");
if (strncmp('=ybegin', $line, 7)==0) {
break;
}
}
if (feof($fileIn)) {
return(2);
}
if (!($pos = strpos($line, 'name='))) {
return(3);
}
$fileName = trim(substr($line, $pos+5));
if ($partNo = getyencparam($line, 'part')) {
$ok = FALSE;
while ($line = fgets($fileIn, 4096)) {
$line = rtrim($line, "\n\r");
if (strncmp('=ypart', $line, 6)==0) {
$ok = TRUE;
break;
}
}
if (!($ok)) {
return(4);
} else {
if (!($begin = getyencparam($line, 'begin'))) {
return(4);
} else {
if (file_exists($folder.$fileName)) {
$fs = filesize($folder.$fileName);
$fmode = 'r+b';
} else {
$fs = 0;
$fmode = 'wb';
}
if (!($fileOut = fopen($folder.$fileName, $fmode))) {
return(5);
}
if (!(fseek($fileOut, $begin-1, SEEK_SET)==0)) {
return(5);
}
}
}
} else {
if (!($fileOut = fopen($folder.$fileName, 'wb'))) {
return(5);
}
}
$char = 0;
$special = FALSE;
while ($line = fgets($fileIn, 4096)) {
$line = rtrim($line, "\n\r");
if (strncmp('=yend', $line, 5)==0) {
break;
}
if ($line != '') {
for ($lcv=0; $lcv < strlen($line); $lcv++) {
$char = ord($line{$lcv});
if ($char!=61) {
$buffer{$bufferLength++} = chr($special?$char-106:$char-42);
if ($bufferLength==$bufferSize) {
if (!fwrite($fileOut, $buffer, $bufferLength)) {
return(5);
}
// *** TO-DO: accumulate buffer to CRC32 sum
$written += $bufferLength;
$bufferLength = 0;
}
$special = FALSE;
} else {
$special = TRUE;
}
}
}
}
if ($bufferLength>0) {
if (!fwrite($fileOut, $buffer, $bufferLength)) {
return(5);
}
// *** TO-DO: accumulate buffer to CRC32 sum
$written += $bufferLength;
}
// *** TO-DO: finalize CRC32 sum
fclose($fileOut);
fclose($fileIn);
if ($line != '') {
if (strncmp('=yend', $line, 5)==0) {
if ($size = getyencparam($line, 'size')) {
if ($size != $written) {
return(6);
}
}
// *** TO-DO: compare finalized CRC32 sum to $pcrc32
$pcrc32 = getyencparam($line, 'pcrc32');
}
}
return(0);
}
?>
test.php
The following example should produce the file joystick.jpg
I'm decoding the two parts in reverse order, just to show that this works.
Done.
Aucun commentaire:
Enregistrer un commentaire