PHP implements functions similar to the Construct library in Python (1) Basic design ideas

王林
Release: 2023-02-23 10:06:02
forward
2096 people have browsed it

Introduction

There is a library Construct in python, which can be used to parse binary data. It is very convenient to use this tool to analyze network packets, formatted data files, etc.

If I had used this tool when analyzing the sqlite database file format a while ago, it would have saved a lot of trouble.

However, Construct2.9 has changed a lot from previous versions. The original python code written in Construct basically needs to be rewritten. In the process of viewing the source code of Construct, I found that the basic implementation idea of ​​​​Construct is the recursive descent analysis method. Use the object's construction method to dynamically define the data structure, and implement the parsing of binary data in the parse method.

I plan to use php to parse binary data, but the implementation idea is completely different from python's Construct.

Recommended PHP video tutorial: //m.sbmmt.com/course/list/29/type/2.html

Basic ideas

Since python's Construct uses python's object syntax to implement the definition and parsing of dynamic hierarchical structures, some structure definitions look obscure due to the limitation of python's object syntax.

My idea is to define a small language specifically used to describe dynamic hierarchical structures, so that it can take care of people's common expression habits as much as possible.

Take this small project as an example, based on the structure definition syntax in C language, and on this basis, conditionalization and loop structure definition are added.

There is a common structure in binary data. The first few bytes store the length of the subsequent data block, followed by the data block. It is not convenient to express this kind of structure with the structure definition of C language. The length of the entire data block varies and cannot be determined at compile time, but can only be determined during parsing. Therefore, it is necessary to extend the structure definition syntax of C language.

The first step is to implement the analysis of the C language structure

In this step, we do not consider the dynamic hierarchical structure definition, but implement this small The core part of the language, at least it must be able to understand the structure definition of C language, and be able to parse binary data based on this structure definition. After this step is completed, the definition and analysis of the dynamic structure will be implemented.

This project is based on the ADOS scripting language engine briefly introduced in the previous file.

Let’s take a look at our task first

Here is a structure definition file that is completely C language specification

blockStruct.h

struct student
{
  char name[2];
  int num;
  int age;
  char addr[3];
};

struct teacher
{
  char name[2];
  int num;  
  char addr[3];
};
Copy after login
Copy after login

Binary data block to be parsed

"\x41\x42\x01\x00\x00\x00\x02\x00\x00\x00\x41\x42\x43\x41\x42\x01\x00\x00\x00\x41\x42\x43"
Copy after login

Hope to get the parsing result

[value] => Array
        (
            [name] => AB
            [num] => 1
            [age] => 2
            [addr] => ABC
        )
[value] => Array
        (
            [name] => AB
            [num] => 1
            [addr] => ABC
        )
Copy after login

Interested friends can pay attention to the relationship between the three

The following It is a lexical rule file that only implements the compilation of C language structures

<?php
/*!
 * struckwkr的词法规则
 * 45022300@qq.com
 * Version 0.9.0
 *
 * Copyright 2019, Zhu Hui
 * Released under the MIT license
 */

