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