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 ، أو ستحتاج إلى تمرير مجموعة Char إلى هذه الوظيفة
$ member -> setValue ( $ value );