UUEncode \ UUDecode

Share your advanced PureBasic knowledge/code with the community.
Wayne Diamond
User
User
Posts: 38
Joined: Tue Dec 30, 2003 1:37 pm
Location: Australia

UUEncode \ UUDecode

Post by Wayne Diamond »

Code updated For 5.20+

Code: Select all

Procedure.s UUDecode(sInBuf.s)
  sOutBuf.s = ""
  For lLoop = 1 To Len(sInBuf) Step 4
    sOutBuf = sOutBuf + Chr((Asc(Mid(sInBuf, lLoop, 1)) - 32) * 4 + (Asc(Mid(sInBuf, lLoop + 1, 1)) - 32) / 16)
    sOutBuf = sOutBuf + Chr((Asc(Mid(sInBuf, lLoop + 1, 1)) % 16) * 16 + (Asc(Mid(sInBuf, lLoop + 2, 1)) - 32) / 4)
    sOutBuf = sOutBuf + Chr((Asc(Mid(sInBuf, lLoop + 2, 1)) % 4) * 64 + Asc(Mid(sInBuf, lLoop + 3, 1)) - 32)
  Next lLoop
  ProcedureReturn sOutBuf
EndProcedure

Procedure.s UUEncode(sInBuf.s)
  sOutBuf.s = ""
  For lLoop = 1 To Len(sInBuf) Step 3
    sOutBuf = sOutBuf + Chr(Asc(Mid(sInBuf, lLoop, 1)) / 4 + 32)
    sOutBuf = sOutBuf + Chr((Asc(Mid(sInBuf, lLoop, 1)) % 4) * 16 + Asc(Mid(sInBuf, lLoop + 1, 1)) / 16 + 32)
    sOutBuf = sOutBuf + Chr((Asc(Mid(sInBuf, lLoop + 1, 1)) % 16) * 4 + Asc(Mid(sInBuf, lLoop + 2, 1)) / 64 + 32)
    sOutBuf = sOutBuf + Chr(Asc(Mid(sInBuf, lLoop + 2, 1)) % 64 + 32) 
  Next lLoop
  ProcedureReturn sOutBuf
EndProcedure

sText.s = uuEncode("1234567890abcdefghijklmnopqrstuvwxyz")
Debug "Encoded=" + sText
sText = uuDecode(sText)
Debug "Decoded=" + sText
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

thx needed this
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

Thanks Wayne, it's very useful!

Tip:
In PB you don't need to write

Code: Select all

a$=a$+b$

Code: Select all

a$+b$
is shorter and the result is the same; works also for numeric values.

Regards
Einander
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

I have a few stupid newbie to UUEncode and email stuff. So be nice. :oops:

I am tring to make myself a program to turn email attachments back to usable items. Can I use these for this function? I also don't know how to tell where to start to grab the portion of the email that is the attachment. Can somebody help me out?

Thanks
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
Wayne Diamond
User
User
Posts: 38
Joined: Tue Dec 30, 2003 1:37 pm
Location: Australia

Post by Wayne Diamond »

RJP,
Can I use these for this function?
Yes, that's they're primary use (email-related), as that is pretty much the only time that UUEncoding is ever used on the Internet.
I also don't know how to tell where to start to grab the portion of the email that is the attachment.
In my demo you'll notice that I was encoding the string "1234567890abcdefghijklmnopqrstuvwxyz". So I ensured that my email client was encoding attachments in UUEncode rather than the default Base64 (there'll be an option in your email client for this), created a dummy file called test.exe, and added that "123(etc)..." string to it. I then emailed that file to myself, and checking the email message source I can then see this:

Code: Select all

begin 666 temp.exe
D,3(S-#4V-S@Y,&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ
`
end
If you just take that one encoded line and remove the "D" from the start of it (so, ",3(S-#4V-S@Y,&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ"), you can then send that entire line to my decode function. The result:
1234567890abcdefghijklmnopqrstuvwxyz

Regards,
Wayne
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post by Seldon »

It means I can pass a memory buffer filled with a file (instead of a string buffer) to your UUEconde function (of course with some modifications) ?
Wayne Diamond
User
User
Posts: 38
Joined: Tue Dec 30, 2003 1:37 pm
Location: Australia

Post by Wayne Diamond »

Seldon, that's correct. The only thing you'd probably need to do is strip out any carriage returns/line feeds (0x0D and 0x0A respectively).

Regards,
Wayne
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

Do you have any base64 encoding/decoding routines? Just wondering.

Sweet picture 8)
Last edited by RJP Computing on Tue Jan 06, 2004 4:43 am, edited 1 time in total.
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
Wayne Diamond
User
User
Posts: 38
Joined: Tue Dec 30, 2003 1:37 pm
Location: Australia

Post by Wayne Diamond »

I have half a dozen different variations of Base 64 including inline asm but haven't had a chance to port any over to Purebasic yet, maybe later this week with any luck

Regards,
Wayne
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Do you have any base64 encoding routines?

What's wrong with PureBasic's base64 encoder?

http://www.purebasic.com/documentation/ ... coder.html
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

@PB,
I need a decode that isn't in pb or I would definatly use that!

@Wayne,

Can't wait. Thanks Wayne.

I use Popcorn for my email program and it doesn't work with attachments, so I want to make a utility that can decode attachments. I am eagerly waiting. I am close with an example from the forum but I can't get it to create working .exe from what outlook sends. I think it is because I can't figure out how to find what the output file size should be.

http://purearea.net/pb/CodeArchiv/Encod ... Example.pb
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> I need a decode that isn't in pb or I would definatly use that!

You originally said encode (a typo I now realize) and hence my confusion. :)

To decode base64:

viewtopic.php?t=4970

Hope this helps!
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

@PB
Yes. Thanks for that help.
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: UUEncode \ UUDecode

Post by Dude »

The UUEncode function is buggy: it always leaves off the first character, compared to online UUEncoders.

The result from the code sample in the first post:

Code: Select all

,3(S-#4V-S@Y,&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ
But from http://www.textencode.com/encoder/uuencodeEncoder and other sites:

Code: Select all

D,3(S-#4V-S@Y,&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: UUEncode \ UUDecode

Post by infratec »

Hi,

the procedure is correct.
What you are missing is the length character.
You can add

Code: Select all

sOutBuf + Chr(Len(sInBuf) + ' ')
at the start to get the wanted result.

Like this (optimized version):

Code: Select all

Procedure.s UUEncode(sInBuf.s)
  Protected sOutBuf.s, lLoop.i
  sOutBuf + Chr(Len(sInBuf) + ' ')
  For lLoop = 1 To Len(sInBuf) Step 3
    sOutBuf + Chr(Asc(Mid(sInBuf, lLoop, 1)) >> 2 + ' ')
    sOutBuf + Chr(((Asc(Mid(sInBuf, lLoop, 1)) & %11) << 4) | (Asc(Mid(sInBuf, lLoop + 1, 1)) >> 4) + ' ')
    sOutBuf + Chr(((Asc(Mid(sInBuf, lLoop + 1, 1)) & %1111) << 2) | (Asc(Mid(sInBuf, lLoop + 2, 1)) >> 6) + ' ')
    sOutBuf + Chr(Asc(Mid(sInBuf, lLoop + 2, 1)) & %111111 + ' ')
  Next lLoop
  ProcedureReturn sOutBuf
EndProcedure
But then you have also to change the Decode to respect the length byte in front.

Bernd
Post Reply