Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- public class Ascii85
- {
- // Encoding method
- public static string toAscii85(byte[] data) {
- string result = "<~~>";
- if (data.Length == 0) { return result; }
- Console.WriteLine("data input: [{0}]", String.Join(", ", data));
- Console.WriteLine("data lenght {0}\ndata.Length % 4: {1}", data.Length, data.Length % 4);
- int addCut = data.Length % 4 > 0 ? 4 - data.Length % 4 : 0;
- Console.WriteLine("addCut {0}", addCut);
- // 8-bit-formated string-stored byte array data. PadLeft keeps leading zeroes.
- string[] bits = data.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')).ToArray();
- Console.WriteLine("bits:\n{0}", String.Join("\n", bits));
- Console.WriteLine("bits.Length: {0}", bits.Length);
- // storing 32-bit integers as strings
- List<string> binaryGroup = new List<string>();
- // adds first and every 4-th 8-bit group after to List<string> and appends other 3 elements right to it
- for (int i = 0; i < bits.Length; i++) {
- if (i % 4 == 0) {
- binaryGroup.Add(bits[i]);
- }
- else {
- binaryGroup[binaryGroup.Count - 1] += bits[i];
- }
- }
- if (addCut > 0) {
- int i = addCut;
- //this will run for i + 1 times
- while (i > 0) {
- binaryGroup[binaryGroup.Count - 1] += "00000000";
- Console.WriteLine("Added 00000000");
- i -= 1;
- }
- }
- Console.WriteLine("binaryGroup:\n{0}", String.Join("\n", binaryGroup));
- Console.WriteLine("last length: {0}", binaryGroup[binaryGroup.Count - 1].Length);
- List<byte> bList = new List<byte>();
- // calculating values
- for (int j = 0; j < binaryGroup.Count; j++) {
- long i = Convert.ToInt64(binaryGroup[j], 2);
- // in case of 'z' (four empty bytes)
- if (i == 0 && data.Length > 3) {
- bList.Add(122);
- }
- else {
- bList.Add(Convert.ToByte(i / (85 * 85 * 85 * 85) + 33));
- long a = i / (85 * 85 * 85 * 85);
- bList.Add(Convert.ToByte(i / (85 * 85 * 85) - 85 * a + 33));
- long b = i / (85 * 85 * 85);
- bList.Add(Convert.ToByte(i / (85 * 85) - 85 * b + 33));
- long c = i / (85 * 85);
- bList.Add(Convert.ToByte(i / 85 - 85 * c + 33));
- bList.Add(Convert.ToByte((i % 85) + 33));
- }
- }
- if (addCut > 0) {
- int i = addCut;
- while (i > 0) {
- Console.WriteLine("Removed {0}", bList[bList.Count-1]);
- bList.RemoveAt(bList.Count-1);
- i -= 1;
- }
- }
- result = "<~";
- foreach (var i in bList) {
- result += Convert.ToChar(i);
- }
- result += "~>";
- Console.WriteLine("result:\n{0}", result);
- return result;
- }
- // SECOND ONE
- // Decoding method
- public static byte[] fromAscii85(string data) {
- int lessThanFive = 0;
- Console.WriteLine("raw data:\n{0}", data);
- // removing braces and whitespaces <~ ~>
- string cleared = Regex.Replace(new string((from c in data where !char.IsWhiteSpace(c) select c).ToArray()),
- @"^<~|~>$", ""); //|{|}|\||v|w|x|y|~", "");
- //string cleared = Regex.Replace(new string((from c in data where !char.IsWhiteSpace(c) select c).ToArray()), @"^<~|~>$", "");
- Console.WriteLine("cleared:\n{0}\n", cleared);
- // Forming "groups" of Fives
- string Fives = "";
- for (int i = 0; i < cleared.Length; i++) {
- if (cleared[i] == 'z') { Fives += "!!!!!"; }
- else { Fives += cleared[i]; }
- }
- while (Fives.Length % 5 != 0) {
- Fives += 'u';
- lessThanFive++;
- }
- Console.WriteLine("Fives:");
- // for debug purposes
- List<string> fvs = new List<string>();
- for (int i = 0; i < Fives.Length; i++) {
- if (i % 5 == 0) {
- fvs.Add(Fives[i].ToString());
- }
- else {
- fvs[fvs.Count - 1] += Fives[i];
- }
- }
- foreach (var i in fvs) {
- Console.WriteLine("{0}\t{1}", i, i.Length);
- //Console.WriteLine("Fives: [{0}]", String.Join(", ", Fives));
- }
- //Console.Write("Fives:\n{0}\nLenght is {1}", string.Join("\t", Fives), x => Fives[(int)x]);
- List<byte> Fours = new List<byte>();
- // decalculating them into groups of fours
- foreach (var c in Fives) {
- Fours.Add(Convert.ToByte(c));
- }
- for (int i = 0; i < Fours.Count; i++) {
- Fours[i] -= (byte)33;
- }
- // group items in Fours into groups by 5 in each, multiply them "by 85..." and sum
- List<long> Decalculated = new List<long>(Fours.Count);
- // double because Math.Pow() doesn't take integers
- double n = 4.0;
- // decalculating values to "1,298,230,816"
- for (int j = 0; j < Fours.Count(); j++) {
- long i = Convert.ToInt64(Fours[j]);
- // for normal ones
- if (n > 0.0 && j != 0) {
- Decalculated.Add(i * Convert.ToInt64(Math.Pow(85.0, n - 1)));
- n = n - 1.0;
- continue;
- }
- // for first and every other first-in-group
- if (j % 5 == 0) {
- Decalculated.Add(i * 52200625);
- n = 4.0;
- continue;
- }
- }
- Console.WriteLine("Decalculated:\n[{0}]", String.Join(",\n", Decalculated));
- Console.WriteLine("Decalculated.Count: {0}", Decalculated.Count);
- //for sums
- List<long> SumFives = new List<long>();
- for (int i = 0; i < Decalculated.Count; i++) {
- if (i % 5 == 0) {
- SumFives.Add(Decalculated[i]);
- }
- else
- SumFives[SumFives.Count - 1] += Decalculated[i];
- }
- Console.WriteLine("SumFives:\n[{0}]", String.Join(",\n", SumFives));
- //for byte representation
- List<byte> result = new List<byte>();
- for (int i = 0; i < SumFives.Count; i++) {
- for (int k = 0; k < 4; k++) {
- result.Add(Convert.ToByte(Convert.ToString(SumFives[i], 2).PadLeft(32, '0').Substring(k * 8, 8), 2));
- //result.Add(Convert.ToByte(SumFives[i]));
- }
- }
- //remove amount of bytes, equal to amount of 'u's added to last of Fives, from last group
- if (lessThanFive > 0) {
- Console.WriteLine("lessThanFive ({0})", lessThanFive);
- result.RemoveRange(result.Count - lessThanFive, lessThanFive);
- }
- Console.Write("\nresult:\n[{0}]\n", string.Join(", ", result));
- //Console.WriteLine("of length ({0})", result.Count);
- //Console.WriteLine("\nin string format:\n\"{0}\"", System.Text.Encoding.ASCII.GetString(result.ToArray()));
- return result.ToArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement