Back to index

lightning-sunbird  0.9+nobinonly
Functions | Variables
jar.c File Reference
#include "jar.h"
#include "jarint.h"

Go to the source code of this file.

Functions

static void jar_destroy_list (ZZList *list)
static int jar_find_first_cert (JAR_Signer *signer, int type, JAR_Item **it)
JARJAR_new (void)
void PR_CALLBACK JAR_destroy (JAR *jar)
int JAR_get_metainfo (JAR *jar, char *name, char *header, void **info, unsigned long *length)
JAR_ContextJAR_find (JAR *jar, char *pattern, jarType type)
void JAR_find_end (JAR_Context *ctx)
int JAR_find_next (JAR_Context *ctx, JAR_Item **it)
JAR_SignerJAR_new_signer (void)
void JAR_destroy_signer (JAR_Signer *signer)
JAR_Signerjar_get_signer (JAR *jar, char *basename)
char * JAR_get_filename (JAR *jar)
char * JAR_get_url (JAR *jar)
int JAR_set_callback (int type, JAR *jar, int(*fn)(int status, JAR *jar, const char *metafile, char *pathname, char *errortext))
void JAR_init_callbacks (char *(*string_cb)(int), void *(*find_cx)(void), void *(*init_cx)(void))
char * JAR_get_error (int status)

Variables

char *(* jar_fn_GetString )(int) = NULL
void *(* jar_fn_FindSomeContext )(void) = NULL
void *(* jar_fn_GetInitContext )(void) = NULL

Function Documentation

Definition at line 117 of file jar.c.

  {
  PORT_Assert( jar != NULL );

  if (jar == NULL)
    return;

  if (jar->fp) JAR_FCLOSE ((PRFileDesc*)jar->fp);

  if (jar->url)      PORT_Free (jar->url);
  if (jar->filename) PORT_Free (jar->filename);

  /* Free the linked list elements */

  jar_destroy_list (jar->manifest);
  ZZ_DestroyList (jar->manifest);

  jar_destroy_list (jar->hashes);
  ZZ_DestroyList (jar->hashes);

  jar_destroy_list (jar->phy);
  ZZ_DestroyList (jar->phy);

  jar_destroy_list (jar->metainfo);
  ZZ_DestroyList (jar->metainfo);

  jar_destroy_list (jar->signers);
  ZZ_DestroyList (jar->signers);

  PORT_Free (jar);
  }

Here is the call graph for this function:

static void jar_destroy_list ( ZZList *  list) [static]

