static void * nss_zalloc_arena_locked ( NSSArena *arena, PRUint32 size ) { void *p; void *rv; struct pointer_header *h; PRUint32 my_size = size + sizeof(struct pointer_header); do { PLArena *_a = (&arena->pool)->current; PRUint32 _nb = (((PRUword)(my_size) + (&arena->pool)->mask) & ~(&arena->pool)->mask); PRUword _p = _a->avail; PRUword _q = _p + _nb; if (_q > _a->limit) _p = (PRUword)PL_ArenaAllocate(&arena->pool, _nb); else _a->avail = _q; p = (void *)_p; ; } while (0); if( (void *)((void *)0) == p ) { nss_SetError(NSS_ERROR_NO_MEMORY); return (void *)((void *)0); } h = (struct pointer_header *)p; h->arena = arena; h->size = size; rv = (void *)((char *)h + sizeof(struct pointer_header)); (void)nsslibc_memset(rv, 0, size); return rv; }
NSS_IMPLEMENT PRStatus nssUTF8_CopyIntoFixedBuffer(NSSUTF8 *string, char *buffer, PRUint32 bufferSize, char pad) { PRUint32 stringSize = 0; #ifdef NSSDEBUG if ((char *)NULL == buffer) { nss_SetError(NSS_ERROR_INVALID_POINTER); return PR_FALSE; } if (0 == bufferSize) { nss_SetError(NSS_ERROR_INVALID_ARGUMENT); return PR_FALSE; } if ((pad & 0x80) != 0x00) { nss_SetError(NSS_ERROR_INVALID_ARGUMENT); return PR_FALSE; } #endif /* NSSDEBUG */ if ((NSSUTF8 *)NULL == string) { string = (NSSUTF8 *)""; } stringSize = nssUTF8_Size(string, (PRStatus *)NULL); stringSize--; /* don't count the trailing null */ if (stringSize > bufferSize) { PRUint32 bs = bufferSize; (void)nsslibc_memcpy(buffer, string, bufferSize); if ((((buffer[bs - 1] & 0x80) == 0x00)) || ((bs > 1) && ((buffer[bs - 2] & 0xE0) == 0xC0)) || ((bs > 2) && ((buffer[bs - 3] & 0xF0) == 0xE0)) || ((bs > 3) && ((buffer[bs - 4] & 0xF8) == 0xF0)) || ((bs > 4) && ((buffer[bs - 5] & 0xFC) == 0xF8)) || ((bs > 5) && ((buffer[bs - 6] & 0xFE) == 0xFC))) { /* It fit exactly */ return PR_SUCCESS; } /* Too long. We have to trim the last character */ for (/*bs*/; bs != 0; bs--) { if ((buffer[bs - 1] & 0xC0) != 0x80) { buffer[bs - 1] = pad; break; } else { buffer[bs - 1] = pad; } } } else { (void)nsslibc_memset(buffer, pad, bufferSize); (void)nsslibc_memcpy(buffer, string, stringSize); } return PR_SUCCESS; }
/* # 955 "arena.c" */ PRStatus nss_ZFreeIf ( void *pointer ) { struct pointer_header *h; if( (void *)((void *)0) == pointer ) { return PR_SUCCESS; } h = (struct pointer_header *)((char *)pointer - sizeof(struct pointer_header)); if( (NSSArena *)((void *)0) == h->arena ) { (void)nsslibc_memset(pointer, 0, h->size); PR_Free(h); return PR_SUCCESS; } else { if( (PRLock *)((void *)0) == h->arena->lock ) { nss_SetError(NSS_ERROR_INVALID_POINTER); return PR_FAILURE; } PR_Lock(h->arena->lock); (void)nsslibc_memset(pointer, 0, h->size); PR_Unlock(h->arena->lock); return PR_SUCCESS; } }
/* find all the certs that represent the appropriate object (cert, priv key, or * pub key) in the cert store. */ static PRUint32 collect_class( CK_OBJECT_CLASS objClass, SecItemClass itemClass, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulAttributeCount, ckmkInternalObject ***listp, PRUint32 *sizep, PRUint32 count, CK_RV *pError ) { ckmkInternalObject *next = NULL; SecKeychainSearchRef searchRef = 0; SecKeychainItemRef itemRef = 0; OSStatus error; /* future, build the attribute list based on the template * so we can refine the search */ error = SecKeychainSearchCreateFromAttributes( NULL, itemClass, NULL, &searchRef); while (noErr == SecKeychainSearchCopyNext(searchRef, &itemRef)) { /* if we don't have an internal object structure, get one */ if ((ckmkInternalObject *)NULL == next) { next = nss_ZNEW(NULL, ckmkInternalObject); if ((ckmkInternalObject *)NULL == next) { *pError = CKR_HOST_MEMORY; goto loser; } } /* fill in the relevant object data */ next->type = ckmkItem; next->objClass = objClass; next->u.item.itemRef = itemRef; next->u.item.itemClass = itemClass; /* see if this is one of the objects we are looking for */ if( CK_TRUE == ckmk_match(pTemplate, ulAttributeCount, next) ) { /* yes, put it on the list */ PUT_OBJECT(next, *pError, *sizep, count, *listp); next = NULL; /* this one is on the list, need to allocate a new one now */ } else { /* no , release the current item and clear out the structure for reuse */ CFRelease(itemRef); /* don't cache the values we just loaded */ nsslibc_memset(next, 0, sizeof(*next)); } } loser: if (searchRef) { CFRelease(searchRef); } nss_ZFreeIf(next); return count; }
/* # 1022 "arena.c" */ extern void * nss_ZRealloc ( void *pointer, PRUint32 newSize ) { NSSArena *arena; struct pointer_header *h, *new_h; PRUint32 my_newSize = newSize + sizeof(struct pointer_header); void *rv; if( my_newSize < sizeof(struct pointer_header) ) { nss_SetError(NSS_ERROR_NO_MEMORY); return (void *)((void *)0); } if( (void *)((void *)0) == pointer ) { nss_SetError(NSS_ERROR_INVALID_POINTER); return (void *)((void *)0); } h = (struct pointer_header *)((char *)pointer - sizeof(struct pointer_header)); if( newSize == h->size ) { return pointer; } arena = h->arena; if (!arena) { new_h = (struct pointer_header *)PR_Calloc(1, my_newSize); if( (struct pointer_header *)((void *)0) == new_h ) { nss_SetError(NSS_ERROR_NO_MEMORY); return (void *)((void *)0); } new_h->arena = (NSSArena *)((void *)0); new_h->size = newSize; rv = (void *)((char *)new_h + sizeof(struct pointer_header)); if( newSize > h->size ) { (void)nsslibc_memcpy(rv, pointer, h->size); (void)nsslibc_memset(&((char *)rv)[ h->size ], 0, (newSize - h->size)); } else { (void)nsslibc_memcpy(rv, pointer, newSize); } (void)nsslibc_memset(pointer, 0, h->size); h->size = 0; PR_Free(h); return rv; } else { void *p; if (!arena->lock) { nss_SetError(NSS_ERROR_INVALID_POINTER); return (void *)((void *)0); } PR_Lock(arena->lock); /* # 1107 "arena.c" */ if( newSize < h->size ) { /* # 1120 "arena.c" */ char *extra = &((char *)pointer)[ newSize ]; (void)nsslibc_memset(extra, 0, (h->size - newSize)); PR_Unlock(arena->lock); return pointer; } do { PLArena *_a = (&arena->pool)->current; PRUint32 _nb = (((PRUword)(my_newSize) + (&arena->pool)->mask) & ~(&arena->pool)->mask); PRUword _p = _a->avail; PRUword _q = _p + _nb; if (_q > _a->limit) _p = (PRUword)PL_ArenaAllocate(&arena->pool, _nb); else _a->avail = _q; p = (void *)_p; ; } while (0); if( (void *)((void *)0) == p ) { PR_Unlock(arena->lock); nss_SetError(NSS_ERROR_NO_MEMORY); return (void *)((void *)0); } new_h = (struct pointer_header *)p; new_h->arena = arena; new_h->size = newSize; rv = (void *)((char *)new_h + sizeof(struct pointer_header)); if (rv != pointer) { (void)nsslibc_memcpy(rv, pointer, h->size); (void)nsslibc_memset(pointer, 0, h->size); } (void)nsslibc_memset(&((char *)rv)[ h->size ], 0, (newSize - h->size)); h->arena = (NSSArena *)((void *)0); h->size = 0; PR_Unlock(arena->lock); return rv; } }