{"id":21820492,"url":"https://github.com/smx-smx/php-struct","last_synced_at":"2025-04-14T02:53:15.536Z","repository":{"id":35395648,"uuid":"39659677","full_name":"smx-smx/php-struct","owner":"smx-smx","description":"Struct implementation in PHP. It supports decoding binary files.","archived":false,"fork":false,"pushed_at":"2019-09-06T21:13:51.000Z","size":22,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T16:56:02.459Z","etag":null,"topics":["binary-data","c-struct","endianness","php","php-library","php-struct","structure"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/smx-smx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-24T21:54:20.000Z","updated_at":"2025-03-12T14:32:23.000Z","dependencies_parsed_at":"2022-09-16T07:41:10.039Z","dependency_job_id":null,"html_url":"https://github.com/smx-smx/php-struct","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smx-smx%2Fphp-struct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smx-smx%2Fphp-struct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smx-smx%2Fphp-struct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smx-smx%2Fphp-struct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smx-smx","download_url":"https://codeload.github.com/smx-smx/php-struct/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248813829,"owners_count":21165631,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary-data","c-struct","endianness","php","php-library","php-struct","structure"],"created_at":"2024-11-27T16:34:45.463Z","updated_at":"2025-04-14T02:53:15.519Z","avatar_url":"https://github.com/smx-smx.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-struct\nStruct implementation in PHP. It supports decoding binary files.\n\nCreating a new struct\n```php\n$myStruct_t = new Struct(\n  \"foo\", uint32_t,        //single uint32_t\n  \"baz\", uint8_t, 30      //array of 30 unsigned chars\n);\n```\nYou can specify flags for a members, such as:\n```php\n$myStruct_t = new Struct(\n  \"beval\", uint32_t, 1, ENDIAN_BIG,       //big endian value\n  \"leval\", uint32_t, 1, ENDIAN_LITTLE,    //little endian value\n  \"myString\", int8_t, 32, VAR_STRING,     //string of 32 characters\n  \"myNumber\", uint32_t, 1, VAR_NUMERIC,   //explicitly uses the PHP's int type\n);\n```\n\nYou can use FLAG_STRSZ to indicate that this member will specify the size of an upcoming string\n```php\n$string_t = new Struct(\n  \"strSz\", uint32_t, 1, FLAG_STRSZ,   //a string follow, and its length is indicated by this member\n  \"varString\", uint8_t, 0,            //the size will be replaced at runtime due to FLAG_STRSZ\n);\n```\nThe use of FLAG_STRSZ makes a structure size unpredictable.\n\n\n\nYou can also use nested structures:\n```php\n$myStruct_t = new Struct(\n  \"foo\", uint32_t,        //single uint32_t\n  \"baz\", uint8_t, 30      //array of 30 unsigned chars\n);\n\n$otherStruct_t = new Struct(\n  \"magic\", uint8_t, 4,\n  \"elements\", $myStruct_t, 32, //creates an array of 32 structures\n);\n```\n\nStructs and files:\n```php\n// Clone the structure template\n$header = clone($header_t);\n// Simple check for proper arguments\nif($argc \u003c 2 || !file_exists($argv[1])){\n\tfprintf(STDERR, \"File not found!\\n\");\n\treturn 1;\n}\n// Open the specified file in read mode\n$f = fopen($argv[1], \"rb\");\n// Get enough data to fill the structure\n$d = fread($f, $header-\u003egetSize());\n// We don't need the file anymore\nfclose($f);\n\n// Put the data we read into the structure\n$header-\u003eapply($d);\n```\n\nParsing the elements\n```php\nprintf(\"Struct size: %d\\n\", $header-\u003egetSize());\nforeach($header-\u003emembers as $name =\u003e $member){\n  printf(\"member '%s', value: 0x%x\\n\", $name, $member-\u003egetValue());\n}\n```\n\nAnd for nested structures?\n```php\nfunction printStruct($struct){\n\tforeach($struct-\u003emembers as $name =\u003e $memb){\n\t\t$value = $memb-\u003egetValue();\n\t\tif(is_array($value)){\n\t\t\tif(Struct::isStructArray($value)){\n\t\t\t\tforeach($value as $subStruct){\n\t\t\t\t\tprintStruct($subStruct);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t//print array of bytes/elements\n\t\t\t\tprintf(\"%s\\n\", $name);\n\t\t\t\tvar_dump($value);\n\t\t\t}\n\t\t} else {\n\t\t\t//print element/value\n\t\t\tprintf(\"%s =\u003e 0x%x\\n\", $name, $value);\n\t\t}\n\t}\n}\n```\n\nGetting the binary data of a member/struct\n```php\n$binaryData = $member-\u003egetData();\n```\n\nSetting the binary data of a member (use this to set strings!)\n```php\n$member-\u003esetData($binData);\n```\n\nGetting the decoded value of a member (according to its type).\n```php\n$value = $member-\u003egetValue();\n```\n\nSetting the value of a member (will get encoded according to its type).\n\n\u003cb\u003eNOTE\u003c/b\u003e: for strings use setData, or you'll need to pass a char array to this function\n```php\n$member-\u003esetValue($value);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmx-smx%2Fphp-struct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmx-smx%2Fphp-struct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmx-smx%2Fphp-struct/lists"}