Advertisement
Guest User

Library of Babel Example Variant To Teach Principles

a guest
Jan 17th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Numerics;
  7.  
  8. namespace LibraryOfBabel.Babel
  9. {
  10.     #region Location Struct
  11.  
  12.     public struct Location
  13.     {
  14.         public BigInteger Cluster;
  15.         public BigInteger Galaxy;
  16.         public BigInteger SolarSystem;
  17.         public BigInteger Planet;
  18.         public BigInteger Country;
  19.         public BigInteger City;
  20.         public BigInteger Street;
  21.         public BigInteger Building;
  22.         public BigInteger Wing;
  23.         public BigInteger Room;
  24.         public BigInteger Wall;
  25.         public BigInteger Shelf;
  26.         public BigInteger Volume;
  27.         public BigInteger Page;
  28.         public BigInteger Paragraph;
  29.     }
  30.  
  31.     #endregion
  32.  
  33.     public class BabelLibrary
  34.     {
  35.         #region Constants
  36.  
  37.         private BigInteger maxClusters = BigInteger.Parse("1000000000000000000000000000000000000000");
  38.         private BigInteger maxGalaxies = BigInteger.Parse("1000000000000000000000000000000000000000");
  39.         private BigInteger maxSolarSystems = BigInteger.Parse("1000000000000000000000000000000000000000");
  40.         private BigInteger maxPlanets = BigInteger.Parse("1000000000000000000000000000000000000000");
  41.         private BigInteger maxCountries = BigInteger.Parse("1000000000000000000000000000000000000000");
  42.         private BigInteger maxCities = BigInteger.Parse("1000000000000000000000000000000000000000");
  43.         private BigInteger maxStreets = BigInteger.Parse("1000000000000000000000000000000000000000");
  44.         private BigInteger maxBuildings = BigInteger.Parse("1000000000000000000000000000000000000000");
  45.         private BigInteger maxWings = BigInteger.Parse("1000000000000000000000000000000000000000");
  46.         private BigInteger maxRooms = BigInteger.Parse("1000000000000000000000000000000000000000");
  47.         private BigInteger maxWalls = BigInteger.Parse("1000000000000000000000000000000000000000");
  48.         private BigInteger maxShelfs = BigInteger.Parse("1000000000000000000000000000000000000000");
  49.         private BigInteger maxVolumes = BigInteger.Parse("1000000000000000000000000000000000000000");
  50.         private BigInteger maxPages = BigInteger.Parse("1000000000000000000000000000000000000000");
  51.         private BigInteger maxParagraphs = BigInteger.Parse("1000000000000000000000000000000000000000");
  52.  
  53.         #endregion
  54.  
  55.         #region Properties
  56.  
  57.         public BigInteger Cluster { get; set; }
  58.         public BigInteger Galaxy { get; set; }
  59.         public BigInteger SolarSystem { get; set; }
  60.         public BigInteger Planet { get; set; }
  61.         public BigInteger Country { get; set; }
  62.         public BigInteger City { get; set; }
  63.         public BigInteger Street { get; set; }
  64.         public BigInteger Building { get; set; }
  65.         public BigInteger Wing { get; set; }
  66.         public BigInteger Room { get; set; }
  67.         public BigInteger Wall { get; set; }
  68.         public BigInteger Shelf { get; set; }
  69.         public BigInteger Volume { get; set; }
  70.         public BigInteger Page { get; set; }
  71.         public BigInteger Paragraph { get; set; }
  72.  
  73.         #endregion
  74.  
  75.         #region Encode/Decode Methods
  76.  
  77.         public Location BabelEncode(byte[] byteArray)
  78.         {
  79.             string bytes = ConvertToBase64(byteArray);
  80.             return RetrieveCoordsOfB64String(Encoding.ASCII.GetBytes(bytes));
  81.         }
  82.  
  83.         public byte[] BabelDecode()
  84.         {
  85.             byte[] location = RetrieveB64StringFromCoords();
  86.             return ConvertFromBase64(Encoding.ASCII.GetString(location));
  87.         }
  88.  
  89.         #endregion
  90.  
  91.         #region Convert to Base64
  92.  
  93.         private string ConvertToBase64(byte[] bigIntArray)
  94.         {
  95.             return System.Convert.ToBase64String(bigIntArray);
  96.         }
  97.  
  98.         #endregion
  99.  
  100.         #region Convert from Base64
  101.  
  102.         private byte[] ConvertFromBase64(string base64String)
  103.         {
  104.             return System.Convert.FromBase64String(base64String);
  105.         }
  106.  
  107.         #endregion
  108.  
  109.         #region Retrieve Coords (Encode Stage)
  110.  
  111.         private Location RetrieveCoordsOfB64String(byte[] byteArray)
  112.         {
  113.             BigInteger bint = new BigInteger(byteArray.Concat(new byte[] { 0 }).ToArray());  //Append Positive Value
  114.  
  115.             BigInteger runningTotal = bint;
  116.             BigInteger clusterUnits = BigInteger.Parse("0");
  117.             BigInteger galaxyUnits = BigInteger.Parse("0");
  118.             BigInteger solarsystemUnits = BigInteger.Parse("0");
  119.             BigInteger planetUnits = BigInteger.Parse("0");
  120.             BigInteger countryUnits = BigInteger.Parse("0");
  121.             BigInteger cityUnits = BigInteger.Parse("0");
  122.             BigInteger streetUnits = BigInteger.Parse("0");
  123.             BigInteger buildingUnits = BigInteger.Parse("0");
  124.             BigInteger wingUnits = BigInteger.Parse("0");
  125.             BigInteger roomUnits = BigInteger.Parse("0");
  126.             BigInteger wallUnits = BigInteger.Parse("0");
  127.             BigInteger shelfUnits = BigInteger.Parse("0");
  128.             BigInteger volumeUnits = BigInteger.Parse("0");
  129.             BigInteger pageUnits = BigInteger.Parse("0");
  130.             BigInteger paragraphUnits = BigInteger.Parse("0");
  131.  
  132.             if (runningTotal > 0)
  133.             {
  134.                 BigInteger clusterMagnitude = maxGalaxies * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  135.                 clusterUnits = runningTotal / clusterMagnitude;
  136.                 runningTotal = runningTotal % clusterMagnitude;
  137.             }
  138.  
  139.             if (runningTotal > 0)
  140.             {
  141.                 BigInteger galaxyMagnitude = maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  142.                 galaxyUnits = runningTotal / galaxyMagnitude;
  143.                 runningTotal = runningTotal % galaxyMagnitude;
  144.             }
  145.  
  146.             if (runningTotal > 0)
  147.             {
  148.                 BigInteger solarSystemMagnitude = maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  149.                 solarsystemUnits = runningTotal / solarSystemMagnitude;
  150.                 runningTotal = runningTotal % solarSystemMagnitude;
  151.             }
  152.  
  153.             if (runningTotal > 0)
  154.             {
  155.                 BigInteger planetMagnitude = maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  156.                 planetUnits = runningTotal / planetMagnitude;
  157.                 runningTotal = runningTotal % planetMagnitude;
  158.             }
  159.  
  160.             if (runningTotal > 0)
  161.             {
  162.                 BigInteger countryMagnitude = maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  163.                 countryUnits = runningTotal / countryMagnitude;
  164.                 runningTotal = runningTotal % countryMagnitude;
  165.             }
  166.  
  167.             if (runningTotal > 0)
  168.             {
  169.                 BigInteger cityMagnitude = maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  170.                 cityUnits = runningTotal / cityMagnitude;
  171.                 runningTotal = runningTotal % cityMagnitude;
  172.             }
  173.  
  174.             if (runningTotal > 0)
  175.             {
  176.                 BigInteger streetMagnitude = maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  177.                 streetUnits = runningTotal / streetMagnitude;
  178.                 runningTotal = runningTotal % streetMagnitude;
  179.             }
  180.  
  181.             if (runningTotal > 0)
  182.             {
  183.                 BigInteger buildingMagnitude = maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  184.                 buildingUnits = runningTotal / buildingMagnitude;
  185.                 runningTotal = runningTotal % buildingMagnitude;
  186.             }
  187.  
  188.             if (runningTotal > 0)
  189.             {
  190.                 BigInteger wingMagnitude = maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  191.                 wingUnits = runningTotal / wingMagnitude;
  192.                 runningTotal = runningTotal % wingMagnitude;
  193.             }
  194.  
  195.             if (runningTotal > 0)
  196.             {
  197.                 BigInteger roomMagnitude = maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  198.                 roomUnits = runningTotal / roomMagnitude;
  199.                 runningTotal = runningTotal % roomMagnitude;
  200.             }
  201.  
  202.             if (runningTotal > 0)
  203.             {
  204.                 BigInteger wallMagnitude = maxShelfs * maxVolumes * maxPages * maxParagraphs;
  205.                 wallUnits = runningTotal / wallMagnitude;
  206.                 runningTotal = runningTotal % wallMagnitude;
  207.             }
  208.  
  209.             if (runningTotal > 0)
  210.             {
  211.                 BigInteger shelfMagnitude = maxVolumes * maxPages * maxParagraphs;
  212.                 shelfUnits = runningTotal / shelfMagnitude;
  213.                 runningTotal = runningTotal % shelfMagnitude;
  214.             }
  215.  
  216.             if (runningTotal > 0)
  217.             {
  218.                 BigInteger volumeMagnitude = maxPages * maxParagraphs;
  219.                 volumeUnits = runningTotal / volumeMagnitude;
  220.                 runningTotal = runningTotal % volumeMagnitude;
  221.             }
  222.  
  223.             if (runningTotal > 0)
  224.             {
  225.                 BigInteger pageMagnitude = maxParagraphs;
  226.                 pageUnits = runningTotal / pageMagnitude;
  227.                 runningTotal = runningTotal % pageMagnitude;
  228.             }
  229.  
  230.             if (runningTotal > 0)
  231.                 paragraphUnits = runningTotal;
  232.  
  233.             Location location = new Location();
  234.             location.Cluster = clusterUnits;
  235.             location.Galaxy = galaxyUnits;
  236.             location.SolarSystem = solarsystemUnits;
  237.             location.Planet = planetUnits;
  238.             location.Country = countryUnits;
  239.             location.City = cityUnits;
  240.             location.Street = streetUnits;
  241.             location.Building = buildingUnits;
  242.             location.Wing = wingUnits;
  243.             location.Room = roomUnits;
  244.             location.Wall = wallUnits;
  245.             location.Shelf = shelfUnits;
  246.             location.Volume = volumeUnits;
  247.             location.Page = pageUnits;
  248.             location.Paragraph = paragraphUnits;
  249.  
  250.             return location;
  251.         }
  252.  
  253.         #endregion
  254.  
  255.         #region Retrieve B64 String (Decode Stage)
  256.  
  257.         private byte[] RetrieveB64StringFromCoords()
  258.         {
  259.             BigInteger pageUnits = Page * maxParagraphs;
  260.             BigInteger volumeUnits = Volume * maxPages * maxParagraphs;
  261.             BigInteger ShelfUnits = Shelf * maxVolumes * maxPages * maxParagraphs;
  262.             BigInteger wallUnits = Wall * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  263.             BigInteger roomUnits = Room * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  264.             BigInteger wingUnits = Wing * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  265.             BigInteger buildingUnits = Building * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  266.             BigInteger streetUnits = Street * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  267.             BigInteger cityUnits = City * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  268.             BigInteger countryUnits = Country * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  269.             BigInteger planetUnits = Planet * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  270.             BigInteger solarsystemUnits = SolarSystem * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  271.             BigInteger galaxyUnits = Galaxy * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  272.             BigInteger clusterUnits = Cluster * maxGalaxies * maxSolarSystems * maxPlanets * maxCountries * maxCities * maxStreets * maxBuildings * maxWings * maxRooms * maxWalls * maxShelfs * maxVolumes * maxPages * maxParagraphs;
  273.  
  274.             BigInteger bint = Paragraph + pageUnits + volumeUnits + ShelfUnits + wallUnits + roomUnits + wingUnits + buildingUnits + streetUnits + cityUnits + countryUnits + planetUnits + solarsystemUnits + galaxyUnits + clusterUnits;
  275.  
  276.             byte[] bar = bint.ToByteArray();
  277.  
  278.             if (bar.Length == 8193)
  279.             {
  280.                 byte[] newbar = new byte[bar.Length - 1];
  281.                 Array.Copy(bar, newbar, newbar.Length);
  282.                 return newbar;
  283.             }
  284.             else
  285.                 return bar;
  286.         }
  287.  
  288.         #endregion
  289.     }
  290. }
  291.  
  292.  
  293.  
  294. using System;
  295. using System.Collections.Generic;
  296. using System.Linq;
  297. using System.Text;
  298. using System.Threading.Tasks;
  299. using System.IO;
  300. using LibraryOfBabel.Babel;
  301. using System.Numerics;
  302.  
  303. namespace LibraryOfBabel.BabelEncoding
  304. {
  305.     public class BabelEncoder
  306.     {
  307.         #region Babel Encoder
  308.  
  309.         public void Encode(string inputFileName, string outputFileName)
  310.         {
  311.             using (FileStream inputFS = new FileStream(inputFileName, FileMode.Open))
  312.             {
  313.                 int currentInputByte = 0;
  314.                 int currentOutputByte = 0;
  315.                 BabelLibrary bl = new BabelLibrary();
  316.  
  317.                 while(currentInputByte < inputFS.Length)
  318.                 {
  319.                     byte[] inputFile = new byte[8192];
  320.                     inputFS.Seek(currentInputByte, SeekOrigin.Begin);
  321.                     inputFS.Read(inputFile, 0, inputFile.Length);
  322.                     Location location = bl.BabelEncode(inputFile);
  323.                     currentInputByte += inputFile.Length;
  324.  
  325.                     string output = null;
  326.                    
  327.                     if(currentInputByte < inputFS.Length)
  328.                         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() + ",";
  329.                     else
  330.                         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();
  331.  
  332.                     using (FileStream outputFS = new FileStream(outputFileName, FileMode.Append))
  333.                     {
  334.                         byte[] outputFile = System.Text.Encoding.ASCII.GetBytes(output);
  335.                         inputFS.Seek(currentOutputByte, SeekOrigin.Begin);
  336.                         outputFS.Write(outputFile, 0, outputFile.Length);
  337.                         currentOutputByte += outputFile.Length;
  338.                     }
  339.                 }
  340.             }
  341.         }
  342.  
  343.         #endregion
  344.  
  345.         #region Babel Decoder
  346.  
  347.         public void Decode(string inputFileName, string outputFileName)
  348.         {
  349.             List<int> babelBlocks = new List<int>();
  350.             long eof = 0;
  351.             int currentByte = 0;
  352.             int blockcount = 0;
  353.  
  354.             //Scan for comma separators as we don't know the size of the babelblocks
  355.             using (FileStream reader = new FileStream(inputFileName, FileMode.Open))
  356.             {
  357.                 while (currentByte < reader.Length)
  358.                 {
  359.                     byte[] temp = null;
  360.  
  361.                     if (reader.Length - currentByte >= 8191)
  362.                         temp = new byte[8192];
  363.                     else
  364.                         temp = new byte[reader.Length - currentByte];
  365.  
  366.                     reader.Seek(currentByte, SeekOrigin.Begin);
  367.                     reader.Read(temp, 0, temp.Length);
  368.                     currentByte += temp.Length;
  369.                    
  370.  
  371.                     for (int i = 0; i < temp.Length; i++)
  372.                     {
  373.                         if (temp[i] == 44)
  374.                             babelBlocks.Add(i + blockcount);
  375.                     }
  376.  
  377.                     blockcount += temp.Length;
  378.                 }
  379.                 eof = reader.Length - 1;
  380.             }
  381.  
  382.             //Grab each BabelBlock
  383.             using (FileStream reader = new FileStream(inputFileName, FileMode.Open))
  384.             {
  385.                 currentByte = 0;
  386.                 int index = 0;
  387.                 int currentOutputByte = 0;
  388.  
  389.                 while (currentByte < reader.Length)
  390.                 {
  391.                     byte[] temp;
  392.  
  393.                     if(index < babelBlocks.Count())
  394.                         temp = new byte[babelBlocks.ElementAt(index) - currentByte];
  395.                     else
  396.                         temp = new byte[reader.Length - 1 - currentByte];
  397.  
  398.                     reader.Seek(currentByte, SeekOrigin.Begin);
  399.                     reader.Read(temp, 0, temp.Length);
  400.  
  401.                     currentByte += temp.Length + 1;
  402.                     index++;
  403.                     string[] location = (Encoding.ASCII.GetString(temp, 0, temp.Length)).Split(':');
  404.  
  405.                     BabelLibrary bl = new BabelLibrary();
  406.  
  407.                     bl.Cluster = BigInteger.Parse(location[0]);
  408.                     bl.Galaxy = BigInteger.Parse(location[1]);
  409.                     bl.SolarSystem = BigInteger.Parse(location[2]);
  410.                     bl.Planet = BigInteger.Parse(location[3]);
  411.                     bl.Country = BigInteger.Parse(location[4]);
  412.                     bl.City = BigInteger.Parse(location[5]);
  413.                     bl.Street = BigInteger.Parse(location[6]);
  414.                     bl.Building = BigInteger.Parse(location[7]);
  415.                     bl.Wing = BigInteger.Parse(location[8]);
  416.                     bl.Room = BigInteger.Parse(location[9]);
  417.                     bl.Wall = BigInteger.Parse(location[10]);
  418.                     bl.Shelf = BigInteger.Parse(location[11]);
  419.                     bl.Volume = BigInteger.Parse(location[12]);
  420.                     bl.Page = BigInteger.Parse(location[13]);
  421.                     bl.Paragraph = BigInteger.Parse(location[14]);
  422.                    
  423.                     byte[] newFile = bl.BabelDecode();
  424.  
  425.                     using (var stream = new FileStream(outputFileName, FileMode.Append))
  426.                     {
  427.                         stream.Seek(currentOutputByte, SeekOrigin.Begin);
  428.                         stream.Write(newFile, 0, newFile.Length);
  429.                         currentOutputByte += newFile.Length;
  430.                     }
  431.                 }
  432.             }
  433.         }
  434.  
  435.         #endregion
  436.     }
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement