Buffer.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef _BUFFER_H_
  32. #define _BUFFER_H_
  33. #include <stdint.h>
  34. #include <malloc.h>
  35. #include <memory.h>
  36. #include "aeerror.h"
  37. #include "se_wrapper.h"
  38. namespace upse
  39. {
  40. class BufferWriter;
  41. class Buffer
  42. {
  43. friend class BufferWriter;
  44. public:
  45. Buffer() : buf(NULL), size(0)
  46. {}
  47. // Needed by list<>
  48. Buffer(const Buffer& buf_) : buf(NULL), size(0)
  49. {
  50. // No error is thrown; when it fails, size and buf will be set to 0 and nullptr
  51. Clone(buf_);
  52. }
  53. ~Buffer()
  54. {
  55. if (buf != NULL)
  56. {
  57. free(buf);
  58. buf = NULL;
  59. size = 0;
  60. }
  61. }
  62. ae_error_t Alloc(uint32_t size_)
  63. {
  64. uint8_t* p = NULL;
  65. if (0 != size_)
  66. {
  67. p = (uint8_t*)calloc(1, size_);
  68. if (NULL == p)
  69. return AE_OUT_OF_MEMORY_ERROR;
  70. }
  71. if (NULL != buf)
  72. free(buf);
  73. buf = p;
  74. size = size_;
  75. return AE_SUCCESS;
  76. }
  77. ae_error_t Alloc(const uint8_t* buf_, uint32_t size_)
  78. {
  79. ae_error_t status = Alloc(size_);
  80. if (AE_SUCCEEDED(status) && size_ > 0)
  81. memcpy_s(buf, size, buf_, size_);
  82. return status;
  83. }
  84. ae_error_t Clone(const Buffer& buf_)
  85. {
  86. return Alloc(buf_.getData(), buf_.getSize());
  87. }
  88. void zeroMemory()
  89. {
  90. if (NULL != buf && size > 0)
  91. memset(buf, 0, size);
  92. }
  93. const uint8_t* getData() const
  94. {
  95. return buf;
  96. }
  97. uint32_t getSize() const
  98. {
  99. return size;
  100. }
  101. ae_error_t Not(Buffer& b) const
  102. {
  103. ae_error_t status = b.Alloc(size);
  104. if (AE_SUCCEEDED(status) && size > 0)
  105. {
  106. for (uint32_t i = 0; i < b.size; i++)
  107. {
  108. b.buf[i] = (uint8_t)~buf[i];
  109. }
  110. }
  111. return status;
  112. }
  113. protected:
  114. uint8_t* buf;
  115. uint32_t size;
  116. private:
  117. Buffer& operator=(const Buffer &that);
  118. Buffer operator~() const;
  119. };
  120. #if 0
  121. inline bool operator==(const Buffer &lhs, const Buffer &rhs)
  122. {
  123. if (lhs.getSize() != rhs.getSize())
  124. {
  125. return false;
  126. }
  127. int result = memcmp(lhs.getData(), rhs.getData(), lhs.getSize());
  128. return result == 0;
  129. }
  130. inline bool operator!=(const Buffer& lhs, const Buffer& rhs)
  131. {
  132. return !(lhs == rhs);
  133. }
  134. #endif
  135. class BufferReader
  136. {
  137. public:
  138. BufferReader(const Buffer& buf_)
  139. : buf(buf_.getData()), currentPos(0), size(buf_.getSize())
  140. {}
  141. #if 0 // DEAD CODE -- disable, but keep
  142. BufferReader(const uint8_t* buf_, uint32_t size_)
  143. : buf(buf_), currentPos(0), size(size_)
  144. {}
  145. BufferReader() : buf(NULL), currentPos(0), size(0)
  146. {}
  147. #endif
  148. ae_error_t readRaw(const uint8_t** ptr)
  149. {
  150. return readRaw(size-currentPos, ptr);
  151. }
  152. ae_error_t readRaw(uint32_t numBytes, const uint8_t** ptr)
  153. {
  154. if (numBytes > size - currentPos)
  155. return AE_INSUFFICIENT_DATA_IN_BUFFER;
  156. if (NULL == ptr)
  157. return AE_INVALID_PARAMETER;
  158. *ptr = buf + currentPos;
  159. currentPos += numBytes;
  160. return AE_SUCCESS;
  161. }
  162. uint32_t getRemainingSize() const
  163. {
  164. return size - currentPos;
  165. }
  166. #if 0 // DEAD CODE -- disable, but keep
  167. //Move current position by skipSize bytes
  168. ae_error_t skip(uint32_t skipSize)
  169. {
  170. if (skipSize > size - currentPos)
  171. return AE_INSUFFICIENT_DATA_IN_BUFFER;
  172. currentPos += skipSize;
  173. return AE_SUCCESS;
  174. }
  175. uint32_t getSize() const
  176. {
  177. return size;
  178. }
  179. uint32_t getOffset() const
  180. {
  181. return currentPos;
  182. }
  183. bool isDataAvailable() const
  184. {
  185. return currentPos != size;
  186. }
  187. #endif
  188. protected:
  189. const uint8_t* buf;
  190. uint32_t currentPos;
  191. const uint32_t size;
  192. private:
  193. BufferReader& operator=(const BufferReader&);
  194. };
  195. class BufferWriter
  196. {
  197. public:
  198. BufferWriter(Buffer& buf_)
  199. : buf(buf_.buf), currentPos(0), size(buf_.size)
  200. {}
  201. #if 0 // DEAD CODE -- disable, but keep
  202. BufferWriter(uint8_t* buf_, uint32_t size_)
  203. : buf(buf_), currentPos(0), size(size_)
  204. {}
  205. BufferWriter(const BufferWriter& buf_) : buf(buf_.buf), currentPos(buf_.currentPos), size(buf_.size)
  206. {}
  207. #endif
  208. // writes specified number of bytes to the buffer at the current position
  209. // returns pointer to the position of the buffer at which write started
  210. ae_error_t writeRaw(const uint8_t* data_, uint32_t size_, uint8_t** startPtr = NULL)
  211. {
  212. if (size_ > (size - currentPos))
  213. return AE_INSUFFICIENT_DATA_IN_BUFFER;
  214. uint8_t* ptr = buf + currentPos;
  215. memcpy_s(ptr, (size-currentPos), data_, size_);
  216. currentPos += size_;
  217. if (NULL != startPtr)
  218. *startPtr = ptr;
  219. return AE_SUCCESS;
  220. }
  221. #if 0 // DEAD CODE -- disable, but keep
  222. ae_error_t write(BufferReader& reader, uint32_t numBytes)
  223. {
  224. const uint8_t* p;
  225. ae_error_t status = reader.readRaw(&p);
  226. if (AE_FAILED(status))
  227. return status;
  228. return writeRaw(p, numBytes);
  229. }
  230. #endif
  231. ae_error_t reserve(uint32_t size_, uint8_t** ptr)
  232. {
  233. if (NULL == ptr)
  234. return AE_FAILURE;
  235. if (size_ > (size - currentPos))
  236. return AE_INSUFFICIENT_DATA_IN_BUFFER;
  237. *ptr = buf + currentPos;
  238. currentPos += size_;
  239. return AE_SUCCESS;
  240. }
  241. protected:
  242. uint8_t* buf;
  243. uint32_t currentPos;
  244. const uint32_t size;
  245. private:
  246. BufferWriter& operator=(const BufferWriter&);
  247. BufferWriter& operator<< (BufferReader& reader);
  248. BufferWriter& operator<< (const BufferReader& reader);
  249. BufferWriter& operator<< (const Buffer& buffer);
  250. };
  251. }
  252. #endif