18static char alphabet[67] =
19 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n";
24 if (ch >=
'A' && ch <=
'Z')
26 if (ch >=
'a' && ch <=
'z')
28 if (ch >=
'0' && ch <=
'9')
36 if (ch ==
'\n' || ch ==
' ' || ch ==
'\r' || ch ==
'\t' ||
37 ch ==
'\f' || ch ==
'\v')
62 while (in.
used() != 0)
64 unsigned char next = in.
getch();
65 bits = (bits << 8) | next;
69 out.
putch(alphabet[bits >> 2]);
74 out.
putch(alphabet[bits >> 4]);
79 out.
putch(alphabet[bits >> 6]);
80 out.
putch(alphabet[bits & 0x3f]);
87 if (
flush && state != ATBIT0)
99 out.
putch(alphabet[bits << 4]);
104 out.
putch(alphabet[bits << 2]);
134 while (in.
used() != 0)
136 unsigned char next = in.
getch();
137 int symbol =
lookup(next);
141 seterror(
"invalid character #%s in base64 input", next);
155 bits = (bits << 6) | symbol;
162 out.
putch(bits >> 4);
167 out.
putch(bits >> 2);
179 "after base64 padding", next);
186 if (
flush && (state == ATBIT2 || state == ATBIT4 || state == ATBIT6))
WvBase64Decoder()
Creates a base 64 decoder.
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
virtual bool _reset()
Template method implementation of reset().
virtual bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
virtual bool _reset()
Template method implementation of reset().
WvBase64Encoder()
Creates a base 64 encoder.
virtual bool _finish(WvBuf &out)
Template method implementation of finish().
size_t used() const
Returns the number of elements in the buffer currently available for reading.
Specialization of WvBufBase for unsigned char type buffers intended for use with raw memory buffers.
int getch()
Returns a single character from the buffer as an int.
void putch(int ch)
Puts a single character into the buffer as an int.
void seterror(WvStringParm message)
Sets an error condition, then setnotok().
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
void setfinished()
Sets 'finished' to true explicitly.
int lookup(const char *str, const char *const *table, bool case_sensitive=false)
Finds a string in an array and returns its index.