/////////////////////////////////////////////////////////////////////// // HexRep.h - converts from binary files to hex files // // ver 1.0 // // // // Language: Visual C++, ver 6.0 // // Platform: Del XPS1000, Windows 2000 Professional // // Application: ImageTrek Project, Fall 2000 // // Author: Jim Fawcett, CST 2-187, Syracuse Univ. // // (315) 443-3948, jfawcett@twcny.rr.com // /////////////////////////////////////////////////////////////////////// #include #include "HexRep.h" #include "timer.h" using namespace std; #define HalfByteToHex(n) ((char) (((n) > 9) ? ((n) - 10 + 'A') : ((n) + '0'))) //----< convert unsigned short (two bytes), to hex >------------------- string toHex(unsigned short value) { string temp; const int mask = 0xf; while(value > 0) { temp.insert(0,HalfByteToHex(mask & value)); value = value >> 4; } while(temp.size() < 2*sizeof(unsigned short)) temp.insert(0,'0'); return temp; } #define HexToHalfByte(ch) ((int) (((ch) > '9') ? ((ch) + 10 - 'A') : ((ch) - '0'))) //----< convert eight hex chars to an unsigned short >----------------- unsigned short fromHex(const char buffer[4]) { unsigned short value = 0; for(int i=0; i<4; i++) { value = value << 4; value += HexToHalfByte(buffer[i]); } return value; } // //----< convert binary file to hex, store in temp file, hex.$$$ >------ void fileToHex(const string &fileName) { unsigned short block; unsigned long numRead; unsigned long numWritten; HANDLE inFileHandle = CreateFile(fileName.c_str(),GENERIC_READ,0,0,OPEN_EXISTING,0,0); if(INVALID_HANDLE_VALUE == inFileHandle) { MessageBox(NULL,"error reading file","File Error",MB_OK); return; } HANDLE tempHandle = CreateFile("hex.$$$",GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); if(INVALID_HANDLE_VALUE == tempHandle) { MessageBox(NULL,"error creating","File Error",MB_OK); exit(1); } BOOL success; do { success = ReadFile(inFileHandle,&block,2,&numRead,0); char ch1 = block; char ch2 = block >> 8; if(numRead > 0) { string temp = toHex(block); WriteFile(tempHandle,temp.c_str(),4,&numWritten,0); } } while(numRead > 0 && success); CloseHandle(tempHandle); CloseHandle(inFileHandle); } // //----< make begin tag >----------------------------------------------- string makeBegTag(const string &filename) { string temp; temp += ""; return temp; } //----< make end tag >------------------------------------------------- string makeEndTag() { return ""; } // //---< convert binary file to element with hex payload in temp file >-- HANDLE makeFileElement(const string &fileName, HANDLE tempHandle) { unsigned short block; unsigned long numRead; unsigned long numWritten; HANDLE inFileHandle = CreateFile(fileName.c_str(),GENERIC_READ,0,0,OPEN_EXISTING,0,0); if(INVALID_HANDLE_VALUE == inFileHandle) { MessageBox(NULL,"error reading file","File Error",MB_OK); return inFileHandle; } if(INVALID_HANDLE_VALUE == tempHandle) { tempHandle = CreateFile("hex.$$$",GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); if(INVALID_HANDLE_VALUE == tempHandle) { MessageBox(NULL,"error creating file","File Error",MB_OK); exit(1); } } string tag = makeBegTag(fileName); WriteFile(tempHandle,tag.c_str(),tag.size(),&numWritten,0); BOOL success; do { success = ReadFile(inFileHandle,&block,2,&numRead,0); char ch1 = block; char ch2 = block >> 8; if(numRead > 0) { string temp = toHex(block); WriteFile(tempHandle,temp.c_str(),4,&numWritten,0); } } while(numRead > 0 && success); tag = makeEndTag(); WriteFile(tempHandle,tag.c_str(),tag.size(),&numWritten,0); CloseHandle(inFileHandle); return tempHandle; } // //----< convert from hex.$$$ temp file to binary file >---------------- void hexToFile(const string &fileName) { BOOL success; unsigned long numWritten, numRead; unsigned short block; char buffer[4]; HANDLE tempHandle = CreateFile("hex.$$$",GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if(INVALID_HANDLE_VALUE == tempHandle) { MessageBox(NULL,"error reading file","File Error",MB_OK); return; } HANDLE outFileHandle = CreateFile(fileName.c_str(),GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0); if(INVALID_HANDLE_VALUE == outFileHandle) { cout << "\n-- failure creating write file --\n"; exit(1); } do { success = ReadFile(tempHandle,&buffer,4,&numRead,0); if(numRead > 0) { block = fromHex(buffer); WriteFile(outFileHandle,&block,2,&numWritten,0); } } while(numRead > 0 && success); CloseHandle(tempHandle); CloseHandle(outFileHandle); } // //----< convert from hex temp file to binary file >-------------------- HANDLE extractFileElement(const string &fileName, HANDLE tempHandle) { BOOL success; unsigned long numWritten, numRead; unsigned short block; char buffer[4]; if(INVALID_HANDLE_VALUE == tempHandle) { tempHandle = CreateFile("hex.$$$",GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0); if(INVALID_HANDLE_VALUE == tempHandle) { MessageBox(NULL,"error reading file","File Error",MB_OK); return tempHandle; } } HANDLE outFileHandle = CreateFile(fileName.c_str(),GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0); if(INVALID_HANDLE_VALUE == outFileHandle) { cout << "\n-- failure creating write file --\n"; exit(1); } // strip of opening tag char ch; do { success = ReadFile(tempHandle,&ch,1,&numRead,0); } while(ch != '>' && success); // // convert payload from hex to binary do { success = ReadFile(tempHandle,&buffer,4,&numRead,0); if(numRead > 0) { if(buffer[0] == '<' || buffer[1] == '<' || buffer[2] == '<' || buffer[3] == '<') break; block = fromHex(buffer); WriteFile(outFileHandle,&block,2,&numWritten,0); } } while(numRead > 0 && success); // strip off closing tag do { success = ReadFile(tempHandle,&ch,1,&numRead,0); } while(ch != '>' && success); CloseHandle(outFileHandle); if(!success) return INVALID_HANDLE_VALUE; return tempHandle; } // //----< display first N bytes of string >------------------------------ void showString(ostream &out, string toDisplay, long N) { const int bytesPerLine = 60; int k = 0, size = toDisplay.size(); for(long i=0; i size-1) return; out << toDisplay[k]; } } } //----< test stub >---------------------------------------------------- #ifdef TEST_HEXREP void main(int argc, char *argv[]) { cout << "\n Testing HexRep " << "\n ================\n"; cout << "\n #1 - testing hex conversions " << "\n ------------------------------\n"; for(int i=0; i<8; i++) { int j = rand(); string hex = toHex(j); int k = fromHex(hex.c_str()); cout << "\n original int = " << j << "\n hex representation = " << hex << "\n recovered int = " << k; } cout << "\n\n"; // cout << "\n #2 - testing file conversion " << "\n ------------------------------\n"; timer t; if(argc > 1 && argv[1][0] != '/' && argv[1][0] != '-') { // not a command argument // - assume command line contains one or more file names // - attempt to convert to XML element with hex payload // and append to file hex.$$$ HANDLE tempHandle = INVALID_HANDLE_VALUE; int j = 0; t.start(); while(++j