philmasterplus wrote:
> I've been trying to create a basic file obfuscation library using the
> XOR operator because I heard that it was fast. Because of the
> suggestions of those at the comp.lang.c++, I have decided to turn the
> library into a file obfuscation library. More details are available at
>
http://groups.google.com/group/comp.lang.c++/browse_frm/thread/68f7859cceff1c34#
>
> First, however, I would like to submit my previous code for peer
> review. I know it is very basic. I know that loading a file into
> memory via std::string is a bad idea, but as I posted in comp.lang.c+
> +, it is a tem****ary measure. I haven't tried using ios::binary yet
> (that's what the people at comp.lang.c++ recommended). I would like to
> have all forms of suggestions.
>
> Note: as I said above, I am going to turn the library into a file
> obfuscation library. I am going to rename the class to FileObfuscator
> or something.
>
> So here goes a bit of my questions:
>
> * What do I need in order to improve the performance/design.
> * How can I bypass the out-of-place EOF character in the middle of a
> binary file? Does the flag ios::binary help?
> * Are there any other fast obfuscation methods out there?
> * Is there an alternative to loading a file onto the memory via
> std::string? I did it in the first place because I didn't know how to
> encode the file with fstream. Therefore, I had to load a file with
> std::ifstream, then write to it with std::ofstream. So the question
> may be rephrased as...how can I XOR-encode or ROT13-encode a text/
> binary file with fstream? Please post some examples!
>
> You may download the library at:
>
http://ftp11.ohpy.com/opftp/index.php?control=Download&nOption=1&file=vrdx0DSozg5FzhP2klrSjKqqjvrlTTH3WKyW9nfrScjt_KdyTJ2J8xOXHsoNwmg0JGv_GCbQnj7uqSbZ09w15ZGOfcDuzlbrextnsn.zip
>
Comments and suggestions:
1) Use std::vector<char> instead of std::string. If I understand your
intention, you want to manipulate an arbitrary stream of bytes that may
or may not be "strings".
2) ios::binary is not only a good idea, but probably a necessary one.
Otherwise your program may wind up misinterpreting some sequences of
bytes as newlines and transforming them to some other sequence of bytes
in an implementation defined manner.
3) The usual idiom for reading a character at a time using the iostream
library looks something like:
ifstream in("filename");
for (char c; in.get(c);)
{
// Do stuff with c.
}
in.get(c) returns false (or at least, something that after various type
conversions tests false) when the operation fails (usually because
you've reached the end of the file). A loop that explicitly tests for
end of file will almost always have an off-by-one error.
4) If your platform sup****ts it, memory mapped files are often a very
fast and efficient solution. See mmap if you use some flavor of Unix.
I think the Windows equivalent is CreateFileMapping/MapViewOfFile,
though my memory may be failing me.
5) For C++ bonus style points lookup std::istream_iterator,
std::ostream_iterator, and std::transform. It might not be the most
efficient solution (or it might be, only measuring can tell!) but
writing this version has the psychological side effect of making you
feel like a C++ badass.
--
Alan Johnson
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|