Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <bits/stdc++.h>
- using namespace std;
- #define x first
- #define y second
- string norm1(string s) {
- while (s[0] == '0' && s.length() > 1)
- s = s.substr(1);
- if (s == "0")
- return "00000000";
- while (s.length() % 8 != 0)
- s = '0' + s;
- return s;
- }
- //string code(const int &k, string v) {
- // static int bits[6] = {7, 11, 16, 21, 26, 31};
- // if(v.length() > bits[k - 1])
- // return "";
- // while (v.length() != bits[k - 1])
- // v = "0" + v;
- // string res;
- // if (k == 1) {
- // res = "0" + v;
- // } else {
- // for (int i = 0; i < k; i++)
- // res += "1";
- // res += "0" + v.substr(0, 7 - k);
- // for (int i = 0; i < k - 1; i++) {
- // res += "10" + v.substr(7 - k + 6 * i, 6);
- // }
- // }
- // return res;
- //}
- map<string, string> f1;
- map<char, string> f;
- string hex(const string &s) {
- string res;
- for (int i = 0; i < s.length(); i += 4)
- res += f1[s.substr(i, 4)];
- while (res[0] == '0' && res.length() > 1)
- res = res.substr(1);
- return res;
- }
- string unhex(const string &s) {
- string res;
- for (int i = 0; i < s.length(); i++)
- res += f[s[i]];
- return norm1(res);
- }
- int decodeSize(const string &s) {
- if (s[0] == '0') return 1;
- if (s[1] == '0') return -1;
- if (s[2] == '0') return 2;
- if (s[3] == '0') return 3;
- if (s[4] == '0') return 4;
- if (s[5] == '0') return 5;
- if (s[6] == '0') return 6;
- return -1;
- }
- string decode(const int &k, const string &s) {
- string res;
- if (k == 1) {
- res = s.substr(1);
- } else {
- res = s.substr(k + 1, 7 - k);
- for (int i = 0; i < k - 1; i++) {
- if (s.substr(8 + 8 * i, 2) == "10")
- res += s.substr(8 + 8 * i + 2, 6);
- else
- return "$";
- }
- }
- return norm1(res);
- }
- vector<string> bytes;
- vector<string> block;
- void printBlock() {
- if (block.size() >= 3) {
- for (const string &s : block)
- cout << hex(s) << ' ';
- cout << endl;
- }
- block.clear();
- }
- int main() {
- f1["0000"] = '0';
- f1["0001"] = '1';
- f1["0010"] = '2';
- f1["0011"] = '3';
- f1["0100"] = '4';
- f1["0101"] = '5';
- f1["0110"] = '6';
- f1["0111"] = '7';
- f1["1000"] = '8';
- f1["1001"] = '9';
- f1["1010"] = 'A';
- f1["1011"] = 'B';
- f1["1100"] = 'C';
- f1["1101"] = 'D';
- f1["1110"] = 'E';
- f1["1111"] = 'F';
- f['0'] = "0000";
- f['1'] = "0001";
- f['2'] = "0010";
- f['3'] = "0011";
- f['4'] = "0100";
- f['5'] = "0101";
- f['6'] = "0110";
- f['7'] = "0111";
- f['8'] = "1000";
- f['9'] = "1001";
- f['A'] = "1010";
- f['B'] = "1011";
- f['C'] = "1100";
- f['D'] = "1101";
- f['E'] = "1110";
- f['F'] = "1111";
- string bs;
- while (cin >> bs) {
- bytes.push_back(unhex(bs));
- }
- for (int i = 0; i < bytes.size(); i++) {
- int k = decodeSize(bytes[i]);
- if (k == -1) {
- printBlock();
- continue;
- }
- string word;
- if (i + k <= bytes.size()) {
- for (int j = 0; j < k; j++)
- word += bytes[i + j];
- string uncypher = decode(k, word);
- if (uncypher != "$") {
- block.push_back(uncypher);
- i += k - 1;
- } else {
- printBlock();
- }
- } else {
- printBlock();
- }
- }
- printBlock();
- return 0;
- }
Add Comment
Please, Sign In to add comment