// This method will grow array's MaxCount. No items will be allocated. // The allocated memory is not initialized because items could be inserted // and removed at any time - so initialization should be performed in // upper level functions like Insert() void FArray::GrowArray(int count, int elementSize) { guard(FArray::GrowArray); assert(count > 0); int prevCount = MaxCount; // check for available space if (DataCount + count > MaxCount) { // not enough space, resize ... MaxCount = ((DataCount + count + 15) / 16) * 16 + 16; if (!IsStatic()) { DataPtr = appRealloc(DataPtr, MaxCount * elementSize); } else { // "static" array becomes non-static void* oldData = DataPtr; // this is a static pointer DataPtr = appMalloc(MaxCount * elementSize); memcpy(DataPtr, oldData, prevCount * elementSize); } } unguardf("%d x %d", count, elementSize); }
/* * Decrypt. * plainText->Data is allocated by the CSP and must be freed (via * free()) by caller. */ CSSM_RETURN cdsaDecrypt( CSSM_CSP_HANDLE cspHandle, const CSSM_KEY *key, const CSSM_DATA *cipherText, CSSM_DATA_PTR plainText) { CSSM_RETURN crtn; CSSM_CC_HANDLE ccHandle; CSSM_DATA remData = {0, NULL}; uint32 bytesDecrypted; crtn = genCryptHandle(cspHandle, key, &ivCommon, &ccHandle); if(crtn) { return crtn; } plainText->Length = 0; plainText->Data = NULL; crtn = CSSM_DecryptData(ccHandle, cipherText, 1, plainText, 1, &bytesDecrypted, &remData); CSSM_DeleteContext(ccHandle); if(crtn) { return crtn; } plainText->Length = bytesDecrypted; if(remData.Length != 0) { /* append remaining data to plainText */ uint32 newLen = plainText->Length + remData.Length; plainText->Data = (uint8 *)appRealloc(plainText->Data, newLen, NULL); memmove(plainText->Data + plainText->Length, remData.Data, remData.Length); plainText->Length = newLen; appFree(remData.Data, NULL); } return CSSM_OK; }
// This method will grow array's MaxCount. No items will be allocated. // The allocated memory is not initialized because items could be inserted // and removed at any time - so initialization should be performed in // upper level functions like Insert() void FArray::GrowArray(int count, int elementSize) { guard(FArray::GrowArray); assert(count > 0); int prevCount = MaxCount; // check for available space int newCount = DataCount + count; if (newCount > MaxCount) { // Not enough space, resize ... // Allow small initial size of array const int minCount = 4; if (newCount > minCount) { MaxCount = Align(DataCount + count, 16) + 16; } else { MaxCount = minCount; } // Align memory block to reduce fragmentation int dataSize = Align(MaxCount * elementSize, 16); // Recompute MaxCount in a case if alignment increases its capacity MaxCount = dataSize / elementSize; // Reallocate memory if (!IsStatic()) { DataPtr = appRealloc(DataPtr, dataSize); } else { // "static" array becomes non-static void* oldData = DataPtr; // this is a static pointer DataPtr = appMalloc(dataSize); memcpy(DataPtr, oldData, prevCount * elementSize); } } unguardf("%d x %d", count, elementSize); }
void FArray::Insert(int index, int count, int elementSize) { guard(FArray::Insert); assert(index >= 0); assert(index <= DataCount); assert(count >= 0); if (!count) return; // check for available space if (DataCount + count > MaxCount) { // not enough space, resize ... int prevCount = MaxCount; MaxCount = ((DataCount + count + 15) / 16) * 16 + 16; if (!IsStatic()) { DataPtr = appRealloc(DataPtr, MaxCount * elementSize); } else { // "static" array becomes non-static void* oldData = DataPtr; // this is a static pointer DataPtr = appMalloc(MaxCount * elementSize); memcpy(DataPtr, oldData, prevCount * elementSize); } // zero added memory memset( (byte*)DataPtr + prevCount * elementSize, 0, (MaxCount - prevCount) * elementSize ); } // move data memmove( (byte*)DataPtr + (index + count) * elementSize, (byte*)DataPtr + index * elementSize, (DataCount - index) * elementSize ); // last operation: advance counter DataCount += count; unguard; }