Back to index

nagios-plugins  1.4.16
Classes | Defines | Functions
base64.h File Reference
#include <stddef.h>
#include <stdbool.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  base64_decode_context

Defines

#define BASE64_LENGTH(inlen)   ((((inlen) + 2) / 3) * 4)
#define base64_decode(in, inlen, out, outlen)   base64_decode_ctx (NULL, in, inlen, out, outlen)
#define base64_decode_alloc(in, inlen, out, outlen)   base64_decode_alloc_ctx (NULL, in, inlen, out, outlen)

Functions

bool isbase64 (char ch)
void base64_encode (const char *restrict in, size_t inlen, char *restrict out, size_t outlen)
size_t base64_encode_alloc (const char *in, size_t inlen, char **out)
void base64_decode_ctx_init (struct base64_decode_context *ctx)
bool base64_decode_ctx (struct base64_decode_context *ctx, const char *restrict in, size_t inlen, char *restrict out, size_t *outlen)
bool base64_decode_alloc_ctx (struct base64_decode_context *ctx, const char *in, size_t inlen, char **out, size_t *outlen)

Class Documentation

struct base64_decode_context

Definition at line 32 of file base64.h.

Class Members
char buf
unsigned int i

Define Documentation

#define base64_decode (   in,
  inlen,
  out,
  outlen 
)    base64_decode_ctx (NULL, in, inlen, out, outlen)

Definition at line 55 of file base64.h.

#define base64_decode_alloc (   in,
  inlen,
  out,
  outlen 
)    base64_decode_alloc_ctx (NULL, in, inlen, out, outlen)

Definition at line 58 of file base64.h.

#define BASE64_LENGTH (   inlen)    ((((inlen) + 2) / 3) * 4)

Definition at line 30 of file base64.h.


Function Documentation

bool base64_decode_alloc_ctx ( struct base64_decode_context ctx,
const char *  in,
size_t  inlen,
char **  out,
size_t *  outlen 
)

Definition at line 550 of file base64.c.

{
  /* This may allocate a few bytes too many, depending on input,
     but it's not worth the extra CPU time to compute the exact size.
     The exact size is 3 * inlen / 4, minus 1 if the input ends
     with "=" and minus another 1 if the input ends with "==".
     Dividing before multiplying avoids the possibility of overflow.  */
  size_t needlen = 3 * (inlen / 4) + 2;

  *out = malloc (needlen);
  if (!*out)
    return true;

  if (!base64_decode_ctx (ctx, in, inlen, *out, &needlen))
    {
      free (*out);
      *out = NULL;
      return false;
    }

  if (outlen)
    *outlen = needlen;

  return true;
}

Here is the call graph for this function:

bool base64_decode_ctx ( struct base64_decode_context ctx,
const char *restrict  in,
size_t  inlen,
char *restrict  out,
size_t *  outlen 
)

Definition at line 459 of file base64.c.

{
  size_t outleft = *outlen;
  bool ignore_newlines = ctx != NULL;
  bool flush_ctx = false;
  unsigned int ctx_i = 0;

  if (ignore_newlines)
    {
      ctx_i = ctx->i;
      flush_ctx = inlen == 0;
    }


  while (true)
    {
      size_t outleft_save = outleft;
      if (ctx_i == 0 && !flush_ctx)
        {
          while (true)
            {
              /* Save a copy of outleft, in case we need to re-parse this
                 block of four bytes.  */
              outleft_save = outleft;
              if (!decode_4 (in, inlen, &out, &outleft))
                break;

              in += 4;
              inlen -= 4;
            }
        }

      if (inlen == 0 && !flush_ctx)
        break;

      /* Handle the common case of 72-byte wrapped lines.
         This also handles any other multiple-of-4-byte wrapping.  */
      if (inlen && *in == '\n' && ignore_newlines)
        {
          ++in;
          --inlen;
          continue;
        }

      /* Restore OUT and OUTLEFT.  */
      out -= outleft_save - outleft;
      outleft = outleft_save;

      {
        char const *in_end = in + inlen;
        char const *non_nl;

        if (ignore_newlines)
          non_nl = get_4 (ctx, &in, in_end, &inlen);
        else
          non_nl = in;  /* Might have nl in this case. */

        /* If the input is empty or consists solely of newlines (0 non-newlines),
           then we're done.  Likewise if there are fewer than 4 bytes when not
           flushing context and not treating newlines as garbage.  */
        if (inlen == 0 || (inlen < 4 && !flush_ctx && ignore_newlines))
          {
            inlen = 0;
            break;
          }
        if (!decode_4 (non_nl, inlen, &out, &outleft))
          break;

        inlen = in_end - in;
      }
    }

  *outlen -= outleft;

  return inlen == 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 307 of file base64.c.

{
  ctx->i = 0;
}
void base64_encode ( const char *restrict  in,
size_t  inlen,
char *restrict  out,
size_t  outlen 
)

Definition at line 69 of file base64.c.

{
  static const char b64str[64] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

  while (inlen && outlen)
    {
      *out++ = b64str[(to_uchar (in[0]) >> 2) & 0x3f];
      if (!--outlen)
        break;
      *out++ = b64str[((to_uchar (in[0]) << 4)
                       + (--inlen ? to_uchar (in[1]) >> 4 : 0))
                      & 0x3f];
      if (!--outlen)
        break;
      *out++ =
        (inlen
         ? b64str[((to_uchar (in[1]) << 2)
                   + (--inlen ? to_uchar (in[2]) >> 6 : 0))
                  & 0x3f]
         : '=');
      if (!--outlen)
        break;
      *out++ = inlen ? b64str[to_uchar (in[2]) & 0x3f] : '=';
      if (!--outlen)
        break;
      if (inlen)
        inlen--;
      if (inlen)
        in += 3;
    }

  if (outlen)
    *out = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

size_t base64_encode_alloc ( const char *  in,
size_t  inlen,
char **  out 
)

Definition at line 116 of file base64.c.

{
  size_t outlen = 1 + BASE64_LENGTH (inlen);

  /* Check for overflow in outlen computation.
   *
   * If there is no overflow, outlen >= inlen.
   *
   * If the operation (inlen + 2) overflows then it yields at most +1, so
   * outlen is 0.
   *
   * If the multiplication overflows, we lose at least half of the
   * correct value, so the result is < ((inlen + 2) / 3) * 2, which is
   * less than (inlen + 2) * 0.66667, which is less than inlen as soon as
   * (inlen > 4).
   */
  if (inlen > outlen)
    {
      *out = NULL;
      return 0;
    }

  *out = malloc (outlen);
  if (!*out)
    return outlen;

  base64_encode (in, inlen, *out, outlen);

  return outlen - 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool isbase64 ( char  ch)

Definition at line 300 of file base64.c.

{
  return uchar_in_range (to_uchar (ch)) && 0 <= b64[to_uchar (ch)];
}

Here is the call graph for this function:

Here is the caller graph for this function: