Louis Maddox
Louis Maddox

Reputation: 5576

URL-safe base64 decoding for R

I don't know if it's just R being notoriously hard to search for but I can't seem to find a URL-safe base64 decoder for R.

Does anyone know of one ?

Edit I've used various decoders, the RCurl::base64decode() function as well as online ones and the results are somewhat inconsistent.

I admit I don't understand the full details of what differentiates a "URL-safe" base64 decoder but find it dismissive to have my question closed in a matter of minutes due to a moderator's misunderstanding of what I'm asking before anyone gets a chance to answer.

Edit 2 The equivalent in Python is base64.urlsafe_b64decode(s), and as yet noone has implemented this in R.

Using library(sos) as recommended in the comments brings up 3 packages, and as expected a Google search would have found them had they URL-safe methods.

The Python docs explain that a URL-safe alphabet

substitutes - instead of + and _ instead of / in the standard Base64 alphabet.

I'd be interested to know how to use a custom alphabet. If I understand correctly the 'raw' mode has already applied the alphabet, and thus mistranslated.

The example above is the body of an email I retrieved through the Gmail API, which base64-encodes with such a URL-safe encoder:

PGRpdiBkaXI9Imx0ciI-SGksPGJyPjxicj5JIGNhbiYjMzk7dCBpbnN0YWxsIHhwcyBiZWNhdXNlIGR1cmluZyBjb21waWxlIEkgZ2V0OjxkaXY-PGJyPjwvZGl2PjxkaXY-PGZvbnQgZmFjZT0ibW9ub3NwYWNlLCBtb25vc3BhY2UiPlRNTE1hdGguY3h4OjUxOjE5OiBmYXRhbCBlcnJvcjogVE1hdGguaDogTm8gc3VjaCBmaWxlIG9yIGRpcmVjdG9yeTxicj48L2ZvbnQ-PC9kaXY-PGRpdj48Zm9udCBmYWNlPSJtb25vc3BhY2UsIG1vbm9zcGFjZSI-PGJyPjwvZm9udD48L2Rpdj48ZGl2PjxkaXY-PGZvbnQgZmFjZT0ibW9ub3NwYWNlLCBtb25vc3BhY2UiPsKgI2luY2x1ZGUgJnF1b3Q7VE1hdGguaCZxdW90OzwvZm9udD48L2Rpdj48ZGl2Pjxmb250IGZhY2U9Im1vbm9zcGFjZSwgbW9ub3NwYWNlIj7CoCDCoCDCoCDCoCDCoCDCoCDCoCDCoCDCoCDCoF48L2ZvbnQ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ga2luZCBvZiBlcnJvciBhbmQgSSBkZWxldGVkIHRoZXNlIGFkZGl0aW9uYWwgZmlsZXMgYWdhaW4gKHNlZSB0aGUgbGluayBhYm92ZSkuPC9mb250PjwvZGl2PjxkaXY-PGZvbnQgZmFjZT0iYXJpYWwsIGhlbHZldGljYSwgc2Fucy1zZXJpZiI-PGJyPjwvZm9udD48L2Rpdj48ZGl2Pjxmb250IGZhY2U9ImFyaWFsLCBoZWx2ZXRpY2EsIHNhbnMtc2VyaWYiPkkgaG9wZSB5b3UgbWF5IGJlIGFibGUgdG8gYWR2aXNlLCBJJiMzOTt2ZSBydW4gb3V0IG9mIGlkZWFzIGFuZCBleHBlcnRpc2Ugb24gaG93IGVsc2UgdG8gcHJvY2VlZC48L2ZvbnQ-PC9kaXY-PGRpdj48Zm9udCBmYWNlPSJhcmlhbCwgaGVsdmV0aWNhLCBzYW5zLXNlcmlmIj48YnI-PC9mb250PjwvZGl2PjxkaXY-PGZvbnQgZmFjZT0iYXJpYWwsIGhlbHZldGljYSwgc2Fucy1zZXJpZiI-QmVzdCB3aXNoZXMsPC9mb250PjwvZGl2PjxkaXY-PGZvbnQgZmFjZT0iYXJpYWwsIGhlbHZldGljYSwgc2Fucy1zZXJpZiI-PGJyPjwvZm9udD48L2Rpdj48ZGl2Pjxmb250IGZhY2U9ImFyaWFsLCBoZWx2ZXRpY2EsIHNhbnMtc2VyaWYiPkxvdWlzPC9mb250PjwvZGl2PjwvZGl2Pg0K

which an online URL-safe decoder will give as

<div dir="ltr">Hi,<br><br>I can't install xps because during compile I get:<div><br></div><div><font face="monospace, monospace">TMLMath.cxx:51:19: fatal error: TMath.h: No such file or directory<br></font></div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace"> #include "TMath.h"</font></div><div><font face="monospace, monospace">                   ^</font></div><div><font face="monospace, monospace">compilation terminated.</font></div><div><font face="monospace, monospace">make: *** [TMLMath.o] Error 1</font></div><div><font face="monospace, monospace">ERROR: compilation failed for package ‘xps’</font></div></div><div><br><font face="arial, helvetica, sans-serif">Do you have any idea where I've gone wrong, or how I might fix it? Further details of how I went about it are <a href="https://github.com/lmmx/devnotes/wiki/Installing-CERN's-ROOT-framework">here</a> and full screenshot attached.</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">I tried manually adding in the files the error message said were missing, but eventually it gave a different kind of error and I deleted these additional files again (see the link above).</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">I hope you may be able to advise, I've run out of ideas and expertise on how else to proceed.</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Best wishes,</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Louis</font></div></div>

The RCurl function gives a truncated and patchy decoding:

<div dir=\"lt\001ȈHi,<br><br>I can't install xps because during compile I get:<d\001\xa5\xd8<br></div><d\001\xa5\xd8<font face=\"monospace, monospace\">TMLMath.cxx:51:19: fatal error: TMath.h: No such file or directory<br></fo\001\xb9\xd0</d\001\xa5\xd8<div><font face=\"monospace, monospac\001\x94\x88<br></font></div><div><d\001\xa5\xd8<font face=\"monospace, monospace\"> #include "TMath.h"</font></div><div><font face=\"monospace, monospace\"> ^</fo\001\xb9\xd0</d\001\xa5\xd8<div><font face=\"monospace, monospac\001\x94\x88compilation terminated.</fo\001\xb9\xd0</d\001\xa5\xd8<div><font face=\"monospace, monospac\001\x94\x88make: *** [TMLMath.o] Error 1</fo\001\xb9\xd0</d\001\xa5\xd8<div><font face=\"monospace, monospac\001\x94\x88ERROR: compilation failed for package ‘xps’</fo\001\xb9\xd0</d\001\xa5\xd8</d\001\xa5\xd8<div><\001\x89\xc8<font face=\"arial, helvetica, sans-seri\001\x98\x88Do you have any idea where I've gone wrong, or how I might fix it? Further details of how I went about it are <a href=\"https://github.com/lmmx/devnotes/wiki/Installing-CERN's-ROOT-framework\">here<

While the online base64 decoder I was using won't decode it at all, reporting an error.

I'll have to write something to use a custom alphabet myself if noone knows a way to do this with existing R packages. I'd prefer not to have to invoke Python, if possible.

Upvotes: 1

Views: 1689

Answers (1)

Louis Maddox
Louis Maddox

Reputation: 5576

Well... that was embarrassingly easy.

base64Decode(gsub("-","+",gsub("/","_",strtodecode)))

Sorry to answer my own question, it seems way more complicated than it is

Edit this actually has to be base64Decode(gsub("-","+",gsub("_","/",strtodecode))) for the case I'm working on, but for an example the above worked - there are multiple base64 alphabets.

Upvotes: 2

Related Questions