/usr/include/mimetic/codec/base64.h is in libmimetic-dev 0.9.7-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | /***************************************************************************
copyright : (C) 2002-2008 by Stefano Barbato
email : stefano@codesink.org
$Id: base64.h,v 1.15 2008-10-07 11:06:26 tat Exp $
***************************************************************************/
#ifndef _MIMETIC_CODEC_BASE64_H_
#define _MIMETIC_CODEC_BASE64_H_
#include <mimetic/circular_buffer.h>
#include <mimetic/codec/codec_base.h>
#include <mimetic/codec/codec_chain.h>
namespace mimetic
{
class Base64
{
enum { LF = 0xA, CR = 0xD, NL = '\n' };
enum { default_maxlen = 76 };
enum { eq_sign = 100 };
static const char sEncTable[];
static const char sDecTable[];
static const int sDecTableSz;
public:
class Encoder; class Decoder;
typedef Encoder encoder_type;
typedef Decoder decoder_type;
/// Base64 encoder
/*!
\sa encode decode
*/
class Encoder: public buffered_codec, public chainable_codec<Encoder>
{
enum { pad_idx = 64 };
char_type m_ch[3];
int m_cidx;
int m_pos, m_maxlen;
template<typename OutIt>
inline void writeBuf(OutIt& out)
{
int pad_count = 3 - m_cidx;
m_cidx = 0;
int idx[4];
idx[0] = m_ch[0] >> 2;
switch(pad_count)
{
case 0:
idx[1] = (((m_ch[0] & 3) << 4) | (m_ch[1] >> 4));
idx[2] = ((m_ch[1] & 0xf) << 2) | (m_ch[2] >> 6);
idx[3] = m_ch[2] & 0x3f;
break;
case 1:
idx[1] = (((m_ch[0] & 3) << 4) | (m_ch[1] >> 4));
idx[2] = (m_ch[1] & 0xf) << 2 ;
idx[3] = pad_idx;
break;
case 2:
idx[1] = (m_ch[0] & 3) << 4;
idx[2] = idx[3] = pad_idx;
break;
}
for(int i = 0; i < 4; ++i)
{
*out = sEncTable[ idx[i] ]; ++out;
if(m_maxlen && ++m_pos > m_maxlen)
{
*out = NL; ++out;
m_pos = 1;
}
}
}
public:
/*! return the multiplier of the required (max) size of the output buffer
* when encoding */
double codeSizeMultiplier() const
{
return 1.5;
}
/*! Constructor, maxlen is the maximum length of every encoded line */
Encoder(int maxlen = default_maxlen)
: m_cidx(0), m_pos(1), m_maxlen(maxlen)
{
}
/*! Returns the name of the codec ("Base64") */
const char* name() const { return "Base64"; }
/*!
Encodes [\p bit,\p eit) and write any encoded char to \p out.
*/
template<typename InIt, typename OutIt>
void process(InIt bit, InIt eit, OutIt out)
{
for(; bit != eit; ++bit)
{
m_ch[m_cidx++] = (char_type)*bit;
if(m_cidx < 3)
continue;
writeBuf(out);
}
if(m_cidx > 0)
writeBuf(out);
}
/*!
Encodes \p c and write any encoded output char to \p out.
\warning You must call flush() when all chars have been
processed by the encode funcion.
\n
\code
while( (c = getchar()) != EOF )
b64.encode(c, out);
b64.flush();
\endcode
\n
\sa flush()
*/
template<typename OutIt>
void process(char_type c, OutIt& out)
{
m_ch[m_cidx++] = c;
if(m_cidx < 3)
return;
writeBuf(out);
}
/*!
Write to \p out any buffered encoded char.
*/
template<typename OutIt>
void flush(OutIt& out)
{
if(m_cidx > 0)
writeBuf(out);
}
};
/// Base64 decoder
/*!
\sa encode decode
*/
class Decoder: public buffered_codec, public chainable_codec<Decoder>
{
int m_cidx;
char_type m_ch[4];
template<typename OutIt>
inline void writeBuf(OutIt& out)
{
if(m_cidx < 4)
{ // malformed, missing chars will be cosidered pad
switch(m_cidx)
{
case 0:
case 1:
return; // ignore;
case 2:
m_ch[2] = m_ch[3] = eq_sign;
break;
case 3:
m_ch[3] = eq_sign;
break;
}
}
m_cidx = 0;
*out = (m_ch[0] << 2 | ((m_ch[1] >> 4) & 0x3) ); ++out;
if(m_ch[2] == eq_sign) return;
*out = (m_ch[1] << 4 | ((m_ch[2] >> 2) & 0xF) ); ++out;
if(m_ch[3] == eq_sign) return;
*out = (m_ch[2] << 6 | m_ch[3]); ++out;
}
public:
/*! Constructor */
Decoder()
: m_cidx(0)
{
}
/*! Returns the name of the codec ("Base64") */
const char* name() const { return "Base64"; }
/*!
Decodes [\p bit,\p eit) and write any decoded char to \p out.
*/
template<typename InIt, typename OutIt>
inline void process(InIt bit, InIt eit, OutIt out)
{
char_type c;
for(; bit != eit; ++bit)
{
c = *bit;
if(c > sDecTableSz || sDecTable[c] == -1)
continue; // malformed or newline
m_ch[m_cidx++] = sDecTable[c];
if(m_cidx < 4)
continue;
writeBuf(out);
}
if(m_cidx > 0)
writeBuf(out);
}
/*!
Decodes \p c and write any decoded output char to \p out.
\warning You must call flush() when all chars have been
processed by the decode funcion.
\n
\code
while( (c = getchar()) != EOF )
b64.decode(c, out);
b64.flush();
\endcode
\n
\sa flush()
*/
template<typename OutIt>
void process(char_type c, OutIt& out)
{
if(c > sDecTableSz || sDecTable[c] == -1)
return; // malformed or newline
m_ch[m_cidx++] = sDecTable[c];
if(m_cidx < 4)
return;
writeBuf(out);
}
/*!
Write to \p out any buffered decoded char.
*/
template<typename OutIt>
void flush(OutIt& out)
{
if(m_cidx > 0)
writeBuf(out);
}
};
}; // Base64
}
#endif
|