10 April
2008

Python includes a uudecode and uuencode module (accessible via "import uu"), but the module only supports base32 uudecoding, and not base64. I needed base64. Although I have posted an official patch to the python2.4 library, python had already moved on to a later version. Here is my patch for python2.5 uu.py to make it able to decode and intrepret base64 data. Encoding in base64 is probably pretty easy too, but I don't need it and have not bothers. I encourage somebody to add it.

One problem in applying this patch, especially from a web page, is that the white space versus tabs might not match. The - l flag (that's a lower case L) tells patch to ignore white space, but since python is senseitve to white space, you might have to adjust things manually.

Also note that on OS X, the uu.py module can be found at
/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/uu.py

The patch file (input for patch -l) is as follows:


--- uu.py.orig 2007-08-25 23:42:35.000000000 -0400
+++ uu.py 2007-08-26 11:42:54.000000000 -0400
@@ -18,6 +18,9 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
+# Modified by Gregory Dudek, April 2008:
+# - added support for decoding of base64 uuencoded files.
+#
# Modified by Jack Jansen, CWI, July 1995:
# - Use binascii module to do the actual line-by-line conversion
# between ascii and binary. This results in a 1000-fold speedup. The C
@@ -81,7 +84,7 @@
out_file.write(' \nend\n')


-def decode(in_file, out_file=None, mode=None, quiet=0):
+def decode(in_file, out_file=None, mode=None, quiet=0, base64=0):
"""Decode uuencoded file"""
#
# Open the input file, if needed.
@@ -100,9 +103,10 @@
if hdr[:5] != 'begin':
continue
hdrfields = hdr.split(" ", 2)
- if len(hdrfields) == 3 and hdrfields[0] == 'begin':
+ if len(hdrfields) == 3 and (hdrfields[0] == 'begin' or hdrfields[0] == 'begin-base64'):
try:
int(hdrfields[1], 8)
+ if hdrfields[0] == 'begin-base64': base64=1
break
except ValueError:
pass
@@ -130,11 +134,13 @@
s = in_file.readline()
while s and s.strip() != 'end':
try:
- data = binascii.a2b_uu(s)
+ if base64: data = binascii.a2b_base64(s)
+ else: data = binascii.a2b_uu(s)
except binascii.Error, v:
# Workaround for broken uuencoders by /Fredrik Lundh
nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3
- data = binascii.a2b_uu(s[:nbytes])
+ if base64: data = binascii.a2b_base64(s[:nbytes])
+ else: data = binascii.a2b_uu(s[:nbytes])
if not quiet:
sys.stderr.write("Warning: %s\n" % str(v))
out_file.write(data)


By Gregory Dudek at | Leave a comment |    
Rate item 113: Rating: 16.4/10 (8 votes cast)
Comments
There are no comments.
Trackbacks
Please send trackback to:/blog/113/tbping
elcaol

acellitr

Posted by: vardronroc at May 18,2009 18:54
elelrelm

trocrorotrbo

Posted by: boctaboboc at May 20,2009 02:27
trtroclet

lachielt

Posted by: chidelsi at May 20,2009 02:27
Post your own response

Each comment is manually screened for the presence of appropriate and substantive content, due to a constant onslaught of comment-spam. This means there may be a delay before your comment appears.


(Some kind of name is required, will be visible)

Required, whatever you enter will be visible to other users.


(Optional, used for "mailto" link)

Your email address is not required, but if you insert it it will be displayed so people can contact you.

Answer this question correctly to demonstrate that you are not a dumb spambot.



The title for your comment.



Your comment goes here. All relevant comments are welcome, except for those that simply promote an irrelevant product or else are used to fraudulently inflate the link count to an irrelevant web page. They appear after moderation. Don't forget to also fill in the captcha below or your text will be rejected automatically!

You must answer this question to prove you are human
Which one has ice cream (pick a number 1-5): 1:DairyQueen, 2:KFC, 3:FBI, 4:NFL, 5:NBC-TV?

Answer this question correctly to demonstrate that you are not a dumb spambot.