php uses pack to process binary files

  • 2021-07-07 06:36:40
  • OfStack

php can read and write binary files using pack and unpack functions.
Today, I have to deal with the problem of a binary file, so I need to use 1. I specially understand the usage of pack under 1, and the usage of unpack is similar to this.

Simply put, the pack function is to give a target format, and the corresponding parameters, you can return binary data.

Here is an example to illustrate. For four integers:


pack("L4", 0,1,2,3)
pack("LLLL", 0,1,2,3)
pack("L", 0).pack("L", 1).pack("L", 2).pack("L", 3)

The above processing result is one, that is to say, format is the format describing the following data.
As for what format can be used, just look at format characters.
For example, a 30-character pack ("a30", "https://www.ofstack. com") means this, which is very simple

The official statement of the pack function is as follows:


 Quote 
pack
(PHP 3, PHP 4, PHP 5)

pack -- Pack data into binary string
Description
string pack ( string format [, mixed args [, mixed ...]] )

Pack given arguments into binary string according to format. Returns binary string containing data. 

The idea to this function was taken from Perl and all formatting codes work the same as there, however, there are some formatting codes that are missing such as Perl's "u" format code. The format string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or * for repeating to the end of the input data. For a, A, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string. Currently implemented are

 Forms  1. pack() format characters

Code Description 
a NUL-padded string 
A SPACE-padded string 
h Hex string, low nibble first 
H Hex string, high nibble first 
c signed char 
C unsigned char 
s signed short (always 16 bit, machine byte order) 
S unsigned short (always 16 bit, machine byte order) 
n unsigned short (always 16 bit, big endian byte order) 
v unsigned short (always 16 bit, little endian byte order) 
i signed integer (machine dependent size and byte order) 
I unsigned integer (machine dependent size and byte order) 
l signed long (always 32 bit, machine byte order) 
L unsigned long (always 32 bit, machine byte order) 
N unsigned long (always 32 bit, big endian byte order) 
V unsigned long (always 32 bit, little endian byte order) 
f float (machine dependent size and representation) 
d double (machine dependent size and representation) 
x NUL byte 
X Back up one byte 
@ NUL-fill to absolute position

Tired of reading English, let's take a look at the corresponding Chinese explanation:


 Quote 
pack() The data compression () function compresses data into 1 A 2 A binary string. 

a - NUL-padded string 
a - NUL-  String filling [padded string] 
A - SPACE-padded string 
A - SPACE-  String filling [padded string] 
h - Hex string, low nibble first 
h  In fact, in fact, the  106 Binary string, low " 4 Bit " [low nibble first] 
H - Hex string, high nibble first 
H - 106 Binary string, high " 4 Bit " [high nibble first] 
c - signed char 
c  In fact, in fact, the   Characters with symbols  
C - unsigned char 
C  In fact, in fact, the   Characters without symbols  
s - signed short (always 16 bit, machine byte order) 
s  In fact, in fact, the   Short pattern with symbols [short] (Usually 16 Bits, in machine byte order)  
S - unsigned short (always 16 bit, machine byte order) 
S  In fact, in fact, the   Short pattern without symbol [short] (Usually 16 Bits, sorted by machine bytes)  
n - unsigned short (always 16 bit, big endian byte order) 
n - Short pattern without symbol [short] (Usually 16 Bit, press big endian Byte sort)  
v - unsigned short (always 16 bit, little endian byte order) 
v - Short pattern without symbol [short] (Usually 16 Bit, press small endian Byte sort)  
i - signed integer (machine dependent size and byte order) 
i  In fact, in fact, the   Signed integer (determined by size and byte order)  
I - unsigned integer (machine dependent size and byte order) 
I  In fact, in fact, the   Unsigned integer (determined by size and byte order)  
l - signed long (always 32 bit, machine byte order) 
l In fact, in fact, the   Long patterns with symbols [long] (Usually 32 Bits, in machine byte order)  
L - unsigned long (always 32 bit, machine byte order) 
L  In fact, in fact, the   Long patterns without symbols [long] (Usually 32 Bits, in machine byte order)  
N - unsigned long (always 32 bit, big endian byte order) 
N  In fact, in fact, the   Long patterns without symbols [long] (Usually 32 Bit, press big edian Byte order)  
V - unsigned long (always 32 bit, little endian byte order) 
V In fact, in fact, the   Long patterns without symbols [long] (Usually 32 Bit, press small edian Byte order)  
f - float (machine dependent size and representation) 
f  Floating point (determined by size and byte order)  
d - double (machine dependent size and representation) 
d  In fact, in fact, the   Double precision (determined by size and byte order)  
x - NUL byte 
x  In fact, in fact, the   Null byte [NUL byte] 
X - Back up one byte 
X-  Back 1 Bytes [Back up one byte] 
@ - NUL-fill to absolute position 
@ - NUL-  Add to 1 Absolute position [absolute position]

The sample code is as follows:


<?php 
$code=array(
"username"=>array("A7"," Zhang 3adfb12"),
"pass"=>array("A10","asdf*#1"),
"age"=>array("C","23"),
"birthday"=>array("I","19900101"),
"email"=>array("A50","www.ofstack.com"));
$stream=join("\0",parkByArr($code));
echo $stream,strlen($stream);
file_put_contents("1.txt",$stream);// Save the stream for reading below 
function parkByArr($arr)
{
 $atArr=array();
 foreach ($arr as $k=>$v)
 {
 $atArr[]=pack($v[0],$v[1]);
 }
 return $atArr;
}
function getAscill($str)
{
 $arr=str_split($str);
 foreach ($arr as $v)
 {
 echo $v,"=",ord($v),"\n";
 }
}
$code=array(
"username"=>array("A20"),
"pass"=>array("A10"),
"age"=>array("C"),
"birthday"=>array("I"),
"email"=>array("A50"));
$stream=file_get_contents("1.txt");
var_dump(parkByArr($stream,$code));
function parkByArr($str,$code)
{
 $Arr=explode("\0",$str);
 $atArr=array();
 $i=0;
 foreach ($code as $k=>$v)
 {
 $atArr[$k]=unpack($v[0],$Arr[$i]);
 $i++;
 }
 return $atArr;
}

Related articles: