0

Large portion of our commercial C++ library relies on templates. We plan to sell our product as header files and dynamically linked libraries (closed-source), but because most of our code base is concentrated in headers, we would de facto be releasing it as open-source with small, easily replaceable chunks missing.

Here is an example of what one of our classes from library interface look like:

template<class ItInput, class ItOutput>
struct serialize{
  ItOutput operator() (ItInput first, ItInput last, ItOutput d_first) {
    // operation on pointers (assuming that ++, -- and * operators work as expected for pointers) 
} 

Is there a way to provide level of obfuscation to our templated code equal to or better than compilation of regular code (i.e. technically reversible but not profitable nor optimal to do so)?

EDIT: To clarify, our goal is to prevent users from reading the implementation, not prevent unlawful copying of our work. For the sake of the question please assume we have a valid reasons for this requirement.

Reverent Lapwing
  • 283
  • 2
  • 11
  • What about using pre-compiled headers? I'm not sure how easy it is to turn those back into source. – Nir Friedman Jul 11 '17 at 16:18
  • While the code is "open"(readable) you can still make it closed source via your license. It doesn't stop people from copying it (pirate bay anyone?) but it does give you the right to go after people that violate it. – NathanOliver Jul 11 '17 at 16:21
  • How does obfuscating the code prevent people from copying it? –  Jul 11 '17 at 16:22
  • The public API of your library is also template based? – t.niese Jul 11 '17 at 16:29
  • 1
    Are you sure that your existing templated code is not already obfuscated enough? I mean so often C++ templates are written like a total gibberish with no meaningful names, no comments and no formatting, all squished into a couple of files... – user7860670 Jul 11 '17 at 16:54
  • 1
    Google finds this: http://stunnix.com/prod/cxxo/ – Jesper Juhl Jul 11 '17 at 16:57
  • Also consider your poor customer passing an incorrect parameter to one of the templates. What will the error message look like? – Bo Persson Jul 11 '17 at 17:23
  • also worth to check PIMPL Idiom: http://en.cppreference.com/w/cpp/language/pimpl – seleciii44 Jul 11 '17 at 17:35
  • Use C++2x modules. They will allow shipping libraries without any headers. They will be compiled to a binary representation of the code AST. Note that in any conceivable way, the templated code will still need to be exposed to the compiler, since it rely on generating code based on the template. So even with a binary format, you'd be able to extract what the code looked like before compilation. – Guillaume Racicot Jul 11 '17 at 17:54
  • @t.niese the whole idea is that library user can use our classes and functions without their classes inheriting from our interfaces (think STL, where ie. Comparable is a template argument rather than interface) – Reverent Lapwing Jul 11 '17 at 18:45

1 Answers1

7

we would de facto be releasing it as open-source

Wrong. "open source" means that your license is compatible with the OSI and it is likely to not be.

Ask your lawyer.

You are wrongly seeking a technical answer to a legal issue.

Is there a way to provide level of obfuscation to our templated code equal to or better than compilation of regular code

If you have time to spend, you might for example replace every identifier in your library with some useless junk. For example, if you use the secret identifier, add something like

#define secret s_1eovFxBcc2F

before any other code. Later you could even run a script replacing every occurrence of secret with s_1eovFxBcc2F. Of course your secret should not appear in any system headers that you use.

IMHO, that would be a loss of time, but might make your manager happy.

What really matters is the license applicable to your clients. No serious business (and certainly not the big ones) can afford going against the law.

To clarify, our goal is to prevent users from reading the implementation,

Then provide only, as the published interface of your library, a set of C-like (and probably declared extern "C") functions, with only opaque data types.

BTW, did you consider the opposite approach, of making your library open source, perhaps with a GPL license (and sell alternative, less free software friendly, licenses), so that every (distributed) program using it has to be GPL-ed also (or has to buy your expansive other license)?

(I insist, legal issues usually don't have technical only solutions)

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    I really like your answer (especially comment about correct licensing), however I don't think it fits our case. If you could provide an answer specifically for the code sample I added with some explanation how to do it (link will suffice), I'll gladly accept your answer. – Reverent Lapwing Jul 11 '17 at 19:08