aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-mime-filter.c
blob: 2887cbbd1e9d4a6ffeaa48ea7d7bc0a4241dee3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 *  Copyright (C) 2000 Ximian Inc.
 *
 *  Authors: Michael Zucchi <notzed@ximian.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <string.h>
#include "camel-mime-filter.h"

/*#define MALLOC_CHECK */ /* for some malloc checking, requires mcheck enabled */

/* only suitable for glibc */
#ifdef MALLOC_CHECK
#include <mcheck.h>
#endif

struct _CamelMimeFilterPrivate {
    char *inbuf;
    size_t inlen;
};

#define PRE_HEAD (64)
#define BACK_HEAD (64)
#define _PRIVATE(o) (((CamelMimeFilter *)(o))->priv)
#define FCLASS(o) ((CamelMimeFilterClass *)(CAMEL_OBJECT_GET_CLASS(o)))

static CamelObjectClass *camel_mime_filter_parent;

static void complete (CamelMimeFilter *mf, char *in, size_t len, 
              size_t prespace, char **out, size_t *outlen, 
              size_t *outprespace);

static void
camel_mime_filter_class_init (CamelMimeFilterClass *klass)
{
    camel_mime_filter_parent = camel_type_get_global_classfuncs (camel_object_get_type ());

    klass->complete = complete;
}

static void
camel_mime_filter_init (CamelMimeFilter *obj)
{
    obj->outreal = NULL;
    obj->outbuf = NULL;
    obj->outsize = 0;

    obj->backbuf = NULL;
    obj->backsize = 0;
    obj->backlen = 0;

    _PRIVATE(obj) = g_malloc0(sizeof(*obj->priv));
}

static void
camel_mime_filter_finalize(CamelObject *o)
{
    CamelMimeFilter *f = (CamelMimeFilter *)o;
    struct _CamelMimeFilterPrivate *p = _PRIVATE(f);

    g_free(f->outreal);
    g_free(f->backbuf);
    g_free(p->inbuf);
    g_free(p);
}

CamelType
camel_mime_filter_get_type (void)
{
    static CamelType camel_mime_filter_type = CAMEL_INVALID_TYPE;
    
    if (camel_mime_filter_type == CAMEL_INVALID_TYPE) {
        camel_mime_filter_type = camel_type_register (CAMEL_OBJECT_TYPE, "CamelMimeFilter",
                                  sizeof (CamelMimeFilter),
                                  sizeof (CamelMimeFilterClass),
                                  (CamelObjectClassInitFunc) camel_mime_filter_class_init,
                                  NULL,
                                  (CamelObjectInitFunc) camel_mime_filter_init,
                                  (CamelObjectFinalizeFunc) camel_mime_filter_finalize);
    }
    
    return camel_mime_filter_type;
}

static void
complete(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlen, size_t *outprespace)
{
    /* default - do nothing */
}

/**
 * camel_mime_filter_new:
 *
 * Create a new CamelMimeFilter object.
 * 
 * Return value: A new CamelMimeFilter widget.
 **/
CamelMimeFilter *
camel_mime_filter_new (void)
{
    CamelMimeFilter *new = CAMEL_MIME_FILTER ( camel_object_new (camel_mime_filter_get_type ()));
    return new;
}

#ifdef MALLOC_CHECK
static void
checkmem(void *p)
{
    if (p) {
        int status = mprobe(p);

        switch (status) {
        case MCHECK_HEAD:
            printf("Memory underrun at %p\n", p);
            abort();
        case MCHECK_TAIL:
            printf("Memory overrun at %p\n", p);
            abort();
        case MCHECK_FREE:
            printf("Double free %p\n", p);
            abort();
        }
    }
}
#endif

static void filter_run(CamelMimeFilter *f,
               char *in, size_t len, size_t prespace,
               char **out, size_t *outlen, size_t *outprespace,
               void (*filterfunc)(CamelMimeFilter *f,
                      char *in, size_t len, size_t prespace,
                      char **out, size_t *outlen, size_t *outprespace))
{
    struct _CamelMimeFilterPrivate *p;

#ifdef MALLOC_CHECK
    checkmem(f->outreal);
    checkmem(f->backbuf);
#endif
    /*
      here we take a performance hit, if the input buffer doesn't
      have the pre-space required.  We make a buffer that does ...
    */
    if (prespace < f->backlen) {
        int newlen = len+prespace+f->backlen;
        p = _PRIVATE(f);
        if (p->inlen < newlen) {
            /* NOTE: g_realloc copies data, we dont need that (slower) */
            g_free(p->inbuf);
            p->inbuf = g_malloc(newlen+PRE_HEAD);
            p->inlen = newlen+PRE_HEAD;
        }
        /* copy to end of structure */
        memcpy(p->inbuf+p->inlen - len, in, len);
        in = p->inbuf+p->inlen - len;
        prespace = p->inlen - len;
    }

#ifdef MALLOC_CHECK
    checkmem(f->outreal);
    checkmem(f->backbuf);
#endif

    /* preload any backed up data */
    if (f->backlen > 0) {
        memcpy(in-f->backlen, f->backbuf, f->backlen);
        in -= f->backlen;
        len += f->backlen;
        prespace -= f->backlen;
        f->backlen = 0;
    }
    
    filterfunc(f, in, len, prespace, out, outlen, outprespace);

#ifdef MALLOC_CHECK
    checkmem(f->outreal);
    checkmem(f->backbuf);
#endif

}

void camel_mime_filter_filter(CamelMimeFilter *f,
                  char *in, size_t len, size_t prespace,
                  char **out, size_t *outlen, size_t *outprespace)
{
    if (FCLASS(f)->filter)
        filter_run(f, in, len, prespace, out, outlen, outprespace, FCLASS(f)->filter);
    else
        g_error("Filter function unplmenented in class");
}

void camel_mime_filter_complete(CamelMimeFilter *f,
                char *in, size_t len, size_t prespace,
                char **out, size_t *outlen, size_t *outprespace)
{
    if (FCLASS(f)->complete)
        filter_run(f, in, len, prespace, out, outlen, outprespace, FCLASS(f)->complete);
}

void camel_mime_filter_reset(CamelMimeFilter *f)
{
    if (FCLASS(f)->reset) {
        FCLASS(f)->reset(f);
    }

    /* could free some buffers, if they are really big? */
    f->backlen = 0;
}

/* sets number of bytes backed up on the input, new calls replace previous ones */
void camel_mime_filter_backup(CamelMimeFilter *f, const char *data, size_t length)
{
    if (f->backsize < length) {
        /* g_realloc copies data, unnecessary overhead */
        g_free(f->backbuf);
        f->backbuf = g_malloc(length+BACK_HEAD);
        f->backsize = length+BACK_HEAD;
    }
    f->backlen = length;
    memcpy(f->backbuf, data, length);
}

/* ensure this much size available for filter output (if required) */
void camel_mime_filter_set_size(CamelMimeFilter *f, size_t size, int keep)
{
    if (f->outsize < size) {
        int offset = f->outptr - f->outreal;
        if (keep) {
            f->outreal = g_realloc(f->outreal, size + PRE_HEAD*4);
        } else {
            g_free(f->outreal);
            f->outreal = g_malloc(size + PRE_HEAD*4);
        }
        f->outptr = f->outreal + offset;
        f->outbuf = f->outreal + PRE_HEAD*4;
        f->outsize = size;
        /* this could be offset from the end of the structure, but 
           this should be good enough */
        f->outpre = PRE_HEAD*4;
    }
}