~ubuntu-branches/ubuntu/feisty/clamav/feisty

« back to all changes in this revision

Viewing changes to libclamav/text.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-02-20 10:33:44 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20070220103344-zgcu2psnx9d98fpa
Tags: upstream-0.90
ImportĀ upstreamĀ versionĀ 0.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2007-2008 Sourcefire, Inc.
3
 
 *
4
 
 *  Authors: Nigel Horne
 
2
 *  Copyright (C) 2002 Nigel Horne <njh@bandsman.co.uk>
5
3
 *
6
4
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License version 2 as
8
 
 *  published by the Free Software Foundation.
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
9
8
 *
10
9
 *  This program is distributed in the hope that it will be useful,
11
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
92
91
#include "clamav-config.h"
93
92
#endif
94
93
 
 
94
#ifndef CL_DEBUG
 
95
#define NDEBUG  /* map CLAMAV debug onto standard */
 
96
#endif
 
97
 
95
98
#include <stdlib.h>
96
99
#ifdef  C_DARWIN
97
100
#include <sys/types.h>
113
116
#include "mbox.h"
114
117
 
115
118
static  text    *textCopy(const text *t_head);
116
 
static  text    *textAdd(text *t_head, const text *t);
117
119
static  void    addToFileblob(const line_t *line, void *arg);
118
120
static  void    getLength(const line_t *line, void *arg);
119
121
static  void    addToBlob(const line_t *line, void *arg);
131
133
        }
132
134
}
133
135
 
 
136
/*
 
137
 * Remove trailing spaces from the lines and trailing blank lines
 
138
 * This could be used to remove trailing blank lines, empty lines etc.,
 
139
 *      but it probably isn't worth the time taken given that it won't reclaim
 
140
 *      much memory
 
141
 */
 
142
text *
 
143
textClean(text *t_head)
 
144
{
 
145
        return t_head;
 
146
}
 
147
 
134
148
/* Clone the current object */
135
149
static text *
136
150
textCopy(const text *t_head)
166
180
}
167
181
 
168
182
/* Add a copy of a text to the end of the current object */
169
 
static text *
 
183
text *
170
184
textAdd(text *t_head, const text *t)
171
185
{
172
186
        text *ret;
173
187
        int count;
174
188
 
175
 
        if(t_head == NULL) {
176
 
                if(t == NULL) {
177
 
                        cli_errmsg("textAdd fails sanity check\n");
178
 
                        return NULL;
179
 
                }
 
189
        if(t_head == NULL)
180
190
                return textCopy(t);
181
 
        }
182
191
 
183
192
        if(t == NULL)
184
193
                return t_head;
225
234
        else {
226
235
                text *anotherText = messageToText(aMessage);
227
236
 
228
 
                if(aText)
229
 
                        return textMove(aText, anotherText);
 
237
                if(aText) {
 
238
                        aText = textAdd(aText, anotherText);
 
239
                        textDestroy(anotherText);
 
240
                        return aText;
 
241
                }
230
242
                return anotherText;
231
243
        }
232
244
}
233
245
 
234
246
/*
235
 
 * Put the contents of the given text at the end of the current object.
236
 
 * The given text emptied; it can be used again if needed, though be warned that
237
 
 * it will have an empty line at the start.
238
 
 */
239
 
text *
240
 
textMove(text *t_head, text *t)
241
 
{
242
 
        text *ret;
243
 
 
244
 
        if(t_head == NULL) {
245
 
                if(t == NULL) {
246
 
                        cli_errmsg("textMove fails sanity check\n");
247
 
                        return NULL;
248
 
                }
249
 
                t_head = (text *)cli_malloc(sizeof(text));
250
 
                if(t_head == NULL)
251
 
                        return NULL;
252
 
                t_head->t_line = t->t_line;
253
 
                t_head->t_next = t->t_next;
254
 
                t->t_line = NULL;
255
 
                t->t_next = NULL;
256
 
                return t_head;
257
 
        }
258
 
 
259
 
        if(t == NULL)
260
 
                return t_head;
261
 
 
262
 
        ret = t_head;
263
 
 
264
 
        while(t_head->t_next)
265
 
                t_head = t_head->t_next;
266
 
 
267
 
        /*
268
 
         * Move the first line manually so that the caller is left clean but
269
 
         * empty, the rest is moved by a simple pointer reassignment
270
 
         */
271
 
        t_head->t_next = (text *)cli_malloc(sizeof(text));
272
 
        if(t_head->t_next == NULL)
273
 
                return NULL;
274
 
        t_head = t_head->t_next;
275
 
 
276
 
        assert(t_head != NULL);
277
 
 
278
 
        if(t->t_line) {
279
 
                t_head->t_line = t->t_line;
280
 
                t->t_line = NULL;
281
 
        } else
282
 
                t_head->t_line = NULL;
283
 
 
284
 
        t_head->t_next = t->t_next;
285
 
        t->t_next = NULL;
286
 
 
287
 
        return ret;
288
 
}
289
 
 
290
 
/*
291
247
 * Transfer the contents of the text into a blob
292
248
 * The caller must free the returned blob if b is NULL
293
249
 */
417
373
static void *
418
374
textIterate(text *t_text, void (*cb)(const line_t *item, void *arg), void *arg, int destroy)
419
375
{
420
 
        /*
421
 
         * Have two loops rather than one, so that we're not checking the
422
 
         * value of "destroy" lots and lots of times
423
 
         */
424
 
#if     0
425
376
        while(t_text) {
426
377
                (*cb)(t_text->t_line, arg);
427
378
 
432
383
 
433
384
                t_text = t_text->t_next;
434
385
        }
435
 
#else
436
 
        if(destroy)
437
 
                while(t_text) {
438
 
                        (*cb)(t_text->t_line, arg);
439
 
 
440
 
                        if(t_text->t_line) {
441
 
                                lineUnlink(t_text->t_line);
442
 
                                t_text->t_line = NULL;
443
 
                        }
444
 
 
445
 
                        t_text = t_text->t_next;
446
 
                }
447
 
        else
448
 
                while(t_text) {
449
 
                        (*cb)(t_text->t_line, arg);
450
 
 
451
 
                        t_text = t_text->t_next;
452
 
                }
453
 
#endif
454
386
        return arg;
455
387
}