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)


Posted by dudek at April 10 21:23 | Leave a comment | permalink link to this entry |
Rate item 113: 113 Rating: 11.4/10 (8 votes cast)
Comments
There are no comments.
Trackbacks
Please send trackback to:/blog//113/tbping
There are no trackbacks.
Post your own response


(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.


(Optional, will appear as a link)

Leave an URL if you want to provide a relevant link that relates to your comment or identifies you.



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.


(you must preview before posting)




Science Blogs - Blog Top Sites Science Blogs - Blog Catalog Blog Directory Science Blogs - Blog Top Sites Science Blogs - Blog Catalog Blog Directory