chiark / gitweb /
Merge branch '2.4.x'
[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 #define CDCF_IGNSPC 256u                /* Ignore whitespace on input */
62
63 #define CDCF_IGNJUNK                    /* Ignore all bad things */     \
64   (CDCF_IGNEQMID | CDCF_IGNZPAD |                                       \
65    CDCF_IGNCASE | CDCF_IGNNEWL | CDCF_IGNSPC | CDCF_IGNINVCH)
66
67 } codec_class;
68
69 #define CODEC_ERRORS(_)                                                 \
70   _(OK,         "No error")                                             \
71   _(INVCH,      "Invalid character")                                    \
72   _(INVEQPAD,   "Invalid padding character")                            \
73   _(INVZPAD,    "Nonzero padding bits")
74 enum {
75 #define DECLERR(name, text) CDCERR_##name,
76   CODEC_ERRORS(DECLERR)
77 #undef DECLERR
78   CDCERR__LIMIT
79 };
80
81 typedef struct codec_ops {
82   const codec_class *c;
83   int (*code)(codec */*c*/, const void */*p*/, size_t /*sz*/, dstr */*d*/);
84   void (*destroy)(codec */*c*/);
85 } codec_ops;
86
87 /*----- Functions provided ------------------------------------------------*/
88
89 /* --- @codec_strerror@ --- *
90  *
91  * Arguments:   @int err@ = error code from codec
92  *
93  * Returns:     Pointer to error text.
94  *
95  * Use:         Converts error codes to human-readable strings.
96  */
97
98 const char *codec_strerror(int /*err*/);
99
100 /*----- Null codec --------------------------------------------------------*/
101
102 extern const codec_class null_codec_class;
103
104 /*----- That's all, folks -------------------------------------------------*/
105
106 #ifdef __cplusplus
107   }
108 #endif
109
110 #endif