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:
63 char *_codec_strerror "codec_strerror"(int err)
67 IGNCASE = CDCF_IGNCASE
68 NOEQPAD = CDCF_NOEQPAD
69 IGNEQPAD = CDCF_IGNEQPAD
70 IGNEQMID = CDCF_IGNEQMID
71 IGNZPAD = CDCF_IGNZPAD
72 IGNNEWL = CDCF_IGNNEWL
74 IGNINVCH = CDCF_IGNINVCH
75 IGNJUNK = CDCF_IGNJUNK
80 INVEQPAD = CDCERR_INVEQPAD
81 INVZPAD = CDCERR_INVZPAD
83 class CodecError (Exception):
84 def __init__(me, err):
86 me.msg = _codec_strerror(err)
90 def codec_strerror(err):
91 return _codec_strerror(err)
93 cdef int code(codec *c, void *p, size_t len, dstr *d) except -1:
95 err = c.ops.code(c, p, len, d)
100 cdef class _BaseCodec:
102 def __cinit__(me, *hunoz, **hukairz):
104 def __init__(me, *hunoz, **hukairz):
105 raise TypeError, 'abstract class'
108 me.c.ops.destroy(me.c)
109 cdef code(me, text, int finishp):
115 raise ValueError, 'Encoding finished'
118 PyObject_AsReadBuffer(text, <cvp *>&p, &len)
119 code(me.c, p, len, &d)
121 code(me.c, NULL, 0, &d)
122 me.c.ops.destroy(me.c)
124 return PyString_FromStringAndSize(d.buf, d.len)
130 cdef class _BaseEncoder (_BaseCodec):
131 def encode(me, text, finishp = False):
132 return me.code(text, finishp)
134 cdef class _BaseDecoder (_BaseCodec):
135 def decode(me, text, finishp = False):
136 return me.code(text, finishp)
138 ###--------------------------------------------------------------------------
141 cdef extern from 'mLib/base64.h':
142 codec_class base64_class
143 codec_class file64_class
144 codec_class base64url_class
146 cdef class Base64Encoder (_BaseEncoder):
147 def __init__(me, indent = '\n', maxline = 72, flags = CDCF_IGNJUNK):
148 me.c = base64_class.encoder(flags, indent, maxline)
150 cdef class Base64Decoder (_BaseDecoder):
151 def __init__(me, flags = CDCF_IGNJUNK):
152 me.c = base64_class.decoder(flags)
154 cdef class File64Encoder (_BaseEncoder):
155 def __init__(me, indent = '\n', maxline = 72, flags = CDCF_IGNJUNK):
156 me.c = file64_class.encoder(flags, indent, maxline)
158 cdef class File64Decoder (_BaseDecoder):
159 def __init__(me, flags = CDCF_IGNJUNK):
160 me.c = file64_class.decoder(flags)
162 cdef class Base64URLEncoder (_BaseEncoder):
163 def __init__(me, indent = '\n', maxline = 72, flags = CDCF_IGNJUNK):
164 me.c = base64url_class.encoder(flags, indent, maxline)
166 cdef class Base64URLDecoder (_BaseDecoder):
167 def __init__(me, flags = CDCF_IGNJUNK):
168 me.c = base64url_class.decoder(flags)
170 ###--------------------------------------------------------------------------
173 cdef extern from 'mLib/base32.h':
174 codec_class base32_class
175 codec_class base32hex_class
177 cdef class Base32Encoder (_BaseEncoder):
178 def __init__(me, indent = '\n', maxline = 72, flags = CDCF_IGNJUNK):
179 me.c = base32_class.encoder(flags, indent, maxline)
181 cdef class Base32Decoder (_BaseDecoder):
182 def __init__(me, flags = CDCF_IGNJUNK):
183 me.c = base32_class.decoder(flags)
185 cdef class Base32HexEncoder (_BaseEncoder):
186 def __init__(me, indent = '\n', maxline = 72, flags = CDCF_IGNJUNK):
187 me.c = base32hex_class.encoder(flags, indent, maxline)
189 cdef class Base32HexDecoder (_BaseDecoder):
190 def __init__(me, flags = CDCF_IGNJUNK):
191 me.c = base32hex_class.decoder(flags)
193 ###--------------------------------------------------------------------------
196 cdef extern from 'mLib/hex.h':
197 codec_class hex_class
199 cdef class HexEncoder (_BaseEncoder):
200 def __init__(me, indent = '\n', maxline = 72,
201 flags = CDCF_IGNJUNK | CDCF_LOWERC):
202 me.c = hex_class.encoder(flags, indent, maxline)
204 cdef class HexDecoder (_BaseDecoder):
205 def __init__(me, flags = CDCF_IGNJUNK | CDCF_LOWERC):
206 me.c = hex_class.decoder(flags)
208 ###----- That's all, folks --------------------------------------------------