diff options
| author | WesleyR23 <[email protected]> | 2024-05-15 18:51:42 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-05-15 18:51:42 -0700 |
| commit | 0357df8c6f39014e6fa2838341be645590df6bca (patch) | |
| tree | 46eb312fbb8f127a28ebd79ea68a3492db07e50d | |
| parent | merging develop after PR (diff) | |
| download | homework-1-wesleyr23-0357df8c6f39014e6fa2838341be645590df6bca.tar.xz homework-1-wesleyr23-0357df8c6f39014e6fa2838341be645590df6bca.zip | |
Homework2 (#3)
* Lab1
* updating
* commiting for merge into develop
| -rw-r--r-- | CST 126/Homework2/Base64Converter.hpp | 117 | ||||
| -rw-r--r-- | CST 126/Homework2/Homework2.vcxproj | 7 | ||||
| -rw-r--r-- | CST 126/Homework2/Homework2.vcxproj.filters | 13 | ||||
| -rw-r--r-- | CST 126/Homework2/destination_file.txt | 0 | ||||
| -rw-r--r-- | CST 126/Homework2/helpers.hpp | 134 | ||||
| -rw-r--r-- | CST 126/Homework2/program.cpp | 143 |
6 files changed, 414 insertions, 0 deletions
diff --git a/CST 126/Homework2/Base64Converter.hpp b/CST 126/Homework2/Base64Converter.hpp new file mode 100644 index 0000000..73980ed --- /dev/null +++ b/CST 126/Homework2/Base64Converter.hpp @@ -0,0 +1,117 @@ +#ifndef BASE64_CONVERTER +#define BASE64_CONVERTER + +#include <iostream> +#include <string> +#include <vector> + +using std::string; +using std::vector; + +typedef unsigned char UC; +typedef unsigned int UI; +#define EXTRA '^' +#define MASK1 0xfc +#define MASK2 0x03 +#define MASK3 0xf0 +#define MASK4 0x0f +#define MASK5 0xc0 +#define MASK6 0x3f +#define MASK7 0x30 +#define MASK8 0x3c + +const string characterMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +inline string Base64Encode(UC const* buffer, size_t& size) +{ + string encoded = ""; + + UI i = 0, + j = 0, + k = 0; + + UC temp_a_3[3], + temp_4[4]; + + for (i = 0; i < size; i += 3) { + + for (j = i, k = 0; j < size && j < i + 3; j++) + temp_a_3[k++] = *(buffer++); + + for (; k < 3; k++) + temp_a_3[k] = '\0'; + + temp_4[0] = (temp_a_3[0] & MASK1) >> 2; + temp_4[1] = ((temp_a_3[0] & MASK2) << 4) + ((temp_a_3[1] & MASK3) >> 4); + temp_4[2] = ((temp_a_3[1] & MASK4) << 2) + ((temp_a_3[2] & MASK5) >> 6); + temp_4[3] = temp_a_3[2] & MASK6; + + for (j = i, k = 0; j < size + 1 && j < i + 4; j++, k++) + encoded += characterMap[temp_4[k]]; + + for (; k < 4; k++) + encoded += EXTRA; + } + + return encoded; +} + +inline vector<UC> Base64Decoder(string const& encoded) +{ + UI i = 0, + j = 0, + k = 0, + in_len = encoded.size(); + + UC temp_a_3[3], + temp_4[4]; + + vector<UC> decoded; + + for (i = 0; i < in_len; i += 4) { + + for (j = i, k = 0; j < i + 4 && encoded[j] != EXTRA; j++) + temp_4[k++] = characterMap.find(encoded[j]); + + for (; k < 4; k++) + temp_4[k++] = '\0'; + + temp_a_3[0] = (temp_4[0] << 2) + ((temp_4[1] & MASK7) >> 4); + temp_a_3[1] = ((temp_4[1] & MASK4) << 4) + ((temp_4[2] & MASK8) >> 2); + temp_a_3[2] = ((temp_4[2] & MASK2) << 6) + temp_4[3]; + + for (j = i, k = 0; k < 3 && encoded[j + 1] != EXTRA; j++, k++) + decoded.push_back(temp_a_3[k]); + } + + return decoded; +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#endif
\ No newline at end of file diff --git a/CST 126/Homework2/Homework2.vcxproj b/CST 126/Homework2/Homework2.vcxproj index a7c6d6c..cf0cae3 100644 --- a/CST 126/Homework2/Homework2.vcxproj +++ b/CST 126/Homework2/Homework2.vcxproj @@ -132,6 +132,13 @@ <ItemGroup> <None Include="FindingPi.exe" /> </ItemGroup> + <ItemGroup> + <ClInclude Include="Base64Converter.hpp" /> + <ClInclude Include="helpers.hpp" /> + </ItemGroup> + <ItemGroup> + <Text Include="destination_file.txt" /> + </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup> diff --git a/CST 126/Homework2/Homework2.vcxproj.filters b/CST 126/Homework2/Homework2.vcxproj.filters index 6328c74..296f0a3 100644 --- a/CST 126/Homework2/Homework2.vcxproj.filters +++ b/CST 126/Homework2/Homework2.vcxproj.filters @@ -24,4 +24,17 @@ <Filter>Source Files</Filter> </None> </ItemGroup> + <ItemGroup> + <ClInclude Include="Base64Converter.hpp"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="helpers.hpp"> + <Filter>Source Files</Filter> + </ClInclude> + </ItemGroup> + <ItemGroup> + <Text Include="destination_file.txt"> + <Filter>Source Files</Filter> + </Text> + </ItemGroup> </Project>
\ No newline at end of file diff --git a/CST 126/Homework2/destination_file.txt b/CST 126/Homework2/destination_file.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/CST 126/Homework2/destination_file.txt diff --git a/CST 126/Homework2/helpers.hpp b/CST 126/Homework2/helpers.hpp new file mode 100644 index 0000000..a9531ec --- /dev/null +++ b/CST 126/Homework2/helpers.hpp @@ -0,0 +1,134 @@ +#ifndef HELPERS_H +#define HELPERS_H + +#include <fstream> +#include <vector> +#include <string> + +using namespace std; + +//file reading things +inline string ReadTextFromFile(const char* fileName, string buffer) +{ + std::ifstream file(fileName); + + unsigned char temporary; + + if (!file.is_open()) + { + std::cerr << "Could not open file for text input: " << fileName; + return buffer; + } + + while (getline(file, buffer)) + buffer += buffer; + + file.close(); + + return buffer; +} + +inline vector<unsigned char> ReadFileAsBinary(const char* fileName, vector<unsigned char> buffer) +{ + try + { + //open the fstream in input mode, with binary mode + std::fstream file(fileName, std::ios::in | std::ios::binary); + + unsigned char temporary; + + if (!file.is_open()) + { + std::cerr << "Could not open file for binary input: " << fileName; + return buffer; + } + + while (file >> temporary) + { + buffer.push_back(temporary); + } + + file.close(); + + return buffer; + } + catch (const std::exception& ex) + { + std::cerr << "Exception during binary file input." << fileName << + "was not successfully streamed to binary." << ex.what(); + + + return buffer; + } +} + + +//file writing things +inline bool WriteTextToFile(const char* fileName, const string fileContents) +{ + std::ofstream file(fileName); + + if(!file.is_open()) + { + std::cerr << "Could not open file: " << fileName; + return false; + } + + file << fileContents; + + file.close(); + + return true; +} + +inline bool WriteFileFromBinary(const char* fileName, vector<unsigned char> buffer) +{ + std::ofstream file(fileName); + int count = 0; + + if (!file.is_open()) + { + std::cerr << "Could not open file: " << fileName; + return false; + } + + for (auto const& x : buffer) { + file << x; + count = count + 1; + if (count == 99) { + file << "\n"; + count = 0; + } + } + + file.close(); + + + return true; +} + + + + + + + + + + + + + + + + + + + + + + + + + +#endif
\ No newline at end of file diff --git a/CST 126/Homework2/program.cpp b/CST 126/Homework2/program.cpp index e69de29..fb5e8c5 100644 --- a/CST 126/Homework2/program.cpp +++ b/CST 126/Homework2/program.cpp @@ -0,0 +1,143 @@ +// Name: Wesley Richelderfer +// Class: CST 126 +// Date: 4/27/24 +// Assignment: Homework 2 + +#include <iostream> +#include <string> + +#include "helpers.hpp" +#include "Base64Converter.hpp" + +using std::string; + +constexpr short ARG_COUNT = 4; + +bool Worker(); +bool Worker(char** argv); + + +int main (const int argc, char* argv[]) +{ + + if(argc == ARG_COUNT) + { + //run command line version + Worker(argv); + } + + + //console app version + return Worker(); +} + +bool Worker() +{ + //files and choice + char option; + string destination; + string source; + + //variables for switch statement + bool success; + vector<unsigned char> buffer; + string product; + size_t size = 0; + + cout << "Would you like to encode or decode? (e or d): "; + cin >> option; + cout << "\n\nWhat is the source file name? "; + cin >> source; + cout << "\n\nWhat is the destination file name? "; + cin >> destination; + + const char* arg2 = source.c_str(); + const char* arg3 = destination.c_str(); + + switch (option) + { + case 'e': + + buffer = ReadFileAsBinary(arg2, buffer); + + size = buffer.size(); + + product = Base64Encode(&buffer[0], size); + + success = WriteTextToFile(arg3, product); + + return success; + + case 'd': + + ReadTextFromFile(arg2, product); + + buffer = Base64Decoder(product); + + success = WriteFileFromBinary(arg3, buffer); + + return success; + + default: + std::cerr << "Invalid command option\n" << + "Valid commands:\n" << + "\t-e source_file.exe destination_file.exe" << + "\t\tEncodes file in source into text in destination txt file." << + "\t-d source_file.txt destination_file.exe" << + "\t\tDecodes text in source file into the destination file.\n\n"; + return false; + } +} + +bool Worker(char** argv) +{ + //files and choice + const char* arg1 = argv[1]; //-e -d + const char* arg2 = argv[2]; //source file name + const char* arg3 = argv[3]; //destination file name + + const char option = arg1[1]; //e or d + + //variables for switch statement + bool success; + vector<unsigned char> buffer; + string product; + size_t size = 0; + + switch(option) + { + case 'e': + + buffer = ReadFileAsBinary(arg2, buffer); + + size = buffer.size(); + + product = Base64Encode(&buffer[0], size); + + success = WriteTextToFile(arg3, product); + + return success; + + case 'd': + + ReadTextFromFile(arg2, product); + + buffer = Base64Decoder(product); + + success = WriteFileFromBinary(arg3, buffer); + + return success; + + default: + + std::cerr << "Invalid command option\n" << + "Valid commands:\n" << + "\t-e source_file.exe destination_file.exe" << + "\t\tEncodes file in source into text in destination txt file." << + "\t-d source_file.txt destination_file.exe" << + "\t\tDecodes text in source file into the destination file.\n\n"; + + return false; + + } +}
\ No newline at end of file |