FuzzerIO.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //===- FuzzerIO.cpp - IO utils. -------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // IO functions.
  10. //===----------------------------------------------------------------------===//
  11. #include "FuzzerInternal.h"
  12. #include <iterator>
  13. #include <fstream>
  14. #include <dirent.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #include <cstdio>
  19. // //
  20. ///////////////////////////////////////////////////////////////////////////////
  21. namespace fuzzer {
  22. static long GetEpoch(const std::string &Path) {
  23. struct stat St;
  24. if (stat(Path.c_str(), &St)) return 0;
  25. return St.st_mtime;
  26. }
  27. static std::vector<std::string> ListFilesInDir(const std::string &Dir,
  28. long *Epoch) {
  29. std::vector<std::string> V;
  30. if (Epoch) {
  31. auto E = GetEpoch(Dir.c_str());
  32. if (*Epoch >= E) return V;
  33. *Epoch = E;
  34. }
  35. DIR *D = opendir(Dir.c_str());
  36. if (!D) return V;
  37. while (auto E = readdir(D)) {
  38. if (E->d_type == DT_REG || E->d_type == DT_LNK)
  39. V.push_back(E->d_name);
  40. }
  41. closedir(D);
  42. return V;
  43. }
  44. Unit FileToVector(const std::string &Path) {
  45. std::ifstream T(Path);
  46. return Unit((std::istreambuf_iterator<char>(T)),
  47. std::istreambuf_iterator<char>());
  48. }
  49. std::string FileToString(const std::string &Path) {
  50. std::ifstream T(Path);
  51. return std::string((std::istreambuf_iterator<char>(T)),
  52. std::istreambuf_iterator<char>());
  53. }
  54. void CopyFileToErr(const std::string &Path) {
  55. Printf("%s", FileToString(Path).c_str());
  56. }
  57. void WriteToFile(const Unit &U, const std::string &Path) {
  58. std::ofstream OF(Path);
  59. OF.write((const char*)U.data(), U.size());
  60. }
  61. void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
  62. long *Epoch) {
  63. long E = Epoch ? *Epoch : 0;
  64. for (auto &X : ListFilesInDir(Path, Epoch)) {
  65. auto FilePath = DirPlusFile(Path, X);
  66. if (Epoch && GetEpoch(FilePath) < E) continue;
  67. V->push_back(FileToVector(FilePath));
  68. }
  69. }
  70. std::string DirPlusFile(const std::string &DirPath,
  71. const std::string &FileName) {
  72. return DirPath + "/" + FileName;
  73. }
  74. void PrintFileAsBase64(const std::string &Path) {
  75. std::string Cmd = "base64 -w 0 < " + Path + "; echo";
  76. ExecuteCommand(Cmd);
  77. }
  78. void Printf(const char *Fmt, ...) {
  79. va_list ap;
  80. va_start(ap, Fmt);
  81. vfprintf(stderr, Fmt, ap);
  82. va_end(ap);
  83. }
  84. } // namespace fuzzer