Back to index

lightning-sunbird  0.9+nobinonly
Defines | Typedefs | Functions
secport.h File Reference
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include "prtypes.h"
#include "prlog.h"
#include "plarena.h"
#include "plstr.h"
#include "seccomon.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define PORT_Assert   PR_ASSERT
#define PORT_ZNew(type)   (type*)PORT_ZAlloc(sizeof(type))
#define PORT_New(type)   (type*)PORT_Alloc(sizeof(type))
#define PORT_ArenaNew(poolp, type)   (type*) PORT_ArenaAlloc(poolp, sizeof(type))
#define PORT_ArenaZNew(poolp, type)   (type*) PORT_ArenaZAlloc(poolp, sizeof(type))
#define PORT_NewArray(type, num)   (type*) PORT_Alloc (sizeof(type)*(num))
#define PORT_ZNewArray(type, num)   (type*) PORT_ZAlloc (sizeof(type)*(num))
#define PORT_ArenaNewArray(poolp, type, num)   (type*) PORT_ArenaAlloc (poolp, sizeof(type)*(num))
#define PORT_ArenaZNewArray(poolp, type, num)   (type*) PORT_ArenaZAlloc (poolp, sizeof(type)*(num))
#define PORT_Atoi   atoi
#define PORT_Memcmp   memcmp
#define PORT_Memcpy   memcpy
#define PORT_Memmove   memmove
#define PORT_Memset   memset
#define PORT_Strcasecmp   PL_strcasecmp
#define PORT_Strcat   strcat
#define PORT_Strchr   strchr
#define PORT_Strrchr   strrchr
#define PORT_Strcmp   strcmp
#define PORT_Strcpy   strcpy
#define PORT_Strlen(s)   strlen(s)
#define PORT_Strncasecmp   PL_strncasecmp
#define PORT_Strncat   strncat
#define PORT_Strncmp   strncmp
#define PORT_Strncpy   strncpy
#define PORT_Strpbrk   strpbrk
#define PORT_Strstr   strstr
#define PORT_Strtok   strtok
#define PORT_Tolower   tolower

Typedefs

typedef unsigned char * inBuf
typedef unsigned char unsigned int inBufLen
typedef unsigned char unsigned
int unsigned char * 
outBuf
typedef unsigned char unsigned
int unsigned char unsigned int 
maxOutBufLen
typedef unsigned char unsigned
int unsigned char unsigned int
unsigned int
outBufLen
typedef unsigned char unsigned
int unsigned char unsigned int
unsigned int PRBool 
swapBytes

Functions

SEC_BEGIN_PROTOS voidPORT_Alloc (size_t len)
 This file contains trivial implementations of the NSS PORT_* functions that md5.c uses.
voidPORT_Realloc (void *old, size_t len)
voidPORT_AllocBlock (size_t len)
voidPORT_ReallocBlock (void *old, size_t len)
void PORT_FreeBlock (void *ptr)
voidPORT_ZAlloc (size_t len)
void PORT_Free (void *ptr)
void PORT_ZFree (void *ptr, size_t len)
char * PORT_Strdup (const char *s)
time_t PORT_Time (void)
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)
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)
typedef PRBool (PR_CALLBACK *PORTCharConversionWSwapFunc)(PRBool toUnicode
void PORT_SetUCS4_UTF8ConversionFunction (PORTCharConversionFunc convFunc)
void PORT_SetUCS2_ASCIIConversionFunction (PORTCharConversionWSwapFunc 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_ASCIIConversion (PRBool toUnicode, unsigned char *inBuf, unsigned int inBufLen, unsigned char *outBuf, unsigned int maxOutBufLen, unsigned int *outBufLen, PRBool swapBytes)
void PORT_SetUCS2_UTF8ConversionFunction (PORTCharConversionFunc convFunc)
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)
 sec_port_ucs4_utf8_conversion_function (PRBool toUnicode, unsigned char *inBuf, unsigned int inBufLen, unsigned char *outBuf, unsigned int maxOutBufLen, unsigned int *outBufLen)
 sec_port_ucs2_utf8_conversion_function (PRBool toUnicode, unsigned char *inBuf, unsigned int inBufLen, unsigned char *outBuf, unsigned int maxOutBufLen, unsigned int *outBufLen)
PRBool sec_port_iso88591_utf8_conversion_function (const unsigned char *inBuf, unsigned int inBufLen, unsigned char *outBuf, unsigned int maxOutBufLen, unsigned int *outBufLen)
int NSS_PutEnv (const char *envVarName, const char *envValue)

Define Documentation

#define PORT_ArenaNew (   poolp,
  type 
)    (type*) PORT_ArenaAlloc(poolp, sizeof(type))

