Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
3.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //
  9. // MessageBuffer - handy for serializing/unserializing
  10. // structures to be sent as messages
  11. // dal - 9/2002
  12. //
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "messbuf.h"
  16. #include "tier1/strtools.h"
  17. ///////////////////////////
  18. //
  19. //
  20. //
  21. MessageBuffer::MessageBuffer()
  22. {
  23. size = DEFAULT_MESSAGE_BUFFER_SIZE;
  24. data = (char *) malloc(size);
  25. len = 0;
  26. offset = 0;
  27. }
  28. ///////////////////////////
  29. //
  30. //
  31. //
  32. MessageBuffer::MessageBuffer(int minsize)
  33. {
  34. size = minsize;
  35. data = (char *) malloc(size);
  36. len = 0;
  37. offset = 0;
  38. }
  39. ///////////////////////////
  40. //
  41. //
  42. //
  43. MessageBuffer::~MessageBuffer()
  44. {
  45. free(data);
  46. }
  47. ///////////////////////////
  48. //
  49. //
  50. //
  51. int
  52. MessageBuffer::getSize()
  53. {
  54. return size;
  55. }
  56. ///////////////////////////
  57. //
  58. //
  59. //
  60. int
  61. MessageBuffer::getLen()
  62. {
  63. return len;
  64. }
  65. ///////////////////////////
  66. //
  67. //
  68. //
  69. int
  70. MessageBuffer::setLen(int nlen)
  71. {
  72. if (nlen < 0) return -1;
  73. if (nlen > size) {
  74. resize(nlen);
  75. }
  76. int res = len;
  77. len = nlen;
  78. return res;
  79. }
  80. ///////////////////////////
  81. //
  82. //
  83. //
  84. int
  85. MessageBuffer::getOffset()
  86. {
  87. return offset;
  88. }
  89. ///////////////////////////
  90. //
  91. //
  92. //
  93. int
  94. MessageBuffer::setOffset(int noffset)
  95. {
  96. if (noffset < 0 || noffset > len) return -1;
  97. int res = offset;
  98. offset = noffset;
  99. return res;
  100. }
  101. ///////////////////////////
  102. //
  103. //
  104. //
  105. int
  106. MessageBuffer::write(void const * p, int bytes)
  107. {
  108. if (bytes + len > size) {
  109. resize(bytes + len);
  110. }
  111. memcpy(data + len, p, bytes);
  112. int res = len;
  113. len += bytes;
  114. return res;
  115. }
  116. ///////////////////////////
  117. //
  118. //
  119. //
  120. int
  121. MessageBuffer::update(int loc, void const * p, int bytes)
  122. {
  123. if (loc + bytes > size) {
  124. resize(loc + bytes);
  125. }
  126. memcpy(data + loc, p, bytes);
  127. if (len < loc + bytes) {
  128. len = loc + bytes;
  129. }
  130. return len;
  131. }
  132. ///////////////////////////
  133. //
  134. //
  135. //
  136. int
  137. MessageBuffer::extract(int loc, void * p, int bytes)
  138. {
  139. if (loc + bytes > len) return -1;
  140. memcpy(p, data + loc, bytes);
  141. return loc + bytes;
  142. }
  143. ///////////////////////////
  144. //
  145. //
  146. //
  147. int
  148. MessageBuffer::read(void * p, int bytes)
  149. {
  150. if (offset + bytes > len) return -1;
  151. memcpy(p, data + offset, bytes);
  152. offset += bytes;
  153. return offset;
  154. }
  155. int MessageBuffer::WriteString( const char *pString )
  156. {
  157. return write( pString, V_strlen( pString ) + 1 );
  158. }
  159. int MessageBuffer::ReadString( char *pOut, int bufferLength )
  160. {
  161. int nChars = 0;
  162. while ( 1 )
  163. {
  164. char ch;
  165. if ( read( &ch, sizeof( ch ) ) == -1 )
  166. {
  167. pOut[0] = 0;
  168. return -1;
  169. }
  170. if ( ch == 0 || nChars >= (bufferLength-1) )
  171. break;
  172. pOut[nChars] = ch;
  173. ++nChars;
  174. }
  175. pOut[nChars] = 0;
  176. return nChars + 1;
  177. }
  178. ///////////////////////////
  179. //
  180. //
  181. //
  182. void
  183. MessageBuffer::clear()
  184. {
  185. memset(data, 0, size);
  186. offset = 0;
  187. len = 0;
  188. }
  189. ///////////////////////////
  190. //
  191. //
  192. //
  193. void
  194. MessageBuffer::clear(int minsize)
  195. {
  196. if (minsize > size) {
  197. resize(minsize);
  198. }
  199. memset(data, 0, size);
  200. offset = 0;
  201. len = 0;
  202. }
  203. ///////////////////////////
  204. //
  205. //
  206. //
  207. void
  208. MessageBuffer::reset(int minsize)
  209. {
  210. if (minsize > size) {
  211. resize(minsize);
  212. }
  213. len = 0;
  214. offset = 0;
  215. }
  216. ///////////////////////////
  217. //
  218. //
  219. //
  220. void
  221. MessageBuffer::resize(int minsize)
  222. {
  223. if (minsize < size) return;
  224. if (size * 2 > minsize) minsize = size * 2;
  225. char * odata = data;
  226. data = (char *) malloc(minsize);
  227. memcpy(data, odata, len);
  228. size = minsize;
  229. free(odata);
  230. }
  231. ///////////////////////////
  232. //
  233. //
  234. void
  235. MessageBuffer::print(FILE * ofile, int num)
  236. {
  237. fprintf(ofile, "Len: %d Offset: %d Size: %d\n", len, offset, size);
  238. if (num > size) num = size;
  239. for (int i=0; i<num; ++i) {
  240. fprintf(ofile, "%02x ", (unsigned char) data[i]);
  241. }
  242. fprintf(ofile, "\n");
  243. }