You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xferbuf.c 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <ipxe/xfer.h>
  24. #include <ipxe/iobuf.h>
  25. #include <ipxe/xferbuf.h>
  26. /** @file
  27. *
  28. * Data transfer buffer
  29. *
  30. */
  31. /**
  32. * Finish using data transfer buffer
  33. *
  34. * @v xferbuf Data transfer buffer
  35. */
  36. void xferbuf_done ( struct xfer_buffer *xferbuf ) {
  37. free ( xferbuf->data );
  38. xferbuf->data = NULL;
  39. xferbuf->len = 0;
  40. xferbuf->pos = 0;
  41. }
  42. /**
  43. * Ensure that data transfer buffer is large enough for the specified size
  44. *
  45. * @v xferbuf Data transfer buffer
  46. * @v len Required minimum size
  47. * @ret rc Return status code
  48. */
  49. static int xferbuf_ensure_size ( struct xfer_buffer *xferbuf, size_t len ) {
  50. void *new_data;
  51. /* If buffer is already large enough, do nothing */
  52. if ( len <= xferbuf->len )
  53. return 0;
  54. /* Extend buffer */
  55. new_data = realloc ( xferbuf->data, len );
  56. if ( ! new_data ) {
  57. DBGC ( xferbuf, "XFERBUF %p could not extend buffer to "
  58. "%zd bytes\n", xferbuf, len );
  59. return -ENOSPC;
  60. }
  61. xferbuf->data = new_data;
  62. xferbuf->len = len;
  63. return 0;
  64. }
  65. /**
  66. * Add received data to data transfer buffer
  67. *
  68. * @v xferbuf Data transfer buffer
  69. * @v iobuf I/O buffer
  70. * @v meta Data transfer metadata
  71. * @ret rc Return status code
  72. */
  73. int xferbuf_deliver ( struct xfer_buffer *xferbuf, struct io_buffer *iobuf,
  74. struct xfer_metadata *meta ) {
  75. size_t len;
  76. size_t max;
  77. int rc;
  78. /* Calculate new buffer position */
  79. if ( meta->flags & XFER_FL_ABS_OFFSET )
  80. xferbuf->pos = 0;
  81. xferbuf->pos += meta->offset;
  82. /* Ensure that we have enough buffer space for this data */
  83. len = iob_len ( iobuf );
  84. max = ( xferbuf->pos + len );
  85. if ( ( rc = xferbuf_ensure_size ( xferbuf, max ) ) != 0 )
  86. goto done;
  87. /* Copy data to buffer */
  88. memcpy ( ( xferbuf->data + xferbuf->pos ), iobuf->data, len );
  89. /* Update current buffer position */
  90. xferbuf->pos += len;
  91. done:
  92. free_iob ( iobuf );
  93. return rc;
  94. }