PHP data compression, encryption and decryption (pack, unpack) _php tutorial

Source: Internet
Author: User
Tags flock fread unpack
network communication, file storage often need to exchange data, in order to reduce network traffic, file storage size and encryption of communication rules, often need to two-way encryption and decryption to ensure data security. The main function of this function in PHP is to use the pack and unpack function pack to compress the data into the string. Syntax: string Pack (string format, mixed [args] ...); Return value: String This function is used to package data compression into a string in place. a-nul-string fills [padded string] fills the string blank with a NULL character a-space-the string fills up [padded string]h– hex String, low "four bit" [Lo Nibble first] (low in front) h -16 binary strings, high "four-bit" [higher nibble first] (high in front) C – signed characters C – unsigned characters S-short pattern with symbols [shorter] (usually 16 bits, in machine byte order) s-Short mode without symbols Usually 16 bits, sorted by machine Byte) n-Short pattern with no sign (usually 16 bits, sorted by large endian byte) v-short pattern with no sign (usually 16 bits, sorted by small endian bytes) I – Integers with symbols (determined by size and byte order) I – unsigned integers (determined by size and byte order) L – long pattern with symbols [long] (usually 32 bits, in machine byte order) L – long (usually 32 bits, in machine byte order) without symbols n – Long mode without symbols [long] (usually 32 bits, in large Edian byte order) V– long pattern without symbols [long] (usually 32 bits, in small Edian byte order) F – floating point (determined by size and byte order) d – double precision (determined by size and byte order) x – Empty bytes [NUL Byte] X-Behind a byte [back up one byte] (rewind a bit) unpack the bit string data. Syntax: string Pack (string format, mixed [args] ...); Return value: Array This function is used to decompress the data of a bit string. This function is identical to Perl's function function with the same name. Case one, pack implementation reduced file data storage size [PHP] _node_struct = Array (' Next ' =>array (1, ' V '), ' prev ' =>array (1, ' V '), ' Data_offset ' =>array (1, ' V '),//data store start location ' Data_size ' =>array (1, ' V '),//Data length ' Ref_count ' =>array (1, ' V '),//reference here, mimic PHP reference count destroy mode ' key ' =>array (+, ' h* '),/ /store key); $this->_file_index_name = $file _index; $this->_file_data_name = $file _data; if (!file_exists ($this->_file_index_name)) {$this->_create_index ();} else{$this->_file_index = fopen ($this->_file_index_name, "rb+");} if (!file_exists ($this->_file_data_name) ) {$this->_create_data ();} else{$this->_file_data = fopen ($this->_file_data_name, "rb+");//binary storage needs to use B}}//create index file Private function _create_ Index () {$this->_file_index = fopen ($this->_file_index_name, "wb+");//binary storage requires the use of B if (! $this->_file_index) throw new Filecacheexception ("Could ' t Open index File:". $this->_file_index_name); $this->_index_puts (0, ' < '? PHP exit ()? '. ' >)///Locate file stream to start position 0, place php tag to prevent download $this->_index_puts ($this->_file_header_size, pack ("V1 ", 0)); }//Create storage file Private Function _create_data () {$this->_file_data = fopen ($this->_file_data_name, "wb+");// Binary storage requires the use of the B if (! $this->_file_index) throw new Filecacheexception ("Could ' t Open index File:". $this->_file_data_ name); $this->_data_puts (0, ' < '? PHP exit ()? '. ' > ')///Locate file stream to start position 0, place php tag to prevent download} Private function _index_puts ($offset, $data, $length =false) {fseek ($this->_file _index, $offset); if ($length) fputs ($this->_file_index, $data, $length); else fputs ($this->_file_index, $data); } Private Function _data_puts ($offset, $data, $length =false) {fseek ($this->_file_data, $offset); if ($length) fputs ($ This->_file_data, $data, $length); else fputs ($this->_file_data, $data); }/** * File lock * @param $is _block is exclusive, blocking lock */Private Function _lock ($file _res, $is _block=true) {flock ($file _res, $is _block? Lock_ex:lock_ex| LOCK_NB); } Private Function _unlock ($file _res) {flock ($file _res, Lock_un),} public function Add ($key, $value) {$key = MD5 ($key); $ Value= Serialize ($value); $this->_lock ($this->_file_index, true); $this->_lock ($this->_file_data, true); Fseek ($this->_file_index, $this->_file_header_size); List (, $index _count) = Unpack (' V1 ', fread ($this->_file_index, 4)); $data _size = filesize ($this->_file_data_name); Fseek ($this->_file_data, $data _size); $value _size = strlen ($value); $this->_data_puts (filesize ($this->_file_data_name), $value); $node _data = Pack ("V1v1v1v1v1h32", ($index _count==0)? 0: $index _count* $this->_inx_node_size, 0, FileSize ($this- >_file_data_name), strlen ($value), 0, $key); $index _count++; $this->_index_puts ($this->_file_header_size, $index _count, 4); $this->_index_puts ($this->get_new_node_pos ($index _count), $node _data); $this->_unlock ($this->_file_data); $this->_unlock ($this->_file_index); The Public Function Get_new_node_pos ($index _count) {return $this->_file_header_size + 4 + $this->_inx_node_size * ($i NDEX_COUNT-1); } Public Function Get_node($key) {$key = MD5 ($key); Fseek ($this->_file_index, $this->_file_header_size); $index _count = fread ($this->_file_ index, 4); if ($index _count>0) {for ($i =0; $i < $index _count; $i + +) {fseek ($this->_file_index, $this->_file_header_siz E + 4 + $this->_inx_node_size * $i); $data = Fread ($this->_file_index, $this->_inx_node_size); $node = Unpack ("V1next/v1prev/v1data_offset/v1data_size/v1ref_count/h32key", $data); if ($key = = $node [' key ']) {return $node;}} }else{return null;}} Public Function Get_data ($offset, $length) {fseek ($this->_file_data, $offset); return Unserialize (Fread ($this- _file_data, $length)); }}//use method $cache = new Filecache (); $cache->add (' ABCDEFG ', ' testabc '); $data = $cache->get_node (' ABCDEFG '); Print_r ($data); echo $cache->get_data ($data [' Data_offset '], $data [' data_size ']); _node_struct = Array (' Next ' =>array (1, ' V '), ' prev ' =>array (1, ' V '), ' Data_offset ' =>array (1, ' V '),//data store start location ' Data_size ' =>array (1, ' V '),//Data length ' Ref_count ' =>array (1, ' V '),//reference here, mimic PHP reference count destroy mode ' key ' =>array (+, ' h* '),/ /store key); $this->_file_index_name = $file _index; $this->_file_data_name = $file _data; if (!file_exists ($this->_file_index_name)) {$this->_create_index ();} else{$this->_file_index = fopen ($this->_file_index_name, "rb+");} if (!file_exists ($this->_file_data_name) ) {$this->_create_data ();} else{$this->_file_data = fopen ($this->_file_data_name, "rb+");//binary storage needs to use B}}//create index file Private function _create_ Index () {$this->_file_index = fopen ($this->_file_index_name, "wb+");//binary storage requires the use of B if (! $this->_file_index) throw new Filecacheexception ("Could ' t Open index File:". $this->_file_index_name); $this->_index_puts (0, ' < '? PHP exit ()? '. ' >)///Locate file stream to start position 0, place php tag to prevent download $this->_index_puts ($this->_file_header_size, pack ("V1 ", 0)); }//Create storage file Private Function _create_data () {$this->_file_data = fopen ($this->_file_data_name, "wb+");// Binary storage requires the use of the B if (! $this->_file_index) throw new Filecacheexception ("Could ' t Open index File:". $this->_file_data_ name); $this->_data_puts (0, ' < '? PHP exit ()? '. ' > ')///Locate file stream to start position 0, place php tag to prevent download} Private function _index_puts ($offset, $data, $length =false) {fseek ($this->_file _index, $offset); if ($length) fputs ($this->_file_index, $data, $length); else fputs ($this->_file_index, $data); } Private Function _data_puts ($offset, $data, $length =false) {fseek ($this->_file_data, $offset); if ($length) fputs ($ This->_file_data, $data, $length); else fputs ($this->_file_data, $data); }/** * File lock * @param $is _block is exclusive, blocking lock */Private Function _lock ($file _res, $is _block=true) {flock ($file _res, $is _block? Lock_ex:lock_ex| LOCK_NB); } Private Function _unlock ($file _res) {flock ($file _res, Lock_un),} public function Add ($key, $value) {$key = MD5 ($key); $ Value= Serialize ($value); $this->_lock ($this->_file_index, true); $this->_lock ($this->_file_data, true); Fseek ($this->_file_index, $this->_file_header_size); List (, $index _count) = Unpack (' V1 ', fread ($this->_file_index, 4)); $data _size = filesize ($this->_file_data_name); Fseek ($this->_file_data, $data _size); $value _size = strlen ($value); $this->_data_puts (filesize ($this->_file_data_name), $value); $node _data = Pack ("V1v1v1v1v1h32", ($index _count==0)? 0: $index _count* $this->_inx_node_size, 0, FileSize ($this- >_file_data_name), strlen ($value), 0, $key); $index _count++; $this->_index_puts ($this->_file_header_size, $index _count, 4); $this->_index_puts ($this->get_new_node_pos ($index _count), $node _data); $this->_unlock ($this->_file_data); $this->_unlock ($this->_file_index); The Public Function Get_new_node_pos ($index _count) {return $this->_file_header_size + 4 + $this->_inx_node_size * ($i NDEX_COUNT-1); } Public Function Get_node($key) {$key = MD5 ($key); Fseek ($this->_file_index, $this->_file_header_size); $index _count = fread ($this->_file_ index, 4); if ($index _count>0) {for ($i =0; $i < $index _count; $i + +) {fseek ($this->_file_index, $this->_file_header_siz E + 4 + $this->_inx_node_size * $i); $data = Fread ($this->_file_index, $this->_inx_node_size); $node = Unpack ("V1next/v1prev/v1data_offset/v1data_size/v1ref_count/h32key", $data); if ($key = = $node [' key ']) {return $node;}} }else{return null;}} Public Function Get_data ($offset, $length) {fseek ($this->_file_data, $offset); return Unserialize (Fread ($this- _file_data, $length)); }}//use method $cache = new Filecache (), $cache->add (' ABCDEFG ', ' testabc '), $data = $cache->get_node (' ABCDEFG ');p Rint_r ($data); Echo $cache->get_data ($data [' Data_offset '], $data [' data_size ']); Case four, socket communication encrypted communication both sides have defined a good encryption format: for example: [php] LOGIN = Array (' COMMAND ' =>array (' A30 ', ' LOGIN '), ' DATA ' =>array (' A30 ', ' HELLO ')); $LOGOUT = Array (' COMMAND ' = =Array (' A30 ', ' LOGOUT '), ' DATA ' =>array (' A30 ', ' good BYE ')); $LOGIN _success = Array (' COMMAND ' =>array (' A30 ', ' login_success '), ' DATA ' =>array (' V1 ', 1)); $LOGOUT _success = Array (' COMMAND ' =>array (' A30 ', ' login_success '), ' DATA ' =>array (' V1 ', Time ())); $LOGIN = Array (' COMMAND ' =>array (' A30 ', ' LOGIN '), ' DATA ' =>array (' A30 ', ' HELLO ')); $LOGOUT = Array (' COMMAND ' =>array (' A30 ', ' LOGOUT '), ' DATA ' =>array (' A30 ', ' good BYE ')); $LOGIN _success = Array (' COMMAND ' =>array (' A30 ', ' login_success '), ' DATA ' =>array (' V1 ', 1)); $LOGOUT _success = Array (' COMMAND ' =>array (' A30 ', ' login_success '), ' DATA ' =>array (' V1 ', Time ())); Server side and client based on the parse command format, find the corresponding data decoding method, get the correct

http://www.bkjia.com/PHPjc/477666.html www.bkjia.com true http://www.bkjia.com/PHPjc/477666.html techarticle network communication, file storage often need to exchange data, in order to reduce network traffic, file storage size and encryption communication rules, often need to double-way data encryption to protect ...

  • Related Article

    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.