Blame nss/lib/util/secport.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
 * secport.c - portability interfaces for security libraries
Packit 40b132
 *
Packit 40b132
 * This file abstracts out libc functionality that libsec depends on
Packit 40b132
 * 
Packit 40b132
 * NOTE - These are not public interfaces
Packit 40b132
 */
Packit 40b132
Packit 40b132
#include "seccomon.h"
Packit 40b132
#include "prmem.h"
Packit 40b132
#include "prerror.h"
Packit 40b132
#include "plarena.h"
Packit 40b132
#include "secerr.h"
Packit 40b132
#include "prmon.h"
Packit 40b132
#include "nssilock.h"
Packit 40b132
#include "secport.h"
Packit 40b132
#include "prenv.h"
Packit 40b132
Packit 40b132
#ifdef DEBUG
Packit 40b132
#define THREADMARK
Packit 40b132
#endif /* DEBUG */
Packit 40b132
Packit 40b132
#ifdef THREADMARK
Packit 40b132
#include "prthread.h"
Packit 40b132
#endif /* THREADMARK */
Packit 40b132
Packit 40b132
#if defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS)
Packit 40b132
#include <stdlib.h>
Packit 40b132
#else
Packit 40b132
#include "wtypes.h"
Packit 40b132
#endif
Packit 40b132
Packit 40b132
#define SET_ERROR_CODE	/* place holder for code to set PR error code. */
Packit 40b132
Packit 40b132
#ifdef THREADMARK
Packit 40b132
typedef struct threadmark_mark_str {
Packit 40b132
  struct threadmark_mark_str *next;
Packit 40b132
  void *mark;
Packit 40b132
} threadmark_mark;
Packit 40b132
Packit 40b132
#endif /* THREADMARK */
Packit 40b132
Packit 40b132
/* The value of this magic must change each time PORTArenaPool changes. */
Packit 40b132
#define ARENAPOOL_MAGIC 0xB8AC9BDF 
Packit 40b132
Packit 40b132
typedef struct PORTArenaPool_str {
Packit 40b132
  PLArenaPool arena;
Packit 40b132
  PRUint32    magic;
Packit 40b132
  PRLock *    lock;
Packit 40b132
#ifdef THREADMARK
Packit 40b132
  PRThread *marking_thread;
Packit 40b132
  threadmark_mark *first_mark;
Packit 40b132
#endif
Packit 40b132
} PORTArenaPool;
Packit 40b132
Packit 40b132
Packit 40b132
/* count of allocation failures. */
Packit 40b132
unsigned long port_allocFailures;
Packit 40b132
Packit 40b132
/* locations for registering Unicode conversion functions.  
Packit 40b132
 * XXX is this the appropriate location?  or should they be
Packit 40b132
 *     moved to client/server specific locations?
Packit 40b132
 */
Packit 40b132
PORTCharConversionFunc ucs4Utf8ConvertFunc;
Packit 40b132
PORTCharConversionFunc ucs2Utf8ConvertFunc;
Packit 40b132
PORTCharConversionWSwapFunc  ucs2AsciiConvertFunc;
Packit 40b132
Packit 40b132
/* NSPR memory allocation functions (PR_Malloc, PR_Calloc, and PR_Realloc)
Packit 40b132
 * use the PRUint32 type for the size parameter. Before we pass a size_t or
Packit 40b132
 * unsigned long size to these functions, we need to ensure it is <= half of
Packit 40b132
 * the maximum PRUint32 value to avoid truncation and catch a negative size.
Packit 40b132
 */
