irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

base64.cpp
Go to the documentation of this file.
1 /* base64.cpp - subroutine to do base64 encoding and decoding. The code came from
2  * SiFang
3  */
4 
5 #include "rodsClient.h"
6 #include "base64.h"
7 #define OK 0
8 
9 static const char *codes =
10  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
11 
12 static const unsigned char mymap[256] = {
13  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
14  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
15  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
16  255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
17  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
18  255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
19  7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
20  19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
21  255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
22  37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
23  49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
24  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
25  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
26  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
27  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
28  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
29  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
30  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
31  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
32  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
33  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
34  255, 255, 255, 255
35 };
36 
45 int base64_encode( const unsigned char *in, unsigned long inlen,
46  unsigned char *out, unsigned long *outlen ) {
47  unsigned long i, len2, leven;
48  unsigned char *p;
49 
50  /* valid output size ? */
51  len2 = 4 * ( ( inlen + 2 ) / 3 );
52  if ( *outlen < len2 + 1 ) {
54  }
55  p = out;
56  leven = 3 * ( inlen / 3 );
57  for ( i = 0; i < leven; i += 3 ) {
58  *p++ = codes[( in[0] >> 2 ) & 0x3F];
59  *p++ = codes[( ( ( in[0] & 3 ) << 4 ) + ( in[1] >> 4 ) ) & 0x3F];
60  *p++ = codes[( ( ( in[1] & 0xf ) << 2 ) + ( in[2] >> 6 ) ) & 0x3F];
61  *p++ = codes[in[2] & 0x3F];
62  in += 3;
63  }
64  /* Pad it if necessary... */
65  if ( i < inlen ) {
66  unsigned a = in[0];
67  unsigned b = ( i + 1 < inlen ) ? in[1] : 0;
68 
69  *p++ = codes[( a >> 2 ) & 0x3F];
70  *p++ = codes[( ( ( a & 3 ) << 4 ) + ( b >> 4 ) ) & 0x3F];
71  *p++ = ( i + 1 < inlen ) ? codes[( ( ( b & 0xf ) << 2 ) ) & 0x3F] : '=';
72  *p++ = '=';
73  }
74 
75  /* append a NULL byte */
76  *p = '\0';
77 
78  /* return ok */
79  *outlen = p - out;
80  return OK;
81 }
82 
91 int base64_decode( const unsigned char *in, unsigned long inlen,
92  unsigned char *out, unsigned long *outlen ) {
93  unsigned long t, x, y, z;
94  unsigned char c;
95  int g;
96 
97  g = 3;
98  for ( x = y = z = t = 0; x < inlen; x++ ) {
99  c = mymap[in[x] & 0xFF];
100  if ( c == 255 ) {
101  continue;
102  }
103  /* the final = symbols are read and used to trim the remaining bytes */
104  if ( c == 254 ) {
105  c = 0;
106  /* prevent g < 0 which would potentially allow an overflow later */
107  if ( --g < 0 ) {
108  return BASE64_INVALID_PACKET;
109  }
110  }
111  else if ( g != 3 ) {
112  /* we only allow = to be at the end */
113  return BASE64_INVALID_PACKET;
114  }
115 
116  t = ( t << 6 ) | c;
117 
118  if ( ++y == 4 ) {
119  if ( z + g > *outlen ) {
120  return BASE64_BUFFER_OVERFLOW;
121  }
122  out[z++] = ( unsigned char )( ( t >> 16 ) & 255 );
123  if ( g > 1 ) {
124  out[z++] = ( unsigned char )( ( t >> 8 ) & 255 );
125  }
126  if ( g > 2 ) {
127  out[z++] = ( unsigned char )( t & 255 );
128  }
129  y = t = 0;
130  }
131  }
132  if ( y != 0 ) {
133  return BASE64_INVALID_PACKET;
134  }
135  *outlen = z;
136  return OK;
137 }
138 
base64_encode
int base64_encode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen)
Definition: base64.cpp:45
codes
static const char * codes
Definition: base64.cpp:9
pid_age.p
p
Definition: pid_age.py:13
BASE64_BUFFER_OVERFLOW
@ BASE64_BUFFER_OVERFLOW
Definition: rodsErrorTable.h:255
OK
#define OK
Definition: base64.cpp:7
mymap
static const unsigned char mymap[256]
Definition: base64.cpp:12
base64.h
rodsClient.h
irods.six.b
def b(s)
Definition: six.py:606
base64_decode
int base64_decode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen)
Definition: base64.cpp:91
manual_cleanup.out
out
Definition: manual_cleanup.py:29
BASE64_INVALID_PACKET
@ BASE64_INVALID_PACKET
Definition: rodsErrorTable.h:256