Two functions to encode and decode UUE (Uuencode)
|
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 |
#include <iostream> #include <string> #include <vector> std::string uuencode(const std::string& input) { std::string encoded; int len = input.length(); for (int i = 0; i < len; i += 3) { unsigned char byte1 = i < len ? input[i] : 0; unsigned char byte2 = (i + 1) < len ? input[i + 1] : 0; unsigned char byte3 = (i + 2) < len ? input[i + 2] : 0; unsigned int combined = (byte1 << 16) | (byte2 << 8) | byte3; for (int j = 18; j >= 0; j -= 6) { unsigned char chunk = (combined >> j) & 0x3F; encoded += (chunk ? chunk + 32 : 96); // encode chunk (0 becomes '`') } } return encoded; } std::string uudecode(const std::string& input) { std::string decoded; int len = input.length(); for (int i = 0; i < len; i += 4) { unsigned int combined = 0; for (int j = 0; j < 4; ++j) { unsigned char c = input[i + j]; c = (c == '`') ? 0 : (c - 32) & 0x3F; // decode character combined = (combined << 6) | c; } decoded += (combined >> 16) & 0xFF; if (i + 2 < len) decoded += (combined >> 8) & 0xFF; if (i + 3 < len) decoded += combined & 0xFF; } return decoded; } |
