Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions | Variables
secport.c File Reference
#include "seccomon.h"
#include "prmem.h"
#include "prerror.h"
#include "plarena.h"
#include "secerr.h"
#include "prmon.h"
#include "nsslocks.h"
#include "secport.h"
#include "prvrsion.h"
#include "prenv.h"
#include "wtypes.h"

Go to the source code of this file.

Classes

struct  PORTArenaPool_str

Defines

#define SET_ERROR_CODE   /* place holder for code to set PR error code. */
#define ARENAPOOL_MAGIC   0xB8AC9BDF
#define MAX_SIZE   0x7fffffffUL

Typedefs

typedef struct PORTArenaPool_str PORTArenaPool

Functions

voidPORT_Alloc (size_t bytes)
 This file contains trivial implementations of the NSS PORT_* functions that md5.c uses.
voidPORT_Realloc (void *oldptr, size_t bytes)
voidPORT_ZAlloc (size_t bytes)
void PORT_Free (void *ptr)
void PORT_ZFree (void *ptr, size_t len)
char * PORT_Strdup (const char *str)
void PORT_SetError (int value)
int PORT_GetError (void)
PLArenaPoolPORT_NewArena (unsigned long chunksize)
voidPORT_ArenaAlloc (PLArenaPool *arena, size_t size)
voidPORT_ArenaZAlloc (PLArenaPool *arena, size_t size)
void PORT_FreeArena (PLArenaPool *arena, PRBool zero)
voidPORT_ArenaGrow (PLArenaPool *arena, void *ptr, size_t oldsize, size_t newsize)
voidPORT_ArenaMark (PLArenaPool *arena)
static void port_ArenaZeroAfterMark (PLArenaPool *arena, void *mark)
static void port_ArenaRelease (PLArenaPool *arena, void *mark, PRBool zero)
void PORT_ArenaRelease (PLArenaPool *arena, void *mark)
void PORT_ArenaZRelease (PLArenaPool *arena, void *mark)
void PORT_ArenaUnmark (PLArenaPool *arena, void *mark)
char * PORT_ArenaStrdup (PLArenaPool *arena, const char *str)
void PORT_SetUCS4_UTF8ConversionFunction (PORTCharConversionFunc convFunc)
void PORT_SetUCS2_ASCIIConversionFunction (PORTCharConversionWSwapFunc convFunc)
void PORT_SetUCS2_UTF8ConversionFunction (PORTCharConversionFunc convFunc)
PRBool PORT_UCS4_UTF8Conversion (PRBool toUnicode, unsigned char *inBuf, unsigned int inBufLen, unsigned char *outBuf, unsigned int maxOutBufLen, unsigned int *outBufLen)
PRBool PORT_UCS2_UTF8Conversion (PRBool toUnicode, unsigned char *inBuf, unsigned int inBufLen, unsigned char *outBuf, unsigned int maxOutBufLen, unsigned int *outBufLen)
PRBool PORT_ISO88591_UTF8Conversion (const unsigned char *inBuf, unsigned int inBufLen, unsigned char *outBuf, unsigned int maxOutBufLen, unsigned int *outBufLen)
PRBool PORT_UCS2_ASCIIConversion (PRBool toUnicode, unsigned char *inBuf, unsigned int inBufLen, unsigned char *outBuf, unsigned int maxOutBufLen, unsigned int *outBufLen, PRBool swapBytes)
int NSS_PutEnv (const char *envVarName, const char *envValue)

Variables

unsigned long port_allocFailures
PORTCharConversionFunc ucs4Utf8ConvertFunc
PORTCharConversionFunc ucs2Utf8ConvertFunc
PORTCharConversionWSwapFunc ucs2AsciiConvertFunc

Class Documentation

struct PORTArenaPool_str

Definition at line 85 of file secport.c.

Collaboration diagram for PORTArenaPool_str:
Class Members
PLArenaPool arena
PRLock * lock
PRUint32 magic

Define Documentation

#define ARENAPOOL_MAGIC   0xB8AC9BDF

Definition at line 83 of file secport.c.

#define MAX_SIZE   0x7fffffffUL

Definition at line 213 of file secport.c.

#define SET_ERROR_CODE   /* place holder for code to set PR error code. */

Definition at line 72 of file secport.c.


Typedef Documentation


Function Documentation

int NSS_PutEnv ( const char *  envVarName,
const char *  envValue 
)

Definition at line 632 of file secport.c.

{
#if  defined(XP_MAC) || defined(_WIN32_WCE)
    return SECFailure;
#else
    SECStatus result = SECSuccess;
    char *    encoded;
    int       putEnvFailed;
#ifdef _WIN32
    PRBool      setOK;

    setOK = SetEnvironmentVariable(envVarName, envValue);
    if (!setOK) {
        SET_ERROR_CODE
        return SECFailure;
    }
#endif

    encoded = (char *)PORT_ZAlloc(strlen(envVarName) + 2 + strlen(envValue));
    strcpy(encoded, envVarName);
    strcat(encoded, "=");
    strcat(encoded, envValue);

    putEnvFailed = putenv(encoded); /* adopt. */
    if (putEnvFailed) {
        SET_ERROR_CODE
        result = SECFailure;
        PORT_Free(encoded);
    }
    return result;
#endif
}
void* PORT_Alloc ( size_t  bytes)

This file contains trivial implementations of the NSS PORT_* functions that md5.c uses.

Definition at line 108 of file secport.c.

{
    void *rv;

    /* Always allocate a non-zero amount of bytes */
    rv = (void *)PR_Malloc(bytes ? bytes : 1);
    if (!rv) {
       ++port_allocFailures;
       PORT_SetError(SEC_ERROR_NO_MEMORY);
    }
    return rv;
}

Here is the call graph for this function:

void* PORT_ArenaAlloc ( PLArenaPool arena,
size_t  size 
)

Definition at line 216 of file secport.c.

{
    void *p = NULL;

    PORTArenaPool *pool = (PORTArenaPool *)arena;

    if (size <= 0) {
       size = 1;
    }

    if (size > MAX_SIZE) {
       /* you lose. */
    } else 
    /* Is it one of ours?  Assume so and check the magic */
    if (ARENAPOOL_MAGIC == pool->magic ) {
       PZ_Lock(pool->lock);
#ifdef THREADMARK
        /* Most likely one of ours.  Is there a thread id? */
       if (pool->marking_thread  &&
           pool->marking_thread != PR_GetCurrentThread() ) {
           /* Another thread holds a mark in this arena */
           PZ_Unlock(pool->lock);
           PORT_SetError(SEC_ERROR_NO_MEMORY);
           PORT_Assert(0);
           return NULL;
       } /* tid != null */
#endif /* THREADMARK */
       PL_ARENA_ALLOCATE(p, arena, size);
       PZ_Unlock(pool->lock);
    } else {
       PL_ARENA_ALLOCATE(p, arena, size);
    }

    if (!p) {
       ++port_allocFailures;
       PORT_SetError(SEC_ERROR_NO_MEMORY);
    }

    return(p);
}
void* PORT_ArenaGrow ( PLArenaPool arena,
void ptr,
size_t  oldsize,
size_t  newsize 
)

Definition at line 328 of file secport.c.

{
    PORTArenaPool *pool = (PORTArenaPool *)arena;
    PORT_Assert(newsize >= oldsize);
    
    if (ARENAPOOL_MAGIC == pool->magic ) {
       PZ_Lock(pool->lock);
       /* Do we do a THREADMARK check here? */
       PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
       PZ_Unlock(pool->lock);
    } else {
       PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) );
    }
    
    return(ptr);
}

Here is the caller graph for this function:

Definition at line 346 of file secport.c.

{
    void * result;

    PORTArenaPool *pool = (PORTArenaPool *)arena;
    if (ARENAPOOL_MAGIC == pool->magic ) {
       PZ_Lock(pool->lock);
#ifdef THREADMARK
       {
         threadmark_mark *tm, **pw;
         PRThread * currentThread = PR_GetCurrentThread();

           if (! pool->marking_thread ) {
              /* First mark */
              pool->marking_thread = currentThread;
           } else if (currentThread != pool->marking_thread ) {
              PZ_Unlock(pool->lock);
              PORT_SetError(SEC_ERROR_NO_MEMORY);
              PORT_Assert(0);
              return NULL;
           }

           result = PL_ARENA_MARK(arena);
           PL_ARENA_ALLOCATE(tm, arena, sizeof(threadmark_mark));
           if (!tm) {
              PZ_Unlock(pool->lock);
              PORT_SetError(SEC_ERROR_NO_MEMORY);
              return NULL;
           }

           tm->mark = result;
           tm->next = (threadmark_mark *)NULL;

           pw = &pool->first_mark;
           while( *pw ) {
               pw = &(*pw)->next;
           }

           *pw = tm;
       }
#else /* THREADMARK */
       result = PL_ARENA_MARK(arena);
#endif /* THREADMARK */
       PZ_Unlock(pool->lock);
    } else {
       /* a "pure" NSPR arena */
       result = PL_ARENA_MARK(arena);
    }
    return result;
}
static void port_ArenaRelease ( PLArenaPool arena,
void mark,
PRBool  zero 
) [static]

Definition at line 422 of file secport.c.

{
    PORTArenaPool *pool = (PORTArenaPool *)arena;
    if (ARENAPOOL_MAGIC == pool->magic ) {
       PZ_Lock(pool->lock);
#ifdef THREADMARK
       {
           threadmark_mark **pw, *tm;

           if (PR_GetCurrentThread() != pool->marking_thread ) {
              PZ_Unlock(pool->lock);
              PORT_SetError(SEC_ERROR_NO_MEMORY);
              PORT_Assert(0);
              return /* no error indication available */ ;
           }

           pw = &pool->first_mark;
           while( *pw && (mark != (*pw)->mark) ) {
              pw = &(*pw)->next;
           }

           if (! *pw ) {
              /* bad mark */
              PZ_Unlock(pool->lock);
              PORT_SetError(SEC_ERROR_NO_MEMORY);
              PORT_Assert(0);
              return /* no error indication available */ ;
           }

           tm = *pw;
           *pw = (threadmark_mark *)NULL;

           if (zero) {
              port_ArenaZeroAfterMark(arena, mark);
           }
           PL_ARENA_RELEASE(arena, mark);

           if (! pool->first_mark ) {
              pool->marking_thread = (PRThread *)NULL;
           }
       }
#else /* THREADMARK */
       if (zero) {
           port_ArenaZeroAfterMark(arena, mark);
       }
       PL_ARENA_RELEASE(arena, mark);
#endif /* THREADMARK */
       PZ_Unlock(pool->lock);
    } else {
       if (zero) {
           port_ArenaZeroAfterMark(arena, mark);
       }
       PL_ARENA_RELEASE(arena, mark);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PORT_ArenaRelease ( PLArenaPool arena,
void mark 
)

Definition at line 479 of file secport.c.

char* PORT_ArenaStrdup ( PLArenaPool arena,
const char *  str 
)

Definition at line 536 of file secport.c.

                                                      {
    int len = PORT_Strlen(str)+1;
    char *newstr;

    newstr = (char*)PORT_ArenaAlloc(arena,len);
    if (newstr) {
        PORT_Memcpy(newstr,str,len);
    }
    return newstr;
}

Here is the caller graph for this function:

void PORT_ArenaUnmark ( PLArenaPool arena,
void mark 
)

Definition at line 494 of file secport.c.

{
#ifdef THREADMARK
    PORTArenaPool *pool = (PORTArenaPool *)arena;
    if (ARENAPOOL_MAGIC == pool->magic ) {
       threadmark_mark **pw, *tm;

       PZ_Lock(pool->lock);

       if (PR_GetCurrentThread() != pool->marking_thread ) {
           PZ_Unlock(pool->lock);
           PORT_SetError(SEC_ERROR_NO_MEMORY);
           PORT_Assert(0);
           return /* no error indication available */ ;
       }

       pw = &pool->first_mark;
       while( ((threadmark_mark *)NULL != *pw) && (mark != (*pw)->mark) ) {
           pw = &(*pw)->next;
       }

       if ((threadmark_mark *)NULL == *pw ) {
           /* bad mark */
           PZ_Unlock(pool->lock);
           PORT_SetError(SEC_ERROR_NO_MEMORY);
           PORT_Assert(0);
           return /* no error indication available */ ;
       }

       tm = *pw;
       *pw = (threadmark_mark *)NULL;

       if (! pool->first_mark ) {
           pool->marking_thread = (PRThread *)NULL;
       }

       PZ_Unlock(pool->lock);
    }
#endif /* THREADMARK */
}
void* PORT_ArenaZAlloc ( PLArenaPool arena,
size_t  size 
)

Definition at line 258 of file secport.c.

{
    void *p;

    if (size <= 0)
        size = 1;

    p = PORT_ArenaAlloc(arena, size);

    if (p) {
       PORT_Memset(p, 0, size);
    }

    return(p);
}
static void port_ArenaZeroAfterMark ( PLArenaPool arena,
void mark 
) [static]

Definition at line 398 of file secport.c.

{
    PLArena *a = arena->current;
    if (a->base <= (PRUword)mark && (PRUword)mark <= a->avail) {
       /* fast path: mark falls in the current arena */
       memset(mark, 0, a->avail - (PRUword)mark);
    } else {
       /* slow path: need to find the arena that mark falls in */
       for (a = arena->first.next; a; a = a->next) {
           PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
           if (a->base <= (PRUword)mark && (PRUword)mark <= a->avail) {
              memset(mark, 0, a->avail - (PRUword)mark);
              a = a->next;
              break;
           }
       }
       for (; a; a = a->next) {
           PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
           memset((void *)a->base, 0, a->avail - a->base);
       }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PORT_ArenaZRelease ( PLArenaPool arena,
void mark 
)

Definition at line 488 of file secport.c.

Here is the caller graph for this function:

void PORT_Free ( void ptr)

Definition at line 149 of file secport.c.

{
    if (ptr) {
       PR_Free(ptr);
    }
}
void PORT_FreeArena ( PLArenaPool arena,
PRBool  zero 
)

Definition at line 278 of file secport.c.

{
    PORTArenaPool *pool = (PORTArenaPool *)arena;
    PRLock *       lock = (PRLock *)0;
    size_t         len  = sizeof *arena;
    extern const PRVersionDescription * libVersionPoint(void);
    static const PRVersionDescription * pvd;
    static PRBool  doFreeArenaPool = PR_FALSE;

    if (ARENAPOOL_MAGIC == pool->magic ) {
       len  = sizeof *pool;
       lock = pool->lock;
       PZ_Lock(lock);
    }
    if (!pvd) {
       /* Each of NSPR's DLLs has a function libVersionPoint().
       ** We could do a lot of extra work to be sure we're calling the
       ** one in the DLL that holds PR_FreeArenaPool, but instead we
       ** rely on the fact that ALL NSPR DLLs in the same directory
       ** must be from the same release, and we call which ever one we get. 
       */
       /* no need for thread protection here */
       pvd = libVersionPoint();
       if ((pvd->vMajor > 4) || 
           (pvd->vMajor == 4 && pvd->vMinor > 1) ||
           (pvd->vMajor == 4 && pvd->vMinor == 1 && pvd->vPatch >= 1)) {
           const char *ev = PR_GetEnv("NSS_DISABLE_ARENA_FREE_LIST");
           if (!ev) doFreeArenaPool = PR_TRUE;
       }
    }
    if (zero) {
       PLArena *a;
       for (a = arena->first.next; a; a = a->next) {
           PR_ASSERT(a->base <= a->avail && a->avail <= a->limit);
           memset((void *)a->base, 0, a->avail - a->base);
       }
    }
    if (doFreeArenaPool) {
       PL_FreeArenaPool(arena);
    } else {
       PL_FinishArenaPool(arena);
    }
    PORT_ZFree(arena, len);
    if (lock) {
       PZ_Unlock(lock);
       PZ_DestroyLock(lock);
    }
}

Definition at line 186 of file secport.c.

{
    return(PR_GetError());
}
PRBool PORT_ISO88591_UTF8Conversion ( const unsigned char *  inBuf,
unsigned int  inBufLen,
unsigned char *  outBuf,
unsigned int  maxOutBufLen,
unsigned int outBufLen 
)

Definition at line 605 of file secport.c.

Here is the caller graph for this function:

PLArenaPool* PORT_NewArena ( unsigned long  chunksize)

Definition at line 194 of file secport.c.

{
    PORTArenaPool *pool;
    
    pool = PORT_ZNew(PORTArenaPool);
    if (!pool) {
       return NULL;
    }
    pool->magic = ARENAPOOL_MAGIC;
    pool->lock = PZ_NewLock(nssILockArena);
    if (!pool->lock) {
       ++port_allocFailures;
       PORT_Free(pool);
       return NULL;
    }
    PL_InitArenaPool(&pool->arena, "security", chunksize, sizeof(double));
    return(&pool->arena);
}
void* PORT_Realloc ( void oldptr,
size_t  bytes 
)

Definition at line 122 of file secport.c.

{
    void *rv;

    rv = (void *)PR_Realloc(oldptr, bytes);
    if (!rv) {
       ++port_allocFailures;
       PORT_SetError(SEC_ERROR_NO_MEMORY);
    }
    return rv;
}

Here is the caller graph for this function:

void PORT_SetError ( int  value)

Definition at line 179 of file secport.c.

{      
    PR_SetError(value, 0);
    return;
}
void PORT_SetUCS2_ASCIIConversionFunction ( PORTCharConversionWSwapFunc  convFunc)

Definition at line 565 of file secport.c.

{ 
    ucs2AsciiConvertFunc = convFunc;
}

Here is the caller graph for this function:

void PORT_SetUCS2_UTF8ConversionFunction ( PORTCharConversionFunc  convFunc)

Definition at line 571 of file secport.c.

{ 
    ucs2Utf8ConvertFunc = convFunc;
}
void PORT_SetUCS4_UTF8ConversionFunction ( PORTCharConversionFunc  convFunc)

Definition at line 559 of file secport.c.

{ 
    ucs4Utf8ConvertFunc = convFunc;
}
char* PORT_Strdup ( const char *  str)

Definition at line 166 of file secport.c.

{
    size_t len = PORT_Strlen(str)+1;
    char *newstr;

    newstr = (char *)PORT_Alloc(len);
    if (newstr) {
        PORT_Memcpy(newstr, str, len);
    }
    return newstr;
}
PRBool PORT_UCS2_ASCIIConversion ( PRBool  toUnicode,
unsigned char *  inBuf,
unsigned int  inBufLen,
unsigned char *  outBuf,
unsigned int  maxOutBufLen,
unsigned int outBufLen,
PRBool  swapBytes 
)

Definition at line 614 of file secport.c.

Here is the caller graph for this function:

PRBool PORT_UCS2_UTF8Conversion ( PRBool  toUnicode,
unsigned char *  inBuf,
unsigned int  inBufLen,
unsigned char *  outBuf,
unsigned int  maxOutBufLen,
unsigned int outBufLen 
)

Definition at line 591 of file secport.c.

Here is the caller graph for this function:

PRBool PORT_UCS4_UTF8Conversion ( PRBool  toUnicode,
unsigned char *  inBuf,
unsigned int  inBufLen,
unsigned char *  outBuf,
unsigned int  maxOutBufLen,
unsigned int outBufLen 
)

Definition at line 577 of file secport.c.

Here is the caller graph for this function:

void* PORT_ZAlloc ( size_t  bytes)

Definition at line 135 of file secport.c.

{
    void *rv;

    /* Always allocate a non-zero amount of bytes */
    rv = (void *)PR_Calloc(1, bytes ? bytes : 1);
    if (!rv) {
       ++port_allocFailures;
       PORT_SetError(SEC_ERROR_NO_MEMORY);
    }
    return rv;
}
void PORT_ZFree ( void ptr,
size_t  len 
)

Definition at line 157 of file secport.c.

{
    if (ptr) {
       memset(ptr, 0, len);
       PR_Free(ptr);
    }
}

Variable Documentation

Definition at line 97 of file secport.c.

PORTCharConversionWSwapFunc ucs2AsciiConvertFunc

Definition at line 105 of file secport.c.

PORTCharConversionFunc ucs2Utf8ConvertFunc

Definition at line 104 of file secport.c.

PORTCharConversionFunc ucs4Utf8ConvertFunc

Definition at line 103 of file secport.c.