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