X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/bac41a7b9a0a9254fa30f8bb6e6038ab71a483e2..ce0ac947b4708d0bc1c7e6789b3e1f3bfc80d6e9:/cdsa/cdsa_utilities/cssmdb.cpp diff --git a/cdsa/cdsa_utilities/cssmdb.cpp b/cdsa/cdsa_utilities/cssmdb.cpp index 4d276249..aa779b0a 100644 --- a/cdsa/cdsa_utilities/cssmdb.cpp +++ b/cdsa/cdsa_utilities/cssmdb.cpp @@ -26,43 +26,6 @@ #include -#if 0 -// XXX Obsolete -CSSM_RETURN AddFooToIntelList( void** theIntelListToAddItTo, unsigned long* theNumberOfThingsAlreadyInTheList, const void* theThingToAdd, size_t theSizeOfTheThingToAdd) -{ // this is to make adding things to Intel LISTs (also called Arrays by the rest of us) easy! We do it everywhere! Join the fun! - CSSM_RETURN result = CSSM_OK; - void* theReallocatedBuffer = NULL; - if( *theIntelListToAddItTo == NULL ) - { - - *theIntelListToAddItTo = malloc(theSizeOfTheThingToAdd); - if(!*theIntelListToAddItTo) - { - result = CSSMERR_CSSM_MEMORY_ERROR; - } - } - else - { - theReallocatedBuffer = realloc((void*)*theIntelListToAddItTo, (*theNumberOfThingsAlreadyInTheList+1) * (theSizeOfTheThingToAdd) ); - if(!theReallocatedBuffer) - { - result = CSSMERR_CSSM_MEMORY_ERROR; - } - else - { - *theIntelListToAddItTo = theReallocatedBuffer; - } - } - - if(result == CSSM_OK ) - { - memcpy( (void*)((char*)*theIntelListToAddItTo+(theSizeOfTheThingToAdd * (*theNumberOfThingsAlreadyInTheList))), theThingToAdd, theSizeOfTheThingToAdd); - (*theNumberOfThingsAlreadyInTheList)++; - } - - return result; -} -#endif // // CssmDbAttributeInfo @@ -126,11 +89,12 @@ CssmDbAttributeData::deleteValues(CssmAllocator &inAllocator) { for (uint32 anIndex = 0; anIndex < NumberOfValues; anIndex++) { - if (Value[anIndex].Length) + if (Value[anIndex].Data) { inAllocator.free(Value[anIndex].Data); - Value[anIndex].Length = 0; } + + Value[anIndex].Length = 0; } inAllocator.free(Value); @@ -262,10 +226,90 @@ CssmAutoDbRecordAttributeData::clear() ArrayBuilder::clear(); } + + +static bool CompareAttributeInfos (const CSSM_DB_ATTRIBUTE_INFO &a, const CSSM_DB_ATTRIBUTE_INFO &b) +{ + // check the format of the names + if (a.AttributeNameFormat != b.AttributeNameFormat) + { + return false; + } + + switch (a.AttributeNameFormat) + { + case CSSM_DB_ATTRIBUTE_NAME_AS_STRING: + { + return strcmp (a.Label.AttributeName, b.Label.AttributeName) == 0; + } + + case CSSM_DB_ATTRIBUTE_NAME_AS_OID: + { + if (a.Label.AttributeOID.Length != b.Label.AttributeOID.Length) + { + return false; + } + + return memcmp (a.Label.AttributeOID.Data, b.Label.AttributeOID.Data, a.Label.AttributeOID.Length) == 0; + } + + + case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER: + { + return a.Label.AttributeID == b.Label.AttributeID; + } + } + + return true; // just to keep the compiler from complaining +} + + + +CssmDbAttributeData* CssmAutoDbRecordAttributeData::findAttribute (const CSSM_DB_ATTRIBUTE_INFO &info) +{ + // walk through the data, looking for an attribute of the same type + unsigned i; + for (i = 0; i < size (); ++i) + { + CssmDbAttributeData& d = at (i); + CSSM_DB_ATTRIBUTE_INFO &inInfo = d.info (); + + if (CompareAttributeInfos (info, inInfo)) + { + return &d; + } + } + + // found nothing? + return NULL; +} + + + +CssmDbAttributeData& CssmAutoDbRecordAttributeData::getAttributeReference (const CSSM_DB_ATTRIBUTE_INFO &info) +{ + // Either find an existing reference to an attribute in the list, or make a new one. + CssmDbAttributeData *anAttr = findAttribute (info); + if (anAttr) // was this already in the list? + { + // clean it up + anAttr->deleteValues (mValueAllocator); + } + else + { + // make a new one + anAttr = &add(); + } + + return *anAttr; +} + + + CssmDbAttributeData & CssmAutoDbRecordAttributeData::add(const CSSM_DB_ATTRIBUTE_INFO &info) { - CssmDbAttributeData &anAttr = add(); + CssmDbAttributeData& anAttr = getAttributeReference (info); anAttr.info(info); return anAttr; } @@ -273,7 +317,7 @@ CssmAutoDbRecordAttributeData::add(const CSSM_DB_ATTRIBUTE_INFO &info) CssmDbAttributeData & CssmAutoDbRecordAttributeData::add(const CSSM_DB_ATTRIBUTE_INFO &info, const CssmPolyData &value) { - CssmDbAttributeData &anAttr = add(); + CssmDbAttributeData &anAttr = getAttributeReference (info); anAttr.set(info, value, mValueAllocator); return anAttr; } @@ -282,7 +326,7 @@ CssmAutoDbRecordAttributeData::add(const CSSM_DB_ATTRIBUTE_INFO &info, const Css // CssmAutoQuery // CssmAutoQuery::CssmAutoQuery(const CSSM_QUERY &query, CssmAllocator &allocator) -: ArrayBuilder(static_cast(SelectionPredicate), +: ArrayBuilder(CssmSelectionPredicate::overlayVar(SelectionPredicate), NumSelectionPredicates, query.NumSelectionPredicates, allocator) {