Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <ipxe/xfer.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/xferbuf.h>
  30. /** @file
  31. *
  32. * Data transfer buffer
  33. *
  34. */
  35. /**
  36. * Finish using data transfer buffer
  37. *
  38. * @v xferbuf Data transfer buffer
  39. */
  40. void xferbuf_done ( struct xfer_buffer *xferbuf ) {
  41. free ( xferbuf->data );
  42. xferbuf->data = NULL;
  43. xferbuf->len = 0;
  44. xferbuf->pos = 0;
  45. }
  46. /**
  47. * Ensure that data transfer buffer is large enough for the specified size
  48. *
  49. * @v xferbuf Data transfer buffer
  50. * @v len Required minimum size
  51. * @ret rc Return status code
  52. */
  53. static int xferbuf_ensure_size ( struct xfer_buffer *xferbuf, size_t len ) {
  54. void *new_data;
  55. /* If buffer is already large enough, do nothing */
  56. if ( len <= xferbuf->len )
  57. return 0;
  58. /* Extend buffer */
  59. new_data = realloc ( xferbuf->data, len );
  60. if ( ! new_data ) {
  61. DBGC ( xferbuf, "XFERBUF %p could not extend buffer to "
  62. "%zd bytes\n", xferbuf, len );
  63. return -ENOSPC;
  64. }
  65. xferbuf->data = new_data;
  66. xferbuf->len = len;
  67. return 0;
  68. }
  69. /**
  70. * Add received data to data transfer buffer
  71. *
  72. * @v xferbuf Data transfer buffer
  73. * @v iobuf I/O buffer
  74. * @v meta Data transfer metadata
  75. * @ret rc Return status code
  76. */
  77. int xferbuf_deliver ( struct xfer_buffer *xferbuf, struct io_buffer *iobuf,
  78. struct xfer_metadata *meta ) {
  79. size_t len;
  80. size_t max;
  81. int rc;
  82. /* Calculate new buffer position */
  83. if ( meta->flags & XFER_FL_ABS_OFFSET )
  84. xferbuf->pos = 0;
  85. xferbuf->pos += meta->offset;
  86. /* Ensure that we have enough buffer space for this data */
  87. len = iob_len ( iobuf );
  88. max = ( xferbuf->pos + len );
  89. if ( ( rc = xferbuf_ensure_size ( xferbuf, max ) ) != 0 )
  90. goto done;
  91. /* Copy data to buffer */
  92. memcpy ( ( xferbuf->data + xferbuf->pos ), iobuf->data, len );
  93. /* Update current buffer position */
  94. xferbuf->pos += len;
  95. done:
  96. free_iob ( iobuf );
  97. return rc;
  98. }