Advertisement
Guest User

codewrs version

a guest
Mar 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 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.  
  7. public class Ascii85
  8. {
  9. // Encoding method
  10. public static string toAscii85(byte[] data) {
  11. string result = "<~~>";
  12. if (data.Length == 0) { return result; }
  13.  
  14. Console.WriteLine("data input: [{0}]", String.Join(", ", data));
  15. Console.WriteLine("data lenght {0}\ndata.Length % 4: {1}", data.Length, data.Length % 4);
  16. int addCut = data.Length % 4 > 0 ? 4 - data.Length % 4 : 0;
  17. Console.WriteLine("addCut {0}", addCut);
  18.  
  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.  
  23. Console.WriteLine("bits:\n{0}", String.Join("\n", bits));
  24. Console.WriteLine("bits.Length: {0}", bits.Length);
  25.  
  26. // storing 32-bit integers as strings
  27. List<string> binaryGroup = new List<string>();
  28.  
  29. // adds first and every 4-th 8-bit group after to List<string> and appends other 3 elements right to it
  30. for (int i = 0; i < bits.Length; i++) {
  31. if (i % 4 == 0) {
  32. binaryGroup.Add(bits[i]);
  33. }
  34. else {
  35. binaryGroup[binaryGroup.Count - 1] += bits[i];
  36. }
  37. }
  38.  
  39. if (addCut > 0) {
  40. int i = addCut;
  41. //this will run for i + 1 times
  42. while (i > 0) {
  43. binaryGroup[binaryGroup.Count - 1] += "00000000";
  44. Console.WriteLine("Added 00000000");
  45. i -= 1;
  46. }
  47. }
  48.  
  49. Console.WriteLine("binaryGroup:\n{0}", String.Join("\n", binaryGroup));
  50. Console.WriteLine("last length: {0}", binaryGroup[binaryGroup.Count - 1].Length);
  51.  
  52. List<byte> bList = new List<byte>();
  53.  
  54. // calculating values
  55. for (int j = 0; j < binaryGroup.Count; j++) {
  56. long i = Convert.ToInt64(binaryGroup[j], 2);
  57.  
  58. // in case of 'z' (four empty bytes)
  59. if (i == 0 && data.Length > 3) {
  60. bList.Add(122);
  61. }
  62.  
  63. else {
  64. bList.Add(Convert.ToByte(i / (85 * 85 * 85 * 85) + 33));
  65. long a = i / (85 * 85 * 85 * 85);
  66. bList.Add(Convert.ToByte(i / (85 * 85 * 85) - 85 * a + 33));
  67. long b = i / (85 * 85 * 85);
  68. bList.Add(Convert.ToByte(i / (85 * 85) - 85 * b + 33));
  69. long c = i / (85 * 85);
  70. bList.Add(Convert.ToByte(i / 85 - 85 * c + 33));
  71. bList.Add(Convert.ToByte((i % 85) + 33));
  72. }
  73. }
  74.  
  75. if (addCut > 0) {
  76. int i = addCut;
  77. while (i > 0) {
  78. Console.WriteLine("Removed {0}", bList[bList.Count-1]);
  79. bList.RemoveAt(bList.Count-1);
  80. i -= 1;
  81. }
  82. }
  83. result = "<~";
  84. foreach (var i in bList) {
  85. result += Convert.ToChar(i);
  86. }
  87. result += "~>";
  88.  
  89. Console.WriteLine("result:\n{0}", result);
  90. return result;
  91. }
  92.  
  93. // SECOND ONE
  94.  
  95. // Decoding method
  96. public static byte[] fromAscii85(string data) {
  97. int lessThanFive = 0;
  98.  
  99. Console.WriteLine("raw data:\n{0}", data);
  100.  
  101. // removing braces and whitespaces <~ ~>
  102. string cleared = Regex.Replace(new string((from c in data where !char.IsWhiteSpace(c) select c).ToArray()),
  103. @"^<~|~>$", ""); //|{|}|\||v|w|x|y|~", "");
  104. //string cleared = Regex.Replace(new string((from c in data where !char.IsWhiteSpace(c) select c).ToArray()), @"^<~|~>$", "");
  105. Console.WriteLine("cleared:\n{0}\n", cleared);
  106.  
  107. // Forming "groups" of Fives
  108. string Fives = "";
  109. for (int i = 0; i < cleared.Length; i++) {
  110. if (cleared[i] == 'z') { Fives += "!!!!!"; }
  111. else { Fives += cleared[i]; }
  112. }
  113.  
  114. while (Fives.Length % 5 != 0) {
  115. Fives += 'u';
  116. lessThanFive++;
  117. }
  118.  
  119. Console.WriteLine("Fives:");
  120. // for debug purposes
  121. List<string> fvs = new List<string>();
  122. for (int i = 0; i < Fives.Length; i++) {
  123. if (i % 5 == 0) {
  124. fvs.Add(Fives[i].ToString());
  125. }
  126. else {
  127. fvs[fvs.Count - 1] += Fives[i];
  128. }
  129. }
  130.  
  131. foreach (var i in fvs) {
  132. Console.WriteLine("{0}\t{1}", i, i.Length);
  133. //Console.WriteLine("Fives: [{0}]", String.Join(", ", Fives));
  134.  
  135. }
  136.  
  137. //Console.Write("Fives:\n{0}\nLenght is {1}", string.Join("\t", Fives), x => Fives[(int)x]);
  138.  
  139. List<byte> Fours = new List<byte>();
  140.  
  141. // decalculating them into groups of fours
  142. foreach (var c in Fives) {
  143. Fours.Add(Convert.ToByte(c));
  144. }
  145.  
  146. for (int i = 0; i < Fours.Count; i++) {
  147. Fours[i] -= (byte)33;
  148. }
  149.  
  150. // group items in Fours into groups by 5 in each, multiply them "by 85..." and sum
  151. List<long> Decalculated = new List<long>(Fours.Count);
  152.  
  153. // double because Math.Pow() doesn't take integers
  154. double n = 4.0;
  155.  
  156. // decalculating values to "1,298,230,816"
  157. for (int j = 0; j < Fours.Count(); j++) {
  158. long i = Convert.ToInt64(Fours[j]);
  159.  
  160. // for normal ones
  161. if (n > 0.0 && j != 0) {
  162. Decalculated.Add(i * Convert.ToInt64(Math.Pow(85.0, n - 1)));
  163. n = n - 1.0;
  164. continue;
  165. }
  166.  
  167. // for first and every other first-in-group
  168. if (j % 5 == 0) {
  169. Decalculated.Add(i * 52200625);
  170. n = 4.0;
  171. continue;
  172. }
  173. }
  174.  
  175. Console.WriteLine("Decalculated:\n[{0}]", String.Join(",\n", Decalculated));
  176. Console.WriteLine("Decalculated.Count: {0}", Decalculated.Count);
  177.  
  178. //for sums
  179. List<long> SumFives = new List<long>();
  180.  
  181. for (int i = 0; i < Decalculated.Count; i++) {
  182. if (i % 5 == 0) {
  183. SumFives.Add(Decalculated[i]);
  184. }
  185.  
  186. else
  187. SumFives[SumFives.Count - 1] += Decalculated[i];
  188. }
  189.  
  190. Console.WriteLine("SumFives:\n[{0}]", String.Join(",\n", SumFives));
  191.  
  192. //for byte representation
  193. List<byte> result = new List<byte>();
  194.  
  195. for (int i = 0; i < SumFives.Count; i++) {
  196. for (int k = 0; k < 4; k++) {
  197. result.Add(Convert.ToByte(Convert.ToString(SumFives[i], 2).PadLeft(32, '0').Substring(k * 8, 8), 2));
  198. //result.Add(Convert.ToByte(SumFives[i]));
  199. }
  200. }
  201.  
  202. //remove amount of bytes, equal to amount of 'u's added to last of Fives, from last group
  203. if (lessThanFive > 0) {
  204. Console.WriteLine("lessThanFive ({0})", lessThanFive);
  205. result.RemoveRange(result.Count - lessThanFive, lessThanFive);
  206. }
  207.  
  208. Console.Write("\nresult:\n[{0}]\n", string.Join(", ", result));
  209. //Console.WriteLine("of length ({0})", result.Count);
  210. //Console.WriteLine("\nin string format:\n\"{0}\"", System.Text.Encoding.ASCII.GetString(result.ToArray()));
  211.  
  212. return result.ToArray();
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement