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