bio_writer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2012 Tobias Brunner
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * Copyright (C) 2010 Martin Willi
  6. * Copyright (C) 2010 revosec AG
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. */
  18. /**
  19. * @defgroup bio_writer bio_writer
  20. * @{ @ingroup bio
  21. */
  22. #ifndef BIO_WRITER_H_
  23. #define BIO_WRITER_H_
  24. typedef struct bio_writer_t bio_writer_t;
  25. #include <library.h>
  26. /**
  27. * Buffered output generator.
  28. *
  29. * @note Integers are converted to network byte order before writing.
  30. */
  31. struct bio_writer_t {
  32. /**
  33. * Append a 8-bit integer to the buffer.
  34. *
  35. * @param value value to append
  36. */
  37. void (*write_uint8)(bio_writer_t *this, uint8_t value);
  38. /**
  39. * Append a 16-bit integer to the buffer.
  40. *
  41. * @param value value to append
  42. */
  43. void (*write_uint16)(bio_writer_t *this, uint16_t value);
  44. /**
  45. * Append a 24-bit integer to the buffer.
  46. *
  47. * @param value value to append
  48. */
  49. void (*write_uint24)(bio_writer_t *this, uint32_t value);
  50. /**
  51. * Append a 32-bit integer to the buffer.
  52. *
  53. * @param value value to append
  54. */
  55. void (*write_uint32)(bio_writer_t *this, uint32_t value);
  56. /**
  57. * Append a 64-bit integer to the buffer.
  58. *
  59. * @param value value to append
  60. */
  61. void (*write_uint64)(bio_writer_t *this, uint64_t value);
  62. /**
  63. * Append a chunk of data without a length header.
  64. *
  65. * @param value value to append
  66. */
  67. void (*write_data)(bio_writer_t *this, chunk_t value);
  68. /**
  69. * Append a chunk of data with a 8-bit length header.
  70. *
  71. * @param value value to append
  72. */
  73. void (*write_data8)(bio_writer_t *this, chunk_t value);
  74. /**
  75. * Append a chunk of data with a 16-bit length header.
  76. *
  77. * @param value value to append
  78. */
  79. void (*write_data16)(bio_writer_t *this, chunk_t value);
  80. /**
  81. * Append a chunk of data with a 24-bit length header.
  82. *
  83. * @param value value to append
  84. */
  85. void (*write_data24)(bio_writer_t *this, chunk_t value);
  86. /**
  87. * Append a chunk of data with a 32-bit length header.
  88. *
  89. * @param value value to append
  90. */
  91. void (*write_data32)(bio_writer_t *this, chunk_t value);
  92. /**
  93. * Prepend a 8-bit length header to existing data.
  94. */
  95. void (*wrap8)(bio_writer_t *this);
  96. /**
  97. * Prepend a 16-bit length header to existing data.
  98. */
  99. void (*wrap16)(bio_writer_t *this);
  100. /**
  101. * Prepend a 24-bit length header to existing data.
  102. */
  103. void (*wrap24)(bio_writer_t *this);
  104. /**
  105. * Prepend a 32-bit length header to existing data.
  106. */
  107. void (*wrap32)(bio_writer_t *this);
  108. /**
  109. * Skips len bytes in the buffer, return chunk of skipped data.
  110. *
  111. * The returned chunk is not valid after calling any other writer function
  112. * (except get_buf()), because a buffer reallocation might move the
  113. * internal buffer to a different memory location!
  114. *
  115. * @param len number of bytes to skip
  116. * @return chunk pointing to skipped bytes in the internal buffer
  117. */
  118. chunk_t (*skip)(bio_writer_t *this, size_t len);
  119. /**
  120. * Get the encoded data buffer.
  121. *
  122. * @return chunk to internal buffer
  123. */
  124. chunk_t (*get_buf)(bio_writer_t *this);
  125. /**
  126. * Return the encoded data buffer and detach it from the writer (resets
  127. * the internal buffer).
  128. *
  129. * @return chunk to internal buffer (has to be freed)
  130. */
  131. chunk_t (*extract_buf)(bio_writer_t *this);
  132. /**
  133. * Destroy a bio_writer_t.
  134. */
  135. void (*destroy)(bio_writer_t *this);
  136. };
  137. /**
  138. * Create a bio_writer instance.
  139. *
  140. * The size of the internal buffer is increased automatically by bufsize (or a
  141. * default if not given) if the initial size does not suffice.
  142. *
  143. * @param bufsize initially allocated buffer size
  144. */
  145. bio_writer_t *bio_writer_create(uint32_t bufsize);
  146. #endif /** BIO_WRITER_H_ @}*/