Definition at line 151 of file secport.h.

#define PORT_ArenaNewArray (   poolp,
  type,
  num 
)    (type*) PORT_ArenaAlloc (poolp, sizeof(type)*(num))

Definition at line 159 of file secport.h.

#define PORT_ArenaZNew (   poolp,
  type 
)    (type*) PORT_ArenaZAlloc(poolp, sizeof(type))

Definition at line 153 of file secport.h.

#define PORT_ArenaZNewArray (   poolp,
  type,
  num 
)    (type*) PORT_ArenaZAlloc (poolp, sizeof(type)*(num))

Definition at line 161 of file secport.h.

Definition at line 148 of file secport.h.

#define PORT_Atoi   atoi

Definition at line 166 of file secport.h.

Definition at line 168 of file secport.h.

Definition at line 169 of file secport.h.

Definition at line 171 of file secport.h.

Definition at line 175 of file secport.h.

#define PORT_New (   type)    (type*)PORT_Alloc(sizeof(type))

Definition at line 150 of file secport.h.

#define PORT_NewArray (   type,
  num 
)    (type*) PORT_Alloc (sizeof(type)*(num))

Definition at line 155 of file secport.h.

Definition at line 177 of file secport.h.

#define PORT_Strcat   strcat

Definition at line 178 of file secport.h.

Definition at line 179 of file secport.h.

#define PORT_Strcmp   strcmp

Definition at line 181 of file secport.h.

#define PORT_Strcpy   strcpy

Definition at line 182 of file secport.h.

#define PORT_Strlen (   s)    strlen(s)

Definition at line 183 of file secport.h.

Definition at line 184 of file secport.h.

#define PORT_Strncat   strncat

Definition at line 185 of file secport.h.

#define PORT_Strncmp   strncmp

Definition at line 186 of file secport.h.

#define PORT_Strncpy   strncpy

Definition at line 187 of file secport.h.

#define PORT_Strpbrk   strpbrk

Definition at line 188 of file secport.h.

#define PORT_Strrchr   strrchr

Definition at line 180 of file secport.h.

#define PORT_Strstr   strstr

Definition at line 189 of file secport.h.

#define PORT_Strtok   strtok

Definition at line 190 of file secport.h.

Definition at line 192 of file secport.h.

#define PORT_ZNew (   type)    (type*)PORT_ZAlloc(sizeof(type))

Definition at line 149 of file secport.h.

#define PORT_ZNewArray (   type,
  num 
)    (type*) PORT_ZAlloc (sizeof(type)*(num))

Definition at line 157 of file secport.h.


Typedef Documentation

typedef unsigned char* inBuf

Definition at line 195 of file secport.h.

typedef unsigned char unsigned int inBufLen

Definition at line 195 of file secport.h.

typedef unsigned char unsigned int unsigned char unsigned int maxOutBufLen

Definition at line 195 of file secport.h.

typedef unsigned char unsigned int unsigned char * outBuf

Definition at line 195 of file secport.h.

typedef unsigned char unsigned int unsigned char unsigned int unsigned int * outBufLen

Definition at line 195 of file secport.h.

typedef unsigned char unsigned int unsigned char unsigned int unsigned int PRBool swapBytes

Definition at line 195 of file secport.h.


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
}
SEC_BEGIN_PROTOS void* PORT_Alloc ( size_t  len)

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

Definition at line 48 of file nssstubs.c.

{
  /* Always allocate a non-zero amount of bytes */
  return (void *)PR_Malloc(bytes ? bytes : 1);
}
void* PORT_AllocBlock ( size_t  len)
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);
}

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;
}
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;
}
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);
}
void PORT_ArenaZRelease ( PLArenaPool arena,
void mark 
)

Definition at line 488 of file secport.c.

void PORT_Free ( void ptr)

Definition at line 55 of file nssstubs.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);
    }
}
void PORT_FreeBlock ( void ptr)

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 
)
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 old,
size_t  len 
)

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;
}
void* PORT_ReallocBlock ( void old,
size_t  len 
)
void PORT_SetError ( int  value)

