int DeleteElement (ElementList **list, int element) { int hashindex = GetHashIndex (element); ElementList *listElement = list[hashindex]; while (listElement) { if (listElement -> element == element) { listElement -> counter--; if (listElement -> counter) return 0; else { if (listElement -> previous) { listElement -> previous -> next = listElement -> next; if (listElement -> next) listElement -> next -> previous = listElement -> previous; } else { list[hashindex] = listElement -> next; if (listElement -> next) listElement -> next -> previous = 0; } free (listElement); return 1; } } listElement = listElement -> next; } puts ("Unable to remove the element from the list"); exit (1); return 0; }
//*** _cpri__StartHash() // Functions starts a hash stack // Start a hash stack and returns the digest size. As a side effect, the // value of 'stateSize' in hashState is updated to indicate the number of bytes // of state that were saved. This function calls GetHashServer() and that function // will put the TPM into failure mode if the hash algorithm is not supported. // return type: CRTYP_RESULT // 0 hash is TPM_ALG_NULL // >0 digest size UINT16 _cpri__StartHash( TPM_ALG_ID hashAlg, // IN: hash algorithm BOOL sequence, // IN: TRUE if the state should be saved CPRI_HASH_STATE *hashState // OUT: the state of hash stack. ) { OSSL_HASH_STATE *state = (OSSL_HASH_STATE *)&hashState->state; const HASH_INFO *hashInfo = GetHashInfoPointer(hashAlg); UINT16 retVal = 0; // Not supported pAssert(sequence == FALSE); // Valid algorithm? if (hashInfo == NULL) { goto Cleanup; } if (BCryptCreateHash(g_hAlg[GetHashIndex(hashAlg)], &state->hHash, NULL, 0, NULL, 0, 0) != 0) { goto Cleanup; } retVal = _cpri__GetDigestSize(hashAlg); hashState->hashAlg = hashAlg; Cleanup: return retVal; }
// //########################################################################### // AddValueImplementation //########################################################################### // void Hash::AddValueImplementation( Plug *plug, const void *value ) { Check_Object(this); Check_Object(plug); Check_Pointer(value); // //------------------------------------------------------------- // Verify that value has not been added //------------------------------------------------------------- // Verify(HasUniqueEntries() ? (FindImplementation(value) == NULL) : (bool)true); // //------------------------------------------------------------- // Find hash entry //------------------------------------------------------------- // IteratorPosition index; index = GetHashIndex(value); // //------------------------------------------------------------- // Get vchain for this index //------------------------------------------------------------- // SortedChain *vchain; Check_Pointer(hashTable); Verify_Index(index); if ((vchain = hashTable[index]) == NULL) { vchain = MakeSortedChain(); Register_Object(vchain); hashTable[index] = vchain; } // //------------------------------------------------------------- // Add to the vchain //------------------------------------------------------------- // Check_Object(vchain); vchain->AddValuePlug(plug, value); }
int InsertElement (ElementList **list, int element) { int hashindex = GetHashIndex (element); ElementList *listElement = list[hashindex]; ElementList *prevElement = 0; while (listElement) { if (listElement -> element == element) { listElement -> counter++; return 0; } else if (listElement -> element > element) break; prevElement = listElement; listElement = listElement -> next; } ElementList *newElement = (ElementList *) malloc (sizeof(ElementList)); if (!newElement) { puts ("Unable to allocate memory for a new element"); exit (1); } newElement -> element = element; newElement -> counter = 1; newElement -> previous = 0; newElement -> next = 0; if (listElement) { if (prevElement) { newElement -> previous = prevElement; prevElement -> next = newElement; } else list[hashindex] = newElement; listElement -> previous = newElement; newElement -> next = listElement; } else { if (prevElement) { prevElement -> next = newElement; newElement -> previous = prevElement; } else list[hashindex] = newElement; } return 1; }
// //########################################################################### // FindImplementation //########################################################################### // Plug* Hash::FindImplementation( const void *value ) { Check_Object(this); Check_Pointer(value); // //------------------------------------------------------------- // Find hash entry //------------------------------------------------------------- // IteratorPosition index; index = GetHashIndex(value); // //------------------------------------------------------------- // Get vchain for this index //------------------------------------------------------------- // SortedChain *vchain; Check_Pointer(hashTable); Verify_Index(index); if ((vchain = hashTable[index]) == NULL) { return NULL; } // //------------------------------------------------------------- // Find in vchain //------------------------------------------------------------- // Check_Object(vchain); return vchain->FindPlug(value); }