php struct
1.0.0
PHP의 구조 구현. 이진 파일 디코딩을 지원합니다.
새로운 구조물 생성
$ myStruct_t = new Struct (
" foo " , uint32_t, //single uint32_t
" baz " , uint8_t, 30 //array of 30 unsigned chars
);다음과 같은 멤버의 플래그를 지정할 수 있습니다.
$ myStruct_t = new Struct (
" beval " , uint32_t, 1 , ENDIAN_BIG , //big endian value
" leval " , uint32_t, 1 , ENDIAN_LITTLE , //little endian value
" myString " , int8_t, 32 , VAR_STRING , //string of 32 characters
" myNumber " , uint32_t, 1 , VAR_NUMERIC , //explicitly uses the PHP's int type
);Flag_Strsz를 사용 하여이 멤버가 다가오는 문자열의 크기를 지정 함을 나타냅니다.
$ string_t = new Struct (
" strSz " , uint32_t, 1 , FLAG_STRSZ , //a string follow, and its length is indicated by this member
" varString " , uint8_t, 0 , //the size will be replaced at runtime due to FLAG_STRSZ
);FLAG_STRSZ를 사용하면 구조 크기를 예측할 수 없습니다.
중첩 구조를 사용할 수도 있습니다.
$ myStruct_t = new Struct (
" foo " , uint32_t, //single uint32_t
" baz " , uint8_t, 30 //array of 30 unsigned chars
);
$ otherStruct_t = new Struct (
" magic " , uint8_t, 4 ,
" elements " , $ myStruct_t , 32 , //creates an array of 32 structures
);스트러크 및 파일 :
// Clone the structure template
$ header = clone ( $ header_t );
// Simple check for proper arguments
if ( $ argc < 2 || ! file_exists ( $ argv [ 1 ])){
fprintf ( STDERR , " File not found! n" );
return 1 ;
}
// Open the specified file in read mode
$ f = fopen ( $ argv [ 1 ], " rb " );
// Get enough data to fill the structure
$ d = fread ( $ f , $ header -> getSize ());
// We don't need the file anymore
fclose ( $ f );
// Put the data we read into the structure
$ header -> apply ( $ d );요소를 구문 분석합니다
printf ( " Struct size: %d n" , $ header -> getSize ());
foreach ( $ header -> members as $ name => $ member ){
printf ( " member '%s', value: 0x%x n" , $ name , $ member -> getValue ());
}그리고 중첩 된 구조물을 위해?
function printStruct ( $ struct ){
foreach ( $ struct -> members as $ name => $ memb ){
$ value = $ memb -> getValue ();
if ( is_array ( $ value )){
if (Struct:: isStructArray ( $ value )){
foreach ( $ value as $ subStruct ){
printStruct ( $ subStruct );
}
} else {
//print array of bytes/elements
printf ( " %s n" , $ name );
var_dump ( $ value );
}
} else {
//print element/value
printf ( " %s => 0x%x n" , $ name , $ value );
}
}
}멤버/구조물의 이진 데이터를 얻습니다
$ binaryData = $ member -> getData ();멤버의 이진 데이터 설정 (이것을 사용하여 문자열을 설정하십시오!)
$ member -> setData ( $ binData );회원의 디코딩 된 값을 얻습니다 (유형에 따라).
$ value = $ member -> getValue ();멤버의 값 설정 (유형에 따라 인코딩됩니다).
참고 : 문자열의 경우 SetData를 사용 하거나이 기능에 숯 배열을 전달해야합니다.
$ member -> setValue ( $ value );