I need to decode Base64-encoded PNG so that I can use just the width, height, & RGB values of all the pixels manually, i.e. no libraries, for my in-game system. After that I'm pretty sure i can handle the rest. However my implementation of a Base64 decoder seems to not be working.
When I swapped the binary for their ASCII, I got this:
.PNG........R..................I.....a.U..X.......d.O..E..F.........S.+....
from this input:
iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVQIHWNkYGD4DwQwkuE/AEzXCPkbs6OPAAAAAElFTkSuQmCC
There might be something wrong with my implementation, because there's no "IHDR" or "IEND" to be seen. All it is is a loop that substrings the input by index of length 1, uses a lookup table that swaps the character to the corresponding six-digit binary and adds that to a string variable. After three times, it would then split the variable into three bytes via string substring, and use lookup tables for those to return the corresponding ASCII (just so I can see if the result would contain chunks like "IHDR" or "IEND", but I assume you would normally just keep adding to the variable and then reference the data from the string variable with substring of length eight to actually use the data to process as a PNG). My overall guess is that I put the wrong values in the look up tables.
I have two look up tables, where A-Z, a-z, 0-9, & +/ correspond to their index in six bits, so A
= 000000, /
= 111111.
The other is their indexes in the like, "main binary table thing" where A = 65 in base 10 that returns their character, thus 65 in binary would return A.
Are these the correct ways of going about creating the tables?
I've done research but its been pretty confusing since I'm mostly self taught, and especially because no one seems to do anything without using libraries anymore. So far I have been stuck on this for about seven months now, so anything helps.
Please try to teach me, or if you're not going to help, redirect me to more useful media, instead of just taking down my post please. I'm actually trying to learn, and if being dedicated for seven months doesn't prove that, than I don't know what does. Thank you.
The code:
Here's actual C# code I had generated based off the visual code, with a few simplifications like the removal of a int "Save Index" that would select different lists to save into in the visual platform I use, thus not used for the decoding specifically. It adds to the list in chucks of 512 due to some limitations with the platform too.
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
string base64input = "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVQIHWNkYGD4DwQwkuE/AEzXCPkbs6OPAAAAAElFTkSuQmCC";
List<string> decoded512chunks = new List<string>();
string byteCompiler = "";
string asciiCompiler = "";
for (int i = 0; i < base64input.Length; i++)
{
string currentChar = base64input.Substring(i, 1);
byteCompiler += GetBitsFromChar(currentChar);
if (i % 4 == 0)
{
string asciiCompilerCheck = "";
asciiCompilerCheck += GetAsciiFromBinary(byteCompiler.Substring(0, 8)) + ",";
asciiCompilerCheck += GetAsciiFromBinary(byteCompiler.Substring(8, 8)) + ",";
asciiCompilerCheck += GetAsciiFromBinary(byteCompiler.Substring(16, 8)) + (i + 1 >= base64input.Length ? "" : ",");
if (asciiCompilerCheck.Length >= 512)
{
decoded512chunks.Add(asciiCompiler);
asciiCompiler = "";
byteCompiler = "";
}
else
{
asciiCompiler += asciiCompilerCheck;
}
}
}
static string GetBitsFromChar(string character)
{
// Retrieve the 6 bits corresponding to the character based on the input switch case (the "lookup table")
return //(switch case result);
}
static char GetAsciiFromBinary(string binary)
{
// Retrieve the ascii corresponding to the input byte based on the input switch case (the "lookup table")
return //(switch case result);
}
}