Definition at line 63 of file nssstubs.c.

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

Definition at line 565 of file secport.c.

{ 
    ucs2AsciiConvertFunc = convFunc;
}
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 *  s)

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;
}
time_t PORT_Time ( void  )
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.

PRBool PORT_UCS2_UTF8Conversion ( PRBool  toUnicode,
unsigned char *  inBuf,
unsigned int  inBufLen,
unsigned char *  outBuf,
unsigned int  maxOutBufLen,
unsigned int outBufLen 
)
PRBool PORT_UCS4_UTF8Conversion ( PRBool  toUnicode,
unsigned char *  inBuf,
unsigned int  inBufLen,
unsigned char *  outBuf,
unsigned int  maxOutBufLen,
unsigned int outBufLen 
)
void* PORT_ZAlloc ( size_t  len)

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);
    }
}
typedef PRBool ( PR_CALLBACK PORTCharConversionWSwapFunc)
PRBool sec_port_iso88591_utf8_conversion_function ( const unsigned char *  inBuf,
unsigned int  inBufLen,
unsigned char *  outBuf,
unsigned int  maxOutBufLen,
unsigned int outBufLen 
)

Definition at line 439 of file utf8.c.

{
  unsigned int i, len = 0;

  PORT_Assert((unsigned int *)NULL != outBufLen);

  for( i = 0; i < inBufLen; i++) {
    if( (inBuf[i] & 0x80) == 0x00 ) len += 1;
    else len += 2;
  }

  if( len > maxOutBufLen ) {
    *outBufLen = len;
    return PR_FALSE;
  }

  len = 0;

  for( i = 0; i < inBufLen; i++) {
    if( (inBuf[i] & 0x80) == 0x00 ) {
      /* 00-7F -> 0xxxxxxx */
      /* 0abcdefg -> 0abcdefg */

      outBuf[len] = inBuf[i];
      len += 1;
    } else {
      /* 80-FF <- 110xxxxx 10xxxxxx */
      /* 00000000 abcdefgh -> 110000ab 10cdefgh */

      outBuf[len+0] = 0xC0 | ((inBuf[i] & 0xC0) >> 6);
      outBuf[len+1] = 0x80 | ((inBuf[i] & 0x3F) >> 0);

      len += 2;
    }
  }

  *outBufLen = len;
  return PR_TRUE;
}

Here is the caller graph for this function:

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

Definition at line 292 of file utf8.c.

