0

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:

visual 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);
}

}

Generated text form

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
T55
  • 11
  • 4
  • What would prove your dedication is putting the source code of what you tried that didn't work in the question. – Mark Adler Jun 27 '23 at 23:38
  • You could also say what language you are using, and add that as a tag. – Mark Adler Jun 27 '23 at 23:46
  • @MarkAdler The language is C# as stated in the title. At least a node based version of it like blender. Its inside a game which is why i cant just use libraries or anything like that. Ik the last msg was kinda passive aggressive but thats bc this is my 3rd post on this as the others got taken down & never gave constructive feedback & this site is kinda notorious for toxicity, so figure id have to call it out or sumn of that nature so i actually get some genuine feedback – T55 Jun 28 '23 at 01:14
  • Don't put an image of code in the question. Put the code in the question, so it can be copied and pasted. – Mark Adler Jul 02 '23 at 06:42
  • it told me something about indenting and stuff, so i just put it as a image. Remember, it might generally not work very well because its a generation, i dont know how to write some of it. Im pretty sure its supposed to be i >=, not i < for example. ill add it here tho: – T55 Jul 02 '23 at 06:47
  • im sorry, i just dont understand y it wouldnt work just copying & pasting the first time, i hope this helps though. Once again, thank you very much, i saw you even helped with the formatting of my post too & such. Much appreciated! – T55 Jul 02 '23 at 06:54
  • id be open to putting it anywhere more convenient if you need – T55 Jul 02 '23 at 06:57
  • **In the question.** Put the code in the question. Not in comments. Paste it in, and with the code selected, hit the code button, which looks like {}, in order to retain the formatting. – Mark Adler Jul 02 '23 at 07:24
  • i tried but it started saying to only doing indenting by 4 spaces or something, dose that mean i have to get rid of all the current indenting of anything & everything in the code, & replace it up to only 4 spaces? Seens very tedious, im on mobile too – T55 Jul 02 '23 at 14:25
  • No. Just paste in the code with its natural indenting, select the code, and hit the code, {}, button. Then save edits. That's it. – Mark Adler Jul 02 '23 at 19:02
  • ah alright, ill try – T55 Jul 03 '23 at 19:34

1 Answers1

0

Without being able to see your code, there is no way for us to know what you're doing wrong.

I see you got "PNG" at the start, so you are at least ordering the bits correctly. Your lookup appears to be correct, at least for the letters. Getting the "G" means you moved from the first four characters (three output bytes) to the second four characters correctly.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
  • Thank you for replying though, do you have somewhere where i can show you a picture of the "code"? – T55 Jun 28 '23 at 01:30
  • Why is code in quotes? What do mean "picture"? Copy the code into the question. – Mark Adler Jun 28 '23 at 01:47
  • did you see other reply? Its node based, u know, visual scripting – T55 Jun 28 '23 at 02:45
  • sorry if replying slow, its off the web so i dont get notifications – T55 Jun 28 '23 at 02:47
  • You can put images in the question. – Mark Adler Jun 28 '23 at 03:36
  • ooh ok, ill try – T55 Jun 29 '23 at 18:19
  • it didnt really work, i havent used stack overflow much because post kept getting taken down & all the toxicity. but i geuss you could go to the link hopefully? – T55 Jun 29 '23 at 18:25
  • It says "Image Not Found [|=(]". You don't need to put the image on some awful hosting site. Directly upload the image here. Click on the image icon when editing (it looks like two mountain peaks and the moon). You will be prompted with "Browse, drag & drop, or paste an image or link". – Mark Adler Jun 29 '23 at 18:40
  • ohh, ok, thank you – T55 Jul 01 '23 at 07:35
  • Sorry. I can't make any sense of the visual code. – Mark Adler Jul 01 '23 at 07:59
  • ah ok, well, i supposed the most important bits is are "look up" tables, so i could take a close up of those & later take a pic where the circuits are spread out some more & add in some text & doodles explaining what it does – T55 Jul 02 '23 at 04:40
  • Your look-up tables must be correct, at least the letters and digits. Otherwise you would not have gotten "PNG". Something is going south a few characters later, since you're not getting "IHDR". – Mark Adler Jul 02 '23 at 04:43
  • yeah, actually, i could probably generate some real C# code, that might help. thank you for your patience – T55 Jul 02 '23 at 05:19