ServerService.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "ServerService.hpp"
  18. const unsigned int ServerService::kPIRParamsOptions = 2;
  19. int ServerService::writePIRParameters(const PIRParameters& pir_params, const std::string& file_path)
  20. {
  21. std::ofstream file(file_path);
  22. if(!file.is_open()) return 1;
  23. file << "d " << pir_params.d << std::endl;
  24. file << "dim ";
  25. for (unsigned int i = 0; i < pir_params.d ; i++) file << std::to_string(pir_params.n[i]) << " ";
  26. file << std::endl;
  27. file << pir_params.crypto_params << std::endl;
  28. file.close();
  29. return 0;
  30. }
  31. int ServerService::readPIRParameters(PIRParameters& pir_params, const std::string& file_path)
  32. {
  33. std::ifstream file(file_path);
  34. if (!file.is_open()) return 1;
  35. std::string line;
  36. std::vector<std::string> fields;
  37. std::getline(file, line);
  38. boost::algorithm::split(fields, line, boost::algorithm::is_any_of(" "));
  39. pir_params.d = atoi(fields.at(1).c_str());
  40. if (file.eof()) return 1;
  41. std::getline(file, line);
  42. boost::algorithm::split(fields, line, boost::algorithm::is_any_of(" "));
  43. unsigned int i;
  44. for (i = 0 ; i < pir_params.d && i < fields.size() - 1 ; i++)
  45. {
  46. pir_params.n[i] = atoi(fields.at(i+1).c_str());
  47. }
  48. if (i != pir_params.d) return 1;
  49. std::getline(file, line);
  50. pir_params.crypto_params = line;
  51. file.close();
  52. return 0;
  53. }