{
  PORT_Assert((unsigned int *)NULL != outBufLen);

  if( toUnicode ) {
    unsigned int i, len = 0;

    for( i = 0; i < inBufLen; ) {
      if( (inBuf[i] & 0x80) == 0x00 ) {
        i += 1;
        len += 2;
      } else if( (inBuf[i] & 0xE0) == 0xC0 ) {
        i += 2;
        len += 2;
      } else if( (inBuf[i] & 0xF0) == 0xE0 ) {
        i += 3;
        len += 2;
      } else if( (inBuf[i] & 0xF8) == 0xF0 ) { 
        i += 4;
        len += 4;
      } else return PR_FALSE;
    }

    if( len > maxOutBufLen ) {
      *outBufLen = len;
      return PR_FALSE;
    }

    len = 0;

    for( i = 0; i < inBufLen; ) {
      PRUint32 ucs4 = sec_port_read_utf8(&i, inBuf, inBufLen);

      if (ucs4 == BAD_UTF8) return PR_FALSE;

      if( ucs4 < 0x10000) {
        outBuf[len+H_0] = (unsigned char)(ucs4 >> 8);
        outBuf[len+H_1] = (unsigned char)ucs4;
        len += 2;
      } else {
       ucs4 -= 0x10000;
        outBuf[len+0+H_0] = (unsigned char)(0xD8 | ((ucs4 >> 18) & 0x3));
        outBuf[len+0+H_1] = (unsigned char)(ucs4 >> 10);
        outBuf[len+2+H_0] = (unsigned char)(0xDC | ((ucs4 >> 8) & 0x3));
        outBuf[len+2+H_1] = (unsigned char)ucs4;
       len += 4;
      }
    }

    *outBufLen = len;
    return PR_TRUE;
  } else {
    unsigned int i, len = 0;
    PORT_Assert((inBufLen % 2) == 0);
    if ((inBufLen % 2) != 0) {
      *outBufLen = 0;
      return PR_FALSE;
    }

    for( i = 0; i < inBufLen; i += 2 ) {
      if( (inBuf[i+H_0] == 0x00) && ((inBuf[i+H_0] & 0x80) == 0x00) ) len += 1;
      else if( inBuf[i+H_0] < 0x08 ) len += 2;
      else if( ((inBuf[i+0+H_0] & 0xDC) == 0xD8) ) {
        if( ((inBuf[i+2+H_0] & 0xDC) == 0xDC) && ((inBufLen - i) > 2) ) {
          i += 2;
          len += 4;
        } else {
          return PR_FALSE;
        }
      }
      else len += 3;
    }

    if( len > maxOutBufLen ) {
      *outBufLen = len;
      return PR_FALSE;
    }

    len = 0;

    for( i = 0; i < inBufLen; i += 2 ) {
      if( (inBuf[i+H_0] == 0x00) && ((inBuf[i+H_1] & 0x80) == 0x00) ) {
        /* 0000-007F -> 0xxxxxx */
        /* 00000000 0abcdefg -> 0abcdefg */

        outBuf[len] = inBuf[i+H_1] & 0x7F;

        len += 1;
      } else if( inBuf[i+H_0] < 0x08 ) {
        /* 0080-07FF -> 110xxxxx 10xxxxxx */
        /* 00000abc defghijk -> 110abcde 10fghijk */

        outBuf[len+0] = 0xC0 | ((inBuf[i+H_0] & 0x07) << 2) 
                             | ((inBuf[i+H_1] & 0xC0) >> 6);
        outBuf[len+1] = 0x80 | ((inBuf[i+H_1] & 0x3F) >> 0);

        len += 2;
      } else if( (inBuf[i+H_0] & 0xDC) == 0xD8 ) {
        int abcde, BCDE;

        PORT_Assert(((inBuf[i+2+H_0] & 0xDC) == 0xDC) && ((inBufLen - i) > 2));

        /* D800-DBFF DC00-DFFF -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
        /* 110110BC DEfghijk 110111lm nopqrstu ->
           { Let abcde = BCDE + 1 }
           11110abc 10defghi 10jklmno 10pqrstu */

        BCDE = ((inBuf[i+H_0] & 0x03) << 2) | ((inBuf[i+H_1] & 0xC0) >> 6);
        abcde = BCDE + 1;

        outBuf[len+0] = 0xF0 | ((abcde & 0x1C) >> 2);
        outBuf[len+1] = 0x80 | ((abcde & 0x03) << 4) 
                             | ((inBuf[i+0+H_1] & 0x3C) >> 2);
        outBuf[len+2] = 0x80 | ((inBuf[i+0+H_1] & 0x03) << 4)
                             | ((inBuf[i+2+H_0] & 0x03) << 2)
                             | ((inBuf[i+2+H_1] & 0xC0) >> 6);
        outBuf[len+3] = 0x80 | ((inBuf[i+2+H_1] & 0x3F) >> 0);

        i += 2;
        len += 4;
      } else {
        /* 0800-FFFF -> 1110xxxx 10xxxxxx 10xxxxxx */
        /* abcdefgh ijklmnop -> 1110abcd 10efghij 10klmnop */

        outBuf[len+0] = 0xE0 | ((inBuf[i+H_0] & 0xF0) >> 4);
        outBuf[len+1] = 0x80 | ((inBuf[i+H_0] & 0x0F) << 2) 
                             | ((inBuf[i+H_1] & 0xC0) >> 6);
        outBuf[len+2] = 0x80 | ((inBuf[i+H_1] & 0x3F) >> 0);

        len += 3;
      }
    }

    *outBufLen = len;
    return PR_TRUE;
  }
}

Here is the caller graph for this function:

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

Definition at line 168 of file utf8.c.

{
  PORT_Assert((unsigned int *)NULL != outBufLen);

  if( toUnicode ) {
    unsigned int i, len = 0;

    for( i = 0; i < inBufLen; ) {
      if( (inBuf[i] & 0x80) == 0x00 ) i += 1;
      else if( (inBuf[i] & 0xE0) == 0xC0 ) i += 2;
      else if( (inBuf[i] & 0xF0) == 0xE0 ) i += 3;
      else if( (inBuf[i] & 0xF8) == 0xF0 ) i += 4;
      else return PR_FALSE;

      len += 4;
    }

    if( len > maxOutBufLen ) {
      *outBufLen = len;
      return PR_FALSE;
    }

    len = 0;

    for( i = 0; i < inBufLen; ) {
      PRUint32 ucs4 = sec_port_read_utf8(&i, inBuf, inBufLen);

      if (ucs4 == BAD_UTF8) return PR_FALSE;
           
      outBuf[len+L_0] = 0x00;
      outBuf[len+L_1] = (unsigned char)(ucs4 >> 16);
      outBuf[len+L_2] = (unsigned char)(ucs4 >> 8);
      outBuf[len+L_3] = (unsigned char)ucs4;

      len += 4;
    }

    *outBufLen = len;
    return PR_TRUE;
  } else {
    unsigned int i, len = 0;
    PORT_Assert((inBufLen % 4) == 0);
    if ((inBufLen % 4) != 0) {
      *outBufLen = 0;
      return PR_FALSE;
    }

    for( i = 0; i < inBufLen; i += 4 ) {
      if( (inBuf[i+L_0] > 0x00) || (inBuf[i+L_1] > 0x10) ) {
       *outBufLen = 0;
       return PR_FALSE;
      } else if( inBuf[i+L_1] >= 0x01 ) len += 4;
      else if( inBuf[i+L_2] >= 0x08 ) len += 3;
      else if( (inBuf[i+L_2] > 0x00) || (inBuf[i+L_3] >= 0x80) ) len += 2;
      else len += 1;
    }

    if( len > maxOutBufLen ) {
      *outBufLen = len;
      return PR_FALSE;
    }

    len = 0;

    for( i = 0; i < inBufLen; i += 4 ) {
      if( inBuf[i+L_1] >= 0x01 ) {
        /* 0001 0000-001F FFFF -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
        /* 00000000 000abcde fghijklm nopqrstu ->
           11110abc 10defghi 10jklmno 10pqrstu */

        outBuf[len+0] = 0xF0 | ((inBuf[i+L_1] & 0x1C) >> 2);
        outBuf[len+1] = 0x80 | ((inBuf[i+L_1] & 0x03) << 4)
                             | ((inBuf[i+L_2] & 0xF0) >> 4);
        outBuf[len+2] = 0x80 | ((inBuf[i+L_2] & 0x0F) << 2)
                             | ((inBuf[i+L_3] & 0xC0) >> 6);
        outBuf[len+3] = 0x80 | ((inBuf[i+L_3] & 0x3F) >> 0);

        len += 4;
      } else if( inBuf[i+L_2] >= 0x08 ) {
        /* 0000 0800-0000 FFFF -> 1110xxxx 10xxxxxx 10xxxxxx */
        /* 00000000 00000000 abcdefgh ijklmnop ->
           1110abcd 10efghij 10klmnop */

        outBuf[len+0] = 0xE0 | ((inBuf[i+L_2] & 0xF0) >> 4);
        outBuf[len+1] = 0x80 | ((inBuf[i+L_2] & 0x0F) << 2)
                             | ((inBuf[i+L_3] & 0xC0) >> 6);
        outBuf[len+2] = 0x80 | ((inBuf[i+L_3] & 0x3F) >> 0);

        len += 3;
      } else if( (inBuf[i+L_2] > 0x00) || (inBuf[i+L_3] >= 0x80) ) {
        /* 0000 0080-0000 07FF -> 110xxxxx 10xxxxxx */
        /* 00000000 00000000 00000abc defghijk ->
           110abcde 10fghijk */

        outBuf[len+0] = 0xC0 | ((inBuf[i+L_2] & 0x07) << 2)
                             | ((inBuf[i+L_3] & 0xC0) >> 6);
        outBuf[len+1] = 0x80 | ((inBuf[i+L_3] & 0x3F) >> 0);

        len += 2;
      } else {
        /* 0000 0000-0000 007F -> 0xxxxxx */
        /* 00000000 00000000 00000000 0abcdefg ->
           0abcdefg */

        outBuf[len+0] = (inBuf[i+L_3] & 0x7F);

        len += 1;
      }
    }
                            
    *outBufLen = len;
    return PR_TRUE;
  }
}

Here is the caller graph for this function: