diff options
| author | Logan <[email protected]> | 2024-05-18 20:14:44 -0700 |
|---|---|---|
| committer | Logan <[email protected]> | 2024-05-18 20:14:44 -0700 |
| commit | e4444d2aefcec66998f4be0e88781c4e3c9bcebd (patch) | |
| tree | 98fb658f4c385e4402bb2cc73e160bf6377972db /CST 126 | |
| parent | Added Menu with Both options. Encoding a file seems to have a padding issue. (diff) | |
| download | homework-1-bobjoe64-e4444d2aefcec66998f4be0e88781c4e3c9bcebd.tar.xz homework-1-bobjoe64-e4444d2aefcec66998f4be0e88781c4e3c9bcebd.zip | |
Fixed padding issue in encoding function
Diffstat (limited to 'CST 126')
| -rw-r--r-- | CST 126/Homework 2/encode.txt | 2 | ||||
| -rw-r--r-- | CST 126/Homework 2/main.cpp | 102 |
2 files changed, 61 insertions, 43 deletions
diff --git a/CST 126/Homework 2/encode.txt b/CST 126/Homework 2/encode.txt index b6fc4c6..32f95c0 100644 --- a/CST 126/Homework 2/encode.txt +++ b/CST 126/Homework 2/encode.txt @@ -1 +1 @@ -hello
\ No newline at end of file +hi
\ No newline at end of file diff --git a/CST 126/Homework 2/main.cpp b/CST 126/Homework 2/main.cpp index b68c1e2..92a5a59 100644 --- a/CST 126/Homework 2/main.cpp +++ b/CST 126/Homework 2/main.cpp @@ -7,42 +7,50 @@ #include <fstream> #include <sstream> -std::string encodeFileToBase64(const std::string& filename); -std::string decodeBase64(const std::string& encoded); -bool decodeBase64ToFile(const std::string& base64Data, const std::string& filename); +using namespace std; + +string encodeFileToBase64(const string& filename); +string decodeBase64(const string& encoded); +bool decodeBase64ToFile(const string& base64Data, const string& filename); int main() { int choice = 0; do { - std::cout << "Welcome to my program! Here are your options:" << std::endl; - std::cout << "0. Quit" << std::endl; - std::cout << "1. Encode File to Base64" << std::endl; - std::cout << "2. Decode Base64 to File" << std::endl; - std::cin >> choice; + cout << "Welcome to my program! Here are your options:" << endl; + cout << "0. Quit" << endl; + cout << "1. Encode File to Base64" << endl; + cout << "2. Decode Base64 to File" << endl; + cin >> choice; switch (choice) { case 1: { - std::string filename = "encode.txt"; - std::string base64Data = encodeFileToBase64(filename); - std::cout << "Base64 Encoded Data:\n" << base64Data << std::endl; + string filename; + cout << "Enter input filename: "; + std::cin.clear(); + std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); + getline(cin, filename); + string base64Data = encodeFileToBase64(filename); + cout << "Base64 Encoded Data:\n" << base64Data << endl; break; } case 2: { - std::string base64Data; - std::cout << "Enter Base64 encoded data: "; - std::cin >> base64Data; + string base64Data; + cout << "Enter Base64 encoded data: "; + cin >> base64Data; - std::string filename; - std::cout << "Enter output filename: "; - std::cin >> filename; + string filename; + cout << "Enter output filename: "; + std::cin.clear(); + std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); + getline(cin, filename); if (decodeBase64ToFile(base64Data, filename)) { - std::cout << "File successfully decoded and written: " << filename << std::endl; + cout << "File successfully decoded and written: " << filename << endl; } else { - std::cerr << "Failed to decode and write file." << std::endl; + cout << "Failed to decode and write file." << endl; } break; } @@ -50,29 +58,29 @@ int main() } while (choice != 0); } -std::string encodeFileToBase64(const std::string& filename) { - std::ifstream file(filename, std::ios::binary | std::ios::ate); +string encodeFileToBase64(const string& filename) { + ifstream file(filename, ios::binary | ios::ate); if (!file.is_open()) { - std::cerr << "Error: Unable to open file " << filename << std::endl; + cout << "Error: Unable to open file " << filename << endl; return ""; } // Get file size - std::streamsize fileSize = file.tellg(); - file.seekg(0, std::ios::beg); + streamsize fileSize = file.tellg(); + file.seekg(0, ios::beg); // Read file into buffer char* buffer = new char[fileSize]; file.read(buffer, fileSize); // Base64 encoding table - const std::string base64_chars = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - "0123456789+/"; + const string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; // Encode buffer to Base64 - std::stringstream encoded; + stringstream encoded; int val = 0, valb = -6; for (int i = 0; i < fileSize; i++) { unsigned char c = buffer[i]; @@ -84,31 +92,41 @@ std::string encodeFileToBase64(const std::string& filename) { } } - // Padding with '=' - while (valb > -6) { + // Handle any remaining bits and add padding + if (valb > -6) { + encoded << base64_chars[((val << 8) >> (valb + 8)) & 0x3F]; + } + while (encoded.str().size() % 4 != 0) { encoded << '='; - valb -= 6; } + /*// Calculate padding + int padding = (3 - (fileSize % 3)) % 3; + + // Add padding with '=' + while (padding--) { + encoded << '='; + }*/ + delete[] buffer; file.close(); return encoded.str(); } -std::string decodeBase64(const std::string& encoded) { +string decodeBase64(const string& encoded) { // Base64 decoding table - const std::string base64_chars = + const string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; - std::string decoded; + string decoded; int val = 0, valb = -8; for (char c : encoded) { if (c == '=') break; - if (base64_chars.find(c) == std::string::npos) { - std::cerr << "Error: Invalid Base64 character detected!" << std::endl; + if (base64_chars.find(c) == string::npos) { + cout << "Error: Invalid Base64 character detected!" << endl; return ""; } val = (val << 6) + base64_chars.find(c); @@ -122,16 +140,16 @@ std::string decodeBase64(const std::string& encoded) { } -bool decodeBase64ToFile(const std::string& base64Data, const std::string& filename) { - std::ofstream file(filename, std::ios::binary); +bool decodeBase64ToFile(const string& base64Data, const string& filename) { + ofstream file(filename, ios::binary); if (!file.is_open()) { - std::cerr << "Error: Unable to create file " << filename << std::endl; + cout << "Error: Unable to create file " << filename << endl; return false; } - std::string decodedData = decodeBase64(base64Data); + string decodedData = decodeBase64(base64Data); if (decodedData.empty()) { - std::cerr << "Error: Failed to decode Base64 data" << std::endl; + cout << "Error: Failed to decode Base64 data" << endl; return false; } |