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 = "";
- Console.WriteLine("data.Length: {0}", data.Length);
- if (data.Length == 0) { return "<~~>"; }
- int addCut = data.Length % 4 > 0 ? 4 - data.Length % 4 : 0;
- Console.WriteLine("data input:\n{0}", String.Join(", ", data));
- Console.WriteLine("data lenght {0}\ndata.Length % 4: {1}\naddCut {2}", data.Length, data.Length % 4, 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}\nbits.Length: {1}", String.Join("\n", bits), bits.Length);
- for (int i = 0; i < bits.Length; i++) {
- Console.WriteLine(bits[i] != "00111111" ? bits[i] : "\t\t{0}", bits[i]); // question mark
- }
- // 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] += "01110101"; // 'u'
- Console.WriteLine("Added 01110101");
- i -= 1;
- }
- }
- Console.WriteLine("binaryGroup:\n{0}", String.Join("\n", binaryGroup));
- Console.WriteLine("last length: {0}", binaryGroup[binaryGroup.Count - 1].Length);
- // calculating values
- List<byte> bList = new List<byte>();
- for (int j = 0; j < binaryGroup.Count; j++) {
- long x = Convert.ToInt64(binaryGroup[j], 2);
- // in case of 'z' (four empty bytes)
- if (x == 0 && data.Length > 3) { bList.Add(122); }
- else {
- long a = x / (85 * 85 * 85 * 85);
- long b = x / (85 * 85 * 85);
- long c = x / (85 * 85);
- long d = x / 85;
- bList.Add(Convert.ToByte(a + 33));
- bList.Add(Convert.ToByte(b - 85 * a + 33));
- bList.Add(Convert.ToByte(c - 85 * b + 33));
- bList.Add(Convert.ToByte(d - 85 * c + 33));
- bList.Add(Convert.ToByte((x % 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 += "~>";
- byte[] tmpb = Encoding.Unicode.GetBytes(result);
- Console.Write("tmpb:\n{0}\n", String.Join(", ", tmpb));
- Console.WriteLine("tmpb.Length: {0}", tmpb.Length);
- byte[] tmpb1 = Encoding.UTF8.GetBytes(result);
- Console.Write("tmpbUTF8:\n{0}\n", String.Join(", ", tmpb1));
- Console.WriteLine("tmpb.Length: {0}", tmpb1.Length);
- bool tst = true;
- for (int b = 0; b < tmpb.Length; b++) {
- if (tmpb[b] != tmpb1[b]) {
- Console.Write("tmpb[{0}] ({1}) != tmpb1[{0}] ({2})", b, tmpb[b], tmpb1[b]);
- tst = false;
- break;
- }
- }
- Console.WriteLine("result.Length: {0}\nresult:\n{1}", result.Length, result);
- Console.WriteLine(tst ? "elements are equal" : "not equal");
- return result;
- }
- // SECOND ONE
- // Decoding method
- public static byte[] fromAscii85(string data) {
- int lessThanFive = 0;
- /// index, \uXXXX unicode element value
- Dictionary<int, string> nonascii85 = new Dictionary<int, string>();
- //int[,] unicodes = new int[data.Length, data.Length];
- Console.WriteLine("raw data:\n{0}", data);
- // removes whitespaces, cuts braces
- string cleared = Regex.Replace(new string((from c in data where !char.IsWhiteSpace(c) select c).ToArray()),
- @"^<~|~>$", ""); //|~|{|}|\||v|w|x|y
- Console.WriteLine("\ncleared:\n{0}\n", cleared);
- string clear = "";
- for (int i = 0; i < cleared.Length; i++) {
- // non-ascii85 character
- if (cleared[i] != 'z' && cleared[i] > 117 || cleared[i] < 32) {
- nonascii85.Add(i, "\\u" + Convert.ToInt32(cleared[i]).ToString("x4"));
- clear += 'y';
- }
- else {
- if (cleared[i] == 'z') { clear += "!!!!!"; }
- else { clear += cleared[i]; }
- }
- }
- Console.WriteLine("nonascii85:");
- foreach (var i in nonascii85) {
- Console.WriteLine("{0}\t{1}", i.Key, i.Value);
- }
- while (clear.Length % 5 != 0) {
- clear += 'u';
- }
- #region log of clear string
- Console.WriteLine("\nclear:");
- for (int i = 0; i < clear.Length; i++) {
- if (clear[i] != 'z' && clear[i] > 117 || clear[i] < 33) {
- Console.BackgroundColor = ConsoleColor.DarkGreen;
- Console.Write(clear[i]);
- Console.ResetColor();
- }
- else { Console.Write(clear[i]); }
- }
- Console.WriteLine("\nclear.length: {0}", clear.Length);
- Console.Write("\n===fives:===");
- for (var i = 0; i < clear.Length; i++) {
- if (clear[i] != 'z' && clear[i] > 117 || clear[i] < 33) {
- Console.BackgroundColor = ConsoleColor.DarkGreen;
- if (i % 5 == 0) { Console.Write("\n" + clear[i]); }
- else { Console.Write(clear[i]); }
- Console.ResetColor();
- }
- else {
- if (i % 5 == 0) { Console.Write("\n" + clear[i]); }
- else { Console.Write(clear[i]); }
- }
- }
- #endregion
- // 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++;
- }
- List<long> calculated = new List<long>();
- foreach (var c in clear) {
- calculated.Add(Convert.ToInt64(c) - 33);
- //calculated.Add(c != 'y' ? Convert.ToInt64(c) - 33 : 121); // 'y' == 121
- }
- Console.Write("\n\ncalculated:\n{0}", String.Join(", ", calculated));
- // double because Math.Pow() doesn't take integers
- double n = 4.0;
- List<long> sums = new List<long>();
- for (int i = 0; i < calculated.Count; i++) {
- if (i % 5 == 0) {
- sums.Add(calculated[i] * 52200625);
- n = 4.0;
- }
- else {
- sums[sums.Count - 1] += calculated[i] * Convert.ToInt64(Math.Pow(85.0, n - 1));
- n = n - 1.0;
- }
- }
- Console.WriteLine("\n\nsums:");
- Console.Write("{0}", String.Join(", ", sums));
- List<byte> result = new List<byte>();
- foreach (var s in sums) {
- for (int k = 0; k < 4; k++) {
- result.Add(Convert.ToByte(Convert.ToString(s, 2).PadLeft(32, '0').Substring(k * 8, 8), 2));
- }
- }
- // decalculating them into groups of fours
- List<byte> Fours = new List<byte>();
- 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);
- // 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("\n\nDecalculated:\n{0}\nDecalculated.Count: {1}\n", String.Join(",\n", Decalculated), 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));
- //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);
- result.RemoveRange(result.Count - lessThanFive, lessThanFive);
- }
- Console.Write("\nresult:\n[{0}]\nof Count ({1})\n", string.Join(", ", result), result.Count);
- //
- // To represent reuslt in a String format "as expected", you can use this for each byte
- //
- string ttst = "\\u" + ((byte)255).ToString("x4");
- Console.WriteLine(ttst);
- Console.WriteLine("\nin string format:\n\"{0}\"\nof Length ({1})", System.Text.Encoding.Unicode.GetChars(result.ToArray()), result.ToArray().Length);
- //return System.Text.Encoding.Unicode.GetChars(result.ToArray());
- return result.ToArray();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement