Guest User

Untitled

a guest
Oct 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define x first
  7. #define y second
  8.  
  9. string norm1(string s) {
  10. while (s[0] == '0' && s.length() > 1)
  11. s = s.substr(1);
  12. if (s == "0")
  13. return "00000000";
  14. while (s.length() % 8 != 0)
  15. s = '0' + s;
  16. return s;
  17. }
  18.  
  19. //string code(const int &k, string v) {
  20. // static int bits[6] = {7, 11, 16, 21, 26, 31};
  21. // if(v.length() > bits[k - 1])
  22. // return "";
  23. // while (v.length() != bits[k - 1])
  24. // v = "0" + v;
  25. // string res;
  26. // if (k == 1) {
  27. // res = "0" + v;
  28. // } else {
  29. // for (int i = 0; i < k; i++)
  30. // res += "1";
  31. // res += "0" + v.substr(0, 7 - k);
  32. // for (int i = 0; i < k - 1; i++) {
  33. // res += "10" + v.substr(7 - k + 6 * i, 6);
  34. // }
  35. // }
  36. // return res;
  37. //}
  38.  
  39. map<string, string> f1;
  40. map<char, string> f;
  41.  
  42. string hex(const string &s) {
  43. string res;
  44. for (int i = 0; i < s.length(); i += 4)
  45. res += f1[s.substr(i, 4)];
  46. while (res[0] == '0' && res.length() > 1)
  47. res = res.substr(1);
  48. return res;
  49. }
  50.  
  51. string unhex(const string &s) {
  52. string res;
  53. for (int i = 0; i < s.length(); i++)
  54. res += f[s[i]];
  55. return norm1(res);
  56. }
  57.  
  58. int decodeSize(const string &s) {
  59. if (s[0] == '0') return 1;
  60. if (s[1] == '0') return -1;
  61. if (s[2] == '0') return 2;
  62. if (s[3] == '0') return 3;
  63. if (s[4] == '0') return 4;
  64. if (s[5] == '0') return 5;
  65. if (s[6] == '0') return 6;
  66. return -1;
  67. }
  68.  
  69. string decode(const int &k, const string &s) {
  70. string res;
  71. if (k == 1) {
  72. res = s.substr(1);
  73. } else {
  74. res = s.substr(k + 1, 7 - k);
  75. for (int i = 0; i < k - 1; i++) {
  76. if (s.substr(8 + 8 * i, 2) == "10")
  77. res += s.substr(8 + 8 * i + 2, 6);
  78. else
  79. return "$";
  80. }
  81. }
  82. return norm1(res);
  83. }
  84.  
  85. vector<string> bytes;
  86. vector<string> block;
  87.  
  88. void printBlock() {
  89. if (block.size() >= 3) {
  90. for (const string &s : block)
  91. cout << hex(s) << ' ';
  92. cout << endl;
  93. }
  94. block.clear();
  95. }
  96.  
  97. int main() {
  98. f1["0000"] = '0';
  99. f1["0001"] = '1';
  100. f1["0010"] = '2';
  101. f1["0011"] = '3';
  102. f1["0100"] = '4';
  103. f1["0101"] = '5';
  104. f1["0110"] = '6';
  105. f1["0111"] = '7';
  106. f1["1000"] = '8';
  107. f1["1001"] = '9';
  108. f1["1010"] = 'A';
  109. f1["1011"] = 'B';
  110. f1["1100"] = 'C';
  111. f1["1101"] = 'D';
  112. f1["1110"] = 'E';
  113. f1["1111"] = 'F';
  114. f['0'] = "0000";
  115. f['1'] = "0001";
  116. f['2'] = "0010";
  117. f['3'] = "0011";
  118. f['4'] = "0100";
  119. f['5'] = "0101";
  120. f['6'] = "0110";
  121. f['7'] = "0111";
  122. f['8'] = "1000";
  123. f['9'] = "1001";
  124. f['A'] = "1010";
  125. f['B'] = "1011";
  126. f['C'] = "1100";
  127. f['D'] = "1101";
  128. f['E'] = "1110";
  129. f['F'] = "1111";
  130. string bs;
  131. while (cin >> bs) {
  132. bytes.push_back(unhex(bs));
  133. }
  134. for (int i = 0; i < bytes.size(); i++) {
  135. int k = decodeSize(bytes[i]);
  136. if (k == -1) {
  137. printBlock();
  138. continue;
  139. }
  140. string word;
  141. if (i + k <= bytes.size()) {
  142. for (int j = 0; j < k; j++)
  143. word += bytes[i + j];
  144. string uncypher = decode(k, word);
  145. if (uncypher != "$") {
  146. block.push_back(uncypher);
  147. i += k - 1;
  148. } else {
  149. printBlock();
  150. }
  151. } else {
  152. printBlock();
  153. }
  154. }
  155. printBlock();
  156. return 0;
  157. }
Add Comment
Please, Sign In to add comment