3 ### Generic encoder/decoder
5 ### (c) 2005 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the Python interface to mLib.
12 ### mLib/Python is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
17 ### mLib/Python is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
22 ### You should have received a copy of the GNU General Public License
23 ### along with mLib/Python; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ###--------------------------------------------------------------------------
29 cdef extern from 'mLib/codec.h':
33 ctypedef struct codec_class:
35 codec *(*encoder)(unsigned f, char *ind, unsigned max)
36 codec *(*decoder)(unsigned f)
38 ctypedef struct codec_ops:
40 int (*code)(codec *c, void *p, size_t, dstr *d)
41 void (*destroy)(codec *c)
42 ctypedef struct codec:
62 char *_codec_strerror "codec_strerror"(int err)
66 IGNCASE = CDCF_IGNCASE
67 NOEQPAD = CDCF_NOEQPAD
68 IGNEQPAD = CDCF_IGNEQPAD
69 IGNEQMID = CDCF_IGNEQMID
70 IGNZPAD = CDCF_IGNZPAD
71 IGNNEWL = CDCF_IGNNEWL
72 IGNINVCH = CDCF_IGNINVCH
73 IGNJUNK = CDCF_IGNJUNK
78 INVEQPAD = CDCERR_INVEQPAD
79 INVZPAD = CDCERR_INVZPAD
81 class CodecError (Exception):
82 def __init__(me, err):
84 me.msg = _codec_strerror(err)
88 def codec_strerror(err):
89 return _codec_strerror(err)
91 cdef int code(codec *c, void *p, size_t len, dstr *d) except -1:
93 err = c.ops.code(c, p, len, d)
98 cdef class _BaseCodec:
100 def __cinit__(me, *hunoz, **hukairz):
102 def __init__(me, *hunoz, **hukairz):
103 raise TypeError, 'abstract class'
106 me.c.ops.destroy(me.c)
107 cdef code(me, text, int finishp):
113 raise ValueError, 'Encoding finished'
116 PyObject_AsReadBuffer(text, &p, &len)
117 code(me.c, p, len, &d)
119 code(me.c, NULL, 0, &d)
120 me.c.ops.destroy(me.c)
122 return PyString_FromStringAndSize(d.buf, d.len)
128 cdef class _BaseEncoder (_BaseCodec):
129 def encode(me, text, finishp = False):
130 return me.code(text, finishp)
132 cdef class _BaseDecoder (_BaseCodec):
133 def decode(me, text, finishp = False):
134 return me.code(text, finishp)
136 ###--------------------------------------------------------------------------
139 cdef extern from 'mLib/base64.h':
140 codec_class base64_class
141 codec_class file64_class
142 codec_class base64url_class
144 cdef class Base64Encoder (_BaseEncoder):
145 def __init__(me, indent = '\n', maxline = 72, flags = CDCF_IGNJUNK):
146 me.c = base64_class.encoder(flags, indent, maxline)
148 cdef class Base64Decoder (_BaseDecoder):
149 def __init__(me, flags = CDCF_IGNJUNK):
150 me.c = base64_class.decoder(flags)
152 cdef class File64Encoder (_BaseEncoder):
153 def __init__(me, indent = '\n', maxline = 72, flags = CDCF_IGNJUNK):
154 me.c = file64_class.encoder(flags, indent, maxline)
156 cdef class File64Decoder (_BaseDecoder):
157 def __init__(me, flags = CDCF_IGNJUNK):
158 me.c = file64_class.decoder(flags)
160 cdef class Base64URLEncoder (_BaseEncoder):
161 def __init__(me, indent = '\n', maxline = 72, flags = CDCF_IGNJUNK):
162 me.c = base64url_class.encoder(flags, indent, maxline)
164 cdef class Base64URLDecoder (_BaseDecoder):
165 def __init__(me, flags = CDCF_IGNJUNK):
166 me.c = base64url_class.decoder(flags)
168 ###--------------------------------------------------------------------------
171 cdef extern from 'mLib/base32.h':
172 codec_class base32_class
173 codec_class base32hex_class
175 cdef class Base32Encoder (_BaseEncoder):
176 def __init__(me, indent = '\n', maxline = 72, flags = CDCF_IGNJUNK):
177 me.c = base32_class.encoder(flags, indent, maxline)
179 cdef class Base32Decoder (_BaseDecoder):
180 def __init__(me, flags = CDCF_IGNJUNK):
181 me.c = base32_class.decoder(flags)
183 cdef class Base32HexEncoder (_BaseEncoder):
184 def __init__(me, indent = '\n', maxline = 72, flags = CDCF_IGNJUNK):
185 me.c = base32hex_class.encoder(flags, indent, maxline)
187 cdef class Base32HexDecoder (_BaseDecoder):
188 def __init__(me, flags = CDCF_IGNJUNK):
189 me.c = base32hex_class.decoder(flags)
191 ###--------------------------------------------------------------------------
194 cdef extern from 'mLib/hex.h':
195 codec_class hex_class
197 cdef class HexEncoder (_BaseEncoder):
198 def __init__(me, indent = '\n', maxline = 72,
199 flags = CDCF_IGNJUNK | CDCF_LOWERC):
200 me.c = hex_class.encoder(flags, indent, maxline)
202 cdef class HexDecoder (_BaseDecoder):
203 def __init__(me, flags = CDCF_IGNJUNK | CDCF_LOWERC):
204 me.c = hex_class.decoder(flags)
206 ###----- That's all, folks --------------------------------------------------