$GLOBALS[&#39;structwkr_lexRules&#39;] =	[
		[&#39;/^\"(.*?)\"/&#39;,&#39;_cons&#39;	,&#39;"&#39;],
		[&#39;/^\(/&#39;,&#39;_lp&#39;			,&#39;(&#39;],
		[&#39;/^\)/&#39;,&#39;_rp&#39;			,&#39;)&#39;],
		[&#39;/^\[/&#39;,&#39;_lb&#39;			,&#39;[&#39;],
		[&#39;/^\]/&#39;,&#39;_rb&#39;			,&#39;]&#39;],
		[&#39;/^\{/&#39;,&#39;_lcb&#39;			,&#39;{&#39;],
		[&#39;/^\}/&#39;,&#39;_rcb&#39;			,&#39;}&#39;],
		[&#39;/^;/&#39;,&#39;_semi&#39;			,&#39;;&#39;],
		[&#39;/^,/&#39;,&#39;_comma&#39;		,&#39;,&#39;],
		[&#39;/^==/&#39;,&#39;_bieq&#39;		,&#39;=&#39;],
		[&#39;/^!=/&#39;,&#39;_uneq&#39;		,&#39;!&#39;],		
		[&#39;/^\>=/&#39;,&#39;_greq&#39;		,&#39;>&#39;],		
		[&#39;/^\<=/&#39;,&#39;_leeq&#39;		,&#39;<&#39;],		
		[&#39;/^=/&#39;,&#39;_equa&#39;			,&#39;=&#39;],		
		[&#39;/^\>/&#39;,&#39;_grea&#39;		,&#39;>&#39;],
		[&#39;/^\</&#39;,&#39;_less&#39;		,&#39;<&#39;],	
		[&#39;/^\+/&#39;,&#39;_add&#39;			,&#39;+&#39;],
		[&#39;/^-/&#39;,&#39;_sub&#39;			,&#39;-&#39;],
		[&#39;/^\*/&#39;,&#39;_mul&#39;			,&#39;*&#39;],
		[&#39;/^\//&#39;,&#39;_div&#39;			,&#39;/&#39;],	
		[&#39;/^%/&#39;,&#39;_mod&#39;			,&#39;$&#39;],
		[&#39;/^&&/&#39;,&#39;_and&#39;			,&#39;&&#39;],	
		[&#39;/^\|\|/&#39;,&#39;_or&#39;		,&#39;|&#39;],	
		[&#39;/^!/&#39;,&#39;_not&#39;			,&#39;!&#39;],	
		[&#39;/^struct/&#39;,&#39;_strukey&#39;	,&#39;s&#39;],
		[&#39;/^char/&#39;,&#39;_char&#39;		,&#39;c&#39;],
		[&#39;/^int/&#39;,&#39;_int&#39;		,&#39;i&#39;],
		[&#39;/^float/&#39;,&#39;_float&#39;	,&#39;f&#39;],
		[&#39;/^double/&#39;,&#39;_double&#39;	,&#39;d&#39;],
		[&#39;/^[0-9]+([.]{1}[0-9]+){0,1}/&#39;,&#39;_num&#39;,&#39;&#39;],
		[&#39;/^\./&#39;,&#39;_dot&#39;			,&#39;.&#39;],
		[&#39;/^[\x{4e00}-\x{9fa5}A-Za-z_][\x{4e00}-\x{9fa5}A-Za-z0-9_]*\b/u&#39;,&#39;_iden&#39;,&#39;&#39;],
		[&#39;/^\s*/&#39;,&#39;_null&#39;,&#39;&#39;]
		];
Copy after login

The following is a grammatical rule file that only implements the compilation of C language structures

<?php
/*!
 * structwkr的语法规则处理器
 * 45022300@qq.com
 * Version 0.9.0
 *
 * Copyright 2019, Zhu Hui
 * Released under the MIT license
 */

namespace Ados;

require_once &#39;const.php&#39;;
require_once __SCRIPTCORE__.&#39;syntax_rule/base_rules_handler.php&#39;;

class StructwkrRulesHandler extends BaseRulesHandler{

//语法分析的起始记号,归约到最后得到就是若干个结构体定义的列表
function startToken(){
	return &#39;_structList&#39;;
}

//附加类型
function extraType($extraArray){
	if(count($extraArray)>0){
		return $extraArray[0];
	}else{
		return &#39;&#39;;
	}
}

//求出放在附加信息中的数组长度
function elementSize($extraArray){
	if(count($extraArray)>0){
		return intval($extraArray[0]);
	}else{
		return 0;
	}
}

//处理源代码中的多条声明语句
function handleStatementList($stack,$coder,$listIndex,$statementIndex){
	
	$t1= $this->topItem($stack,$statementIndex);	

	if($listIndex>0){
		$t2= $this->topItem($stack,$listIndex);
		//将元素名所在项的附加信息数组找出来
		$extraArray=$t2[TokenExtraIndex];
	}else{ //$listIndex=0表示,statementList是上级节点,附加信息数组应该是空数组
		$extraArray=[];
	}
	//将statement中的附加信息添加到statementList的附加信息中去
	$extraArray[]=$t1[TokenExtraIndex];

	return [&#39;#&#39;,$extraArray];
}

//处理源代码中的一条声明语句
function handleStatement($stack,$coder,$typeItemIndex,$idenItemIndex){
	
	$t1= $this->topItem($stack,$typeItemIndex);
	$elementType = 	$t1[TokenValueIndex];

	$t2= $this->topItem($stack,$idenItemIndex);
	$elementName = 	$t2[TokenValueIndex];
	
	//将元素名所在项的附加信息数组找出来
	$extraArray=$t2[TokenExtraIndex];
	$valLen =$extraArray[0];
	//附加信息中包含 元素名称,元素类型,数据长度
	return [$elementName,[$elementName,$elementType,$valLen]];
}


//语法规则处理函数名由规则右边部分与规则左边部分拼接而成
//语法规则定义的先后决定了归约时匹配的顺序,要根据实际的语法安排
//如果不熟悉语法,随意调整语法规则的先后次序将有可能导致语法错误

// struct list  {{{

function _structList_0_structList_struct($stack,$coder){

	$coder->pushBlockTail();
	return [&#39;#&#39;,[]];
	
	
}

function _structList_0_struct($stack,$coder){	
	$coder->pushBlockTail();
	return [&#39;#&#39;,[]];
}

// struct list  }}}


// struct   {{{

function _struct_0_structName_blockStatement_semi($stack,$coder){

	$t1= $this->topItem($stack,3);
	$structName = 	$t1[TokenValueIndex];

	$t2= $this->topItem($stack,2);
	$extraArray=$t2[TokenExtraIndex];
	
	return [$structName,$extraArray];
}

// struct   }}}

// struct name     {{{

function _structName_0_strukey_iden($stack,$coder){  

	$t1= $this->topItem($stack,1);
	$structName = 	$t1[TokenValueIndex];

	$coder->pushBlockHeader($structName);	
	return $this->pass($stack,1);
}

// struct name     }}}

// blockStatement    {{{

function _blockStatement_0_lcb_statementList_rcb($stack,$coder){

	return $this->pass($stack,2);

}

// blockStatement    }}}

// statement list  {{{

function _statementList_0_statementList_statement($stack,$coder){
	
	return $this->handleStatementList($stack,$coder,2,1);
}

function _statementList_0_statement($stack,$coder){
	//此处0表示statementList是上一级节点,要做特殊处理
	return $this->handleStatementList($stack,$coder,0,1);	
}

// statement list  }}}


// statement       {{{

function _statement_0_double_term_semi($stack,$coder){	

	$t1= $this->topItem($stack,2);
	$elementName = 	$t1[TokenValueIndex];
	$parseFuncName = &#39;parseDouble&#39;;	
	$coder->pushBlockBody($parseFuncName,$elementName);	
	
	return $this->handleStatement($stack,$coder,3,2);
}

function _statement_0_float_term_semi($stack,$coder){	

	$t1= $this->topItem($stack,2);
	$elementName = 	$t1[TokenValueIndex];
	$parseFuncName = &#39;parseFloat&#39;;	
	$coder->pushBlockBody($parseFuncName,$elementName);	
	
	return $this->handleStatement($stack,$coder,3,2);
}


function _statement_0_char_term_semi($stack,$coder){

	$t1= $this->topItem($stack,2);
	$elementName = 	$t1[TokenValueIndex];
	$size = $this->elementSize($t1[TokenExtraIndex]);
	$parseFuncName = &#39;parseFixStr&#39;;	
	$coder->pushBlockBody($parseFuncName,$elementName,$size);		
	
	return $this->handleStatement($stack,$coder,3,2);
}

function _statement_0_int_term_semi($stack,$coder){	

	$t1= $this->topItem($stack,2);
	$elementName = 	$t1[TokenValueIndex];
	$parseFuncName = &#39;parseInt&#39;;	
	$coder->pushBlockBody($parseFuncName,$elementName,4);	
	
	return $this->handleStatement($stack,$coder,3,2);
}

// statement        }}}


//   term    {{{

function _term_0_term_array($stack,$coder){

	$t1= $this->topItem($stack,1);
	$valLen = 	$t1[TokenValueIndex];
	//将数据长度放入附加信息 
	$t2= $this->topItem($stack,2);
	return [$t2[TokenValueIndex],[$valLen]];
	
	
}

function _term_0_iden($stack,$coder){
	$t1= $this->topItem($stack,1);
	//未指定数据长度时将长度值设为0 
	$valLen = 	&#39;0&#39;;	
	$t2= $this->topItem($stack,2);
	return [$t1[TokenValueIndex],[$valLen]];	
}

//   term     }}}


// array 	  {{{

function _array_0_lb_num_rb($stack,$coder){

	return $this->pass($stack,2);	
}

// array 	   }}}


} // end of class
Copy after login

The following is the basic data Parsing functions for types, integers, and strings (note, used for experiments, not yet covering all basic data types)

<?php

//用于解析基本数据类型的内置函数

namespace Ados;

//解析一个字节,得到无符号整数值
function parseByte($context,$size = 0){
	$pos=$context[&#39;pos&#39;];
	$data = substr($context[&#39;data&#39;], $pos);
	if(strlen($data)>0){
		$raw = substr($data, 0,1);
		$value = unpack("C1",$raw)[1];
		return [&#39;value&#39;=>$value,&#39;size&#39;=>1,&#39;error&#39;=>0,&#39;msg&#39;=>&#39;ok&#39;];
	}else{
		return [&#39;value&#39;=>False,&#39;size&#39;=>0,&#39;error&#39;=>1,&#39;msg&#39;=>&#39;not enough for parseByte&#39;];
	}
	
}

//解析一个有符号整数
function parseInt($context,$size = 4){
	if(!($size == 2 or $size == 4 or $size == 8 )){
		return [&#39;value&#39;=>False,&#39;size&#39;=>0,&#39;error&#39;=>2,&#39;msg&#39;=>&#39;not a valid size&#39;];
	}
	if($size == 2) $format = "s1";
	if($size == 4) $format = "l1";
	if($size == 8) $format = "q1";

	$pos=$context[&#39;pos&#39;];
	$data = substr($context[&#39;data&#39;], $pos);
	if(strlen($data)>=$size){
		$raw = substr($data, 0,$size);
		$value = unpack($format,$raw)[1];
		return [&#39;value&#39;=>$value,&#39;size&#39;=>$size,&#39;error&#39;=>0,&#39;msg&#39;=>&#39;ok&#39;];
	}else{
		return [&#39;value&#39;=>False,&#39;size&#39;=>0,&#39;error&#39;=>1,&#39;msg&#39;=>&#39;no data for parseInt&#39;];
	}
	
}

//解析一个大端无符号整数
function parseBUInt($context,$size = 4){
	if(!($size == 2 or $size == 4 or $size == 8 )){
		return [&#39;value&#39;=>False,&#39;size&#39;=>0,&#39;error&#39;=>2,&#39;msg&#39;=>&#39;not a valid size&#39;];
	}
	if($size == 2) $format = "n1";
	if($size == 4) $format = "N1";
	if($size == 8) $format = "J1";

	$pos=$context[&#39;pos&#39;];
	$data = substr($context[&#39;data&#39;], $pos);
	if(strlen($data)>=$size){
		$raw = substr($data, 0,$size);
		$value = unpack($format,$raw)[1];
		return [&#39;value&#39;=>$value,&#39;size&#39;=>$size,&#39;error&#39;=>0,&#39;msg&#39;=>&#39;ok&#39;];
	}else{
		return [&#39;value&#39;=>False,&#39;size&#39;=>0,&#39;error&#39;=>1,&#39;msg&#39;=>&#39;no data for parseBUInt&#39;];
	}
	
}

//解析一个小端无符号整数
function parseLUInt($context,$size = 4){
	if(!($size == 2 or $size == 4 or $size == 8 )){
		return [&#39;value&#39;=>False,&#39;size&#39;=>0,&#39;error&#39;=>2,&#39;msg&#39;=>&#39;not a valid size&#39;];
	}
	if($size == 2) $format = "v1";
	if($size == 4) $format = "NL";
	if($size == 8) $format = "P1";

	$pos=$context[&#39;pos&#39;];
	$data = substr($context[&#39;data&#39;], $pos);
	if(strlen($data)>=$size){
		$raw = substr($data, 0,$size);
		$value = unpack($format,$raw)[1];
		return [&#39;value&#39;=>$value,&#39;size&#39;=>$size,&#39;error&#39;=>0,&#39;msg&#39;=>&#39;ok&#39;];
	}else{
		return [&#39;value&#39;=>False,&#39;size&#39;=>0,&#39;error&#39;=>1,&#39;msg&#39;=>&#39;no data for parseLUInt&#39;];
	}
	
}

//解析一个null结束的字符串
function parseString($context,$size=0){
	$pos=$context[&#39;pos&#39;];
	$data = substr($context[&#39;data&#39;], $pos);

	$p=0;
	$raw = substr($data, $p,1);
	$byte = unpack("C1",$raw)[1];	
	$result =&#39;&#39;;
	while($byte){
		$result.=$raw;
		$p++;
		if($p>=strlen($data)){
			return [&#39;value&#39;=>False,&#39;size&#39;=>0,&#39;error&#39;=>1,&#39;msg&#39;=>&#39;not find null end for parseString&#39;];
		}
		$raw = substr($data, $p,1);
		$byte = unpack("C1",$raw)[1];		
	}
	return [&#39;value&#39;=>$result,&#39;size&#39;=>$p,&#39;error&#39;=>0,&#39;msg&#39;=>&#39;ok&#39;];
}

//解析一个定长字符串
function parseFixStr($context,$size=0){
	$pos=$context[&#39;pos&#39;];
	$data = substr($context[&#39;data&#39;], $pos);
	//var_dump($data);	
	if(strlen($data)>=$size){
		$result =&#39;&#39;;
		for($i=0;$i<$size;$i++){
			$raw = substr($data, $i,1);
			$value = unpack("a1",$raw)[1];
			$result.=$value;			
		}
		return [&#39;value&#39;=>$result,&#39;size&#39;=>$size,&#39;error&#39;=>0,&#39;msg&#39;=>&#39;ok&#39;]; 
	}
	return [&#39;value&#39;=>False,&#39;size&#39;=>0,&#39;error&#39;=>1,&#39;msg&#39;=>&#39;not enough for parseFixedString&#39;];
}
Copy after login

The following is a working function for template replacement

<?php

//用于进行模板替换的工作函数

namespace Ados;

defined(&#39;__STRUCT_PARSE_TEMP__&#39;) or define(&#39;__STRUCT_PARSE_TEMP__&#39;, &#39;./&#39;);
define(&#39;_blockParsTempFile_&#39;,__STRUCT_PARSE_TEMP__.&#39;parseFunc.template.php&#39;);

//取出两个记号串之间的内容
function strBetweenToke($src,$toke1,$toke2){
	$p1 = strpos($src,$toke1);
	$p2 = strpos($src,$toke2);
	return substr($src,$p1+strlen($toke1),$p2-$p1-strlen($toke1));
}

//取得块分析函数的header
function blockHeaderStr(){
	$src = file_get_contents(_blockParsTempFile_);
	return strBetweenToke($src,&#39;/*blockHeader{{*/&#39;,&#39;/*blockHeader}}*/&#39;);
}

//取得块分析函数的body
function blockBodyStr(){
	$src = file_get_contents(_blockParsTempFile_);
	return strBetweenToke($src,&#39;/*blockBody{{*/&#39;,&#39;/*blockBody}}*/&#39;);
}

//取得块分析函数的tail
function blockTailStr(){
	$src = file_get_contents(_blockParsTempFile_);
	return strBetweenToke($src,&#39;/*blockTail{{*/&#39;,&#39;/*blockTail}}*/&#39;);
}

define(&#39;_blockHeaderStr_&#39;,blockHeaderStr());
define(&#39;_blockBodyStr_&#39;,blockBodyStr());
define(&#39;_blockTailStr_&#39;,blockTailStr());

function makeBlockHeader($blockName){
	return str_replace(&#39;parseBlock_Temp&#39;, &#39;parse&#39;.$blockName, _blockHeaderStr_);
}

function makeblockBody($parseFuncName,$filedName=&#39;&#39;,$filedSize=0){
	$tmp = str_replace(&#39;parseByte&#39;, $parseFuncName, _blockBodyStr_);
	$tmp = str_replace(&#39;$filedName&#39;, $filedName, $tmp);
	$tmp = str_replace(&#39;$filedSize&#39;, $filedSize, $tmp);
	return $tmp;
}

function makeblockTail(){
	return _blockTailStr_ ;
}


/*
echo blockHeaderStr();
echo blockBodyStr();
echo blockTailStr();

echo makeBlockHeader(&#39;Test1&#39;);
echo makeblockBody(&#39;parseInt&#39;);
echo makeblockBody(&#39;parseStr&#39;);
echo makeblockTail();
*/
Copy after login

With that After these preparations, a grammar-guided encoder can be implemented to generate the final parsing function.
The following is a simple encoder that generates script code that can be used to parse binary data when syntactically analyzing the structure definition.

<?php
/*!
 * structwkr编码器,
 *
 * 45022300@qq.com
 * Version 0.9.0
 *
 * Copyright 2019, Zhu Hui
 * Released under the MIT license
 */

namespace Ados;

require_once __SCRIPTCORE__.&#39;coder/base_coder.php&#39;;
require_once __STRUCT_PARSE_TEMP__.&#39;templateReplaceFuncs.php&#39;;


class StructwkrCoder extends BaseCoder{

	public function __construct($engine)
	{
		if($engine){ 
			$this->engine = $engine;
		}else{ 
			exit(&#39;the engine is not valid in StructwkrCoder construct.&#39;);
		}
	}

	//编译得到的最终结果
	public function codeLines(){
		if(count($this->codeLines)<1){
			return &#39;&#39;;
		}
		$script=&#39;&#39;;		
		for ($i=0;$i< count($this->codeLines);$i+=1) {
			$script.=$this->codeLines[$i];
		}	
		return $script;
	}	

	//输出编译后的结果
	public function printCodeLines(){
		echo $this->codeLines();	
	}

	//添加一个块解析函数头
	public function pushBlockHeader($structName){
		$structName=ucfirst($structName);
		$content = makeBlockHeader($structName);
		array_push($this->codeLines, $content);
		$lineIndex=$this->lineIndex;
		$this->lineIndex+=1;
		return $lineIndex;		
	}

	//添加一个块解析函数体
	public function pushBlockBody($parseFuncName,$filedName=&#39;&#39;,$filedSize=0){
		$content = makeblockBody($parseFuncName,$filedName,$filedSize);
		array_push($this->codeLines, $content);
		$lineIndex=$this->lineIndex;
		$this->lineIndex+=1;
		return $lineIndex;		
	}

	//添加一个块解析函数尾
	public function pushBlockTail(){
		$content = makeblockTail();
		array_push($this->codeLines, $content);
		$lineIndex=$this->lineIndex;
		$this->lineIndex+=1;
		return $lineIndex;		
	}

}
Copy after login

Automatically generated script file for parsing

Compile the C language structure, and finally get a series of functions that can be used to parse binary data. For example, two structures are defined in the above structure definition file

struct student
{
  char name[2];
  int num;
  int age;
  char addr[3];
};

struct teacher
{
  char name[2];
  int num;  
  char addr[3];
};
Copy after login
Copy after login

Then the parsing functions parseStudent and parseTeacher corresponding to these two structures will be generated.

The following is the automatically generated test script

False,'size'=>0,'error'=>$expRes['error'],'msg'=>$expRes['msg']];
	}

	$expRes = parseInt($context,4);
	if($expRes['error']==0){
		$filed = 'num';
		if($filed){
			$valueArray[$filed]=$expRes['value'];
		}else{
			$valueArray[]=$expRes['value'];
		}		
		$context['pos']+=$expRes['size'];
		$totalSize+= $expRes['size'];
	}else{
		return ['value'=>False,'size'=>0,'error'=>$expRes['error'],'msg'=>$expRes['msg']];
	}

	$expRes = parseInt($context,4);
	if($expRes['error']==0){
		$filed = 'age';
		if($filed){
			$valueArray[$filed]=$expRes['value'];
		}else{
			$valueArray[]=$expRes['value'];
		}		
		$context['pos']+=$expRes['size'];
		$totalSize+= $expRes['size'];
	}else{
		return ['value'=>False,'size'=>0,'error'=>$expRes['error'],'msg'=>$expRes['msg']];
	}

	$expRes = parseFixStr($context,3);
	if($expRes['error']==0){
		$filed = 'addr';
		if($filed){
			$valueArray[$filed]=$expRes['value'];
		}else{
			$valueArray[]=$expRes['value'];
		}		
		$context['pos']+=$expRes['size'];
		$totalSize+= $expRes['size'];
	}else{
		return ['value'=>False,'size'=>0,'error'=>$expRes['error'],'msg'=>$expRes['msg']];
	}

	return ['value'=>$valueArray,'size'=>$totalSize,'error'=>0,'msg'=>'ok'];
}

function parseTeacher($context,$size=0){
	$valueArray=[];
	$totalSize = 0;

	$expRes = parseFixStr($context,2);
	if($expRes['error']==0){
		$filed = 'name';
		if($filed){
			$valueArray[$filed]=$expRes['value'];
		}else{
			$valueArray[]=$expRes['value'];
		}		
		$context['pos']+=$expRes['size'];
		$totalSize+= $expRes['size'];
	}else{
		return ['value'=>False,'size'=>0,'error'=>$expRes['error'],'msg'=>$expRes['msg']];
	}

	$expRes = parseInt($context,4);
	if($expRes['error']==0){
		$filed = 'num';
		if($filed){
			$valueArray[$filed]=$expRes['value'];
		}else{
			$valueArray[]=$expRes['value'];
		}		
		$context['pos']+=$expRes['size'];
		$totalSize+= $expRes['size'];
	}else{
		return ['value'=>False,'size'=>0,'error'=>$expRes['error'],'msg'=>$expRes['msg']];
	}

	$expRes = parseFixStr($context,3);
	if($expRes['error']==0){
		$filed = 'addr';
		if($filed){
			$valueArray[$filed]=$expRes['value'];
		}else{
			$valueArray[]=$expRes['value'];
		}		
		$context['pos']+=$expRes['size'];
		$totalSize+= $expRes['size'];
	}else{
		return ['value'=>False,'size'=>0,'error'=>$expRes['error'],'msg'=>$expRes['msg']];
	}

	return ['value'=>$valueArray,'size'=>$totalSize,'error'=>0,'msg'=>'ok'];
}
Copy after login

Run this test script and the results are as follows:

Array
(
    [value] => Array
        (
            [name] => AB
            [num] => 1
            [age] => 2
            [addr] => ABC
        )

    [size] => 13
    [error] => 0
    [msg] => ok
)
Array
(
    [value] => Array
        (
            [name] => AB
            [num] => 1
            [addr] => ABC
        )

    [size] => 9
    [error] => 0
    [msg] => ok
)
Copy after login

OK! The first step of the task has been completed, and then consider implementing it Parsing of a structure with conditional judgment.

For more related questions, please visit the PHP Chinese website: //m.sbmmt.com/

The above is the detailed content of PHP implements functions similar to the Construct library in Python (1) Basic design ideas. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template