diff options
| author | rPatrickWarner <[email protected]> | 2024-05-08 06:08:58 -0700 |
|---|---|---|
| committer | rPatrickWarner <[email protected]> | 2024-05-08 06:08:58 -0700 |
| commit | fb87b3d9bb8d028e8e16bfcaefdc8b27aeb2791d (patch) | |
| tree | e7535e9602021b097051fac58767154b704ef193 /CST 126/Homework2 | |
| parent | more changes (diff) | |
| download | homework-1-reecepwarner-fb87b3d9bb8d028e8e16bfcaefdc8b27aeb2791d.tar.xz homework-1-reecepwarner-fb87b3d9bb8d028e8e16bfcaefdc8b27aeb2791d.zip | |
almost complete. decode and encode improvements
Diffstat (limited to 'CST 126/Homework2')
| -rw-r--r-- | CST 126/Homework2/Base64Conversion.hpp | 52 | ||||
| -rw-r--r-- | CST 126/Homework2/Base64Helper.hpp | 39 | ||||
| -rw-r--r-- | CST 126/Homework2/Homework2.vcxproj | 1 | ||||
| -rw-r--r-- | CST 126/Homework2/Homework2.vcxproj.filters | 1 | ||||
| -rw-r--r-- | CST 126/Homework2/destination_file.txt | 2 | ||||
| -rw-r--r-- | CST 126/Homework2/main.cpp | 112 | ||||
| -rw-r--r-- | CST 126/Homework2/newdestination_txt | 1 | ||||
| -rw-r--r-- | CST 126/Homework2/nextdest.txt | bin | 0 -> 132 bytes |
8 files changed, 143 insertions, 65 deletions
diff --git a/CST 126/Homework2/Base64Conversion.hpp b/CST 126/Homework2/Base64Conversion.hpp index 8081abd..1458d29 100644 --- a/CST 126/Homework2/Base64Conversion.hpp +++ b/CST 126/Homework2/Base64Conversion.hpp @@ -4,8 +4,6 @@ #include <bitset> #include <vector> - - char CharacterMapArray[] = { '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', 'a', 'b', 'c', 'd','e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', @@ -18,8 +16,8 @@ inline char* Base64Encode(char* buffer, const size_t& size) size_t firstvalue, secondvalue, thirdvalue, fourthvalue = 0; - std::vector<size_t>TempBuff = {}; - + std::vector<char>TempBuff = {}; + for (auto i = 0, j = 0; i < size; i+=3, j+=4) { @@ -38,20 +36,60 @@ inline char* Base64Encode(char* buffer, const size_t& size) TempBuff.push_back(CharacterMapArray[thirdvalue]); TempBuff.push_back(CharacterMapArray[fourthvalue]); } + size_t NewSize = TempBuff.size(); - delete[] buffer; buffer = nullptr; buffer = new char[NewSize]; for (auto i = 0; i < NewSize; i++) { buffer[i] = TempBuff[i]; + std::cout << buffer[i]; } - - + return buffer; +} + +inline char* Base64Decode(char* buffer, const size_t size) +{ + std::vector<char> TempBuffer = {}; + size_t firstvalue, secondvalue, thirdvalue = 0; + + for (auto i = 0u; i < size; i++) + { + for (auto j = 0u; j < size; j++) + { + if (buffer[i] == CharacterMapArray[j]) + { + buffer[i] = j;break; + } + } + } + for (auto i = 0u; i < size; i += 4) + { + firstvalue = buffer[i] ^ (buffer[i + 1] << 6); + secondvalue = (buffer[i + 1] >> 2) ^ (buffer[i + 2] << 4); + thirdvalue = (buffer[i + 2] >> 4) ^ (buffer[i + 3] << 2); + TempBuffer.push_back(firstvalue); + TempBuffer.push_back(secondvalue); + TempBuffer.push_back(thirdvalue); + } + size_t NewSize = TempBuffer.size(); + + buffer = nullptr; + + buffer = new char[NewSize]; + + for (auto i = 0; i < NewSize; i++) + { + buffer[i] = TempBuffer[i]; + std::cout << buffer[i]; + } + + + return buffer; } #endif
\ No newline at end of file diff --git a/CST 126/Homework2/Base64Helper.hpp b/CST 126/Homework2/Base64Helper.hpp index df04dbf..990d1a0 100644 --- a/CST 126/Homework2/Base64Helper.hpp +++ b/CST 126/Homework2/Base64Helper.hpp @@ -2,7 +2,9 @@ #define BASE64_HELPER_HPP #include <fstream> #include <iostream> - +using std::numeric_limits; +using std::streamsize; +constexpr size_t MAX_STREAM_SIZE = numeric_limits<streamsize>::max(); inline const size_t& SizeOfFile(const char* fileName) { @@ -45,7 +47,6 @@ inline char* ReadFileAsBinary(const char* fileName, char* buffer, const size_t& { try { - //open fstream in input mode, with binary mode std::fstream File(fileName,std::ios::in| std::ios::binary); @@ -89,9 +90,6 @@ inline bool WriteFileFromBinary(const size_t& size, const char* fileName, const File.write(buffer, size); File.close(); - /*delete[] buffer; - buffer = nullptr;*/ - return buffer; } @@ -124,6 +122,37 @@ inline bool WriteTextToFile(const char* fileName, const char* fileContents) return false; } +inline void Prompts(const char* Prompt) +{ + std::cout << Prompt; +} + +inline const char* InputFileName(const char* Prompt) +{ + + char* FileName = nullptr; + FileName = new char[101]; + + Prompts(Prompt); + + std::cout.flush(); + + + std::cin >> FileName; + + while (!std::cin) + { + std::cout << Prompt << std::endl; + std::cin.clear(); + + std::cin.ignore(MAX_STREAM_SIZE, '\n'); + std::cin >> FileName; + + } + + + return FileName; +} #endif
\ No newline at end of file diff --git a/CST 126/Homework2/Homework2.vcxproj b/CST 126/Homework2/Homework2.vcxproj index 0618b39..188b85a 100644 --- a/CST 126/Homework2/Homework2.vcxproj +++ b/CST 126/Homework2/Homework2.vcxproj @@ -135,6 +135,7 @@ </ItemGroup> <ItemGroup> <Text Include="destination_file.txt" /> + <Text Include="nextdest.txt" /> <Text Include="randomtext.txt" /> </ItemGroup> <ItemGroup> diff --git a/CST 126/Homework2/Homework2.vcxproj.filters b/CST 126/Homework2/Homework2.vcxproj.filters index 6bf0ece..19a24ba 100644 --- a/CST 126/Homework2/Homework2.vcxproj.filters +++ b/CST 126/Homework2/Homework2.vcxproj.filters @@ -30,6 +30,7 @@ <ItemGroup> <Text Include="destination_file.txt" /> <Text Include="randomtext.txt" /> + <Text Include="nextdest.txt" /> </ItemGroup> <ItemGroup> <None Include="..\..\..\..\..\..\Downloads\FindingPi.exe"> diff --git a/CST 126/Homework2/destination_file.txt b/CST 126/Homework2/destination_file.txt index 2df8fcd..52dc435 100644 --- a/CST 126/Homework2/destination_file.txt +++ b/CST 126/Homework2/destination_file.txt @@ -1 +1 @@ -0VGe0BCdvBCdlNHdg8Wd0BiYp5WYylHImlGblBiZ152Y0l2buNXDKM3btVGIt9mclBydvJHZzBSYuRGIzRXdmZWDKM3btVG
\ No newline at end of file +0VGe0BCdvBCdlNHdg8Wd0BiYp5WYylHImlGblBiZ152Y0l2buNXDKM3btVGIt9mclBydvJHZzBSYuRGIzRXdmZWDKM3btVGIt9mclBydvJHZzBiZvJHI0V2c0lmbnFS+��������|f�jŴ
\ No newline at end of file diff --git a/CST 126/Homework2/main.cpp b/CST 126/Homework2/main.cpp index 82a2d32..2f9d3d3 100644 --- a/CST 126/Homework2/main.cpp +++ b/CST 126/Homework2/main.cpp @@ -10,60 +10,69 @@ bool Worker(char** argv); int main(const int argc, char* argv[]) { - char* buffer = nullptr; - char* MyEncodedCharArray = nullptr; - bool success = false; - const size_t size = SizeOfFile("randomtext.txt"); - buffer = new char[1]; - - buffer = ReadFileAsBinary("randomtext.txt", buffer, size); - - MyEncodedCharArray = Base64Encode(buffer, size); - - success = WriteFileFromBinary(size, "destination_file.txt", buffer); - delete[] buffer; - //if (argc == ARG_COUNT) - //{ - // Worker(argv); - //} - //else - //{ - // //run other version! - //} - - - + if (argc == ARG_COUNT) + { + Worker(argv); + } + else + { + Worker(); + } - return Worker(); //can be used to trigger error controls + return 0; } bool Worker() { - char option = 'e'; - - switch (option) + bool success = false; + char* MyEncodedCharArray = nullptr; + const char* MyFile = nullptr; + const char* Destination = nullptr; + char* buffer = nullptr; + size_t size = 0; + buffer = new char[size]; + char Option = '\0'; + do { - case 'e': - //file reading binary - //encoding - //file writing text - return true; - case 'd': - //file reading text - //decoding work - //file writing binary - return true; - default: - std::cerr << "Error, invalid command option\n" << - "Valid commands:\n" << - "\t -e source_file.exe destination_file.exe" << - "\tEncodes file in source to text in destination txt file.\n\n" << - "\t-d source_file.text destination_file.exe" << - "\tDecodes text in source file into the destination file.\n\n"; - return false; - } + Prompts("!Welcome to the Base64 Encoding/Decoding Menu!\n\ne)Base64 Encode Your File\nd)Decode Your Encoded File\nf)Exit\n\n"); + + std::cin >> Option; + switch (Option) + { + case 'e': + + MyFile = InputFileName("What is the name of the file you wish to encode?: "); + Destination = InputFileName("\nWhat is the name of the file that you wish to save the encoded information?: "); + size = SizeOfFile(MyFile); + buffer = ReadFileAsBinary(MyFile, buffer, size); + success = WriteTextToFile(Destination, Base64Encode(buffer, size)); + delete[] buffer; + return true; + case 'd': + + MyFile = InputFileName("What is the name of the file you wish to decode?: "); + Destination = InputFileName("\nWhat is the name of the file you wish to save the decoded information?: "); + size = SizeOfFile(MyFile); + buffer = ReadTextFromFile(MyFile, buffer); + success = WriteFileFromBinary(size, Destination, Base64Decode(buffer, size)); + delete[] buffer; + return true; + case'f': + std::cout << "Have a great day!" << std::endl; + break; + default: + std::cerr << "Error, invalid command option\n" << + "Valid commands:\n" << + "\t -e source_file.exe destination_file.exe" << + "\tEncodes file in source to text in destination txt file.\n\n" << + "\t-d source_file.text destination_file.exe" << + "\tDecodes text in source file into the destination file.\n\n"; + return false; + } + } while (Option != 'f'); + return false; } @@ -82,22 +91,21 @@ bool Worker(char** argv) switch (option) { case 'e': - //filereading binary buffer = new char[size]; - + buffer = ReadFileAsBinary(arg2, buffer, size); MyEncodedCharArray = Base64Encode(buffer, size); - success = WriteFileFromBinary(size, arg3, buffer); + success = WriteTextToFile(arg3, MyEncodedCharArray); delete[] buffer; return success; case 'd': - //file readingtext - //decoding work - //file writing binary + buffer = new char[size]; + buffer = ReadTextFromFile(arg2, buffer); + success = WriteFileFromBinary(size, arg3, Base64Decode(buffer, size)); return true; default: std::cerr << "Error, invalid command option\n" << diff --git a/CST 126/Homework2/newdestination_txt b/CST 126/Homework2/newdestination_txt new file mode 100644 index 0000000..b1e7100 --- /dev/null +++ b/CST 126/Homework2/newdestination_txt @@ -0,0 +1 @@ +RFVURFVURFVURFVURFVURFVURFVURFVURFVURFVURFVURFVURFVUR1v+
\ No newline at end of file diff --git a/CST 126/Homework2/nextdest.txt b/CST 126/Homework2/nextdest.txt Binary files differnew file mode 100644 index 0000000..15ee807 --- /dev/null +++ b/CST 126/Homework2/nextdest.txt |