TableSerializer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "TableSerializer.h"
  11. #include "DS_Table.h"
  12. #include "BitStream.h"
  13. #include "StringCompressor.h"
  14. #include "RakAssert.h"
  15. using namespace RakNet;
  16. void TableSerializer::SerializeTable(DataStructures::Table *in, RakNet::BitStream *out)
  17. {
  18. DataStructures::Page<unsigned, DataStructures::Table::Row*, _TABLE_BPLUS_TREE_ORDER> *cur = in->GetRows().GetListHead();
  19. const DataStructures::List<DataStructures::Table::ColumnDescriptor> &columns=in->GetColumns();
  20. SerializeColumns(in, out);
  21. out->Write((unsigned)in->GetRows().Size());
  22. unsigned rowIndex;
  23. while (cur)
  24. {
  25. for (rowIndex=0; rowIndex < (unsigned)cur->size; rowIndex++)
  26. {
  27. SerializeRow(cur->data[rowIndex], cur->keys[rowIndex], columns, out);
  28. }
  29. cur=cur->next;
  30. }
  31. }
  32. void TableSerializer::SerializeColumns(DataStructures::Table *in, RakNet::BitStream *out)
  33. {
  34. const DataStructures::List<DataStructures::Table::ColumnDescriptor> &columns=in->GetColumns();
  35. out->Write((unsigned)columns.Size());
  36. unsigned i;
  37. for (i=0; i<columns.Size(); i++)
  38. {
  39. StringCompressor::Instance()->EncodeString(columns[i].columnName, _TABLE_MAX_COLUMN_NAME_LENGTH, out);
  40. out->Write((unsigned char)columns[i].columnType);
  41. }
  42. }
  43. void TableSerializer::SerializeColumns(DataStructures::Table *in, RakNet::BitStream *out, DataStructures::List<int> &skipColumnIndices)
  44. {
  45. const DataStructures::List<DataStructures::Table::ColumnDescriptor> &columns=in->GetColumns();
  46. out->Write((unsigned)columns.Size()-skipColumnIndices.Size());
  47. unsigned i;
  48. for (i=0; i<columns.Size(); i++)
  49. {
  50. if (skipColumnIndices.GetIndexOf(i)==(unsigned)-1)
  51. {
  52. StringCompressor::Instance()->EncodeString(columns[i].columnName, _TABLE_MAX_COLUMN_NAME_LENGTH, out);
  53. out->Write((unsigned char)columns[i].columnType);
  54. }
  55. }
  56. }
  57. bool TableSerializer::DeserializeTable(unsigned char *serializedTable, unsigned int dataLength, DataStructures::Table *out)
  58. {
  59. RakNet::BitStream in((unsigned char*) serializedTable, dataLength, false);
  60. return DeserializeTable(&in, out);
  61. }
  62. bool TableSerializer::DeserializeTable(RakNet::BitStream *in, DataStructures::Table *out)
  63. {
  64. unsigned rowSize;
  65. DeserializeColumns(in,out);
  66. if (in->Read(rowSize)==false || rowSize>100000)
  67. {
  68. RakAssert(0);
  69. return false; // Hacker crash prevention
  70. }
  71. unsigned rowIndex;
  72. for (rowIndex=0; rowIndex < rowSize; rowIndex++)
  73. {
  74. if (DeserializeRow(in, out)==false)
  75. return false;
  76. }
  77. return true;
  78. }
  79. bool TableSerializer::DeserializeColumns(RakNet::BitStream *in, DataStructures::Table *out)
  80. {
  81. unsigned columnSize;
  82. unsigned char columnType;
  83. char columnName[_TABLE_MAX_COLUMN_NAME_LENGTH];
  84. if (in->Read(columnSize)==false || columnSize > 10000)
  85. return false; // Hacker crash prevention
  86. out->Clear();
  87. unsigned i;
  88. for (i=0; i<columnSize; i++)
  89. {
  90. StringCompressor::Instance()->DecodeString(columnName, 32, in);
  91. in->Read(columnType);
  92. out->AddColumn(columnName, (DataStructures::Table::ColumnType)columnType);
  93. }
  94. return true;
  95. }
  96. void TableSerializer::SerializeRow(DataStructures::Table::Row *in, unsigned keyIn, const DataStructures::List<DataStructures::Table::ColumnDescriptor> &columns, RakNet::BitStream *out)
  97. {
  98. unsigned cellIndex;
  99. out->Write(keyIn);
  100. unsigned int columnsSize = columns.Size();
  101. out->Write(columnsSize);
  102. for (cellIndex=0; cellIndex<columns.Size(); cellIndex++)
  103. {
  104. out->Write(cellIndex);
  105. SerializeCell(out, in->cells[cellIndex], columns[cellIndex].columnType);
  106. }
  107. }
  108. void TableSerializer::SerializeRow(DataStructures::Table::Row *in, unsigned keyIn, const DataStructures::List<DataStructures::Table::ColumnDescriptor> &columns, RakNet::BitStream *out, DataStructures::List<int> &skipColumnIndices)
  109. {
  110. unsigned cellIndex;
  111. out->Write(keyIn);
  112. unsigned int numEntries=0;
  113. for (cellIndex=0; cellIndex<columns.Size(); cellIndex++)
  114. {
  115. if (skipColumnIndices.GetIndexOf(cellIndex)==(unsigned)-1)
  116. {
  117. numEntries++;
  118. }
  119. }
  120. out->Write(numEntries);
  121. for (cellIndex=0; cellIndex<columns.Size(); cellIndex++)
  122. {
  123. if (skipColumnIndices.GetIndexOf(cellIndex)==(unsigned)-1)
  124. {
  125. out->Write(cellIndex);
  126. SerializeCell(out, in->cells[cellIndex], columns[cellIndex].columnType);
  127. }
  128. }
  129. }
  130. bool TableSerializer::DeserializeRow(RakNet::BitStream *in, DataStructures::Table *out)
  131. {
  132. const DataStructures::List<DataStructures::Table::ColumnDescriptor> &columns=out->GetColumns();
  133. unsigned numEntries;
  134. DataStructures::Table::Row *row;
  135. unsigned key;
  136. if (in->Read(key)==false)
  137. return false;
  138. row=out->AddRow(key);
  139. unsigned int cnt;
  140. in->Read(numEntries);
  141. for (cnt=0; cnt<numEntries; cnt++)
  142. {
  143. unsigned cellIndex;
  144. in->Read(cellIndex);
  145. if (DeserializeCell(in, row->cells[cellIndex], columns[cellIndex].columnType)==false)
  146. {
  147. out->RemoveRow(key);
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. void TableSerializer::SerializeCell(RakNet::BitStream *out, DataStructures::Table::Cell *cell, DataStructures::Table::ColumnType columnType)
  154. {
  155. out->Write(cell->isEmpty);
  156. if (cell->isEmpty==false)
  157. {
  158. if (columnType==DataStructures::Table::NUMERIC)
  159. {
  160. out->Write(cell->i);
  161. }
  162. else if (columnType==DataStructures::Table::STRING)
  163. {
  164. StringCompressor::Instance()->EncodeString(cell->c, 65535, out);
  165. }
  166. else if (columnType==DataStructures::Table::POINTER)
  167. {
  168. out->Write(cell->ptr);
  169. }
  170. else
  171. {
  172. // Binary
  173. RakAssert(columnType==DataStructures::Table::BINARY);
  174. RakAssert(cell->i>0);
  175. unsigned binaryLength;
  176. binaryLength=(unsigned)cell->i;
  177. out->Write(binaryLength);
  178. out->WriteAlignedBytes((const unsigned char*) cell->c, (const unsigned int) cell->i);
  179. }
  180. }
  181. }
  182. bool TableSerializer::DeserializeCell(RakNet::BitStream *in, DataStructures::Table::Cell *cell, DataStructures::Table::ColumnType columnType)
  183. {
  184. bool isEmpty=false;
  185. double value;
  186. void *ptr;
  187. char tempString[65535];
  188. cell->Clear();
  189. if (in->Read(isEmpty)==false)
  190. return false;
  191. if (isEmpty==false)
  192. {
  193. if (columnType==DataStructures::Table::NUMERIC)
  194. {
  195. if (in->Read(value)==false)
  196. return false;
  197. cell->Set(value);
  198. }
  199. else if (columnType==DataStructures::Table::STRING)
  200. {
  201. if (StringCompressor::Instance()->DecodeString(tempString, 65535, in)==false)
  202. return false;
  203. cell->Set(tempString);
  204. }
  205. else if (columnType==DataStructures::Table::POINTER)
  206. {
  207. if (in->Read(ptr)==false)
  208. return false;
  209. cell->SetPtr(ptr);
  210. }
  211. else
  212. {
  213. unsigned binaryLength;
  214. // Binary
  215. RakAssert(columnType==DataStructures::Table::BINARY);
  216. if (in->Read(binaryLength)==false || binaryLength > 10000000)
  217. return false; // Sanity check to max binary cell of 10 megabytes
  218. in->AlignReadToByteBoundary();
  219. if (BITS_TO_BYTES(in->GetNumberOfUnreadBits())<(BitSize_t)binaryLength)
  220. return false;
  221. cell->Set((char*) in->GetData()+BITS_TO_BYTES(in->GetReadOffset()), (int) binaryLength);
  222. in->IgnoreBits(BYTES_TO_BITS((int) binaryLength));
  223. }
  224. }
  225. return true;
  226. }
  227. void TableSerializer::SerializeFilterQuery(RakNet::BitStream *in, DataStructures::Table::FilterQuery *query)
  228. {
  229. StringCompressor::Instance()->EncodeString(query->columnName,_TABLE_MAX_COLUMN_NAME_LENGTH,in,0);
  230. in->WriteCompressed(query->columnIndex);
  231. in->Write((unsigned char) query->operation);
  232. in->Write(query->cellValue->isEmpty);
  233. if (query->cellValue->isEmpty==false)
  234. {
  235. in->Write(query->cellValue->i);
  236. in->WriteAlignedBytesSafe((const char*)query->cellValue->c,(const unsigned int)query->cellValue->i,10000000); // Sanity check to max binary cell of 10 megabytes
  237. in->Write(query->cellValue->ptr);
  238. }
  239. }
  240. bool TableSerializer::DeserializeFilterQuery(RakNet::BitStream *out, DataStructures::Table::FilterQuery *query)
  241. {
  242. bool b;
  243. RakAssert(query->cellValue);
  244. StringCompressor::Instance()->DecodeString(query->columnName,_TABLE_MAX_COLUMN_NAME_LENGTH,out,0);
  245. out->ReadCompressed(query->columnIndex);
  246. unsigned char op;
  247. out->Read(op);
  248. query->operation=(DataStructures::Table::FilterQueryType) op;
  249. query->cellValue->Clear();
  250. b=out->Read(query->cellValue->isEmpty);
  251. if (query->cellValue->isEmpty==false)
  252. {
  253. // HACK - cellValue->i is used for integer, character, and binary data. However, for character and binary c will be 0. So use that to determine if the data was integer or not.
  254. out->Read(query->cellValue->i);
  255. unsigned int inputLength;
  256. out->ReadAlignedBytesSafeAlloc(&query->cellValue->c,inputLength,10000000); // Sanity check to max binary cell of 10 megabytes
  257. if (query->cellValue->c)
  258. query->cellValue->i=inputLength;
  259. b=out->Read(query->cellValue->ptr);
  260. }
  261. return b;
  262. }
  263. void TableSerializer::SerializeFilterQueryList(RakNet::BitStream *in, DataStructures::Table::FilterQuery *query, unsigned int numQueries, unsigned int maxQueries)
  264. {
  265. (void) maxQueries;
  266. in->Write((bool)(query && numQueries>0));
  267. if (query==0 || numQueries<=0)
  268. return;
  269. RakAssert(numQueries<=maxQueries);
  270. in->WriteCompressed(numQueries);
  271. unsigned i;
  272. for (i=0; i < numQueries; i++)
  273. {
  274. SerializeFilterQuery(in, query);
  275. }
  276. }
  277. bool TableSerializer::DeserializeFilterQueryList(RakNet::BitStream *out, DataStructures::Table::FilterQuery **query, unsigned int *numQueries, unsigned int maxQueries, int allocateExtraQueries)
  278. {
  279. bool b, anyQueries=false;
  280. out->Read(anyQueries);
  281. if (anyQueries==false)
  282. {
  283. if (allocateExtraQueries<=0)
  284. *query=0;
  285. else
  286. *query=new DataStructures::Table::FilterQuery[allocateExtraQueries];
  287. *numQueries=0;
  288. return true;
  289. }
  290. b=out->ReadCompressed(*numQueries);
  291. if (*numQueries>maxQueries)
  292. {
  293. RakAssert(0);
  294. *numQueries=maxQueries;
  295. }
  296. if (*numQueries==0)
  297. return b;
  298. *query=new DataStructures::Table::FilterQuery[*numQueries+allocateExtraQueries];
  299. DataStructures::Table::FilterQuery *queryPtr = *query;
  300. unsigned i;
  301. for (i=0; i < *numQueries; i++)
  302. {
  303. queryPtr[i].cellValue=new DataStructures::Table::Cell;
  304. b=DeserializeFilterQuery(out, queryPtr+i);
  305. }
  306. return b;
  307. }
  308. void TableSerializer::DeallocateQueryList(DataStructures::Table::FilterQuery *query, unsigned int numQueries)
  309. {
  310. if (query==0 || numQueries==0)
  311. return;
  312. unsigned i;
  313. for (i=0; i < numQueries; i++)
  314. RakNet::OP_DELETE(query[i].cellValue, _FILE_AND_LINE_);
  315. RakNet::OP_DELETE_ARRAY(query, _FILE_AND_LINE_);
  316. }
粤ICP备19079148号