Pack, unpack homemade binary "database", packunpack_PHP tutorial

Source: Internet
Author: User
Tags format definition
Pack and unpack are self-made binary "databases" and packunpack. Pack, unpack homemade binary "database", packunpack introduction pack, unpack function, if not touched socket, this may be unfamiliar, these two functions are used as pack for socket interaction, unpack for self-made binary "database", and packunpack
Introduction

Pack and unpack functions, which may be unfamiliar if they have never touched the socket. These two functions interact with each other in a set of packages and load the data into a binary string, and unpackage the data in the binary string. There are many formats in it. you can check the official manual for the specific format (or call the interface to view it after reading this article ), here I mainly use pack ("N", int), pack ("a", str) and their two corresponding unwrapped functions. N is explained below in the manual, it occupies 4 bytes, and the large-end mode (in fact, it is the problem that the low position is in the front or back ). A is to package the string, and fill it with NULL (\ 0, or the character corresponding to assic code 0) when the specified value is not enough.

N - unsigned long (always 32 bit, big endian byte order)

a - NUL-padded string

I will use this package to unpackage functions for a function manual query tool, or it can be said to be a self-made small binary database.

Design Data format

When I create this binary database, I will create two files, one is the index file and the other is the file of the data to be queried. let's look at their structure separately:

The number in the brackets represents the number of bytes ,"~ "The number of bytes in the tilde representation is uncertain.

Data File. The first php is a formal string "php", which occupies 4 bytes. it is followed by the version description and the length is uncertain (this length can be obtained from the index file below ), the next step is the subject of information storage. First, a function name contains four bytes of lenName, followed by the function name. The length is uncertain and the value corresponding to the previous lenName is determined. Next, lenVal occupies four bytes, the following describes the specific function description. the length is determined by the corresponding lenVal value.

Content Storage Format Definition ++ | php (4) | version description (~) | ++ | LenName (4) | function name (~) | ++ | LenVal (4) | function content (~) | ++. .....

Index files, index files are relatively simple, all of which store the pointer positions starting with each function in the above storage file, each location occupies 4 bytes.

Index format definition ++ + | position (4) | ++. .....

Query implementation

Because the content in the stored files is stored in the order of function names, and the indexes are stored in the order of function storage, it is convenient to obtain them, you can easily obtain the desired function by using the bipartite method.

The following methods are used for query:

1. obtain the value of an index from the specified position (that is, the pointer position of the corresponding function storage file)

/*** Obtain the location of a record from the index file * @ start location in the param index file, obtain four bytes from the start position as the start position of a function description * @ return returns the storage position pointer offset corresponding to the index position */private function _ getOneIndex ($ pos) {fseek ($ this-> _ indexHandle, $ pos); $ len = unpack ("Nlen", fread ($ this-> _ indexHandle, 4 )); return $ len ['len'];}

Second, get a len (4) + val (~) from the specified pointer offset (~) Format content

/*** Get a len + val content from the specified pointer offset * @ param $ pos file pointer offset * @ return returns an array, including length and value */private function _ getStoreLenValFormat ($ pos) {fseek ($ this-> _ storeHandle, $ pos); $ len = unpack ("Nlen ", fread ($ this-> _ storeHandle, 4); $ len = $ len ['len']; $ val = fread ($ this-> _ storeHandle, $ len ); return array ('len' => $ len, 'value' => $ val ,);}

Third, obtain the description of the function, which is also the most important part. obtain a record from the data file using the binary method.

/*** Get function content ** @ param the name of the function to be searched * @ return returns the json string of the function description */public function get ($ func) {if (! $ This-> isInit () return; $ begin = 0; $ end = filesize ($ this-> _ indexFile)/4; $ ret = '[]'; while ($ begin <$ end) {$ mid = floor ($ begin + $ end)/2); $ pos = $ mid * 4; // $ mid is only the position of the pointer variable. you also need to multiply the pointer length by 4 $ pos = $ this-> _ getOneIndex ($ pos ); $ name = $ this-> _ getStoreLenValFormat ($ pos); $ flag = strcmp ($ func, $ name ['value']); if ($ flag = 0) {$ val = $ this-> _ getStoreLenValFormat ($ pos + 4 + $ name ['len']); $ ret = $ val ['value']; br Eak;} elseif ($ flag <0) {$ end = $ mid? $ Mid-1: $ mid;} else {$ begin = $ mid? $ Mid + 1: $ mid;} return $ ret ;}

It is easy to use. you only need to include the class library file and the storage file database, and then call a few codes.

  init('zh');echo $t->get("unpack");

The output is a json string, which is converted as follows, with detailed descriptions and concise examples.

{    "name": "unpack",    "desc": "Unpack data from binary string.",    "long_desc": "Unpacks from a binary string into an array according to the given `format`.\\n\\nThe unpacked data is stored in an associative array. To accomplish this you have to name the different format codes and separate them by a slash /. If a repeater argument is present, then each of the array keys will have a sequence number behind the given name.",    "ver": "PHP 4, PHP 5",    "ret_desc": "Returns an associative array containing unpacked elements of binary string.",    "seealso": [        "pack"    ],    "url": "function.unpack",    "class": null,    "params": [        {            "list": [                {                    "type": "string",                    "var": "$format",                    "beh": 0,                    "desc": "See pack() for an explanation of the format codes."                },                {                    "type": "string",                    "var": "$data",                    "beh": 0,                    "desc": "The packed data."                }            ],            "ret_type": "array"        }    ],    "examples": [        {            "title": "unpack() example",            "source": "$binarydata = \"\\x04\\x00\\xa0\\x00\";\n$array = unpack(\"cchars/nint\", $binarydata);",            "output": null        },        {            "title": "unpack() example with a repeater",            "source": "$binarydata = \"\\x04\\x00\\xa0\\x00\";\n$array = unpack(\"c2chars/nint\", $binarydata);",            "output": null        },        {            "title": "unpack() example with unnamed keys",            "source": "$binarydata = \"\\x32\\x42\\x00\\xa0\";\n$array = unpack(\"c2/n\", $binarydata);\nvar_dump($array);",            "output": null        }    ]}

Finally, the directory structure is attached:

+phpManual    +manual        +phpManual            +zh                |_manualIndex                |_manualStore        |_phpManual.php    |_test.php

This is the complete address of the program:

Complete example address

Reference

All phpmanual data from https://github.com/aizuyan/php-doc-parser here

  

The copyright of this article is owned by the author iforever (luluyrt@163.com), without the author's consent to prohibit any form of Reprint, repost the article must be in the obvious position on the article page to give the author and the original connection, otherwise, you are entitled to pursue legal liability.

Introduction: pack and unpack functions. if you haven't touched socket, this may be unfamiliar. These two functions interact with each other in the socket...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.