Back to index

opendkim  2.6.2
Defines | Functions | Variables
base32.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dkim.h"

Go to the source code of this file.

Defines

#define BLKSIZE_RAW   5
#define BLKSIZE_ENC   8

Functions

int dkim_base32_encode (char *buf, size_t *buflen, const void *data, size_t size)

Variables

static const char cb32 [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"

Define Documentation

#define BLKSIZE_ENC   8

Definition at line 29 of file base32.c.

#define BLKSIZE_RAW   5

Definition at line 28 of file base32.c.


Function Documentation

int dkim_base32_encode ( char *  buf,
size_t *  buflen,
const void *  data,
size_t  size 
)

Definition at line 54 of file base32.c.

{
       unsigned int lastbits;
       unsigned int padding;
       int iout = 0;
       int iin = 0;
       unsigned char *udata;

       udata = (unsigned char *) data;

       for (;;)
       {
              if (iout >= *buflen || iin >= size)
                     break;

              buf[iout] = cb32[((udata[iin] & 0xf8) >> 3)];
              iout++;

              if (iout >= *buflen || iin >= size)
              {
                     iout--;       /* previous char is useless */
                     break;
              }

              buf[iout] = cb32[((udata[iin] & 0x07) << 2) |
                              ((iin + 1 < size) ?
                               ((udata[iin + 1] & 0xc0) >> 6) : 0)];
              iin++;               /* 0 complete, iin = 1 */
              iout++;

              if (iout >= *buflen || iin >= size)
                     break;
              buf[iout] = cb32[((udata[iin] & 0x3e) >> 1)];
              iout++;

              if (iout >= *buflen || iin >= size)
              {
                     iout--;              /* previous char is useless */
                     break;
              }
              buf[iout] = cb32[((udata[iin] & 0x01) << 4) |
                              ((iin + 1 < size) ?
                               ((udata[iin + 1] & 0xf0) >> 4) : 0)];
              iin++;               /* 1 complete, iin = 2 */
              iout++;

              if (iout >= *buflen || iin >= size)
                     break;
              buf[iout] = cb32[((udata[iin] & 0x0f) << 1) |
                              ((iin + 1 < size) ?
                               ((udata[iin + 1] & 0x80) >> 7) : 0)];
              iin++;               /* 2 complete, iin = 3 */
              iout++;

              if (iout >= *buflen || iin >= size)
                     break;
              buf[iout] = cb32[((udata[iin] & 0x7c) >> 2)];
              iout++;

              if (iout >= *buflen || iin >= size)
              {
                     iout--;              /* previous char is useless */
                     break;
              }
              buf[iout] = cb32[((udata[iin] & 0x03) << 3) |
                              ((iin + 1 < size) ?
                               ((udata[iin + 1] & 0xe0) >> 5) : 0)];
              iin++;               /* 3 complete, iin = 4 */
              iout++;

              if (iout >= *buflen || iin >= size)
                     break;
              buf[iout] = cb32[((udata[iin] & 0x1f))];
              iin++;               /* 4 complete, iin = 5 */
              iout++;
       }

       /* append padding */
       lastbits = (size * 8) % 40;
       if (lastbits == 0)
              padding = 0;
       else if (lastbits == 8)
              padding = 6;
       else if (lastbits == 16)
              padding = 4;
       else if (lastbits == 24)
              padding = 3;
       else if (lastbits == 32)
              padding = 1;

       while (padding > 0 && iout < *buflen)
       {
              buf[iout++] = '=';
              padding--;
       }

       /* ensure NULL termination */
       buf[iout] = '\0';

       /* store number of bytes from data that was used */
       *buflen = iin;

       return iout;
}

Here is the caller graph for this function:


Variable Documentation

const char cb32[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" [static]

Definition at line 31 of file base32.c.