Definition at line 149 of file jar.c.

  {
  ZZLink *link, *oldlink;

  JAR_Item *it;

  JAR_Physical *phy;
  JAR_Digest *dig;
  JAR_Cert *fing;
  JAR_Metainfo *met;
  JAR_Signer *signer;

  if (list && !ZZ_ListEmpty (list))
    {
    link = ZZ_ListHead (list);

    while (!ZZ_ListIterDone (list, link))
      {
      it = link->thing;
      if (!it) goto next;

      if (it->pathname) PORT_Free (it->pathname);
 
      switch (it->type)
        {
        case jarTypeMeta:

          met = (JAR_Metainfo *) it->data;
          if (met)
            {
            if (met->header) PORT_Free (met->header);
            if (met->info) PORT_Free (met->info);
            PORT_Free (met);
            }
          break;

       case jarTypePhy:

          phy = (JAR_Physical *) it->data;
          if (phy)
            PORT_Free (phy);
          break;

        case jarTypeSign:

          fing = (JAR_Cert *) it->data;
          if (fing)
            {
            if (fing->cert)
              CERT_DestroyCertificate (fing->cert);
            if (fing->key)
              PORT_Free (fing->key);
            PORT_Free (fing);
            }
         break;

        case jarTypeSect:
        case jarTypeMF:
        case jarTypeSF:

          dig = (JAR_Digest *) it->data;
          if (dig)
            {
            PORT_Free (dig);
            }
          break;

        case jarTypeOwner:

          signer = (JAR_Signer *) it->data;

          if (signer)
            JAR_destroy_signer (signer);

          break;

        default:

          /* PORT_Assert( 1 != 2 ); */
          break;
        }

      PORT_Free (it);

    next:

      oldlink = link;
      link = link->next;

      ZZ_DestroyLink (oldlink);
      }
    }
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 627 of file jar.c.

  {
  if (signer)
    {
    if (signer->owner) PORT_Free (signer->owner);
    if (signer->digest) PORT_Free (signer->digest);

    jar_destroy_list (signer->sf);
    ZZ_DestroyList (signer->sf);

    jar_destroy_list (signer->certs);
    ZZ_DestroyList (signer->certs);

    PORT_Free (signer);
    }
  }

Here is the call graph for this function:

JAR_Context* JAR_find ( JAR jar,
char *  pattern,
jarType  type 
)

Definition at line 309 of file jar.c.

  {
  JAR_Context *ctx;

  PORT_Assert( jar != NULL );

  if (!jar)
    return NULL;

  ctx = (JAR_Context *) PORT_ZAlloc (sizeof (JAR_Context));

  if (ctx == NULL)
    return NULL;

  ctx->jar = jar;

  if (pattern)
    {
    if ((ctx->pattern = PORT_Strdup (pattern)) == NULL)
      {
      PORT_Free (ctx);
      return NULL;
      }
    }

  ctx->finding = type;

  switch (type)
    {
    case jarTypeMF:     ctx->next = ZZ_ListHead (jar->hashes);
                        break;

    case jarTypeSF:
    case jarTypeSign:   ctx->next = NULL;
                        ctx->nextsign = ZZ_ListHead (jar->signers);
                        break;

    case jarTypeSect:   ctx->next = ZZ_ListHead (jar->manifest);
                        break;

    case jarTypePhy:    ctx->next = ZZ_ListHead (jar->phy);
                        break;

    case jarTypeOwner:  if (jar->signers)
                          ctx->next = ZZ_ListHead (jar->signers);
                        else
                          ctx->next = NULL;
                        break;

    case jarTypeMeta:   ctx->next = ZZ_ListHead (jar->metainfo);
                        break;

    default:            PORT_Assert( 1 != 2);
                        break;
    }

  return ctx;
  }

Here is the call graph for this function:

Definition at line 375 of file jar.c.

  {
  PORT_Assert( ctx != NULL );

  if (ctx)
    {
    if (ctx->pattern) 
      PORT_Free (ctx->pattern);
    PORT_Free (ctx);
    }
  }

Here is the call graph for this function:

static int jar_find_first_cert ( JAR_Signer signer,
int  type,
JAR_Item **  it 
) [static]

Definition at line 552 of file jar.c.

  {
  ZZLink *link;
  ZZList *list;

  int status = JAR_ERR_PNF;

  list = signer->certs;

  *it = NULL;

  if (ZZ_ListEmpty (list))
    {
    /* empty list */
    return JAR_ERR_PNF;
    }

  for (link = ZZ_ListHead (list); 
       !ZZ_ListIterDone (list, link); 
       link = link->next)
    {
    if (link->thing->type == type)
      {
      *it = link->thing;
      status = 0;
      break;
      }
    }

  return status;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

int JAR_find_next ( JAR_Context ctx,
JAR_Item **  it 
)

Definition at line 395 of file jar.c.

  {
  JAR *jar;
  ZZList *list = NULL;

  int finding;

  JAR_Signer *signer = NULL;

  PORT_Assert( ctx != NULL );
  PORT_Assert( ctx->jar != NULL );

  jar = ctx->jar;

  /* Internally, convert jarTypeSign to jarTypeSF, and return
     the actual attached certificate later */

  finding = (ctx->finding == jarTypeSign) ? jarTypeSF : ctx->finding;

  if (ctx->nextsign)
    {
      if (ZZ_ListIterDone (jar->signers, ctx->nextsign))
       {
       *it = NULL;
       return -1;
       }
    PORT_Assert (ctx->nextsign->thing != NULL);
    signer = (JAR_Signer*)ctx->nextsign->thing->data;
    }


  /* Find out which linked list to traverse. Then if
     necessary, advance to the next linked list. */

  while (1)
    {
    switch (finding)
      {
      case jarTypeSign:    /* not any more */
                           PORT_Assert( finding != jarTypeSign );
                           list = signer->certs;
                           break;

      case jarTypeSect:    list = jar->manifest;
                           break;

      case jarTypePhy:     list = jar->phy;
                           break;

      case jarTypeSF:      /* signer, not jar */
                           PORT_Assert( signer != NULL );
                           list = signer->sf;
                           break;

      case jarTypeMF:      list = jar->hashes;
                           break;

      case jarTypeOwner:   list = jar->signers;
                           break;

      case jarTypeMeta:    list = jar->metainfo;
                           break;

      default:             PORT_Assert( 1 != 2 );
                           break;
      }

    if (list == NULL)
      {
      *it = NULL;
      return -1;
      }

    /* When looping over lists of lists, advance
       to the next signer. This is done when multiple
       signers are possible. */

    if (ZZ_ListIterDone (list, ctx->next))
      {
      if (ctx->nextsign && jar->signers) 
        {
        ctx->nextsign = ctx->nextsign->next;
        if (!ZZ_ListIterDone (jar->signers, ctx->nextsign)) 
          {
          PORT_Assert (ctx->nextsign->thing != NULL);

          signer = (JAR_Signer*)ctx->nextsign->thing->data;
          PORT_Assert( signer != NULL );

          ctx->next = NULL;
          continue;
          }
        }
      *it = NULL;
      return -1;
      }

    /* if the signer changed, still need to fill
       in the "next" link */

    if (ctx->nextsign && ctx->next == NULL)
      {
      switch (finding)
        {
        case jarTypeSF:

          ctx->next = ZZ_ListHead (signer->sf);
          break;

        case jarTypeSign:

          ctx->next = ZZ_ListHead (signer->certs);
          break;
        }
      }

    PORT_Assert( ctx->next != NULL );


    while (!ZZ_ListIterDone (list, ctx->next))
      {
      *it = ctx->next->thing;
      ctx->next = ctx->next->next;

      if (!it || !*it || (*it)->type != finding)
        continue;

      if (ctx->pattern && *ctx->pattern)
        {
        if (PORT_Strcmp ((*it)->pathname, ctx->pattern))
          continue;
        }

      /* We have a valid match. If this is a jarTypeSign
         return the certificate instead.. */

      if (ctx->finding == jarTypeSign)
        {
        JAR_Item *itt;

        /* just the first one for now */
        if (jar_find_first_cert (signer, jarTypeSign, &itt) >= 0) 
           {
           *it = itt;
           return 0;
           }

        continue;      
        }

      return 0;
      }

    } /* end while */
  }

Here is the call graph for this function:

char* JAR_get_error ( int  status)

Definition at line 756 of file jar.c.

  { 
  char *errstring = NULL;

  switch (status)
    {
    case JAR_ERR_GENERAL:
      errstring = "General JAR file error";
      break;

    case JAR_ERR_FNF:
      errstring = "JAR file not found";
      break;

    case JAR_ERR_CORRUPT:
      errstring = "Corrupt JAR file";
      break;

    case JAR_ERR_MEMORY:
      errstring = "Out of memory";
      break;

    case JAR_ERR_DISK:
      errstring = "Disk error (perhaps out of space)";
      break;

    case JAR_ERR_ORDER:
      errstring = "Inconsistent files in META-INF directory";
      break;

    case JAR_ERR_SIG:
      errstring = "Invalid digital signature file";
      break;

    case JAR_ERR_METADATA:
      errstring = "JAR metadata failed verification";
      break;

    case JAR_ERR_ENTRY:
      errstring = "No Manifest entry for this JAR entry";
      break;

    case JAR_ERR_HASH:
      errstring = "Invalid Hash of this JAR entry";
      break;

    case JAR_ERR_PK7:
      errstring = "Strange PKCS7 or RSA failure";
      break;

    case JAR_ERR_PNF:
      errstring = "Path not found inside JAR file";
      break;

    default:
      if (jar_fn_GetString)
        {
        errstring = jar_fn_GetString (status);
        }
      else
        {
        /* this is not a normal situation, and would only be
           called in cases of improper initialization */

        char *err;

        err = (char*)PORT_Alloc (40);
        if (err)
          PR_snprintf (err, 39,  "Error %d\n", status);
        else
          err = "Error! Bad! Out of memory!";

        return err;
        }
      break;
    }

  return errstring;
  }

Here is the call graph for this function:

char* JAR_get_filename ( JAR jar)

Definition at line 680 of file jar.c.

  {
  return jar->filename;
  }
int JAR_get_metainfo ( JAR jar,
char *  name,
char *  header,
void **  info,
unsigned long length 
)

Definition at line 252 of file jar.c.

  {
  JAR_Item *it;

  ZZLink *link;
  ZZList *list;

  JAR_Metainfo *met;

  PORT_Assert( jar != NULL && header != NULL );

  if (jar == NULL || header == NULL)
    return JAR_ERR_PNF;

  list = jar->metainfo;

  if (ZZ_ListEmpty (list))
    return JAR_ERR_PNF;

  for (link = ZZ_ListHead (list); 
       !ZZ_ListIterDone (list, link); 
       link = link->next)
    {
    it = link->thing;
    if (it->type == jarTypeMeta)
      {
      if ((name && !it->pathname) || (!name && it->pathname))
        continue;

      if (name && it->pathname && strcmp (it->pathname, name))
        continue;

      met = (JAR_Metainfo *) it->data;

      if (!PORT_Strcasecmp (met->header, header))
        {
        *info = PORT_Strdup (met->info);
        *length = PORT_Strlen (met->info);
        return 0;
        }
      }
    }

  return JAR_ERR_PNF;
  }

Here is the call graph for this function:

JAR_Signer* jar_get_signer ( JAR jar,
char *  basename 
)

Definition at line 644 of file jar.c.

  {
  JAR_Item *it;
  JAR_Context *ctx;

  JAR_Signer *candidate;
  JAR_Signer *signer = NULL;

  ctx = JAR_find (jar, NULL, jarTypeOwner);

  if (ctx == NULL)
    return NULL;

  while (JAR_find_next (ctx, &it) >= 0)
    {
    candidate = (JAR_Signer *) it->data;
    if (*basename == '*' || !PORT_Strcmp (candidate->owner, basename))
      {
      signer = candidate;
      break;
      }
    }

  JAR_find_end (ctx);

  return signer;
  }

Here is the call graph for this function:

char* JAR_get_url ( JAR jar)

Definition at line 693 of file jar.c.

  {
  return jar->url;
  }
void JAR_init_callbacks ( char *(*)(int string_cb,
void *(*)(void find_cx,
void *(*)(void init_cx 
)

Definition at line 734 of file jar.c.

  {
  jar_fn_GetString = string_cb;
  jar_fn_FindSomeContext = find_cx;
  jar_fn_GetInitContext = init_cx;
  }
JAR* JAR_new ( void  )

Definition at line 61 of file jar.c.

  {
  JAR *jar;

  if ((jar = (JAR*)PORT_ZAlloc (sizeof (JAR))) == NULL)
    goto loser;

  if ((jar->manifest = ZZ_NewList()) == NULL)
    goto loser;

  if ((jar->hashes = ZZ_NewList()) == NULL)
    goto loser;

  if ((jar->phy = ZZ_NewList()) == NULL)
    goto loser;

  if ((jar->metainfo = ZZ_NewList()) == NULL)
    goto loser;

  if ((jar->signers = ZZ_NewList()) == NULL)
    goto loser;

  return jar;

loser:

  if (jar)
    {
    if (jar->manifest)
      ZZ_DestroyList (jar->manifest);

    if (jar->hashes)
      ZZ_DestroyList (jar->hashes);

    if (jar->phy)
      ZZ_DestroyList (jar->phy);

    if (jar->metainfo)
      ZZ_DestroyList (jar->metainfo);

    if (jar->signers)
      ZZ_DestroyList (jar->signers);

    PORT_Free (jar);
    }

  return NULL;
  }

Here is the call graph for this function:

Definition at line 584 of file jar.c.

  {
  JAR_Signer *signer;

  signer = (JAR_Signer *) PORT_ZAlloc (sizeof (JAR_Signer));

  if (signer == NULL)
    goto loser;


  /* certs */
  signer->certs = ZZ_NewList();

  if (signer->certs == NULL)
    goto loser;


  /* sf */
  signer->sf = ZZ_NewList();

  if (signer->sf == NULL)
    goto loser;


  return signer;


loser:

  if (signer)
    {
    if (signer->certs) 
      ZZ_DestroyList (signer->certs);

    if (signer->sf) 
      ZZ_DestroyList (signer->sf);

    PORT_Free (signer);
    }

  return NULL;
  }

Here is the call graph for this function:

int JAR_set_callback ( int  type,
JAR jar,
int(*)(int status, JAR *jar, const char *metafile, char *pathname, char *errortext)  fn 
)

Definition at line 705 of file jar.c.

  {
  if (type == JAR_CB_SIGNAL)
    {
    jar->signal = fn;
    return 0;
    }
  else
    return -1;
  }

Variable Documentation

Definition at line 727 of file jar.c.

Definition at line 730 of file jar.c.

char*(* jar_fn_GetString)(int) = NULL

Definition at line 724 of file jar.c.