CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Classes | Macros | Functions
base64.h File Reference
#include <stddef.h>
#include <stdbool.h>

Go to the source code of this file.

Classes

struct  base64_decode_context
 

Macros

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

Macro Definition 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::auth::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.

554 {
555  /* This may allocate a few bytes too many, depending on input,
556  but it's not worth the extra CPU time to compute the exact size.
557  The exact size is 3 * (inlen + (ctx ? ctx->i : 0)) / 4, minus 1 if the
558  input ends with "=" and minus another 1 if the input ends with "==".
559  Dividing before multiplying avoids the possibility of overflow. */
560  size_t needlen = 3 * (inlen / 4) + 3;
561 
562  *out = static_cast<char*>(malloc (needlen));
563  if (!*out)
564  return true;
565 
566  if (!base64_decode_ctx (ctx, in, inlen, *out, &needlen))
567  {
568  free (*out);
569  *out = NULL;
570  return false;
571  }
572 
573  if (outlen)
574  *outlen = needlen;
575 
576  return true;
577 }
#define NULL
Definition: scimark2.h:8
bool base64_decode_ctx(struct base64_decode_context *ctx, const char *in, size_t inlen, char *out, size_t *outlen)
Definition: base64.cc:460
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().

463 {
464  size_t outleft = *outlen;
465  bool ignore_newlines = ctx != NULL;
466  bool flush_ctx = false;
467  unsigned int ctx_i = 0;
468 
469  if (ignore_newlines)
470  {
471  ctx_i = ctx->i;
472  flush_ctx = inlen == 0;
473  }
474 
475 
476  while (true)
477  {
478  size_t outleft_save = outleft;
479  if (ctx_i == 0 && !flush_ctx)
480  {
481  while (true)
482  {
483  /* Save a copy of outleft, in case we need to re-parse this
484  block of four bytes. */
485  outleft_save = outleft;
486  if (!decode_4 (in, inlen, &out, &outleft))
487  break;
488 
489  in += 4;
490  inlen -= 4;
491  }
492  }
493 
494  if (inlen == 0 && !flush_ctx)
495  break;
496 
497  /* Handle the common case of 72-byte wrapped lines.
498  This also handles any other multiple-of-4-byte wrapping. */
499  if (inlen && *in == '\n' && ignore_newlines)
500  {
501  ++in;
502  --inlen;
503  continue;
504  }
505 
506  /* Restore OUT and OUTLEFT. */
507  out -= outleft_save - outleft;
508  outleft = outleft_save;
509 
510  {
511  char const *in_end = in + inlen;
512  char const *non_nl;
513 
514  if (ignore_newlines)
515  non_nl = get_4 (ctx, &in, in_end, &inlen);
516  else
517  non_nl = in; /* Might have nl in this case. */
518 
519  /* If the input is empty or consists solely of newlines (0 non-newlines),
520  then we're done. Likewise if there are fewer than 4 bytes when not
521  flushing context and not treating newlines as garbage. */
522  if (inlen == 0 || (inlen < 4 && !flush_ctx && ignore_newlines))
523  {
524  inlen = 0;
525  break;
526  }
527  if (!decode_4 (non_nl, inlen, &out, &outleft))
528  break;
529 
530  inlen = in_end - in;
531  }
532  }
533 
534  *outlen -= outleft;
535 
536  return inlen == 0;
537 }
#define NULL
Definition: scimark2.h:8
unsigned int i
Definition: base64.h:36
static bool decode_4(char const *in, size_t inlen, char **outp, size_t *outleft)
Definition: base64.cc:375
static char * get_4(struct base64_decode_context *ctx, char const **in, char const *in_end, size_t *n_non_newline)
Definition: base64.cc:321
void base64_decode_ctx_init ( struct base64_decode_context ctx)

Definition at line 308 of file base64.cc.

References base64_decode_context::i.

309 {
310  ctx->i = 0;
311 }
unsigned int i
Definition: base64.h:36
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().

72 {
73  static const char* b64str =
74  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
75 
76  while (inlen && outlen)
77  {
78  *out++ = b64str[(to_uchar (in[0]) >> 2) & 0x3f];
79  if (!--outlen)
80  break;
81  *out++ = b64str[((to_uchar (in[0]) << 4)
82  + (--inlen ? to_uchar (in[1]) >> 4 : 0))
83  & 0x3f];
84  if (!--outlen)
85  break;
86  *out++ =
87  (inlen
88  ? b64str[((to_uchar (in[1]) << 2)
89  + (--inlen ? to_uchar (in[2]) >> 6 : 0))
90  & 0x3f]
91  : '=');
92  if (!--outlen)
93  break;
94  *out++ = inlen ? b64str[to_uchar (in[2]) & 0x3f] : '=';
95  if (!--outlen)
96  break;
97  if (inlen)
98  inlen--;
99  if (inlen)
100  in += 3;
101  }
102 
103  if (outlen)
104  *out = '\0';
105 }
static const char * b64str
Definition: DecodingKey.cc:17
static unsigned char to_uchar(char ch)
Definition: base64.cc:60
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::auth::Cipher::b64encrypt().

118 {
119  size_t outlen = 1 + BASE64_LENGTH (inlen);
120 
121  /* Check for overflow in outlen computation.
122  *
123  * If there is no overflow, outlen >= inlen.
124  *
125  * If the operation (inlen + 2) overflows then it yields at most +1, so
126  * outlen is 0.
127  *
128  * If the multiplication overflows, we lose at least half of the
129  * correct value, so the result is < ((inlen + 2) / 3) * 2, which is
130  * less than (inlen + 2) * 0.66667, which is less than inlen as soon as
131  * (inlen > 4).
132  */
133  if (inlen > outlen)
134  {
135  *out = NULL;
136  return 0;
137  }
138 
139  *out = static_cast<char*>(malloc (outlen));
140  if (!*out)
141  return outlen;
142 
143  base64_encode (in, inlen, *out, outlen);
144 
145  return outlen - 1;
146 }
#define NULL
Definition: scimark2.h:8
std::string base64_encode(unsigned char const *, unsigned int len)
Definition: PixelBase64.cc:41
#define BASE64_LENGTH(inlen)
Definition: base64.h:32
bool isbase64 ( char  ch)

Definition at line 301 of file base64.cc.

References b64, to_uchar(), and uchar_in_range.

Referenced by decode_4().

302 {
303  return uchar_in_range (to_uchar (ch)) && 0 <= b64[to_uchar (ch)];
304 }
static const signed char b64[0x100]
Definition: base64.cc:224
static unsigned char to_uchar(char ch)
Definition: base64.cc:60
#define uchar_in_range(c)
Definition: base64.cc:294