CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Macros | Functions | Variables
base64.cc File Reference
#include "base64.h"
#include <stdlib.h>
#include <limits.h>
#include <string.h>

Go to the source code of this file.

Macros

#define B64(_)
 
#define return_false
 
#define uchar_in_range(c)   ((c) <= 255)
 

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)
 
static bool decode_4 (char const *in, size_t inlen, char **outp, size_t *outleft)
 
static char * get_4 (struct base64_decode_context *ctx, char const **in, char const *in_end, size_t *n_non_newline)
 
bool isbase64 (char ch)
 
static unsigned char to_uchar (char ch)
 

Variables

static const signed char b64 [0x100]
 

Macro Definition Documentation

#define B64 (   _)

Definition at line 157 of file base64.cc.

#define return_false
Value:
do \
{ \
*outp = out; \
return false; \
} \
while (false)
tuple out
Definition: dbtoconf.py:99

Definition at line 360 of file base64.cc.

Referenced by decode_4().

#define uchar_in_range (   c)    ((c) <= 255)

Definition at line 294 of file base64.cc.

Referenced by isbase64().

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
tuple out
Definition: dbtoconf.py:99
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
tuple out
Definition: dbtoconf.py:99
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
tuple out
Definition: dbtoconf.py:99
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().

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
tuple out
Definition: dbtoconf.py:99
static bool decode_4 ( char const *  in,
size_t  inlen,
char **  outp,
size_t *  outleft 
)
inlinestatic

Definition at line 375 of file base64.cc.

References b64, isbase64(), dbtoconf::out, return_false, and to_uchar().

Referenced by base64_decode_ctx().

377 {
378  char *out = *outp;
379  if (inlen < 2)
380  return false;
381 
382  if (!isbase64 (in[0]) || !isbase64 (in[1]))
383  return false;
384 
385  if (*outleft)
386  {
387  *out++ = ((b64[to_uchar (in[0])] << 2)
388  | (b64[to_uchar (in[1])] >> 4));
389  --*outleft;
390  }
391 
392  if (inlen == 2)
393  return_false;
394 
395  if (in[2] == '=')
396  {
397  if (inlen != 4)
398  return_false;
399 
400  if (in[3] != '=')
401  return_false;
402  }
403  else
404  {
405  if (!isbase64 (in[2]))
406  return_false;
407 
408  if (*outleft)
409  {
410  *out++ = (((b64[to_uchar (in[1])] << 4) & 0xf0)
411  | (b64[to_uchar (in[2])] >> 2));
412  --*outleft;
413  }
414 
415  if (inlen == 3)
416  return_false;
417 
418  if (in[3] == '=')
419  {
420  if (inlen != 4)
421  return_false;
422  }
423  else
424  {
425  if (!isbase64 (in[3]))
426  return_false;
427 
428  if (*outleft)
429  {
430  *out++ = (((b64[to_uchar (in[2])] << 6) & 0xc0)
431  | b64[to_uchar (in[3])]);
432  --*outleft;
433  }
434  }
435  }
436 
437  *outp = out;
438  return true;
439 }
static const signed char b64[0x100]
Definition: base64.cc:224
bool isbase64(char ch)
Definition: base64.cc:301
static unsigned char to_uchar(char ch)
Definition: base64.cc:60
tuple out
Definition: dbtoconf.py:99
#define return_false
Definition: base64.cc:360
static char* get_4 ( struct base64_decode_context ctx,
char const **  in,
char const *  in_end,
size_t *  n_non_newline 
)
inlinestatic

Definition at line 321 of file base64.cc.

References base64_decode_context::buf, trackerHits::c, base64_decode_context::i, recoMuon::in, NULL, AlCaHLTBitMon_ParallelJobs::p, and lumiQTWidget::t.

Referenced by base64_decode_ctx().

324 {
325  if (ctx->i == 4)
326  ctx->i = 0;
327 
328  if (ctx->i == 0)
329  {
330  char const *t = *in;
331  if (4 <= in_end - *in && memchr (t, '\n', 4) == NULL)
332  {
333  /* This is the common case: no newline. */
334  *in += 4;
335  *n_non_newline = 4;
336  return (char *) t;
337  }
338  }
339 
340  {
341  /* Copy non-newline bytes into BUF. */
342  char const *p = *in;
343  while (p < in_end)
344  {
345  char c = *p++;
346  if (c != '\n')
347  {
348  ctx->buf[ctx->i++] = c;
349  if (ctx->i == 4)
350  break;
351  }
352  }
353 
354  *in = p;
355  *n_non_newline = ctx->i;
356  return ctx->buf;
357  }
358 }
#define NULL
Definition: scimark2.h:8
unsigned int i
Definition: base64.h:36
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
static unsigned char to_uchar ( char  ch)
inlinestatic

Definition at line 60 of file base64.cc.

Referenced by base64_encode(), decode_4(), and isbase64().

61 {
62  return ch;
63 }

Variable Documentation

const signed char b64[0x100]
static

Definition at line 224 of file base64.cc.

Referenced by decode_4(), and isbase64().