--- /dev/null
+""" Python 'base64_codec' Codec - base64 content transfer encoding\r
+\r
+ Unlike most of the other codecs which target Unicode, this codec\r
+ will return Python string objects for both encode and decode.\r
+\r
+ Written by Marc-Andre Lemburg (mal@lemburg.com).\r
+\r
+"""\r
+import codecs, base64\r
+\r
+### Codec APIs\r
+\r
+def base64_encode(input,errors='strict'):\r
+\r
+ """ Encodes the object input and returns a tuple (output\r
+ object, length consumed).\r
+\r
+ errors defines the error handling to apply. It defaults to\r
+ 'strict' handling which is the only currently supported\r
+ error handling for this codec.\r
+\r
+ """\r
+ assert errors == 'strict'\r
+ output = base64.encodestring(input)\r
+ return (output, len(input))\r
+\r
+def base64_decode(input,errors='strict'):\r
+\r
+ """ Decodes the object input and returns a tuple (output\r
+ object, length consumed).\r
+\r
+ input must be an object which provides the bf_getreadbuf\r
+ buffer slot. Python strings, buffer objects and memory\r
+ mapped files are examples of objects providing this slot.\r
+\r
+ errors defines the error handling to apply. It defaults to\r
+ 'strict' handling which is the only currently supported\r
+ error handling for this codec.\r
+\r
+ """\r
+ assert errors == 'strict'\r
+ output = base64.decodestring(input)\r
+ return (output, len(input))\r
+\r
+class Codec(codecs.Codec):\r
+\r
+ def encode(self, input,errors='strict'):\r
+ return base64_encode(input,errors)\r
+ def decode(self, input,errors='strict'):\r
+ return base64_decode(input,errors)\r
+\r
+class IncrementalEncoder(codecs.IncrementalEncoder):\r
+ def encode(self, input, final=False):\r
+ assert self.errors == 'strict'\r
+ return base64.encodestring(input)\r
+\r
+class IncrementalDecoder(codecs.IncrementalDecoder):\r
+ def decode(self, input, final=False):\r
+ assert self.errors == 'strict'\r
+ return base64.decodestring(input)\r
+\r
+class StreamWriter(Codec,codecs.StreamWriter):\r
+ pass\r
+\r
+class StreamReader(Codec,codecs.StreamReader):\r
+ pass\r
+\r
+### encodings module API\r
+\r
+def getregentry():\r
+ return codecs.CodecInfo(\r
+ name='base64',\r
+ encode=base64_encode,\r
+ decode=base64_decode,\r
+ incrementalencoder=IncrementalEncoder,\r
+ incrementaldecoder=IncrementalDecoder,\r
+ streamwriter=StreamWriter,\r
+ streamreader=StreamReader,\r
+ )\r