DBGenerator.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (C) 2014 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
  2. * This file is part of XPIR.
  3. *
  4. * XPIR is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * XPIR is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with XPIR. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "DBGenerator.hpp"
  18. /************************************************/
  19. /* Default constructor : no splitting */
  20. /* -> 1 input file -> 1 output stream */
  21. /************************************************/
  22. DBGenerator::DBGenerator(uint64_t nbStreams, uint64_t streamBytesize, bool silent):
  23. random_engine(0), // Fixed seed of 0
  24. random_distribution()
  25. {
  26. maxFileBytesize=streamBytesize;
  27. nbFiles = 0;
  28. #ifdef SEND_CATALOG
  29. for (unsigned int i = 0 ; i < nbStreams ; i++) {
  30. std::string fileName= std::to_string(i);
  31. file_list.push_back( fileName );
  32. }
  33. #endif
  34. nbFiles = nbStreams;
  35. if(!silent)
  36. {
  37. std::cout << "DBGenerator: The size of the database is " << maxFileBytesize*nbFiles << " bytes" << std::endl;
  38. std::cout << "DBGenerator: The number of elements in the catalog is " << nbFiles << std::endl;
  39. }
  40. }
  41. DBGenerator::~DBGenerator() {}
  42. std::string DBGenerator::getCatalog(const bool typeOfCatalog) {
  43. std::string buf;
  44. if(typeOfCatalog) {
  45. // Start with the number of elements in the catalog
  46. buf = std::to_string((unsigned int)0)+ "\n";
  47. buf += std::to_string(getNbStream())+ "\n";
  48. // Then for each file contactenate (with newlines) filename and filesize
  49. for (auto f : file_list)
  50. {
  51. buf += f + "\n" + std::to_string(maxFileBytesize) + "\n";
  52. }
  53. return buf;
  54. }
  55. // else we want a compact representation, i.e. nbFiles / fileSize
  56. buf = std::to_string((unsigned int)1)+ "\n";
  57. buf += std::to_string(getNbStream())+ "\n";
  58. buf += std::to_string(maxFileBytesize)+ "\n";
  59. return buf;
  60. }
  61. //uint64_t DBGenerator::getDBSizeBits() {
  62. // return maxFileBytesize*nbFiles*8;
  63. //}
  64. uint64_t DBGenerator::getNbStream() {
  65. return nbFiles;
  66. }
  67. uint64_t DBGenerator::getmaxFileBytesize() {
  68. return maxFileBytesize;
  69. }
  70. bool DBGenerator::openStream(uint64_t streamNb, uint64_t requested_offset) {
  71. return true;
  72. }
  73. uint64_t DBGenerator::readStream(uint64_t streamNb, char * buf, uint64_t size) {
  74. //for (unsigned char i = 0xaa, j = 0; j < size; i++, j++)
  75. //{
  76. // buf[j] = i;
  77. //}
  78. //#define NDSS_SNIFFER
  79. #ifdef NDSS_SNIFFER
  80. for (int i = 0; i < size/4; i++) {
  81. buf[i<<2] = random_distribution(random_engine);
  82. }
  83. #else
  84. char ccc=0xaa;
  85. memset(buf, ccc++, size);
  86. #endif
  87. return size;
  88. }
  89. void DBGenerator::closeStream(uint64_t streamNb) {}
  90. void DBGenerator::readAggregatedStream(uint64_t streamNb, uint64_t alpha, uint64_t offset, uint64_t bytes_per_file, char* rawBits){
  91. readStream(0, 0, 0);
  92. uint64_t fileByteSize = std::min(bytes_per_file, maxFileBytesize-offset);
  93. uint64_t startStream = streamNb*alpha;
  94. uint64_t endStream = std::min(streamNb*alpha + alpha - 1, getNbStream() - 1);
  95. uint64_t paddingStreams = std::max((long long)((streamNb)*alpha+alpha) - (long long)getNbStream(), (long long)0);
  96. memset(rawBits, 0xaa, fileByteSize*(endStream-startStream + 1));
  97. if(paddingStreams !=0)
  98. {
  99. bzero(rawBits + (endStream % alpha) * fileByteSize, fileByteSize*paddingStreams);
  100. }
  101. }