int main(int argc, char *argv[]) { try { std::string input("abcdefghijklmnopqrstuvwxyz"); char buffer[32]; memset(buffer, 0, 32); StringStream sStream; sStream.write((const sys::byte*) input.c_str(), 5); sStream.read((sys::byte*) buffer, 3); cout << buffer << endl; memset(buffer, 0, 32); sStream.write((const sys::byte*) input.c_str(), 10); sStream.read((sys::byte*) buffer, 11); cout << buffer << endl; memset(buffer, 0, 32); sStream.read((sys::byte*) buffer, 100); cout << buffer << endl; } catch (Throwable& t) { cerr << "Caught throwable: " << t.toString() << endl; } catch (...) { cerr << "Caught unknown exception" << endl; } return 0; }
/* * Return count of obfuscated local variables */ size_t obfuscateLocalVarsInExpression(StringStream &stream, char *pExpStart, const char *pExpEnd, LocalVarsStack &localVarsInBlock, LocalVarsStack &localVarsInModule) { size_t count = 0; char *p = pExpStart; while (*p && p < pExpEnd) { if (isStringStart(p)) { size_t size = skipStringAndMove(&p, NULL); stream.write(p - size, size); continue; } const char *pWordStart = p; while (*p && isVarChar(*p)) { ++p; } size_t wordSize = p - pWordStart; if (wordSize) { bool bFind; stObfuscatedName var; var.name.assign(pWordStart, wordSize); bFind = localVarsInBlock.find(var); if (!bFind) bFind = localVarsInModule.find(var); if (bFind) { stream << var.fake_name; ++count; } else { stream << var.name; } } stream << *p; ++p; } return count; }
/* * Scaning all text * Skip the "string" * Why do finds the function name: * function call: * foo(arg1, arg2, ...) * a = foo(arg1, arg2, ...) * when function send to other function * trycall(foo) * --------------------- * therefore function name is near '(' and ')' */ ptrdiff_t replaceGlobalFunctions(const char *szFileName, FakeFunctions &globalFunctions, const StringList &excludeFunctions) { if (!szFileName || !szFileName[0]) return 0; FILE *file = fopen(szFileName, "rt"); if (!file) return 0; fseek(file, 0, SEEK_END); int size = ftell(file); rewind(file); char *pDataInSource = new char[size + 20]; // + 20 for additional 10_"data"_10 memset(pDataInSource, 0, 10); char *pDataIn = pDataInSource + 10; // for delete/clear data size_t realSize = fread(pDataIn, 1, size, file); memset(pDataIn + realSize, 0, 10); fclose(file); StringStream stream; char *p = pDataIn; while (*p) { if (isStringStart(p)) { size_t size = skipStringAndMove(&p, NULL); stream.write(p - size, size); continue; } if (!strncmp(p, "tostring", sizeof("tostring") - 1)) *p = *p; char *pStart = p; while (isAlphaFun(*p)) { ++p; } size_t wordSize = p - pStart; if (wordSize) { if (isFunctionNameInCode(pStart, p)) { std::string str(pStart, wordSize); StringMapConstIter iter = globalFunctions.find(str); if (iter != globalFunctions.end()) { StringListConstIter IterFunExclude = std::find(excludeFunctions.begin(), excludeFunctions.end(), str); if (IterFunExclude == excludeFunctions.end()) { // replace stream.write(iter->second.c_str(), FAKE_FUNCTION_LEN); continue; } } } stream.write(pStart, wordSize); if (!p[0]) { break; } } stream << *p; ++p; } file = fopen(szFileName, "wt"); const std::string& str = stream.str(); size_t obfuscateSize = str.length(); if (file) { fwrite(str.c_str(), 1, obfuscateSize, file); fclose(file); } delete[] pDataInSource; print("%8d %s\n", obfuscateSize - realSize, szFileName); return obfuscateSize - realSize; }