Packit 40b132
#define MAX_SIZE (PR_UINT32_MAX >> 1)
Packit 40b132
Packit 40b132
void *
Packit 40b132
PORT_Alloc(size_t bytes)
Packit 40b132
{
Packit 40b132
    void *rv = NULL;
Packit 40b132
Packit 40b132
    if (bytes <= MAX_SIZE) {
Packit 40b132
	/* Always allocate a non-zero amount of bytes */
Packit 40b132
	rv = PR_Malloc(bytes ? bytes : 1);
Packit 40b132
    }
Packit 40b132
    if (!rv) {
Packit 40b132
	++port_allocFailures;
Packit 40b132
	PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
    }
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
void *
Packit 40b132
PORT_Realloc(void *oldptr, size_t bytes)
Packit 40b132
{
Packit 40b132
    void *rv = NULL;
Packit 40b132
Packit 40b132
    if (bytes <= MAX_SIZE) {
Packit 40b132
	rv = PR_Realloc(oldptr, bytes);
Packit 40b132
    }
Packit 40b132
    if (!rv) {
Packit 40b132
	++port_allocFailures;
Packit 40b132
	PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
    }
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
void *
Packit 40b132
PORT_ZAlloc(size_t bytes)
Packit 40b132
{
Packit 40b132
    void *rv = NULL;
Packit 40b132
Packit 40b132
    if (bytes <= MAX_SIZE) {
Packit 40b132
	/* Always allocate a non-zero amount of bytes */
Packit 40b132
	rv = PR_Calloc(1, bytes ? bytes : 1);
Packit 40b132
    }
Packit 40b132
    if (!rv) {
Packit 40b132
	++port_allocFailures;
Packit 40b132
	PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
    }
Packit 40b132
    return rv;
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
PORT_Free(void *ptr)
Packit 40b132
{
Packit 40b132
    if (ptr) {
Packit 40b132
	PR_Free(ptr);
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
PORT_ZFree(void *ptr, size_t len)
Packit 40b132
{
Packit 40b132
    if (ptr) {
Packit 40b132
	memset(ptr, 0, len);
Packit 40b132
	PR_Free(ptr);
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
char *
Packit 40b132
PORT_Strdup(const char *str)
Packit 40b132
{
Packit 40b132
    size_t len = PORT_Strlen(str)+1;
Packit 40b132
    char *newstr;
Packit 40b132
Packit 40b132
    newstr = (char *)PORT_Alloc(len);
Packit 40b132
    if (newstr) {
Packit 40b132
        PORT_Memcpy(newstr, str, len);
Packit 40b132
    }
Packit 40b132
    return newstr;
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
PORT_SetError(int value)
Packit 40b132
{	
Packit 40b132
#ifdef DEBUG_jp96085
Packit 40b132
    PORT_Assert(value != SEC_ERROR_REUSED_ISSUER_AND_SERIAL);
Packit 40b132
#endif
Packit 40b132
    PR_SetError(value, 0);
Packit 40b132
    return;
Packit 40b132
}
Packit 40b132
Packit 40b132
int
Packit 40b132
PORT_GetError(void)
Packit 40b132
{
Packit 40b132
    return(PR_GetError());
Packit 40b132
}
Packit 40b132
Packit 40b132
/********************* Arena code follows *****************************
Packit 40b132
 * ArenaPools are like heaps.  The memory in them consists of large blocks,
Packit 40b132
 * called arenas, which are allocated from the/a system heap.  Inside an
Packit 40b132
 * ArenaPool, the arenas are organized as if they were in a stack.  Newly
Packit 40b132
 * allocated arenas are "pushed" on that stack.  When you attempt to
Packit 40b132
 * allocate memory from an ArenaPool, the code first looks to see if there
Packit 40b132
 * is enough unused space in the top arena on the stack to satisfy your
Packit 40b132
 * request, and if so, your request is satisfied from that arena.
Packit 40b132
 * Otherwise, a new arena is allocated (or taken from NSPR's list of freed
Packit 40b132
 * arenas) and pushed on to the stack.  The new arena is always big enough
Packit 40b132
 * to satisfy the request, and is also at least a minimum size that is
Packit 40b132
 * established at the time that the ArenaPool is created.
Packit 40b132
 *
Packit 40b132
 * The ArenaMark function returns the address of a marker in the arena at
Packit 40b132
 * the top of the arena stack.  It is the address of the place in the arena
Packit 40b132
 * on the top of the arena stack from which the next block of memory will
Packit 40b132
 * be allocated.  Each ArenaPool has its own separate stack, and hence
Packit 40b132
 * marks are only relevant to the ArenaPool from which they are gotten.
Packit 40b132
 * Marks may be nested.  That is, a thread can get a mark, and then get
Packit 40b132
 * another mark.
Packit 40b132
 *
Packit 40b132
 * It is intended that all the marks in an ArenaPool may only be owned by a
Packit 40b132
 * single thread.  In DEBUG builds, this is enforced.  In non-DEBUG builds,
Packit 40b132
 * it is not.  In DEBUG builds, when a thread gets a mark from an
Packit 40b132
 * ArenaPool, no other thread may acquire a mark in that ArenaPool while
Packit 40b132
 * that mark exists, that is, until that mark is unmarked or released.
Packit 40b132
 * Therefore, it is important that every mark be unmarked or released when
Packit 40b132
 * the creating thread has no further need for exclusive ownership of the
Packit 40b132
 * right to manage the ArenaPool.
Packit 40b132
 *
Packit 40b132
 * The ArenaUnmark function discards the ArenaMark at the address given,
Packit 40b132
 * and all marks nested inside that mark (that is, acquired from that same
Packit 40b132
 * ArenaPool while that mark existed).   It is an error for a thread other
Packit 40b132
 * than the mark's creator to try to unmark it.  When a thread has unmarked
Packit 40b132
 * all its marks from an ArenaPool, then another thread is able to set
Packit 40b132
 * marks in that ArenaPool.  ArenaUnmark does not deallocate (or "pop") any
Packit 40b132
 * memory allocated from the ArenaPool since the mark was created.
Packit 40b132
 *
Packit 40b132
 * ArenaRelease "pops" the stack back to the mark, deallocating all the
Packit 40b132
 * memory allocated from the arenas in the ArenaPool since that mark was
Packit 40b132
 * created, and removing any arenas from the ArenaPool that have no
Packit 40b132
 * remaining active allocations when that is done.  It implicitly releases
Packit 40b132
 * any marks nested inside the mark being explicitly released.  It is the
Packit 40b132
 * only operation, other than destroying the arenapool, that potentially
Packit 40b132
 * reduces the number of arenas on the stack.  Otherwise, the stack grows
Packit 40b132
 * until the arenapool is destroyed, at which point all the arenas are
Packit 40b132
 * freed or returned to a "free arena list", depending on their sizes.
Packit 40b132
 */
Packit 40b132
PLArenaPool *
Packit 40b132
PORT_NewArena(unsigned long chunksize)
Packit 40b132
{
Packit 40b132
    PORTArenaPool *pool;
Packit 40b132
    
Packit 40b132
    if (chunksize > MAX_SIZE) {
Packit 40b132
	PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
    pool = PORT_ZNew(PORTArenaPool);
Packit 40b132
    if (!pool) {
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
    pool->magic = ARENAPOOL_MAGIC;
Packit 40b132
    pool->lock = PZ_NewLock(nssILockArena);
Packit 40b132
    if (!pool->lock) {
Packit 40b132
	++port_allocFailures;
Packit 40b132
	PORT_Free(pool);
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
    PL_InitArenaPool(&pool->arena, "security", chunksize, sizeof(double));
Packit 40b132
    return(&pool->arena);
Packit 40b132
}
Packit 40b132
Packit 40b132
void *
Packit 40b132
PORT_ArenaAlloc(PLArenaPool *arena, size_t size)
Packit 40b132
{
Packit 40b132
    void *p = NULL;
Packit 40b132
Packit 40b132
    PORTArenaPool *pool = (PORTArenaPool *)arena;
Packit 40b132
Packit 40b132
    if (size <= 0) {
Packit 40b132
	size = 1;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (size > MAX_SIZE) {
Packit 40b132
	/* you lose. */
Packit 40b132
    } else 
Packit 40b132
    /* Is it one of ours?  Assume so and check the magic */
Packit 40b132
    if (ARENAPOOL_MAGIC == pool->magic ) {
Packit 40b132
	PZ_Lock(pool->lock);
Packit 40b132
#ifdef THREADMARK
Packit 40b132
        /* Most likely one of ours.  Is there a thread id? */
Packit 40b132
	if (pool->marking_thread  &&
Packit 40b132
	    pool->marking_thread != PR_GetCurrentThread() ) {
Packit 40b132
	    /* Another thread holds a mark in this arena */
Packit 40b132
	    PZ_Unlock(pool->lock);
Packit 40b132
	    PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
	    PORT_Assert(0);
Packit 40b132
	    return NULL;
Packit 40b132
	} /* tid != null */
Packit 40b132
#endif /* THREADMARK */
Packit 40b132
	PL_ARENA_ALLOCATE(p, arena, size);
Packit 40b132
	PZ_Unlock(pool->lock);
Packit 40b132
    } else {
Packit 40b132
	PL_ARENA_ALLOCATE(p, arena, size);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (!p) {
Packit 40b132
	++port_allocFailures;
Packit 40b132
	PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    return(p);
Packit 40b132
}
Packit 40b132
Packit 40b132
void *
Packit 40b132
PORT_ArenaZAlloc(PLArenaPool *arena, size_t size)
Packit 40b132
{
Packit 40b132
    void *p;
Packit 40b132
Packit 40b132
    if (size <= 0)
Packit 40b132
        size = 1;
Packit 40b132
Packit 40b132
    p = PORT_ArenaAlloc(arena, size);
Packit 40b132
Packit 40b132
    if (p) {
Packit 40b132
	PORT_Memset(p, 0, size);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    return(p);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * If zero is true, zeroize the arena memory before freeing it.
Packit 40b132
 */
Packit 40b132
void
Packit 40b132
PORT_FreeArena(PLArenaPool *arena, PRBool zero)
Packit 40b132
{
Packit 40b132
    PORTArenaPool *pool = (PORTArenaPool *)arena;
Packit 40b132
    PRLock *       lock = (PRLock *)0;
Packit 40b132
    size_t         len  = sizeof *arena;
Packit 40b132
    static PRBool  checkedEnv = PR_FALSE;
Packit 40b132
    static PRBool  doFreeArenaPool = PR_FALSE;
Packit 40b132
Packit 40b132
    if (!pool)
Packit 40b132
    	return;
Packit 40b132
    if (ARENAPOOL_MAGIC == pool->magic ) {
Packit 40b132
	len  = sizeof *pool;
Packit 40b132
	lock = pool->lock;
Packit 40b132
	PZ_Lock(lock);
Packit 40b132
    }
Packit 40b132
    if (!checkedEnv) {
Packit 40b132
	/* no need for thread protection here */
Packit 40b132
	doFreeArenaPool = (PR_GetEnv("NSS_DISABLE_ARENA_FREE_LIST") == NULL);
Packit 40b132
	checkedEnv = PR_TRUE;
Packit 40b132
    }
Packit 40b132
    if (zero) {
Packit 40b132
	PL_ClearArenaPool(arena, 0);
Packit 40b132
    }
Packit 40b132
    if (doFreeArenaPool) {
Packit 40b132
	PL_FreeArenaPool(arena);
Packit 40b132
    } else {
Packit 40b132
	PL_FinishArenaPool(arena);
Packit 40b132
    }
Packit 40b132
    PORT_ZFree(arena, len);
Packit 40b132
    if (lock) {
Packit 40b132
	PZ_Unlock(lock);
Packit 40b132
	PZ_DestroyLock(lock);
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
void *
Packit 40b132
PORT_ArenaGrow(PLArenaPool *arena, void *ptr, size_t oldsize, size_t newsize)
Packit 40b132
{
Packit 40b132
    PORTArenaPool *pool = (PORTArenaPool *)arena;
Packit 40b132
    PORT_Assert(newsize >= oldsize);
Packit 40b132
    
Packit 40b132
    if (newsize > MAX_SIZE) {
Packit 40b132
	PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
	return NULL;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    if (ARENAPOOL_MAGIC == pool->magic ) {
Packit 40b132
	PZ_Lock(pool->lock);
Packit 40b132
	/* Do we do a THREADMARK check here? */
Packit 40b132
	PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
Packit 40b132
	PZ_Unlock(pool->lock);
Packit 40b132
    } else {
Packit 40b132
	PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
Packit 40b132
    }
Packit 40b132
    
Packit 40b132
    return(ptr);
Packit 40b132
}
Packit 40b132
Packit 40b132
void *
Packit 40b132
PORT_ArenaMark(PLArenaPool *arena)
Packit 40b132
{
Packit 40b132
    void * result;
Packit 40b132
Packit 40b132
    PORTArenaPool *pool = (PORTArenaPool *)arena;
Packit 40b132
    if (ARENAPOOL_MAGIC == pool->magic ) {
Packit 40b132
	PZ_Lock(pool->lock);
Packit 40b132
#ifdef THREADMARK
Packit 40b132
	{
Packit 40b132
	  threadmark_mark *tm, **pw;
Packit 40b132
	  PRThread * currentThread = PR_GetCurrentThread();
Packit 40b132
Packit 40b132
	    if (! pool->marking_thread ) {
Packit 40b132
		/* First mark */
Packit 40b132
		pool->marking_thread = currentThread;
Packit 40b132
	    } else if (currentThread != pool->marking_thread ) {
Packit 40b132
		PZ_Unlock(pool->lock);
Packit 40b132
		PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
		PORT_Assert(0);
Packit 40b132
		return NULL;
Packit 40b132
	    }
Packit 40b132
Packit 40b132
	    result = PL_ARENA_MARK(arena);
Packit 40b132
	    PL_ARENA_ALLOCATE(tm, arena, sizeof(threadmark_mark));
Packit 40b132
	    if (!tm) {
Packit 40b132
		PZ_Unlock(pool->lock);
Packit 40b132
		PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
		return NULL;
Packit 40b132
	    }
Packit 40b132
Packit 40b132
	    tm->mark = result;
Packit 40b132
	    tm->next = (threadmark_mark *)NULL;
Packit 40b132
Packit 40b132
	    pw = &pool->first_mark;
Packit 40b132
	    while( *pw ) {
Packit 40b132
		 pw = &(*pw)->next;
Packit 40b132
	    }
Packit 40b132
Packit 40b132
	    *pw = tm;
Packit 40b132
	}
Packit 40b132
#else /* THREADMARK */
Packit 40b132
	result = PL_ARENA_MARK(arena);
Packit 40b132
#endif /* THREADMARK */
Packit 40b132
	PZ_Unlock(pool->lock);
Packit 40b132
    } else {
Packit 40b132
	/* a "pure" NSPR arena */
Packit 40b132
	result = PL_ARENA_MARK(arena);
Packit 40b132
    }
Packit 40b132
    return result;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * This function accesses the internals of PLArena, which is why it needs
Packit 40b132
 * to use the NSPR internal macro PL_MAKE_MEM_UNDEFINED before the memset
Packit 40b132
 * calls.
Packit 40b132
 *
Packit 40b132
 * We should move this function to NSPR as PL_ClearArenaAfterMark or add
Packit 40b132
 * a PL_ARENA_CLEAR_AND_RELEASE macro.
Packit 40b132
 *
Packit 40b132
 * TODO: remove the #ifdef PL_MAKE_MEM_UNDEFINED tests when NSPR 4.10+ is
Packit 40b132
 * widely available.
Packit 40b132
 */
Packit 40b132
static void
Packit 40b132
port_ArenaZeroAfterMark(PLArenaPool *arena, void *mark)
Packit 40b132
{
Packit 40b132
    PLArena *a = arena->current;
Packit 40b132
    if (a->base <= (PRUword)mark && (PRUword)mark <= a->avail) {
Packit 40b132
	/* fast path: mark falls in the current arena */
Packit 40b132
#ifdef PL_MAKE_MEM_UNDEFINED
Packit 40b132
	PL_MAKE_MEM_UNDEFINED(mark, a->avail - (PRUword)mark);
Packit 40b132
#endif
Packit 40b132
	memset(mark, 0, a->avail - (PRUword)mark);
Packit 40b132
    } else {
Packit 40b132
	/* slow path: need to find the arena that mark falls in */
Packit 40b132
	for (a = arena->first.next; a; a = a->next) {
Packit 40b132
	    PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
Packit 40b132
	    if (a->base <= (PRUword)mark && (PRUword)mark <= a->avail) {
Packit 40b132
#ifdef PL_MAKE_MEM_UNDEFINED
Packit 40b132
		PL_MAKE_MEM_UNDEFINED(mark, a->avail - (PRUword)mark);
Packit 40b132
#endif
Packit 40b132
		memset(mark, 0, a->avail - (PRUword)mark);
Packit 40b132
		a = a->next;
Packit 40b132
		break;
Packit 40b132
	    }
Packit 40b132
	}
Packit 40b132
	for (; a; a = a->next) {
Packit 40b132
	    PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
Packit 40b132
#ifdef PL_MAKE_MEM_UNDEFINED
Packit 40b132
	    PL_MAKE_MEM_UNDEFINED((void *)a->base, a->avail - a->base);
Packit 40b132
#endif
Packit 40b132
	    memset((void *)a->base, 0, a->avail - a->base);
Packit 40b132
	}
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
static void
Packit 40b132
port_ArenaRelease(PLArenaPool *arena, void *mark, PRBool zero)
Packit 40b132
{
Packit 40b132
    PORTArenaPool *pool = (PORTArenaPool *)arena;
Packit 40b132
    if (ARENAPOOL_MAGIC == pool->magic ) {
Packit 40b132
	PZ_Lock(pool->lock);
Packit 40b132
#ifdef THREADMARK
Packit 40b132
	{
Packit 40b132
	    threadmark_mark **pw, *tm;
Packit 40b132
Packit 40b132
	    if (PR_GetCurrentThread() != pool->marking_thread ) {
Packit 40b132
		PZ_Unlock(pool->lock);
Packit 40b132
		PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
		PORT_Assert(0);
Packit 40b132
		return /* no error indication available */ ;
Packit 40b132
	    }
Packit 40b132
Packit 40b132
	    pw = &pool->first_mark;
Packit 40b132
	    while( *pw && (mark != (*pw)->mark) ) {
Packit 40b132
		pw = &(*pw)->next;
Packit 40b132
	    }
Packit 40b132
Packit 40b132
	    if (! *pw ) {
Packit 40b132
		/* bad mark */
Packit 40b132
		PZ_Unlock(pool->lock);
Packit 40b132
		PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
		PORT_Assert(0);
Packit 40b132
		return /* no error indication available */ ;
Packit 40b132
	    }
Packit 40b132
Packit 40b132
	    tm = *pw;
Packit 40b132
	    *pw = (threadmark_mark *)NULL;
Packit 40b132
Packit 40b132
	    if (zero) {
Packit 40b132
		port_ArenaZeroAfterMark(arena, mark);
Packit 40b132
	    }
Packit 40b132
	    PL_ARENA_RELEASE(arena, mark);
Packit 40b132
Packit 40b132
	    if (! pool->first_mark ) {
Packit 40b132
		pool->marking_thread = (PRThread *)NULL;
Packit 40b132
	    }
Packit 40b132
	}
Packit 40b132
#else /* THREADMARK */
Packit 40b132
	if (zero) {
Packit 40b132
	    port_ArenaZeroAfterMark(arena, mark);
Packit 40b132
	}
Packit 40b132
	PL_ARENA_RELEASE(arena, mark);
Packit 40b132
#endif /* THREADMARK */
Packit 40b132
	PZ_Unlock(pool->lock);
Packit 40b132
    } else {
Packit 40b132
	if (zero) {
Packit 40b132
	    port_ArenaZeroAfterMark(arena, mark);
Packit 40b132
	}
Packit 40b132
	PL_ARENA_RELEASE(arena, mark);
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
PORT_ArenaRelease(PLArenaPool *arena, void *mark)
Packit 40b132
{
Packit 40b132
    port_ArenaRelease(arena, mark, PR_FALSE);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Zeroize the arena memory before releasing it.
Packit 40b132
 */
Packit 40b132
void
Packit 40b132
PORT_ArenaZRelease(PLArenaPool *arena, void *mark)
Packit 40b132
{
Packit 40b132
    port_ArenaRelease(arena, mark, PR_TRUE);
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
PORT_ArenaUnmark(PLArenaPool *arena, void *mark)
Packit 40b132
{
Packit 40b132
#ifdef THREADMARK
Packit 40b132
    PORTArenaPool *pool = (PORTArenaPool *)arena;
Packit 40b132
    if (ARENAPOOL_MAGIC == pool->magic ) {
Packit 40b132
	threadmark_mark **pw, *tm;
Packit 40b132
Packit 40b132
	PZ_Lock(pool->lock);
Packit 40b132
Packit 40b132
	if (PR_GetCurrentThread() != pool->marking_thread ) {
Packit 40b132
	    PZ_Unlock(pool->lock);
Packit 40b132
	    PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
	    PORT_Assert(0);
Packit 40b132
	    return /* no error indication available */ ;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	pw = &pool->first_mark;
Packit 40b132
	while( ((threadmark_mark *)NULL != *pw) && (mark != (*pw)->mark) ) {
Packit 40b132
	    pw = &(*pw)->next;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	if ((threadmark_mark *)NULL == *pw ) {
Packit 40b132
	    /* bad mark */
Packit 40b132
	    PZ_Unlock(pool->lock);
Packit 40b132
	    PORT_SetError(SEC_ERROR_NO_MEMORY);
Packit 40b132
	    PORT_Assert(0);
Packit 40b132
	    return /* no error indication available */ ;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	tm = *pw;
Packit 40b132
	*pw = (threadmark_mark *)NULL;
Packit 40b132
Packit 40b132
	if (! pool->first_mark ) {
Packit 40b132
	    pool->marking_thread = (PRThread *)NULL;
Packit 40b132
	}
Packit 40b132
Packit 40b132
	PZ_Unlock(pool->lock);
Packit 40b132
    }
Packit 40b132
#endif /* THREADMARK */
Packit 40b132
}
Packit 40b132
Packit 40b132
char *
Packit 40b132
PORT_ArenaStrdup(PLArenaPool *arena, const char *str) {
Packit 40b132
    int len = PORT_Strlen(str)+1;
Packit 40b132
    char *newstr;
Packit 40b132
Packit 40b132
    newstr = (char*)PORT_ArenaAlloc(arena,len);
Packit 40b132
    if (newstr) {
Packit 40b132
        PORT_Memcpy(newstr,str,len);
Packit 40b132
    }
Packit 40b132
    return newstr;
Packit 40b132
}
Packit 40b132
Packit 40b132
/********************** end of arena functions ***********************/
Packit 40b132
Packit 40b132
/****************** unicode conversion functions ***********************/
Packit 40b132
/*
Packit 40b132
 * NOTE: These conversion functions all assume that the multibyte
Packit 40b132
 * characters are going to be in NETWORK BYTE ORDER, not host byte
Packit 40b132
 * order.  This is because the only time we deal with UCS-2 and UCS-4
Packit 40b132
 * are when the data was received from or is going to be sent out
Packit 40b132
 * over the wire (in, e.g. certificates).
Packit 40b132
 */
Packit 40b132
Packit 40b132
void
Packit 40b132
PORT_SetUCS4_UTF8ConversionFunction(PORTCharConversionFunc convFunc)
Packit 40b132
{ 
Packit 40b132
    ucs4Utf8ConvertFunc = convFunc;
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
PORT_SetUCS2_ASCIIConversionFunction(PORTCharConversionWSwapFunc convFunc)
Packit 40b132
{ 
Packit 40b132
    ucs2AsciiConvertFunc = convFunc;
Packit 40b132
}
Packit 40b132
Packit 40b132
void
Packit 40b132
PORT_SetUCS2_UTF8ConversionFunction(PORTCharConversionFunc convFunc)
Packit 40b132
{ 
Packit 40b132
    ucs2Utf8ConvertFunc = convFunc;
Packit 40b132
}
Packit 40b132
Packit 40b132
PRBool 
Packit 40b132
PORT_UCS4_UTF8Conversion(PRBool toUnicode, unsigned char *inBuf,
Packit 40b132
			 unsigned int inBufLen, unsigned char *outBuf,
Packit 40b132
			 unsigned int maxOutBufLen, unsigned int *outBufLen)
Packit 40b132
{
Packit 40b132
    if(!ucs4Utf8ConvertFunc) {
Packit 40b132
      return sec_port_ucs4_utf8_conversion_function(toUnicode,
Packit 40b132
        inBuf, inBufLen, outBuf, maxOutBufLen, outBufLen);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    return (*ucs4Utf8ConvertFunc)(toUnicode, inBuf, inBufLen, outBuf, 
Packit 40b132
				  maxOutBufLen, outBufLen);
Packit 40b132
}
Packit 40b132
Packit 40b132
PRBool 
Packit 40b132
PORT_UCS2_UTF8Conversion(PRBool toUnicode, unsigned char *inBuf,
Packit 40b132
			 unsigned int inBufLen, unsigned char *outBuf,
Packit 40b132
			 unsigned int maxOutBufLen, unsigned int *outBufLen)
Packit 40b132
{
Packit 40b132
    if(!ucs2Utf8ConvertFunc) {
Packit 40b132
      return sec_port_ucs2_utf8_conversion_function(toUnicode,
Packit 40b132
        inBuf, inBufLen, outBuf, maxOutBufLen, outBufLen);
Packit 40b132
    }
Packit 40b132
Packit 40b132
    return (*ucs2Utf8ConvertFunc)(toUnicode, inBuf, inBufLen, outBuf, 
Packit 40b132
				  maxOutBufLen, outBufLen);
Packit 40b132
}
Packit 40b132
Packit 40b132
PRBool 
Packit 40b132
PORT_ISO88591_UTF8Conversion(const unsigned char *inBuf,
Packit 40b132
			 unsigned int inBufLen, unsigned char *outBuf,
Packit 40b132
			 unsigned int maxOutBufLen, unsigned int *outBufLen)
Packit 40b132
{
Packit 40b132
    return sec_port_iso88591_utf8_conversion_function(inBuf, inBufLen,
Packit 40b132
      outBuf, maxOutBufLen, outBufLen);
Packit 40b132
}
Packit 40b132
Packit 40b132
PRBool 
Packit 40b132
PORT_UCS2_ASCIIConversion(PRBool toUnicode, unsigned char *inBuf,
Packit 40b132
			  unsigned int inBufLen, unsigned char *outBuf,
Packit 40b132
			  unsigned int maxOutBufLen, unsigned int *outBufLen,
Packit 40b132
			  PRBool swapBytes)
Packit 40b132
{
Packit 40b132
    if(!ucs2AsciiConvertFunc) {
Packit 40b132
	return PR_FALSE;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    return (*ucs2AsciiConvertFunc)(toUnicode, inBuf, inBufLen, outBuf, 
Packit 40b132
				  maxOutBufLen, outBufLen, swapBytes);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/* Portable putenv.  Creates/replaces an environment variable of the form
Packit 40b132
 *  envVarName=envValue
Packit 40b132
 */
Packit 40b132
int
Packit 40b132
NSS_PutEnv(const char * envVarName, const char * envValue)
Packit 40b132
{
Packit 40b132
    SECStatus result = SECSuccess;
Packit 40b132
    char *    encoded;
Packit 40b132
    int       putEnvFailed;
Packit 40b132
#ifdef _WIN32
Packit 40b132
    PRBool      setOK;
Packit 40b132
Packit 40b132
    setOK = SetEnvironmentVariable(envVarName, envValue);
Packit 40b132
    if (!setOK) {
Packit 40b132
        SET_ERROR_CODE
Packit 40b132
        return SECFailure;
Packit 40b132
    }
Packit 40b132
#endif
Packit 40b132
Packit 40b132
    encoded = (char *)PORT_ZAlloc(strlen(envVarName) + 2 + strlen(envValue));
Packit 40b132
    strcpy(encoded, envVarName);
Packit 40b132
    strcat(encoded, "=");
Packit 40b132
    strcat(encoded, envValue);
Packit 40b132
Packit 40b132
    putEnvFailed = putenv(encoded); /* adopt. */
Packit 40b132
    if (putEnvFailed) {
Packit 40b132
        SET_ERROR_CODE
Packit 40b132
        result = SECFailure;
Packit 40b132
        PORT_Free(encoded);
Packit 40b132
    }
Packit 40b132
    return result;
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * Perform a constant-time compare of two memory regions. The return value is
Packit 40b132
 * 0 if the memory regions are equal and non-zero otherwise.
Packit 40b132
 */
Packit 40b132
int
Packit 40b132
NSS_SecureMemcmp(const void *ia, const void *ib, size_t n)
Packit 40b132
{
Packit 40b132
    const unsigned char *a = (const unsigned char*) ia;
Packit 40b132
    const unsigned char *b = (const unsigned char*) ib;
Packit 40b132
    size_t i;
Packit 40b132
    unsigned char r = 0;
Packit 40b132
Packit 40b132
    for (i = 0; i < n; ++i) {
Packit 40b132
        r |= *a++ ^ *b++;
Packit 40b132
    }
Packit 40b132
Packit 40b132
    return r;
Packit 40b132
}