项目作者: smx-smx

项目描述 :
Struct implementation in PHP. It supports decoding binary files.
高级语言: PHP
项目地址: git://github.com/smx-smx/php-struct.git
创建时间: 2015-07-24T21:54:20Z
项目社区:https://github.com/smx-smx/php-struct

开源协议:MIT License

下载


php-struct

Struct implementation in PHP. It supports decoding binary files.

Creating a new struct

  1. $myStruct_t = new Struct(
  2. "foo", uint32_t, //single uint32_t
  3. "baz", uint8_t, 30 //array of 30 unsigned chars
  4. );

You can specify flags for a members, such as:

  1. $myStruct_t = new Struct(
  2. "beval", uint32_t, 1, ENDIAN_BIG, //big endian value
  3. "leval", uint32_t, 1, ENDIAN_LITTLE, //little endian value
  4. "myString", int8_t, 32, VAR_STRING, //string of 32 characters
  5. "myNumber", uint32_t, 1, VAR_NUMERIC, //explicitly uses the PHP's int type
  6. );

You can use FLAG_STRSZ to indicate that this member will specify the size of an upcoming string

  1. $string_t = new Struct(
  2. "strSz", uint32_t, 1, FLAG_STRSZ, //a string follow, and its length is indicated by this member
  3. "varString", uint8_t, 0, //the size will be replaced at runtime due to FLAG_STRSZ
  4. );

The use of FLAG_STRSZ makes a structure size unpredictable.

You can also use nested structures:

  1. $myStruct_t = new Struct(
  2. "foo", uint32_t, //single uint32_t
  3. "baz", uint8_t, 30 //array of 30 unsigned chars
  4. );
  5. $otherStruct_t = new Struct(
  6. "magic", uint8_t, 4,
  7. "elements", $myStruct_t, 32, //creates an array of 32 structures
  8. );

Structs and files:

  1. // Clone the structure template
  2. $header = clone($header_t);
  3. // Simple check for proper arguments
  4. if($argc < 2 || !file_exists($argv[1])){
  5. fprintf(STDERR, "File not found!\n");
  6. return 1;
  7. }
  8. // Open the specified file in read mode
  9. $f = fopen($argv[1], "rb");
  10. // Get enough data to fill the structure
  11. $d = fread($f, $header->getSize());
  12. // We don't need the file anymore
  13. fclose($f);
  14. // Put the data we read into the structure
  15. $header->apply($d);

Parsing the elements

  1. printf("Struct size: %d\n", $header->getSize());
  2. foreach($header->members as $name => $member){
  3. printf("member '%s', value: 0x%x\n", $name, $member->getValue());
  4. }

And for nested structures?

  1. function printStruct($struct){
  2. foreach($struct->members as $name => $memb){
  3. $value = $memb->getValue();
  4. if(is_array($value)){
  5. if(Struct::isStructArray($value)){
  6. foreach($value as $subStruct){
  7. printStruct($subStruct);
  8. }
  9. } else {
  10. //print array of bytes/elements
  11. printf("%s\n", $name);
  12. var_dump($value);
  13. }
  14. } else {
  15. //print element/value
  16. printf("%s => 0x%x\n", $name, $value);
  17. }
  18. }
  19. }

Getting the binary data of a member/struct

  1. $binaryData = $member->getData();

Setting the binary data of a member (use this to set strings!)

  1. $member->setData($binData);

Getting the decoded value of a member (according to its type).

  1. $value = $member->getValue();

Setting the value of a member (will get encoded according to its type).

NOTE: for strings use setData, or you’ll need to pass a char array to this function

  1. $member->setValue($value);