Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

/users/u3/mtikir/PMaCInstrumentor_v1601/src/BinaryFile.C

Go to the documentation of this file.
00001 #include <Base.h>
00002 #include <BinaryFile.h>
00003 #include <XCoffFile.h>
00004 #include <BinaryFile.h>
00005 
00006 void BinaryInputFile::readFileInMemory(char* fileName) {
00007 
00008     if(inBuffer){
00009         PRINT_INFOR("This object has already read an executable file\n");
00010         return;
00011     }
00012 
00013     FILE* inFile = NULL;
00014 
00015     inFile = fopen(fileName,"r");
00016     if(!inFile){
00017         PRINT_ERROR("Input file can not be opened [%s]",fileName);
00018     }
00019 
00020     fseek(inFile,0,SEEK_END);
00021     int32_t length = (int32_t)ftell(inFile);
00022 
00023     if(length <= 0){
00024         PRINT_ERROR("Input file size is 0 [%s]",fileName);
00025     }
00026 
00027     inBuffer = new char[length];
00028     fseek(inFile,0,SEEK_SET);
00029     int32_t check = (int32_t)fread(inBuffer,sizeof(char),length,inFile);
00030     if(check != length){
00031         PRINT_ERROR("Input file can not be read completely [%s]",fileName);
00032     }
00033 
00034     fclose(inFile);
00035 
00036     PRINT_INFOR("Input file is read with success [%s] with %d bytes",fileName,length);
00037 
00038     inBufferPointer = inBuffer;
00039     inBufferSize = length;
00040 
00041     PRINT_INFOR("Input file ranges [0x0,%#x]",length);
00042 
00043 }
00044 
00045 
00046 char* BinaryInputFile::copyBytes(void* buff,uint32_t bytes) 
00047 { 
00048 
00049     char* last = inBuffer + inBufferSize;
00050     char* next = inBufferPointer + bytes;
00051 
00052     if(next > last){
00053         PRINT_DEBUG("Returning NULL from copyBytes because next > last");
00054         return NULL;
00055     }
00056 
00057     void* ret = memcpy(buff,inBufferPointer,bytes); 
00058     if(ret != buff){
00059         PRINT_DEBUG("Returning NULL from copyBytes because ret != buff");
00060         return NULL;
00061     }
00062     return inBufferPointer;
00063 }
00064 
00065 char* BinaryInputFile::copyBytesIterate(void* buff,uint32_t bytes)
00066 { 
00067 
00068     if(!copyBytes(buff,bytes)){
00069         return NULL;
00070     }
00071 
00072     inBufferPointer += bytes; 
00073     return inBufferPointer;
00074 }
00075 
00076 char* BinaryInputFile::onlyIterate(uint32_t bytes){
00077     char* last = inBuffer + inBufferSize;
00078     char* next = inBufferPointer + bytes;
00079 
00080     if(next > last){
00081         return NULL;
00082     }
00083 
00084     inBufferPointer += bytes; 
00085     return inBufferPointer;
00086 }
00087 
00088 char* BinaryInputFile::moreBytes(){
00089     char* last = inBuffer + inBufferSize;
00090     if(inBufferPointer >= last){
00091         return NULL;
00092     }
00093     return inBufferPointer;
00094 }
00095 
00096 
00097 char* BinaryInputFile::fileOffsetToPointer(uint64_t fileOffset){
00098     char* last = inBuffer + inBufferSize;
00099     char* next = inBuffer + fileOffset;
00100 
00101     if(next >= last){
00102         return NULL;
00103     }
00104     return next;
00105 }
00106 
00107 char* BinaryInputFile::setInBufferPointer(uint64_t fileOffset){
00108     char* next = fileOffsetToPointer(fileOffset);
00109     if(next){
00110         inBufferPointer = next;
00111     }
00112     return next;
00113 }
00114 
00115 char* BinaryInputFile::isInBuffer(char* ptr){
00116     char* last = inBuffer + inBufferSize;
00117 
00118     if((ptr >= last) || (ptr < inBuffer)){
00119         return NULL;
00120     }
00121     return ptr;
00122 }
00123 
00124 char* BinaryInputFile::setInPointer(char* ptr){
00125     if(isInBuffer(ptr)){
00126         inBufferPointer = ptr;
00127     } else {
00128         ptr = NULL;
00129     }
00130     return ptr;
00131 }
00132 
00133 uint32_t BinaryInputFile::bytesLeftInBuffer(){
00134     char* last = inBuffer + inBufferSize;
00135     return (uint32_t)(last-inBufferPointer);
00136 }
00137 
00138 void BinaryOutputFile::copyBytes(char* buffer,uint32_t size,uint32_t offset) {
00139     int32_t error_code = fseek(outFile,offset,SEEK_SET);
00140     if(error_code){
00141         PRINT_ERROR("Error skeeing to the output file");
00142     }
00143     error_code = fwrite(buffer,sizeof(char),size,outFile);
00144     if((uint32_t)error_code != size){
00145         PRINT_ERROR("Error writing to the output file");
00146     }
00147 }
00148 
00149 uint32_t BinaryOutputFile::alreadyWritten(){
00150     return (uint32_t)ftell(outFile);
00151 }
00152 
00153 void BinaryOutputFile::open(char* fileName) { 
00154     outFile = fopen(fileName,"w");
00155 }
00156 
00157 bool BinaryOutputFile::operator!() { return !outFile; }
00158 
00159 void BinaryOutputFile::close() { fclose(outFile); }
00160 

Generated on Mon Jan 28 11:08:32 2008 for PMaCInstrumentor by doxygen 1.3.5