mysqlnd_read_buffer.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. | https://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. #include "php.h"
  18. #include "mysqlnd.h"
  19. #include "mysqlnd_debug.h"
  20. #include "mysqlnd_read_buffer.h"
  21. /* {{{ mysqlnd_read_buffer_is_empty */
  22. static bool
  23. mysqlnd_read_buffer_is_empty(const MYSQLND_READ_BUFFER * const buffer)
  24. {
  25. return buffer->len? FALSE:TRUE;
  26. }
  27. /* }}} */
  28. /* {{{ mysqlnd_read_buffer_read */
  29. static void
  30. mysqlnd_read_buffer_read(MYSQLND_READ_BUFFER * buffer, const size_t count, zend_uchar * dest)
  31. {
  32. if (buffer->len >= count) {
  33. memcpy(dest, buffer->data + buffer->offset, count);
  34. buffer->offset += count;
  35. buffer->len -= count;
  36. }
  37. }
  38. /* }}} */
  39. /* {{{ mysqlnd_read_buffer_bytes_left */
  40. static size_t
  41. mysqlnd_read_buffer_bytes_left(const MYSQLND_READ_BUFFER * const buffer)
  42. {
  43. return buffer->len;
  44. }
  45. /* }}} */
  46. /* {{{ mysqlnd_read_buffer_free */
  47. static void
  48. mysqlnd_read_buffer_free(MYSQLND_READ_BUFFER ** buffer)
  49. {
  50. DBG_ENTER("mysqlnd_read_buffer_free");
  51. if (*buffer) {
  52. mnd_efree((*buffer)->data);
  53. mnd_efree(*buffer);
  54. *buffer = NULL;
  55. }
  56. DBG_VOID_RETURN;
  57. }
  58. /* }}} */
  59. /* {{{ mysqlnd_create_read_buffer */
  60. PHPAPI MYSQLND_READ_BUFFER *
  61. mysqlnd_create_read_buffer(const size_t count)
  62. {
  63. MYSQLND_READ_BUFFER * ret = mnd_emalloc(sizeof(MYSQLND_READ_BUFFER));
  64. DBG_ENTER("mysqlnd_create_read_buffer");
  65. ret->is_empty = mysqlnd_read_buffer_is_empty;
  66. ret->read = mysqlnd_read_buffer_read;
  67. ret->bytes_left = mysqlnd_read_buffer_bytes_left;
  68. ret->free_buffer = mysqlnd_read_buffer_free;
  69. ret->data = mnd_emalloc(count);
  70. ret->size = ret->len = count;
  71. ret->offset = 0;
  72. DBG_RETURN(ret);
  73. }
  74. /* }}} */