Back to index

opendkim  2.6.4
Functions | Variables
dkim-util.c File Reference
#include "build-config.h"
#include <sys/param.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "dkim-internal.h"
#include "dkim-types.h"
#include "dkim-util.h"

Go to the source code of this file.

Functions

void dkim_error __P ((DKIM *, const char *,...))
void * dkim_malloc (DKIM_LIB *libhandle, void *closure, size_t nbytes)
void dkim_mfree (DKIM_LIB *libhandle, void *closure, void *ptr)
unsigned char * dkim_strdup (DKIM *dkim, const unsigned char *str, size_t len)
DKIM_STAT dkim_tmpfile (DKIM *dkim, int *fp, _Bool keep)
static _Bool dkim_dstring_resize (struct dkim_dstring *dstr, int len)
struct dkim_dstringdkim_dstring_new (DKIM *dkim, int len, int maxlen)
void dkim_dstring_free (struct dkim_dstring *dstr)
_Bool dkim_dstring_copy (struct dkim_dstring *dstr, unsigned char *str)
_Bool dkim_dstring_cat (struct dkim_dstring *dstr, unsigned char *str)
_Bool dkim_dstring_cat1 (struct dkim_dstring *dstr, int c)
_Bool dkim_dstring_catn (struct dkim_dstring *dstr, unsigned char *str, size_t nbytes)
unsigned char * dkim_dstring_get (struct dkim_dstring *dstr)
int dkim_dstring_len (struct dkim_dstring *dstr)
void dkim_dstring_blank (struct dkim_dstring *dstr)
size_t dkim_dstring_printf (struct dkim_dstring *dstr, char *fmt,...)

Variables

static char dkim_util_c_id [] = "@(#)$Id: dkim-util.c,v 1.7.58.1 2010/10/27 21:43:08 cm-msk Exp $"

Function Documentation

void dkim_error __P ( (DKIM *, const char *,...)  )
void dkim_dstring_blank ( struct dkim_dstring dstr)

Definition at line 548 of file dkim-util.c.

{
       assert(dstr != NULL);

       dstr->ds_len = 0;
       dstr->ds_buf[0] = '\0';
}

Here is the caller graph for this function:

_Bool dkim_dstring_cat ( struct dkim_dstring dstr,
unsigned char *  str 
)

Definition at line 384 of file dkim-util.c.

{
       size_t len;
       size_t needed;

       assert(dstr != NULL);
       assert(str != NULL);

       len = strlen((char *) str);
       needed = dstr->ds_len + len;

       /* too big? */
       if (dstr->ds_max > 0 && needed >= dstr->ds_max)
              return FALSE;

       /* fits now? */
       if (dstr->ds_alloc <= needed)
       {
              /* nope; try to resize */
              if (!dkim_dstring_resize(dstr, needed + 1))
                     return FALSE;
       }

       /* append */
       memcpy(dstr->ds_buf + dstr->ds_len, str, len + 1);
       dstr->ds_len += len;

       return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

_Bool dkim_dstring_cat1 ( struct dkim_dstring dstr,
int  c 
)

Definition at line 429 of file dkim-util.c.

{
       int len;

       assert(dstr != NULL);

       len = dstr->ds_len + 1;

       /* too big? */
       if (dstr->ds_max > 0 && len >= dstr->ds_max)
              return FALSE;

       /* fits now? */
       if (dstr->ds_alloc <= len)
       {
              /* nope; try to resize */
              if (!dkim_dstring_resize(dstr, len + 1))
                     return FALSE;
       }

       /* append */
       dstr->ds_buf[dstr->ds_len++] = c;
       dstr->ds_buf[dstr->ds_len] = '\0';

       return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

_Bool dkim_dstring_catn ( struct dkim_dstring dstr,
unsigned char *  str,
size_t  nbytes 
)

Definition at line 472 of file dkim-util.c.

{
       size_t needed;

       assert(dstr != NULL);
       assert(str != NULL);

       needed = dstr->ds_len + nbytes;

       /* too big? */
       if (dstr->ds_max > 0 && needed >= dstr->ds_max)
              return FALSE;

       /* fits now? */
       if (dstr->ds_alloc <= needed)
       {
              /* nope; try to resize */
              if (!dkim_dstring_resize(dstr, needed + 1))
                     return FALSE;
       }

       /* append */
       memcpy(dstr->ds_buf + dstr->ds_len, str, nbytes);
       dstr->ds_len += nbytes;
       dstr->ds_buf[dstr->ds_len] = '\0';

       return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

_Bool dkim_dstring_copy ( struct dkim_dstring dstr,
unsigned char *  str 
)

Definition at line 341 of file dkim-util.c.

{
       int len;

       assert(dstr != NULL);
       assert(str != NULL);

       len = strlen((char *) str);

       /* too big? */
       if (dstr->ds_max > 0 && len >= dstr->ds_max)
              return FALSE;

       /* fits now? */
       if (dstr->ds_alloc <= len)
       {
              /* nope; try to resize */
              if (!dkim_dstring_resize(dstr, len + 1))
                     return FALSE;
       }

       /* copy */
       memcpy(dstr->ds_buf, str, len + 1);
       dstr->ds_len = len;

       return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void dkim_dstring_free ( struct dkim_dstring dstr)

Definition at line 312 of file dkim-util.c.

{
       DKIM_LIB *lib;
       DKIM *dkim;

       assert(dstr != NULL);

       dkim = dstr->ds_dkim;
       lib = dkim->dkim_libhandle;

       dkim_mfree(lib, dkim->dkim_closure, dstr->ds_buf);
       dkim_mfree(lib, dkim->dkim_closure, dstr);
}

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned char* dkim_dstring_get ( struct dkim_dstring dstr)

Definition at line 512 of file dkim-util.c.

{
       assert(dstr != NULL);

       return dstr->ds_buf;
}

Here is the caller graph for this function:

int dkim_dstring_len ( struct dkim_dstring dstr)

Definition at line 530 of file dkim-util.c.

{
       assert(dstr != NULL);

       return dstr->ds_len;
}

Here is the caller graph for this function:

struct dkim_dstring* dkim_dstring_new ( DKIM *  dkim,
int  len,
int  maxlen 
) [read]

Definition at line 259 of file dkim-util.c.

{
       struct dkim_dstring *new;
       DKIM_LIB *lib;

       assert(dkim != NULL);

       /* fail on invalid parameters */
       if ((maxlen > 0 && len > maxlen) || len < 0)
              return NULL;

       lib = dkim->dkim_libhandle;

       if (len < BUFRSZ)
              len = BUFRSZ;

       new = dkim_malloc(lib, dkim->dkim_closure, sizeof(struct dkim_dstring));
       if (new == NULL)
       {
              dkim_error(dkim, "unable to allocate %d byte(s)",
                         sizeof(struct dkim_dstring));
              return NULL;
       }

       new->ds_buf = dkim_malloc(lib, dkim->dkim_closure, len);
       if (new->ds_buf == NULL)
       {
              dkim_error(dkim, "unable to allocate %d byte(s)",
                         sizeof(struct dkim_dstring));
              dkim_mfree(lib, dkim->dkim_closure, new);
              return NULL;
       }

       memset(new->ds_buf, '\0', len);
       new->ds_alloc = len;
       new->ds_len = 0;
       new->ds_max = maxlen;
       new->ds_dkim = dkim;

       return new;
}

Here is the call graph for this function:

Here is the caller graph for this function:

size_t dkim_dstring_printf ( struct dkim_dstring dstr,
char *  fmt,
  ... 
)

Definition at line 569 of file dkim-util.c.

{
       size_t len;
       va_list ap;
       va_list ap2;

       assert(dstr != NULL);
       assert(fmt != NULL);

       va_start(ap, fmt);
       va_copy(ap2, ap);
       len = vsnprintf((char *) dstr->ds_buf + dstr->ds_len, dstr->ds_alloc,
                       fmt, ap);
       va_end(ap);

       if (len > dstr->ds_len)
       {
              if (!dkim_dstring_resize(dstr, len + 1))
              {
                     va_end(ap2);
                     return (size_t) -1;
              }

              len = vsnprintf((char *) dstr->ds_buf + dstr->ds_len,
                              dstr->ds_alloc, fmt, ap2);
       }

       va_end(ap2);

       dstr->ds_len += len;

       return dstr->ds_len;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static _Bool dkim_dstring_resize ( struct dkim_dstring dstr,
int  len 
) [static]

Definition at line 185 of file dkim-util.c.

{
       int newsz;
       unsigned char *new;
       DKIM *dkim;
       DKIM_LIB *lib;

       assert(dstr != NULL);
       assert(len > 0);

       if (dstr->ds_alloc >= len)
              return TRUE;

       dkim = dstr->ds_dkim;
       lib = dkim->dkim_libhandle;

       /* must resize */
       for (newsz = dstr->ds_alloc * 2;
            newsz < len;
            newsz *= 2)
       {
              /* impose ds_max limit, if specified */
              if (dstr->ds_max > 0 && newsz > dstr->ds_max)
              {
                     if (len <= dstr->ds_max)
                     {
                            newsz = len;
                            break;
                     }

                     dkim_error(dkim, "maximum string size exceeded");
                     return FALSE;
              }

              /* check for overflow */
              if (newsz > INT_MAX / 2)
              {
                     /* next iteration will overflow "newsz" */
                     dkim_error(dkim, "internal string limit reached");
                     return FALSE;
              }
       }

       new = dkim_malloc(lib, dkim->dkim_closure, newsz);
       if (new == NULL)
       {
              dkim_error(dkim, "unable to allocate %d byte(s)", newsz);
              return FALSE;
       }

       memcpy(new, dstr->ds_buf, dstr->ds_alloc);

       dkim_mfree(lib, dkim->dkim_closure, dstr->ds_buf);

       dstr->ds_alloc = newsz;
       dstr->ds_buf = new;

       return TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* dkim_malloc ( DKIM_LIB *  libhandle,
void *  closure,
size_t  nbytes 
)

Definition at line 50 of file dkim-util.c.

{
       assert(libhandle != NULL);

       if (libhandle->dkiml_malloc == NULL)
              return malloc(nbytes);
       else
              return libhandle->dkiml_malloc(closure, nbytes);
}

Here is the caller graph for this function:

void dkim_mfree ( DKIM_LIB *  libhandle,
void *  closure,
void *  ptr 
)

Definition at line 73 of file dkim-util.c.

{
       assert(libhandle != NULL);

       if (libhandle->dkiml_free == NULL)
              free(ptr);
       else
              libhandle->dkiml_free(closure, ptr);
}

Here is the caller graph for this function:

unsigned char* dkim_strdup ( DKIM *  dkim,
const unsigned char *  str,
size_t  len 
)

Definition at line 97 of file dkim-util.c.

{
       unsigned char *new;

       assert(dkim != NULL);
       assert(str != NULL);

       if (len == 0)
              len = strlen((char *) str);

       new = dkim_malloc(dkim->dkim_libhandle, dkim->dkim_closure, len + 1);
       if (new != NULL)
       {
              memcpy(new, str, len);
              new[len] = '\0';
       }
       else
       {
              dkim_error(dkim, "unable to allocate %d byte(s)", len + 1);
       }
       return new;
}

Here is the call graph for this function:

Here is the caller graph for this function:

DKIM_STAT dkim_tmpfile ( DKIM *  dkim,
int *  fp,
_Bool  keep 
)

Definition at line 133 of file dkim-util.c.

{
       int fd;
       char path[MAXPATHLEN + 1];

       assert(dkim != NULL);
       assert(fp != NULL);

       if (dkim->dkim_id != NULL)
       {
              snprintf(path, MAXPATHLEN, "%s/dkim.%s.XXXXXX",
                       dkim->dkim_libhandle->dkiml_tmpdir, dkim->dkim_id);
       }
       else
       {
              snprintf(path, MAXPATHLEN, "%s/dkim.XXXXXX",
                       dkim->dkim_libhandle->dkiml_tmpdir);
       }

       fd = mkstemp(path);
       if (fd == -1)
       {
              dkim_error(dkim, "can't create temporary file at %s: %s",
                         path, strerror(errno));
              return DKIM_STAT_NORESOURCE;
       }

       *fp = fd;

       if (!keep)
              (void) unlink(path);

       return DKIM_STAT_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char dkim_util_c_id[] = "@(#)$Id: dkim-util.c,v 1.7.58.1 2010/10/27 21:43:08 cm-msk Exp $" [static]

Definition at line 9 of file dkim-util.c.