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
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...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.