Php binary security _ PHP Tutorial

Source: Internet
Author: User
Php binary security. This article describes php's binary security from three perspectives: 1. what is php binary security? 2. what structure ensures the binary security of php; 3. this structure also provides php binary security.

This article describes the binary security of php from three perspectives: 1. what is php binary security? 2. what structure ensures the binary security of php; 3. what other applications are there in this structure?

You can also know what it is.

One sentence:

Php internal functions can guarantee the expected results when operating binary data, such as str_replace, stristr, strcmp, and so on. we will say these functions are binary safe.

Give a column:

Let's compare the strcmp functions in C and php.

The C code is as follows:

main(){    char ab[] = "aa\0b";    char ac[] = "aa\0c";    printf("%d\n", strcmp(ab, ac));    printf("%d\n", strlen(ab)); }

Result:

0

2

Explanation:

That is to say, the C language considers the AB and ac strings to be equal, and the AB length is 2.


The php code is as follows:

 
Result:

Int (-1)

Int (4)

Explanation:

That is, php considers the AB and ac strings to be equal, and the AB length is 4.

Clever, you should have found out where the problem is. Good, for C language, '\ 0' is the string terminator, so in C language, for the string "aa \ 0b ", when it reads '\ 0', the default character reading is completed, and the following string' B 'is discarded. as a result, we can see that the value of strlen ("aa \ 0b") is 2.

The problem is that php is developed by C. Why does php achieve binary security?

Let's take a look at the php variable storage zval structure.


Php will determine which member to access the value based on the value of type. when it is a string, we will access the str structure identified by the red box, which is the storage structure of the underlying string, it has two values, one is the pointer to the string val, and the other is the len value that records the length of the string, because of the value of len, resulting in php binary security: it does not need to judge whether the entire string has been read through the '\ 0' ending character like C, but read through the length specified by len.

Zookeeper
It can be seen that a small data structure improvement brings us more room for imagination. A small step in structure and a huge step in functionality.

Expansion:

This easy-to-use structure will obviously be used everywhere. our commonly used redis uses this structure when storing data at the underlying layer, redis does not directly use a traditional string representation (an array of characters ending with null characters) in the C language. Instead, it builds a simple dynamic string (SDS) and use SDS as the default string representation of Redis.

Let's take a look at the structure definition of SDS.
Struct sdshdr {// record the number of bytes used in the buf array // equal to the length of the string stored by SDS int len; // record the number of unused bytes in the buf array int free; // byte array, used to save the string char buf [];};
As you can see, we have seen the familiar len value, which ensures the binary security of redis storage.

The following content is sufficient to clarify this:(From http://redisbook.com/preview/sds/different_between_sds_and_c_string.html#id6)

The characters in the C string must conform to certain encoding (such as ASCII), and cannot contain null characters except the end of the string, otherwise, the null characters first read by the program will be mistaken for the end of the string-these restrictions allow the C string to only store text data, however, binary data such as images, audios, videos, and compressed files cannot be stored.

For example, if there is a special data format that uses null characters to separate multiple words, as shown in 2-17, this format cannot be saved using a C string, because the function used by the C string only recognizes"Redis"And ignore"Cluster".

Although the database is generally used to save text data, there are many scenarios where the database is used to store binary data. therefore, to ensure that Redis can be used in different scenarios, SDS APIs are binary-safe: all SDS APIs process SDS in binary mode and store it inbufThe program does not limit, filter, or assume that the data is written and read.

This is what we use SDSbufAttribute is called a byte array. Redis uses this array not to store characters, but to store a series of binary data.

For example, there is no problem in using SDS to save the Special Data format mentioned previously, because SDS useslenAttribute value instead of null characters to determine whether the string ends, as shown in 2-18.

Buf "]; buf [label = "{'R' | 'E' | 'D' | 'I' | 'S' | '\ 0' | 'C' | 'L' | 'U' |'s '| 'T' | 'E' | 'R' |' \ 0' | '\ 0'} "]; // sdshdr: buf-> buf;} ">

By using binary secure SDS instead of a C string, Redis can not only save text data, but also store binary data in any format.



This article describes the binary security of php from three perspectives: 1. what is php binary security? 2. what structure ensures the binary security of php; 3. this structure also...

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.