summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorJack Jansen <jack.jansen@cwi.nl>1995-10-04 16:39:20 (GMT)
committerJack Jansen <jack.jansen@cwi.nl>1995-10-04 16:39:20 (GMT)
commit951213e8ca3fb25b6765dfbcd148b20584516894 (patch)
treea5e62d8d6960ffab9bdac188e178b5c0b6056b30 /Lib
parent84bbc2e3e3067ef68d59ec6ac1d12e20a7042ba7 (diff)
downloadcpython-951213e8ca3fb25b6765dfbcd148b20584516894.zip
cpython-951213e8ca3fb25b6765dfbcd148b20584516894.tar.gz
cpython-951213e8ca3fb25b6765dfbcd148b20584516894.tar.bz2
Use binascii module (resulting in a 60-fold speedup:-)
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/base64.py90
1 files changed, 14 insertions, 76 deletions
diff --git a/Lib/base64.py b/Lib/base64.py
index 90b5273..c5a7ca7 100755
--- a/Lib/base64.py
+++ b/Lib/base64.py
@@ -2,95 +2,33 @@
# version).
# Parameters set by RFX-XXXX.
-MAXLINESIZE = 76 # Excluding the CRLF
-INVAR = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
-PAD = '='
+#
+# Modified 04-Oct-95 by Jack to use binascii module
-# Check that I typed that string correctly...
-if len(INVAR) <> 64: raise RuntimeError, 'wrong INVAR string!?!?'
+import binascii
-# Compute the inverse table, for decode().
-inverse = {}
-for i in range(64): inverse[INVAR[i]] = i
-del i
-inverse[PAD] = 0
+MAXLINESIZE = 76 # Excluding the CRLF
+MAXBINSIZE = (MAXLINESIZE/4)*3
# Encode a file.
def encode(input, output):
- line = ''
- BUFSIZE = 8192
- leftover = ''
while 1:
- s = input.read(BUFSIZE)
+ s = input.read(MAXBINSIZE)
if not s: break
- s = leftover + s
- i = 0
- while i+3 <= len(s):
- quad = makequad(s[i:i+3])
- i = i+3
- if len(line) + 4 > MAXLINESIZE:
- output.write(line + '\n')
- line = ''
- line = line + quad
- leftover = s[i:]
- if leftover:
- quad = makeshortquad(leftover)
- if len(line) + 4 > MAXLINESIZE:
- output.write(line + '\n')
- line = ''
- line = line + quad
- if line:
- output.write(line + '\n')
-
-def makequad(s): # Return the quad for a 3 character string
- x = ord(s[0])*0x10000 + ord(s[1])*0x100 + ord(s[2])
- x, c4 = divmod(x, 64)
- x, c3 = divmod(x, 64)
- c1, c2 = divmod(x, 64)
- return INVAR[c1] + INVAR[c2] +INVAR[c3] + INVAR[c4]
-
-def makeshortquad(s): # Return the quad value for a 1 or 2 character string
- n = len(s)
- while len(s) < 3:
- s = s + '\0'
- quad = makequad(s)
- if n == 2:
- quad = quad[:3] + PAD
- elif n == 1:
- quad = quad[:2] + 2*PAD
- return quad
+ while len(s) < MAXBINSIZE:
+ ns = input.read(MAXBINSIZE-len(s))
+ if not ns: break
+ s = s + ns
+ line = binascii.b2a_base64(s)
+ output.write(line)
# Decode a file.
def decode(input, output):
- BUFSIZE = 8192
- bits, n, bytes, prev = 0, 0, '', ''
while 1:
line = input.readline()
if not line: break
- for c in line:
- if inverse.has_key(c):
- bits = bits*64 + inverse[c]
- n = n+6
- if n == 24:
- triplet = decodequad(bits)
- if c == PAD:
- if prev == PAD:
- triplet = triplet[:1]
- else:
- triplet = triplet[:2]
- bits, n = 0, 0
- bytes = bytes + triplet
- if len(bytes) > BUFSIZE:
- output.write(bytes[:BUFSIZE])
- bytes = bytes[BUFSIZE:]
- prev = c
- if bytes:
- output.write(bytes)
-
-def decodequad(bits): # Turn 24 bits into 3 characters
- bits, c3 = divmod(bits, 256)
- c1, c2 = divmod(bits, 256)
- return chr(c1) + chr(c2) + chr(c3)
+ s = binascii.a2b_base64(line)
+ output.write(s)
def encodestring(s):
import StringIO