<?php
/**
    riff-ripper.php
    Version 1
    File-ripper that rips multiple files out of an uncompressed archive-like file.
    It makes the following assumptions:
        - The files modified in any way, including any form of compression
        - The end delimiter is NOT included in the files
    This version also does not have any real comments or documentation.
 */

/*
    SETTINGS
*/

define("BUFFER_SIZE"2048); // Buffer size. Note the buffer will actually be twice this.
define("START_DELIM""RIFF");
define("END_DELIM""RIFF");
define("INCLUDE_END_DELIM_IN_OUTPUT"false); // not yet used
define("PATH_PREFIX""sounds/");
define("FILE_EXT"".wav");

/*
    END OF SETTINGS
*/

function main()
{
    
$inputfile fopen("sound.dat""r");
    
    if (
$inputfile)
    {
        
$buffer fread($inputfileBUFFER_SIZE);
        
$savedBuffer "";
        
$haveRIFF false;
        
$filesFound 0;
        
        while(!
feof($inputfile))
        {
            
// Add to the buffer
            
$buffer .= fread($inputfileBUFFER_SIZE);
            
            
// Find a RIFF
            
if ((($pos strpos($bufferEND_DELIM)) !== false) &&
                (
$haveRIFF == true))
            {
                
// Copy buffer to where file ends
                
$savedBuffer .= cutBuffer($buffer$pos);
                
// Save the data to a file
                
saveToFile(PATH_PREFIX $filesFound FILE_EXT,
                    
$savedBuffer);
                
                echo 
"\rSaved file #$filesFound.";
                
                
// Increment file counter
                
$filesFound++;
                
                
// Reset variables
                
$savedBuffer "";
                
$haveRIFF false;
            }
            else if ((
$pos strpos($bufferSTART_DELIM)) !== false)
            {
                
// Copy buffer where file begins
                
cutBuffer($buffer$pos);
                
$savedBuffer $buffer;
                
$buffer "";
                
// Mark that we have a RIFF.
                
$haveRIFF true;
            }
            else if (
$haveRIFF)
            {
                
// Keep old buffer data
                
$savedBuffer .= cutBuffer($bufferBUFFER_SIZE);
            }
            else
            {
                
// Drop old buffer data
                
cutBuffer($bufferBUFFER_SIZE);
            }
        }
        
        
// Reached end of file. If we still have a RIFF pending, save it.
        
if ($haveRIFF)
        {
            
saveToFile(PATH_PREFIX $filesFound FILE_EXT,
                        
$savedBuffer);
        }
        
// Close file
        
fclose($inputfile);
        
        echo 
"\n" $filesFound " files ripped.";
    }
    else
        echo 
"Could not open input file.\n";
}

function 
cutBuffer(&$buffer$pos$reverse false)
{
    
$cut "";
    if (
$pos != 0)
    {
        
$cut substr($buffer0$pos);
        if (
$pos == strlen($buffer))
            
$buffer "";
        else
            
$buffer substr($buffer$pos);
    }
    return 
$cut;
}

function 
saveToFile($pathAndFile$data)
{
    
$outputFile fopen($pathAndFile"xb");
    if (
$outputFile)
    {
        
fwrite($outputFile$data);
    }
    else
        die(
"\nCould not save $pathAndFile, terminating.\n");
}

main();
?>