Advertisement
Guest User

:')

a guest
Mar 25th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. public class Ascii85
  7. {
  8. // Encoding method
  9. public static string toAscii85(byte[] data) {
  10. string result = "";
  11. Console.WriteLine("data.Length: {0}", data.Length);
  12.  
  13. if (data.Length == 0) { return "<~~>"; }
  14.  
  15. int addCut = data.Length % 4 > 0 ? 4 - data.Length % 4 : 0;
  16.  
  17. Console.WriteLine("data input:\n{0}", String.Join(", ", data));
  18. Console.WriteLine("data lenght {0}\ndata.Length % 4: {1}\naddCut {2}", data.Length, data.Length % 4, addCut);
  19.  
  20. // 8-bit-formated string-stored byte array data. PadLeft keeps leading zeroes.
  21. string[] bits = data.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')).ToArray();
  22. //Console.WriteLine("bits:\n{0}\nbits.Length: {1}", String.Join("\n", bits), bits.Length);
  23.  
  24. for (int i = 0; i < bits.Length; i++) {
  25. Console.WriteLine(bits[i] != "00111111" ? bits[i] : "\t\t{0}", bits[i]); // question mark
  26. }
  27.  
  28. // storing 32-bit integers as strings
  29. List<string> binaryGroup = new List<string>();
  30.  
  31. // adds first and every 4-th 8-bit group after to List<string> and appends other 3 elements right to it
  32. for (int i = 0; i < bits.Length; i++) {
  33. if (i % 4 == 0) { binaryGroup.Add(bits[i]); }
  34. else { binaryGroup[binaryGroup.Count - 1] += bits[i]; }
  35. }
  36.  
  37. if (addCut > 0) {
  38. int i = addCut;
  39. //this will run for i + 1 times
  40. while (i > 0) {
  41. binaryGroup[binaryGroup.Count - 1] += "01110101"; // 'u'
  42. Console.WriteLine("Added 01110101");
  43. i -= 1;
  44. }
  45. }
  46.  
  47. Console.WriteLine("binaryGroup:\n{0}", String.Join("\n", binaryGroup));
  48. Console.WriteLine("last length: {0}", binaryGroup[binaryGroup.Count - 1].Length);
  49.  
  50. // calculating values
  51. List<byte> bList = new List<byte>();
  52. for (int j = 0; j < binaryGroup.Count; j++) {
  53. long x = Convert.ToInt64(binaryGroup[j], 2);
  54. // in case of 'z' (four empty bytes)
  55. if (x == 0 && data.Length > 3) { bList.Add(122); }
  56.  
  57. else {
  58. long a = x / (85 * 85 * 85 * 85);
  59. long b = x / (85 * 85 * 85);
  60. long c = x / (85 * 85);
  61. long d = x / 85;
  62. bList.Add(Convert.ToByte(a + 33));
  63. bList.Add(Convert.ToByte(b - 85 * a + 33));
  64. bList.Add(Convert.ToByte(c - 85 * b + 33));
  65. bList.Add(Convert.ToByte(d - 85 * c + 33));
  66. bList.Add(Convert.ToByte((x % 85) + 33));
  67. }
  68. }
  69.  
  70. if (addCut > 0) {
  71. int i = addCut;
  72. while (i > 0) {
  73. Console.WriteLine("Removed {0}", bList[bList.Count - 1]);
  74. bList.RemoveAt(bList.Count - 1);
  75. i -= 1;
  76. }
  77. }
  78. result = "<~";
  79. foreach (var i in bList) {
  80. result += Convert.ToChar(i);
  81. }
  82. result += "~>";
  83.  
  84. byte[] tmpb = Encoding.Unicode.GetBytes(result);
  85. Console.Write("tmpb:\n{0}\n", String.Join(", ", tmpb));
  86. Console.WriteLine("tmpb.Length: {0}", tmpb.Length);
  87.  
  88. byte[] tmpb1 = Encoding.UTF8.GetBytes(result);
  89. Console.Write("tmpbUTF8:\n{0}\n", String.Join(", ", tmpb1));
  90. Console.WriteLine("tmpb.Length: {0}", tmpb1.Length);
  91.  
  92. bool tst = true;
  93. for (int b = 0; b < tmpb.Length; b++) {
  94. if (tmpb[b] != tmpb1[b]) {
  95. Console.Write("tmpb[{0}] ({1}) != tmpb1[{0}] ({2})", b, tmpb[b], tmpb1[b]);
  96. tst = false;
  97. break;
  98. }
  99. }
  100.  
  101. Console.WriteLine("result.Length: {0}\nresult:\n{1}", result.Length, result);
  102. Console.WriteLine(tst ? "elements are equal" : "not equal");
  103.  
  104. return result;
  105. }
  106.  
  107. // SECOND ONE
  108.  
  109. // Decoding method
  110. public static byte[] fromAscii85(string data) {
  111. int lessThanFive = 0;
  112.  
  113. /// index, \uXXXX unicode element value
  114. Dictionary<int, string> nonascii85 = new Dictionary<int, string>();
  115.  
  116. //int[,] unicodes = new int[data.Length, data.Length];
  117. Console.WriteLine("raw data:\n{0}", data);
  118.  
  119. // removes whitespaces, cuts braces
  120. string cleared = Regex.Replace(new string((from c in data where !char.IsWhiteSpace(c) select c).ToArray()),
  121. @"^<~|~>$", ""); //|~|{|}|\||v|w|x|y
  122. Console.WriteLine("\ncleared:\n{0}\n", cleared);
  123.  
  124. string clear = "";
  125. for (int i = 0; i < cleared.Length; i++) {
  126. // non-ascii85 character
  127. if (cleared[i] != 'z' && cleared[i] > 117 || cleared[i] < 32) {
  128. nonascii85.Add(i, "\\u" + Convert.ToInt32(cleared[i]).ToString("x4"));
  129. clear += 'y';
  130. }
  131.  
  132. else {
  133. if (cleared[i] == 'z') { clear += "!!!!!"; }
  134. else { clear += cleared[i]; }
  135. }
  136. }
  137.  
  138. Console.WriteLine("nonascii85:");
  139. foreach (var i in nonascii85) {
  140. Console.WriteLine("{0}\t{1}", i.Key, i.Value);
  141. }
  142.  
  143. while (clear.Length % 5 != 0) {
  144. clear += 'u';
  145. }
  146.  
  147. #region log of clear string
  148. Console.WriteLine("\nclear:");
  149. for (int i = 0; i < clear.Length; i++) {
  150. if (clear[i] != 'z' && clear[i] > 117 || clear[i] < 33) {
  151. Console.BackgroundColor = ConsoleColor.DarkGreen;
  152. Console.Write(clear[i]);
  153. Console.ResetColor();
  154. }
  155. else { Console.Write(clear[i]); }
  156. }
  157.  
  158. Console.WriteLine("\nclear.length: {0}", clear.Length);
  159. Console.Write("\n===fives:===");
  160. for (var i = 0; i < clear.Length; i++) {
  161. if (clear[i] != 'z' && clear[i] > 117 || clear[i] < 33) {
  162. Console.BackgroundColor = ConsoleColor.DarkGreen;
  163. if (i % 5 == 0) { Console.Write("\n" + clear[i]); }
  164. else { Console.Write(clear[i]); }
  165. Console.ResetColor();
  166. }
  167. else {
  168. if (i % 5 == 0) { Console.Write("\n" + clear[i]); }
  169. else { Console.Write(clear[i]); }
  170. }
  171. }
  172. #endregion
  173.  
  174. // Forming "groups" of Fives
  175. string Fives = "";
  176. for (int i = 0; i < cleared.Length; i++) {
  177. if (cleared[i] == 'z') { Fives += "!!!!!"; }
  178. else { Fives += cleared[i]; }
  179. }
  180.  
  181. while (Fives.Length % 5 != 0) {
  182. Fives += 'u';
  183. lessThanFive++;
  184. }
  185.  
  186. List<long> calculated = new List<long>();
  187. foreach (var c in clear) {
  188. calculated.Add(Convert.ToInt64(c) - 33);
  189. //calculated.Add(c != 'y' ? Convert.ToInt64(c) - 33 : 121); // 'y' == 121
  190. }
  191.  
  192. Console.Write("\n\ncalculated:\n{0}", String.Join(", ", calculated));
  193.  
  194. // double because Math.Pow() doesn't take integers
  195. double n = 4.0;
  196. List<long> sums = new List<long>();
  197.  
  198. for (int i = 0; i < calculated.Count; i++) {
  199. if (i % 5 == 0) {
  200. sums.Add(calculated[i] * 52200625);
  201. n = 4.0;
  202. }
  203. else {
  204. sums[sums.Count - 1] += calculated[i] * Convert.ToInt64(Math.Pow(85.0, n - 1));
  205. n = n - 1.0;
  206. }
  207. }
  208.  
  209. Console.WriteLine("\n\nsums:");
  210. Console.Write("{0}", String.Join(", ", sums));
  211.  
  212. List<byte> result = new List<byte>();
  213. foreach (var s in sums) {
  214. for (int k = 0; k < 4; k++) {
  215. result.Add(Convert.ToByte(Convert.ToString(s, 2).PadLeft(32, '0').Substring(k * 8, 8), 2));
  216. }
  217. }
  218.  
  219. // decalculating them into groups of fours
  220. List<byte> Fours = new List<byte>();
  221. foreach (var c in Fives) {
  222. Fours.Add(Convert.ToByte(c));
  223. }
  224.  
  225. for (int i = 0; i < Fours.Count; i++) {
  226. Fours[i] -= (byte)33;
  227. }
  228.  
  229. // group items in Fours into groups by 5 in each, multiply them "by 85..." and sum
  230. List<long> Decalculated = new List<long>(Fours.Count);
  231.  
  232. // decalculating values to "1,298,230,816"
  233. for (int j = 0; j < Fours.Count(); j++) {
  234. long i = Convert.ToInt64(Fours[j]);
  235.  
  236. // for normal ones
  237. if (n > 0.0 && j != 0) {
  238. Decalculated.Add(i * Convert.ToInt64(Math.Pow(85.0, n - 1)));
  239. n = n - 1.0;
  240. continue;
  241. }
  242.  
  243. // for first and every other first-in-group
  244. if (j % 5 == 0) {
  245. Decalculated.Add(i * 52200625);
  246. n = 4.0;
  247. continue;
  248. }
  249. }
  250.  
  251. Console.WriteLine("\n\nDecalculated:\n{0}\nDecalculated.Count: {1}\n", String.Join(",\n", Decalculated), Decalculated.Count);
  252.  
  253. //for sums
  254. List<long> SumFives = new List<long>();
  255.  
  256. for (int i = 0; i < Decalculated.Count; i++) {
  257. if (i % 5 == 0) {
  258. SumFives.Add(Decalculated[i]);
  259. }
  260.  
  261. else
  262. SumFives[SumFives.Count - 1] += Decalculated[i];
  263. }
  264.  
  265. Console.WriteLine("SumFives:\n{0}", String.Join(",\n", SumFives));
  266.  
  267. //remove amount of bytes, equal to amount of 'u's added to last of Fives, from last group
  268. if (lessThanFive > 0) {
  269. Console.WriteLine("lessThanFive ({0})", lessThanFive);
  270. //result.RemoveRange(result.Count - lessThanFive, lessThanFive);
  271. result.RemoveRange(result.Count - lessThanFive, lessThanFive);
  272. }
  273.  
  274. Console.Write("\nresult:\n[{0}]\nof Count ({1})\n", string.Join(", ", result), result.Count);
  275.  
  276. //
  277. // To represent reuslt in a String format "as expected", you can use this for each byte
  278. //
  279. string ttst = "\\u" + ((byte)255).ToString("x4");
  280. Console.WriteLine(ttst);
  281.  
  282. Console.WriteLine("\nin string format:\n\"{0}\"\nof Length ({1})", System.Text.Encoding.Unicode.GetChars(result.ToArray()), result.ToArray().Length);
  283.  
  284. //return System.Text.Encoding.Unicode.GetChars(result.ToArray());
  285. return result.ToArray();
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement