grantcurell
August 4th, 2013, 10:47 PM
In the code below, if the call to tellg() inside the print statement is not there, the program will not loop and will immediately terminate after the one pass. If that call to tellg() is included, the program works exactly as expected. The input must be greater than 100 bytes in order to observe the problem. At first I considered that maybe a badbit was being set. I tested that theory by replacing the printf w/tellg() with a call to file.clear(). TO my astonishment, the program failed just as before. When I put the tellg() back in the program worked as expected. For clarification, it may be helpful to know that for fstream the get and put pointers are separate.
/* Quick n' Dirty File Encoder
* Author: Grant Curell
* Date: 3 August 2013
* Encodes a file using the xor function. The key is whatever memblocktemp
* is initialized to. The function uses the output of the first xor as the
* input for the next xor and so on and so forth.
*/
#include <fstream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define BLOCKSIZE 100
char memblock[BLOCKSIZE], memblocktemp[BLOCKSIZE];
int main () {
//Open the file in binary mode
fstream file;
file.open("test.exe", ios::in|ios::out|ios::binary);
memset(memblocktemp, 'G', BLOCKSIZE);
//Make sure the file successfully opened
if (file.is_open()) {
int bytes_read;
while(file.good()) {
file.read(memblock, BLOCKSIZE); //Read Block
//Count the number of bytes read in to ensure we don't
//write past the end of the file
bytes_read = file.gcount();
//xor the bytes read in
for(int x=0; x<BLOCKSIZE; x++) {
memblock[x] ^= memblocktemp[x];
memblocktemp[x] = memblock[x];
}
file.seekp(-bytes_read, ios::cur);
if(file.eof()) {
file.clear();
file.seekp(-bytes_read, ios::cur);
file.write(memblock, bytes_read);
break;
}
file.write(memblock, bytes_read); //Write to the block
printf("File get pointer is at %d\n", (int)file.tellg());
}
file.close();
printf("Operation complete.\n");
} else
printf("Unable to open file\n");
return 0;
}
/* Quick n' Dirty File Encoder
* Author: Grant Curell
* Date: 3 August 2013
* Encodes a file using the xor function. The key is whatever memblocktemp
* is initialized to. The function uses the output of the first xor as the
* input for the next xor and so on and so forth.
*/
#include <fstream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define BLOCKSIZE 100
char memblock[BLOCKSIZE], memblocktemp[BLOCKSIZE];
int main () {
//Open the file in binary mode
fstream file;
file.open("test.exe", ios::in|ios::out|ios::binary);
memset(memblocktemp, 'G', BLOCKSIZE);
//Make sure the file successfully opened
if (file.is_open()) {
int bytes_read;
while(file.good()) {
file.read(memblock, BLOCKSIZE); //Read Block
//Count the number of bytes read in to ensure we don't
//write past the end of the file
bytes_read = file.gcount();
//xor the bytes read in
for(int x=0; x<BLOCKSIZE; x++) {
memblock[x] ^= memblocktemp[x];
memblocktemp[x] = memblock[x];
}
file.seekp(-bytes_read, ios::cur);
if(file.eof()) {
file.clear();
file.seekp(-bytes_read, ios::cur);
file.write(memblock, bytes_read);
break;
}
file.write(memblock, bytes_read); //Write to the block
printf("File get pointer is at %d\n", (int)file.tellg());
}
file.close();
printf("Operation complete.\n");
} else
printf("Unable to open file\n");
return 0;
}