tags:

views:

370

answers:

8

Hello everyone,

I wish to send some components to my customers. The reasons I want to deliver source code are:

1) My class is templatized. Customer might use any template argument, so I can't pre-compile and send .o file.

2) The customer might use different compiler versions for gcc than mine. So I want him to do compilation at his end.

Now, I can't reveal my source code for obvious reasons. The max I can do is to reveal the .h file. Any ideas how I may achieve this. I am thinking about some hooks in gcc that supports decryption before compilation, etc. Is this possible?

In short, I want him to be able to compile this code without being able to peek inside.

+1  A: 

Hello, what about pimpls?

Benoit
Would that work for templates?
JoshD
Good remark. I think it works. You have to templatize both the implementation struct and the working class.
Benoit
A: 

You can probably compile to assembler using gcc an give your customer the asm source..

RC
I do not believe that will work for templates, which OP mentioned he is using.
abelenky
It also has the same set of problems the question says should be avoided: a need to distribute different versions of the code for each target system, as the assembly already encodes dependencies on CPU, OS, system headers, exact library versions etc..
Tony
+3  A: 

use some c++ obfuscators may be help?: http://www.semdesigns.com/products/obfuscators/CppObfuscationExample.html
or Magle It

pinichi
Surely selecting C++ templates was already a good start for obfuscation?
JUST MY correct OPINION
+10  A: 

Contract = good, obfuscation = ungood.

That said, you can always do a kind of PIMPL idiom to serve your customer with binaries and just templated wrappers in the header(s). The idea is then to use an "untyped" separately compiled implementation, where the templated wrapper just provides type safety for client code. That's how one often did things before compilers started to understand how to optimize templates, that is, to avoid machine-code level code bloat, but it only provides some measure of protection about trivial copy-and-paste theft, not any protection against someone willing to delve into the machine code.

But perhaps the effort is then greater than just reinventing your functionality?

Cheers & hth.,

– Alf

PS: I saw "one new answer" posted while writing this, and hey, you can "load new answers". This has happened several times earlier, also. It seems to me that people here rush into answering, perhaps to gain scores (theory: when a posting gains one score then others are perhaps more likely to score it up again). I think on principle one shouldn't do that rush job of answering. But I nevertheless apologize if this merely duplicates the answer that popped in, because if so then it's just due to my obstinate refusal to go with the flow...

Alf P. Steinbach
Your postscriptum belongs to meta.stackoverflow.com
mouviciel
+1 for taking your time and writing such a good detailed answer! :-)
Vinzenz
You are correct in saying that in my case, the effort to reverse-engineer would be greater than reinvent. That being said, can you please elaborate your solution, perhaps with a link to an example?
deeJ
Alf P. Steinbach
+2  A: 

Just adding some terminology to Alf's answer: The Thin template idiom is what you might look at. It basically simulates the functionality of a generic. Don't get confused by the wikipedia article which pops up in google, you don't have to use void*...

This, of course, does not guarantee binary compatibility. As usual with 'native' c++, you either compile the component for customers platform yourself and deploy the binary, or give them your code... The difference to the pure generic component code is that you can do the former at all.

paul_71
+1  A: 

First, if you're going to provide the source code, then you have to provide the source code. Sure, you could encrypt it, but even if GCC had a "decrypt before compile" option, it would need to decrypt the code, and if GCC can decrypt the code, so can your customer.

What you're asking is impossible. (If you find a way to do it, I believe the movie industry might have a multi-million contract for you. They currently have to resort to expensive custom hardware to prevent people from ripping content, and that only works to a limited degree)

As for your "obvious reasons" why you don't want to provide the source code, I don't see why they're obvious. What would happen if you provided the source code?

You have two options:

  • provide the source code in its entirety, or
  • compile everything that can be precompiled into a (static or dynamic) library, and provide your customer with that, plus the header files.
jalf
* "that only works to a limited degree" * ... where "limited degree" means "not at all", right?
JUST MY correct OPINION
A: 

1) My class is templatized. Customer might use any template argument, so I can't pre-compile and send .o file.

2) The customer might use different compiler versions for gcc than mine. So I want him to do compilation at his end.

Now, I can't reveal my source code for obvious reasons. The max I can do is to reveal the .h file. Any ideas how I may achieve this. I am thinking about some hooks in gcc that supports decryption before compilation, etc. Is this possible?

In short, I want him to be able to compile this code without being able to peek inside.

Consideration 2) above encompasses A) ABI differences such that the same code compiled with different compiler versions/vendors on the same platform is incompatible, as well as B) the differences in system libraries, kernel versions etc. that the code might be dependent on. The only general solution is to compile on the specific platforms. Either you do it for all platforms, or you give them all the source code and they do it. That's not just the headers and template implementation, that's your out-of-line functions too. You might mitigate A) a little by building a wall of more interoperable extern "C" functions, but you're basically stuck when it comes to B).

So, can you decrypt during compilation? Only if you ship your own hacked GCC binaries to them, built for their specific system, which is probably more hassle than providing different builds of your own libraries (though it may address the template/header exposure issue).

Alternatively, you could employ source code obfuscation techniques. This is probably - practically - as good as it gets. I don't know what tools are out there, but it's an approach that people have pursued for decades (though I'm yet to hear anyone recommend it), so there's sure to be some mature tools.

Re templated code - other people have suggested a templated front end to a C-style generic implementation shipped as a precompiled object. That may or may not be practical (clearly risks performance degradation, and you have to capture the set of type-specific operations you want - e.g. by instantiating a type-specific class derived from an abstract operations base class) but anyway the precompiled object still runs afoul of B).

One other thought... clients might take your source code, but are unlikely to understand it as well as you. Even if they build more systems dependent on their version of it, in a way they're getting more locked in, and may have more need for your services in future. And, if you see they've not played fair, you charge them for it appropriately when the time comes.

Tony
A: 

It seems with gcc 4.5 comes the support for plugins. So you can provide your own .so which would be, for instance, called before compilation stage starts. So you can have all kinds of tricks(decryption of source file) in there, neatly hidden. This would also be portable solution as no change is made to g++ per se.

This is exactly what I was looking for. You can read more here: http://www.codesynthesis.com/~boris/blog/2010/05/03/parsing-cxx-with-gcc-plugin-part-1/

deeJ
As it turns out, this isn't the silver bullet I was looking out. There are license issues, each plugin has to be released with GPL. Down goes my hopes for implementing an decrypting algorithm :)
deeJ