Jump to content

how to use the PACK function


neohunter

Recommended Posts

hi, im just learning php since one month ago. this is my first post on this forum! so i wanna say hi to everybody ^^..

now i hope you can help me... i need to generate a custom binary file... let me explain you fast...

this is the format of the file

[code]
TRIADPATCHERPATCHBIN -> main  -> this is the header
[00][00][00][00]    -> filecount -> this bytes is a count of the files inside the file
------------------------------- NOW FOR EVERY FILE -------------------
[0C][00][00][00]    -> filenamecount  -> the numbers of bytes in the filename
originalfilename    -> file_from            -> the name
[0C][00][00][00]    -> file_tonamecount  -> the numbers of bytes in the file_to
destinefilename      -> file_toname  -> the name of the file_to =P
[07][00][00][00]    -> contentcount  -> the lenght in bytes
Content              -> content -> the content of this file

and the last block repeat for every file inside the file...
[/code]

well, so i start making a function; make_tpp($files)

files is an array:

$files = array('file_from.grf' => 'file_to.grf');

now i need to use the funcion pack for generate the file... i tried witout the array.. i did something like this...

[code]
$file_from='file_from.grf';
$file_to='file_to.grf';
$content='content test';

$data= pack('a20VVa*Va*Va*',
'TRIADPATCHERPATCHBIN',
1,
strlen($file_from),
$file_from,
strlen($file_to),
$file_to,
$strlen($content),
$content);

#next i save it to a file...
if(($fp=fopen('test.hex',"wb"))){
fwrite($fp,$data);
fclose ($fp);
}
[/code]

And thats works perfectly! but i need to pass more that one file, so i need a way to pass multi parameters to the pack function...

[code]
foreach ($files as $file_from => $file_to){
#[0C][00][00][00]    -> filenamecount            ->  V
$hex_format .= 'V';
$hex[] = strlen($file_origen);

#originalfilename    -> file_origen              ->  a*
$hex_format .= 'a*';
$hex[] = $file_origen;

#[0C][00][00][00]    -> destfilenamecount        ->  V
$hex_format .= 'V';
$hex[] = strlen($file_dest);

#destinefilename      -> destinefilename          ->  a*
$hex_format .= 'a*';
$hex[] = $file_dest;

#[07][00][00][00]    -> contentcount            ->  V
$_content = file_get_contents($file_origen);
$hex_format .= 'V';
$hex[] = strlen($_content);

#content
$hex_format .= 'a*';
$hex[] = $_content;

}
pack($hex_format,???????????);
[/code]

do you understand me????!?!?!?!?!??!

Link to comment
https://forums.phpfreaks.com/topic/28580-how-to-use-the-pack-function/
Share on other sites

try to explain better...

the pack funcion is

pack(format, parm1, parm2, parm3... etcetc);

so, when the array containt one file i need to give to the pack funcion 8 parameters. but when the array  containt 2 files i will need to give 14 parameters to the pack function!

i just dont know how to do that...
OK I SOLVED IT! JUST CONCATENATING ( .= )

#the header--
$data= pack('a20V','TRIADPATCHERPATCHBIN',2); #name V

#and the foreach goes here...
$data.=pack('Va*Va*Va*',
strlen($file_from),
$file_from,
strlen($file_to),
$file_to,
strlen($content),
$content);


#SUCCES!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.