-1

for some reason, I needed to put a file in a DLL. I knew I could put it in a pure resource DLL, but I didn't want anyone else to know how to read the file。 So I want to be able to put this file in a DLL along with the functions on how to read the file. Does anyone know how to do that ? Thanks

huaixiu
  • 3
  • 1
  • What Kind of information do you want to export? Is that representable as simple `std::string`s, or `std::istringstream`s? – πάντα ῥεῖ Dec 24 '21 at 02:53
  • 1
    If you don't want anyone to know how to read the file, but the instructions on how to read the file are right next to the file, how are they prevented from reading the file? It sounds like you might be relying on "security through obscurity" which should be avoided. – JohnFilleau Dec 24 '21 at 02:56
  • in fact, this file is a model file for deep learning. I will provide functions in the DLL on how to read this model. @πάνταῥεῖ – huaixiu Dec 24 '21 at 02:59
  • I don't want the customer to get the model file separately for things like weight updates, but I do allow them to inference with the model. @πάνταῥεῖ – huaixiu Dec 24 '21 at 03:06
  • Once you ship a DLL you have given up **all** control over protecting its contents. It doesn't matter whether you export that data as a symbol, as a custom or standard resource, or not export the data at all. An attacker can trivially read the information you're trying to protect. – IInspectable Dec 30 '21 at 12:43

1 Answers1

2

Here is one idea that works for both text and binary files alike.

Store the file as base64 encoded with

$ cat file.txt
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

$ cat file.txt | base64 > base64.txt

$ cat base64.txt
SGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29y
bGQKSGVsbG8gV29ybGQKCg==

Then in your c++ file

static std::string fileout = R"(
SGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29ybGQKSGVsbG8gV29y
bGQKSGVsbG8gV29ybGQKCg==
)";

std::string readme() {
    return base64::decode( fileout );
}

You can find several base64::decode() versions here: Base64 decode snippet in C++

  • I use this technique to store golden data into my C++ unit tests. It works beautifully. –  Dec 24 '21 at 03:20