ByteBuffer.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #ifdef _WIN32
  2. #include "stdafx.h"
  3. #endif
  4. #include "ByteBuffer.h"
  5. #ifdef BB_USE_NS
  6. namespace bb {
  7. #endif
  8. /**
  9. * ByteBuffer constructor
  10. * Reserves specified size in internal vector
  11. *
  12. * @param size Size (in bytes) of space to preallocate internally. Default is set in DEFAULT_SIZE
  13. */
  14. ByteBuffer::ByteBuffer(uint32_t size) {
  15. buf.reserve(size);
  16. clear();
  17. #ifdef BB_UTILITY
  18. name = "";
  19. #endif
  20. }
  21. /**
  22. * ByteBuffer constructor
  23. * Consume an entire uint8_t array of length len in the ByteBuffer
  24. *
  25. * @param arr uint8_t array of data (should be of length len)
  26. * @param size Size of space to allocate
  27. */
  28. ByteBuffer::ByteBuffer(uint8_t* arr, uint32_t size) {
  29. // If the provided array is NULL, allocate a blank buffer of the provided size
  30. if (arr == NULL) {
  31. buf.reserve(size);
  32. clear();
  33. } else { // Consume the provided array
  34. buf.reserve(size);
  35. clear();
  36. putBytes(arr, size);
  37. }
  38. #ifdef BB_UTILITY
  39. name = "";
  40. #endif
  41. }
  42. /**
  43. * Bytes Remaining
  44. * Returns the number of bytes from the current read position till the end of the buffer
  45. *
  46. * @return Number of bytes from rpos to the end (size())
  47. */
  48. uint32_t ByteBuffer::bytesRemaining() {
  49. return size() - rpos;
  50. }
  51. /**
  52. * Clear
  53. * Clears out all data from the internal vector (original preallocated size remains), resets the positions to 0
  54. */
  55. void ByteBuffer::clear() {
  56. rpos = 0;
  57. wpos = 0;
  58. buf.clear();
  59. }
  60. /**
  61. * Clone
  62. * Allocate an exact copy of the ByteBuffer on the heap and return a pointer
  63. *
  64. * @return A pointer to the newly cloned ByteBuffer. NULL if no more memory available
  65. */
  66. ByteBuffer* ByteBuffer::clone() {
  67. ByteBuffer* ret = new ByteBuffer(buf.size());
  68. // Copy data
  69. for (uint32_t i = 0; i < buf.size(); i++) {
  70. ret->put((uint8_t) get(i));
  71. }
  72. // Reset positions
  73. ret->setReadPos(0);
  74. ret->setWritePos(0);
  75. return ret;
  76. }
  77. /**
  78. * Equals, test for data equivilancy
  79. * Compare this ByteBuffer to another by looking at each byte in the internal buffers and making sure they are the same
  80. *
  81. * @param other A pointer to a ByteBuffer to compare to this one
  82. * @return True if the internal buffers match. False if otherwise
  83. */
  84. bool ByteBuffer::equals(ByteBuffer* other) {
  85. // If sizes aren't equal, they can't be equal
  86. if (size() != other->size())
  87. return false;
  88. // Compare byte by byte
  89. uint32_t len = size();
  90. for (uint32_t i = 0; i < len; i++) {
  91. if ((uint8_t) get(i) != (uint8_t) other->get(i))
  92. return false;
  93. }
  94. return true;
  95. }
  96. /**
  97. * Resize
  98. * Reallocates memory for the internal buffer of size newSize. Read and write positions will also be reset
  99. *
  100. * @param newSize The amount of memory to allocate
  101. */
  102. void ByteBuffer::resize(uint32_t newSize) {
  103. buf.resize(newSize);
  104. rpos = 0;
  105. wpos = 0;
  106. }
  107. /**
  108. * Size
  109. * Returns the size of the internal buffer...not necessarily the length of bytes used as data!
  110. *
  111. * @return size of the internal buffer
  112. */
  113. uint32_t ByteBuffer::size() {
  114. return buf.size();
  115. }
  116. // Replacement
  117. /**
  118. * Replace
  119. * Replace occurance of a particular uint8_t, key, with the uint8_t rep
  120. *
  121. * @param key uint8_t to find for replacement
  122. * @param rep uint8_t to replace the found key with
  123. * @param start Index to start from. By default, start is 0
  124. * @param firstOccuranceOnly If true, only replace the first occurance of the key. If false, replace all occurances. False by default
  125. */
  126. void ByteBuffer::replace(uint8_t key, uint8_t rep, uint32_t start, bool firstOccuranceOnly) {
  127. uint32_t len = buf.size();
  128. for (uint32_t i = start; i < len; i++) {
  129. uint8_t data = read<uint8_t>(i);
  130. // Wasn't actually found, bounds of buffer were exceeded
  131. if ((key != 0) && (data == 0))
  132. break;
  133. // Key was found in array, perform replacement
  134. if (data == key) {
  135. buf[i] = rep;
  136. if (firstOccuranceOnly)
  137. return;
  138. }
  139. }
  140. }
  141. // Read Functions
  142. uint8_t ByteBuffer::peek() const {
  143. return read<uint8_t>(rpos);
  144. }
  145. uint8_t ByteBuffer::get() const {
  146. return read<uint8_t>();
  147. }
  148. uint8_t ByteBuffer::get(uint32_t index) const {
  149. return read<uint8_t>(index);
  150. }
  151. void ByteBuffer::getBytes(uint8_t* buf, uint32_t len) const {
  152. for (uint32_t i = 0; i < len; i++) {
  153. buf[i] = read<uint8_t>();
  154. }
  155. }
  156. char ByteBuffer::getChar() const {
  157. return read<char>();
  158. }
  159. char ByteBuffer::getChar(uint32_t index) const {
  160. return read<char>(index);
  161. }
  162. double ByteBuffer::getDouble() const {
  163. return read<double>();
  164. }
  165. double ByteBuffer::getDouble(uint32_t index) const {
  166. return read<double>(index);
  167. }
  168. float ByteBuffer::getFloat() const {
  169. return read<float>();
  170. }
  171. float ByteBuffer::getFloat(uint32_t index) const {
  172. return read<float>(index);
  173. }
  174. uint32_t ByteBuffer::getInt() const {
  175. return read<uint32_t>();
  176. }
  177. uint32_t ByteBuffer::getInt(uint32_t index) const {
  178. return read<uint32_t>(index);
  179. }
  180. uint64_t ByteBuffer::getLong() const {
  181. return read<uint64_t>();
  182. }
  183. uint64_t ByteBuffer::getLong(uint32_t index) const {
  184. return read<uint64_t>(index);
  185. }
  186. uint16_t ByteBuffer::getShort() const {
  187. return read<uint16_t>();
  188. }
  189. uint16_t ByteBuffer::getShort(uint32_t index) const {
  190. return read<uint16_t>(index);
  191. }
  192. // Write Functions
  193. void ByteBuffer::put(ByteBuffer* src) {
  194. uint32_t len = src->size();
  195. for (uint32_t i = 0; i < len; i++)
  196. append<uint8_t>(src->get(i));
  197. }
  198. void ByteBuffer::put(uint8_t b) {
  199. append<uint8_t>(b);
  200. }
  201. void ByteBuffer::put(uint8_t b, uint32_t index) {
  202. insert<uint8_t>(b, index);
  203. }
  204. void ByteBuffer::putBytes(uint8_t* b, uint32_t len) {
  205. // Insert the data one byte at a time into the internal buffer at position i+starting index
  206. for (uint32_t i = 0; i < len; i++)
  207. append<uint8_t>(b[i]);
  208. }
  209. void ByteBuffer::putBytes(uint8_t* b, uint32_t len, uint32_t index) {
  210. wpos = index;
  211. // Insert the data one byte at a time into the internal buffer at position i+starting index
  212. for (uint32_t i = 0; i < len; i++)
  213. append<uint8_t>(b[i]);
  214. }
  215. void ByteBuffer::putChar(char value) {
  216. append<char>(value);
  217. }
  218. void ByteBuffer::putChar(char value, uint32_t index) {
  219. insert<char>(value, index);
  220. }
  221. void ByteBuffer::putDouble(double value) {
  222. append<double>(value);
  223. }
  224. void ByteBuffer::putDouble(double value, uint32_t index) {
  225. insert<double>(value, index);
  226. }
  227. void ByteBuffer::putFloat(float value) {
  228. append<float>(value);
  229. }
  230. void ByteBuffer::putFloat(float value, uint32_t index) {
  231. insert<float>(value, index);
  232. }
  233. void ByteBuffer::putInt(uint32_t value) {
  234. append<uint32_t>(value);
  235. }
  236. void ByteBuffer::putInt(uint32_t value, uint32_t index) {
  237. insert<uint32_t>(value, index);
  238. }
  239. void ByteBuffer::putLong(uint64_t value) {
  240. append<uint64_t>(value);
  241. }
  242. void ByteBuffer::putLong(uint64_t value, uint32_t index) {
  243. insert<uint64_t>(value, index);
  244. }
  245. void ByteBuffer::putShort(uint16_t value) {
  246. append<uint16_t>(value);
  247. }
  248. void ByteBuffer::putShort(uint16_t value, uint32_t index) {
  249. insert<uint16_t>(value, index);
  250. }
  251. // Utility Functions
  252. #ifdef BB_UTILITY
  253. void ByteBuffer::setName(std::string n) {
  254. name = n;
  255. }
  256. std::string ByteBuffer::getName() {
  257. return name;
  258. }
  259. void ByteBuffer::printInfo() {
  260. uint32_t length = buf.size();
  261. std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". Info Print" << std::endl;
  262. }
  263. void ByteBuffer::printAH() {
  264. uint32_t length = buf.size();
  265. std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". ASCII & Hex Print" << std::endl;
  266. for (uint32_t i = 0; i < length; i++) {
  267. std::printf("0x%02x ", buf[i]);
  268. }
  269. std::printf("\n");
  270. for (uint32_t i = 0; i < length; i++) {
  271. std::printf("%c ", buf[i]);
  272. }
  273. std::printf("\n");
  274. }
  275. void ByteBuffer::printAscii() {
  276. uint32_t length = buf.size();
  277. std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". ASCII Print" << std::endl;
  278. for (uint32_t i = 0; i < length; i++) {
  279. std::printf("%c ", buf[i]);
  280. }
  281. std::printf("\n");
  282. }
  283. void ByteBuffer::printHex() {
  284. uint32_t length = buf.size();
  285. std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << ". Hex Print" << std::endl;
  286. for (uint32_t i = 0; i < length; i++) {
  287. std::printf("0x%02x ", buf[i]);
  288. }
  289. std::printf("\n");
  290. }
  291. void ByteBuffer::printPosition() {
  292. uint32_t length = buf.size();
  293. std::cout << "ByteBuffer " << name.c_str() << " Length: " << length << " Read Pos: " << rpos << ". Write Pos: "
  294. << wpos << std::endl;
  295. }
  296. #ifdef BB_USE_NS
  297. }
  298. #endif
  299. #endif