SimpleStreamBuffer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @file SimpleStreamBuffer.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <string.h>
  23. #include <stddef.h>
  24. #include <misc/balloc.h>
  25. #include <misc/minmax.h>
  26. #include "SimpleStreamBuffer.h"
  27. static void try_output (SimpleStreamBuffer *o)
  28. {
  29. ASSERT(o->output_data_len > 0)
  30. // calculate number of bytes to output
  31. int bytes = bmin_int(o->output_data_len, o->buf_used);
  32. if (bytes == 0) {
  33. return;
  34. }
  35. // copy bytes to output
  36. memcpy(o->output_data, o->buf, bytes);
  37. // shift buffer
  38. memmove(o->buf, o->buf + bytes, o->buf_used - bytes);
  39. o->buf_used -= bytes;
  40. // forget data
  41. o->output_data_len = -1;
  42. // done
  43. StreamRecvInterface_Done(&o->output, bytes);
  44. }
  45. static void output_handler_recv (SimpleStreamBuffer *o, uint8_t *data, int data_len)
  46. {
  47. DebugObject_Access(&o->d_obj);
  48. ASSERT(o->output_data_len == -1)
  49. ASSERT(data)
  50. ASSERT(data_len > 0)
  51. // remember data
  52. o->output_data = data;
  53. o->output_data_len = data_len;
  54. try_output(o);
  55. }
  56. int SimpleStreamBuffer_Init (SimpleStreamBuffer *o, int buf_size, BPendingGroup *pg)
  57. {
  58. ASSERT(buf_size > 0)
  59. // init arguments
  60. o->buf_size = buf_size;
  61. // init output
  62. StreamRecvInterface_Init(&o->output, (StreamRecvInterface_handler_recv)output_handler_recv, o, pg);
  63. // allocate buffer
  64. if (!(o->buf = BAlloc(buf_size))) {
  65. goto fail1;
  66. }
  67. // init buffer state
  68. o->buf_used = 0;
  69. // set no output data
  70. o->output_data_len = -1;
  71. DebugObject_Init(&o->d_obj);
  72. return 1;
  73. fail1:
  74. StreamRecvInterface_Free(&o->output);
  75. return 0;
  76. }
  77. void SimpleStreamBuffer_Free (SimpleStreamBuffer *o)
  78. {
  79. DebugObject_Free(&o->d_obj);
  80. // free buffer
  81. BFree(o->buf);
  82. // free output
  83. StreamRecvInterface_Free(&o->output);
  84. }
  85. StreamRecvInterface * SimpleStreamBuffer_GetOutput (SimpleStreamBuffer *o)
  86. {
  87. DebugObject_Access(&o->d_obj);
  88. return &o->output;
  89. }
  90. int SimpleStreamBuffer_Write (SimpleStreamBuffer *o, uint8_t *data, int data_len)
  91. {
  92. DebugObject_Access(&o->d_obj);
  93. ASSERT(data_len >= 0)
  94. if (data_len > o->buf_size - o->buf_used) {
  95. return 0;
  96. }
  97. // copy to buffer
  98. memcpy(o->buf + o->buf_used, data, data_len);
  99. // update buffer state
  100. o->buf_used += data_len;
  101. // continue outputting
  102. if (o->output_data_len > 0) {
  103. try_output(o);
  104. }
  105. return 1;
  106. }