aboutsummaryrefslogtreecommitdiff
path: root/CST 126/Homework 2/main.cpp
blob: 92a5a59323daf91dd62c4f34bd259d85f13a868f (plain) (blame)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Name: Logan Gillespie
// Class: CST 126
// Date: 5/16/24
// Assignment: Homework 2

#include <iostream>
#include <fstream>
#include <sstream>

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 {
        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:
        {
            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:
        {
            string base64Data;
            cout << "Enter Base64 encoded data: ";
            cin >> base64Data;

            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)) {
                cout << "File successfully decoded and written: " << filename << endl;
            }
            else {
                cout << "Failed to decode and write file." << endl;
            }
            break;
        }
        }
    } while (choice != 0);
}

string encodeFileToBase64(const string& filename) {
    ifstream file(filename, ios::binary | ios::ate);
    if (!file.is_open()) {
        cout << "Error: Unable to open file " << filename << endl;
        return "";
    }

    // Get file size
    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 string base64_chars =
             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
             "abcdefghijklmnopqrstuvwxyz"
             "0123456789+/";

    // Encode buffer to Base64
    stringstream encoded;
    int val = 0, valb = -6;
    for (int i = 0; i < fileSize; i++) {
        unsigned char c = buffer[i];
        val = (val << 8) + c;
        valb += 8;
        while (valb >= 0) {
            encoded << base64_chars[(val >> valb) & 0x3F];
            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 << '=';
    }

    /*// Calculate padding
    int padding = (3 - (fileSize % 3)) % 3;

    // Add padding with '='
    while (padding--) {
        encoded << '=';
    }*/

    delete[] buffer;
    file.close();

    return encoded.str();
}

string decodeBase64(const string& encoded) {
    // Base64 decoding table
    const string base64_chars =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz"
        "0123456789+/";

    string decoded;
    int val = 0, valb = -8;
    for (char c : encoded) {
        if (c == '=') break;
        if (base64_chars.find(c) == string::npos) {
            cout << "Error: Invalid Base64 character detected!" << endl;
            return "";
        }
        val = (val << 6) + base64_chars.find(c);
        valb += 6;
        if (valb >= 0) {
            decoded.push_back(char((val >> valb) & 0xFF));
            valb -= 8;
        }
    }
    return decoded;
}


bool decodeBase64ToFile(const string& base64Data, const string& filename) {
    ofstream file(filename, ios::binary);
    if (!file.is_open()) {
        cout << "Error: Unable to create file " << filename << endl;
        return false;
    }

    string decodedData = decodeBase64(base64Data);
    if (decodedData.empty()) {
        cout << "Error: Failed to decode Base64 data" << endl;
        return false;
    }

    file.write(decodedData.c_str(), decodedData.size());
    file.close();
    return true;
}