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.Threading.Tasks;
- using System.Numerics;
- namespace LibraryOfBabel.Babel
- {
- #region Location Struct
- public struct Location
- {
- public BigInteger Cluster;
- public BigInteger Galaxy;
- public BigInteger SolarSystem;
- public BigInteger Planet;
- public BigInteger Country;
- public BigInteger City;
- public BigInteger Street;
- public BigInteger Building;
- public BigInteger Wing;
- public BigInteger Room;
- public BigInteger Wall;
- public BigInteger Shelf;
- public BigInteger Volume;
- public BigInteger Page;
- public BigInteger Paragraph;
- }
- #endregion
- public class BabelLibrary
- {
- #region Constants
- private BigInteger maxClusters = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxGalaxies = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxSolarSystems = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxPlanets = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxCountries = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxCities = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxStreets = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxBuildings = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxWings = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxRooms = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxWalls = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxShelfs = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxVolumes = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxPages = BigInteger.Parse("1000000000000000000000000000000000000000");
- private BigInteger maxParagraphs = BigInteger.Parse("1000000000000000000000000000000000000000");
- #endregion
- #region Properties
- public BigInteger Cluster { get; set; }
- public BigInteger Galaxy { get; set; }
- public BigInteger SolarSystem { get; set; }
- public BigInteger Planet { get; set; }
- public BigInteger Country { get; set; }
- public BigInteger City { get; set; }
- public BigInteger Street { get; set; }
- public BigInteger Building { get; set; }
- public BigInteger Wing { get; set; }
- public BigInteger Room { get; set; }
- public BigInteger Wall { get; set; }
- public BigInteger Shelf { get; set; }
- public BigInteger Volume { get; set; }
- public BigInteger Page { get; set; }
- public BigInteger Paragraph { get; set; }
- #endregion
- #region Encode/Decode Methods
- public Location BabelEncode(byte[] byteArray)
- {
- string bytes = ConvertToBase64(byteArray);
- return RetrieveCoordsOfB64String(Encoding.ASCII.GetBytes(bytes));
- }
- public byte[] BabelDecode()
- {
- byte[] location = RetrieveB64StringFromCoords();
- return ConvertFromBase64(Encoding.ASCII.GetString(location));
- }
- #endregion
- #region Convert to Base64
- private string ConvertToBase64(byte[] bigIntArray)
- {
- return System.Convert.ToBase64String(bigIntArray);
- }
- #endregion
- #region Convert from Base64
- private byte[] ConvertFromBase64(string base64String)
- {
- return System.Convert.FromBase64String(base64String);
- }
- #endregion
- #region Retrieve Coords (Encode Stage)
- private Location RetrieveCoordsOfB64String(byte[] byteArray)
- {
- BigInteger bint = new BigInteger(byteArray.Concat(new byte[] { 0 }).ToArray()); //Append Positive Value
- BigInteger runningTotal = bint;
- BigInteger clusterUnits = BigInteger.Parse("0");
- BigInteger galaxyUnits = BigInteger.Parse("0");
- BigInteger solarsystemUnits = BigInteger.Parse("0");
- BigInteger planetUnits = BigInteger.Parse("0");
- BigInteger countryUnits = BigInteger.Parse("0");
- BigInteger cityUnits = BigInteger.Parse("0");
- BigInteger streetUnits = BigInteger.Parse("0");
- BigInteger buildingUnits = BigInteger.Parse("0");
- BigInteger wingUnits = BigInteger.Parse("0");
- BigInteger roomUnits = BigInteger.Parse("0");
- BigInteger wallUnits = BigInteger.Parse("0");
- BigInteger shelfUnits = BigInteger.Parse("0");
- BigInteger volumeUnits = BigInteger.Parse("0");
- BigInteger pageUnits = BigInteger.Parse("0");
- BigInteger paragraphUnits = BigInteger.Parse("0");
- if (runningTotal > 0)
- {
- BigInteger clusterMagnitude = maxGalaxies * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- clusterUnits = runningTotal / clusterMagnitude;
- runningTotal = runningTotal % clusterMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger galaxyMagnitude = maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- galaxyUnits = runningTotal / galaxyMagnitude;
- runningTotal = runningTotal % galaxyMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger solarSystemMagnitude = maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- solarsystemUnits = runningTotal / solarSystemMagnitude;
- runningTotal = runningTotal % solarSystemMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger planetMagnitude = maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- planetUnits = runningTotal / planetMagnitude;
- runningTotal = runningTotal % planetMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger countryMagnitude = maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- countryUnits = runningTotal / countryMagnitude;
- runningTotal = runningTotal % countryMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger cityMagnitude = maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- cityUnits = runningTotal / cityMagnitude;
- runningTotal = runningTotal % cityMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger streetMagnitude = maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- streetUnits = runningTotal / streetMagnitude;
- runningTotal = runningTotal % streetMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger buildingMagnitude = maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- buildingUnits = runningTotal / buildingMagnitude;
- runningTotal = runningTotal % buildingMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger wingMagnitude = maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- wingUnits = runningTotal / wingMagnitude;
- runningTotal = runningTotal % wingMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger roomMagnitude = maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- roomUnits = runningTotal / roomMagnitude;
- runningTotal = runningTotal % roomMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger wallMagnitude = maxShelfs * maxVolumes * maxPages * maxParagraphs;
- wallUnits = runningTotal / wallMagnitude;
- runningTotal = runningTotal % wallMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger shelfMagnitude = maxVolumes * maxPages * maxParagraphs;
- shelfUnits = runningTotal / shelfMagnitude;
- runningTotal = runningTotal % shelfMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger volumeMagnitude = maxPages * maxParagraphs;
- volumeUnits = runningTotal / volumeMagnitude;
- runningTotal = runningTotal % volumeMagnitude;
- }
- if (runningTotal > 0)
- {
- BigInteger pageMagnitude = maxParagraphs;
- pageUnits = runningTotal / pageMagnitude;
- runningTotal = runningTotal % pageMagnitude;
- }
- if (runningTotal > 0)
- paragraphUnits = runningTotal;
- Location location = new Location();
- location.Cluster = clusterUnits;
- location.Galaxy = galaxyUnits;
- location.SolarSystem = solarsystemUnits;
- location.Planet = planetUnits;
- location.Country = countryUnits;
- location.City = cityUnits;
- location.Street = streetUnits;
- location.Building = buildingUnits;
- location.Wing = wingUnits;
- location.Room = roomUnits;
- location.Wall = wallUnits;
- location.Shelf = shelfUnits;
- location.Volume = volumeUnits;
- location.Page = pageUnits;
- location.Paragraph = paragraphUnits;
- return location;
- }
- #endregion
- #region Retrieve B64 String (Decode Stage)
- private byte[] RetrieveB64StringFromCoords()
- {
- BigInteger pageUnits = Page * maxParagraphs;
- BigInteger volumeUnits = Volume * maxPages * maxParagraphs;
- BigInteger ShelfUnits = Shelf * maxVolumes * maxPages * maxParagraphs;
- BigInteger wallUnits = Wall * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger roomUnits = Room * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger wingUnits = Wing * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger buildingUnits = Building * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger streetUnits = Street * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger cityUnits = City * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger countryUnits = Country * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger planetUnits = Planet * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger solarsystemUnits = SolarSystem * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger galaxyUnits = Galaxy * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger clusterUnits = Cluster * maxGalaxies * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
- BigInteger bint = Paragraph + pageUnits + volumeUnits + ShelfUnits + wallUnits + roomUnits + wingUnits + buildingUnits + streetUnits + cityUnits + countryUnits + planetUnits + solarsystemUnits + galaxyUnits + clusterUnits;
- byte[] bar = bint.ToByteArray();
- if (bar.Length == 8193)
- {
- byte[] newbar = new byte[bar.Length - 1];
- Array.Copy(bar, newbar, newbar.Length);
- return newbar;
- }
- else
- return bar;
- }
- #endregion
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using LibraryOfBabel.Babel;
- using System.Numerics;
- namespace LibraryOfBabel.BabelEncoding
- {
- public class BabelEncoder
- {
- #region Babel Encoder
- public void Encode(string inputFileName, string outputFileName)
- {
- using (FileStream inputFS = new FileStream(inputFileName, FileMode.Open))
- {
- int currentInputByte = 0;
- int currentOutputByte = 0;
- BabelLibrary bl = new BabelLibrary();
- while(currentInputByte < inputFS.Length)
- {
- byte[] inputFile = new byte[8192];
- inputFS.Seek(currentInputByte, SeekOrigin.Begin);
- inputFS.Read(inputFile, 0, inputFile.Length);
- Location location = bl.BabelEncode(inputFile);
- currentInputByte += inputFile.Length;
- string output = null;
- if(currentInputByte < inputFS.Length)
- output = location.Cluster.ToString() + ":" + location.Galaxy.ToString() + ":" + location.SolarSystem.ToString() + ":" + location.Planet.ToString() + ":" + location.Country.ToString() + ":" + location.City.ToString() + ":" + location.Street.ToString() + ":" + location.Building.ToString() + ":" + location.Wing.ToString() + ":" + location.Room.ToString() + ":" + location.Wall.ToString() + ":" + location.Shelf.ToString() + ":" + location.Volume.ToString() + ":" + location.Page.ToString() + ":" + location.Paragraph.ToString() + ",";
- else
- output = location.Cluster.ToString() + ":" + location.Galaxy.ToString() + ":" + location.SolarSystem.ToString() + ":" + location.Planet.ToString() + ":" + location.Country.ToString() + ":" + location.City.ToString() + ":" + location.Street.ToString() + ":" + location.Building.ToString() + ":" + location.Wing.ToString() + ":" + location.Room.ToString() + ":" + location.Wall.ToString() + ":" + location.Shelf.ToString() + ":" + location.Volume.ToString() + ":" + location.Page.ToString() + ":" + location.Paragraph.ToString();
- using (FileStream outputFS = new FileStream(outputFileName, FileMode.Append))
- {
- byte[] outputFile = System.Text.Encoding.ASCII.GetBytes(output);
- inputFS.Seek(currentOutputByte, SeekOrigin.Begin);
- outputFS.Write(outputFile, 0, outputFile.Length);
- currentOutputByte += outputFile.Length;
- }
- }
- }
- }
- #endregion
- #region Babel Decoder
- public void Decode(string inputFileName, string outputFileName)
- {
- List<int> babelBlocks = new List<int>();
- long eof = 0;
- int currentByte = 0;
- int blockcount = 0;
- //Scan for comma separators as we don't know the size of the babelblocks
- using (FileStream reader = new FileStream(inputFileName, FileMode.Open))
- {
- while (currentByte < reader.Length)
- {
- byte[] temp = null;
- if (reader.Length - currentByte >= 8191)
- temp = new byte[8192];
- else
- temp = new byte[reader.Length - currentByte];
- reader.Seek(currentByte, SeekOrigin.Begin);
- reader.Read(temp, 0, temp.Length);
- currentByte += temp.Length;
- for (int i = 0; i < temp.Length; i++)
- {
- if (temp[i] == 44)
- babelBlocks.Add(i + blockcount);
- }
- blockcount += temp.Length;
- }
- eof = reader.Length - 1;
- }
- //Grab each BabelBlock
- using (FileStream reader = new FileStream(inputFileName, FileMode.Open))
- {
- currentByte = 0;
- int index = 0;
- int currentOutputByte = 0;
- while (currentByte < reader.Length)
- {
- byte[] temp;
- if(index < babelBlocks.Count())
- temp = new byte[babelBlocks.ElementAt(index) - currentByte];
- else
- temp = new byte[reader.Length - 1 - currentByte];
- reader.Seek(currentByte, SeekOrigin.Begin);
- reader.Read(temp, 0, temp.Length);
- currentByte += temp.Length + 1;
- index++;
- string[] location = (Encoding.ASCII.GetString(temp, 0, temp.Length)).Split(':');
- BabelLibrary bl = new BabelLibrary();
- bl.Cluster = BigInteger.Parse(location[0]);
- bl.Galaxy = BigInteger.Parse(location[1]);
- bl.SolarSystem = BigInteger.Parse(location[2]);
- bl.Planet = BigInteger.Parse(location[3]);
- bl.Country = BigInteger.Parse(location[4]);
- bl.City = BigInteger.Parse(location[5]);
- bl.Street = BigInteger.Parse(location[6]);
- bl.Building = BigInteger.Parse(location[7]);
- bl.Wing = BigInteger.Parse(location[8]);
- bl.Room = BigInteger.Parse(location[9]);
- bl.Wall = BigInteger.Parse(location[10]);
- bl.Shelf = BigInteger.Parse(location[11]);
- bl.Volume = BigInteger.Parse(location[12]);
- bl.Page = BigInteger.Parse(location[13]);
- bl.Paragraph = BigInteger.Parse(location[14]);
- byte[] newFile = bl.BabelDecode();
- using (var stream = new FileStream(outputFileName, FileMode.Append))
- {
- stream.Seek(currentOutputByte, SeekOrigin.Begin);
- stream.Write(newFile, 0, newFile.Length);
- currentOutputByte += newFile.Length;
- }
- }
- }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement