<?php

$ffname = $fflocation ."Sfc2Spd.sds";

$ffdbstring = file_get_contents($ffname);

$ffdb_format ="";

	$ffdb_format .= 'a4savever/';
    	$ffdb_format .= 'Vobjectnum/';
   	$ffdb_format .= 'Vlastobjectid/';
	$ffdb_format .= 'Vcampsize1/';
	$ffdb_format .= 'Vcampsize2/';
	$ffdb_format .= 'Vcamphdrsize/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'a'.$dbdecarray['camphdrsize'].'campheader/';
	$ffdb_format .= 'Vcampseperator/';
	$ffdb_format .= 'a6campobjheader/';
	$ffdb_format .= 'Vcampidentifier/';
	$ffdb_format .= 'Vcampunknown/';
	$ffdb_format .= 'Vcampnamelen/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'a'.$dbdecarray['campnamelen'].'campaignname/';
	$ffdb_format .= 'Vcampdesclen/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'a'.$dbdecarray['campdesclen'].'campdesc/';
	$ffdb_format .= 'Vcampearlymaplen/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'a'.$dbdecarray['campearlymaplen'].'campearlymap/';
	$ffdb_format .= 'Vcampmiddlemaplen/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);


	$ffdb_format .= 'a'.$dbdecarray['campmiddlemaplen'].'campmiddlemap/';
	$ffdb_format .= 'Vcamplatemaplen/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);


	$ffdb_format .= 'a'.$dbdecarray['camplatemaplen'].'camplatemap/';
	$ffdb_format .= 'Vcampnummissions/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);


	for ($i = 1; $i <= $dbdecarray['campnummissions']; $i++) {
		$ffdb_format .= 'Vcampmissionnamelen'.$i.'/';
		$dbdecarray = unpack ($ffdb_format,$ffdbstring);
		$ffdb_format .= 'a'.$dbdecarray['campmissionnamelen'.$i].'campmissionname'.$i.'/';
	}
	$ffdb_format .= 'Vcampnumraces/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'V'.$dbdecarray['campnumraces'].'camprace/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'Vcampstartera/';
	$ffdb_format .= 'Vcampdifficulty/';
	$ffdb_format .= 'Vcamptriggerprestige/';
	$ffdb_format .= 'Vcamptriggerlen/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'a'.$dbdecarray['camptriggerlen'].'camptriggermission/'; 
	$ffdb_format .= 'Vcampnamelen2/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'a'.$dbdecarray['campnamelen2'].'campaignname2/'; 
	$ffdb_format .= 'Vcampspcaptnamelen/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	if ($dbdecarray['campspcaptnamelen'] == 0) {
		$ffdb_format .= 'Ccampspcaptname/';
	} else {
		$ffdb_format .= 'a'.$dbdecarray['campspcaptnamelen'].'campspcaptname/'; 
	}
	$ffdb_format .= 'Vcampspcaptrace/';
	$ffdb_format .= 'Vcampspprestige/';
	$ffdb_format .= 'Vcampunknown5/';
	//$ffdb_format .= 'VcampunknownSP/';
	$ffdb_format .= 'Vcampturnssofar/';
	$ffdb_format .= 'Vcampunknown10/';
	$ffdb_format .= 'Vcampturnsperyear/';
	$ffdb_format .= 'Vcamplengthofturn/';
	$ffdb_format .= 'Vcampbaseyear/';
	$ffdb_format .= 'Vcampearlyerastart/';
	$ffdb_format .= 'Vcampmiderastart/';
	$ffdb_format .= 'Vcamplateerastart/';
	$ffdb_format .= 'Vcampadverastart/';
	$ffdb_format .= 'Vcampnumaccounts/';
	$ffdb_format .= 'Vcampmaxaccounts/';
	$ffdb_format .= 'Vcampnumplaying/';
	$ffdb_format .= 'Vcampmaxplaying/';
	$ffdb_format .= 'Vcampversionlen/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'a'.$dbdecarray['campversionlen'].'campversion/'; 
	$ffdb_format .= 'Vcampversionlen2/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'a'.$dbdecarray['campversionlen2'].'campversion2/'; 
	// omitted Dave's remaininglength stuff...
	$ffdb_format .= 'Vmapsize1/';
	$ffdb_format .= 'Vmapsize2/';
	$ffdb_format .= 'Vmaphdrlen/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'a'.$dbdecarray['maphdrlen'].'mapheader/';
	$ffdb_format .= 'Vmapseperator/';
	$ffdb_format .= 'a6mapobjheader/';
	$ffdb_format .= 'Vmapidentifier/';
	$ffdb_format .= 'Vmapunknown1/';
	$ffdb_format .= 'Vmapnumcolumns/';
	$ffdb_format .= 'Vmapnumrows/';
	$ffdb_format .= 'amapunknown2/';

	//long remaininglen;
	// the following is the product of numcolumns and numrows
	//long numhexes;

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

