Changed makefile to be cleaner and easier to manage.
Implemented basic xorOperation. Need to generalize operation so that there is minimal code duplication between xor, or, and, not, etc by simply changing the operation step in the for loop.
1.1 --- a/Command.cpp Fri Jul 03 11:21:44 2009 -0400
1.2 +++ b/Command.cpp Sun Jul 05 17:13:07 2009 -0400
1.3 @@ -67,12 +67,30 @@
1.4 {
1.5 std::ifstream ifs(command->filename, std::ifstream::in | std::ifstream::binary);
1.6
1.7 -
1.8 + if(ifs.good() != true)
1.9 + {
1.10 + printf("Error opening %s\n", command->filename);
1.11 + ifs.close();
1.12 + return;
1.13 + }
1.14
1.15 // calculate the total file size
1.16 ifs.seekg(0, std::ios::end);
1.17 + if(ifs.good() != true)
1.18 + {
1.19 + printf("File operation failed!\n");
1.20 + ifs.close();
1.21 + return;
1.22 + }
1.23 +
1.24 int fileSize = ifs.tellg();
1.25 -
1.26 + if(ifs.good() != true)
1.27 + {
1.28 + printf("File operation failed!\n");
1.29 + ifs.close();
1.30 + return;
1.31 + }
1.32 +
1.33 if(command->end > fileSize)
1.34 command->end = fileSize;
1.35 if(command->start > fileSize)
1.36 @@ -114,9 +132,12 @@
1.37 {
1.38 std::ifstream ifs(filename, std::ifstream::in | std::ifstream::binary);
1.39
1.40 -
1.41 -
1.42 - // calculate the total file size
1.43 + if(ifs.good() != true)
1.44 + {
1.45 + printf("Error opening %s\n", filename);
1.46 + ifs.close();
1.47 + return;
1.48 + }
1.49
1.50 // calculate how many bytes we're printing
1.51 int length = end - start;
1.52 @@ -128,7 +149,21 @@
1.53
1.54 // seek to the start and scan in
1.55 ifs.seekg(start, std::ios::beg);
1.56 + if(ifs.good() != true)
1.57 + {
1.58 + printf("File operation failed!\n");
1.59 + ifs.close();
1.60 + return;
1.61 + }
1.62 ifs.read((char*)dataBuffer, length);
1.63 + if(ifs.good() != true)
1.64 + {
1.65 + printf("File operation failed!\n");
1.66 + ifs.close();
1.67 + return;
1.68 + }
1.69 +
1.70 + ifs.close();
1.71
1.72 int currentAddress = start;
1.73
1.74 @@ -203,7 +238,77 @@
1.75 }
1.76 }
1.77
1.78 -void Command::xorOperation(){}
1.79 +void Command::xorOperation()
1.80 +{
1.81 + // create a stream to the file & verify its valid
1.82 + std::ifstream ifs(filename, std::ifstream::in | std::ifstream::binary);
1.83 +
1.84 + if(ifs.good() != true)
1.85 + {
1.86 + printf("Error opening %s", filename);
1.87 + return;
1.88 + }
1.89 +
1.90 + // copy our data into a buffer
1.91 + int length = end - start;
1.92 +
1.93 + // TODO: Optimise for large files, so large amounts of memory aren't used
1.94 + unsigned char buffer[length];
1.95 + memset(buffer, 0, length);
1.96 +
1.97 + ifs.seekg(start, std::ios::beg);
1.98 + ifs.read((char*)buffer, length);
1.99 + if(ifs.good() != true)
1.100 + {
1.101 + printf("Read error!\n");
1.102 + ifs.close();
1.103 + return;
1.104 + }
1.105 +
1.106 + ifs.close();
1.107 +
1.108 + // apply xor to the buffer
1.109 + // TODO: Possibly do this in parallel. Investigate GPU, SIMD, etc
1.110 + int dataIndex = 0;
1.111 + for(int i = 0; i < length; i++)
1.112 + {
1.113 + buffer[i] ^= data[dataIndex];
1.114 +
1.115 + // get the next element of data to operate with
1.116 + if(++dataIndex >= dataCount)
1.117 + dataIndex = 0;
1.118 + }
1.119 +
1.120 + // get a FILE* to write with
1.121 + FILE* output = fopen(filename, "r+");
1.122 +
1.123 + if(output == 0)
1.124 + {
1.125 + printf("Error opening %s", filename);
1.126 + return;
1.127 + }
1.128 +
1.129 + // move the pointer to the proper location
1.130 + if(fseek(output, start, SEEK_SET) != 0)
1.131 + {
1.132 + printf("File operation failed!\n");
1.133 + fclose(output);
1.134 + return;
1.135 + }
1.136 +
1.137 + // copy the buffer back into the file
1.138 + if(length != fwrite(buffer, sizeof(char), length, output))
1.139 + {
1.140 + printf("Error writing back to file!\n");
1.141 + fclose(output);
1.142 + return;
1.143 + }
1.144 +
1.145 + // close the stream
1.146 + fclose(output);
1.147 +
1.148 +}
1.149 +
1.150 void Command::orOperation(){}
1.151 void Command::andOperation(){}
1.152 void Command::notOperation(){}
2.1 --- a/makefile Fri Jul 03 11:21:44 2009 -0400
2.2 +++ b/makefile Sun Jul 05 17:13:07 2009 -0400
2.3 @@ -1,21 +1,29 @@
2.4 -$(CC) = g++ -g3 -gdwarf2
2.5 -$(cc) = gcc -g3 -gdwarf2
2.6 +$(CXX)=g++
2.7 +$(CC)=gcc
2.8
2.9 -all: executable
2.10 +EXECUTABLE = hexciting
2.11 +OBJECTS = CommandParser.tab.o CommandParser.yy.o Command.o
2.12 +LIBRARIES = -lfl
2.13
2.14 -executable: CommandParser.tab.o CommandParser.yy.o Command.o
2.15 - g++ -g -o hexciting CommandParser.yy.o CommandParser.tab.o Command.o -lfl
2.16 +all: $(EXECUTABLE)
2.17
2.18 -CommandParser.yy.o: CommandParser.l
2.19 - flex -o CommandParser.yy.c CommandParser.l
2.20 - gcc -g -c CommandParser.yy.c
2.21 +debug: CXX += -DDEBUG -g
2.22 +debug: CC += -DDEBUG -g
2.23 +debug: $(EXECUTABLE)
2.24
2.25 -CommandParser.tab.o: CommandParser.y
2.26 - bison -d CommandParser.y
2.27 - g++ -g -c CommandParser.tab.c
2.28 +$(EXECUTABLE): $(OBJECTS)
2.29 + $(CXX) -o $@ $^ $(LIBRARIES)
2.30
2.31 -Command.o: Command.cpp
2.32 - g++ -g -c Command.cpp
2.33 +%.yy.o: *.l
2.34 + flex -o $*.yy.c $<
2.35 + $(CC) -c $*.yy.c
2.36 +
2.37 +%.tab.o: CommandParser.y
2.38 + bison -d $<
2.39 + $(CXX) -c $*.tab.c
2.40 +
2.41 +%.o: %.cpp
2.42 + $(CXX) -c $<
2.43
2.44 clean:
2.45 - rm -f CommandParser.tab.* CommandParser.yy.* hexciting *.o
2.46 + rm -f $(EXECUTABLE) $(OBJECTS) *.yy.c *.tab.c