1
0
This repository has been archived on 2025-03-06. You can view files and clone it, but cannot push or open issues or pull requests.
Jip J. Dekker f2a1c4e389 Squashed 'software/mza/' content from commit f970a59b17
git-subtree-dir: software/mza
git-subtree-split: f970a59b177c13ca3dd8aaef8cc6681d83b7e813
2021-07-11 16:34:30 +10:00

80 lines
1.6 KiB
C++

// :mode=c++:
/*
encode.h - c++ wrapper for a base64 encoding algorithm
This is part of the libb64 project, and has been placed in the public domain.
For details, see http://sourceforge.net/projects/libb64
*/
#ifndef BASE64_ENCODE_H
#define BASE64_ENCODE_H
#define BUFFERSIZE 4096
#include <iostream>
namespace base64
{
extern "C"
{
#include <minizinc/thirdparty/b64/cencode.h>
}
struct encoder
{
base64_encodestate _state;
int _buffersize;
encoder(int buffersize_in = BUFFERSIZE)
: _buffersize(buffersize_in)
{}
int encode(char value_in)
{
return base64_encode_value(value_in);
}
int encode(const char* code_in, const int length_in, char* plaintext_out)
{
return base64_encode_block(code_in, length_in, plaintext_out, &_state);
}
int encode_end(char* plaintext_out)
{
return base64_encode_blockend(plaintext_out, &_state);
}
void encode(std::istream& istream_in, std::ostream& ostream_in)
{
base64_init_encodestate(&_state);
//
const int N = _buffersize;
char* plaintext = new char[N];
char* code = new char[2*N];
int plainlength;
int codelength;
do
{
istream_in.read(plaintext, N);
plainlength = istream_in.gcount();
//
codelength = encode(plaintext, plainlength, code);
ostream_in.write(code, codelength);
}
while (istream_in.good() && plainlength > 0);
codelength = encode_end(code);
ostream_in.write(code, codelength);
//
base64_init_encodestate(&_state);
delete [] code;
delete [] plaintext;
}
};
} // namespace base64
#endif // BASE64_ENCODE_H