ByteBuffer.cpp 8.0 KB

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