xref: /PHP-8.0/ext/mysqlnd/mysqlnd_read_buffer.c (revision 5d6e923d)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | http://www.php.net/license/3_01.txt                                  |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Authors: Andrey Hristov <andrey@php.net>                             |
14   |          Ulf Wendel <uw@php.net>                                     |
15   +----------------------------------------------------------------------+
16 */
17 
18 #include "php.h"
19 #include "mysqlnd.h"
20 #include "mysqlnd_debug.h"
21 #include "mysqlnd_read_buffer.h"
22 
23 
24 /* {{{ mysqlnd_read_buffer_is_empty */
25 static zend_bool
mysqlnd_read_buffer_is_empty(const MYSQLND_READ_BUFFER * const buffer)26 mysqlnd_read_buffer_is_empty(const MYSQLND_READ_BUFFER * const buffer)
27 {
28 	return buffer->len? FALSE:TRUE;
29 }
30 /* }}} */
31 
32 
33 /* {{{ mysqlnd_read_buffer_read */
34 static void
mysqlnd_read_buffer_read(MYSQLND_READ_BUFFER * buffer,const size_t count,zend_uchar * dest)35 mysqlnd_read_buffer_read(MYSQLND_READ_BUFFER * buffer, const size_t count, zend_uchar * dest)
36 {
37 	if (buffer->len >= count) {
38 		memcpy(dest, buffer->data + buffer->offset, count);
39 		buffer->offset += count;
40 		buffer->len -= count;
41 	}
42 }
43 /* }}} */
44 
45 
46 /* {{{ mysqlnd_read_buffer_bytes_left */
47 static size_t
mysqlnd_read_buffer_bytes_left(const MYSQLND_READ_BUFFER * const buffer)48 mysqlnd_read_buffer_bytes_left(const MYSQLND_READ_BUFFER * const buffer)
49 {
50 	return buffer->len;
51 }
52 /* }}} */
53 
54 
55 /* {{{ mysqlnd_read_buffer_free */
56 static void
mysqlnd_read_buffer_free(MYSQLND_READ_BUFFER ** buffer)57 mysqlnd_read_buffer_free(MYSQLND_READ_BUFFER ** buffer)
58 {
59 	DBG_ENTER("mysqlnd_read_buffer_free");
60 	if (*buffer) {
61 		mnd_efree((*buffer)->data);
62 		mnd_efree(*buffer);
63 		*buffer = NULL;
64 	}
65 	DBG_VOID_RETURN;
66 }
67 /* }}} */
68 
69 
70 /* {{{ mysqlnd_create_read_buffer */
71 PHPAPI MYSQLND_READ_BUFFER *
mysqlnd_create_read_buffer(const size_t count)72 mysqlnd_create_read_buffer(const size_t count)
73 {
74 	MYSQLND_READ_BUFFER * ret = mnd_emalloc(sizeof(MYSQLND_READ_BUFFER));
75 	DBG_ENTER("mysqlnd_create_read_buffer");
76 	ret->is_empty = mysqlnd_read_buffer_is_empty;
77 	ret->read = mysqlnd_read_buffer_read;
78 	ret->bytes_left = mysqlnd_read_buffer_bytes_left;
79 	ret->free_buffer = mysqlnd_read_buffer_free;
80 	ret->data = mnd_emalloc(count);
81 	ret->size = ret->len = count;
82 	ret->offset = 0;
83 	DBG_RETURN(ret);
84 }
85 /* }}} */
86