Vargol / VR180PhotoTools

Some .net tools for converting between equirectangular images and Googles VR180 Photo format
MIT License
8 stars 3 forks source link

Your Base64 Padding calculation is wrong #1

Open MarqueIV opened 3 years ago

MarqueIV commented 3 years ago

Hey! Found your project tonight and it was exactly what I needed! Thanks!! However, your Base64 padding calculation is incorrect.

Specifically, in JpegParser.cs in the ProcessExtendedXMPXML function, you're running a mod 4 on the length of the data, which returns the count of the chars that don't perfectly align on a four-byte boundary. In the case of Base64, that will always return 0, 2 or 3. The problem is you're using that value as the index into the substring "==" which is incorrect, and in the case of 3, crashes the app.

What you should be doing is padding the string to align on a multiple of four characters, meaning when you get 2 you pad with 2 but when you get a 3, you only pad with 1, and of course when you get back 0, you don't pad at all.

Here's the code you can use to get the correct padding which fixes your crash...

var padding = "";
switch (attribute.Value.Length % 4) {

    case 2:
        padding = "==";
        break;

    case 3:
        padding = "=";
        break;
};

Note: Normally I'd make a PR for you, but I've heavily modified your files so much it would be more work than just sharing this with you.

Hope this helps!

MarqueIV commented 3 years ago

By the way, here's a link to my fork of your work. The other thing I did was modified your vr180ToEquiPhoto to prompt you for a directory of vr180 photos so it can convert them all in a batch rather than you having to batch them externally. You also have the option to rename them, and if you do, it will auto-generate number suffixes for you.

Feel free to crib any of that and pull back into yours. Happy to help! :)

https://github.com/MarqueIV/VR180PhotoTools