CMS 3D CMS Logo

Classes | Defines | Functions

/data/refman/pasoursint/CMSSW_5_3_10/src/CondCore/DBCommon/src/base64.h File Reference

#include <stddef.h>
#include <stdbool.h>

Go to the source code of this file.

Classes

struct  base64_decode_context

Defines

#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)
#define BASE64_LENGTH(inlen)   ((((inlen) + 2) / 3) * 4)

Functions

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

Define Documentation

#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)

Definition at line 60 of file base64.h.

Referenced by cond::Cipher::b64decrypt().

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

Definition at line 32 of file base64.h.

Referenced by base64_encode_alloc().


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 551 of file base64.cc.

References base64_decode_ctx(), and NULL.

{
  /* 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 + (ctx ? ctx->i : 0)) / 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) + 3;

  *out = static_cast<char*>(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;
}
bool base64_decode_ctx ( struct base64_decode_context ctx,
const char *  in,
size_t  inlen,
char *  out,
size_t *  outlen 
)

Definition at line 460 of file base64.cc.

References decode_4(), get_4(), base64_decode_context::i, recoMuon::in, and NULL.

Referenced by base64_decode_alloc_ctx().

{
  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;
}
void base64_decode_ctx_init ( struct base64_decode_context ctx)

Definition at line 308 of file base64.cc.

References base64_decode_context::i.

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

Definition at line 70 of file base64.cc.

References b64str, and to_uchar().

{
  static const char* b64str =
    "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';
}
size_t base64_encode_alloc ( const char *  in,
size_t  inlen,
char **  out 
)

Definition at line 117 of file base64.cc.

References base64_encode(), BASE64_LENGTH, and NULL.

Referenced by cond::Cipher::b64encrypt().

{
  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 = static_cast<char*>(malloc (outlen));
  if (!*out)
    return outlen;

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

  return outlen - 1;
}
bool isbase64 ( char  ch)

Definition at line 301 of file base64.cc.

References b64, to_uchar(), and uchar_in_range.

Referenced by decode_4().

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