Reputation: 10936
I wrote this little converter to convert Base2 file data to Base64. The resulting output is incorrect and I can't figure out why. The problem is compounded if an image file is passed in as input: random NULL chars (0x00) are inserted into the output which should never happen. The output should only contain a subset of the Base64Table (see code).
Input:
Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.
Expected Output:
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=
(from Wikipedia page on Base64)
Actual Output:
YW7MaXPMZGnMdGnMZ3XMc2jMZCzMbm/MIG/MbHnMYnnMaGnMIHLMYXPMbizMYnXMIGLMIHTMaXPMc2nMZ3XMYXLMcGHMc2nMbiDMcm/MIG/MaGXMIGHMaW3MbHPMIHfMaWPMIGnMIGHMbHXMdCDMZiDMaGXMbWnMZCzMdGjMdCDMeSDMIHDMcnPMdmXMYW7MZSDMZiDMZWzMZ2jMIGnMIHTMZSDMb27MaW7MZWTMYW7MIGnMZGXMYXTMZ2HMbGXMZ2XMZXLMdGnMbiDMZiDMbm/MbGXMZ2XMIGXMY2XMZHPMdGjMIHPMb3LMIHbMaGXMZW7MZSDMZiDMbnnMY2HMbmHMIHDMZWHMdXLMLnLM
Code:
#include <iostream>
#include <fstream>
char Base64Table[64] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', //0-25
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', //26-51
'0','1','2','3','4','5','6','7','8','9', //52-61
'+','/'}; //62-63
char PADDING_CHAR = '=';
char GetFirstSymbol(char* buffer);
char GetSecondSymbol(char* buffer);
char GetThirdSymbol(char* buffer);
char GetFourthSymbol(char* buffer);
int main(int argc, char** argv) {
if(argc != 2) {
std::cout << "Incorrect number of arguments." << std::endl;
std::cout << "Press Enter to quit.";
while(!std::cin.get());
return 0;
}
std::cout << "Converting " << argv[1] << std::endl;
std::ifstream input;
input.open(argv[1], std::ios_base::binary);
std::ofstream output;
output.open("data.base64", std::ios_base::binary);
char count[1] = {'\0'};
unsigned long file_size = 0;
char buffer[3] = {'\0', '\0', '\0'};
while(input.fail() == false) {
input.read(reinterpret_cast<char*>(count), sizeof(count));
++file_size;
}
input.clear();
input.seekg(0);
while(input.fail() == false) {
input.read(reinterpret_cast<char*>(buffer), sizeof(buffer));
char firstsymbol = GetFirstSymbol(buffer);
char secondsymbol = GetSecondSymbol(buffer);
char thirdsymbol = GetThirdSymbol(buffer);
char fourthsymbol = GetFourthSymbol(buffer);
output.write(reinterpret_cast<char*>(&firstsymbol), sizeof(firstsymbol));
output.write(reinterpret_cast<char*>(&secondsymbol), sizeof(secondsymbol));
if(file_size % 3 == 2) {
output.write(reinterpret_cast<char*>(&PADDING_CHAR), sizeof(PADDING_CHAR));
continue;
} else if(file_size % 3 == 1) {
output.write(reinterpret_cast<char*>(&PADDING_CHAR), sizeof(PADDING_CHAR));
output.write(reinterpret_cast<char*>(&PADDING_CHAR), sizeof(PADDING_CHAR));
continue;
}
output.write(reinterpret_cast<char*>(&thirdsymbol), sizeof(thirdsymbol));
output.write(reinterpret_cast<char*>(&fourthsymbol), sizeof(fourthsymbol));
}
input.clear();
input.close();
output.clear();
output.close();
return 0;
}
//Gets the 6 most significant digits of the first byte.
char GetFirstSymbol(char* buffer) {
int index = (buffer[1] >> 2);
return Base64Table[index];
}
//Gets the 2 least significant digits from previous (first) byte and 4 most significant from the second byte.
char GetSecondSymbol(char* buffer) {
int index = (((buffer[1] & 0x03) << 4) | ((buffer[2] & 0xF0) >> 4));
return Base64Table[index];
}
//Gets the 4 least significant digits from previous (second) byte and 2 least significant from the third byte.
char GetThirdSymbol(char* buffer) {
int index = (((buffer[2] & 0x0F) << 2) | ((buffer[3] & 0xC0) >> 6));
return Base64Table[index];
}
//Gets the 6 least significant digits from the third byte.
char GetFourthSymbol(char* buffer) {
int index = (buffer[3] & 0x3F);
return Base64Table[index];
}
Upvotes: 1
Views: 210
Reputation: 6914
C/C++ pointers and arrays are very powerful beasts but they can hurt you too! so you should take care when working with them. you define buffer
as char[3]
but access it from index [1] to index [3] and this is your error since indexes of C/C++ arrays are 0 based and according to this you have buffer [0][1][2][memory of other variables]
you left buffer[0]
unchanged and instead of that you are writing to the memory of another variable and this is the reason of random errors and invalid values!!!
Upvotes: 2