Definition at line 35 of file compare.cpp. 00035 { 00036 /*declaration block*/ 00037 char* currentOutput = NULL; 00038 int lengthCurrentOutput = 0; 00039 char* iterationWord = NULL; 00040 usedIntType* pVariableIntPart = NULL; 00041 bool found = false; 00042 bool methodSupported = true; 00043 int index = 0; 00044 cryptoFunction function = NULL; 00045 00046 00047 /*input validation*/ 00048 if ( referenceOutputBytes == NULL ) { 00049 printf("compare(): no reference hash delivered. exiting...\n"); 00050 return DETECT_BYTES_WRONG_PARAMETER_ERROR; 00051 } 00052 00053 00054 /*look for a function pointer for the given crypto method*/ 00055 function = getFunctionPointer(cryptoMethod); 00056 if ( !function ) { 00057 //printf("compare(): there is no crypto function \"%s\" specified in \"cryptoFunctions.h\". exiting...\n", cryptoMethod); 00058 return DETECT_BYTES_METHOD_NOT_SUPPORTED; 00059 } 00060 00061 00062 /*initialization block*/ 00063 currentOutput = NULL; /*the output length depends on the crypto method and space is alocated in the implementation therefore*/ 00064 /*allocates space for the preset bytes, the used int type plus one '\0'*/ 00065 /*calloc sets all allocated bytes to null*/ 00066 iterationWord = (char*)calloc(LENGTH_GIVEN_BYTES+LENGTH_DETECTED_BYTES+1, 1); 00067 /*copy the preset bytes to our input word*/ 00068 index = 0; 00069 while ( index < LENGTH_GIVEN_BYTES ) { 00070 iterationWord[index] = presetIterationBytes[index]; 00071 index++; 00072 } 00073 /*mark the beginning of the input word's variable part int pointer*/ 00074 pVariableIntPart = (usedIntType*)(iterationWord + LENGTH_GIVEN_BYTES); 00075 00076 /*do the main comparison loop*/ 00077 found = false; 00078 methodSupported = true; 00079 do { 00080 /*create a cancelation point in the main loop*/ 00081 pthread_testcancel(); 00082 #ifdef _DEBUG_ 00083 /*debug-output*/ 00084 printf("%u: \"%s\" \n",*pVariableIntPart , iterationWord); 00085 #endif 00086 /*calculate the hash to the current input word*/ 00087 lengthCurrentOutput = function((const unsigned char*)referenceInputBytes, inputLength, (const unsigned char*)iterationWord, (unsigned char**)¤tOutput); 00088 if (lengthCurrentOutput == DETECT_BYTES_METHOD_NOT_SUPPORTED) { 00089 methodSupported = false; 00090 } 00091 #ifdef _DEBUG_ 00092 if ( referenceInputBytes ) { 00093 printf("Input: "); 00094 printHashBytes((const char*)referenceInputBytes,inputLength); 00095 printf("\n"); 00096 } 00097 printf("Iteration: "); 00098 printHashBytes((const char*)iterationWord,LENGTH_ITERATION_WORD); 00099 printf("\n"); 00100 printf("Output: "); 00101 printHashBytes(currentOutput,lengthCurrentOutput); 00102 printf("\n\n"); 00103 #endif 00104 if (methodSupported) { 00105 /*check for match*/ 00106 found = isEqual(currentOutput, referenceOutputBytes, lengthCurrentOutput); 00107 } 00108 /*increase the variable int part of the word*/ 00109 (*pVariableIntPart)++; 00110 //sleep(2); 00111 } while ( !found && methodSupported && *pVariableIntPart > 0 ); 00112 00113 /*check if a match was found*/ 00114 if ( found ) { 00115 (*pVariableIntPart)--; 00116 *((usedIntType*)detectedIterationBytes) = *pVariableIntPart; 00117 } 00118 00119 /*free the corresponding variables*/ 00120 free(currentOutput); 00121 free(iterationWord); 00122 00123 /*return the end status of comparing*/ 00124 if (found) { 00125 return DETECT_BYTES_DETECTED; 00126 } 00127 else if (methodSupported) { 00128 return DETECT_BYTES_NOT_DETECTED; 00129 } 00130 /*else*/ 00131 return DETECT_BYTES_METHOD_NOT_SUPPORTED; 00132 }
|
1.5.1