for ($i = 1; $i <= ($dbdecarray['mapnumcolumns'] * $dbdecarray['mapnumrows']); $i++) {

	//maphexobject
	$ffdb_format .= 'Vmaphex'.$i.'size1/';
	$ffdb_format .= 'Vmaphex'.$i.'size2/';
	$ffdb_format .= 'Vmaphex'.$i.'hdrlen/';

//$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	//$ffdb_format .= 'a'.$dbdecarray['maphex'.$i.'hdrlen'].'maphex'.$i.'header/';
	$ffdb_format .= 'a17maphex'.$i.'header/';

	$ffdb_format .= 'Vmaphex'.$i.'seperator/';
	$ffdb_format .= 'a6maphex'.$i.'objheader/';
	$ffdb_format .= 'Vmaphex'.$i.'identifier/';
	$ffdb_format .= 'a9maphex'.$i.'unknown/';
	$ffdb_format .= 'Vmaphex'.$i.'column/';
	$ffdb_format .= 'Vmaphex'.$i.'row/';
	$ffdb_format .= 'Vmaphex'.$i.'opmarkerA/';
	$ffdb_format .= 'Vmaphex'.$i.'opmarkerB/';
	$ffdb_format .= 'Vmaphex'.$i.'race/';
	$ffdb_format .= 'Vmaphex'.$i.'opmarkerC/';
	$ffdb_format .= 'Vmaphex'.$i.'cartel/';
	$ffdb_format .= 'Vmaphex'.$i.'terrain/';
	$ffdb_format .= 'Vmaphex'.$i.'planet/';
	$ffdb_format .= 'Vmaphex'.$i.'base/';
	$ffdb_format .= 'Vmaphex'.$i.'maxecon/';
	$ffdb_format .= 'Vmaphex'.$i.'currecon/';
	$ffdb_format .= 'Vmaphex'.$i.'opmarkerD /';
	$ffdb_format .= 'Vmaphex'.$i.'opmarkerE/';
	$ffdb_format .= 'Vmaphex'.$i.'maxdv/';
	$ffdb_format .= 'Vmaphex'.$i.'opmarkerF/';
	$ffdb_format .= 'Vmaphex'.$i.'cartelmaxdv/';
	$ffdb_format .= 'Vmaphex'.$i.'opmarkerG/';
	$ffdb_format .= 'Vmaphex'.$i.'opmarkerH/';
	$ffdb_format .= 'Vmaphex'.$i.'currdv/';
	$ffdb_format .= 'Vmaphex'.$i.'opmarkerI/';
	$ffdb_format .= 'Vmaphex'.$i.'cartelcurrdv/';
	$ffdb_format .= 'Vmaphex'.$i.'maximpa/';
	$ffdb_format .= 'Vmaphex'.$i.'maximpb/';
	$ffdb_format .= 'Vmaphex'.$i.'currimpa/';
	$ffdb_format .= 'Vmaphex'.$i.'currimpb/';
	//$ffdb_format .= 'amaphex'.$i.'unknown2/';

	//char *unknown2; // the balance of the object size, if any
	//long remaininglen;
}

$dbdecarray = unpack ($ffdb_format,$ffdbstring);


	//political tension matrix

	$ffdb_format .= 'Vpolobjsize1/';
	$ffdb_format .= 'Vpolobjsize2/';
	$ffdb_format .= 'Vpolhdrlength/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	$ffdb_format .= 'a'.$dbdecarray['polhdrlength'].'polheader/';

	$ffdb_format .= 'Vpolseperator/';
	$ffdb_format .= 'a6polobjheader/';
	$ffdb_format .= 'Vpolidentifier/';
	$ffdb_format .= 'Vpolunknown1/';
	$ffdb_format .= 'Vpolnumraces/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	for ($i = 1; $i <= $dbdecarray['polnumraces']*$dbdecarray['polnumraces']; $i++) {
		$ffdb_format .= 'vpolinittension'.$i.'/';
		$ffdb_format .= 'Cpolcurrreltension'.$i.'/';
	}

	$ffdb_format .= 'Cpolallyratio/';
	$ffdb_format .= 'Cpolneutralratio/';

$dbdecarray = unpack ($ffdb_format,$ffdbstring);

	// Character Table
	// need to setup loop based on header count

	//$NumCharacters = substr_count($ffdbstring, "AVtServCharacter"); //this works

	//echo 'NumCharacters = ', $NumCharacters. '
	//<br>';

	//$ffdb_format .= 'Vcharsize1/';
	//$ffdb_format .= 'Vcharsize2/';
	//$ffdb_format .= 'Vcharhdrsize/'
	//$ffdb_format .= 'a20charheader/';	
	
//end of db 
?>