I want to base64 encode and decode a file and show it on the output screen.
My input file is as follows:
In other words
MAC address: "XY-XX-XX-XX-XX-XX" "X" can be anything hexadecimal. The hexadecimal "Y", written in binary format, is Y: "kmnp", where "p" is the least significant bit;
p=0 --> unicast;
p=1 --> multicast;
n=0 --> globally assigned MAC;
n=1 --> locally administered;
So, actually MAC can be changed to any combination in which p=0 and n=1;
"Y" can be 2, 6, A or E.
So the possible MAC addresses in Windows 7 for wireless adapters:
My code is:
#include <stdio.h>
#include <string.h>
#include <string>
#include <cmath>
#include <cassert>
#include <limits>
#include <stdexcept>
#include <cctype>
#include <iostream>
using std::cout;
using std::endl;
static const int CHARS= 57;
static const char b64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char reverse_table[128] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 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, 64, 64, 64, 64, 64,
64, 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, 64, 64, 64, 64, 64
};
std::string base64_encode(const ::std::string &bindata)
{
using ::std::string;
using ::std::numeric_limits;
if (bindata.size() > (numeric_limits<string::size_type>::max() / 4u) * 3u) {
throw ::std::length_error("Converting too large a string to base64.");
}
const ::std::size_t binlen = bindata.size();
string retval((((binlen + 2) / 3) * 4), '=');
::std::size_t outpos = 0;
int bits_collected = 0;
unsigned int accumulator = 0;
const string::const_iterator binend = bindata.end();
for (string::const_iterator i = bindata.begin(); i != binend; ++i) {
accumulator = (accumulator << 8) | (*i & 0xffu);
bits_collected += 8;
while (bits_collected >= 6) {
bits_collected -= 6;
retval[outpos++] = b64_table[(accumulator >> bits_collected) & 0x3fu];
}
}
if (bits_collected > 0) { assert(bits_collected < 6);
accumulator <<= 6 - bits_collected;
retval[outpos++] = b64_table[accumulator & 0x3fu];
}
assert(outpos >= (retval.size() - 2));
assert(outpos <= retval.size());
return retval;
}
std::string base64_decode(const ::std::string &ascdata)
{
using ::std::string;
string retval;
const string::const_iterator last = ascdata.end();
int bits_collected = 0;
unsigned int accumulator = 0;
for (string::const_iterator i = ascdata.begin(); i != last; ++i) {
const int c = *i;
if (::std::isspace(c) || c == '=') {
continue;
}
if ((c > 127) || (c < 0) || (reverse_table[c] > 63)) {
throw ::std::invalid_argument("This contains characters not legal in a base64 encoded string.");
}
accumulator = (accumulator << 6) | reverse_table[c];
bits_collected += 6;
if (bits_collected >= 8) {
bits_collected -= 8;
retval += (char)((accumulator >> bits_collected) & 0xffu);
}
}
return retval;
}
struct upload_status
{
int lines_read;
};
char (*read_file())[CHARS]
{
FILE* hFile=NULL;
char (*fileBuf)[CHARS] =NULL;
std::string fileName("Hello.txt");
size_t fileSize,len(0);
hFile = fopen(fileName.c_str(),"rb");
if (hFile == NULL)
throw "File Does not exist.";
fseek(hFile,0,SEEK_END);
fileSize = ftell(hFile);
fseek(hFile,0,SEEK_SET);
if(fileSize > 1*1024)
{}
int no_of_rows = fileSize/54 + 1;
fileBuf = new char[no_of_rows + 1][CHARS];
for (len=0 ; len < no_of_rows; ++len)
fread(fileBuf[len],sizeof(char),54,hFile);
strcpy(fileBuf[len],"");
return fileBuf;
}
int main()
{
char *data;
char (*FEdata)[CHARS] = NULL;
int len(1);
struct upload_status upload_ctx;
upload_ctx.lines_read = 0;
std::string str,Estr("");
FEdata = read_file();
while (len)
{
data = FEdata[upload_ctx.lines_read];
if(strcmp(data,""))
{
size_t len = strlen(data);
str = std::string(data);
Estr += base64_encode(str);
cout<<base64_encode(str);
upload_ctx.lines_read++;
continue;
}
len = 0;
}
cout<<"\n\n\n\n"<<base64_decode(Estr);
return 0;
}
This code is working well in mingw compiler, but in visual c++ its showing extra long encoding and decoding(I mean it's working as it should.)
Any suggestion ??