Blame nss/lib/util/secitem.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Support routines for SECItem data structure.
Packit 40b132
 */
Packit 40b132
Packit 40b132
#include "seccomon.h"
Packit 40b132
#include "secitem.h"
Packit 40b132
#include "secerr.h"
Packit 40b132
#include "secport.h"
Packit 40b132
Packit 40b132
SECItem *
Packit 40b132
SECITEM_AllocItem(PLArenaPool *arena, SECItem *item, unsigned int len)
Packit 40b132
{
Packit 40b132
    SECItem *result = NULL;
Packit 40b132
    void *mark = NULL;
Packit 40b132
Packit 40b132
    if (arena != NULL) {
Packit 40b132
	mark = PORT_ArenaMark(arena);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (item == NULL) {
Packit 40b132
	if (arena != NULL) {
Packit 40b132
	    result = PORT_ArenaZAlloc(arena, sizeof(SECItem));
Packit 40b132
	} else {
Packit 40b132
	    result = PORT_ZAlloc(sizeof(SECItem));
Packit 40b132
	}
Packit 40b132
	if (result == NULL) {
Packit 40b132
	    goto loser;
Packit 40b132
	}
Packit 40b132
    } else {
Packit 40b132
	PORT_Assert(item->data == NULL);
Packit 40b132
	result = item;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    result->len = len;
Packit 40b132
    if (len) {
Packit 40b132
	if (arena != NULL) {
Packit 40b132
	    result->data = PORT_ArenaAlloc(arena, len);
Packit 40b132
	} else {
Packit 40b132
	    result->data = PORT_Alloc(len);
Packit 40b132
	}
Packit 40b132
	if (result->data == NULL) {
Packit 40b132
	    goto loser;
Packit 40b132
	}
Packit 40b132
    } else {
Packit 40b132
	result->data = NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (mark) {
Packit 40b132
	PORT_ArenaUnmark(arena, mark);
Packit 40b132
    }
Packit 40b132
    return(result);
Packit 40b132
Packit 40b132
loser:
Packit 40b132
    if ( arena != NULL ) {
Packit 40b132
	if (mark) {
Packit 40b132
	    PORT_ArenaRelease(arena, mark);
Packit 40b132
	}
Packit 40b132
	if (item != NULL) {
Packit 40b132
	    item->data = NULL;
Packit 40b132
	    item->len = 0;
Packit 40b132
	}
Packit 40b132
    } else {
Packit 40b132
	if (result != NULL) {
Packit 40b132
	    SECITEM_FreeItem(result, (item == NULL) ? PR_TRUE : PR_FALSE);
Packit 40b132
	}
Packit 40b132
	/*
Packit 40b132
	 * If item is not NULL, the above has set item->data and
Packit 40b132
	 * item->len to 0.
Packit 40b132
	 */
Packit 40b132
    }
Packit 40b132
    return(NULL);
Packit 40b132
}
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
SECITEM_ReallocItem(PLArenaPool *arena, SECItem *item, unsigned int oldlen,
Packit 40b132
		    unsigned int newlen)
Packit 40b132
{
Packit 40b132
    PORT_Assert(item != NULL);
Packit 40b132
    if (item == NULL) {
Packit 40b132
	/* XXX Set error.  But to what? */
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    /*
Packit 40b132
     * If no old length, degenerate to just plain alloc.
Packit 40b132
     */
Packit 40b132
    if (oldlen == 0) {
Packit 40b132
	PORT_Assert(item->data == NULL || item->len == 0);
Packit 40b132
	if (newlen == 0) {
Packit 40b132
	    /* Nothing to do.  Weird, but not a failure.  */
Packit 40b132
	    return SECSuccess;
Packit 40b132
	}
Packit 40b132
	item->len = newlen;
Packit 40b132
	if (arena != NULL) {
Packit 40b132
	    item->data = PORT_ArenaAlloc(arena, newlen);
Packit 40b132
	} else {
Packit 40b132
	    item->data = PORT_Alloc(newlen);
Packit 40b132
	}
Packit 40b132
    } else {
Packit 40b132
	if (arena != NULL) {
Packit 40b132
	    item->data = PORT_ArenaGrow(arena, item->data, oldlen, newlen);
Packit 40b132
	} else {
Packit 40b132
	    item->data = PORT_Realloc(item->data, newlen);
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (item->data == NULL) {
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    return SECSuccess;
Packit 40b132
}
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
SECITEM_ReallocItemV2(PLArenaPool *arena, SECItem *item, unsigned int newlen)
Packit 40b132
{
Packit 40b132
    unsigned char *newdata = NULL;
Packit 40b132
Packit 40b132
    PORT_Assert(item);
Packit 40b132
    if (!item) {
Packit 40b132
	PORT_SetError(SEC_ERROR_INVALID_ARGS);
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
    
Packit 40b132
    if (item->len == newlen) {
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (!newlen) {
Packit 40b132
	if (!arena) {
Packit 40b132
	    PORT_Free(item->data);
Packit 40b132
	}
Packit 40b132
	item->data = NULL;
Packit 40b132
	item->len = 0;
Packit 40b132
	return SECSuccess;
Packit 40b132
    }
Packit 40b132
    
Packit 40b132
    if (!item->data) {
Packit 40b132
	/* allocate fresh block of memory */
Packit 40b132
	PORT_Assert(!item->len);
Packit 40b132
	if (arena) {
Packit 40b132
	    newdata = PORT_ArenaAlloc(arena, newlen);
Packit 40b132
	} else {
Packit 40b132
	    newdata = PORT_Alloc(newlen);
Packit 40b132
	}
Packit 40b132
    } else {
Packit 40b132
	/* reallocate or adjust existing block of memory */
Packit 40b132
	if (arena) {
Packit 40b132
	    if (item->len > newlen) {
Packit 40b132
		/* There's no need to realloc a shorter block from the arena,
Packit 40b132
		 * because it would result in using even more memory!
Packit 40b132
		 * Therefore we'll continue to use the old block and 
Packit 40b132
		 * set the item to the shorter size.
Packit 40b132
		 */
Packit 40b132
		item->len = newlen;
Packit 40b132
		return SECSuccess;
Packit 40b132
	    }
Packit 40b132
	    newdata = PORT_ArenaGrow(arena, item->data, item->len, newlen);
Packit 40b132
	} else {
Packit 40b132
	    newdata = PORT_Realloc(item->data, newlen);
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (!newdata) {
Packit 40b132
	PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
	return SECFailure;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    item->len = newlen;
Packit 40b132
    item->data = newdata;
Packit 40b132
    return SECSuccess;
Packit 40b132
}
Packit 40b132
Packit 40b132
SECComparison
Packit 40b132
SECITEM_CompareItem(const SECItem *a, const SECItem *b)
Packit 40b132
{
Packit 40b132
    unsigned m;
Packit 40b132
    int rv;
Packit 40b132
Packit 40b132
    if (a == b)
Packit 40b132
    	return SECEqual;
Packit 40b132
    if (!a || !a->len || !a->data) 
Packit 40b132
        return (!b || !b->len || !b->data) ? SECEqual : SECLessThan;
Packit 40b132
    if (!b || !b->len || !b->data) 
Packit 40b132
    	return SECGreaterThan;
Packit 40b132
Packit 40b132
    m = ( ( a->len < b->len ) ? a->len : b->len );
Packit 40b132
    
Packit 40b132
    rv = PORT_Memcmp(a->data, b->data, m);
Packit 40b132
    if (rv) {
Packit 40b132
	return rv < 0 ? SECLessThan : SECGreaterThan;
Packit 40b132
    }
Packit 40b132
    if (a->len < b->len) {
Packit 40b132
	return SECLessThan;
Packit 40b132
    }
Packit 40b132
    if (a->len == b->len) {
Packit 40b132
	return SECEqual;
Packit 40b132
    }
Packit 40b132
    return SECGreaterThan;
Packit 40b132
}
Packit 40b132
Packit 40b132
PRBool
Packit 40b132
SECITEM_ItemsAreEqual(const SECItem *a, const SECItem *b)
Packit 40b132
{
Packit 40b132
    if (a->len != b->len)
Packit 40b132
        return PR_FALSE;
Packit 40b132
    if (!a->len)
Packit 40b132
    	return PR_TRUE;
Packit 40b132
    if (!a->data || !b->data) {
Packit 40b132
        /* avoid null pointer crash. */
Packit 40b132
	return (PRBool)(a->data == b->data);
Packit 40b132
    }
Packit 40b132
    return (PRBool)!PORT_Memcmp(a->data, b->data, a->len);
Packit 40b132
}
Packit 40b132
Packit 40b132
SECItem *
Packit 40b132
SECITEM_DupItem(const SECItem *from)
Packit 40b132
{
Packit 40b132
    return SECITEM_ArenaDupItem(NULL, from);
Packit 40b132
}
Packit 40b132
Packit 40b132
SECItem *
Packit 40b132
SECITEM_ArenaDupItem(PLArenaPool *arena, const SECItem *from)
Packit 40b132
{
Packit 40b132
    SECItem *to;
Packit 40b132
    
Packit 40b132
    if ( from == NULL ) {
Packit 40b132
	return(NULL);
Packit 40b132
    }
Packit 40b132
    
Packit 40b132
    if ( arena != NULL ) {
Packit 40b132
	to = (SECItem *)PORT_ArenaAlloc(arena, sizeof(SECItem));
Packit 40b132
    } else {
Packit 40b132
	to = (SECItem *)PORT_Alloc(sizeof(SECItem));
Packit 40b132
    }
Packit 40b132
    if ( to == NULL ) {
Packit 40b132
	return(NULL);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if ( arena != NULL ) {
Packit 40b132
	to->data = (unsigned char *)PORT_ArenaAlloc(arena, from->len);
Packit 40b132
    } else {
Packit 40b132
	to->data = (unsigned char *)PORT_Alloc(from->len);
Packit 40b132
    }
Packit 40b132
    if ( to->data == NULL ) {
Packit 40b132
	PORT_Free(to);
Packit 40b132
	return(NULL);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    to->len = from->len;
Packit 40b132
    to->type = from->type;
Packit 40b132
    if ( to->len ) {
Packit 40b132
	PORT_Memcpy(to->data, from->data, to->len);
Packit 40b132
    }
Packit 40b132
    
Packit 40b132
    return(to);
Packit 40b132
}
Packit 40b132
Packit 40b132
SECStatus
Packit 40b132
SECITEM_CopyItem(PLArenaPool *arena, SECItem *to, const SECItem *from)
Packit 40b132
{
Packit 40b132
    to->type = from->type;
Packit 40b132
    if (from->data && from->len) {
Packit 40b132
	if ( arena ) {
Packit 40b132
	    to->data = (unsigned char*) PORT_ArenaAlloc(arena, from->len);
Packit 40b132
	} else {
Packit 40b132
	    to->data = (unsigned char*) PORT_Alloc(from->len);
Packit 40b132
	}
Packit 40b132
	
Packit 40b132
	if (!to->data) {
Packit 40b132
	    return SECFailure;
Packit 40b132
	}
Packit 40b132
	PORT_Memcpy(to->data, from->data, from->len);
Packit 40b132
	to->len = from->len;
Packit 40b132
    } else {
Packit 40b132
	/*
Packit 40b132
	 * If from->data is NULL but from->len is nonzero, this function
Packit 40b132
	 * will succeed.  Is this right?
Packit 40b132
	 */
Packit 40b132
	to->data = 0;
Packit 40b132
	to->len = 0;
Packit 40b132
    }
Packit 40b132
    return SECSuccess;
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
SECITEM_FreeItem(SECItem *zap, PRBool freeit)
Packit 40b132
{
Packit 40b132
    if (zap) {
Packit 40b132
	PORT_Free(zap->data);
Packit 40b132
	zap->data = 0;
Packit 40b132
	zap->len = 0;
Packit 40b132
	if (freeit) {
Packit 40b132
	    PORT_Free(zap);
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
SECITEM_ZfreeItem(SECItem *zap, PRBool freeit)
Packit 40b132
{
Packit 40b132
    if (zap) {
Packit 40b132
	PORT_ZFree(zap->data, zap->len);
Packit 40b132
	zap->data = 0;
Packit 40b132
	zap->len = 0;
Packit 40b132
	if (freeit) {
Packit 40b132
	    PORT_ZFree(zap, sizeof(SECItem));
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
/* these reroutines were taken from pkix oid.c, which is supposed to
Packit 40b132
 * replace this file some day */
Packit 40b132
/*
Packit 40b132
 * This is the hash function.  We simply XOR the encoded form with
Packit 40b132
 * itself in sizeof(PLHashNumber)-byte chunks.  Improving this
Packit 40b132
 * routine is left as an excercise for the more mathematically
Packit 40b132
 * inclined student.
Packit 40b132
 */
Packit 40b132
PLHashNumber PR_CALLBACK
Packit 40b132
SECITEM_Hash ( const void *key)
Packit 40b132
{
Packit 40b132
    const SECItem *item = (const SECItem *)key;
Packit 40b132
    PLHashNumber rv = 0;
Packit 40b132
Packit 40b132
    PRUint8 *data = (PRUint8 *)item->data;
Packit 40b132
    PRUint32 i;
Packit 40b132
    PRUint8 *rvc = (PRUint8 *)&rv;
Packit 40b132
Packit 40b132
    for( i = 0; i < item->len; i++ ) {
Packit 40b132
        rvc[ i % sizeof(rv) ] ^= *data;
Packit 40b132
        data++;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * This is the key-compare function.  It simply does a lexical
Packit 40b132
 * comparison on the item data.  This does not result in
Packit 40b132
 * quite the same ordering as the "sequence of numbers" order,
Packit 40b132
 * but heck it's only used internally by the hash table anyway.
Packit 40b132
 */
Packit 40b132
PRIntn PR_CALLBACK
Packit 40b132
SECITEM_HashCompare ( const void *k1, const void *k2)
Packit 40b132
{
Packit 40b132
    const SECItem *i1 = (const SECItem *)k1;
Packit 40b132
    const SECItem *i2 = (const SECItem *)k2;
Packit 40b132
Packit 40b132
    return SECITEM_ItemsAreEqual(i1,i2);
Packit 40b132
}
Packit 40b132
Packit 40b132
SECItemArray *
Packit 40b132
SECITEM_AllocArray(PLArenaPool *arena, SECItemArray *array, unsigned int len)
Packit 40b132
{
Packit 40b132
    SECItemArray *result = NULL;
Packit 40b132
    void *mark = NULL;
Packit 40b132
Packit 40b132
    if (array != NULL && array->items != NULL) {
Packit 40b132
        PORT_Assert(0);
Packit 40b132
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
Packit 40b132
        return NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (arena != NULL) {
Packit 40b132
        mark = PORT_ArenaMark(arena);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (array == NULL) {
Packit 40b132
        if (arena != NULL) {
Packit 40b132
            result = PORT_ArenaZAlloc(arena, sizeof(SECItemArray));
Packit 40b132
        } else {
Packit 40b132
            result = PORT_ZAlloc(sizeof(SECItemArray));
Packit 40b132
        }
Packit 40b132
        if (result == NULL) {
Packit 40b132
            goto loser;
Packit 40b132
        }
Packit 40b132
    } else {
Packit 40b132
        result = array;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    result->len = len;
Packit 40b132
    if (len) {
Packit 40b132
        if (arena != NULL) {
Packit 40b132
            result->items = PORT_ArenaZNewArray(arena, SECItem, len);
Packit 40b132
        } else {
Packit 40b132
            result->items = PORT_ZNewArray(SECItem, len);
Packit 40b132
        }
Packit 40b132
        if (result->items == NULL) {
Packit 40b132
            goto loser;
Packit 40b132
        }
Packit 40b132
    } else {
Packit 40b132
        result->items = NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (mark) {
Packit 40b132
        PORT_ArenaUnmark(arena, mark);
Packit 40b132
    }
Packit 40b132
    return result;
Packit 40b132
Packit 40b132
loser:
Packit 40b132
    if ( arena != NULL ) {
Packit 40b132
        if (mark) {
Packit 40b132
            PORT_ArenaRelease(arena, mark);
Packit 40b132
        }
Packit 40b132
    } else {
Packit 40b132
        if (result != NULL && array == NULL) {
Packit 40b132
            PORT_Free(result);
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
    if (array != NULL) {
Packit 40b132
        array->items = NULL;
Packit 40b132
        array->len = 0;
Packit 40b132
    }
Packit 40b132
    return NULL;
Packit 40b132
}
Packit 40b132
Packit 40b132
static void
Packit 40b132
secitem_FreeArray(SECItemArray *array, PRBool zero_items, PRBool freeit)
Packit 40b132
{
Packit 40b132
    unsigned int i;
Packit 40b132
Packit 40b132
    if (!array || !array->len || !array->items)
Packit 40b132
        return;
Packit 40b132
Packit 40b132
    for (i = 0; i < array->len; ++i) {
Packit 40b132
        SECItem *item = &array->items[i];
Packit 40b132
Packit 40b132
        if (item->data) {
Packit 40b132
            if (zero_items) {
Packit 40b132
                SECITEM_ZfreeItem(item, PR_FALSE);
Packit 40b132
            } else {
Packit 40b132
                SECITEM_FreeItem(item, PR_FALSE);
Packit 40b132
            }
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
    PORT_Free(array->items);
Packit 40b132
    array->items = NULL;
Packit 40b132
    array->len = 0;
Packit 40b132
Packit 40b132
    if (freeit)
Packit 40b132
        PORT_Free(array);
Packit 40b132
}
Packit 40b132
Packit 40b132
void SECITEM_FreeArray(SECItemArray *array, PRBool freeit)
Packit 40b132
{
Packit 40b132
    secitem_FreeArray(array, PR_FALSE, freeit);
Packit 40b132
}
Packit 40b132
Packit 40b132
void SECITEM_ZfreeArray(SECItemArray *array, PRBool freeit)
Packit 40b132
{
Packit 40b132
    secitem_FreeArray(array, PR_TRUE, freeit);
Packit 40b132
}
Packit 40b132
Packit 40b132
SECItemArray *
Packit 40b132
SECITEM_DupArray(PLArenaPool *arena, const SECItemArray *from)
Packit 40b132
{
Packit 40b132
    SECItemArray *result;
Packit 40b132
    unsigned int i;
Packit 40b132
Packit 40b132
    /* Require a "from" array.
Packit 40b132
     * Reject an inconsistent "from" array with NULL data and nonzero length.
Packit 40b132
     * However, allow a "from" array of zero length.
Packit 40b132
     */
Packit 40b132
    if (!from || (!from->items && from->len))
Packit 40b132
        return NULL;
Packit 40b132
Packit 40b132
    result = SECITEM_AllocArray(arena, NULL, from->len);
Packit 40b132
    if (!result)
Packit 40b132
        return NULL;
Packit 40b132
Packit 40b132
    for (i = 0; i < from->len; ++i) {
Packit 40b132
        SECStatus rv = SECITEM_CopyItem(arena,
Packit 40b132
                                        &result->items[i], &from->items[i]);
Packit 40b132
        if (rv != SECSuccess) {
Packit 40b132
            SECITEM_ZfreeArray(result, PR_TRUE);
Packit 40b132
            return NULL;
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
Packit 40b132
    return result;
Packit 40b132
}