chiark / gitweb /
codec/{base32,hex}.h: Include `codec.h'.
[mLib] / codec / codec.h
1 /* -*-c-*-
2  *
3  * Abstract interface to codecs
4  *
5  * (c) 2009 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * mLib 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 Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 #ifndef MLIB_CODEC_H
29 #define MLIB_CODEC_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #ifndef MLIB_DSTR_H
38 #  include "dstr.h"
39 #endif
40
41 /*----- Data structures ---------------------------------------------------*/
42
43 typedef struct codec {
44   const struct codec_ops *ops;
45 } codec;
46
47 typedef struct codec_class {
48   const char *name;
49   codec *(*encoder)(unsigned /*flags*/,
50                     const char */*indent*/, unsigned /*maxline*/);
51   codec *(*decoder)(unsigned /*flags*/);
52
53 #define CDCF_LOWERC 1u                  /* Prefer lower case */
54 #define CDCF_IGNCASE 2u                 /* Ignore case on input */
55 #define CDCF_NOEQPAD 4u                 /* No `=' padding */
56 #define CDCF_IGNEQPAD 8u                /* Ignore `=' pad errors on input */
57 #define CDCF_IGNEQMID 16u               /* Ignore pad chars on input */
58 #define CDCF_IGNZPAD 32u                /* Ignore zero padding on input */
59 #define CDCF_IGNNEWL 64u                /* Ignore newlines on input */
60 #define CDCF_IGNINVCH 128u              /* Ignore invalid chars on input */
61
62 #define CDCF_IGNJUNK                    /* Ignore all bad things */     \
63   (CDCF_IGNEQMID | CDCF_IGNZPAD |                                       \
64    CDCF_IGNCASE | CDCF_IGNNEWL | CDCF_IGNINVCH)
65
66 } codec_class;
67
68 #define CODEC_ERRORS(_)                                                 \
69   _(OK,         "No error")                                             \
70   _(INVCH,      "Invalid character")                                    \
71   _(INVEQPAD,   "Invalid padding character")                            \
72   _(INVZPAD,    "Nonzero padding bits")
73 enum {
74 #define DECLERR(name, text) CDCERR_##name,
75   CODEC_ERRORS(DECLERR)
76 #undef DECLERR
77   CDCERR__LIMIT
78 };
79
80 typedef struct codec_ops {
81   const codec_class *c;
82   int (*code)(codec */*c*/, const void */*p*/, size_t /*sz*/, dstr */*d*/);
83   void (*destroy)(codec */*c*/);
84 } codec_ops;
85
86 /*----- Functions provided ------------------------------------------------*/
87
88 /* --- @codec_strerror@ --- *
89  *
90  * Arguments:   @int err@ = error code from codec
91  *
92  * Returns:     Pointer to error text.
93  *
94  * Use:         Converts error codes to human-readable strings.
95  */
96
97 const char *codec_strerror(int /*err*/);
98
99 /*----- Null codec --------------------------------------------------------*/
100
101 extern const codec_class null_codec_class;
102
103 /*----- That's all, folks -------------------------------------------------*/
104
105 #ifdef __cplusplus
106   }
107 #endif
108
109 #endif