Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Bypassing the EOF marker

Reply
Thread Tools

Bypassing the EOF marker

 
 
philmasterplus
Guest
Posts: n/a
 
      04-19-2008
I am trying to create a simple file encoder (obfuscator) class using
the XOR(^) operator.

I am using objects of the class ifstream and ofstream to input/output
to the file. Right now, I am experimenting with xor-encoding a file
with a given C-string.

The problem is, the obfuscated file contains the EOF character for my
system, and my program cannot proceed to reencode (decode) the rest of
the obfuscated file.

The code can be simplified as follows:

<code>
//Assuming all necessary includes are in place

int main()
{
ifstream ifs("test.txt");
char cbuf;
string sbuf;

//Other code blah blah blah...

cbuf = ifs.get();
while (!ifs.eof()) //Recognizes the EOF-marker-in-the-middle-of-the-
file
{
sbuf.push_back(cbuf);
cbuf = ifs.get();
}

//Other code blah blah blah...
}

</code>


Yeah, I know, loading the whole file into a std::string variable is a
BAD idea, but it's a temporary hack. Which brings me to another
question: How do you xor-encode a file without opening an ifstream and
an ofstream? I know about the std::fstream class, but I don't know how
to use it.

Any form of example code (preferably with indents) would be
appreciated!

P. S. please also comment on my programming style. On a request, I
will upload the whole project.
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      04-19-2008
philmasterplus wrote:
> I am trying to create a simple file encoder (obfuscator) class using
> the XOR(^) operator.
>
> I am using objects of the class ifstream and ofstream to
> input/output to the file. Right now, I am experimenting with
> xor-encoding a file with a given C-string.
>
> The problem is, the obfuscated file contains the EOF character for
> my system, and my program cannot proceed to reencode (decode) the
> rest of the obfuscated file.
>
> The code can be simplified as follows:
>
> <code>
> //Assuming all necessary includes are in place
>
> int main()
> {
> ifstream ifs("test.txt");
> char cbuf;
> string sbuf;
>
> //Other code blah blah blah...
>
> cbuf = ifs.get();
> while (!ifs.eof()) //Recognizes the EOF-marker-in-the-middle-of-the-
> file
> {
> sbuf.push_back(cbuf);
> cbuf = ifs.get();
> }
>
> //Other code blah blah blah...
> }
>
> </code>
>
>
> Yeah, I know, loading the whole file into a std::string variable is
> a BAD idea, but it's a temporary hack. Which brings me to another
> question: How do you xor-encode a file without opening an ifstream
> and an ofstream? I know about the std::fstream class, but I don't
> know how to use it.
>
> Any form of example code (preferably with indents) would be
> appreciated!
>
> P. S. please also comment on my programming style. On a request, I
> will upload the whole project.


The way you handle it, it isn't really a text file, is it? Recognizing
EOF markers inside the file, or not, is totally system dependent.
There is no general rule for this.

Your best shot will probably be to open the files in binary mode,
like:

ifstream ifs("test.txt", std::ios::binary);


Bo Persson


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      04-19-2008
On 19 avr, 14:23, philmasterplus <philmasterp...@gmail.com> wrote:
> I am trying to create a simple file encoder (obfuscator) class
> using the XOR(^) operator.


> I am using objects of the class ifstream and ofstream to
> input/output to the file. Right now, I am experimenting with
> xor-encoding a file with a given C-string.


> The problem is, the obfuscated file contains the EOF character
> for my system, and my program cannot proceed to reencode
> (decode) the rest of the obfuscated file.


More to the point, the "obfuscated" file isn't text, so cannot
be written (nor read) if the file is opened in text mode. You
have two choices: open in binary, or use something like rot-13
for obfuscation, which ensures that the obfuscated data is text.
(Rot-13 is very minimal obfuscation, but throw in a little
shuffling, and it should be as good or better than xor'ing.)

> The code can be simplified as follows:
>
> <code>
> //Assuming all necessary includes are in place
> int main()
> {
> ifstream ifs("test.txt");


If this is to read your obfuscated text:

std::ifstream ifs( "test.txt", std::ios::binary ) ;

And of course, in real code, you'll want to check that the open
succeeded.

> char cbuf;
> string sbuf;


> //Other code blah blah blah...


> cbuf = ifs.get();
> while (!ifs.eof()) //Recognizes the EOF-marker-in-the-middle-of-the-
> file


In theory, this test may cause you to miss the last character.
There are two "classical" solutions:

while ( ifs.get( cbuf ) ) { ...

or declare cbuf as an int, and:

while ( cbuf != EOF ) { ...

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Windows] Any way to distinguish ^C Induced EOF from ^Z EOF? Jan Burse Java 67 03-14-2012 12:21 AM
ifstream eof not reporting eof? SpreadTooThin C++ 10 06-15-2007 08:49 AM
if EOF = -1, can't a valid character == EOF and cause problems? Kobu C Programming 10 03-04-2005 10:40 PM
Insert EOF marker into file zambak C Programming 2 11-16-2003 01:15 PM
HTML text marker Joerg Firefox 5 09-18-2003 01:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57