Back to index

opendkim  2.6.2
Functions | Variables
base64.h File Reference
#include <sys/types.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int dkim_base64_decode (u_char *str, u_char *buf, size_t buflen)
int dkim_base64_encode (u_char *data, size_t datalen, u_char *buf, size_t buflen)

Variables

static char base64_h_id [] = "@(#)$Id: base64.h,v 1.1 2009/07/16 19:12:03 cm-msk Exp $"

Function Documentation

int dkim_base64_decode ( u_char *  str,
u_char *  buf,
size_t  buflen 
)

Definition at line 59 of file base64.c.

{
       int n = 0;
       int bits = 0;
       int char_count = 0;
       u_char *c;

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

       for (c = str; *c != '=' && *c != '\0'; c++)
       {
              /* end padding */
              if (*c == '=' || *c == '\0')
                     break;

              /* skip stuff not part of the base64 alphabet (RFC2045) */
              if (!((*c >= 'A' && *c <= 'Z') ||
                    (*c >= 'a' && *c <= 'z') ||
                    (*c >= '0' && *c <= '9') ||
                    (*c == '+') ||
                    (*c == '/')))
                     continue;

              /* everything else gets decoded */
              bits += decoder[(int) *c];
              char_count++;
              if (n + 3 > buflen)
                     return -2;
              if (char_count == 4)
              {
                     buf[n++] = (bits >> 16);
                     buf[n++] = ((bits >> 8) & 0xff);
                     buf[n++] = (bits & 0xff);
                     bits = 0;
                     char_count = 0;
              }
              else
              {
                     bits <<= 6;
              }
       }

       /* XXX -- don't bother checking for proper termination (for now) */

       /* process trailing data, if any */
       switch (char_count)
       {
         case 0:
              break;

         case 1:
              /* base64 decoding incomplete; at least two bits missing */
              return -1;

         case 2:
              if (n + 1 > buflen)
                     return -2;
              buf[n++] = (bits >> 10);
              break;

         case 3:
              if (n + 2 > buflen)
                     return -2;
              buf[n++] = (bits >> 16);
              buf[n++] = ((bits >> 8) & 0xff);
              break;
       }

       return n;
}

Here is the caller graph for this function:

int dkim_base64_encode ( u_char *  data,
size_t  datalen,
u_char *  buf,
size_t  buflen 
)

Definition at line 146 of file base64.c.

{
       int bits;
       int c;
       int char_count;
       size_t n;

       assert(data != NULL);
       assert(buf != NULL);

       bits = 0;
       char_count = 0;
       n = 0;

       for (c = 0; c < datalen; c++)
       {
              bits += data[c];
              char_count++;
              if (char_count == 3)
              {
                     if (n + 4 > buflen)
                            return -1;

                     buf[n++] = alphabet[bits >> 18];
                     buf[n++] = alphabet[(bits >> 12) & 0x3f];
                     buf[n++] = alphabet[(bits >> 6) & 0x3f];
                     buf[n++] = alphabet[bits & 0x3f];
                     bits = 0;
                     char_count = 0;
              }
              else
              {
                     bits <<= 8;
              }
       }

       if (char_count != 0)
       {
              if (n + 4 > buflen)
                     return -1;

              bits <<= 16 - (8 * char_count);
              buf[n++] = alphabet[bits >> 18];
              buf[n++] = alphabet[(bits >> 12) & 0x3f];
              if (char_count == 1)
              {
                     buf[n++] = '=';
                     buf[n++] = '=';
              }
              else
              {
                     buf[n++] = alphabet[(bits >> 6) & 0x3f];
                     buf[n++] = '=';
              }
       }

       return n;
}

Here is the caller graph for this function:


Variable Documentation

char base64_h_id[] = "@(#)$Id: base64.h,v 1.1 2009/07/16 19:12:03 cm-msk Exp $" [static]

Definition at line 12 of file base64.h.