ITHare / obf

C++ Code+Data Obfuscation Library with Build-Time Polymorphism
Other
107 stars 11 forks source link

Help! How to aply the obf to my code. For the sake of learning. #58

Open Dagdelo opened 6 years ago

Dagdelo commented 6 years ago

Hi! I'm trying to apply the same obfuscation in the obftest.cpp in this simple Fibonacci Recursive with Memoisation.

#include <iostream>
#include <map>
#include "../../kscope/test/lest.hpp"
#include "../src/obf.h"

#ifdef ITHARE_OBF_TEST_NO_NAMESPACE
using namespace ithare::obf;
using namespace ithare::obf::tls;
#else
#define ITOBF ithare::obf::
#endif

class MyException {
public:
  MyException(std::string msg)
    : message(msg) {
  }
  virtual const char* what() const {
    return message.c_str();
  }

private:
  std::string message;
};

ITHARE_OBF_NOINLINE OBFI6(int) fibonacci(OBFI6(int) num)
{
  static std::map<OBFI6(int), OBFI6(int)> cache{{0, 0}, {1, 1}};

  auto found = cache.find(num);
  if (found != std::end(cache)) {
    // For debugging purposes, to check that the cache is doing something
    std::cout << OBF5S("Found in cache: ") << num << OBF5S(" -> ") << found->second << '\n';
    return found->second;
  }

  OBFI6(int) result = fibonacci(num - 1) + fibonacci(num - 2);
  cache[num] = result;
  return result;
}

int main()
{
  OBFI6(int) num;
  do{
    std::cout << "i: ";
    std::cin >> num;
  }while(num < 3);

  OBFI6(int) res = fibonacci(num);
  std::cout << num << OBF5S("th fibonacci number: ") << res << std::endl;
}

I stoped here... Rly don't know how to finish the code for trying compiling.

nobugs-hare commented 6 years ago

Corrected code:

ITHARE_OBF_NOINLINE OBFI6(int) fibonacci(OBFI6(int) num)
{
  static std::map<OBFI6(int), OBFI6(int)> cache{{0, 0}, {1, 1}};

  auto found = cache.find(num);
  if (found != std::end(cache)) {
    // For debugging purposes, to check that the cache is doing something
    std::cout << OBFS5L("Found in cache: ").value() << num << OBFS5L(" -> ").value() << found->second << '\n';//sometimes we may need to add ".value()" to obfuscated variables/constants
    return found->second;
  }

  OBFI6(int) result = fibonacci(num - 1) + fibonacci(num - 2);
  cache[num] = result;
  return result;
}

int main()
{
  int num;//cannot obfuscate parameters passed by reference to standard library (in this case - to cin >>); can obfuscate after reading from cin if necessary
  do{
    std::cout << "i: ";
    std::cin >> num;
  }while(num < 3);

  OBFI6(int) res = fibonacci(num);
  std::cout << num << OBFS5L("th fibonacci number: ").value() << res << std::endl;
}
nobugs-hare commented 6 years ago

A few minor notes: for it to compile, you have to tell your compiler to use C++17, and to add #include somewhere. The code above was tested with current Apple Clang.

Dagdelo commented 6 years ago

Ok. thx.