class::calc::Base64

class calc::Base64
  : public api::CalcInfoObject

This class includes methods to encode and decode data using the base64 algorithm. Base64 is useful for representing binary data using text characters. This can be useful when transmitting data over certain serial links that do not support binary transfers.

To encode data in Base 64 format use this code:

#include <sapi/calc.hpp>

u8 raw_data[64]; //raw binary data that needs to be encoded
char * encoded_data;
int encoded_size;

encoded_size = Base64::calc_encoded_size(64);
encoded_data = malloc(encoded_size);
Base64::encode(encoded_data, raw_data, 64);

You can then decode the data using this code snippet:

#include <sapi/calc.hpp>

char encoded_data[64]; //Base64 encoded data that needs to be decoded
u8 * raw_data;
int decoded_size;

decoded_size = Base64::calc_decoded_size(64);
raw_data = malloc(decoded_size);
Base64::decode(raw_data, encoded_data, 64);

Members

static var::Data decode(

const arg::ImplicitBase64EncodedString input
)

static int decode(

const arg::SourceFile input,
const arg::DestinationFile output,
const arg::Size size
)

static var::String encode(

const arg::ImplicitSourceData input
)

static int encode(

const arg::SourceFile source,
const arg::DestinationFile destination,
const arg::Size size
)

Details

static var::Data decode(

const arg::ImplicitBase64EncodedString input
)

Decodes base64 encoded data.

Returns

Zero

#include <sapi/calc.hpp>
#include <sapi/var.hpp>

String encoded_string;
//assign base64 encoded string
Data raw_data;

raw_data = Base64::decode(encoded_string);

static int decode(

const arg::SourceFile input,
const arg::DestinationFile output,
const arg::Size size
)

Reads base64 encoded data from input and writes raw, decoded data to output.

Returns

Number of decoded bytes written to output

The method reads size bytes (or to EOF if size is zero) from input start at the current location. The output string is written to output at the current location.


static var::String encode(

const arg::ImplicitSourceData input
)

Encodes data to the base64 format.

Returns

Number of bytes in the encoded string

#include <sapi/calc.hpp>
#include <sapi/var.hpp>

Data raw_data(64); //raw binary data that needs to be encoded

String result = encode(raw_data);
if( result.is_empty() ){
 //failed to encode
}

static int encode(

const arg::SourceFile source,
const arg::DestinationFile destination,
const arg::Size size
)

Reads binary data from input and writes a Base64 encoded string to output.

Returns

Number of encoded bytes written to output

The method reads size bytes (or to EOF if size is zero) from input start at the current location. The output string is written to output at the current location.

#include <sapi/calc.hpp>
#include <sapi/fs.hpp>

File source;
File destination;

source.open(
  arg::FilePath("/home/raw_data.dat"),
  OpenFile::read_only()
  );

destination.create(
  arg::DestinationFilePath("/home/base64_encoded.txt"),
  arg::IsOverwrite(true)
);

Base64::encode(
  arg::SourceFile(source),
  arg::DestinationFile(destination),
  );

X

Thanks for Coming!

Subscribe to news and updates